blob: e3a7305f004eae82e235d3543e56d34624e4ba51 [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"
Chris Lattnerc3aae252005-01-07 07:46:32 +000017#include "llvm/Constants.h"
Dan Gohmancc751bb2010-05-07 01:09:21 +000018#include "llvm/Analysis/DebugInfo.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000019#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000020#include "llvm/Function.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000021#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000022#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000023#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000025#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000026#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000027#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 Anderson825b72b2009-08-11 20:47:22 +000065 case MVT::f32: return &APFloat::IEEEsingle;
66 case MVT::f64: return &APFloat::IEEEdouble;
67 case MVT::f80: return &APFloat::x87DoubleExtended;
68 case MVT::f128: return &APFloat::IEEEquad;
69 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerec4a5672008-03-05 06:48:13 +000070 }
71}
72
NAKAMURA Takumib99ea7c2012-03-16 00:01:55 +000073SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000074
Jim Laskey58b968b2005-08-17 20:08:02 +000075//===----------------------------------------------------------------------===//
76// ConstantFPSDNode Class
77//===----------------------------------------------------------------------===//
78
79/// isExactlyValue - We don't rely on operator== working on double values, as
80/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
81/// As such, this method can be used to do an exact bit-for-bit comparison of
82/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000083bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000084 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000085}
86
Owen Andersone50ed302009-08-10 22:56:29 +000087bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000088 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000089 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000090
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000091 // PPC long double cannot be converted to any other type.
Owen Anderson825b72b2009-08-11 20:47:22 +000092 if (VT == MVT::ppcf128 ||
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000093 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000094 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000095
Dale Johannesenf04afdb2007-08-30 00:23:21 +000096 // convert modifies in place, so make a copy.
97 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000098 bool losesInfo;
Owen Andersone50ed302009-08-10 22:56:29 +000099 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +0000100 &losesInfo);
101 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000102}
103
Jim Laskey58b968b2005-08-17 20:08:02 +0000104//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +0000105// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +0000106//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +0000107
Evan Chenga8df1662006-03-27 06:58:47 +0000108/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +0000109/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000110bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000111 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000112 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000113 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000114
Evan Chenga8df1662006-03-27 06:58:47 +0000115 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
Chris Lattner61d43992006-03-25 22:57:01 +0000117 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Chris Lattner61d43992006-03-25 22:57:01 +0000119 // Skip over all of the undef values.
120 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
121 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Chris Lattner61d43992006-03-25 22:57:01 +0000123 // Do not accept an all-undef vector.
124 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000125
Chris Lattner61d43992006-03-25 22:57:01 +0000126 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000127 // elements. We have to be a bit careful here, as the type of the constant
128 // may not be the same as the type of the vector elements due to type
129 // legalization (the elements are promoted to a legal type for the target and
130 // a vector of a type may be legal when the base element type is not).
131 // We only want to check enough bits to cover the vector elements, because
132 // we care if the resultant vector is all ones, not whether the individual
133 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000134 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000135 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Evan Chenga8df1662006-03-27 06:58:47 +0000136 if (isa<ConstantSDNode>(NotZero)) {
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000137 if (cast<ConstantSDNode>(NotZero)->getAPIntValue().countTrailingOnes() <
138 EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000139 return false;
140 } else if (isa<ConstantFPSDNode>(NotZero)) {
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000141 if (cast<ConstantFPSDNode>(NotZero)->getValueAPF()
142 .bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000143 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000144 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000145 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000146
Chris Lattner61d43992006-03-25 22:57:01 +0000147 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000148 // undefs. Even with the above element type twiddling, this should be OK, as
149 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000150 for (++i; i != e; ++i)
151 if (N->getOperand(i) != NotZero &&
152 N->getOperand(i).getOpcode() != ISD::UNDEF)
153 return false;
154 return true;
155}
156
157
Evan Cheng4a147842006-03-26 09:50:58 +0000158/// isBuildVectorAllZeros - Return true if the specified node is a
159/// BUILD_VECTOR where all of the elements are 0 or undef.
160bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000161 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000162 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000163 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000164
Evan Cheng4a147842006-03-26 09:50:58 +0000165 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000166
Evan Chenga8df1662006-03-27 06:58:47 +0000167 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000168
Evan Chenga8df1662006-03-27 06:58:47 +0000169 // Skip over all of the undef values.
170 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
171 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000172
Evan Chenga8df1662006-03-27 06:58:47 +0000173 // Do not accept an all-undef vector.
174 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000175
Dan Gohman68f32cb2009-06-04 16:49:15 +0000176 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000177 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000178 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000179 if (isa<ConstantSDNode>(Zero)) {
180 if (!cast<ConstantSDNode>(Zero)->isNullValue())
181 return false;
182 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000183 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000184 return false;
185 } else
186 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000187
Dan Gohman68f32cb2009-06-04 16:49:15 +0000188 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000189 // undefs.
190 for (++i; i != e; ++i)
191 if (N->getOperand(i) != Zero &&
192 N->getOperand(i).getOpcode() != ISD::UNDEF)
193 return false;
194 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000195}
196
Evan Chengefec7512008-02-18 23:04:32 +0000197/// isScalarToVector - Return true if the specified node is a
198/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
199/// element is not an undef.
200bool ISD::isScalarToVector(const SDNode *N) {
201 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
202 return true;
203
204 if (N->getOpcode() != ISD::BUILD_VECTOR)
205 return false;
206 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
207 return false;
208 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000209 if (NumElems == 1)
210 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000211 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000212 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000213 if (V.getOpcode() != ISD::UNDEF)
214 return false;
215 }
216 return true;
217}
218
Chris Lattnerc3aae252005-01-07 07:46:32 +0000219/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
220/// when given the operation for (X op Y).
221ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
222 // To perform this operation, we just need to swap the L and G bits of the
223 // operation.
224 unsigned OldL = (Operation >> 2) & 1;
225 unsigned OldG = (Operation >> 1) & 1;
226 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
227 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000228 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000229}
230
231/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
232/// 'op' is a valid SetCC operation.
233ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
234 unsigned Operation = Op;
235 if (isInteger)
236 Operation ^= 7; // Flip L, G, E bits, but not U.
237 else
238 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000239
Chris Lattnerc3aae252005-01-07 07:46:32 +0000240 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000241 Operation &= ~8; // Don't let N and U bits get set.
242
Chris Lattnerc3aae252005-01-07 07:46:32 +0000243 return ISD::CondCode(Operation);
244}
245
246
247/// isSignedOp - For an integer comparison, return 1 if the comparison is a
248/// signed operation and 2 if the result is an unsigned comparison. Return zero
249/// if the operation does not depend on the sign of the input (setne and seteq).
250static int isSignedOp(ISD::CondCode Opcode) {
251 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000252 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000253 case ISD::SETEQ:
254 case ISD::SETNE: return 0;
255 case ISD::SETLT:
256 case ISD::SETLE:
257 case ISD::SETGT:
258 case ISD::SETGE: return 1;
259 case ISD::SETULT:
260 case ISD::SETULE:
261 case ISD::SETUGT:
262 case ISD::SETUGE: return 2;
263 }
264}
265
266/// getSetCCOrOperation - Return the result of a logical OR between different
267/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
268/// returns SETCC_INVALID if it is not possible to represent the resultant
269/// comparison.
270ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
271 bool isInteger) {
272 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
273 // Cannot fold a signed integer setcc with an unsigned integer setcc.
274 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000275
Chris Lattnerc3aae252005-01-07 07:46:32 +0000276 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278 // If the N and U bits get set then the resultant comparison DOES suddenly
279 // care about orderedness, and is true when ordered.
280 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000281 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000282
Chris Lattnere41102b2006-05-12 17:03:46 +0000283 // Canonicalize illegal integer setcc's.
284 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
285 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000286
Chris Lattnerc3aae252005-01-07 07:46:32 +0000287 return ISD::CondCode(Op);
288}
289
290/// getSetCCAndOperation - Return the result of a logical AND between different
291/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
292/// function returns zero if it is not possible to represent the resultant
293/// comparison.
294ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
295 bool isInteger) {
296 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
297 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000298 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000299
300 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000301 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000302
Chris Lattnera83385f2006-04-27 05:01:07 +0000303 // Canonicalize illegal integer setcc's.
304 if (isInteger) {
305 switch (Result) {
306 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000307 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000308 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000309 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
310 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
311 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000312 }
313 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000314
Chris Lattnera83385f2006-04-27 05:01:07 +0000315 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000316}
317
Jim Laskey58b968b2005-08-17 20:08:02 +0000318//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000319// SDNode Profile Support
320//===----------------------------------------------------------------------===//
321
Jim Laskeydef69b92006-10-27 23:52:51 +0000322/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
323///
Jim Laskey583bd472006-10-27 23:46:08 +0000324static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
325 ID.AddInteger(OpC);
326}
327
328/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
329/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000330static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000331 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000332}
333
Jim Laskeydef69b92006-10-27 23:52:51 +0000334/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
335///
Jim Laskey583bd472006-10-27 23:46:08 +0000336static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000337 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000338 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000339 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000340 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000341 }
Jim Laskey583bd472006-10-27 23:46:08 +0000342}
343
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000344/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
345///
346static void AddNodeIDOperands(FoldingSetNodeID &ID,
347 const SDUse *Ops, unsigned NumOps) {
348 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000349 ID.AddPointer(Ops->getNode());
350 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000351 }
352}
353
Jim Laskey583bd472006-10-27 23:46:08 +0000354static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000355 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000356 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000357 AddNodeIDOpcode(ID, OpC);
358 AddNodeIDValueTypes(ID, VTList);
359 AddNodeIDOperands(ID, OpList, N);
360}
361
Duncan Sands0dc40452008-10-27 15:30:53 +0000362/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
363/// the NodeID data.
364static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000365 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000366 case ISD::TargetExternalSymbol:
367 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000368 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000369 default: break; // Normal nodes don't need extra info.
370 case ISD::TargetConstant:
371 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000372 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 break;
374 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000375 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000376 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000377 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000378 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000379 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000380 case ISD::GlobalAddress:
381 case ISD::TargetGlobalTLSAddress:
382 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000383 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000384 ID.AddPointer(GA->getGlobal());
385 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000386 ID.AddInteger(GA->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000387 break;
388 }
389 case ISD::BasicBlock:
390 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
391 break;
392 case ISD::Register:
393 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
394 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000395 case ISD::RegisterMask:
396 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
397 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000398 case ISD::SRCVALUE:
399 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
400 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000401 case ISD::FrameIndex:
402 case ISD::TargetFrameIndex:
403 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
404 break;
405 case ISD::JumpTable:
406 case ISD::TargetJumpTable:
407 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000408 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000409 break;
410 case ISD::ConstantPool:
411 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000412 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000413 ID.AddInteger(CP->getAlignment());
414 ID.AddInteger(CP->getOffset());
415 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000416 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000417 else
418 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000419 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 break;
421 }
422 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000423 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000424 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000425 ID.AddInteger(LD->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000426 break;
427 }
428 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000429 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000430 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000431 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 break;
433 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000434 case ISD::ATOMIC_CMP_SWAP:
435 case ISD::ATOMIC_SWAP:
436 case ISD::ATOMIC_LOAD_ADD:
437 case ISD::ATOMIC_LOAD_SUB:
438 case ISD::ATOMIC_LOAD_AND:
439 case ISD::ATOMIC_LOAD_OR:
440 case ISD::ATOMIC_LOAD_XOR:
441 case ISD::ATOMIC_LOAD_NAND:
442 case ISD::ATOMIC_LOAD_MIN:
443 case ISD::ATOMIC_LOAD_MAX:
444 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000445 case ISD::ATOMIC_LOAD_UMAX:
446 case ISD::ATOMIC_LOAD:
447 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000448 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000449 ID.AddInteger(AT->getMemoryVT().getRawBits());
450 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang28873102008-06-25 08:15:39 +0000451 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000452 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000453 case ISD::VECTOR_SHUFFLE: {
454 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000455 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000456 i != e; ++i)
457 ID.AddInteger(SVN->getMaskElt(i));
458 break;
459 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000460 case ISD::TargetBlockAddress:
461 case ISD::BlockAddress: {
Dan Gohman29cbade2009-11-20 23:18:13 +0000462 ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
463 ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000464 break;
465 }
Mon P Wang28873102008-06-25 08:15:39 +0000466 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000467}
468
Duncan Sands0dc40452008-10-27 15:30:53 +0000469/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
470/// data.
471static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
472 AddNodeIDOpcode(ID, N->getOpcode());
473 // Add the return value info.
474 AddNodeIDValueTypes(ID, N->getVTList());
475 // Add the operand info.
476 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
477
478 // Handle SDNode leafs with special info.
479 AddNodeIDCustom(ID, N);
480}
481
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000482/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000483/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000484/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000485///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000486static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000487encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000488 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000489 assert((ConvType & 3) == ConvType &&
490 "ConvType may not require more than 2 bits!");
491 assert((AM & 7) == AM &&
492 "AM may not require more than 3 bits!");
493 return ConvType |
494 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000495 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000496 (isNonTemporal << 6) |
497 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000498}
499
Jim Laskey583bd472006-10-27 23:46:08 +0000500//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000501// SelectionDAG Class
502//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000503
Duncan Sands0dc40452008-10-27 15:30:53 +0000504/// doNotCSE - Return true if CSE should not be performed for this node.
505static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000506 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000507 return true; // Never CSE anything that produces a flag.
508
509 switch (N->getOpcode()) {
510 default: break;
511 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000512 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000513 return true; // Never CSE these nodes.
514 }
515
516 // Check that remaining values produced are not flags.
517 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000518 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000519 return true; // Never CSE anything that produces a flag.
520
521 return false;
522}
523
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000524/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000525/// SelectionDAG.
526void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000527 // Create a dummy node (which is not added to allnodes), that adds a reference
528 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000529 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000530
Chris Lattner190a4182006-08-04 17:45:20 +0000531 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000532
Chris Lattner190a4182006-08-04 17:45:20 +0000533 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000534 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000535 if (I->use_empty())
536 DeadNodes.push_back(I);
537
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000538 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000539
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000540 // If the root changed (e.g. it was a dead load, update the root).
541 setRoot(Dummy.getValue());
542}
543
544/// RemoveDeadNodes - This method deletes the unreachable nodes in the
545/// given list, and any nodes that become unreachable as a result.
546void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
547 DAGUpdateListener *UpdateListener) {
548
Chris Lattner190a4182006-08-04 17:45:20 +0000549 // Process the worklist, deleting the nodes and adding their uses to the
550 // worklist.
551 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000552 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000553
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000554 if (UpdateListener)
555 UpdateListener->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000556
Chris Lattner190a4182006-08-04 17:45:20 +0000557 // Take the node out of the appropriate CSE map.
558 RemoveNodeFromCSEMaps(N);
559
560 // Next, brutally remove the operand list. This is safe to do, as there are
561 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000562 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
563 SDUse &Use = *I++;
564 SDNode *Operand = Use.getNode();
565 Use.set(SDValue());
566
Chris Lattner190a4182006-08-04 17:45:20 +0000567 // Now that we removed this operand, see if there are no uses of it left.
568 if (Operand->use_empty())
569 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000570 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000571
Dan Gohmanc5336122009-01-19 22:39:36 +0000572 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000573 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000574}
575
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000576void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000577 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000578
579 // Create a dummy node that adds a reference to the root node, preventing
580 // it from being deleted. (This matters if the root is an operand of the
581 // dead node.)
582 HandleSDNode Dummy(getRoot());
583
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000584 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000585}
586
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000587void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000588 // First take this out of the appropriate CSE map.
589 RemoveNodeFromCSEMaps(N);
590
Scott Michelfdc40a02009-02-17 22:15:04 +0000591 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000592 // AllNodes list, and delete the node.
593 DeleteNodeNotInCSEMaps(N);
594}
595
596void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000597 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
598 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000599
Dan Gohmanf06c8352008-09-30 18:30:35 +0000600 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000601 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000602
Dan Gohmanc5336122009-01-19 22:39:36 +0000603 DeallocateNode(N);
604}
605
606void SelectionDAG::DeallocateNode(SDNode *N) {
607 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000608 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000609
Dan Gohmanc5336122009-01-19 22:39:36 +0000610 // Set the opcode to DELETED_NODE to help catch bugs when node
611 // memory is reallocated.
612 N->NodeType = ISD::DELETED_NODE;
613
Dan Gohman11467282008-08-26 01:44:34 +0000614 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000615
616 // Remove the ordering of this node.
Bill Wendling187361b2010-01-23 10:26:57 +0000617 Ordering->remove(N);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000618
Evan Chengbfcb3052010-03-25 01:38:16 +0000619 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000620 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000621 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
622 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000623}
624
Chris Lattner149c58c2005-08-16 18:17:10 +0000625/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
626/// correspond to it. This is useful when we're about to delete or repurpose
627/// the node. We don't want future request for structurally identical nodes
628/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000629bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000630 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000631 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000632 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000633 case ISD::CONDCODE:
634 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
635 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000636 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000637 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
638 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000639 case ISD::ExternalSymbol:
640 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000641 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000642 case ISD::TargetExternalSymbol: {
643 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
644 Erased = TargetExternalSymbols.erase(
645 std::pair<std::string,unsigned char>(ESN->getSymbol(),
646 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000647 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000648 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000649 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000650 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000651 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000652 Erased = ExtendedValueTypeNodes.erase(VT);
653 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
655 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000656 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000657 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000658 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000659 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000660 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000661 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
662 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000663 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000664 break;
665 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000666#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000667 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000668 // flag result (which cannot be CSE'd) or is one of the special cases that are
669 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000670 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000671 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000672 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000673 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000674 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000675 }
676#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000677 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000678}
679
Dan Gohman39946102009-01-25 16:29:12 +0000680/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
681/// maps and modified in place. Add it back to the CSE maps, unless an identical
682/// node already exists, in which case transfer all its users to the existing
683/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000684///
Dan Gohman39946102009-01-25 16:29:12 +0000685void
686SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
687 DAGUpdateListener *UpdateListener) {
688 // For node types that aren't CSE'd, just act as if no identical node
689 // already exists.
690 if (!doNotCSE(N)) {
691 SDNode *Existing = CSEMap.GetOrInsertNode(N);
692 if (Existing != N) {
693 // If there was already an existing matching node, use ReplaceAllUsesWith
694 // to replace the dead one with the existing one. This can cause
695 // recursive merging of other unrelated nodes down the line.
696 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000697
Dan Gohman39946102009-01-25 16:29:12 +0000698 // N is now dead. Inform the listener if it exists and delete it.
Scott Michelfdc40a02009-02-17 22:15:04 +0000699 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000700 UpdateListener->NodeDeleted(N, Existing);
701 DeleteNodeNotInCSEMaps(N);
702 return;
703 }
704 }
Evan Cheng71e86852008-07-08 20:06:39 +0000705
Dan Gohman39946102009-01-25 16:29:12 +0000706 // If the node doesn't already exist, we updated it. Inform a listener if
707 // it exists.
Scott Michelfdc40a02009-02-17 22:15:04 +0000708 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000709 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000710}
711
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000712/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000713/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000714/// return null, otherwise return a pointer to the slot it would take. If a
715/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000716SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000717 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000718 if (doNotCSE(N))
719 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000720
Dan Gohman475871a2008-07-27 21:46:04 +0000721 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000722 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000723 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000724 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000725 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000726 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000727}
728
729/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000730/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000731/// return null, otherwise return a pointer to the slot it would take. If a
732/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000733SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000734 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000735 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000736 if (doNotCSE(N))
737 return 0;
738
Dan Gohman475871a2008-07-27 21:46:04 +0000739 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000740 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000741 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000742 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000743 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000744 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000745}
746
747
748/// 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 Lattnera5682852006-08-07 23:03:03 +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.
Scott Michelfdc40a02009-02-17 22:15:04 +0000752SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000753 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000754 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000755 if (doNotCSE(N))
756 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000757
Jim Laskey583bd472006-10-27 23:46:08 +0000758 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000759 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
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}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000764
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000765#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000766/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
767static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000768 switch (N->getOpcode()) {
769 default:
770 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000771 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000772 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000773 assert(N->getNumValues() == 1 && "Too many results!");
774 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
775 "Wrong return type!");
776 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
777 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
778 "Mismatched operand types!");
779 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
780 "Wrong operand type!");
781 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
782 "Wrong return type size");
783 break;
784 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000785 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000786 assert(N->getNumValues() == 1 && "Too many results!");
787 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000788 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000789 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000790 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000791 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000792 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000793 (EltVT.isInteger() && I->getValueType().isInteger() &&
794 EltVT.bitsLE(I->getValueType()))) &&
795 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000796 assert(I->getValueType() == N->getOperand(0).getValueType() &&
797 "Operands must all have the same type");
798 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000799 break;
800 }
801 }
802}
803
Duncan Sands59d2dad2010-11-20 11:25:00 +0000804/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
805static void VerifySDNode(SDNode *N) {
806 // The SDNode allocators cannot be used to allocate nodes with fields that are
807 // not present in an SDNode!
808 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
809 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
810 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
811 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
812 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
813 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
814 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
815 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
816 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
817 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
818 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
819 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
820 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
821 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
822 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
823 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
824 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
825 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
826 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
827
828 VerifyNodeCommon(N);
829}
830
831/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
832/// invalid.
833static void VerifyMachineNode(SDNode *N) {
834 // The MachineNode allocators cannot be used to allocate nodes with fields
835 // that are not present in a MachineNode!
836 // Currently there are no such nodes.
837
838 VerifyNodeCommon(N);
839}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000840#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000841
Owen Andersone50ed302009-08-10 22:56:29 +0000842/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000843/// given type.
844///
Owen Andersone50ed302009-08-10 22:56:29 +0000845unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000846 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000847 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000848 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000849
850 return TLI.getTargetData()->getABITypeAlignment(Ty);
851}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000852
Dale Johannesen92570c42009-02-07 02:15:05 +0000853// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000854SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Dan Gohmanff7a5622010-05-11 17:31:57 +0000855 : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
Devang Patel0508d042011-12-15 18:21:18 +0000856 OptLevel(OL), EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
Daniel Dunbar819309e2009-12-16 20:10:05 +0000857 Root(getEntryNode()), Ordering(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000858 AllNodes.push_back(&EntryNode);
Bill Wendling187361b2010-01-23 10:26:57 +0000859 Ordering = new SDNodeOrdering();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000860 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000861}
862
Chris Lattnerde6e7832010-04-05 06:10:13 +0000863void SelectionDAG::init(MachineFunction &mf) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000864 MF = &mf;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000865 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000866}
867
Chris Lattner78ec3112003-08-11 14:57:33 +0000868SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000869 allnodes_clear();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000870 delete Ordering;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000871 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000872}
873
874void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000875 assert(&*AllNodes.begin() == &EntryNode);
876 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000877 while (!AllNodes.empty())
878 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000879}
880
Dan Gohman7c3234c2008-08-27 23:52:12 +0000881void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000882 allnodes_clear();
883 OperandAllocator.Reset();
884 CSEMap.clear();
885
886 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000887 ExternalSymbols.clear();
888 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000889 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
890 static_cast<CondCodeSDNode*>(0));
891 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
892 static_cast<SDNode*>(0));
893
Dan Gohmane7852d02009-01-26 04:35:06 +0000894 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000895 AllNodes.push_back(&EntryNode);
896 Root = getEntryNode();
Dan Gohman27445f02010-06-18 15:40:58 +0000897 Ordering->clear();
Evan Cheng31441b72010-03-29 20:48:30 +0000898 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000899}
900
Nadav Rotema3c42f32011-09-27 11:16:47 +0000901SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
902 return VT.bitsGT(Op.getValueType()) ?
903 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
904 getNode(ISD::TRUNCATE, DL, VT, Op);
905}
906
Duncan Sands3a66a682009-10-13 21:04:12 +0000907SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
908 return VT.bitsGT(Op.getValueType()) ?
909 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
910 getNode(ISD::TRUNCATE, DL, VT, Op);
911}
912
913SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
914 return VT.bitsGT(Op.getValueType()) ?
915 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
916 getNode(ISD::TRUNCATE, DL, VT, Op);
917}
918
Owen Andersone50ed302009-08-10 22:56:29 +0000919SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000920 assert(!VT.isVector() &&
921 "getZeroExtendInReg should use the vector element type instead of "
922 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000923 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000924 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
925 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000926 VT.getSizeInBits());
927 return getNode(ISD::AND, DL, Op.getValueType(), Op,
928 getConstant(Imm, Op.getValueType()));
929}
930
Bob Wilson4c245462009-01-22 17:39:32 +0000931/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
932///
Owen Andersone50ed302009-08-10 22:56:29 +0000933SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000934 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000935 SDValue NegOne =
936 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000937 return getNode(ISD::XOR, DL, VT, Val, NegOne);
938}
939
Owen Andersone50ed302009-08-10 22:56:29 +0000940SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000941 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000942 assert((EltVT.getSizeInBits() >= 64 ||
943 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
944 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000945 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000946}
947
Owen Andersone50ed302009-08-10 22:56:29 +0000948SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000949 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000950}
951
Owen Andersone50ed302009-08-10 22:56:29 +0000952SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000953 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000954
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000955 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000956 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000957
Duncan Sands28b77e92011-09-06 19:07:46 +0000958 // In some cases the vector type is legal but the element type is illegal and
959 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
960 // inserted value (the type does not need to match the vector element type).
961 // Any extra bits introduced will be truncated away.
962 if (VT.isVector() && TLI.getTypeAction(*getContext(), EltVT) ==
963 TargetLowering::TypePromoteInteger) {
964 EltVT = TLI.getTypeToTransformTo(*getContext(), EltVT);
965 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
966 Elt = ConstantInt::get(*getContext(), NewVal);
967 }
968
969 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
970 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000971 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000972 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000973 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +0000974 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +0000975 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000976 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000977 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000978 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000979 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000980
Dan Gohman89081322007-12-12 22:21:26 +0000981 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +0000982 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000983 CSEMap.InsertNode(N, IP);
984 AllNodes.push_back(N);
985 }
986
Dan Gohman475871a2008-07-27 21:46:04 +0000987 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000988 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000989 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000990 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000991 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +0000992 }
993 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000994}
995
Dan Gohman475871a2008-07-27 21:46:04 +0000996SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000997 return getConstant(Val, TLI.getPointerTy(), isTarget);
998}
999
1000
Owen Andersone50ed302009-08-10 22:56:29 +00001001SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001002 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001003}
1004
Owen Andersone50ed302009-08-10 22:56:29 +00001005SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001006 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001007
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001008 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001009
Chris Lattnerd8658612005-02-17 20:17:32 +00001010 // Do the map lookup using the actual bit pattern for the floating point
1011 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1012 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001013 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001014 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001015 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001016 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001017 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001018 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001019 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001020 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001021 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001022
Evan Chengc908dcd2007-06-29 21:36:04 +00001023 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001024 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001025 CSEMap.InsertNode(N, IP);
1026 AllNodes.push_back(N);
1027 }
1028
Dan Gohman475871a2008-07-27 21:46:04 +00001029 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001030 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001031 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001032 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +00001033 // FIXME DebugLoc info might be appropriate here
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001034 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001035 }
1036 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001037}
1038
Owen Andersone50ed302009-08-10 22:56:29 +00001039SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001040 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001042 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001043 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001044 return getConstantFP(APFloat(Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001045 else if (EltVT==MVT::f80 || EltVT==MVT::f128) {
1046 bool ignored;
1047 APFloat apf = APFloat(Val);
1048 apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1049 &ignored);
1050 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001051 } else
1052 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001053}
1054
Devang Patel0d881da2010-07-06 22:08:15 +00001055SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001056 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001057 bool isTargetGA,
1058 unsigned char TargetFlags) {
1059 assert((TargetFlags == 0 || isTargetGA) &&
1060 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001061
Dan Gohman6520e202008-10-18 02:06:02 +00001062 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersone50ed302009-08-10 22:56:29 +00001063 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001064 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001065 if (BitWidth < 64)
1066 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
1067
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001068 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1069 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001070 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001071 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001072 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001073 }
1074
Chris Lattner2a4ed822009-06-25 21:21:14 +00001075 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001076 if (GVar && GVar->isThreadLocal())
1077 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1078 else
1079 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001080
Jim Laskey583bd472006-10-27 23:46:08 +00001081 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001082 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001083 ID.AddPointer(GV);
1084 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001085 ID.AddInteger(TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001086 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001087 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001088 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001089
Devang Patel0d881da2010-07-06 22:08:15 +00001090 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001091 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001092 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001094 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095}
1096
Owen Andersone50ed302009-08-10 22:56:29 +00001097SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001098 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001099 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001100 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001101 ID.AddInteger(FI);
1102 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001103 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001104 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001105
Dan Gohman95531882010-03-18 18:49:47 +00001106 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001107 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001108 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001109 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001110}
1111
Owen Andersone50ed302009-08-10 22:56:29 +00001112SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001113 unsigned char TargetFlags) {
1114 assert((TargetFlags == 0 || isTarget) &&
1115 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001116 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001117 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001118 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001119 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001120 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001121 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001122 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001123 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001124
Dan Gohman95531882010-03-18 18:49:47 +00001125 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1126 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001127 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001128 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001130}
1131
Dan Gohman46510a72010-04-15 01:51:59 +00001132SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001133 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001134 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001135 unsigned char TargetFlags) {
1136 assert((TargetFlags == 0 || isTarget) &&
1137 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001138 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001139 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001140 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001141 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001142 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001143 ID.AddInteger(Alignment);
1144 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001145 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001146 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001147 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001148 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001149 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001150
Dan Gohman95531882010-03-18 18:49:47 +00001151 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1152 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001153 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001154 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001155 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001156}
1157
Chris Lattnerc3aae252005-01-07 07:46:32 +00001158
Owen Andersone50ed302009-08-10 22:56:29 +00001159SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001160 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001161 bool isTarget,
1162 unsigned char TargetFlags) {
1163 assert((TargetFlags == 0 || isTarget) &&
1164 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001165 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001166 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001167 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001168 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001169 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001170 ID.AddInteger(Alignment);
1171 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001172 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001173 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001174 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001175 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001176 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001177
Dan Gohman95531882010-03-18 18:49:47 +00001178 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1179 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001180 CSEMap.InsertNode(N, IP);
1181 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001182 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001183}
1184
Dan Gohman475871a2008-07-27 21:46:04 +00001185SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001186 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001187 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001188 ID.AddPointer(MBB);
1189 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001190 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001191 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001192
Dan Gohman95531882010-03-18 18:49:47 +00001193 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001194 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001196 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197}
1198
Owen Andersone50ed302009-08-10 22:56:29 +00001199SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001200 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1201 ValueTypeNodes.size())
1202 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001203
Duncan Sands83ec4b62008-06-06 12:08:01 +00001204 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001205 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001206
Dan Gohman475871a2008-07-27 21:46:04 +00001207 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001208 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001209 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001210 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001211}
1212
Owen Andersone50ed302009-08-10 22:56:29 +00001213SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001214 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001215 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001216 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001217 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001218 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001219}
1220
Owen Andersone50ed302009-08-10 22:56:29 +00001221SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001222 unsigned char TargetFlags) {
1223 SDNode *&N =
1224 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1225 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001226 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001227 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001229 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230}
1231
Dan Gohman475871a2008-07-27 21:46:04 +00001232SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001233 if ((unsigned)Cond >= CondCodeNodes.size())
1234 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001235
Chris Lattner079a27a2005-08-09 20:40:02 +00001236 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001237 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001238 CondCodeNodes[Cond] = N;
1239 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001240 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001241
Dan Gohman475871a2008-07-27 21:46:04 +00001242 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001243}
1244
Nate Begeman5a5ca152009-04-29 05:20:52 +00001245// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1246// the shuffle mask M that point at N1 to point at N2, and indices that point
1247// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001248static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1249 std::swap(N1, N2);
1250 int NElts = M.size();
1251 for (int i = 0; i != NElts; ++i) {
1252 if (M[i] >= NElts)
1253 M[i] -= NElts;
1254 else if (M[i] >= 0)
1255 M[i] += NElts;
1256 }
1257}
1258
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001259SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001260 SDValue N2, const int *Mask) {
1261 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001262 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001263 "Vector Shuffle VTs must be a vectors");
1264 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1265 && "Vector Shuffle VTs must have same element type");
1266
1267 // Canonicalize shuffle undef, undef -> undef
1268 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001269 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001270
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001271 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001272 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001273 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001274 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001275 for (unsigned i = 0; i != NElts; ++i) {
1276 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001277 MaskVec.push_back(Mask[i]);
1278 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001279
Nate Begeman9008ca62009-04-27 18:41:29 +00001280 // Canonicalize shuffle v, v -> v, undef
1281 if (N1 == N2) {
1282 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001283 for (unsigned i = 0; i != NElts; ++i)
1284 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001285 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001286
Nate Begeman9008ca62009-04-27 18:41:29 +00001287 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1288 if (N1.getOpcode() == ISD::UNDEF)
1289 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001290
Nate Begeman9008ca62009-04-27 18:41:29 +00001291 // Canonicalize all index into lhs, -> shuffle lhs, undef
1292 // Canonicalize all index into rhs, -> shuffle rhs, undef
1293 bool AllLHS = true, AllRHS = true;
1294 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001295 for (unsigned i = 0; i != NElts; ++i) {
1296 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001297 if (N2Undef)
1298 MaskVec[i] = -1;
1299 else
1300 AllLHS = false;
1301 } else if (MaskVec[i] >= 0) {
1302 AllRHS = false;
1303 }
1304 }
1305 if (AllLHS && AllRHS)
1306 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001307 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001308 N2 = getUNDEF(VT);
1309 if (AllRHS) {
1310 N1 = getUNDEF(VT);
1311 commuteShuffle(N1, N2, MaskVec);
1312 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001313
Nate Begeman9008ca62009-04-27 18:41:29 +00001314 // If Identity shuffle, or all shuffle in to undef, return that node.
1315 bool AllUndef = true;
1316 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001317 for (unsigned i = 0; i != NElts; ++i) {
1318 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001319 if (MaskVec[i] >= 0) AllUndef = false;
1320 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001321 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001322 return N1;
1323 if (AllUndef)
1324 return getUNDEF(VT);
1325
1326 FoldingSetNodeID ID;
1327 SDValue Ops[2] = { N1, N2 };
1328 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001329 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001330 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001331
Nate Begeman9008ca62009-04-27 18:41:29 +00001332 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001333 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001334 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001335
Nate Begeman9008ca62009-04-27 18:41:29 +00001336 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1337 // SDNode doesn't have access to it. This memory will be "leaked" when
1338 // the node is deallocated, but recovered when the NodeAllocator is released.
1339 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1340 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001341
Dan Gohman95531882010-03-18 18:49:47 +00001342 ShuffleVectorSDNode *N =
1343 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001344 CSEMap.InsertNode(N, IP);
1345 AllNodes.push_back(N);
1346 return SDValue(N, 0);
1347}
1348
Owen Andersone50ed302009-08-10 22:56:29 +00001349SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001350 SDValue Val, SDValue DTy,
1351 SDValue STy, SDValue Rnd, SDValue Sat,
1352 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001353 // If the src and dest types are the same and the conversion is between
1354 // integer types of the same sign or two floats, no conversion is necessary.
1355 if (DTy == STy &&
1356 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001357 return Val;
1358
1359 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001360 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1361 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001362 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001363 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001364 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001365
Dan Gohman95531882010-03-18 18:49:47 +00001366 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1367 Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001368 CSEMap.InsertNode(N, IP);
1369 AllNodes.push_back(N);
1370 return SDValue(N, 0);
1371}
1372
Owen Andersone50ed302009-08-10 22:56:29 +00001373SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001374 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001375 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001376 ID.AddInteger(RegNo);
1377 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001378 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001379 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001380
Dan Gohman95531882010-03-18 18:49:47 +00001381 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001382 CSEMap.InsertNode(N, IP);
1383 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001384 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001385}
1386
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001387SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1388 FoldingSetNodeID ID;
1389 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1390 ID.AddPointer(RegMask);
1391 void *IP = 0;
1392 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1393 return SDValue(E, 0);
1394
1395 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1396 CSEMap.InsertNode(N, IP);
1397 AllNodes.push_back(N);
1398 return SDValue(N, 0);
1399}
1400
Chris Lattner7561d482010-03-14 02:33:54 +00001401SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001402 FoldingSetNodeID ID;
1403 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001404 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1405 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001406 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001407 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001408 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409
Dan Gohman95531882010-03-18 18:49:47 +00001410 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001411 CSEMap.InsertNode(N, IP);
1412 AllNodes.push_back(N);
1413 return SDValue(N, 0);
1414}
1415
Chris Lattner7561d482010-03-14 02:33:54 +00001416
Dan Gohman46510a72010-04-15 01:51:59 +00001417SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Dan Gohman29cbade2009-11-20 23:18:13 +00001418 bool isTarget,
1419 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001420 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1421
1422 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001423 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001424 ID.AddPointer(BA);
Dan Gohman29cbade2009-11-20 23:18:13 +00001425 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001426 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001427 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001428 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001429
Dan Gohman95531882010-03-18 18:49:47 +00001430 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001431 CSEMap.InsertNode(N, IP);
1432 AllNodes.push_back(N);
1433 return SDValue(N, 0);
1434}
1435
Dan Gohman475871a2008-07-27 21:46:04 +00001436SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001437 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001438 "SrcValue is not a pointer?");
1439
Jim Laskey583bd472006-10-27 23:46:08 +00001440 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001441 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001442 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001443
Chris Lattner61b09412006-08-11 21:01:22 +00001444 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001445 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001446 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001447
Dan Gohman95531882010-03-18 18:49:47 +00001448 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001449 CSEMap.InsertNode(N, IP);
1450 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001451 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001452}
1453
Chris Lattnerdecc2672010-04-07 05:20:54 +00001454/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1455SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1456 FoldingSetNodeID ID;
1457 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1458 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001459
Chris Lattnerdecc2672010-04-07 05:20:54 +00001460 void *IP = 0;
1461 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1462 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001463
Chris Lattnerdecc2672010-04-07 05:20:54 +00001464 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1465 CSEMap.InsertNode(N, IP);
1466 AllNodes.push_back(N);
1467 return SDValue(N, 0);
1468}
1469
1470
Duncan Sands92abc622009-01-31 15:50:11 +00001471/// getShiftAmountOperand - Return the specified value casted to
1472/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001473SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001474 EVT OpTy = Op.getValueType();
Owen Anderson6154f6c2011-03-07 18:29:47 +00001475 MVT ShTy = TLI.getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001476 if (OpTy == ShTy || OpTy.isVector()) return Op;
1477
1478 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001479 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001480}
1481
Chris Lattner37ce9df2007-10-15 17:47:20 +00001482/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1483/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001484SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001485 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001486 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001487 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001488 unsigned StackAlign =
1489 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
David Greene3f2bf852009-11-12 20:49:22 +00001491 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001492 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1493}
1494
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001495/// CreateStackTemporary - Create a stack temporary suitable for holding
1496/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001497SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001498 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1499 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001500 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1501 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001502 const TargetData *TD = TLI.getTargetData();
1503 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1504 TD->getPrefTypeAlignment(Ty2));
1505
1506 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001507 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001508 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1509}
1510
Owen Andersone50ed302009-08-10 22:56:29 +00001511SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001512 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001513 // These setcc operations always fold.
1514 switch (Cond) {
1515 default: break;
1516 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001517 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001518 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001519 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001520
Chris Lattnera83385f2006-04-27 05:01:07 +00001521 case ISD::SETOEQ:
1522 case ISD::SETOGT:
1523 case ISD::SETOGE:
1524 case ISD::SETOLT:
1525 case ISD::SETOLE:
1526 case ISD::SETONE:
1527 case ISD::SETO:
1528 case ISD::SETUO:
1529 case ISD::SETUEQ:
1530 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001531 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001532 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001533 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Gabor Greifba36cb52008-08-28 21:40:38 +00001535 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001536 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001537 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001538 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Chris Lattnerc3aae252005-01-07 07:46:32 +00001540 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001541 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001542 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1543 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001544 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1545 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1546 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1547 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1548 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1549 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1550 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1551 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001552 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001553 }
Chris Lattner67255a12005-04-07 18:14:58 +00001554 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001555 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1556 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001557 // No compile time operations on this type yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001559 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001560
1561 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001562 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001563 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001564 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001565 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001566 // fall through
1567 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001568 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001569 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001570 // fall through
1571 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001572 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001573 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001574 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001575 // fall through
1576 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001577 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001578 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001579 // fall through
1580 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001582 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001583 // fall through
1584 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001585 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001586 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001587 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001588 // fall through
1589 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001590 R==APFloat::cmpEqual, VT);
1591 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1592 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1593 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1594 R==APFloat::cmpEqual, VT);
1595 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1596 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1597 R==APFloat::cmpLessThan, VT);
1598 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1599 R==APFloat::cmpUnordered, VT);
1600 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1601 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001602 }
1603 } else {
1604 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001605 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001606 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001607 }
1608
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001609 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001610 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001611}
1612
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001613/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1614/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001615bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001616 // This predicate is not safe for vector operations.
1617 if (Op.getValueType().isVector())
1618 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001619
Dan Gohman87862e72009-12-11 21:31:27 +00001620 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001621 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1622}
1623
Dan Gohmanea859be2007-06-22 14:59:07 +00001624/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1625/// this predicate to simplify operations downstream. Mask is known to be zero
1626/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001627bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001628 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001629 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001630 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001631 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001632 return (KnownZero & Mask) == Mask;
1633}
1634
1635/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1636/// known to be either zero or one and return them in the KnownZero/KnownOne
1637/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1638/// processing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001639void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001640 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001641 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001642 unsigned BitWidth = Mask.getBitWidth();
Dan Gohman87862e72009-12-11 21:31:27 +00001643 assert(BitWidth == Op.getValueType().getScalarType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001644 "Mask size mismatches value type size!");
1645
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001646 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001647 if (Depth == 6 || Mask == 0)
1648 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001649
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001650 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001651
1652 switch (Op.getOpcode()) {
1653 case ISD::Constant:
1654 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001655 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001656 KnownZero = ~KnownOne & Mask;
1657 return;
1658 case ISD::AND:
1659 // If either the LHS or the RHS are Zero, the result is zero.
1660 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001661 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1662 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001663 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1664 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001665
1666 // Output known-1 bits are only known if set in both the LHS & RHS.
1667 KnownOne &= KnownOne2;
1668 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1669 KnownZero |= KnownZero2;
1670 return;
1671 case ISD::OR:
1672 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001673 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1674 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001675 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1676 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1677
Dan Gohmanea859be2007-06-22 14:59:07 +00001678 // Output known-0 bits are only known if clear in both the LHS & RHS.
1679 KnownZero &= KnownZero2;
1680 // Output known-1 are known to be set if set in either the LHS | RHS.
1681 KnownOne |= KnownOne2;
1682 return;
1683 case ISD::XOR: {
1684 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1685 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001686 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1687 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1688
Dan Gohmanea859be2007-06-22 14:59:07 +00001689 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001690 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001691 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1692 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1693 KnownZero = KnownZeroOut;
1694 return;
1695 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001696 case ISD::MUL: {
1697 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1698 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1699 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1700 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1701 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1702
1703 // If low bits are zero in either operand, output low known-0 bits.
1704 // Also compute a conserative estimate for high known-0 bits.
1705 // More trickiness is possible, but this is sufficient for the
1706 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001707 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001708 unsigned TrailZ = KnownZero.countTrailingOnes() +
1709 KnownZero2.countTrailingOnes();
1710 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001711 KnownZero2.countLeadingOnes(),
1712 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001713
1714 TrailZ = std::min(TrailZ, BitWidth);
1715 LeadZ = std::min(LeadZ, BitWidth);
1716 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1717 APInt::getHighBitsSet(BitWidth, LeadZ);
1718 KnownZero &= Mask;
1719 return;
1720 }
1721 case ISD::UDIV: {
1722 // For the purposes of computing leading zeros we can conservatively
1723 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001724 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001725 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1726 ComputeMaskedBits(Op.getOperand(0),
1727 AllOnes, KnownZero2, KnownOne2, Depth+1);
1728 unsigned LeadZ = KnownZero2.countLeadingOnes();
1729
Jay Foad7a874dd2010-12-01 08:53:58 +00001730 KnownOne2.clearAllBits();
1731 KnownZero2.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001732 ComputeMaskedBits(Op.getOperand(1),
1733 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001734 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1735 if (RHSUnknownLeadingOnes != BitWidth)
1736 LeadZ = std::min(BitWidth,
1737 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001738
1739 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1740 return;
1741 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001742 case ISD::SELECT:
1743 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1744 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001745 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1746 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1747
Dan Gohmanea859be2007-06-22 14:59:07 +00001748 // Only known if known in both the LHS and RHS.
1749 KnownOne &= KnownOne2;
1750 KnownZero &= KnownZero2;
1751 return;
1752 case ISD::SELECT_CC:
1753 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1754 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001755 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1756 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1757
Dan Gohmanea859be2007-06-22 14:59:07 +00001758 // Only known if known in both the LHS and RHS.
1759 KnownOne &= KnownOne2;
1760 KnownZero &= KnownZero2;
1761 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001762 case ISD::SADDO:
1763 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001764 case ISD::SSUBO:
1765 case ISD::USUBO:
1766 case ISD::SMULO:
1767 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001768 if (Op.getResNo() != 1)
1769 return;
Duncan Sands03228082008-11-23 15:47:28 +00001770 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001771 case ISD::SETCC:
1772 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001773 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1774 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001775 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001776 return;
1777 case ISD::SHL:
1778 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1779 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001780 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001781
1782 // If the shift count is an invalid immediate, don't do anything.
1783 if (ShAmt >= BitWidth)
1784 return;
1785
1786 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001787 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001788 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001789 KnownZero <<= ShAmt;
1790 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001791 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001792 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001793 }
1794 return;
1795 case ISD::SRL:
1796 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1797 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001798 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001799
Dan Gohmand4cf9922008-02-26 18:50:50 +00001800 // If the shift count is an invalid immediate, don't do anything.
1801 if (ShAmt >= BitWidth)
1802 return;
1803
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001804 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001806 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001807 KnownZero = KnownZero.lshr(ShAmt);
1808 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001809
Dan Gohman72d2fd52008-02-13 22:43:25 +00001810 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 KnownZero |= HighBits; // High bits known zero.
1812 }
1813 return;
1814 case ISD::SRA:
1815 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001816 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001817
Dan Gohmand4cf9922008-02-26 18:50:50 +00001818 // If the shift count is an invalid immediate, don't do anything.
1819 if (ShAmt >= BitWidth)
1820 return;
1821
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001822 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001823 // If any of the demanded bits are produced by the sign extension, we also
1824 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001825 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1826 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001827 InDemandedMask |= APInt::getSignBit(BitWidth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001828
Dan Gohmanea859be2007-06-22 14:59:07 +00001829 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1830 Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001831 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001832 KnownZero = KnownZero.lshr(ShAmt);
1833 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001834
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001836 APInt SignBit = APInt::getSignBit(BitWidth);
1837 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001838
Dan Gohmanca93a432008-02-20 16:30:17 +00001839 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001840 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001841 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001842 KnownOne |= HighBits; // New bits are known one.
1843 }
1844 }
1845 return;
1846 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001847 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001848 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
1850 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001851 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001852 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001853
Dan Gohman977a76f2008-02-13 22:28:48 +00001854 APInt InSignBit = APInt::getSignBit(EBits);
1855 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001856
Dan Gohmanea859be2007-06-22 14:59:07 +00001857 // If the sign extended bits are demanded, we know that the sign
1858 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001859 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001860 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Dan Gohmanea859be2007-06-22 14:59:07 +00001863 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1864 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001865 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1866
Dan Gohmanea859be2007-06-22 14:59:07 +00001867 // If the sign bit of the input is known set or clear, then we know the
1868 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001869 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001870 KnownZero |= NewBits;
1871 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001872 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001873 KnownOne |= NewBits;
1874 KnownZero &= ~NewBits;
1875 } else { // Input sign bit unknown
1876 KnownZero &= ~NewBits;
1877 KnownOne &= ~NewBits;
1878 }
1879 return;
1880 }
1881 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001882 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001883 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001884 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001885 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001886 unsigned LowBits = Log2_32(BitWidth)+1;
1887 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001888 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001889 return;
1890 }
1891 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001892 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001893 LoadSDNode *LD = cast<LoadSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00001894 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001895 unsigned MemBits = VT.getScalarType().getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001896 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 }
1898 return;
1899 }
1900 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001901 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001902 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001903 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
Jay Foad40f8f622010-12-07 08:25:19 +00001904 APInt InMask = Mask.trunc(InBits);
1905 KnownZero = KnownZero.trunc(InBits);
1906 KnownOne = KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001907 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001908 KnownZero = KnownZero.zext(BitWidth);
1909 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001910 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 return;
1912 }
1913 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001914 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001915 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001916 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001917 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
Jay Foad40f8f622010-12-07 08:25:19 +00001918 APInt InMask = Mask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001919
1920 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001921 // bit is demanded. Temporarily set this bit in the mask for our callee.
1922 if (NewBits.getBoolValue())
1923 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001924
Jay Foad40f8f622010-12-07 08:25:19 +00001925 KnownZero = KnownZero.trunc(InBits);
1926 KnownOne = KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001927 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1928
1929 // Note if the sign bit is known to be zero or one.
1930 bool SignBitKnownZero = KnownZero.isNegative();
1931 bool SignBitKnownOne = KnownOne.isNegative();
1932 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1933 "Sign bit can't be known to be both zero and one!");
1934
1935 // If the sign bit wasn't actually demanded by our caller, we don't
1936 // want it set in the KnownZero and KnownOne result values. Reset the
1937 // mask and reapply it to the result values.
Jay Foad40f8f622010-12-07 08:25:19 +00001938 InMask = Mask.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001939 KnownZero &= InMask;
1940 KnownOne &= InMask;
1941
Jay Foad40f8f622010-12-07 08:25:19 +00001942 KnownZero = KnownZero.zext(BitWidth);
1943 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001944
Dan Gohman977a76f2008-02-13 22:28:48 +00001945 // If the sign bit is known zero or one, the top bits match.
1946 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001948 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001950 return;
1951 }
1952 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001953 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001954 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001955 APInt InMask = Mask.trunc(InBits);
1956 KnownZero = KnownZero.trunc(InBits);
1957 KnownOne = KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001958 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001959 KnownZero = KnownZero.zext(BitWidth);
1960 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001961 return;
1962 }
1963 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001965 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001966 APInt InMask = Mask.zext(InBits);
1967 KnownZero = KnownZero.zext(InBits);
1968 KnownOne = KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001969 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001970 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001971 KnownZero = KnownZero.trunc(BitWidth);
1972 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001973 break;
1974 }
1975 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001976 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001977 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michelfdc40a02009-02-17 22:15:04 +00001978 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanea859be2007-06-22 14:59:07 +00001979 KnownOne, Depth+1);
1980 KnownZero |= (~InMask) & Mask;
1981 return;
1982 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001983 case ISD::FGETSIGN:
1984 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001985 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001986 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001987
Dan Gohman23e8b712008-04-28 17:02:21 +00001988 case ISD::SUB: {
1989 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1990 // We know that the top bits of C-X are clear if X contains less bits
1991 // than C (i.e. no wrap-around can happen). For example, 20-X is
1992 // positive if we can prove that X is >= 0 and < 16.
1993 if (CLHS->getAPIntValue().isNonNegative()) {
1994 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1995 // NLZ can't be BitWidth with no sign bit
1996 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1997 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1998 Depth+1);
1999
2000 // If all of the MaskV bits are known to be zero, then we know the
2001 // output top bits are zero, because we now know that the output is
2002 // from [0-C].
2003 if ((KnownZero2 & MaskV) == MaskV) {
2004 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2005 // Top bits known zero.
2006 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
2007 }
2008 }
2009 }
2010 }
2011 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002012 case ISD::ADD:
2013 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002014 // Output known-0 bits are known if clear or set in both the low clear bits
2015 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2016 // low 3 bits clear.
Dan Gohmanbc7588e2010-04-21 00:19:28 +00002017 APInt Mask2 = APInt::getLowBitsSet(BitWidth,
2018 BitWidth - Mask.countLeadingZeros());
Dan Gohman23e8b712008-04-28 17:02:21 +00002019 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002020 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002021 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2022
2023 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002024 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002025 KnownZeroOut = std::min(KnownZeroOut,
2026 KnownZero2.countTrailingOnes());
2027
Chris Lattnerda605882010-12-19 20:38:28 +00002028 if (Op.getOpcode() == ISD::ADD) {
2029 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2030 return;
2031 }
2032
2033 // With ADDE, a carry bit may be added in, so we can only use this
2034 // information if we know (at least) that the low two bits are clear. We
2035 // then return to the caller that the low bit is unknown but that other bits
2036 // are known zero.
2037 if (KnownZeroOut >= 2) // ADDE
2038 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002039 return;
2040 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002041 case ISD::SREM:
2042 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002043 const APInt &RA = Rem->getAPIntValue().abs();
2044 if (RA.isPowerOf2()) {
2045 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002046 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
2047 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002048
Duncan Sands5c2873a2010-01-29 09:45:26 +00002049 // The low bits of the first operand are unchanged by the srem.
2050 KnownZero = KnownZero2 & LowBits;
2051 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002052
Duncan Sands5c2873a2010-01-29 09:45:26 +00002053 // If the first operand is non-negative or has all low bits zero, then
2054 // the upper bits are all zero.
2055 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2056 KnownZero |= ~LowBits;
2057
2058 // If the first operand is negative and not all low bits are zero, then
2059 // the upper bits are all one.
2060 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2061 KnownOne |= ~LowBits;
2062
2063 KnownZero &= Mask;
2064 KnownOne &= Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00002065
2066 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002067 }
2068 }
2069 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002070 case ISD::UREM: {
2071 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002072 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002073 if (RA.isPowerOf2()) {
2074 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002075 APInt Mask2 = LowBits & Mask;
2076 KnownZero |= ~LowBits & Mask;
2077 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
2078 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2079 break;
2080 }
2081 }
2082
2083 // Since the result is less than or equal to either operand, any leading
2084 // zero bits in either operand must also exist in the result.
2085 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
2086 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
2087 Depth+1);
2088 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
2089 Depth+1);
2090
2091 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2092 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002093 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00002094 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
2095 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002096 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002097 case ISD::FrameIndex:
2098 case ISD::TargetFrameIndex:
2099 if (unsigned Align = InferPtrAlignment(Op)) {
2100 // The low bits are known zero if the pointer is aligned.
2101 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2102 return;
2103 }
2104 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002105
Dan Gohmanea859be2007-06-22 14:59:07 +00002106 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002107 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2108 break;
2109 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002110 case ISD::INTRINSIC_WO_CHAIN:
2111 case ISD::INTRINSIC_W_CHAIN:
2112 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002113 // Allow the target to implement this method for its nodes.
2114 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
2115 Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002116 return;
2117 }
2118}
2119
2120/// ComputeNumSignBits - Return the number of times the sign bit of the
2121/// register is replicated into the other bits. We know that at least 1 bit
2122/// is always equal to the sign bit (itself), but other cases can give us
2123/// information. For example, immediately after an "SRA X, 2", we know that
2124/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002125unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002126 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002127 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002128 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002129 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002130 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002131
Dan Gohmanea859be2007-06-22 14:59:07 +00002132 if (Depth == 6)
2133 return 1; // Limit search depth.
2134
2135 switch (Op.getOpcode()) {
2136 default: break;
2137 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002139 return VTBits-Tmp+1;
2140 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002141 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002142 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002145 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002146 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002148
Dan Gohmanea859be2007-06-22 14:59:07 +00002149 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002150 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002152
Dan Gohmanea859be2007-06-22 14:59:07 +00002153 case ISD::SIGN_EXTEND_INREG:
2154 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002155 Tmp =
2156 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002157 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002158
Dan Gohmanea859be2007-06-22 14:59:07 +00002159 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2160 return std::max(Tmp, Tmp2);
2161
2162 case ISD::SRA:
2163 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2164 // SRA X, C -> adds C sign bits.
2165 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002166 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002167 if (Tmp > VTBits) Tmp = VTBits;
2168 }
2169 return Tmp;
2170 case ISD::SHL:
2171 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2172 // shl destroys sign bits.
2173 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002174 if (C->getZExtValue() >= VTBits || // Bad shift.
2175 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2176 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002177 }
2178 break;
2179 case ISD::AND:
2180 case ISD::OR:
2181 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002182 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002183 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002184 if (Tmp != 1) {
2185 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2186 FirstAnswer = std::min(Tmp, Tmp2);
2187 // We computed what we know about the sign bits as our first
2188 // answer. Now proceed to the generic code that uses
2189 // ComputeMaskedBits, and pick whichever answer is better.
2190 }
2191 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002192
2193 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002194 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002195 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002196 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002197 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002198
2199 case ISD::SADDO:
2200 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002201 case ISD::SSUBO:
2202 case ISD::USUBO:
2203 case ISD::SMULO:
2204 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002205 if (Op.getResNo() != 1)
2206 break;
Duncan Sands03228082008-11-23 15:47:28 +00002207 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002208 case ISD::SETCC:
2209 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002210 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002211 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002212 return VTBits;
2213 break;
2214 case ISD::ROTL:
2215 case ISD::ROTR:
2216 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002217 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002218
Dan Gohmanea859be2007-06-22 14:59:07 +00002219 // Handle rotate right by N like a rotate left by 32-N.
2220 if (Op.getOpcode() == ISD::ROTR)
2221 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2222
2223 // If we aren't rotating out all of the known-in sign bits, return the
2224 // number that are left. This handles rotl(sext(x), 1) for example.
2225 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2226 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2227 }
2228 break;
2229 case ISD::ADD:
2230 // Add can have at most one carry bit. Thus we know that the output
2231 // is, at worst, one more bit than the inputs.
2232 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2233 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002234
Dan Gohmanea859be2007-06-22 14:59:07 +00002235 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002236 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002237 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002238 APInt KnownZero, KnownOne;
2239 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002240 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002241
Dan Gohmanea859be2007-06-22 14:59:07 +00002242 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2243 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002244 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002245 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002246
Dan Gohmanea859be2007-06-22 14:59:07 +00002247 // If we are subtracting one from a positive number, there is no carry
2248 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002249 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002250 return Tmp;
2251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002252
Dan Gohmanea859be2007-06-22 14:59:07 +00002253 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2254 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002255 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002256
Dan Gohmanea859be2007-06-22 14:59:07 +00002257 case ISD::SUB:
2258 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2259 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002260
Dan Gohmanea859be2007-06-22 14:59:07 +00002261 // Handle NEG.
2262 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002263 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002264 APInt KnownZero, KnownOne;
2265 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002266 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2267 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2268 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002269 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002270 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002271
Dan Gohmanea859be2007-06-22 14:59:07 +00002272 // If the input is known to be positive (the sign bit is known clear),
2273 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002274 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002275 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 // Otherwise, we treat this like a SUB.
2278 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002279
Dan Gohmanea859be2007-06-22 14:59:07 +00002280 // Sub can have at most one carry bit. Thus we know that the output
2281 // is, at worst, one more bit than the inputs.
2282 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2283 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002284 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002285 case ISD::TRUNCATE:
2286 // FIXME: it's tricky to do anything useful for this, but it is an important
2287 // case for targets like X86.
2288 break;
2289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Dan Gohmanea859be2007-06-22 14:59:07 +00002291 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2292 if (Op.getOpcode() == ISD::LOAD) {
2293 LoadSDNode *LD = cast<LoadSDNode>(Op);
2294 unsigned ExtType = LD->getExtensionType();
2295 switch (ExtType) {
2296 default: break;
2297 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002298 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002299 return VTBits-Tmp+1;
2300 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002301 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002302 return VTBits-Tmp;
2303 }
2304 }
2305
2306 // Allow the target to implement this method for its nodes.
2307 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002308 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002309 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2310 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2311 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002312 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002313 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Dan Gohmanea859be2007-06-22 14:59:07 +00002315 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2316 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002317 APInt KnownZero, KnownOne;
2318 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002319 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002320
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002321 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002322 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002323 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002324 Mask = KnownOne;
2325 } else {
2326 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002327 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002328 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002329
Dan Gohmanea859be2007-06-22 14:59:07 +00002330 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2331 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002332 Mask = ~Mask;
2333 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002334 // Return # leading zeros. We use 'min' here in case Val was zero before
2335 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002336 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002337}
2338
Chris Lattner0a9481f2011-02-13 22:25:43 +00002339/// isBaseWithConstantOffset - Return true if the specified operand is an
2340/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2341/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2342/// semantics as an ADD. This handles the equivalence:
Chris Lattner463b3c22011-02-14 06:14:42 +00002343/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002344bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2345 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2346 !isa<ConstantSDNode>(Op.getOperand(1)))
2347 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002348
2349 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002350 !MaskedValueIsZero(Op.getOperand(0),
2351 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2352 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002353
Chris Lattner0a9481f2011-02-13 22:25:43 +00002354 return true;
2355}
2356
2357
Dan Gohman8d44b282009-09-03 20:34:31 +00002358bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2359 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002360 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002361 return true;
2362
2363 // If the value is a constant, we can obviously see if it is a NaN or not.
2364 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2365 return !C->getValueAPF().isNaN();
2366
2367 // TODO: Recognize more cases here.
2368
2369 return false;
2370}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002371
Dan Gohmane8326932010-02-24 06:52:40 +00002372bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2373 // If the value is a constant, we can obviously see if it is a zero or not.
2374 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2375 return !C->isZero();
2376
2377 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002378 switch (Op.getOpcode()) {
2379 default: break;
2380 case ISD::OR:
2381 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2382 return !C->isNullValue();
2383 break;
2384 }
Dan Gohmane8326932010-02-24 06:52:40 +00002385
2386 return false;
2387}
2388
2389bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2390 // Check the obvious case.
2391 if (A == B) return true;
2392
2393 // For for negative and positive zero.
2394 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2395 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2396 if (CA->isZero() && CB->isZero()) return true;
2397
2398 // Otherwise they may not be equal.
2399 return false;
2400}
2401
Chris Lattnerc3aae252005-01-07 07:46:32 +00002402/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002403///
Owen Andersone50ed302009-08-10 22:56:29 +00002404SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002405 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002406 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002407 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002408 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002409 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002410
Dan Gohman95531882010-03-18 18:49:47 +00002411 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002412 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002413
Chris Lattner4a283e92006-08-11 18:38:11 +00002414 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002415#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002416 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002417#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002418 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002419}
2420
Bill Wendling7ade28c2009-01-28 22:17:52 +00002421SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002422 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002423 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002424 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002425 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002426 switch (Opcode) {
2427 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002428 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002429 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002430 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002431 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002432 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002433 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002434 case ISD::UINT_TO_FP:
2435 case ISD::SINT_TO_FP: {
Chris Lattner7fb5c2d2010-03-15 16:05:15 +00002436 // No compile time operations on ppcf128.
2437 if (VT == MVT::ppcf128) break;
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002438 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002439 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002440 Opcode==ISD::SINT_TO_FP,
2441 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002442 return getConstantFP(apf, VT);
2443 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002444 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002445 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002446 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002447 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002448 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002449 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002450 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002451 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002452 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002453 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002454 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002455 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002456 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002457 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002458 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002459 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002460 }
2461 }
2462
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002463 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002464 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002465 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002466 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002467 switch (Opcode) {
2468 case ISD::FNEG:
2469 V.changeSign();
2470 return getConstantFP(V, VT);
2471 case ISD::FABS:
2472 V.clearSign();
2473 return getConstantFP(V, VT);
2474 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002475 case ISD::FP_EXTEND: {
2476 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002477 // This can return overflow, underflow, or inexact; we don't care.
2478 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002479 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002480 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002481 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002482 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002483 case ISD::FP_TO_SINT:
2484 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002485 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002486 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002487 assert(integerPartWidth >= 64);
2488 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002489 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002490 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002491 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002492 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2493 break;
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00002494 APInt api(VT.getSizeInBits(), x);
Dale Johannesen2f91f302009-04-24 21:34:13 +00002495 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002496 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002497 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002498 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002499 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002500 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002501 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002502 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002503 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002504 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002505 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002506
Gabor Greifba36cb52008-08-28 21:40:38 +00002507 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002508 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002509 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002510 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002511 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002512 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002513 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002514 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002515 assert(VT.isFloatingPoint() &&
2516 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002517 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002518 assert((!VT.isVector() ||
2519 VT.getVectorNumElements() ==
2520 Operand.getValueType().getVectorNumElements()) &&
2521 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002522 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002523 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002524 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002525 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002526 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002527 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002528 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002529 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2530 "Invalid sext node, dst < src!");
2531 assert((!VT.isVector() ||
2532 VT.getVectorNumElements() ==
2533 Operand.getValueType().getVectorNumElements()) &&
2534 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002535 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002536 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002537 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002538 // sext(undef) = 0, because the top bits will all be the same.
2539 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002540 break;
2541 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002543 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002544 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002545 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2546 "Invalid zext node, dst < src!");
2547 assert((!VT.isVector() ||
2548 VT.getVectorNumElements() ==
2549 Operand.getValueType().getVectorNumElements()) &&
2550 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002551 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002552 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002553 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002554 else if (OpOpcode == ISD::UNDEF)
2555 // zext(undef) = 0, because the top bits will be zero.
2556 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002557 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002558 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002559 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002560 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002561 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002562 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2563 "Invalid anyext node, dst < src!");
2564 assert((!VT.isVector() ||
2565 VT.getVectorNumElements() ==
2566 Operand.getValueType().getVectorNumElements()) &&
2567 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002568
Dan Gohman9e86a732010-06-18 00:08:30 +00002569 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2570 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002571 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002572 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002573 else if (OpOpcode == ISD::UNDEF)
2574 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002575
2576 // (ext (trunx x)) -> x
2577 if (OpOpcode == ISD::TRUNCATE) {
2578 SDValue OpOp = Operand.getNode()->getOperand(0);
2579 if (OpOp.getValueType() == VT)
2580 return OpOp;
2581 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002582 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002583 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002584 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002585 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002586 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002587 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2588 "Invalid truncate node, src < dst!");
2589 assert((!VT.isVector() ||
2590 VT.getVectorNumElements() ==
2591 Operand.getValueType().getVectorNumElements()) &&
2592 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002593 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002594 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002595 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2596 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002597 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002598 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2599 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002600 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002601 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002602 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002603 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002604 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002605 if (OpOpcode == ISD::UNDEF)
2606 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002607 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002608 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002609 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002611 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002612 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002613 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2614 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002615 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002616 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002617 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002618 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002619 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002620 (VT.getVectorElementType() == Operand.getValueType() ||
2621 (VT.getVectorElementType().isInteger() &&
2622 Operand.getValueType().isInteger() &&
2623 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002624 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002625 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002626 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002627 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2628 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2629 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2630 Operand.getConstantOperandVal(1) == 0 &&
2631 Operand.getOperand(0).getValueType() == VT)
2632 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002633 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002634 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002635 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002636 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002637 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002638 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002639 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002640 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002641 break;
2642 case ISD::FABS:
2643 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002644 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002645 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002646 }
2647
Chris Lattner43247a12005-08-25 19:12:10 +00002648 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002649 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002650 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002651 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002652 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002653 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002654 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002655 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002656 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002657
Dan Gohman95531882010-03-18 18:49:47 +00002658 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002659 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002660 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002661 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002662 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002663
Chris Lattnerc3aae252005-01-07 07:46:32 +00002664 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002665#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002666 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002667#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002668 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002669}
2670
Bill Wendling688d1c42008-09-24 10:16:24 +00002671SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002672 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002673 ConstantSDNode *Cst1,
2674 ConstantSDNode *Cst2) {
2675 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2676
2677 switch (Opcode) {
2678 case ISD::ADD: return getConstant(C1 + C2, VT);
2679 case ISD::SUB: return getConstant(C1 - C2, VT);
2680 case ISD::MUL: return getConstant(C1 * C2, VT);
2681 case ISD::UDIV:
2682 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2683 break;
2684 case ISD::UREM:
2685 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2686 break;
2687 case ISD::SDIV:
2688 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2689 break;
2690 case ISD::SREM:
2691 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2692 break;
2693 case ISD::AND: return getConstant(C1 & C2, VT);
2694 case ISD::OR: return getConstant(C1 | C2, VT);
2695 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2696 case ISD::SHL: return getConstant(C1 << C2, VT);
2697 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2698 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2699 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2700 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2701 default: break;
2702 }
2703
2704 return SDValue();
2705}
2706
Owen Andersone50ed302009-08-10 22:56:29 +00002707SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002708 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002709 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2710 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002711 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002712 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002713 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002714 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2715 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002716 // Fold trivial token factors.
2717 if (N1.getOpcode() == ISD::EntryToken) return N2;
2718 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002719 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002720 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002721 case ISD::CONCAT_VECTORS:
2722 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2723 // one big BUILD_VECTOR.
2724 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2725 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002726 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2727 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002728 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002729 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002730 }
2731 break;
Chris Lattner76365122005-01-16 02:23:22 +00002732 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002733 assert(VT.isInteger() && "This operator does not apply to FP types!");
2734 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002735 N1.getValueType() == VT && "Binary operator types must match!");
2736 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2737 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002738 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002739 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002740 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2741 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002742 break;
Chris Lattner76365122005-01-16 02:23:22 +00002743 case ISD::OR:
2744 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002745 case ISD::ADD:
2746 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002747 assert(VT.isInteger() && "This operator does not apply to FP types!");
2748 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002749 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002750 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2751 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002752 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002753 return N1;
2754 break;
Chris Lattner76365122005-01-16 02:23:22 +00002755 case ISD::UDIV:
2756 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002757 case ISD::MULHU:
2758 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002759 case ISD::MUL:
2760 case ISD::SDIV:
2761 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002762 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002763 assert(N1.getValueType() == N2.getValueType() &&
2764 N1.getValueType() == VT && "Binary operator types must match!");
2765 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002766 case ISD::FADD:
2767 case ISD::FSUB:
2768 case ISD::FMUL:
2769 case ISD::FDIV:
2770 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002771 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002772 if (Opcode == ISD::FADD) {
2773 // 0+x --> x
2774 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2775 if (CFP->getValueAPF().isZero())
2776 return N2;
2777 // x+0 --> x
2778 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2779 if (CFP->getValueAPF().isZero())
2780 return N1;
2781 } else if (Opcode == ISD::FSUB) {
2782 // x-0 --> x
2783 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2784 if (CFP->getValueAPF().isZero())
2785 return N1;
2786 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002787 }
Dale Johannesen78995512010-05-15 18:38:02 +00002788 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002789 assert(N1.getValueType() == N2.getValueType() &&
2790 N1.getValueType() == VT && "Binary operator types must match!");
2791 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002792 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2793 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002794 N1.getValueType().isFloatingPoint() &&
2795 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002796 "Invalid FCOPYSIGN!");
2797 break;
Chris Lattner76365122005-01-16 02:23:22 +00002798 case ISD::SHL:
2799 case ISD::SRA:
2800 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002801 case ISD::ROTL:
2802 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002803 assert(VT == N1.getValueType() &&
2804 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002805 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002806 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002807 // Verify that the shift amount VT is bit enough to hold valid shift
2808 // amounts. This catches things like trying to shift an i1024 value by an
2809 // i8, which is easy to fall into in generic code that uses
2810 // TLI.getShiftAmount().
2811 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002812 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002813 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002814
2815 // Always fold shifts of i1 values so the code generator doesn't need to
2816 // handle them. Since we know the size of the shift has to be less than the
2817 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002818 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002819 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002820 if (N2C && N2C->isNullValue())
2821 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002822 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002823 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002824 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002825 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002826 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002827 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002828 assert(EVT.isVector() == VT.isVector() &&
2829 "FP_ROUND_INREG type should be vector iff the operand "
2830 "type is vector!");
2831 assert((!EVT.isVector() ||
2832 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2833 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002834 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002835 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002836 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002837 break;
2838 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002839 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002840 assert(VT.isFloatingPoint() &&
2841 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002842 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002843 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002844 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002845 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002846 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002847 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002848 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002849 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002851 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002852 assert(!EVT.isVector() &&
2853 "AssertSExt/AssertZExt type should be the vector element type "
2854 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002855 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002856 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002857 break;
2858 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002859 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002860 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002861 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002863 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002864 assert(EVT.isVector() == VT.isVector() &&
2865 "SIGN_EXTEND_INREG type should be vector iff the operand "
2866 "type is vector!");
2867 assert((!EVT.isVector() ||
2868 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2869 "Vector element counts must match in SIGN_EXTEND_INREG");
2870 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002871 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002872
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002873 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002874 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002875 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002876 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002877 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002878 return getConstant(Val, VT);
2879 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002880 break;
2881 }
2882 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002883 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2884 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002885 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002886
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002887 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2888 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002889 if (N2C &&
2890 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002891 N1.getNumOperands() > 0) {
2892 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002893 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002894 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002895 N1.getOperand(N2C->getZExtValue() / Factor),
2896 getConstant(N2C->getZExtValue() % Factor,
2897 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002898 }
2899
2900 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2901 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002902 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2903 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002904 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002905 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002906 // If the vector element type is not legal, the BUILD_VECTOR operands
2907 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002908 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2909 }
2910 if (VT != VEltTy) {
2911 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2912 // result is implicitly extended.
2913 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002914 }
2915 return Elt;
2916 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002917
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002918 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2919 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002920 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002921 // If the indices are the same, return the inserted element else
2922 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002923 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002924 SDValue N1Op2 = N1.getOperand(2);
2925 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2926
2927 if (N1Op2C && N2C) {
2928 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2929 if (VT == N1.getOperand(1).getValueType())
2930 return N1.getOperand(1);
2931 else
2932 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2933 }
2934
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002935 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002936 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002937 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002938 break;
2939 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002940 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002941 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2942 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002943 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002944 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002945
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002946 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2947 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002948 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002949 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002950 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002951
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002952 // EXTRACT_ELEMENT of a constant int is also very common.
2953 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002954 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002955 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002956 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2957 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002958 }
2959 break;
David Greene91585092011-01-26 15:38:49 +00002960 case ISD::EXTRACT_SUBVECTOR: {
2961 SDValue Index = N2;
2962 if (VT.isSimple() && N1.getValueType().isSimple()) {
2963 assert(VT.isVector() && N1.getValueType().isVector() &&
2964 "Extract subvector VTs must be a vectors!");
2965 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
2966 "Extract subvector VTs must have the same element type!");
2967 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
2968 "Extract subvector must be from larger vector to smaller vector!");
2969
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00002970 if (isa<ConstantSDNode>(Index.getNode())) {
2971 assert((VT.getVectorNumElements() +
2972 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00002973 <= N1.getValueType().getVectorNumElements())
2974 && "Extract subvector overflow!");
2975 }
2976
2977 // Trivial extraction.
2978 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
2979 return N1;
2980 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002981 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002982 }
David Greene91585092011-01-26 15:38:49 +00002983 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002984
2985 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002986 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002987 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2988 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002989 } else { // Cannonicalize constant to RHS if commutative
2990 if (isCommutativeBinOp(Opcode)) {
2991 std::swap(N1C, N2C);
2992 std::swap(N1, N2);
2993 }
2994 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002995 }
2996
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002997 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002998 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2999 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003000 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003001 if (!N2CFP && isCommutativeBinOp(Opcode)) {
3002 // Cannonicalize constant to RHS if commutative
3003 std::swap(N1CFP, N2CFP);
3004 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00003005 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003006 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3007 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003008 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003009 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003010 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003011 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003012 return getConstantFP(V1, VT);
3013 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003014 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003015 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3016 if (s!=APFloat::opInvalidOp)
3017 return getConstantFP(V1, VT);
3018 break;
3019 case ISD::FMUL:
3020 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3021 if (s!=APFloat::opInvalidOp)
3022 return getConstantFP(V1, VT);
3023 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003024 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003025 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3026 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3027 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003028 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003029 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003030 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3031 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3032 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003033 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003034 case ISD::FCOPYSIGN:
3035 V1.copySign(V2);
3036 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003037 default: break;
3038 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003039 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003040 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003041
Chris Lattner62b57722006-04-20 05:39:12 +00003042 // Canonicalize an UNDEF to the RHS, even over a constant.
3043 if (N1.getOpcode() == ISD::UNDEF) {
3044 if (isCommutativeBinOp(Opcode)) {
3045 std::swap(N1, N2);
3046 } else {
3047 switch (Opcode) {
3048 case ISD::FP_ROUND_INREG:
3049 case ISD::SIGN_EXTEND_INREG:
3050 case ISD::SUB:
3051 case ISD::FSUB:
3052 case ISD::FDIV:
3053 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003054 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003055 return N1; // fold op(undef, arg2) -> undef
3056 case ISD::UDIV:
3057 case ISD::SDIV:
3058 case ISD::UREM:
3059 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003060 case ISD::SRL:
3061 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003062 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003063 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3064 // For vectors, we can't easily build an all zero vector, just return
3065 // the LHS.
3066 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003067 }
3068 }
3069 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003070
3071 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003072 if (N2.getOpcode() == ISD::UNDEF) {
3073 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003074 case ISD::XOR:
3075 if (N1.getOpcode() == ISD::UNDEF)
3076 // Handle undef ^ undef -> 0 special case. This is a common
3077 // idiom (misuse).
3078 return getConstant(0, VT);
3079 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003080 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003081 case ISD::ADDC:
3082 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003083 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003084 case ISD::UDIV:
3085 case ISD::SDIV:
3086 case ISD::UREM:
3087 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003088 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003089 case ISD::FADD:
3090 case ISD::FSUB:
3091 case ISD::FMUL:
3092 case ISD::FDIV:
3093 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003094 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003095 return N2;
3096 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003097 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003098 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003099 case ISD::SRL:
3100 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003101 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003102 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3103 // For vectors, we can't easily build an all zero vector, just return
3104 // the LHS.
3105 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003106 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003107 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003108 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003109 // For vectors, we can't easily build an all one vector, just return
3110 // the LHS.
3111 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003112 case ISD::SRA:
3113 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003114 }
3115 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003116
Chris Lattner27e9b412005-05-11 18:57:39 +00003117 // Memoize this node if possible.
3118 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003119 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003120 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003121 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003122 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003123 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003124 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003125 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003126 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003127
Dan Gohman95531882010-03-18 18:49:47 +00003128 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003129 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003130 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003131 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003132 }
3133
Chris Lattnerc3aae252005-01-07 07:46:32 +00003134 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003135#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003136 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003137#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003138 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003139}
3140
Owen Andersone50ed302009-08-10 22:56:29 +00003141SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003142 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003143 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003144 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003145 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003146 case ISD::CONCAT_VECTORS:
3147 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3148 // one big BUILD_VECTOR.
3149 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3150 N2.getOpcode() == ISD::BUILD_VECTOR &&
3151 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003152 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3153 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003154 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3155 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003156 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003157 }
3158 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003159 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003160 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003161 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003162 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003163 break;
3164 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003165 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003166 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003167 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003168 return N2; // select true, X, Y -> X
3169 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003170 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003171
3172 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003173 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003174 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003175 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003176 case ISD::INSERT_SUBVECTOR: {
3177 SDValue Index = N3;
3178 if (VT.isSimple() && N1.getValueType().isSimple()
3179 && N2.getValueType().isSimple()) {
3180 assert(VT.isVector() && N1.getValueType().isVector() &&
3181 N2.getValueType().isVector() &&
3182 "Insert subvector VTs must be a vectors");
3183 assert(VT == N1.getValueType() &&
3184 "Dest and insert subvector source types must match!");
3185 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3186 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003187 if (isa<ConstantSDNode>(Index.getNode())) {
3188 assert((N2.getValueType().getVectorNumElements() +
3189 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003190 <= VT.getVectorNumElements())
3191 && "Insert subvector overflow!");
3192 }
3193
3194 // Trivial insertion.
3195 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3196 return N2;
3197 }
3198 break;
3199 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003200 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003201 // Fold bit_convert nodes from a type to themselves.
3202 if (N1.getValueType() == VT)
3203 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003204 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003205 }
3206
Chris Lattner43247a12005-08-25 19:12:10 +00003207 // Memoize node if it doesn't produce a flag.
3208 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003209 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003210 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003211 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003212 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003213 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003214 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003215 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003216 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003217
Dan Gohman95531882010-03-18 18:49:47 +00003218 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003219 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003220 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003221 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003222 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003223
Chris Lattnerc3aae252005-01-07 07:46:32 +00003224 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003225#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003226 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003227#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003228 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003229}
3230
Owen Andersone50ed302009-08-10 22:56:29 +00003231SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003232 SDValue N1, SDValue N2, SDValue N3,
3233 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003234 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003235 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003236}
3237
Owen Andersone50ed302009-08-10 22:56:29 +00003238SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003239 SDValue N1, SDValue N2, SDValue N3,
3240 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003241 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003242 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003243}
3244
Dan Gohman98ca4f22009-08-05 01:29:28 +00003245/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3246/// the incoming stack arguments to be loaded from the stack.
3247SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3248 SmallVector<SDValue, 8> ArgChains;
3249
3250 // Include the original chain at the beginning of the list. When this is
3251 // used by target LowerCall hooks, this helps legalize find the
3252 // CALLSEQ_BEGIN node.
3253 ArgChains.push_back(Chain);
3254
3255 // Add a chain value for each stack argument.
3256 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3257 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3258 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3259 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3260 if (FI->getIndex() < 0)
3261 ArgChains.push_back(SDValue(L, 1));
3262
3263 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003264 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003265 &ArgChains[0], ArgChains.size());
3266}
3267
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003268/// SplatByte - Distribute ByteVal over NumBits bits.
3269static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3270 APInt Val = APInt(NumBits, ByteVal);
3271 unsigned Shift = 8;
3272 for (unsigned i = NumBits; i > 8; i >>= 1) {
3273 Val = (Val << Shift) | Val;
3274 Shift <<= 1;
3275 }
3276 return Val;
3277}
3278
Dan Gohman707e0182008-04-12 04:36:06 +00003279/// getMemsetValue - Vectorized representation of the memset value
3280/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003281static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003282 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003283 assert(Value.getOpcode() != ISD::UNDEF);
3284
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003285 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003286 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003287 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003288 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003289 return DAG.getConstant(Val, VT);
3290 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003291 }
Evan Chengf0df0312008-05-15 08:39:06 +00003292
Dale Johannesen0f502f62009-02-03 22:26:09 +00003293 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003294 if (NumBits > 8) {
3295 // Use a multiplication with 0x010101... to extend the input to the
3296 // required length.
3297 APInt Magic = SplatByte(NumBits, 0x01);
3298 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003299 }
3300
3301 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003302}
3303
Dan Gohman707e0182008-04-12 04:36:06 +00003304/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3305/// used when a memcpy is turned into a memset when the source is a constant
3306/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003307static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003308 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003309 // Handle vector with all elements zero.
3310 if (Str.empty()) {
3311 if (VT.isInteger())
3312 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003313 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003314 return DAG.getConstantFP(0.0, VT);
3315 else if (VT.isVector()) {
3316 unsigned NumElts = VT.getVectorNumElements();
3317 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003318 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003319 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3320 EltVT, NumElts)));
3321 } else
3322 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003323 }
3324
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003326 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3327 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3328
Dan Gohman707e0182008-04-12 04:36:06 +00003329 uint64_t Val = 0;
Chris Lattner18c7f802012-02-05 02:29:43 +00003330 if (TLI.isLittleEndian()) {
3331 for (unsigned i = 0; i != NumBytes; ++i)
3332 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3333 } else {
3334 for (unsigned i = 0; i != NumBytes; ++i)
3335 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003336 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003337
Dan Gohman707e0182008-04-12 04:36:06 +00003338 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003339}
3340
Scott Michelfdc40a02009-02-17 22:15:04 +00003341/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003342///
Dan Gohman475871a2008-07-27 21:46:04 +00003343static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003344 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003345 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003346 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003347 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003348}
3349
Evan Chengf0df0312008-05-15 08:39:06 +00003350/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3351///
Chris Lattner18c7f802012-02-05 02:29:43 +00003352static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003353 unsigned SrcDelta = 0;
3354 GlobalAddressSDNode *G = NULL;
3355 if (Src.getOpcode() == ISD::GlobalAddress)
3356 G = cast<GlobalAddressSDNode>(Src);
3357 else if (Src.getOpcode() == ISD::ADD &&
3358 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3359 Src.getOperand(1).getOpcode() == ISD::Constant) {
3360 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003361 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003362 }
3363 if (!G)
3364 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003365
Chris Lattner18c7f802012-02-05 02:29:43 +00003366 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003367}
Dan Gohman707e0182008-04-12 04:36:06 +00003368
Evan Cheng255f20f2010-04-01 06:04:33 +00003369/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3370/// to replace the memset / memcpy. Return true if the number of memory ops
3371/// is below the threshold. It returns the types of the sequence of
3372/// memory ops to perform memset / memcpy by reference.
3373static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003374 unsigned Limit, uint64_t Size,
3375 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003376 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00003377 bool MemcpyStrSrc,
Evan Cheng255f20f2010-04-01 06:04:33 +00003378 SelectionDAG &DAG,
3379 const TargetLowering &TLI) {
3380 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3381 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003382 // If 'SrcAlign' is zero, that means the memory operation does not need to
3383 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3384 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3385 // is the specified alignment of the memory operation. If it is zero, that
3386 // means it's possible to change the alignment of the destination.
3387 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3388 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003389 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003390 IsZeroVal, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003391 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003392
Chris Lattneracee6472010-03-07 07:45:08 +00003393 if (VT == MVT::Other) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003394 if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003395 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003396 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003397 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003398 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 case 0: VT = MVT::i64; break;
3400 case 4: VT = MVT::i32; break;
3401 case 2: VT = MVT::i16; break;
3402 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003403 }
3404 }
3405
Owen Anderson766b5ef2009-08-11 21:59:30 +00003406 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003407 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003408 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003409 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003410
Duncan Sands8e4eb092008-06-08 20:54:56 +00003411 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003412 VT = LVT;
3413 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003414
Dan Gohman707e0182008-04-12 04:36:06 +00003415 unsigned NumMemOps = 0;
3416 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003417 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003418 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003419 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng255f20f2010-04-01 06:04:33 +00003420 if (VT.isVector() || VT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003422 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003424 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003425 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003426 // This can result in a type that is not legal on the target, e.g.
3427 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003429 VTSize >>= 1;
3430 }
Dan Gohman707e0182008-04-12 04:36:06 +00003431 }
Dan Gohman707e0182008-04-12 04:36:06 +00003432
3433 if (++NumMemOps > Limit)
3434 return false;
3435 MemOps.push_back(VT);
3436 Size -= VTSize;
3437 }
3438
3439 return true;
3440}
3441
Dale Johannesen0f502f62009-02-03 22:26:09 +00003442static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003443 SDValue Chain, SDValue Dst,
3444 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003445 unsigned Align, bool isVol,
3446 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003447 MachinePointerInfo DstPtrInfo,
3448 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003449 // Turn a memcpy of undef to nop.
3450 if (Src.getOpcode() == ISD::UNDEF)
3451 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003452
Dan Gohman21323f32008-05-29 19:42:22 +00003453 // Expand memcpy to a series of load and store ops if the size operand falls
3454 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003455 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3456 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003458 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003459 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003460 MachineFunction &MF = DAG.getMachineFunction();
3461 MachineFrameInfo *MFI = MF.getFrameInfo();
3462 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003463 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3464 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3465 DstAlignCanChange = true;
3466 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3467 if (Align > SrcAlign)
3468 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003469 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003470 bool CopyFromStr = isMemSrcFromString(Src, Str);
3471 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003472 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003473
Evan Cheng94107ba2010-04-01 18:19:11 +00003474 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003475 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003476 (isZeroStr ? 0 : SrcAlign),
3477 true, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003478 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003479
Evan Cheng255f20f2010-04-01 06:04:33 +00003480 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003481 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003482 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3483 if (NewAlign > Align) {
3484 // Give the stack frame object a larger alignment if needed.
3485 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3486 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3487 Align = NewAlign;
3488 }
3489 }
Dan Gohman707e0182008-04-12 04:36:06 +00003490
Dan Gohman475871a2008-07-27 21:46:04 +00003491 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003492 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003493 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003494 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003495 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003496 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003497 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003498
Evan Cheng255f20f2010-04-01 06:04:33 +00003499 if (CopyFromStr &&
3500 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003501 // It's unlikely a store of a vector immediate can be done in a single
3502 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003503 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003504 // FIXME: Handle other cases where store of vector immediate is done in
3505 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003506 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Dale Johannesen0f502f62009-02-03 22:26:09 +00003507 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003508 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003509 DstPtrInfo.getWithOffset(DstOff), isVol,
3510 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003511 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003512 // The type might not be legal for the target. This should only happen
3513 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003514 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3515 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003516 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003517 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003518 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003519 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003520 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003521 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003522 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003523 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003524 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003525 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3526 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003527 }
3528 OutChains.push_back(Store);
3529 SrcOff += VTSize;
3530 DstOff += VTSize;
3531 }
3532
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003534 &OutChains[0], OutChains.size());
3535}
3536
Dale Johannesen0f502f62009-02-03 22:26:09 +00003537static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003538 SDValue Chain, SDValue Dst,
3539 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003540 unsigned Align, bool isVol,
3541 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003542 MachinePointerInfo DstPtrInfo,
3543 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003544 // Turn a memmove of undef to nop.
3545 if (Src.getOpcode() == ISD::UNDEF)
3546 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003547
3548 // Expand memmove to a series of load and store ops if the size operand falls
3549 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003550 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003551 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003552 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003553 MachineFunction &MF = DAG.getMachineFunction();
3554 MachineFrameInfo *MFI = MF.getFrameInfo();
3555 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003556 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3557 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3558 DstAlignCanChange = true;
3559 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3560 if (Align > SrcAlign)
3561 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003562 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003563
Evan Cheng94107ba2010-04-01 18:19:11 +00003564 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003565 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003566 SrcAlign, true, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003567 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003568
Evan Cheng255f20f2010-04-01 06:04:33 +00003569 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003570 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003571 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3572 if (NewAlign > Align) {
3573 // Give the stack frame object a larger alignment if needed.
3574 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3575 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3576 Align = NewAlign;
3577 }
3578 }
Dan Gohman21323f32008-05-29 19:42:22 +00003579
Evan Cheng255f20f2010-04-01 06:04:33 +00003580 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003581 SmallVector<SDValue, 8> LoadValues;
3582 SmallVector<SDValue, 8> LoadChains;
3583 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003584 unsigned NumMemOps = MemOps.size();
3585 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003586 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003587 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003588 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003589
Dale Johannesen0f502f62009-02-03 22:26:09 +00003590 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003591 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003592 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003593 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003594 LoadValues.push_back(Value);
3595 LoadChains.push_back(Value.getValue(1));
3596 SrcOff += VTSize;
3597 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003599 &LoadChains[0], LoadChains.size());
3600 OutChains.clear();
3601 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003602 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003603 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003604 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003605
Dale Johannesen0f502f62009-02-03 22:26:09 +00003606 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003607 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003608 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003609 OutChains.push_back(Store);
3610 DstOff += VTSize;
3611 }
3612
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003614 &OutChains[0], OutChains.size());
3615}
3616
Dale Johannesen0f502f62009-02-03 22:26:09 +00003617static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003618 SDValue Chain, SDValue Dst,
3619 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003620 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003621 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003622 // Turn a memset of undef to nop.
3623 if (Src.getOpcode() == ISD::UNDEF)
3624 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003625
3626 // Expand memset to a series of load/store ops if the size operand
3627 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003628 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003629 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003630 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003631 MachineFunction &MF = DAG.getMachineFunction();
3632 MachineFrameInfo *MFI = MF.getFrameInfo();
3633 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003634 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3635 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3636 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003637 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003638 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003639 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003640 Size, (DstAlignCanChange ? 0 : Align), 0,
Lang Hames15701f82011-10-26 23:50:43 +00003641 IsZeroVal, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003642 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003643
Evan Cheng255f20f2010-04-01 06:04:33 +00003644 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003645 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003646 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3647 if (NewAlign > Align) {
3648 // Give the stack frame object a larger alignment if needed.
3649 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3650 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3651 Align = NewAlign;
3652 }
3653 }
3654
Dan Gohman475871a2008-07-27 21:46:04 +00003655 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003656 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003657 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003658
3659 // Find the largest store and generate the bit pattern for it.
3660 EVT LargestVT = MemOps[0];
3661 for (unsigned i = 1; i < NumMemOps; i++)
3662 if (MemOps[i].bitsGT(LargestVT))
3663 LargestVT = MemOps[i];
3664 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3665
Dan Gohman707e0182008-04-12 04:36:06 +00003666 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003667 EVT VT = MemOps[i];
Benjamin Kramer80220362011-01-02 19:57:05 +00003668
3669 // If this store is smaller than the largest store see whether we can get
3670 // the smaller value for free with a truncate.
3671 SDValue Value = MemSetValue;
3672 if (VT.bitsLT(LargestVT)) {
3673 if (!LargestVT.isVector() && !VT.isVector() &&
3674 TLI.isTruncateFree(LargestVT, VT))
3675 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3676 else
3677 Value = getMemsetValue(Src, VT, DAG, dl);
3678 }
3679 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003680 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003681 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003682 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003683 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003684 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003685 DstOff += VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003686 }
3687
Owen Anderson825b72b2009-08-11 20:47:22 +00003688 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003689 &OutChains[0], OutChains.size());
3690}
3691
Dale Johannesen0f502f62009-02-03 22:26:09 +00003692SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003693 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003694 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003695 MachinePointerInfo DstPtrInfo,
3696 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003697
3698 // Check to see if we should lower the memcpy to loads and stores first.
3699 // For cases within the target-specified limits, this is the best choice.
3700 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3701 if (ConstantSize) {
3702 // Memcpy with size zero? Just return the original chain.
3703 if (ConstantSize->isNullValue())
3704 return Chain;
3705
Evan Cheng255f20f2010-04-01 06:04:33 +00003706 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3707 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003708 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003709 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003710 return Result;
3711 }
3712
3713 // Then check to see if we should lower the memcpy with target-specific
3714 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003715 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003716 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003717 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003718 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003719 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003720 return Result;
3721
3722 // If we really need inline code and the target declined to provide it,
3723 // use a (potentially long) sequence of loads and stores.
3724 if (AlwaysInline) {
3725 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003726 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003727 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003728 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003729 }
3730
Dan Gohman7b55d362010-04-05 20:24:08 +00003731 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3732 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3733 // respect volatile, so they may do things like read or write memory
3734 // beyond the given memory regions. But fixing this isn't easy, and most
3735 // people don't care.
3736
Dan Gohman707e0182008-04-12 04:36:06 +00003737 // Emit a library call.
3738 TargetLowering::ArgListTy Args;
3739 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003740 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003741 Entry.Node = Dst; Args.push_back(Entry);
3742 Entry.Node = Src; Args.push_back(Entry);
3743 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003744 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003745 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003746 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003747 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003748 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3749 /*isTailCall=*/false,
3750 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003751 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003752 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003753 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003754 return CallResult.second;
3755}
3756
Dale Johannesen0f502f62009-02-03 22:26:09 +00003757SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003758 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003759 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003760 MachinePointerInfo DstPtrInfo,
3761 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003762
Dan Gohman21323f32008-05-29 19:42:22 +00003763 // Check to see if we should lower the memmove to loads and stores first.
3764 // For cases within the target-specified limits, this is the best choice.
3765 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3766 if (ConstantSize) {
3767 // Memmove with size zero? Just return the original chain.
3768 if (ConstantSize->isNullValue())
3769 return Chain;
3770
Dan Gohman475871a2008-07-27 21:46:04 +00003771 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003772 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003773 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003774 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003775 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003776 return Result;
3777 }
Dan Gohman707e0182008-04-12 04:36:06 +00003778
3779 // Then check to see if we should lower the memmove with target-specific
3780 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003781 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003782 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003783 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003784 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003785 return Result;
3786
Mon P Wang01f0e852010-04-06 08:27:51 +00003787 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3788 // not be safe. See memcpy above for more details.
3789
Dan Gohman707e0182008-04-12 04:36:06 +00003790 // Emit a library call.
3791 TargetLowering::ArgListTy Args;
3792 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003793 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003794 Entry.Node = Dst; Args.push_back(Entry);
3795 Entry.Node = Src; Args.push_back(Entry);
3796 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003797 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003798 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003799 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003800 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003801 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3802 /*isTailCall=*/false,
3803 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003804 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003805 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003806 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003807 return CallResult.second;
3808}
3809
Dale Johannesen0f502f62009-02-03 22:26:09 +00003810SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003811 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003812 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003813 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003814
3815 // Check to see if we should lower the memset to stores first.
3816 // For cases within the target-specified limits, this is the best choice.
3817 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3818 if (ConstantSize) {
3819 // Memset with size zero? Just return the original chain.
3820 if (ConstantSize->isNullValue())
3821 return Chain;
3822
Mon P Wang20adc9d2010-04-04 03:10:48 +00003823 SDValue Result =
3824 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003825 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003826
Gabor Greifba36cb52008-08-28 21:40:38 +00003827 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003828 return Result;
3829 }
3830
3831 // Then check to see if we should lower the memset with target-specific
3832 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003833 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003834 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003835 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003836 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003837 return Result;
3838
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003839 // Emit a library call.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003840 Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003841 TargetLowering::ArgListTy Args;
3842 TargetLowering::ArgListEntry Entry;
3843 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3844 Args.push_back(Entry);
3845 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 if (Src.getValueType().bitsGT(MVT::i32))
3847 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003848 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003849 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003850 Entry.Node = Src;
3851 Entry.Ty = Type::getInt32Ty(*getContext());
3852 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003853 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003854 Entry.Node = Size;
3855 Entry.Ty = IntPtrTy;
3856 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003857 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003858 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003859 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003860 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003861 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003862 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3863 /*isTailCall=*/false,
3864 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003865 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003866 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003867 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003868 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003869}
3870
Owen Andersone50ed302009-08-10 22:56:29 +00003871SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003872 SDValue Chain, SDValue Ptr, SDValue Cmp,
3873 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003874 unsigned Alignment,
3875 AtomicOrdering Ordering,
3876 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003877 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3878 Alignment = getEVTAlignment(MemVT);
3879
Evan Chengff89dcb2009-10-18 18:16:27 +00003880 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003881 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3882
3883 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003884 // FIXME: Volatile isn't really correct; we should keep track of atomic
3885 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003886 Flags |= MachineMemOperand::MOVolatile;
3887
3888 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00003889 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003890
Eli Friedman55ba8162011-07-29 03:05:32 +00003891 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
3892 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003893}
3894
3895SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3896 SDValue Chain,
3897 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00003898 SDValue Swp, MachineMemOperand *MMO,
3899 AtomicOrdering Ordering,
3900 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003901 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3902 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3903
Owen Andersone50ed302009-08-10 22:56:29 +00003904 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003905
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003907 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003908 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003909 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3910 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3911 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003912 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3913 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003914 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003915 }
Dan Gohman95531882010-03-18 18:49:47 +00003916 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003917 Ptr, Cmp, Swp, MMO, Ordering,
3918 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003919 CSEMap.InsertNode(N, IP);
3920 AllNodes.push_back(N);
3921 return SDValue(N, 0);
3922}
3923
Owen Andersone50ed302009-08-10 22:56:29 +00003924SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003925 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003926 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003927 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00003928 unsigned Alignment,
3929 AtomicOrdering Ordering,
3930 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003931 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3932 Alignment = getEVTAlignment(MemVT);
3933
Evan Chengff89dcb2009-10-18 18:16:27 +00003934 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00003935 // A monotonic store does not load; a release store "loads" in the sense
3936 // that other stores cannot be sunk past it.
3937 // (An atomicrmw obviously both loads and stores.)
3938 unsigned Flags = MachineMemOperand::MOStore;
3939 if (Opcode != ISD::ATOMIC_STORE || Ordering > Monotonic)
3940 Flags |= MachineMemOperand::MOLoad;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003941
3942 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003943 // FIXME: Volatile isn't really correct; we should keep track of atomic
3944 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003945 Flags |= MachineMemOperand::MOVolatile;
3946
3947 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00003948 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00003949 MemVT.getStoreSize(), Alignment);
3950
Eli Friedman55ba8162011-07-29 03:05:32 +00003951 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
3952 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003953}
3954
3955SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3956 SDValue Chain,
3957 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00003958 MachineMemOperand *MMO,
3959 AtomicOrdering Ordering,
3960 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003961 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3962 Opcode == ISD::ATOMIC_LOAD_SUB ||
3963 Opcode == ISD::ATOMIC_LOAD_AND ||
3964 Opcode == ISD::ATOMIC_LOAD_OR ||
3965 Opcode == ISD::ATOMIC_LOAD_XOR ||
3966 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003967 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003968 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003969 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003970 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00003971 Opcode == ISD::ATOMIC_SWAP ||
3972 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003973 "Invalid Atomic Op");
3974
Owen Andersone50ed302009-08-10 22:56:29 +00003975 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003976
Eli Friedman327236c2011-08-24 20:50:09 +00003977 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
3978 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003979 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003980 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003981 SDValue Ops[] = {Chain, Ptr, Val};
3982 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3983 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003984 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3985 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003986 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003987 }
Dan Gohman95531882010-03-18 18:49:47 +00003988 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003989 Ptr, Val, MMO,
3990 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003991 CSEMap.InsertNode(N, IP);
3992 AllNodes.push_back(N);
3993 return SDValue(N, 0);
3994}
3995
Eli Friedman327236c2011-08-24 20:50:09 +00003996SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3997 EVT VT, SDValue Chain,
3998 SDValue Ptr,
3999 const Value* PtrVal,
4000 unsigned Alignment,
4001 AtomicOrdering Ordering,
4002 SynchronizationScope SynchScope) {
4003 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4004 Alignment = getEVTAlignment(MemVT);
4005
4006 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00004007 // A monotonic load does not store; an acquire load "stores" in the sense
4008 // that other loads cannot be hoisted past it.
4009 unsigned Flags = MachineMemOperand::MOLoad;
4010 if (Ordering > Monotonic)
4011 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004012
4013 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004014 // FIXME: Volatile isn't really correct; we should keep track of atomic
4015 // orderings in the memoperand.
Eli Friedman327236c2011-08-24 20:50:09 +00004016 Flags |= MachineMemOperand::MOVolatile;
4017
4018 MachineMemOperand *MMO =
4019 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4020 MemVT.getStoreSize(), Alignment);
4021
4022 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4023 Ordering, SynchScope);
4024}
4025
4026SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4027 EVT VT, SDValue Chain,
4028 SDValue Ptr,
4029 MachineMemOperand *MMO,
4030 AtomicOrdering Ordering,
4031 SynchronizationScope SynchScope) {
4032 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4033
4034 SDVTList VTs = getVTList(VT, MVT::Other);
4035 FoldingSetNodeID ID;
4036 ID.AddInteger(MemVT.getRawBits());
4037 SDValue Ops[] = {Chain, Ptr};
4038 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
4039 void* IP = 0;
4040 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4041 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4042 return SDValue(E, 0);
4043 }
4044 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4045 Ptr, MMO, Ordering, SynchScope);
4046 CSEMap.InsertNode(N, IP);
4047 AllNodes.push_back(N);
4048 return SDValue(N, 0);
4049}
4050
Duncan Sands4bdcb612008-07-02 17:40:58 +00004051/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004052SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4053 DebugLoc dl) {
4054 if (NumOps == 1)
4055 return Ops[0];
4056
Owen Andersone50ed302009-08-10 22:56:29 +00004057 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004058 VTs.reserve(NumOps);
4059 for (unsigned i = 0; i < NumOps; ++i)
4060 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004061 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004062 Ops, NumOps);
4063}
4064
Dan Gohman475871a2008-07-27 21:46:04 +00004065SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004066SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004067 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004068 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004069 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004070 unsigned Align, bool Vol,
4071 bool ReadMem, bool WriteMem) {
4072 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004073 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004074 ReadMem, WriteMem);
4075}
4076
4077SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004078SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4079 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004080 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004081 unsigned Align, bool Vol,
4082 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004083 if (Align == 0) // Ensure that codegen never sees alignment 0
4084 Align = getEVTAlignment(MemVT);
4085
4086 MachineFunction &MF = getMachineFunction();
4087 unsigned Flags = 0;
4088 if (WriteMem)
4089 Flags |= MachineMemOperand::MOStore;
4090 if (ReadMem)
4091 Flags |= MachineMemOperand::MOLoad;
4092 if (Vol)
4093 Flags |= MachineMemOperand::MOVolatile;
4094 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004095 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004096
4097 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4098}
4099
4100SDValue
4101SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4102 const SDValue *Ops, unsigned NumOps,
4103 EVT MemVT, MachineMemOperand *MMO) {
4104 assert((Opcode == ISD::INTRINSIC_VOID ||
4105 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004106 Opcode == ISD::PREFETCH ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004107 (Opcode <= INT_MAX &&
4108 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4109 "Opcode is not a memory-accessing opcode!");
4110
Dale Johannesene8c17332009-01-29 00:47:48 +00004111 // Memoize the node unless it returns a flag.
4112 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004113 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004114 FoldingSetNodeID ID;
4115 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4116 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004117 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4118 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004119 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004121
Dan Gohman95531882010-03-18 18:49:47 +00004122 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4123 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004124 CSEMap.InsertNode(N, IP);
4125 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004126 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4127 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004128 }
4129 AllNodes.push_back(N);
4130 return SDValue(N, 0);
4131}
4132
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004133/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4134/// MachinePointerInfo record from it. This is particularly useful because the
4135/// code generator has many cases where it doesn't bother passing in a
4136/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4137static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4138 // If this is FI+Offset, we can model it.
4139 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4140 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4141
4142 // If this is (FI+Offset1)+Offset2, we can model it.
4143 if (Ptr.getOpcode() != ISD::ADD ||
4144 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4145 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4146 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004147
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004148 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4149 return MachinePointerInfo::getFixedStack(FI, Offset+
4150 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4151}
4152
4153/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4154/// MachinePointerInfo record from it. This is particularly useful because the
4155/// code generator has many cases where it doesn't bother passing in a
4156/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4157static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4158 // If the 'Offset' value isn't a constant, we can't handle this.
4159 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4160 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4161 if (OffsetOp.getOpcode() == ISD::UNDEF)
4162 return InferPointerInfo(Ptr);
4163 return MachinePointerInfo();
4164}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004165
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004166
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004167SDValue
4168SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4169 EVT VT, DebugLoc dl, SDValue Chain,
4170 SDValue Ptr, SDValue Offset,
4171 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004172 bool isVolatile, bool isNonTemporal, bool isInvariant,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004173 unsigned Alignment, const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004174 assert(Chain.getValueType() == MVT::Other &&
4175 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004176 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4177 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004178
Dan Gohmanc76909a2009-09-25 20:36:54 +00004179 unsigned Flags = MachineMemOperand::MOLoad;
4180 if (isVolatile)
4181 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004182 if (isNonTemporal)
4183 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004184 if (isInvariant)
4185 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004186
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004187 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4188 // clients.
4189 if (PtrInfo.V == 0)
4190 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004191
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004192 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004193 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004194 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
4195 TBAAInfo);
Evan Chengbcc80172010-07-07 22:15:37 +00004196 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004197}
4198
4199SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004200SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004201 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004202 SDValue Ptr, SDValue Offset, EVT MemVT,
4203 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004204 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004205 ExtType = ISD::NON_EXTLOAD;
4206 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004207 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004208 } else {
4209 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004210 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4211 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004212 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004213 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004214 assert(VT.isVector() == MemVT.isVector() &&
4215 "Cannot use trunc store to convert to or from a vector!");
4216 assert((!VT.isVector() ||
4217 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4218 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004219 }
4220
4221 bool Indexed = AM != ISD::UNINDEXED;
4222 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4223 "Unindexed load with an offset!");
4224
4225 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004226 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004227 SDValue Ops[] = { Chain, Ptr, Offset };
4228 FoldingSetNodeID ID;
4229 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004230 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004231 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004232 MMO->isNonTemporal(),
4233 MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004234 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004235 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4236 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004237 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004238 }
Dan Gohman95531882010-03-18 18:49:47 +00004239 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4240 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004241 CSEMap.InsertNode(N, IP);
4242 AllNodes.push_back(N);
4243 return SDValue(N, 0);
4244}
4245
Owen Andersone50ed302009-08-10 22:56:29 +00004246SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004247 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004248 MachinePointerInfo PtrInfo,
4249 bool isVolatile, bool isNonTemporal,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004250 bool isInvariant, unsigned Alignment,
4251 const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004252 SDValue Undef = getUNDEF(Ptr.getValueType());
4253 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004254 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4255 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004256}
Dale Johannesene8c17332009-01-29 00:47:48 +00004257
Stuart Hastingsa9011292011-02-16 16:23:55 +00004258SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004259 SDValue Chain, SDValue Ptr,
4260 MachinePointerInfo PtrInfo, EVT MemVT,
4261 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004262 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004263 SDValue Undef = getUNDEF(Ptr.getValueType());
4264 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004265 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004266 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004267}
4268
4269
Dan Gohman475871a2008-07-27 21:46:04 +00004270SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004271SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4272 SDValue Offset, ISD::MemIndexedMode AM) {
4273 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4274 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4275 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004276 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004277 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004278 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
4279 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004280}
4281
Dale Johannesene8c17332009-01-29 00:47:48 +00004282SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004283 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004284 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004285 unsigned Alignment, const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004286 assert(Chain.getValueType() == MVT::Other &&
4287 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004288 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004289 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004290
Dan Gohmanc76909a2009-09-25 20:36:54 +00004291 unsigned Flags = MachineMemOperand::MOStore;
4292 if (isVolatile)
4293 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004294 if (isNonTemporal)
4295 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004296
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004297 if (PtrInfo.V == 0)
4298 PtrInfo = InferPointerInfo(Ptr);
4299
4300 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004301 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004302 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004303 Val.getValueType().getStoreSize(), Alignment,
4304 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004305
4306 return getStore(Chain, dl, Val, Ptr, MMO);
4307}
4308
4309SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4310 SDValue Ptr, MachineMemOperand *MMO) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004311 assert(Chain.getValueType() == MVT::Other &&
4312 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004313 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004314 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004315 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004316 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4317 FoldingSetNodeID ID;
4318 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004319 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004320 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004321 MMO->isNonTemporal(), MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004322 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004323 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4324 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004325 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004326 }
Dan Gohman95531882010-03-18 18:49:47 +00004327 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4328 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004329 CSEMap.InsertNode(N, IP);
4330 AllNodes.push_back(N);
4331 return SDValue(N, 0);
4332}
4333
Dale Johannesene8c17332009-01-29 00:47:48 +00004334SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004335 SDValue Ptr, MachinePointerInfo PtrInfo,
4336 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004337 unsigned Alignment,
4338 const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004339 assert(Chain.getValueType() == MVT::Other &&
4340 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004341 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4342 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004343
Dan Gohmanc76909a2009-09-25 20:36:54 +00004344 unsigned Flags = MachineMemOperand::MOStore;
4345 if (isVolatile)
4346 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004347 if (isNonTemporal)
4348 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004349
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004350 if (PtrInfo.V == 0)
4351 PtrInfo = InferPointerInfo(Ptr);
4352
4353 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004354 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004355 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4356 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004357
4358 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4359}
4360
4361SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4362 SDValue Ptr, EVT SVT,
4363 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004364 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004365
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004366 assert(Chain.getValueType() == MVT::Other &&
4367 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004368 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004369 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004370
Dan Gohman2e141d72009-12-14 23:40:38 +00004371 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4372 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004373 assert(VT.isInteger() == SVT.isInteger() &&
4374 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004375 assert(VT.isVector() == SVT.isVector() &&
4376 "Cannot use trunc store to convert to or from a vector!");
4377 assert((!VT.isVector() ||
4378 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4379 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004380
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004382 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004383 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4384 FoldingSetNodeID ID;
4385 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004386 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004387 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004388 MMO->isNonTemporal(), MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004389 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004390 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4391 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004392 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004393 }
Dan Gohman95531882010-03-18 18:49:47 +00004394 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4395 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004396 CSEMap.InsertNode(N, IP);
4397 AllNodes.push_back(N);
4398 return SDValue(N, 0);
4399}
4400
Dan Gohman475871a2008-07-27 21:46:04 +00004401SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004402SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4403 SDValue Offset, ISD::MemIndexedMode AM) {
4404 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4405 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4406 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004407 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004408 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4409 FoldingSetNodeID ID;
4410 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004411 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004412 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00004413 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004414 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004415 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004416
Dan Gohman95531882010-03-18 18:49:47 +00004417 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4418 ST->isTruncatingStore(),
4419 ST->getMemoryVT(),
4420 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004421 CSEMap.InsertNode(N, IP);
4422 AllNodes.push_back(N);
4423 return SDValue(N, 0);
4424}
4425
Owen Andersone50ed302009-08-10 22:56:29 +00004426SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004427 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004428 SDValue SV,
4429 unsigned Align) {
4430 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4431 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004432}
4433
Owen Andersone50ed302009-08-10 22:56:29 +00004434SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004435 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004436 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004437 case 0: return getNode(Opcode, DL, VT);
4438 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4439 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4440 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004441 default: break;
4442 }
4443
Dan Gohman475871a2008-07-27 21:46:04 +00004444 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004445 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004446 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004447 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004448}
4449
Owen Andersone50ed302009-08-10 22:56:29 +00004450SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004451 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004452 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004453 case 0: return getNode(Opcode, DL, VT);
4454 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4455 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4456 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004457 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004459
Chris Lattneref847df2005-04-09 03:27:28 +00004460 switch (Opcode) {
4461 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004462 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004463 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004464 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4465 "LHS and RHS of condition must have same type!");
4466 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4467 "True and False arms of SelectCC must have same type!");
4468 assert(Ops[2].getValueType() == VT &&
4469 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004470 break;
4471 }
4472 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004473 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004474 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4475 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004476 break;
4477 }
Chris Lattneref847df2005-04-09 03:27:28 +00004478 }
4479
Chris Lattner385328c2005-05-14 07:42:29 +00004480 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004481 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004482 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004483
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004484 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004485 FoldingSetNodeID ID;
4486 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004487 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004488
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004489 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004490 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004491
Dan Gohman95531882010-03-18 18:49:47 +00004492 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004493 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004494 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004495 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004496 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004497
Chris Lattneref847df2005-04-09 03:27:28 +00004498 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004499#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004500 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004501#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004502 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004503}
4504
Bill Wendling7ade28c2009-01-28 22:17:52 +00004505SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004506 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004507 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004508 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004509 Ops, NumOps);
4510}
4511
Bill Wendling7ade28c2009-01-28 22:17:52 +00004512SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004513 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004514 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004515 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004516 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4517 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004518}
4519
Bill Wendling7ade28c2009-01-28 22:17:52 +00004520SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4521 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004522 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004523 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004524
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004525#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004526 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004527 // FIXME: figure out how to safely handle things like
4528 // int foo(int x) { return 1 << (x & 255); }
4529 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004530 case ISD::SRA_PARTS:
4531 case ISD::SRL_PARTS:
4532 case ISD::SHL_PARTS:
4533 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004534 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004535 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004536 else if (N3.getOpcode() == ISD::AND)
4537 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4538 // If the and is only masking out bits that cannot effect the shift,
4539 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004540 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004541 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004542 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004543 }
4544 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004545 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004546#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004547
Chris Lattner43247a12005-08-25 19:12:10 +00004548 // Memoize the node unless it returns a flag.
4549 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004550 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004551 FoldingSetNodeID ID;
4552 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004553 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004554 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004555 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004556
Dan Gohman0e5f1302008-07-07 23:02:41 +00004557 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004558 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004559 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004560 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004561 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004562 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4563 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004564 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004565 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004566 }
Chris Lattnera5682852006-08-07 23:03:03 +00004567 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004568 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004569 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004570 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004571 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004572 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004573 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004574 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4575 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004576 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004577 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004578 }
Chris Lattner43247a12005-08-25 19:12:10 +00004579 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004580 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004581#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004582 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004583#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004584 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004585}
4586
Bill Wendling7ade28c2009-01-28 22:17:52 +00004587SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4588 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004589}
4590
Bill Wendling7ade28c2009-01-28 22:17:52 +00004591SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4592 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004593 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004594 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004595}
4596
Bill Wendling7ade28c2009-01-28 22:17:52 +00004597SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4598 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004599 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004600 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004601}
4602
Bill Wendling7ade28c2009-01-28 22:17:52 +00004603SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4604 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004605 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004606 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004607}
4608
Bill Wendling7ade28c2009-01-28 22:17:52 +00004609SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4610 SDValue N1, SDValue N2, SDValue N3,
4611 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004612 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004613 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004614}
4615
Bill Wendling7ade28c2009-01-28 22:17:52 +00004616SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4617 SDValue N1, SDValue N2, SDValue N3,
4618 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004619 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004620 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004621}
4622
Owen Andersone50ed302009-08-10 22:56:29 +00004623SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004624 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004625}
4626
Owen Andersone50ed302009-08-10 22:56:29 +00004627SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004628 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4629 E = VTList.rend(); I != E; ++I)
4630 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4631 return *I;
4632
Owen Andersone50ed302009-08-10 22:56:29 +00004633 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004634 Array[0] = VT1;
4635 Array[1] = VT2;
4636 SDVTList Result = makeVTList(Array, 2);
4637 VTList.push_back(Result);
4638 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004639}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004640
Owen Andersone50ed302009-08-10 22:56:29 +00004641SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004642 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4643 E = VTList.rend(); I != E; ++I)
4644 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4645 I->VTs[2] == VT3)
4646 return *I;
4647
Owen Andersone50ed302009-08-10 22:56:29 +00004648 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004649 Array[0] = VT1;
4650 Array[1] = VT2;
4651 Array[2] = VT3;
4652 SDVTList Result = makeVTList(Array, 3);
4653 VTList.push_back(Result);
4654 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004655}
4656
Owen Andersone50ed302009-08-10 22:56:29 +00004657SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004658 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4659 E = VTList.rend(); I != E; ++I)
4660 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4661 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4662 return *I;
4663
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004664 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004665 Array[0] = VT1;
4666 Array[1] = VT2;
4667 Array[2] = VT3;
4668 Array[3] = VT4;
4669 SDVTList Result = makeVTList(Array, 4);
4670 VTList.push_back(Result);
4671 return Result;
4672}
4673
Owen Andersone50ed302009-08-10 22:56:29 +00004674SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004675 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004676 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004677 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004678 case 2: return getVTList(VTs[0], VTs[1]);
4679 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004680 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004681 default: break;
4682 }
4683
Dan Gohmane8be6c62008-07-17 19:10:17 +00004684 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4685 E = VTList.rend(); I != E; ++I) {
4686 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4687 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004688
Chris Lattner70046e92006-08-15 17:46:01 +00004689 bool NoMatch = false;
4690 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004691 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004692 NoMatch = true;
4693 break;
4694 }
4695 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004696 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004698
Owen Andersone50ed302009-08-10 22:56:29 +00004699 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004700 std::copy(VTs, VTs+NumVTs, Array);
4701 SDVTList Result = makeVTList(Array, NumVTs);
4702 VTList.push_back(Result);
4703 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004704}
4705
4706
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004707/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4708/// specified operands. If the resultant node already exists in the DAG,
4709/// this does not modify the specified node, instead it returns the node that
4710/// already exists. If the resultant node does not exist in the DAG, the
4711/// input node is returned. As a degenerate case, if you specify the same
4712/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004713SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004714 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004715
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004716 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004717 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004718
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004719 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004720 void *InsertPos = 0;
4721 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004722 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004723
Dan Gohman79acd2b2008-07-21 22:38:59 +00004724 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004725 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004726 if (!RemoveNodeFromCSEMaps(N))
4727 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004728
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004729 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004730 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004731
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004732 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004733 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004734 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004735}
4736
Dan Gohman027657d2010-06-18 15:30:29 +00004737SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004738 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004739
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004740 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004741 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004742 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004743
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004744 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004745 void *InsertPos = 0;
4746 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004747 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004748
Dan Gohman79acd2b2008-07-21 22:38:59 +00004749 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004750 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004751 if (!RemoveNodeFromCSEMaps(N))
4752 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004753
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004754 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004755 if (N->OperandList[0] != Op1)
4756 N->OperandList[0].set(Op1);
4757 if (N->OperandList[1] != Op2)
4758 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004759
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004760 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004761 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004762 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004763}
4764
Dan Gohman027657d2010-06-18 15:30:29 +00004765SDNode *SelectionDAG::
4766UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004767 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004768 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004769}
4770
Dan Gohman027657d2010-06-18 15:30:29 +00004771SDNode *SelectionDAG::
4772UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004773 SDValue Op3, SDValue Op4) {
4774 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004775 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004776}
4777
Dan Gohman027657d2010-06-18 15:30:29 +00004778SDNode *SelectionDAG::
4779UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004780 SDValue Op3, SDValue Op4, SDValue Op5) {
4781 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004782 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004783}
4784
Dan Gohman027657d2010-06-18 15:30:29 +00004785SDNode *SelectionDAG::
4786UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004787 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004788 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004789
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004790 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004791 bool AnyChange = false;
4792 for (unsigned i = 0; i != NumOps; ++i) {
4793 if (Ops[i] != N->getOperand(i)) {
4794 AnyChange = true;
4795 break;
4796 }
4797 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004798
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004799 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004800 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004801
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004802 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004803 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004804 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004805 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004806
Dan Gohman7ceda162008-05-02 00:05:03 +00004807 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004808 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004809 if (!RemoveNodeFromCSEMaps(N))
4810 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004811
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004812 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004813 for (unsigned i = 0; i != NumOps; ++i)
4814 if (N->OperandList[i] != Ops[i])
4815 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004816
4817 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004818 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004819 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004820}
4821
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004822/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004823/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004824void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004825 // Unlike the code in MorphNodeTo that does this, we don't need to
4826 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004827 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4828 SDUse &Use = *I++;
4829 Use.set(SDValue());
4830 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004831}
Chris Lattner149c58c2005-08-16 18:17:10 +00004832
Dan Gohmane8be6c62008-07-17 19:10:17 +00004833/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4834/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004835///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004836SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004837 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004838 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004839 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004840}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004841
Dan Gohmane8be6c62008-07-17 19:10:17 +00004842SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004843 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004844 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004845 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004846 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004847}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004848
Dan Gohmane8be6c62008-07-17 19:10:17 +00004849SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004850 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004851 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004852 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004853 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004854 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004855}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004856
Dan Gohmane8be6c62008-07-17 19:10:17 +00004857SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004858 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004860 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004861 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004862 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004863}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004864
Dan Gohmane8be6c62008-07-17 19:10:17 +00004865SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004866 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004867 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004868 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004869 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004870}
4871
Dan Gohmane8be6c62008-07-17 19:10:17 +00004872SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004873 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004874 unsigned NumOps) {
4875 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004876 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004877}
4878
Dan Gohmane8be6c62008-07-17 19:10:17 +00004879SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004880 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004881 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004882 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004883}
4884
Dan Gohmane8be6c62008-07-17 19:10:17 +00004885SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004886 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004887 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004888 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004889 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004890}
4891
Bill Wendling13d6d442008-12-01 23:28:22 +00004892SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004893 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004894 const SDValue *Ops, unsigned NumOps) {
4895 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4896 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4897}
4898
Scott Michelfdc40a02009-02-17 22:15:04 +00004899SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004900 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004901 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004902 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004903 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004904 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004905}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004906
Scott Michelfdc40a02009-02-17 22:15:04 +00004907SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004908 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004909 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004910 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004911 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004912 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004913}
4914
Dan Gohmane8be6c62008-07-17 19:10:17 +00004915SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004916 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004917 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004918 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004919 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004921 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004922}
4923
Dan Gohmane8be6c62008-07-17 19:10:17 +00004924SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004925 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004926 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004927 SDValue Op3) {
4928 SDVTList VTs = getVTList(VT1, VT2, VT3);
4929 SDValue Ops[] = { Op1, Op2, Op3 };
4930 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4931}
4932
4933SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004935 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00004936 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4937 // Reset the NodeID to -1.
4938 N->setNodeId(-1);
4939 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004940}
4941
Devang Patel0508d042011-12-15 18:21:18 +00004942/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
4943/// the line number information on the merged node since it is not possible to
4944/// preserve the information that operation is associated with multiple lines.
4945/// This will make the debugger working better at -O0, were there is a higher
4946/// probability having other instructions associated with that line.
4947///
4948SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
4949 DebugLoc NLoc = N->getDebugLoc();
4950 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
4951 N->setDebugLoc(DebugLoc());
4952 }
4953 return N;
4954}
4955
Chris Lattner21221e32010-02-28 21:36:14 +00004956/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00004957/// return type, opcode, and operands.
4958///
4959/// Note that MorphNodeTo returns the resultant node. If there is already a
4960/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004961/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004962///
4963/// Using MorphNodeTo is faster than creating a new node and swapping it in
4964/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004965/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004966/// the node's users.
4967///
4968SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004969 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004970 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004971 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004972 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004973 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004974 FoldingSetNodeID ID;
4975 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004976 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00004977 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004978 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004979
Dan Gohman095cc292008-09-13 01:54:27 +00004980 if (!RemoveNodeFromCSEMaps(N))
4981 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004982
Dan Gohmane8be6c62008-07-17 19:10:17 +00004983 // Start the morphing.
4984 N->NodeType = Opc;
4985 N->ValueList = VTs.VTs;
4986 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004987
Dan Gohmane8be6c62008-07-17 19:10:17 +00004988 // Clear the operands list, updating used nodes to remove this from their
4989 // use list. Keep track of any operands that become dead as a result.
4990 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004991 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4992 SDUse &Use = *I++;
4993 SDNode *Used = Use.getNode();
4994 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004995 if (Used->use_empty())
4996 DeadNodeSet.insert(Used);
4997 }
4998
Dan Gohmanc76909a2009-09-25 20:36:54 +00004999 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5000 // Initialize the memory references information.
5001 MN->setMemRefs(0, 0);
5002 // If NumOps is larger than the # of operands we can have in a
5003 // MachineSDNode, reallocate the operand list.
5004 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5005 if (MN->OperandsNeedDelete)
5006 delete[] MN->OperandList;
5007 if (NumOps > array_lengthof(MN->LocalOperands))
5008 // We're creating a final node that will live unmorphed for the
5009 // remainder of the current SelectionDAG iteration, so we can allocate
5010 // the operands directly out of a pool with no recycling metadata.
5011 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005012 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005013 else
5014 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5015 MN->OperandsNeedDelete = false;
5016 } else
5017 MN->InitOperands(MN->OperandList, Ops, NumOps);
5018 } else {
5019 // If NumOps is larger than the # of operands we currently have, reallocate
5020 // the operand list.
5021 if (NumOps > N->NumOperands) {
5022 if (N->OperandsNeedDelete)
5023 delete[] N->OperandList;
5024 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005025 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005026 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005027 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005028 }
5029
5030 // Delete any nodes that are still dead after adding the uses for the
5031 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005032 if (!DeadNodeSet.empty()) {
5033 SmallVector<SDNode *, 16> DeadNodes;
5034 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5035 E = DeadNodeSet.end(); I != E; ++I)
5036 if ((*I)->use_empty())
5037 DeadNodes.push_back(*I);
5038 RemoveDeadNodes(DeadNodes);
5039 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005040
Dan Gohmane8be6c62008-07-17 19:10:17 +00005041 if (IP)
5042 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005043 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005044}
5045
Chris Lattner0fb094f2005-11-19 01:44:53 +00005046
Dan Gohman602b0c82009-09-25 18:54:59 +00005047/// getMachineNode - These are used for target selectors to create a new node
5048/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005049///
Dan Gohman602b0c82009-09-25 18:54:59 +00005050/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005051/// node of the specified opcode and operands, it returns that node instead of
5052/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005053MachineSDNode *
5054SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005055 SDVTList VTs = getVTList(VT);
5056 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005057}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005058
Dan Gohmanc81b7832009-10-10 01:29:16 +00005059MachineSDNode *
5060SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005061 SDVTList VTs = getVTList(VT);
5062 SDValue Ops[] = { Op1 };
5063 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005064}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005065
Dan Gohmanc81b7832009-10-10 01:29:16 +00005066MachineSDNode *
5067SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5068 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005069 SDVTList VTs = getVTList(VT);
5070 SDValue Ops[] = { Op1, Op2 };
5071 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005072}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005073
Dan Gohmanc81b7832009-10-10 01:29:16 +00005074MachineSDNode *
5075SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5076 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005077 SDVTList VTs = getVTList(VT);
5078 SDValue Ops[] = { Op1, Op2, Op3 };
5079 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005080}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005081
Dan Gohmanc81b7832009-10-10 01:29:16 +00005082MachineSDNode *
5083SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5084 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005085 SDVTList VTs = getVTList(VT);
5086 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005087}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005088
Dan Gohmanc81b7832009-10-10 01:29:16 +00005089MachineSDNode *
5090SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005091 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005092 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005093}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005094
Dan Gohmanc81b7832009-10-10 01:29:16 +00005095MachineSDNode *
5096SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5097 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005098 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005099 SDValue Ops[] = { Op1 };
5100 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005101}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005102
Dan Gohmanc81b7832009-10-10 01:29:16 +00005103MachineSDNode *
5104SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5105 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005106 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005107 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005108 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005109}
5110
Dan Gohmanc81b7832009-10-10 01:29:16 +00005111MachineSDNode *
5112SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5113 EVT VT1, EVT VT2, SDValue Op1,
5114 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005115 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005116 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005117 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005118}
5119
Dan Gohmanc81b7832009-10-10 01:29:16 +00005120MachineSDNode *
5121SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5122 EVT VT1, EVT VT2,
5123 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005124 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005125 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
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,
5130 EVT VT1, EVT VT2, EVT VT3,
5131 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005132 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005133 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005134 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005135}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005136
Dan Gohmanc81b7832009-10-10 01:29:16 +00005137MachineSDNode *
5138SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5139 EVT VT1, EVT VT2, EVT VT3,
5140 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005141 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005142 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005143 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005144}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005145
Dan Gohmanc81b7832009-10-10 01:29:16 +00005146MachineSDNode *
5147SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5148 EVT VT1, EVT VT2, EVT VT3,
5149 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005150 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005151 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005152}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005153
Dan Gohmanc81b7832009-10-10 01:29:16 +00005154MachineSDNode *
5155SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5156 EVT VT2, EVT VT3, EVT VT4,
5157 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005158 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005159 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005160}
5161
Dan Gohmanc81b7832009-10-10 01:29:16 +00005162MachineSDNode *
5163SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5164 const std::vector<EVT> &ResultTys,
5165 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005166 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5167 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5168}
5169
Dan Gohmanc81b7832009-10-10 01:29:16 +00005170MachineSDNode *
5171SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5172 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005173 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005174 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005175 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005176
5177 if (DoCSE) {
5178 FoldingSetNodeID ID;
5179 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5180 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005181 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5182 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5183 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005184 }
5185
5186 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005187 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005188
5189 // Initialize the operands list.
5190 if (NumOps > array_lengthof(N->LocalOperands))
5191 // We're creating a final node that will live unmorphed for the
5192 // remainder of the current SelectionDAG iteration, so we can allocate
5193 // the operands directly out of a pool with no recycling metadata.
5194 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5195 Ops, NumOps);
5196 else
5197 N->InitOperands(N->LocalOperands, Ops, NumOps);
5198 N->OperandsNeedDelete = false;
5199
5200 if (DoCSE)
5201 CSEMap.InsertNode(N, IP);
5202
5203 AllNodes.push_back(N);
5204#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005205 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005206#endif
5207 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005208}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005209
Dan Gohman6a402dc2009-08-19 18:16:17 +00005210/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005211/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005212SDValue
5213SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5214 SDValue Operand) {
5215 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005216 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005217 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005218 return SDValue(Subreg, 0);
5219}
5220
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005221/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005222/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005223SDValue
5224SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5225 SDValue Operand, SDValue Subreg) {
5226 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005227 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005228 VT, Operand, Subreg, SRIdxVal);
5229 return SDValue(Result, 0);
5230}
5231
Evan Cheng08b11732008-03-22 01:55:50 +00005232/// getNodeIfExists - Get the specified node if it's already available, or
5233/// else return NULL.
5234SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005235 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005236 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005237 FoldingSetNodeID ID;
5238 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5239 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005240 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005241 return E;
5242 }
5243 return NULL;
5244}
5245
Evan Cheng31441b72010-03-29 20:48:30 +00005246/// getDbgValue - Creates a SDDbgValue node.
5247///
5248SDDbgValue *
5249SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5250 DebugLoc DL, unsigned O) {
5251 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5252}
5253
5254SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005255SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005256 DebugLoc DL, unsigned O) {
5257 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5258}
5259
5260SDDbgValue *
5261SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5262 DebugLoc DL, unsigned O) {
5263 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5264}
5265
Dan Gohmana72d2a22010-03-03 21:33:37 +00005266namespace {
5267
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005268/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005269/// pointed to by a use iterator is deleted, increment the use iterator
5270/// so that it doesn't dangle.
5271///
5272/// This class also manages a "downlink" DAGUpdateListener, to forward
5273/// messages to ReplaceAllUsesWith's callers.
5274///
5275class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
5276 SelectionDAG::DAGUpdateListener *DownLink;
5277 SDNode::use_iterator &UI;
5278 SDNode::use_iterator &UE;
5279
5280 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5281 // Increment the iterator as needed.
5282 while (UI != UE && N == *UI)
5283 ++UI;
5284
5285 // Then forward the message.
5286 if (DownLink) DownLink->NodeDeleted(N, E);
5287 }
5288
5289 virtual void NodeUpdated(SDNode *N) {
5290 // Just forward the message.
5291 if (DownLink) DownLink->NodeUpdated(N);
5292 }
5293
5294public:
5295 RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl,
5296 SDNode::use_iterator &ui,
5297 SDNode::use_iterator &ue)
5298 : DownLink(dl), UI(ui), UE(ue) {}
5299};
5300
5301}
5302
Evan Cheng99157a02006-08-07 22:13:29 +00005303/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005304/// This can cause recursive merging of nodes in the DAG.
5305///
Chris Lattner11d049c2008-02-03 03:35:22 +00005306/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005307///
Dan Gohman475871a2008-07-27 21:46:04 +00005308void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005309 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005310 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005311 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005312 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005313 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005314
Dan Gohman39946102009-01-25 16:29:12 +00005315 // Iterate over all the existing uses of From. New uses will be added
5316 // to the beginning of the use list, which we avoid visiting.
5317 // This specifically avoids visiting uses of From that arise while the
5318 // replacement is happening, because any such uses would be the result
5319 // of CSE: If an existing node looks like From after one of its operands
5320 // is replaced by To, we don't want to replace of all its users with To
5321 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005322 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005323 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005324 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005325 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005326
Chris Lattner8b8749f2005-08-17 19:00:20 +00005327 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005328 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005329
Dan Gohman39946102009-01-25 16:29:12 +00005330 // A user can appear in a use list multiple times, and when this
5331 // happens the uses are usually next to each other in the list.
5332 // To help reduce the number of CSE recomputations, process all
5333 // the uses of this user that we can find this way.
5334 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005335 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005336 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005337 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005338 } while (UI != UE && *UI == User);
5339
5340 // Now that we have modified User, add it back to the CSE maps. If it
5341 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005342 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner8b52f212005-08-26 18:36:28 +00005343 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005344
5345 // If we just RAUW'd the root, take note.
5346 if (FromN == getRoot())
5347 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005348}
5349
5350/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5351/// This can cause recursive merging of nodes in the DAG.
5352///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005353/// This version assumes that for each value of From, there is a
5354/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005355///
Chris Lattner1e111c72005-09-07 05:37:01 +00005356void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005357 DAGUpdateListener *UpdateListener) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005358#ifndef NDEBUG
5359 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5360 assert((!From->hasAnyUseOfValue(i) ||
5361 From->getValueType(i) == To->getValueType(i)) &&
5362 "Cannot use this version of ReplaceAllUsesWith!");
5363#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005364
5365 // Handle the trivial case.
5366 if (From == To)
5367 return;
5368
Dan Gohmandbe664a2009-01-19 21:44:21 +00005369 // Iterate over just the existing users of From. See the comments in
5370 // the ReplaceAllUsesWith above.
5371 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005372 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005373 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005374 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005375
Chris Lattner8b52f212005-08-26 18:36:28 +00005376 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005377 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005378
Dan Gohman39946102009-01-25 16:29:12 +00005379 // A user can appear in a use list multiple times, and when this
5380 // happens the uses are usually next to each other in the list.
5381 // To help reduce the number of CSE recomputations, process all
5382 // the uses of this user that we can find this way.
5383 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005384 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005385 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005386 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005387 } while (UI != UE && *UI == User);
5388
5389 // Now that we have modified User, add it back to the CSE maps. If it
5390 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005391 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005392 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005393
5394 // If we just RAUW'd the root, take note.
5395 if (From == getRoot().getNode())
5396 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005397}
5398
Chris Lattner8b52f212005-08-26 18:36:28 +00005399/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5400/// This can cause recursive merging of nodes in the DAG.
5401///
5402/// This version can replace From with any result values. To must match the
5403/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00005404void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00005405 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005406 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005407 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00005408 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005409
Dan Gohmandbe664a2009-01-19 21:44:21 +00005410 // Iterate over just the existing users of From. See the comments in
5411 // the ReplaceAllUsesWith above.
5412 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005413 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005414 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005415 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005416
Chris Lattner7b2880c2005-08-24 22:44:39 +00005417 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005418 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005419
Dan Gohman39946102009-01-25 16:29:12 +00005420 // A user can appear in a use list multiple times, and when this
5421 // happens the uses are usually next to each other in the list.
5422 // To help reduce the number of CSE recomputations, process all
5423 // the uses of this user that we can find this way.
5424 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005425 SDUse &Use = UI.getUse();
5426 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005427 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005428 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005429 } while (UI != UE && *UI == User);
5430
5431 // Now that we have modified User, add it back to the CSE maps. If it
5432 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005433 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005434 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005435
5436 // If we just RAUW'd the root, take note.
5437 if (From == getRoot().getNode())
5438 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005439}
5440
Chris Lattner012f2412006-02-17 21:58:01 +00005441/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005442/// uses of other values produced by From.getNode() alone. The Deleted
5443/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005444void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005445 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005446 // Handle the really simple, really trivial case efficiently.
5447 if (From == To) return;
5448
Chris Lattner012f2412006-02-17 21:58:01 +00005449 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005450 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005451 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005452 return;
5453 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005454
Dan Gohman39946102009-01-25 16:29:12 +00005455 // Iterate over just the existing users of From. See the comments in
5456 // the ReplaceAllUsesWith above.
5457 SDNode::use_iterator UI = From.getNode()->use_begin(),
5458 UE = From.getNode()->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005459 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005460 while (UI != UE) {
5461 SDNode *User = *UI;
5462 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005463
Dan Gohman39946102009-01-25 16:29:12 +00005464 // A user can appear in a use list multiple times, and when this
5465 // happens the uses are usually next to each other in the list.
5466 // To help reduce the number of CSE recomputations, process all
5467 // the uses of this user that we can find this way.
5468 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005469 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005470
5471 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005472 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005473 ++UI;
5474 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005475 }
Dan Gohman39946102009-01-25 16:29:12 +00005476
5477 // If this node hasn't been modified yet, it's still in the CSE maps,
5478 // so remove its old self from the CSE maps.
5479 if (!UserRemovedFromCSEMaps) {
5480 RemoveNodeFromCSEMaps(User);
5481 UserRemovedFromCSEMaps = true;
5482 }
5483
5484 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005485 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005486 } while (UI != UE && *UI == User);
5487
5488 // We are iterating over all uses of the From node, so if a use
5489 // doesn't use the specific value, no changes are made.
5490 if (!UserRemovedFromCSEMaps)
5491 continue;
5492
Chris Lattner01d029b2007-10-15 06:10:22 +00005493 // Now that we have modified User, add it back to the CSE maps. If it
5494 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005495 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005496 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005497
5498 // If we just RAUW'd the root, take note.
5499 if (From == getRoot())
5500 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005501}
5502
Dan Gohman39946102009-01-25 16:29:12 +00005503namespace {
5504 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5505 /// to record information about a use.
5506 struct UseMemo {
5507 SDNode *User;
5508 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005509 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005510 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005511
5512 /// operator< - Sort Memos by User.
5513 bool operator<(const UseMemo &L, const UseMemo &R) {
5514 return (intptr_t)L.User < (intptr_t)R.User;
5515 }
Dan Gohman39946102009-01-25 16:29:12 +00005516}
5517
Dan Gohmane8be6c62008-07-17 19:10:17 +00005518/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005519/// uses of other values produced by From.getNode() alone. The same value
5520/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005521/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005522void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5523 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005524 unsigned Num,
5525 DAGUpdateListener *UpdateListener){
5526 // Handle the simple, trivial case efficiently.
5527 if (Num == 1)
5528 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5529
Dan Gohman39946102009-01-25 16:29:12 +00005530 // Read up all the uses and make records of them. This helps
5531 // processing new uses that are introduced during the
5532 // replacement process.
5533 SmallVector<UseMemo, 4> Uses;
5534 for (unsigned i = 0; i != Num; ++i) {
5535 unsigned FromResNo = From[i].getResNo();
5536 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005537 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005538 E = FromNode->use_end(); UI != E; ++UI) {
5539 SDUse &Use = UI.getUse();
5540 if (Use.getResNo() == FromResNo) {
5541 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005542 Uses.push_back(Memo);
5543 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005544 }
Dan Gohman39946102009-01-25 16:29:12 +00005545 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005546
Dan Gohmane7852d02009-01-26 04:35:06 +00005547 // Sort the uses, so that all the uses from a given User are together.
5548 std::sort(Uses.begin(), Uses.end());
5549
Dan Gohman39946102009-01-25 16:29:12 +00005550 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5551 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005552 // We know that this user uses some value of From. If it is the right
5553 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005554 SDNode *User = Uses[UseIndex].User;
5555
5556 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005557 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005558
Dan Gohmane7852d02009-01-26 04:35:06 +00005559 // The Uses array is sorted, so all the uses for a given User
5560 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005561 // To help reduce the number of CSE recomputations, process all
5562 // the uses of this user that we can find this way.
5563 do {
5564 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005565 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005566 ++UseIndex;
5567
Dan Gohmane7852d02009-01-26 04:35:06 +00005568 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005569 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5570
Dan Gohmane8be6c62008-07-17 19:10:17 +00005571 // Now that we have modified User, add it back to the CSE maps. If it
5572 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005573 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005574 }
5575}
5576
Evan Chenge6f35d82006-08-01 08:20:41 +00005577/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005578/// based on their topological order. It returns the maximum id and a vector
5579/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005580unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005581
Dan Gohmanf06c8352008-09-30 18:30:35 +00005582 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005583
Dan Gohmanf06c8352008-09-30 18:30:35 +00005584 // SortedPos tracks the progress of the algorithm. Nodes before it are
5585 // sorted, nodes after it are unsorted. When the algorithm completes
5586 // it is at the end of the list.
5587 allnodes_iterator SortedPos = allnodes_begin();
5588
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005589 // Visit all the nodes. Move nodes with no operands to the front of
5590 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005591 // operand count. Before we do this, the Node Id fields of the nodes
5592 // may contain arbitrary values. After, the Node Id fields for nodes
5593 // before SortedPos will contain the topological sort index, and the
5594 // Node Id fields for nodes At SortedPos and after will contain the
5595 // count of outstanding operands.
5596 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5597 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005598 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005599 unsigned Degree = N->getNumOperands();
5600 if (Degree == 0) {
5601 // A node with no uses, add it to the result array immediately.
5602 N->setNodeId(DAGSize++);
5603 allnodes_iterator Q = N;
5604 if (Q != SortedPos)
5605 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005606 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005607 ++SortedPos;
5608 } else {
5609 // Temporarily use the Node Id as scratch space for the degree count.
5610 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005611 }
5612 }
5613
Dan Gohmanf06c8352008-09-30 18:30:35 +00005614 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5615 // such that by the time the end is reached all nodes will be sorted.
5616 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5617 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005618 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005619 // N is in sorted position, so all its uses have one less operand
5620 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005621 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5622 UI != UE; ++UI) {
5623 SDNode *P = *UI;
5624 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005625 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005626 --Degree;
5627 if (Degree == 0) {
5628 // All of P's operands are sorted, so P may sorted now.
5629 P->setNodeId(DAGSize++);
5630 if (P != SortedPos)
5631 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005632 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005633 ++SortedPos;
5634 } else {
5635 // Update P's outstanding operand count.
5636 P->setNodeId(Degree);
5637 }
5638 }
David Greene221925e2010-01-20 00:59:23 +00005639 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005640#ifndef NDEBUG
5641 SDNode *S = ++I;
5642 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005643 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005644#endif
5645 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005646 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005647 }
5648
5649 assert(SortedPos == AllNodes.end() &&
5650 "Topological sort incomplete!");
5651 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5652 "First node in topological sort is not the entry token!");
5653 assert(AllNodes.front().getNodeId() == 0 &&
5654 "First node in topological sort has non-zero id!");
5655 assert(AllNodes.front().getNumOperands() == 0 &&
5656 "First node in topological sort has operands!");
5657 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5658 "Last node in topologic sort has unexpected id!");
5659 assert(AllNodes.back().use_empty() &&
5660 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005661 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005662 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005663}
5664
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005665/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005666void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005667 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005668 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005669}
Evan Chenge6f35d82006-08-01 08:20:41 +00005670
Bill Wendling0777e922009-12-21 21:59:52 +00005671/// GetOrdering - Get the order for the SDNode.
5672unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5673 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005674 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005675}
5676
Evan Chengbfcb3052010-03-25 01:38:16 +00005677/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5678/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005679void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5680 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005681 if (SD)
5682 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005683}
Evan Cheng091cba12006-07-27 06:39:06 +00005684
Devang Patela2e868d2011-01-25 23:27:42 +00005685/// TransferDbgValues - Transfer SDDbgValues.
5686void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5687 if (From == To || !From.getNode()->getHasDebugValue())
5688 return;
5689 SDNode *FromNode = From.getNode();
5690 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005691 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005692 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005693 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005694 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005695 SDDbgValue *Dbg = *I;
5696 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5697 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5698 Dbg->getOffset(), Dbg->getDebugLoc(),
5699 Dbg->getOrder());
5700 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005701 }
5702 }
Devang Patela778f5c2011-02-18 22:43:42 +00005703 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5704 E = ClonedDVs.end(); I != E; ++I)
5705 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005706}
5707
Jim Laskey58b968b2005-08-17 20:08:02 +00005708//===----------------------------------------------------------------------===//
5709// SDNode Class
5710//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005711
Chris Lattner48b85922007-02-04 02:41:42 +00005712HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005713 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005714}
5715
Devang Patel0d881da2010-07-06 22:08:15 +00005716GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5717 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005718 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005719 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005720 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005721}
Chris Lattner48b85922007-02-04 02:41:42 +00005722
Owen Andersone50ed302009-08-10 22:56:29 +00005723MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005724 MachineMemOperand *mmo)
5725 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005726 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005727 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005728 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005729 assert(isNonTemporal() == MMO->isNonTemporal() &&
5730 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005731 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005732}
5733
Scott Michelfdc40a02009-02-17 22:15:04 +00005734MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005735 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005736 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005737 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005738 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005739 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005740 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005741 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5742 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005743}
5744
Jim Laskey583bd472006-10-27 23:46:08 +00005745/// Profile - Gather unique data for the node.
5746///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005747void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005748 AddNodeIDNode(ID, this);
5749}
5750
Owen Andersond8110fb2009-08-25 22:27:22 +00005751namespace {
5752 struct EVTArray {
5753 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005754
Owen Andersond8110fb2009-08-25 22:27:22 +00005755 EVTArray() {
5756 VTs.reserve(MVT::LAST_VALUETYPE);
5757 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5758 VTs.push_back(MVT((MVT::SimpleValueType)i));
5759 }
5760 };
5761}
5762
Owen Andersone50ed302009-08-10 22:56:29 +00005763static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005764static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005765static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005766
Chris Lattnera3255112005-11-08 23:30:28 +00005767/// getValueTypeList - Return a pointer to the specified value type.
5768///
Owen Andersone50ed302009-08-10 22:56:29 +00005769const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005770 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005771 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005772 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005773 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005774 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005775 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005776 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005777 }
Chris Lattnera3255112005-11-08 23:30:28 +00005778}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005779
Chris Lattner5c884562005-01-12 18:37:47 +00005780/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5781/// indicated value. This method ignores uses of other values defined by this
5782/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005783bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005784 assert(Value < getNumValues() && "Bad value!");
5785
Roman Levensteindc1adac2008-04-07 10:06:32 +00005786 // TODO: Only iterate over uses of a given value of the node
5787 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005788 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005789 if (NUses == 0)
5790 return false;
5791 --NUses;
5792 }
Chris Lattner5c884562005-01-12 18:37:47 +00005793 }
5794
5795 // Found exactly the right number of uses?
5796 return NUses == 0;
5797}
5798
5799
Evan Cheng33d55952007-08-02 05:29:38 +00005800/// hasAnyUseOfValue - Return true if there are any use of the indicated
5801/// value. This method ignores uses of other values defined by this operation.
5802bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5803 assert(Value < getNumValues() && "Bad value!");
5804
Dan Gohman1373c1c2008-07-09 22:39:01 +00005805 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005806 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005807 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005808
5809 return false;
5810}
5811
5812
Dan Gohman2a629952008-07-27 18:06:42 +00005813/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005814///
Dan Gohman2a629952008-07-27 18:06:42 +00005815bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005816 bool Seen = false;
5817 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005818 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005819 if (User == this)
5820 Seen = true;
5821 else
5822 return false;
5823 }
5824
5825 return Seen;
5826}
5827
Evan Chenge6e97e62006-11-03 07:31:32 +00005828/// isOperand - Return true if this node is an operand of N.
5829///
Dan Gohman475871a2008-07-27 21:46:04 +00005830bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005831 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5832 if (*this == N->getOperand(i))
5833 return true;
5834 return false;
5835}
5836
Evan Cheng917be682008-03-04 00:41:45 +00005837bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005838 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005839 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005840 return true;
5841 return false;
5842}
Evan Cheng4ee62112006-02-05 06:29:23 +00005843
Chris Lattner572dee72008-01-16 05:49:24 +00005844/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005845/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005846/// side-effecting instructions on any chain path. In practice, this looks
5847/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005848/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005849bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005850 unsigned Depth) const {
5851 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005852
Chris Lattner572dee72008-01-16 05:49:24 +00005853 // Don't search too deeply, we just want to be able to see through
5854 // TokenFactor's etc.
5855 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005856
Chris Lattner572dee72008-01-16 05:49:24 +00005857 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005858 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005859 if (getOpcode() == ISD::TokenFactor) {
5860 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005861 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5862 return false;
5863 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005865
Chris Lattner572dee72008-01-16 05:49:24 +00005866 // Loads don't have side effects, look through them.
5867 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5868 if (!Ld->isVolatile())
5869 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5870 }
5871 return false;
5872}
5873
Lang Hames944520f2011-07-07 04:31:51 +00005874/// hasPredecessor - Return true if N is a predecessor of this node.
5875/// N is either an operand of this node, or can be reached by recursively
5876/// traversing up the operands.
5877/// NOTE: This is an expensive method. Use it carefully.
5878bool SDNode::hasPredecessor(const SDNode *N) const {
5879 SmallPtrSet<const SDNode *, 32> Visited;
5880 SmallVector<const SDNode *, 16> Worklist;
5881 return hasPredecessorHelper(N, Visited, Worklist);
5882}
Dan Gohman6688d612009-10-28 03:44:30 +00005883
Lang Hames944520f2011-07-07 04:31:51 +00005884bool SDNode::hasPredecessorHelper(const SDNode *N,
5885 SmallPtrSet<const SDNode *, 32> &Visited,
5886 SmallVector<const SDNode *, 16> &Worklist) const {
5887 if (Visited.empty()) {
5888 Worklist.push_back(this);
5889 } else {
5890 // Take a look in the visited set. If we've already encountered this node
5891 // we needn't search further.
5892 if (Visited.count(N))
5893 return true;
5894 }
5895
5896 // Haven't visited N yet. Continue the search.
5897 while (!Worklist.empty()) {
5898 const SDNode *M = Worklist.pop_back_val();
5899 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
5900 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00005901 if (Visited.insert(Op))
5902 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00005903 if (Op == N)
5904 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00005905 }
Lang Hames944520f2011-07-07 04:31:51 +00005906 }
Dan Gohman6688d612009-10-28 03:44:30 +00005907
5908 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005909}
5910
Evan Chengc5484282006-10-04 00:56:09 +00005911uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5912 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005913 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005914}
5915
Mon P Wangcd6e7252009-11-30 02:42:02 +00005916SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5917 assert(N->getNumValues() == 1 &&
5918 "Can't unroll a vector with multiple results!");
5919
5920 EVT VT = N->getValueType(0);
5921 unsigned NE = VT.getVectorNumElements();
5922 EVT EltVT = VT.getVectorElementType();
5923 DebugLoc dl = N->getDebugLoc();
5924
5925 SmallVector<SDValue, 8> Scalars;
5926 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5927
5928 // If ResNE is 0, fully unroll the vector op.
5929 if (ResNE == 0)
5930 ResNE = NE;
5931 else if (NE > ResNE)
5932 NE = ResNE;
5933
5934 unsigned i;
5935 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00005936 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00005937 SDValue Operand = N->getOperand(j);
5938 EVT OperandVT = Operand.getValueType();
5939 if (OperandVT.isVector()) {
5940 // A vector operand; extract a single element.
5941 EVT OperandEltVT = OperandVT.getVectorElementType();
5942 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5943 OperandEltVT,
5944 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00005945 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005946 } else {
5947 // A scalar operand; just use it as is.
5948 Operands[j] = Operand;
5949 }
5950 }
5951
5952 switch (N->getOpcode()) {
5953 default:
5954 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5955 &Operands[0], Operands.size()));
5956 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00005957 case ISD::VSELECT:
5958 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
5959 &Operands[0], Operands.size()));
5960 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00005961 case ISD::SHL:
5962 case ISD::SRA:
5963 case ISD::SRL:
5964 case ISD::ROTL:
5965 case ISD::ROTR:
5966 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00005967 getShiftAmountOperand(Operands[0].getValueType(),
5968 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005969 break;
Dan Gohmand1996362010-01-09 02:13:55 +00005970 case ISD::SIGN_EXTEND_INREG:
5971 case ISD::FP_ROUND_INREG: {
5972 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
5973 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5974 Operands[0],
5975 getValueType(ExtVT)));
5976 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00005977 }
5978 }
5979
5980 for (; i < ResNE; ++i)
5981 Scalars.push_back(getUNDEF(EltVT));
5982
5983 return getNode(ISD::BUILD_VECTOR, dl,
5984 EVT::getVectorVT(*getContext(), EltVT, ResNE),
5985 &Scalars[0], Scalars.size());
5986}
5987
Evan Cheng64fa4a92009-12-09 01:36:00 +00005988
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005989/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
5990/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00005991/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005992bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00005993 unsigned Bytes, int Dist) const {
5994 if (LD->getChain() != Base->getChain())
5995 return false;
5996 EVT VT = LD->getValueType(0);
5997 if (VT.getSizeInBits() / 8 != Bytes)
5998 return false;
5999
6000 SDValue Loc = LD->getOperand(1);
6001 SDValue BaseLoc = Base->getOperand(1);
6002 if (Loc.getOpcode() == ISD::FrameIndex) {
6003 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6004 return false;
6005 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6006 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6007 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6008 int FS = MFI->getObjectSize(FI);
6009 int BFS = MFI->getObjectSize(BFI);
6010 if (FS != BFS || FS != (int)Bytes) return false;
6011 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6012 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006013
6014 // Handle X+C
6015 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6016 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6017 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006018
Dan Gohman46510a72010-04-15 01:51:59 +00006019 const GlobalValue *GV1 = NULL;
6020 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006021 int64_t Offset1 = 0;
6022 int64_t Offset2 = 0;
6023 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6024 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6025 if (isGA1 && isGA2 && GV1 == GV2)
6026 return Offset1 == (Offset2 + Dist*Bytes);
6027 return false;
6028}
6029
6030
Evan Chengf2dc5c72009-12-09 01:04:59 +00006031/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6032/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006033unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006034 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006035 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006036 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006037 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006038 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
6039 APInt AllOnes = APInt::getAllOnesValue(PtrWidth);
6040 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
6041 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), AllOnes,
6042 KnownZero, KnownOne, TLI.getTargetData());
6043 unsigned AlignBits = KnownZero.countTrailingOnes();
6044 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6045 if (Align)
6046 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006047 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006048
Evan Chengf2dc5c72009-12-09 01:04:59 +00006049 // If this is a direct reference to a stack slot, use information about the
6050 // stack slot's alignment.
6051 int FrameIdx = 1 << 31;
6052 int64_t FrameOffset = 0;
6053 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6054 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006055 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006056 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006057 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006058 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6059 FrameOffset = Ptr.getConstantOperandVal(1);
6060 }
6061
6062 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006063 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006064 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6065 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006066 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006067 }
6068
6069 return 0;
6070}
6071
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006072// getAddressSpace - Return the address space this GlobalAddress belongs to.
6073unsigned GlobalAddressSDNode::getAddressSpace() const {
6074 return getGlobal()->getType()->getAddressSpace();
6075}
6076
6077
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006078Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006079 if (isMachineConstantPoolEntry())
6080 return Val.MachineCPVal->getType();
6081 return Val.ConstVal->getType();
6082}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006083
Bob Wilson24e338e2009-03-02 23:24:16 +00006084bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6085 APInt &SplatUndef,
6086 unsigned &SplatBitSize,
6087 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006088 unsigned MinSplatBits,
6089 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006090 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006091 assert(VT.isVector() && "Expected a vector type");
6092 unsigned sz = VT.getSizeInBits();
6093 if (MinSplatBits > sz)
6094 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006095
Bob Wilson24e338e2009-03-02 23:24:16 +00006096 SplatValue = APInt(sz, 0);
6097 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006098
Bob Wilson24e338e2009-03-02 23:24:16 +00006099 // Get the bits. Bits with undefined values (when the corresponding element
6100 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6101 // in SplatValue. If any of the values are not constant, give up and return
6102 // false.
6103 unsigned int nOps = getNumOperands();
6104 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6105 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006106
6107 for (unsigned j = 0; j < nOps; ++j) {
6108 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006109 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006110 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006111
Bob Wilson24e338e2009-03-02 23:24:16 +00006112 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006113 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006114 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006115 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006116 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006117 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006118 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006119 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006120 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006121 }
6122
Bob Wilson24e338e2009-03-02 23:24:16 +00006123 // The build_vector is all constants or undefs. Find the smallest element
6124 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006125
Bob Wilson24e338e2009-03-02 23:24:16 +00006126 HasAnyUndefs = (SplatUndef != 0);
6127 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006128
Bob Wilson24e338e2009-03-02 23:24:16 +00006129 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006130 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6131 APInt LowValue = SplatValue.trunc(HalfSize);
6132 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6133 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006134
Bob Wilson24e338e2009-03-02 23:24:16 +00006135 // If the two halves do not match (ignoring undef bits), stop here.
6136 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6137 MinSplatBits > HalfSize)
6138 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006139
Bob Wilson24e338e2009-03-02 23:24:16 +00006140 SplatValue = HighValue | LowValue;
6141 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006142
Bob Wilson24e338e2009-03-02 23:24:16 +00006143 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006144 }
6145
Bob Wilson24e338e2009-03-02 23:24:16 +00006146 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006147 return true;
6148}
Nate Begeman9008ca62009-04-27 18:41:29 +00006149
Owen Andersone50ed302009-08-10 22:56:29 +00006150bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006151 // Find the first non-undef value in the shuffle mask.
6152 unsigned i, e;
6153 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6154 /* search */;
6155
Nate Begemana6415752009-04-29 18:13:31 +00006156 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006157
Nate Begeman5a5ca152009-04-29 05:20:52 +00006158 // Make sure all remaining elements are either undef or the same as the first
6159 // non-undef value.
6160 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006161 if (Mask[i] >= 0 && Mask[i] != Idx)
6162 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006163 return true;
6164}
David Greenecf495bc2010-01-20 20:13:31 +00006165
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006166#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006167static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006168 SmallPtrSet<const SDNode*, 32> &Visited,
6169 SmallPtrSet<const SDNode*, 32> &Checked) {
6170 // If this node has already been checked, don't check it again.
6171 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006172 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006173
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006174 // If a node has already been visited on this depth-first walk, reject it as
6175 // a cycle.
6176 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006177 dbgs() << "Offending node:\n";
6178 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006179 errs() << "Detected cycle in SelectionDAG\n";
6180 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006181 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006182
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006183 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6184 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006185
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006186 Checked.insert(N);
6187 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006188}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006189#endif
David Greenecf495bc2010-01-20 20:13:31 +00006190
6191void llvm::checkForCycles(const llvm::SDNode *N) {
6192#ifdef XDEBUG
6193 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006194 SmallPtrSet<const SDNode*, 32> visited;
6195 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006196 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006197#endif
6198}
6199
6200void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6201 checkForCycles(DAG->getRoot().getNode());
6202}