blob: 48f8f773be171f9d509ad49f211a49ee25204ab5 [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: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001892 LoadSDNode *LD = cast<LoadSDNode>(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00001893 if (ISD::isZEXTLoad(Op.getNode())) {
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;
Rafael Espindola95d594c2012-03-31 18:14:00 +00001897 } else if (const MDNode *Ranges = LD->getRanges()) {
1898 computeMaskedBitsLoad(*Ranges, Mask, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001899 }
1900 return;
1901 }
1902 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001903 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001904 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001905 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
Jay Foad40f8f622010-12-07 08:25:19 +00001906 APInt InMask = Mask.trunc(InBits);
1907 KnownZero = KnownZero.trunc(InBits);
1908 KnownOne = KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001909 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001910 KnownZero = KnownZero.zext(BitWidth);
1911 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001912 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001913 return;
1914 }
1915 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001916 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001917 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001918 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001919 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
Jay Foad40f8f622010-12-07 08:25:19 +00001920 APInt InMask = Mask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001921
1922 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001923 // bit is demanded. Temporarily set this bit in the mask for our callee.
1924 if (NewBits.getBoolValue())
1925 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001926
Jay Foad40f8f622010-12-07 08:25:19 +00001927 KnownZero = KnownZero.trunc(InBits);
1928 KnownOne = KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001929 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1930
1931 // Note if the sign bit is known to be zero or one.
1932 bool SignBitKnownZero = KnownZero.isNegative();
1933 bool SignBitKnownOne = KnownOne.isNegative();
1934 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1935 "Sign bit can't be known to be both zero and one!");
1936
1937 // If the sign bit wasn't actually demanded by our caller, we don't
1938 // want it set in the KnownZero and KnownOne result values. Reset the
1939 // mask and reapply it to the result values.
Jay Foad40f8f622010-12-07 08:25:19 +00001940 InMask = Mask.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001941 KnownZero &= InMask;
1942 KnownOne &= InMask;
1943
Jay Foad40f8f622010-12-07 08:25:19 +00001944 KnownZero = KnownZero.zext(BitWidth);
1945 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001946
Dan Gohman977a76f2008-02-13 22:28:48 +00001947 // If the sign bit is known zero or one, the top bits match.
1948 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001949 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001950 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001951 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001952 return;
1953 }
1954 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001955 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001956 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001957 APInt InMask = Mask.trunc(InBits);
1958 KnownZero = KnownZero.trunc(InBits);
1959 KnownOne = KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001960 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001961 KnownZero = KnownZero.zext(BitWidth);
1962 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001963 return;
1964 }
1965 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001966 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001967 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001968 APInt InMask = Mask.zext(InBits);
1969 KnownZero = KnownZero.zext(InBits);
1970 KnownOne = KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001971 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001972 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001973 KnownZero = KnownZero.trunc(BitWidth);
1974 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001975 break;
1976 }
1977 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001978 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001979 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michelfdc40a02009-02-17 22:15:04 +00001980 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanea859be2007-06-22 14:59:07 +00001981 KnownOne, Depth+1);
1982 KnownZero |= (~InMask) & Mask;
1983 return;
1984 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001985 case ISD::FGETSIGN:
1986 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001987 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001988 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001989
Dan Gohman23e8b712008-04-28 17:02:21 +00001990 case ISD::SUB: {
1991 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1992 // We know that the top bits of C-X are clear if X contains less bits
1993 // than C (i.e. no wrap-around can happen). For example, 20-X is
1994 // positive if we can prove that X is >= 0 and < 16.
1995 if (CLHS->getAPIntValue().isNonNegative()) {
1996 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1997 // NLZ can't be BitWidth with no sign bit
1998 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1999 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
2000 Depth+1);
2001
2002 // If all of the MaskV bits are known to be zero, then we know the
2003 // output top bits are zero, because we now know that the output is
2004 // from [0-C].
2005 if ((KnownZero2 & MaskV) == MaskV) {
2006 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2007 // Top bits known zero.
2008 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
2009 }
2010 }
2011 }
2012 }
2013 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002014 case ISD::ADD:
2015 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002016 // Output known-0 bits are known if clear or set in both the low clear bits
2017 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2018 // low 3 bits clear.
Dan Gohmanbc7588e2010-04-21 00:19:28 +00002019 APInt Mask2 = APInt::getLowBitsSet(BitWidth,
2020 BitWidth - Mask.countLeadingZeros());
Dan Gohman23e8b712008-04-28 17:02:21 +00002021 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002022 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002023 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2024
2025 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002026 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002027 KnownZeroOut = std::min(KnownZeroOut,
2028 KnownZero2.countTrailingOnes());
2029
Chris Lattnerda605882010-12-19 20:38:28 +00002030 if (Op.getOpcode() == ISD::ADD) {
2031 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2032 return;
2033 }
2034
2035 // With ADDE, a carry bit may be added in, so we can only use this
2036 // information if we know (at least) that the low two bits are clear. We
2037 // then return to the caller that the low bit is unknown but that other bits
2038 // are known zero.
2039 if (KnownZeroOut >= 2) // ADDE
2040 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002041 return;
2042 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002043 case ISD::SREM:
2044 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002045 const APInt &RA = Rem->getAPIntValue().abs();
2046 if (RA.isPowerOf2()) {
2047 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002048 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
2049 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002050
Duncan Sands5c2873a2010-01-29 09:45:26 +00002051 // The low bits of the first operand are unchanged by the srem.
2052 KnownZero = KnownZero2 & LowBits;
2053 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002054
Duncan Sands5c2873a2010-01-29 09:45:26 +00002055 // If the first operand is non-negative or has all low bits zero, then
2056 // the upper bits are all zero.
2057 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2058 KnownZero |= ~LowBits;
2059
2060 // If the first operand is negative and not all low bits are zero, then
2061 // the upper bits are all one.
2062 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2063 KnownOne |= ~LowBits;
2064
2065 KnownZero &= Mask;
2066 KnownOne &= Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00002067
2068 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002069 }
2070 }
2071 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002072 case ISD::UREM: {
2073 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002074 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002075 if (RA.isPowerOf2()) {
2076 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002077 APInt Mask2 = LowBits & Mask;
2078 KnownZero |= ~LowBits & Mask;
2079 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
2080 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2081 break;
2082 }
2083 }
2084
2085 // Since the result is less than or equal to either operand, any leading
2086 // zero bits in either operand must also exist in the result.
2087 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
2088 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
2089 Depth+1);
2090 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
2091 Depth+1);
2092
2093 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2094 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002095 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00002096 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
2097 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002098 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002099 case ISD::FrameIndex:
2100 case ISD::TargetFrameIndex:
2101 if (unsigned Align = InferPtrAlignment(Op)) {
2102 // The low bits are known zero if the pointer is aligned.
2103 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2104 return;
2105 }
2106 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002107
Dan Gohmanea859be2007-06-22 14:59:07 +00002108 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002109 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2110 break;
2111 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002112 case ISD::INTRINSIC_WO_CHAIN:
2113 case ISD::INTRINSIC_W_CHAIN:
2114 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002115 // Allow the target to implement this method for its nodes.
2116 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
2117 Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002118 return;
2119 }
2120}
2121
2122/// ComputeNumSignBits - Return the number of times the sign bit of the
2123/// register is replicated into the other bits. We know that at least 1 bit
2124/// is always equal to the sign bit (itself), but other cases can give us
2125/// information. For example, immediately after an "SRA X, 2", we know that
2126/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002127unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002130 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002132 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Dan Gohmanea859be2007-06-22 14:59:07 +00002134 if (Depth == 6)
2135 return 1; // Limit search depth.
2136
2137 switch (Op.getOpcode()) {
2138 default: break;
2139 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 return VTBits-Tmp+1;
2142 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002143 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Dan Gohmanea859be2007-06-22 14:59:07 +00002146 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002147 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002148 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002150
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002152 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002153 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002154
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 case ISD::SIGN_EXTEND_INREG:
2156 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002157 Tmp =
2158 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002159 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Dan Gohmanea859be2007-06-22 14:59:07 +00002161 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2162 return std::max(Tmp, Tmp2);
2163
2164 case ISD::SRA:
2165 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2166 // SRA X, C -> adds C sign bits.
2167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002168 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002169 if (Tmp > VTBits) Tmp = VTBits;
2170 }
2171 return Tmp;
2172 case ISD::SHL:
2173 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2174 // shl destroys sign bits.
2175 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002176 if (C->getZExtValue() >= VTBits || // Bad shift.
2177 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2178 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002179 }
2180 break;
2181 case ISD::AND:
2182 case ISD::OR:
2183 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002184 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002185 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002186 if (Tmp != 1) {
2187 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2188 FirstAnswer = std::min(Tmp, Tmp2);
2189 // We computed what we know about the sign bits as our first
2190 // answer. Now proceed to the generic code that uses
2191 // ComputeMaskedBits, and pick whichever answer is better.
2192 }
2193 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002194
2195 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002196 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002197 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002198 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002199 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002200
2201 case ISD::SADDO:
2202 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002203 case ISD::SSUBO:
2204 case ISD::USUBO:
2205 case ISD::SMULO:
2206 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002207 if (Op.getResNo() != 1)
2208 break;
Duncan Sands03228082008-11-23 15:47:28 +00002209 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002210 case ISD::SETCC:
2211 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002212 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002213 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002214 return VTBits;
2215 break;
2216 case ISD::ROTL:
2217 case ISD::ROTR:
2218 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002219 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002220
Dan Gohmanea859be2007-06-22 14:59:07 +00002221 // Handle rotate right by N like a rotate left by 32-N.
2222 if (Op.getOpcode() == ISD::ROTR)
2223 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2224
2225 // If we aren't rotating out all of the known-in sign bits, return the
2226 // number that are left. This handles rotl(sext(x), 1) for example.
2227 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2228 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2229 }
2230 break;
2231 case ISD::ADD:
2232 // Add can have at most one carry bit. Thus we know that the output
2233 // is, at worst, one more bit than the inputs.
2234 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2235 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002236
Dan Gohmanea859be2007-06-22 14:59:07 +00002237 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002238 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002239 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002240 APInt KnownZero, KnownOne;
2241 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002242 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002243
Dan Gohmanea859be2007-06-22 14:59:07 +00002244 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2245 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002246 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002247 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002248
Dan Gohmanea859be2007-06-22 14:59:07 +00002249 // If we are subtracting one from a positive number, there is no carry
2250 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002251 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002252 return Tmp;
2253 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002254
Dan Gohmanea859be2007-06-22 14:59:07 +00002255 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2256 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002257 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002258
Dan Gohmanea859be2007-06-22 14:59:07 +00002259 case ISD::SUB:
2260 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2261 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002262
Dan Gohmanea859be2007-06-22 14:59:07 +00002263 // Handle NEG.
2264 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002265 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002266 APInt KnownZero, KnownOne;
2267 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002268 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2269 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2270 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002271 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002272 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002273
Dan Gohmanea859be2007-06-22 14:59:07 +00002274 // If the input is known to be positive (the sign bit is known clear),
2275 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002276 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002278
Dan Gohmanea859be2007-06-22 14:59:07 +00002279 // Otherwise, we treat this like a SUB.
2280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002281
Dan Gohmanea859be2007-06-22 14:59:07 +00002282 // Sub can have at most one carry bit. Thus we know that the output
2283 // is, at worst, one more bit than the inputs.
2284 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2285 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002286 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002287 case ISD::TRUNCATE:
2288 // FIXME: it's tricky to do anything useful for this, but it is an important
2289 // case for targets like X86.
2290 break;
2291 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002292
Dan Gohmanea859be2007-06-22 14:59:07 +00002293 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2294 if (Op.getOpcode() == ISD::LOAD) {
2295 LoadSDNode *LD = cast<LoadSDNode>(Op);
2296 unsigned ExtType = LD->getExtensionType();
2297 switch (ExtType) {
2298 default: break;
2299 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002300 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002301 return VTBits-Tmp+1;
2302 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002303 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002304 return VTBits-Tmp;
2305 }
2306 }
2307
2308 // Allow the target to implement this method for its nodes.
2309 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002310 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002311 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2312 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2313 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002314 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002315 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002316
Dan Gohmanea859be2007-06-22 14:59:07 +00002317 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2318 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002319 APInt KnownZero, KnownOne;
2320 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002321 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002322
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002323 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002324 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002325 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002326 Mask = KnownOne;
2327 } else {
2328 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002329 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002331
Dan Gohmanea859be2007-06-22 14:59:07 +00002332 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2333 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002334 Mask = ~Mask;
2335 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002336 // Return # leading zeros. We use 'min' here in case Val was zero before
2337 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002338 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002339}
2340
Chris Lattner0a9481f2011-02-13 22:25:43 +00002341/// isBaseWithConstantOffset - Return true if the specified operand is an
2342/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2343/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2344/// semantics as an ADD. This handles the equivalence:
Chris Lattner463b3c22011-02-14 06:14:42 +00002345/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002346bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2347 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2348 !isa<ConstantSDNode>(Op.getOperand(1)))
2349 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002350
2351 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002352 !MaskedValueIsZero(Op.getOperand(0),
2353 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2354 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002355
Chris Lattner0a9481f2011-02-13 22:25:43 +00002356 return true;
2357}
2358
2359
Dan Gohman8d44b282009-09-03 20:34:31 +00002360bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2361 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002362 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002363 return true;
2364
2365 // If the value is a constant, we can obviously see if it is a NaN or not.
2366 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2367 return !C->getValueAPF().isNaN();
2368
2369 // TODO: Recognize more cases here.
2370
2371 return false;
2372}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002373
Dan Gohmane8326932010-02-24 06:52:40 +00002374bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2375 // If the value is a constant, we can obviously see if it is a zero or not.
2376 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2377 return !C->isZero();
2378
2379 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002380 switch (Op.getOpcode()) {
2381 default: break;
2382 case ISD::OR:
2383 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2384 return !C->isNullValue();
2385 break;
2386 }
Dan Gohmane8326932010-02-24 06:52:40 +00002387
2388 return false;
2389}
2390
2391bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2392 // Check the obvious case.
2393 if (A == B) return true;
2394
2395 // For for negative and positive zero.
2396 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2397 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2398 if (CA->isZero() && CB->isZero()) return true;
2399
2400 // Otherwise they may not be equal.
2401 return false;
2402}
2403
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002405///
Owen Andersone50ed302009-08-10 22:56:29 +00002406SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002407 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002408 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002409 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002410 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002411 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002412
Dan Gohman95531882010-03-18 18:49:47 +00002413 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002414 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002415
Chris Lattner4a283e92006-08-11 18:38:11 +00002416 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002417#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002418 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002419#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002420 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002421}
2422
Bill Wendling7ade28c2009-01-28 22:17:52 +00002423SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002424 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002425 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002427 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002428 switch (Opcode) {
2429 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002430 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002431 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002432 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002433 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002434 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002435 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002436 case ISD::UINT_TO_FP:
2437 case ISD::SINT_TO_FP: {
Chris Lattner7fb5c2d2010-03-15 16:05:15 +00002438 // No compile time operations on ppcf128.
2439 if (VT == MVT::ppcf128) break;
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002440 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002441 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002442 Opcode==ISD::SINT_TO_FP,
2443 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002444 return getConstantFP(apf, VT);
2445 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002446 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002447 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002448 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002449 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002450 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002451 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002452 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002453 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002454 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002455 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002456 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002457 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002458 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002459 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002460 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002461 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002462 }
2463 }
2464
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002465 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002466 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002467 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002469 switch (Opcode) {
2470 case ISD::FNEG:
2471 V.changeSign();
2472 return getConstantFP(V, VT);
2473 case ISD::FABS:
2474 V.clearSign();
2475 return getConstantFP(V, VT);
2476 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002477 case ISD::FP_EXTEND: {
2478 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002479 // This can return overflow, underflow, or inexact; we don't care.
2480 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002481 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002482 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002483 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002484 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002485 case ISD::FP_TO_SINT:
2486 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002487 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002488 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002489 assert(integerPartWidth >= 64);
2490 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002491 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002492 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002493 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002494 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2495 break;
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00002496 APInt api(VT.getSizeInBits(), x);
Dale Johannesen2f91f302009-04-24 21:34:13 +00002497 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002498 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002499 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002500 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002501 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002502 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002503 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002504 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002505 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002506 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002507 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002508
Gabor Greifba36cb52008-08-28 21:40:38 +00002509 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002510 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002511 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002512 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002513 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002514 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002515 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002516 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002517 assert(VT.isFloatingPoint() &&
2518 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002519 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002520 assert((!VT.isVector() ||
2521 VT.getVectorNumElements() ==
2522 Operand.getValueType().getVectorNumElements()) &&
2523 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002524 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002525 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002526 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002527 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002528 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002529 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002530 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002531 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2532 "Invalid sext node, dst < src!");
2533 assert((!VT.isVector() ||
2534 VT.getVectorNumElements() ==
2535 Operand.getValueType().getVectorNumElements()) &&
2536 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002537 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002538 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002539 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002540 // sext(undef) = 0, because the top bits will all be the same.
2541 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002542 break;
2543 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002544 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002545 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002546 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002547 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2548 "Invalid zext node, dst < src!");
2549 assert((!VT.isVector() ||
2550 VT.getVectorNumElements() ==
2551 Operand.getValueType().getVectorNumElements()) &&
2552 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002553 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002554 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002555 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002556 else if (OpOpcode == ISD::UNDEF)
2557 // zext(undef) = 0, because the top bits will be zero.
2558 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002559 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002560 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002561 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002562 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002563 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002564 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2565 "Invalid anyext node, dst < src!");
2566 assert((!VT.isVector() ||
2567 VT.getVectorNumElements() ==
2568 Operand.getValueType().getVectorNumElements()) &&
2569 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002570
Dan Gohman9e86a732010-06-18 00:08:30 +00002571 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2572 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002573 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002574 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002575 else if (OpOpcode == ISD::UNDEF)
2576 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002577
2578 // (ext (trunx x)) -> x
2579 if (OpOpcode == ISD::TRUNCATE) {
2580 SDValue OpOp = Operand.getNode()->getOperand(0);
2581 if (OpOp.getValueType() == VT)
2582 return OpOp;
2583 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002584 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002585 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002586 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002587 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002588 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002589 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2590 "Invalid truncate node, src < dst!");
2591 assert((!VT.isVector() ||
2592 VT.getVectorNumElements() ==
2593 Operand.getValueType().getVectorNumElements()) &&
2594 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002595 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002596 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002597 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2598 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002599 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002600 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2601 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002602 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002603 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002604 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002605 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002606 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002607 if (OpOpcode == ISD::UNDEF)
2608 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002609 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002610 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002611 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002612 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002613 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002614 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002615 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2616 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002617 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002618 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002619 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002620 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002621 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002622 (VT.getVectorElementType() == Operand.getValueType() ||
2623 (VT.getVectorElementType().isInteger() &&
2624 Operand.getValueType().isInteger() &&
2625 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002626 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002627 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002628 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002629 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2630 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2631 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2632 Operand.getConstantOperandVal(1) == 0 &&
2633 Operand.getOperand(0).getValueType() == VT)
2634 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002635 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002636 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002637 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002638 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002639 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002640 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002641 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002642 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002643 break;
2644 case ISD::FABS:
2645 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002646 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002647 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002648 }
2649
Chris Lattner43247a12005-08-25 19:12:10 +00002650 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002651 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002652 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002653 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002654 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002655 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002656 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002657 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002658 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002659
Dan Gohman95531882010-03-18 18:49:47 +00002660 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002661 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002662 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002663 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002664 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002665
Chris Lattnerc3aae252005-01-07 07:46:32 +00002666 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002667#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002668 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002669#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002670 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002671}
2672
Bill Wendling688d1c42008-09-24 10:16:24 +00002673SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002674 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002675 ConstantSDNode *Cst1,
2676 ConstantSDNode *Cst2) {
2677 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2678
2679 switch (Opcode) {
2680 case ISD::ADD: return getConstant(C1 + C2, VT);
2681 case ISD::SUB: return getConstant(C1 - C2, VT);
2682 case ISD::MUL: return getConstant(C1 * C2, VT);
2683 case ISD::UDIV:
2684 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2685 break;
2686 case ISD::UREM:
2687 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2688 break;
2689 case ISD::SDIV:
2690 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2691 break;
2692 case ISD::SREM:
2693 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2694 break;
2695 case ISD::AND: return getConstant(C1 & C2, VT);
2696 case ISD::OR: return getConstant(C1 | C2, VT);
2697 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2698 case ISD::SHL: return getConstant(C1 << C2, VT);
2699 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2700 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2701 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2702 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2703 default: break;
2704 }
2705
2706 return SDValue();
2707}
2708
Owen Andersone50ed302009-08-10 22:56:29 +00002709SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002710 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002711 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2712 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002713 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002714 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002715 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002716 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2717 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002718 // Fold trivial token factors.
2719 if (N1.getOpcode() == ISD::EntryToken) return N2;
2720 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002721 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002722 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002723 case ISD::CONCAT_VECTORS:
2724 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2725 // one big BUILD_VECTOR.
2726 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2727 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002728 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2729 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002730 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002731 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002732 }
2733 break;
Chris Lattner76365122005-01-16 02:23:22 +00002734 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002735 assert(VT.isInteger() && "This operator does not apply to FP types!");
2736 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002737 N1.getValueType() == VT && "Binary operator types must match!");
2738 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2739 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002740 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002741 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002742 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2743 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002744 break;
Chris Lattner76365122005-01-16 02:23:22 +00002745 case ISD::OR:
2746 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002747 case ISD::ADD:
2748 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002749 assert(VT.isInteger() && "This operator does not apply to FP types!");
2750 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002751 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002752 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2753 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002754 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002755 return N1;
2756 break;
Chris Lattner76365122005-01-16 02:23:22 +00002757 case ISD::UDIV:
2758 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002759 case ISD::MULHU:
2760 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002761 case ISD::MUL:
2762 case ISD::SDIV:
2763 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002764 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002765 assert(N1.getValueType() == N2.getValueType() &&
2766 N1.getValueType() == VT && "Binary operator types must match!");
2767 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002768 case ISD::FADD:
2769 case ISD::FSUB:
2770 case ISD::FMUL:
2771 case ISD::FDIV:
2772 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002773 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002774 if (Opcode == ISD::FADD) {
2775 // 0+x --> x
2776 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2777 if (CFP->getValueAPF().isZero())
2778 return N2;
2779 // x+0 --> x
2780 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2781 if (CFP->getValueAPF().isZero())
2782 return N1;
2783 } else if (Opcode == ISD::FSUB) {
2784 // x-0 --> x
2785 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2786 if (CFP->getValueAPF().isZero())
2787 return N1;
2788 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002789 }
Dale Johannesen78995512010-05-15 18:38:02 +00002790 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002791 assert(N1.getValueType() == N2.getValueType() &&
2792 N1.getValueType() == VT && "Binary operator types must match!");
2793 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002794 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2795 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002796 N1.getValueType().isFloatingPoint() &&
2797 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002798 "Invalid FCOPYSIGN!");
2799 break;
Chris Lattner76365122005-01-16 02:23:22 +00002800 case ISD::SHL:
2801 case ISD::SRA:
2802 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002803 case ISD::ROTL:
2804 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002805 assert(VT == N1.getValueType() &&
2806 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002807 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002808 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002809 // Verify that the shift amount VT is bit enough to hold valid shift
2810 // amounts. This catches things like trying to shift an i1024 value by an
2811 // i8, which is easy to fall into in generic code that uses
2812 // TLI.getShiftAmount().
2813 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002814 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002815 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002816
2817 // Always fold shifts of i1 values so the code generator doesn't need to
2818 // handle them. Since we know the size of the shift has to be less than the
2819 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002820 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002821 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002822 if (N2C && N2C->isNullValue())
2823 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002824 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002825 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002826 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002827 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002828 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002829 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002830 assert(EVT.isVector() == VT.isVector() &&
2831 "FP_ROUND_INREG type should be vector iff the operand "
2832 "type is vector!");
2833 assert((!EVT.isVector() ||
2834 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2835 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002836 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002837 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002838 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002839 break;
2840 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002841 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002842 assert(VT.isFloatingPoint() &&
2843 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002844 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002845 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002846 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002847 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002848 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002849 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002850 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002851 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002852 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002853 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002854 assert(!EVT.isVector() &&
2855 "AssertSExt/AssertZExt type should be the vector element type "
2856 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002857 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002858 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002859 break;
2860 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002861 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002862 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002863 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002864 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002865 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002866 assert(EVT.isVector() == VT.isVector() &&
2867 "SIGN_EXTEND_INREG type should be vector iff the operand "
2868 "type is vector!");
2869 assert((!EVT.isVector() ||
2870 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2871 "Vector element counts must match in SIGN_EXTEND_INREG");
2872 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002873 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002874
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002875 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002876 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002877 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002878 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002879 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002880 return getConstant(Val, VT);
2881 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002882 break;
2883 }
2884 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002885 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2886 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002887 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002888
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002889 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2890 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002891 if (N2C &&
2892 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002893 N1.getNumOperands() > 0) {
2894 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002896 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002897 N1.getOperand(N2C->getZExtValue() / Factor),
2898 getConstant(N2C->getZExtValue() % Factor,
2899 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002900 }
2901
2902 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2903 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002904 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2905 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002906 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002907 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002908 // If the vector element type is not legal, the BUILD_VECTOR operands
2909 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002910 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2911 }
2912 if (VT != VEltTy) {
2913 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2914 // result is implicitly extended.
2915 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002916 }
2917 return Elt;
2918 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002919
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002920 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2921 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002922 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002923 // If the indices are the same, return the inserted element else
2924 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002925 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002926 SDValue N1Op2 = N1.getOperand(2);
2927 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2928
2929 if (N1Op2C && N2C) {
2930 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2931 if (VT == N1.getOperand(1).getValueType())
2932 return N1.getOperand(1);
2933 else
2934 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2935 }
2936
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002937 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002938 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002939 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002940 break;
2941 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002942 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002943 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2944 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002945 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002946 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002947
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002948 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2949 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002950 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002951 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002952 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002953
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002954 // EXTRACT_ELEMENT of a constant int is also very common.
2955 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002956 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002957 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002958 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2959 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002960 }
2961 break;
David Greene91585092011-01-26 15:38:49 +00002962 case ISD::EXTRACT_SUBVECTOR: {
2963 SDValue Index = N2;
2964 if (VT.isSimple() && N1.getValueType().isSimple()) {
2965 assert(VT.isVector() && N1.getValueType().isVector() &&
2966 "Extract subvector VTs must be a vectors!");
2967 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
2968 "Extract subvector VTs must have the same element type!");
2969 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
2970 "Extract subvector must be from larger vector to smaller vector!");
2971
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00002972 if (isa<ConstantSDNode>(Index.getNode())) {
2973 assert((VT.getVectorNumElements() +
2974 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00002975 <= N1.getValueType().getVectorNumElements())
2976 && "Extract subvector overflow!");
2977 }
2978
2979 // Trivial extraction.
2980 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
2981 return N1;
2982 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002983 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002984 }
David Greene91585092011-01-26 15:38:49 +00002985 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002986
2987 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002988 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002989 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2990 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002991 } else { // Cannonicalize constant to RHS if commutative
2992 if (isCommutativeBinOp(Opcode)) {
2993 std::swap(N1C, N2C);
2994 std::swap(N1, N2);
2995 }
2996 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002997 }
2998
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002999 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003000 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3001 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003002 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003003 if (!N2CFP && isCommutativeBinOp(Opcode)) {
3004 // Cannonicalize constant to RHS if commutative
3005 std::swap(N1CFP, N2CFP);
3006 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003008 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3009 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003010 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003011 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003012 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003013 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003014 return getConstantFP(V1, VT);
3015 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003016 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003017 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3018 if (s!=APFloat::opInvalidOp)
3019 return getConstantFP(V1, VT);
3020 break;
3021 case ISD::FMUL:
3022 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3023 if (s!=APFloat::opInvalidOp)
3024 return getConstantFP(V1, VT);
3025 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003026 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003027 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3028 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3029 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003030 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003031 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003032 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3033 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3034 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003035 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003036 case ISD::FCOPYSIGN:
3037 V1.copySign(V2);
3038 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003039 default: break;
3040 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003041 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003042 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003043
Chris Lattner62b57722006-04-20 05:39:12 +00003044 // Canonicalize an UNDEF to the RHS, even over a constant.
3045 if (N1.getOpcode() == ISD::UNDEF) {
3046 if (isCommutativeBinOp(Opcode)) {
3047 std::swap(N1, N2);
3048 } else {
3049 switch (Opcode) {
3050 case ISD::FP_ROUND_INREG:
3051 case ISD::SIGN_EXTEND_INREG:
3052 case ISD::SUB:
3053 case ISD::FSUB:
3054 case ISD::FDIV:
3055 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003056 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003057 return N1; // fold op(undef, arg2) -> undef
3058 case ISD::UDIV:
3059 case ISD::SDIV:
3060 case ISD::UREM:
3061 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003062 case ISD::SRL:
3063 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003064 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003065 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3066 // For vectors, we can't easily build an all zero vector, just return
3067 // the LHS.
3068 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003069 }
3070 }
3071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003072
3073 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003074 if (N2.getOpcode() == ISD::UNDEF) {
3075 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003076 case ISD::XOR:
3077 if (N1.getOpcode() == ISD::UNDEF)
3078 // Handle undef ^ undef -> 0 special case. This is a common
3079 // idiom (misuse).
3080 return getConstant(0, VT);
3081 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003082 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003083 case ISD::ADDC:
3084 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003085 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003086 case ISD::UDIV:
3087 case ISD::SDIV:
3088 case ISD::UREM:
3089 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003090 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003091 case ISD::FADD:
3092 case ISD::FSUB:
3093 case ISD::FMUL:
3094 case ISD::FDIV:
3095 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003096 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003097 return N2;
3098 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003099 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003100 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003101 case ISD::SRL:
3102 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003103 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003104 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3105 // For vectors, we can't easily build an all zero vector, just return
3106 // the LHS.
3107 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003108 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003109 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003110 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003111 // For vectors, we can't easily build an all one vector, just return
3112 // the LHS.
3113 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003114 case ISD::SRA:
3115 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003116 }
3117 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003118
Chris Lattner27e9b412005-05-11 18:57:39 +00003119 // Memoize this node if possible.
3120 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003121 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003122 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003124 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003125 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003126 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003127 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003128 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003129
Dan Gohman95531882010-03-18 18:49:47 +00003130 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003131 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003132 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003133 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003134 }
3135
Chris Lattnerc3aae252005-01-07 07:46:32 +00003136 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003137#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003138 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003139#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003140 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003141}
3142
Owen Andersone50ed302009-08-10 22:56:29 +00003143SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003144 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003145 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003146 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003147 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003148 case ISD::CONCAT_VECTORS:
3149 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3150 // one big BUILD_VECTOR.
3151 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3152 N2.getOpcode() == ISD::BUILD_VECTOR &&
3153 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003154 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3155 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003156 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3157 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003158 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003159 }
3160 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003161 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003162 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003163 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003164 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003165 break;
3166 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003167 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003168 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003169 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003170 return N2; // select true, X, Y -> X
3171 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003172 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003173
3174 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003175 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003176 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003177 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003178 case ISD::INSERT_SUBVECTOR: {
3179 SDValue Index = N3;
3180 if (VT.isSimple() && N1.getValueType().isSimple()
3181 && N2.getValueType().isSimple()) {
3182 assert(VT.isVector() && N1.getValueType().isVector() &&
3183 N2.getValueType().isVector() &&
3184 "Insert subvector VTs must be a vectors");
3185 assert(VT == N1.getValueType() &&
3186 "Dest and insert subvector source types must match!");
3187 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3188 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003189 if (isa<ConstantSDNode>(Index.getNode())) {
3190 assert((N2.getValueType().getVectorNumElements() +
3191 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003192 <= VT.getVectorNumElements())
3193 && "Insert subvector overflow!");
3194 }
3195
3196 // Trivial insertion.
3197 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3198 return N2;
3199 }
3200 break;
3201 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003202 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003203 // Fold bit_convert nodes from a type to themselves.
3204 if (N1.getValueType() == VT)
3205 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003206 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003207 }
3208
Chris Lattner43247a12005-08-25 19:12:10 +00003209 // Memoize node if it doesn't produce a flag.
3210 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003211 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003212 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003213 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003214 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003215 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003216 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003217 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003218 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003219
Dan Gohman95531882010-03-18 18:49:47 +00003220 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003221 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003222 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003223 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003224 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003225
Chris Lattnerc3aae252005-01-07 07:46:32 +00003226 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003227#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003228 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003229#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003230 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003231}
3232
Owen Andersone50ed302009-08-10 22:56:29 +00003233SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003234 SDValue N1, SDValue N2, SDValue N3,
3235 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003236 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003237 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003238}
3239
Owen Andersone50ed302009-08-10 22:56:29 +00003240SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003241 SDValue N1, SDValue N2, SDValue N3,
3242 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003244 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003245}
3246
Dan Gohman98ca4f22009-08-05 01:29:28 +00003247/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3248/// the incoming stack arguments to be loaded from the stack.
3249SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3250 SmallVector<SDValue, 8> ArgChains;
3251
3252 // Include the original chain at the beginning of the list. When this is
3253 // used by target LowerCall hooks, this helps legalize find the
3254 // CALLSEQ_BEGIN node.
3255 ArgChains.push_back(Chain);
3256
3257 // Add a chain value for each stack argument.
3258 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3259 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3260 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3261 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3262 if (FI->getIndex() < 0)
3263 ArgChains.push_back(SDValue(L, 1));
3264
3265 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003267 &ArgChains[0], ArgChains.size());
3268}
3269
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003270/// SplatByte - Distribute ByteVal over NumBits bits.
3271static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3272 APInt Val = APInt(NumBits, ByteVal);
3273 unsigned Shift = 8;
3274 for (unsigned i = NumBits; i > 8; i >>= 1) {
3275 Val = (Val << Shift) | Val;
3276 Shift <<= 1;
3277 }
3278 return Val;
3279}
3280
Dan Gohman707e0182008-04-12 04:36:06 +00003281/// getMemsetValue - Vectorized representation of the memset value
3282/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003283static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003284 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003285 assert(Value.getOpcode() != ISD::UNDEF);
3286
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003287 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003288 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003289 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003290 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003291 return DAG.getConstant(Val, VT);
3292 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003293 }
Evan Chengf0df0312008-05-15 08:39:06 +00003294
Dale Johannesen0f502f62009-02-03 22:26:09 +00003295 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003296 if (NumBits > 8) {
3297 // Use a multiplication with 0x010101... to extend the input to the
3298 // required length.
3299 APInt Magic = SplatByte(NumBits, 0x01);
3300 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003301 }
3302
3303 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003304}
3305
Dan Gohman707e0182008-04-12 04:36:06 +00003306/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3307/// used when a memcpy is turned into a memset when the source is a constant
3308/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003309static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003310 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003311 // Handle vector with all elements zero.
3312 if (Str.empty()) {
3313 if (VT.isInteger())
3314 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003315 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003316 return DAG.getConstantFP(0.0, VT);
3317 else if (VT.isVector()) {
3318 unsigned NumElts = VT.getVectorNumElements();
3319 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003320 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003321 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3322 EltVT, NumElts)));
3323 } else
3324 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003325 }
3326
Duncan Sands83ec4b62008-06-06 12:08:01 +00003327 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003328 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3329 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3330
Dan Gohman707e0182008-04-12 04:36:06 +00003331 uint64_t Val = 0;
Chris Lattner18c7f802012-02-05 02:29:43 +00003332 if (TLI.isLittleEndian()) {
3333 for (unsigned i = 0; i != NumBytes; ++i)
3334 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3335 } else {
3336 for (unsigned i = 0; i != NumBytes; ++i)
3337 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003338 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003339
Dan Gohman707e0182008-04-12 04:36:06 +00003340 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003341}
3342
Scott Michelfdc40a02009-02-17 22:15:04 +00003343/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003344///
Dan Gohman475871a2008-07-27 21:46:04 +00003345static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003346 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003347 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003348 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003349 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003350}
3351
Evan Chengf0df0312008-05-15 08:39:06 +00003352/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3353///
Chris Lattner18c7f802012-02-05 02:29:43 +00003354static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003355 unsigned SrcDelta = 0;
3356 GlobalAddressSDNode *G = NULL;
3357 if (Src.getOpcode() == ISD::GlobalAddress)
3358 G = cast<GlobalAddressSDNode>(Src);
3359 else if (Src.getOpcode() == ISD::ADD &&
3360 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3361 Src.getOperand(1).getOpcode() == ISD::Constant) {
3362 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003363 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003364 }
3365 if (!G)
3366 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003367
Chris Lattner18c7f802012-02-05 02:29:43 +00003368 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003369}
Dan Gohman707e0182008-04-12 04:36:06 +00003370
Evan Cheng255f20f2010-04-01 06:04:33 +00003371/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3372/// to replace the memset / memcpy. Return true if the number of memory ops
3373/// is below the threshold. It returns the types of the sequence of
3374/// memory ops to perform memset / memcpy by reference.
3375static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003376 unsigned Limit, uint64_t Size,
3377 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003378 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00003379 bool MemcpyStrSrc,
Evan Cheng255f20f2010-04-01 06:04:33 +00003380 SelectionDAG &DAG,
3381 const TargetLowering &TLI) {
3382 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3383 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003384 // If 'SrcAlign' is zero, that means the memory operation does not need to
3385 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3386 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3387 // is the specified alignment of the memory operation. If it is zero, that
3388 // means it's possible to change the alignment of the destination.
3389 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3390 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003391 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003392 IsZeroVal, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003393 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003394
Chris Lattneracee6472010-03-07 07:45:08 +00003395 if (VT == MVT::Other) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003396 if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003397 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003398 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003399 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003400 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 case 0: VT = MVT::i64; break;
3402 case 4: VT = MVT::i32; break;
3403 case 2: VT = MVT::i16; break;
3404 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003405 }
3406 }
3407
Owen Anderson766b5ef2009-08-11 21:59:30 +00003408 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003409 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003410 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003411 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003412
Duncan Sands8e4eb092008-06-08 20:54:56 +00003413 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003414 VT = LVT;
3415 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003416
Dan Gohman707e0182008-04-12 04:36:06 +00003417 unsigned NumMemOps = 0;
3418 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003419 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003420 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003421 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng255f20f2010-04-01 06:04:33 +00003422 if (VT.isVector() || VT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003424 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003426 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003427 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003428 // This can result in a type that is not legal on the target, e.g.
3429 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003430 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003431 VTSize >>= 1;
3432 }
Dan Gohman707e0182008-04-12 04:36:06 +00003433 }
Dan Gohman707e0182008-04-12 04:36:06 +00003434
3435 if (++NumMemOps > Limit)
3436 return false;
3437 MemOps.push_back(VT);
3438 Size -= VTSize;
3439 }
3440
3441 return true;
3442}
3443
Dale Johannesen0f502f62009-02-03 22:26:09 +00003444static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003445 SDValue Chain, SDValue Dst,
3446 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003447 unsigned Align, bool isVol,
3448 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003449 MachinePointerInfo DstPtrInfo,
3450 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003451 // Turn a memcpy of undef to nop.
3452 if (Src.getOpcode() == ISD::UNDEF)
3453 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003454
Dan Gohman21323f32008-05-29 19:42:22 +00003455 // Expand memcpy to a series of load and store ops if the size operand falls
3456 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003457 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3458 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003459 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003460 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003461 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003462 MachineFunction &MF = DAG.getMachineFunction();
3463 MachineFrameInfo *MFI = MF.getFrameInfo();
3464 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003465 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3466 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3467 DstAlignCanChange = true;
3468 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3469 if (Align > SrcAlign)
3470 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003471 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003472 bool CopyFromStr = isMemSrcFromString(Src, Str);
3473 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003474 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003475
Evan Cheng94107ba2010-04-01 18:19:11 +00003476 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003477 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003478 (isZeroStr ? 0 : SrcAlign),
3479 true, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003480 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003481
Evan Cheng255f20f2010-04-01 06:04:33 +00003482 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003483 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003484 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3485 if (NewAlign > Align) {
3486 // Give the stack frame object a larger alignment if needed.
3487 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3488 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3489 Align = NewAlign;
3490 }
3491 }
Dan Gohman707e0182008-04-12 04:36:06 +00003492
Dan Gohman475871a2008-07-27 21:46:04 +00003493 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003494 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003495 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003496 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003497 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003498 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003499 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003500
Evan Cheng255f20f2010-04-01 06:04:33 +00003501 if (CopyFromStr &&
3502 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003503 // It's unlikely a store of a vector immediate can be done in a single
3504 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003505 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003506 // FIXME: Handle other cases where store of vector immediate is done in
3507 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003508 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Dale Johannesen0f502f62009-02-03 22:26:09 +00003509 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003510 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003511 DstPtrInfo.getWithOffset(DstOff), isVol,
3512 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003513 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003514 // The type might not be legal for the target. This should only happen
3515 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003516 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3517 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003518 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003519 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003520 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003521 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003522 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003523 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003524 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003525 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003526 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003527 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3528 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003529 }
3530 OutChains.push_back(Store);
3531 SrcOff += VTSize;
3532 DstOff += VTSize;
3533 }
3534
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003536 &OutChains[0], OutChains.size());
3537}
3538
Dale Johannesen0f502f62009-02-03 22:26:09 +00003539static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003540 SDValue Chain, SDValue Dst,
3541 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003542 unsigned Align, bool isVol,
3543 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003544 MachinePointerInfo DstPtrInfo,
3545 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003546 // Turn a memmove of undef to nop.
3547 if (Src.getOpcode() == ISD::UNDEF)
3548 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003549
3550 // Expand memmove to a series of load and store ops if the size operand falls
3551 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003552 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003553 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003554 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003555 MachineFunction &MF = DAG.getMachineFunction();
3556 MachineFrameInfo *MFI = MF.getFrameInfo();
3557 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003558 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3559 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3560 DstAlignCanChange = true;
3561 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3562 if (Align > SrcAlign)
3563 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003564 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003565
Evan Cheng94107ba2010-04-01 18:19:11 +00003566 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003567 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003568 SrcAlign, true, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003569 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003570
Evan Cheng255f20f2010-04-01 06:04:33 +00003571 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003572 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003573 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3574 if (NewAlign > Align) {
3575 // Give the stack frame object a larger alignment if needed.
3576 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3577 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3578 Align = NewAlign;
3579 }
3580 }
Dan Gohman21323f32008-05-29 19:42:22 +00003581
Evan Cheng255f20f2010-04-01 06:04:33 +00003582 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003583 SmallVector<SDValue, 8> LoadValues;
3584 SmallVector<SDValue, 8> LoadChains;
3585 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003586 unsigned NumMemOps = MemOps.size();
3587 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003588 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003589 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003590 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003591
Dale Johannesen0f502f62009-02-03 22:26:09 +00003592 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003593 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003594 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003595 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003596 LoadValues.push_back(Value);
3597 LoadChains.push_back(Value.getValue(1));
3598 SrcOff += VTSize;
3599 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003601 &LoadChains[0], LoadChains.size());
3602 OutChains.clear();
3603 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003604 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003605 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003606 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003607
Dale Johannesen0f502f62009-02-03 22:26:09 +00003608 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003609 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003610 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003611 OutChains.push_back(Store);
3612 DstOff += VTSize;
3613 }
3614
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003616 &OutChains[0], OutChains.size());
3617}
3618
Dale Johannesen0f502f62009-02-03 22:26:09 +00003619static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003620 SDValue Chain, SDValue Dst,
3621 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003622 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003623 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003624 // Turn a memset of undef to nop.
3625 if (Src.getOpcode() == ISD::UNDEF)
3626 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003627
3628 // Expand memset to a series of load/store ops if the size operand
3629 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003630 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003631 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003632 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003633 MachineFunction &MF = DAG.getMachineFunction();
3634 MachineFrameInfo *MFI = MF.getFrameInfo();
3635 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003636 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3637 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3638 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003639 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003640 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003641 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003642 Size, (DstAlignCanChange ? 0 : Align), 0,
Lang Hames15701f82011-10-26 23:50:43 +00003643 IsZeroVal, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003644 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003645
Evan Cheng255f20f2010-04-01 06:04:33 +00003646 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003647 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003648 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3649 if (NewAlign > Align) {
3650 // Give the stack frame object a larger alignment if needed.
3651 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3652 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3653 Align = NewAlign;
3654 }
3655 }
3656
Dan Gohman475871a2008-07-27 21:46:04 +00003657 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003658 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003659 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003660
3661 // Find the largest store and generate the bit pattern for it.
3662 EVT LargestVT = MemOps[0];
3663 for (unsigned i = 1; i < NumMemOps; i++)
3664 if (MemOps[i].bitsGT(LargestVT))
3665 LargestVT = MemOps[i];
3666 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3667
Dan Gohman707e0182008-04-12 04:36:06 +00003668 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003669 EVT VT = MemOps[i];
Benjamin Kramer80220362011-01-02 19:57:05 +00003670
3671 // If this store is smaller than the largest store see whether we can get
3672 // the smaller value for free with a truncate.
3673 SDValue Value = MemSetValue;
3674 if (VT.bitsLT(LargestVT)) {
3675 if (!LargestVT.isVector() && !VT.isVector() &&
3676 TLI.isTruncateFree(LargestVT, VT))
3677 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3678 else
3679 Value = getMemsetValue(Src, VT, DAG, dl);
3680 }
3681 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003682 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003683 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003684 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003685 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003686 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003687 DstOff += VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003688 }
3689
Owen Anderson825b72b2009-08-11 20:47:22 +00003690 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003691 &OutChains[0], OutChains.size());
3692}
3693
Dale Johannesen0f502f62009-02-03 22:26:09 +00003694SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003695 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003696 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003697 MachinePointerInfo DstPtrInfo,
3698 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003699
3700 // Check to see if we should lower the memcpy to loads and stores first.
3701 // For cases within the target-specified limits, this is the best choice.
3702 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3703 if (ConstantSize) {
3704 // Memcpy with size zero? Just return the original chain.
3705 if (ConstantSize->isNullValue())
3706 return Chain;
3707
Evan Cheng255f20f2010-04-01 06:04:33 +00003708 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3709 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003710 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003711 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003712 return Result;
3713 }
3714
3715 // Then check to see if we should lower the memcpy with target-specific
3716 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003717 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003718 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003719 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003720 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003721 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003722 return Result;
3723
3724 // If we really need inline code and the target declined to provide it,
3725 // use a (potentially long) sequence of loads and stores.
3726 if (AlwaysInline) {
3727 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003728 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003729 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003730 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003731 }
3732
Dan Gohman7b55d362010-04-05 20:24:08 +00003733 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3734 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3735 // respect volatile, so they may do things like read or write memory
3736 // beyond the given memory regions. But fixing this isn't easy, and most
3737 // people don't care.
3738
Dan Gohman707e0182008-04-12 04:36:06 +00003739 // Emit a library call.
3740 TargetLowering::ArgListTy Args;
3741 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003742 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003743 Entry.Node = Dst; Args.push_back(Entry);
3744 Entry.Node = Src; Args.push_back(Entry);
3745 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003746 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003747 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003748 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003749 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003750 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3751 /*isTailCall=*/false,
3752 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003753 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003754 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003755 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003756 return CallResult.second;
3757}
3758
Dale Johannesen0f502f62009-02-03 22:26:09 +00003759SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003760 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003761 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003762 MachinePointerInfo DstPtrInfo,
3763 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003764
Dan Gohman21323f32008-05-29 19:42:22 +00003765 // Check to see if we should lower the memmove to loads and stores first.
3766 // For cases within the target-specified limits, this is the best choice.
3767 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3768 if (ConstantSize) {
3769 // Memmove with size zero? Just return the original chain.
3770 if (ConstantSize->isNullValue())
3771 return Chain;
3772
Dan Gohman475871a2008-07-27 21:46:04 +00003773 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003774 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003775 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003776 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003777 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003778 return Result;
3779 }
Dan Gohman707e0182008-04-12 04:36:06 +00003780
3781 // Then check to see if we should lower the memmove with target-specific
3782 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003783 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003784 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003785 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003786 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003787 return Result;
3788
Mon P Wang01f0e852010-04-06 08:27:51 +00003789 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3790 // not be safe. See memcpy above for more details.
3791
Dan Gohman707e0182008-04-12 04:36:06 +00003792 // Emit a library call.
3793 TargetLowering::ArgListTy Args;
3794 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003795 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003796 Entry.Node = Dst; Args.push_back(Entry);
3797 Entry.Node = Src; Args.push_back(Entry);
3798 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003799 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003800 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003801 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003802 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003803 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3804 /*isTailCall=*/false,
3805 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003806 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003807 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003808 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003809 return CallResult.second;
3810}
3811
Dale Johannesen0f502f62009-02-03 22:26:09 +00003812SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003813 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003814 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003815 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003816
3817 // Check to see if we should lower the memset to stores first.
3818 // For cases within the target-specified limits, this is the best choice.
3819 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3820 if (ConstantSize) {
3821 // Memset with size zero? Just return the original chain.
3822 if (ConstantSize->isNullValue())
3823 return Chain;
3824
Mon P Wang20adc9d2010-04-04 03:10:48 +00003825 SDValue Result =
3826 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003827 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003828
Gabor Greifba36cb52008-08-28 21:40:38 +00003829 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003830 return Result;
3831 }
3832
3833 // Then check to see if we should lower the memset with target-specific
3834 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003835 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003836 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003837 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003838 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003839 return Result;
3840
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003841 // Emit a library call.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003842 Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003843 TargetLowering::ArgListTy Args;
3844 TargetLowering::ArgListEntry Entry;
3845 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3846 Args.push_back(Entry);
3847 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 if (Src.getValueType().bitsGT(MVT::i32))
3849 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003850 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003852 Entry.Node = Src;
3853 Entry.Ty = Type::getInt32Ty(*getContext());
3854 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003855 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003856 Entry.Node = Size;
3857 Entry.Ty = IntPtrTy;
3858 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003859 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003860 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003861 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003862 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003863 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003864 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3865 /*isTailCall=*/false,
3866 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003867 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003868 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003869 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003870 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003871}
3872
Owen Andersone50ed302009-08-10 22:56:29 +00003873SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003874 SDValue Chain, SDValue Ptr, SDValue Cmp,
3875 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003876 unsigned Alignment,
3877 AtomicOrdering Ordering,
3878 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003879 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3880 Alignment = getEVTAlignment(MemVT);
3881
Evan Chengff89dcb2009-10-18 18:16:27 +00003882 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003883 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3884
3885 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003886 // FIXME: Volatile isn't really correct; we should keep track of atomic
3887 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003888 Flags |= MachineMemOperand::MOVolatile;
3889
3890 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00003891 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003892
Eli Friedman55ba8162011-07-29 03:05:32 +00003893 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
3894 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003895}
3896
3897SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3898 SDValue Chain,
3899 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00003900 SDValue Swp, MachineMemOperand *MMO,
3901 AtomicOrdering Ordering,
3902 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003903 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3904 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3905
Owen Andersone50ed302009-08-10 22:56:29 +00003906 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003907
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003909 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003910 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003911 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3912 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3913 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003914 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3915 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003916 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003917 }
Dan Gohman95531882010-03-18 18:49:47 +00003918 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003919 Ptr, Cmp, Swp, MMO, Ordering,
3920 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003921 CSEMap.InsertNode(N, IP);
3922 AllNodes.push_back(N);
3923 return SDValue(N, 0);
3924}
3925
Owen Andersone50ed302009-08-10 22:56:29 +00003926SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003927 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003928 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003929 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00003930 unsigned Alignment,
3931 AtomicOrdering Ordering,
3932 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003933 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3934 Alignment = getEVTAlignment(MemVT);
3935
Evan Chengff89dcb2009-10-18 18:16:27 +00003936 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00003937 // A monotonic store does not load; a release store "loads" in the sense
3938 // that other stores cannot be sunk past it.
3939 // (An atomicrmw obviously both loads and stores.)
3940 unsigned Flags = MachineMemOperand::MOStore;
3941 if (Opcode != ISD::ATOMIC_STORE || Ordering > Monotonic)
3942 Flags |= MachineMemOperand::MOLoad;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003943
3944 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003945 // FIXME: Volatile isn't really correct; we should keep track of atomic
3946 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003947 Flags |= MachineMemOperand::MOVolatile;
3948
3949 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00003950 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00003951 MemVT.getStoreSize(), Alignment);
3952
Eli Friedman55ba8162011-07-29 03:05:32 +00003953 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
3954 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003955}
3956
3957SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3958 SDValue Chain,
3959 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00003960 MachineMemOperand *MMO,
3961 AtomicOrdering Ordering,
3962 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003963 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3964 Opcode == ISD::ATOMIC_LOAD_SUB ||
3965 Opcode == ISD::ATOMIC_LOAD_AND ||
3966 Opcode == ISD::ATOMIC_LOAD_OR ||
3967 Opcode == ISD::ATOMIC_LOAD_XOR ||
3968 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003969 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003970 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003971 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003972 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00003973 Opcode == ISD::ATOMIC_SWAP ||
3974 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003975 "Invalid Atomic Op");
3976
Owen Andersone50ed302009-08-10 22:56:29 +00003977 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003978
Eli Friedman327236c2011-08-24 20:50:09 +00003979 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
3980 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003981 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003982 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003983 SDValue Ops[] = {Chain, Ptr, Val};
3984 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3985 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003986 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3987 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003988 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003989 }
Dan Gohman95531882010-03-18 18:49:47 +00003990 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003991 Ptr, Val, MMO,
3992 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003993 CSEMap.InsertNode(N, IP);
3994 AllNodes.push_back(N);
3995 return SDValue(N, 0);
3996}
3997
Eli Friedman327236c2011-08-24 20:50:09 +00003998SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3999 EVT VT, SDValue Chain,
4000 SDValue Ptr,
4001 const Value* PtrVal,
4002 unsigned Alignment,
4003 AtomicOrdering Ordering,
4004 SynchronizationScope SynchScope) {
4005 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4006 Alignment = getEVTAlignment(MemVT);
4007
4008 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00004009 // A monotonic load does not store; an acquire load "stores" in the sense
4010 // that other loads cannot be hoisted past it.
4011 unsigned Flags = MachineMemOperand::MOLoad;
4012 if (Ordering > Monotonic)
4013 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004014
4015 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004016 // FIXME: Volatile isn't really correct; we should keep track of atomic
4017 // orderings in the memoperand.
Eli Friedman327236c2011-08-24 20:50:09 +00004018 Flags |= MachineMemOperand::MOVolatile;
4019
4020 MachineMemOperand *MMO =
4021 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4022 MemVT.getStoreSize(), Alignment);
4023
4024 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4025 Ordering, SynchScope);
4026}
4027
4028SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4029 EVT VT, SDValue Chain,
4030 SDValue Ptr,
4031 MachineMemOperand *MMO,
4032 AtomicOrdering Ordering,
4033 SynchronizationScope SynchScope) {
4034 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4035
4036 SDVTList VTs = getVTList(VT, MVT::Other);
4037 FoldingSetNodeID ID;
4038 ID.AddInteger(MemVT.getRawBits());
4039 SDValue Ops[] = {Chain, Ptr};
4040 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
4041 void* IP = 0;
4042 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4043 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4044 return SDValue(E, 0);
4045 }
4046 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4047 Ptr, MMO, Ordering, SynchScope);
4048 CSEMap.InsertNode(N, IP);
4049 AllNodes.push_back(N);
4050 return SDValue(N, 0);
4051}
4052
Duncan Sands4bdcb612008-07-02 17:40:58 +00004053/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004054SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4055 DebugLoc dl) {
4056 if (NumOps == 1)
4057 return Ops[0];
4058
Owen Andersone50ed302009-08-10 22:56:29 +00004059 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004060 VTs.reserve(NumOps);
4061 for (unsigned i = 0; i < NumOps; ++i)
4062 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004063 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004064 Ops, NumOps);
4065}
4066
Dan Gohman475871a2008-07-27 21:46:04 +00004067SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004068SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004069 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004070 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004071 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004072 unsigned Align, bool Vol,
4073 bool ReadMem, bool WriteMem) {
4074 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004075 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004076 ReadMem, WriteMem);
4077}
4078
4079SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004080SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4081 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004082 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004083 unsigned Align, bool Vol,
4084 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004085 if (Align == 0) // Ensure that codegen never sees alignment 0
4086 Align = getEVTAlignment(MemVT);
4087
4088 MachineFunction &MF = getMachineFunction();
4089 unsigned Flags = 0;
4090 if (WriteMem)
4091 Flags |= MachineMemOperand::MOStore;
4092 if (ReadMem)
4093 Flags |= MachineMemOperand::MOLoad;
4094 if (Vol)
4095 Flags |= MachineMemOperand::MOVolatile;
4096 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004097 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004098
4099 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4100}
4101
4102SDValue
4103SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4104 const SDValue *Ops, unsigned NumOps,
4105 EVT MemVT, MachineMemOperand *MMO) {
4106 assert((Opcode == ISD::INTRINSIC_VOID ||
4107 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004108 Opcode == ISD::PREFETCH ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004109 (Opcode <= INT_MAX &&
4110 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4111 "Opcode is not a memory-accessing opcode!");
4112
Dale Johannesene8c17332009-01-29 00:47:48 +00004113 // Memoize the node unless it returns a flag.
4114 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004115 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004116 FoldingSetNodeID ID;
4117 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4118 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004119 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4120 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004121 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004122 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004123
Dan Gohman95531882010-03-18 18:49:47 +00004124 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4125 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004126 CSEMap.InsertNode(N, IP);
4127 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004128 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4129 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004130 }
4131 AllNodes.push_back(N);
4132 return SDValue(N, 0);
4133}
4134
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004135/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4136/// MachinePointerInfo record from it. This is particularly useful because the
4137/// code generator has many cases where it doesn't bother passing in a
4138/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4139static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4140 // If this is FI+Offset, we can model it.
4141 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4142 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4143
4144 // If this is (FI+Offset1)+Offset2, we can model it.
4145 if (Ptr.getOpcode() != ISD::ADD ||
4146 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4147 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4148 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004149
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004150 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4151 return MachinePointerInfo::getFixedStack(FI, Offset+
4152 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4153}
4154
4155/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4156/// MachinePointerInfo record from it. This is particularly useful because the
4157/// code generator has many cases where it doesn't bother passing in a
4158/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4159static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4160 // If the 'Offset' value isn't a constant, we can't handle this.
4161 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4162 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4163 if (OffsetOp.getOpcode() == ISD::UNDEF)
4164 return InferPointerInfo(Ptr);
4165 return MachinePointerInfo();
4166}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004167
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004168
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004169SDValue
4170SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4171 EVT VT, DebugLoc dl, SDValue Chain,
4172 SDValue Ptr, SDValue Offset,
4173 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004174 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004175 unsigned Alignment, const MDNode *TBAAInfo,
4176 const MDNode *Ranges) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004177 assert(Chain.getValueType() == MVT::Other &&
4178 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004179 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4180 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004181
Dan Gohmanc76909a2009-09-25 20:36:54 +00004182 unsigned Flags = MachineMemOperand::MOLoad;
4183 if (isVolatile)
4184 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004185 if (isNonTemporal)
4186 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004187 if (isInvariant)
4188 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004189
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004190 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4191 // clients.
4192 if (PtrInfo.V == 0)
4193 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004194
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004195 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004196 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004197 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004198 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004199 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004200}
4201
4202SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004203SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004204 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004205 SDValue Ptr, SDValue Offset, EVT MemVT,
4206 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004207 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004208 ExtType = ISD::NON_EXTLOAD;
4209 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004210 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004211 } else {
4212 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004213 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4214 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004215 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004216 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004217 assert(VT.isVector() == MemVT.isVector() &&
4218 "Cannot use trunc store to convert to or from a vector!");
4219 assert((!VT.isVector() ||
4220 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4221 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004222 }
4223
4224 bool Indexed = AM != ISD::UNINDEXED;
4225 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4226 "Unindexed load with an offset!");
4227
4228 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004229 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004230 SDValue Ops[] = { Chain, Ptr, Offset };
4231 FoldingSetNodeID ID;
4232 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004233 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004234 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004235 MMO->isNonTemporal(),
4236 MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004237 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004238 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4239 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004240 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004241 }
Dan Gohman95531882010-03-18 18:49:47 +00004242 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4243 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004244 CSEMap.InsertNode(N, IP);
4245 AllNodes.push_back(N);
4246 return SDValue(N, 0);
4247}
4248
Owen Andersone50ed302009-08-10 22:56:29 +00004249SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004250 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004251 MachinePointerInfo PtrInfo,
4252 bool isVolatile, bool isNonTemporal,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004253 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004254 const MDNode *TBAAInfo,
4255 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004256 SDValue Undef = getUNDEF(Ptr.getValueType());
4257 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004258 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4259 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004260}
Dale Johannesene8c17332009-01-29 00:47:48 +00004261
Stuart Hastingsa9011292011-02-16 16:23:55 +00004262SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004263 SDValue Chain, SDValue Ptr,
4264 MachinePointerInfo PtrInfo, EVT MemVT,
4265 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004266 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004267 SDValue Undef = getUNDEF(Ptr.getValueType());
4268 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004269 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004270 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004271}
4272
4273
Dan Gohman475871a2008-07-27 21:46:04 +00004274SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004275SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4276 SDValue Offset, ISD::MemIndexedMode AM) {
4277 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4278 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4279 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004280 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004281 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004282 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
4283 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004284}
4285
Dale Johannesene8c17332009-01-29 00:47:48 +00004286SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004287 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004288 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004289 unsigned Alignment, const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004290 assert(Chain.getValueType() == MVT::Other &&
4291 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004292 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004293 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004294
Dan Gohmanc76909a2009-09-25 20:36:54 +00004295 unsigned Flags = MachineMemOperand::MOStore;
4296 if (isVolatile)
4297 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004298 if (isNonTemporal)
4299 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004300
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004301 if (PtrInfo.V == 0)
4302 PtrInfo = InferPointerInfo(Ptr);
4303
4304 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004305 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004306 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004307 Val.getValueType().getStoreSize(), Alignment,
4308 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004309
4310 return getStore(Chain, dl, Val, Ptr, MMO);
4311}
4312
4313SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4314 SDValue Ptr, MachineMemOperand *MMO) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004315 assert(Chain.getValueType() == MVT::Other &&
4316 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004317 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004318 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004319 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004320 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4321 FoldingSetNodeID ID;
4322 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004323 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004324 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004325 MMO->isNonTemporal(), MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004326 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004327 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4328 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004329 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004330 }
Dan Gohman95531882010-03-18 18:49:47 +00004331 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4332 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004333 CSEMap.InsertNode(N, IP);
4334 AllNodes.push_back(N);
4335 return SDValue(N, 0);
4336}
4337
Dale Johannesene8c17332009-01-29 00:47:48 +00004338SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004339 SDValue Ptr, MachinePointerInfo PtrInfo,
4340 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004341 unsigned Alignment,
4342 const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004343 assert(Chain.getValueType() == MVT::Other &&
4344 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004345 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4346 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004347
Dan Gohmanc76909a2009-09-25 20:36:54 +00004348 unsigned Flags = MachineMemOperand::MOStore;
4349 if (isVolatile)
4350 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004351 if (isNonTemporal)
4352 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004353
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004354 if (PtrInfo.V == 0)
4355 PtrInfo = InferPointerInfo(Ptr);
4356
4357 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004358 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004359 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4360 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004361
4362 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4363}
4364
4365SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4366 SDValue Ptr, EVT SVT,
4367 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004368 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004369
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004370 assert(Chain.getValueType() == MVT::Other &&
4371 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004372 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004373 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004374
Dan Gohman2e141d72009-12-14 23:40:38 +00004375 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4376 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004377 assert(VT.isInteger() == SVT.isInteger() &&
4378 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004379 assert(VT.isVector() == SVT.isVector() &&
4380 "Cannot use trunc store to convert to or from a vector!");
4381 assert((!VT.isVector() ||
4382 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4383 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004384
Owen Anderson825b72b2009-08-11 20:47:22 +00004385 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004386 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004387 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4388 FoldingSetNodeID ID;
4389 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004390 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004391 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004392 MMO->isNonTemporal(), MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004393 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004394 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4395 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004396 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004397 }
Dan Gohman95531882010-03-18 18:49:47 +00004398 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4399 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004400 CSEMap.InsertNode(N, IP);
4401 AllNodes.push_back(N);
4402 return SDValue(N, 0);
4403}
4404
Dan Gohman475871a2008-07-27 21:46:04 +00004405SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004406SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4407 SDValue Offset, ISD::MemIndexedMode AM) {
4408 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4409 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4410 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004411 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004412 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4413 FoldingSetNodeID ID;
4414 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004415 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004416 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00004417 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004418 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004419 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004420
Dan Gohman95531882010-03-18 18:49:47 +00004421 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4422 ST->isTruncatingStore(),
4423 ST->getMemoryVT(),
4424 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004425 CSEMap.InsertNode(N, IP);
4426 AllNodes.push_back(N);
4427 return SDValue(N, 0);
4428}
4429
Owen Andersone50ed302009-08-10 22:56:29 +00004430SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004431 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004432 SDValue SV,
4433 unsigned Align) {
4434 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4435 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004436}
4437
Owen Andersone50ed302009-08-10 22:56:29 +00004438SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004439 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004440 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004441 case 0: return getNode(Opcode, DL, VT);
4442 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4443 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4444 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004445 default: break;
4446 }
4447
Dan Gohman475871a2008-07-27 21:46:04 +00004448 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004449 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004450 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004451 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004452}
4453
Owen Andersone50ed302009-08-10 22:56:29 +00004454SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004455 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004456 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004457 case 0: return getNode(Opcode, DL, VT);
4458 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4459 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4460 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004461 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004462 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004463
Chris Lattneref847df2005-04-09 03:27:28 +00004464 switch (Opcode) {
4465 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004466 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004467 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004468 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4469 "LHS and RHS of condition must have same type!");
4470 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4471 "True and False arms of SelectCC must have same type!");
4472 assert(Ops[2].getValueType() == VT &&
4473 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004474 break;
4475 }
4476 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004477 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004478 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4479 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004480 break;
4481 }
Chris Lattneref847df2005-04-09 03:27:28 +00004482 }
4483
Chris Lattner385328c2005-05-14 07:42:29 +00004484 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004485 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004486 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004487
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004488 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004489 FoldingSetNodeID ID;
4490 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004491 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004492
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004493 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004494 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004495
Dan Gohman95531882010-03-18 18:49:47 +00004496 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004497 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004498 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004499 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004500 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004501
Chris Lattneref847df2005-04-09 03:27:28 +00004502 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004503#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004504 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004505#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004506 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004507}
4508
Bill Wendling7ade28c2009-01-28 22:17:52 +00004509SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004510 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004511 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004512 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004513 Ops, NumOps);
4514}
4515
Bill Wendling7ade28c2009-01-28 22:17:52 +00004516SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004517 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004518 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004519 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004520 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4521 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004522}
4523
Bill Wendling7ade28c2009-01-28 22:17:52 +00004524SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4525 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004526 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004527 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004528
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004529#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004530 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004531 // FIXME: figure out how to safely handle things like
4532 // int foo(int x) { return 1 << (x & 255); }
4533 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004534 case ISD::SRA_PARTS:
4535 case ISD::SRL_PARTS:
4536 case ISD::SHL_PARTS:
4537 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004539 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004540 else if (N3.getOpcode() == ISD::AND)
4541 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4542 // If the and is only masking out bits that cannot effect the shift,
4543 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004544 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004545 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004546 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004547 }
4548 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004549 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004550#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004551
Chris Lattner43247a12005-08-25 19:12:10 +00004552 // Memoize the node unless it returns a flag.
4553 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004554 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004555 FoldingSetNodeID ID;
4556 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004557 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004558 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004559 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004560
Dan Gohman0e5f1302008-07-07 23:02:41 +00004561 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004562 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004563 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004564 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004565 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004566 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4567 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004568 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004569 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004570 }
Chris Lattnera5682852006-08-07 23:03:03 +00004571 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004572 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004573 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004574 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004575 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004576 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004577 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004578 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4579 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004580 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004581 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004582 }
Chris Lattner43247a12005-08-25 19:12:10 +00004583 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004584 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004585#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004586 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004587#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004588 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004589}
4590
Bill Wendling7ade28c2009-01-28 22:17:52 +00004591SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4592 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004593}
4594
Bill Wendling7ade28c2009-01-28 22:17:52 +00004595SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4596 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004597 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004598 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004599}
4600
Bill Wendling7ade28c2009-01-28 22:17:52 +00004601SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4602 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004604 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004605}
4606
Bill Wendling7ade28c2009-01-28 22:17:52 +00004607SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4608 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004609 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004610 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004611}
4612
Bill Wendling7ade28c2009-01-28 22:17:52 +00004613SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4614 SDValue N1, SDValue N2, SDValue N3,
4615 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004616 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004617 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004618}
4619
Bill Wendling7ade28c2009-01-28 22:17:52 +00004620SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4621 SDValue N1, SDValue N2, SDValue N3,
4622 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004623 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004624 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004625}
4626
Owen Andersone50ed302009-08-10 22:56:29 +00004627SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004628 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004629}
4630
Owen Andersone50ed302009-08-10 22:56:29 +00004631SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004632 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4633 E = VTList.rend(); I != E; ++I)
4634 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4635 return *I;
4636
Owen Andersone50ed302009-08-10 22:56:29 +00004637 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004638 Array[0] = VT1;
4639 Array[1] = VT2;
4640 SDVTList Result = makeVTList(Array, 2);
4641 VTList.push_back(Result);
4642 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004643}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004644
Owen Andersone50ed302009-08-10 22:56:29 +00004645SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004646 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4647 E = VTList.rend(); I != E; ++I)
4648 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4649 I->VTs[2] == VT3)
4650 return *I;
4651
Owen Andersone50ed302009-08-10 22:56:29 +00004652 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004653 Array[0] = VT1;
4654 Array[1] = VT2;
4655 Array[2] = VT3;
4656 SDVTList Result = makeVTList(Array, 3);
4657 VTList.push_back(Result);
4658 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004659}
4660
Owen Andersone50ed302009-08-10 22:56:29 +00004661SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004662 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4663 E = VTList.rend(); I != E; ++I)
4664 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4665 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4666 return *I;
4667
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004668 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004669 Array[0] = VT1;
4670 Array[1] = VT2;
4671 Array[2] = VT3;
4672 Array[3] = VT4;
4673 SDVTList Result = makeVTList(Array, 4);
4674 VTList.push_back(Result);
4675 return Result;
4676}
4677
Owen Andersone50ed302009-08-10 22:56:29 +00004678SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004679 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004680 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004681 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004682 case 2: return getVTList(VTs[0], VTs[1]);
4683 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004684 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004685 default: break;
4686 }
4687
Dan Gohmane8be6c62008-07-17 19:10:17 +00004688 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4689 E = VTList.rend(); I != E; ++I) {
4690 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4691 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004692
Chris Lattner70046e92006-08-15 17:46:01 +00004693 bool NoMatch = false;
4694 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004695 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004696 NoMatch = true;
4697 break;
4698 }
4699 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004700 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004701 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004702
Owen Andersone50ed302009-08-10 22:56:29 +00004703 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004704 std::copy(VTs, VTs+NumVTs, Array);
4705 SDVTList Result = makeVTList(Array, NumVTs);
4706 VTList.push_back(Result);
4707 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004708}
4709
4710
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004711/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4712/// specified operands. If the resultant node already exists in the DAG,
4713/// this does not modify the specified node, instead it returns the node that
4714/// already exists. If the resultant node does not exist in the DAG, the
4715/// input node is returned. As a degenerate case, if you specify the same
4716/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004717SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004718 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004719
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004720 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004721 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004722
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004723 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004724 void *InsertPos = 0;
4725 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004726 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004727
Dan Gohman79acd2b2008-07-21 22:38:59 +00004728 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004729 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004730 if (!RemoveNodeFromCSEMaps(N))
4731 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004732
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004733 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004734 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004735
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004736 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004737 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004738 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004739}
4740
Dan Gohman027657d2010-06-18 15:30:29 +00004741SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004742 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004743
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004744 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004745 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004746 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004747
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004748 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004749 void *InsertPos = 0;
4750 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004751 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004752
Dan Gohman79acd2b2008-07-21 22:38:59 +00004753 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004754 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004755 if (!RemoveNodeFromCSEMaps(N))
4756 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004757
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004758 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004759 if (N->OperandList[0] != Op1)
4760 N->OperandList[0].set(Op1);
4761 if (N->OperandList[1] != Op2)
4762 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004763
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004764 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004765 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004766 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004767}
4768
Dan Gohman027657d2010-06-18 15:30:29 +00004769SDNode *SelectionDAG::
4770UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004771 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004772 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004773}
4774
Dan Gohman027657d2010-06-18 15:30:29 +00004775SDNode *SelectionDAG::
4776UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004777 SDValue Op3, SDValue Op4) {
4778 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004779 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004780}
4781
Dan Gohman027657d2010-06-18 15:30:29 +00004782SDNode *SelectionDAG::
4783UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004784 SDValue Op3, SDValue Op4, SDValue Op5) {
4785 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004786 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004787}
4788
Dan Gohman027657d2010-06-18 15:30:29 +00004789SDNode *SelectionDAG::
4790UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004791 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004792 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004793
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004794 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004795 bool AnyChange = false;
4796 for (unsigned i = 0; i != NumOps; ++i) {
4797 if (Ops[i] != N->getOperand(i)) {
4798 AnyChange = true;
4799 break;
4800 }
4801 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004802
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004803 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004804 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004805
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004806 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004807 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004808 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004809 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004810
Dan Gohman7ceda162008-05-02 00:05:03 +00004811 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004812 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004813 if (!RemoveNodeFromCSEMaps(N))
4814 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004815
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004816 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004817 for (unsigned i = 0; i != NumOps; ++i)
4818 if (N->OperandList[i] != Ops[i])
4819 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004820
4821 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004822 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004823 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004824}
4825
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004826/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004827/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004828void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004829 // Unlike the code in MorphNodeTo that does this, we don't need to
4830 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004831 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4832 SDUse &Use = *I++;
4833 Use.set(SDValue());
4834 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004835}
Chris Lattner149c58c2005-08-16 18:17:10 +00004836
Dan Gohmane8be6c62008-07-17 19:10:17 +00004837/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4838/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004839///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004840SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004841 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004842 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004843 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004844}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004845
Dan Gohmane8be6c62008-07-17 19:10:17 +00004846SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004847 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004848 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004849 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004850 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004851}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004852
Dan Gohmane8be6c62008-07-17 19:10:17 +00004853SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004854 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004855 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004856 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004857 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004858 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004859}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004860
Dan Gohmane8be6c62008-07-17 19:10:17 +00004861SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004862 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004863 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004864 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004865 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004866 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004867}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004868
Dan Gohmane8be6c62008-07-17 19:10:17 +00004869SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004870 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004871 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004872 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004873 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004874}
4875
Dan Gohmane8be6c62008-07-17 19:10:17 +00004876SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004877 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004878 unsigned NumOps) {
4879 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004880 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004881}
4882
Dan Gohmane8be6c62008-07-17 19:10:17 +00004883SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004884 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004885 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004886 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004887}
4888
Dan Gohmane8be6c62008-07-17 19:10:17 +00004889SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004890 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004891 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004892 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004893 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004894}
4895
Bill Wendling13d6d442008-12-01 23:28:22 +00004896SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004897 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004898 const SDValue *Ops, unsigned NumOps) {
4899 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4900 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4901}
4902
Scott Michelfdc40a02009-02-17 22:15:04 +00004903SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004904 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004905 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004906 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004907 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004908 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004909}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004910
Scott Michelfdc40a02009-02-17 22:15:04 +00004911SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004912 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004914 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004915 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004916 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004917}
4918
Dan Gohmane8be6c62008-07-17 19:10:17 +00004919SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004920 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004921 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004922 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004923 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004924 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004925 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004926}
4927
Dan Gohmane8be6c62008-07-17 19:10:17 +00004928SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004929 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004930 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004931 SDValue Op3) {
4932 SDVTList VTs = getVTList(VT1, VT2, VT3);
4933 SDValue Ops[] = { Op1, Op2, Op3 };
4934 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4935}
4936
4937SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004938 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004939 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00004940 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4941 // Reset the NodeID to -1.
4942 N->setNodeId(-1);
4943 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004944}
4945
Devang Patel0508d042011-12-15 18:21:18 +00004946/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
4947/// the line number information on the merged node since it is not possible to
4948/// preserve the information that operation is associated with multiple lines.
4949/// This will make the debugger working better at -O0, were there is a higher
4950/// probability having other instructions associated with that line.
4951///
4952SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
4953 DebugLoc NLoc = N->getDebugLoc();
4954 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
4955 N->setDebugLoc(DebugLoc());
4956 }
4957 return N;
4958}
4959
Chris Lattner21221e32010-02-28 21:36:14 +00004960/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00004961/// return type, opcode, and operands.
4962///
4963/// Note that MorphNodeTo returns the resultant node. If there is already a
4964/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004965/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004966///
4967/// Using MorphNodeTo is faster than creating a new node and swapping it in
4968/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004969/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004970/// the node's users.
4971///
4972SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004973 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004974 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004975 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004976 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004977 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004978 FoldingSetNodeID ID;
4979 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004980 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00004981 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004982 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004983
Dan Gohman095cc292008-09-13 01:54:27 +00004984 if (!RemoveNodeFromCSEMaps(N))
4985 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004986
Dan Gohmane8be6c62008-07-17 19:10:17 +00004987 // Start the morphing.
4988 N->NodeType = Opc;
4989 N->ValueList = VTs.VTs;
4990 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004991
Dan Gohmane8be6c62008-07-17 19:10:17 +00004992 // Clear the operands list, updating used nodes to remove this from their
4993 // use list. Keep track of any operands that become dead as a result.
4994 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004995 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4996 SDUse &Use = *I++;
4997 SDNode *Used = Use.getNode();
4998 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004999 if (Used->use_empty())
5000 DeadNodeSet.insert(Used);
5001 }
5002
Dan Gohmanc76909a2009-09-25 20:36:54 +00005003 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5004 // Initialize the memory references information.
5005 MN->setMemRefs(0, 0);
5006 // If NumOps is larger than the # of operands we can have in a
5007 // MachineSDNode, reallocate the operand list.
5008 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5009 if (MN->OperandsNeedDelete)
5010 delete[] MN->OperandList;
5011 if (NumOps > array_lengthof(MN->LocalOperands))
5012 // We're creating a final node that will live unmorphed for the
5013 // remainder of the current SelectionDAG iteration, so we can allocate
5014 // the operands directly out of a pool with no recycling metadata.
5015 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005016 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005017 else
5018 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5019 MN->OperandsNeedDelete = false;
5020 } else
5021 MN->InitOperands(MN->OperandList, Ops, NumOps);
5022 } else {
5023 // If NumOps is larger than the # of operands we currently have, reallocate
5024 // the operand list.
5025 if (NumOps > N->NumOperands) {
5026 if (N->OperandsNeedDelete)
5027 delete[] N->OperandList;
5028 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005029 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005030 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005031 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005032 }
5033
5034 // Delete any nodes that are still dead after adding the uses for the
5035 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005036 if (!DeadNodeSet.empty()) {
5037 SmallVector<SDNode *, 16> DeadNodes;
5038 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5039 E = DeadNodeSet.end(); I != E; ++I)
5040 if ((*I)->use_empty())
5041 DeadNodes.push_back(*I);
5042 RemoveDeadNodes(DeadNodes);
5043 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005044
Dan Gohmane8be6c62008-07-17 19:10:17 +00005045 if (IP)
5046 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005047 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005048}
5049
Chris Lattner0fb094f2005-11-19 01:44:53 +00005050
Dan Gohman602b0c82009-09-25 18:54:59 +00005051/// getMachineNode - These are used for target selectors to create a new node
5052/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005053///
Dan Gohman602b0c82009-09-25 18:54:59 +00005054/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005055/// node of the specified opcode and operands, it returns that node instead of
5056/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005057MachineSDNode *
5058SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005059 SDVTList VTs = getVTList(VT);
5060 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005061}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005062
Dan Gohmanc81b7832009-10-10 01:29:16 +00005063MachineSDNode *
5064SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005065 SDVTList VTs = getVTList(VT);
5066 SDValue Ops[] = { Op1 };
5067 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005068}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005069
Dan Gohmanc81b7832009-10-10 01:29:16 +00005070MachineSDNode *
5071SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5072 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005073 SDVTList VTs = getVTList(VT);
5074 SDValue Ops[] = { Op1, Op2 };
5075 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005076}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005077
Dan Gohmanc81b7832009-10-10 01:29:16 +00005078MachineSDNode *
5079SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5080 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005081 SDVTList VTs = getVTList(VT);
5082 SDValue Ops[] = { Op1, Op2, Op3 };
5083 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005084}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005085
Dan Gohmanc81b7832009-10-10 01:29:16 +00005086MachineSDNode *
5087SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5088 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005089 SDVTList VTs = getVTList(VT);
5090 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005091}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005092
Dan Gohmanc81b7832009-10-10 01:29:16 +00005093MachineSDNode *
5094SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005095 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005096 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005097}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005098
Dan Gohmanc81b7832009-10-10 01:29:16 +00005099MachineSDNode *
5100SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5101 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005102 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005103 SDValue Ops[] = { Op1 };
5104 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005105}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005106
Dan Gohmanc81b7832009-10-10 01:29:16 +00005107MachineSDNode *
5108SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5109 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005110 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005111 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005112 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005113}
5114
Dan Gohmanc81b7832009-10-10 01:29:16 +00005115MachineSDNode *
5116SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5117 EVT VT1, EVT VT2, SDValue Op1,
5118 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005119 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005120 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005121 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005122}
5123
Dan Gohmanc81b7832009-10-10 01:29:16 +00005124MachineSDNode *
5125SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5126 EVT VT1, EVT VT2,
5127 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005128 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005129 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005130}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005131
Dan Gohmanc81b7832009-10-10 01:29:16 +00005132MachineSDNode *
5133SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5134 EVT VT1, EVT VT2, EVT VT3,
5135 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005136 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005137 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005138 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005139}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005140
Dan Gohmanc81b7832009-10-10 01:29:16 +00005141MachineSDNode *
5142SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5143 EVT VT1, EVT VT2, EVT VT3,
5144 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005145 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005146 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005147 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005148}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005149
Dan Gohmanc81b7832009-10-10 01:29:16 +00005150MachineSDNode *
5151SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5152 EVT VT1, EVT VT2, EVT VT3,
5153 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005154 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005155 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005156}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005157
Dan Gohmanc81b7832009-10-10 01:29:16 +00005158MachineSDNode *
5159SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5160 EVT VT2, EVT VT3, EVT VT4,
5161 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005162 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005163 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005164}
5165
Dan Gohmanc81b7832009-10-10 01:29:16 +00005166MachineSDNode *
5167SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5168 const std::vector<EVT> &ResultTys,
5169 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005170 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5171 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5172}
5173
Dan Gohmanc81b7832009-10-10 01:29:16 +00005174MachineSDNode *
5175SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5176 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005177 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005178 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005179 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005180
5181 if (DoCSE) {
5182 FoldingSetNodeID ID;
5183 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5184 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005185 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5186 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5187 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005188 }
5189
5190 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005191 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005192
5193 // Initialize the operands list.
5194 if (NumOps > array_lengthof(N->LocalOperands))
5195 // We're creating a final node that will live unmorphed for the
5196 // remainder of the current SelectionDAG iteration, so we can allocate
5197 // the operands directly out of a pool with no recycling metadata.
5198 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5199 Ops, NumOps);
5200 else
5201 N->InitOperands(N->LocalOperands, Ops, NumOps);
5202 N->OperandsNeedDelete = false;
5203
5204 if (DoCSE)
5205 CSEMap.InsertNode(N, IP);
5206
5207 AllNodes.push_back(N);
5208#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005209 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005210#endif
5211 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005212}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005213
Dan Gohman6a402dc2009-08-19 18:16:17 +00005214/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005215/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005216SDValue
5217SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5218 SDValue Operand) {
5219 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005220 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005221 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005222 return SDValue(Subreg, 0);
5223}
5224
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005225/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005226/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005227SDValue
5228SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5229 SDValue Operand, SDValue Subreg) {
5230 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005231 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005232 VT, Operand, Subreg, SRIdxVal);
5233 return SDValue(Result, 0);
5234}
5235
Evan Cheng08b11732008-03-22 01:55:50 +00005236/// getNodeIfExists - Get the specified node if it's already available, or
5237/// else return NULL.
5238SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005239 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005240 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005241 FoldingSetNodeID ID;
5242 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5243 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005244 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005245 return E;
5246 }
5247 return NULL;
5248}
5249
Evan Cheng31441b72010-03-29 20:48:30 +00005250/// getDbgValue - Creates a SDDbgValue node.
5251///
5252SDDbgValue *
5253SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5254 DebugLoc DL, unsigned O) {
5255 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5256}
5257
5258SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005259SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005260 DebugLoc DL, unsigned O) {
5261 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5262}
5263
5264SDDbgValue *
5265SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5266 DebugLoc DL, unsigned O) {
5267 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5268}
5269
Dan Gohmana72d2a22010-03-03 21:33:37 +00005270namespace {
5271
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005272/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005273/// pointed to by a use iterator is deleted, increment the use iterator
5274/// so that it doesn't dangle.
5275///
5276/// This class also manages a "downlink" DAGUpdateListener, to forward
5277/// messages to ReplaceAllUsesWith's callers.
5278///
5279class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
5280 SelectionDAG::DAGUpdateListener *DownLink;
5281 SDNode::use_iterator &UI;
5282 SDNode::use_iterator &UE;
5283
5284 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5285 // Increment the iterator as needed.
5286 while (UI != UE && N == *UI)
5287 ++UI;
5288
5289 // Then forward the message.
5290 if (DownLink) DownLink->NodeDeleted(N, E);
5291 }
5292
5293 virtual void NodeUpdated(SDNode *N) {
5294 // Just forward the message.
5295 if (DownLink) DownLink->NodeUpdated(N);
5296 }
5297
5298public:
5299 RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl,
5300 SDNode::use_iterator &ui,
5301 SDNode::use_iterator &ue)
5302 : DownLink(dl), UI(ui), UE(ue) {}
5303};
5304
5305}
5306
Evan Cheng99157a02006-08-07 22:13:29 +00005307/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005308/// This can cause recursive merging of nodes in the DAG.
5309///
Chris Lattner11d049c2008-02-03 03:35:22 +00005310/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005311///
Dan Gohman475871a2008-07-27 21:46:04 +00005312void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005313 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005314 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005315 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005316 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005317 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005318
Dan Gohman39946102009-01-25 16:29:12 +00005319 // Iterate over all the existing uses of From. New uses will be added
5320 // to the beginning of the use list, which we avoid visiting.
5321 // This specifically avoids visiting uses of From that arise while the
5322 // replacement is happening, because any such uses would be the result
5323 // of CSE: If an existing node looks like From after one of its operands
5324 // is replaced by To, we don't want to replace of all its users with To
5325 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005326 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005327 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005328 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005329 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005330
Chris Lattner8b8749f2005-08-17 19:00:20 +00005331 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005332 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005333
Dan Gohman39946102009-01-25 16:29:12 +00005334 // A user can appear in a use list multiple times, and when this
5335 // happens the uses are usually next to each other in the list.
5336 // To help reduce the number of CSE recomputations, process all
5337 // the uses of this user that we can find this way.
5338 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005339 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005340 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005341 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005342 } while (UI != UE && *UI == User);
5343
5344 // Now that we have modified User, add it back to the CSE maps. If it
5345 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005346 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner8b52f212005-08-26 18:36:28 +00005347 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005348
5349 // If we just RAUW'd the root, take note.
5350 if (FromN == getRoot())
5351 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005352}
5353
5354/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5355/// This can cause recursive merging of nodes in the DAG.
5356///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005357/// This version assumes that for each value of From, there is a
5358/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005359///
Chris Lattner1e111c72005-09-07 05:37:01 +00005360void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005361 DAGUpdateListener *UpdateListener) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005362#ifndef NDEBUG
5363 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5364 assert((!From->hasAnyUseOfValue(i) ||
5365 From->getValueType(i) == To->getValueType(i)) &&
5366 "Cannot use this version of ReplaceAllUsesWith!");
5367#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005368
5369 // Handle the trivial case.
5370 if (From == To)
5371 return;
5372
Dan Gohmandbe664a2009-01-19 21:44:21 +00005373 // Iterate over just the existing users of From. See the comments in
5374 // the ReplaceAllUsesWith above.
5375 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005376 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005377 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005378 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005379
Chris Lattner8b52f212005-08-26 18:36:28 +00005380 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005381 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005382
Dan Gohman39946102009-01-25 16:29:12 +00005383 // A user can appear in a use list multiple times, and when this
5384 // happens the uses are usually next to each other in the list.
5385 // To help reduce the number of CSE recomputations, process all
5386 // the uses of this user that we can find this way.
5387 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005388 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005389 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005390 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005391 } while (UI != UE && *UI == User);
5392
5393 // Now that we have modified User, add it back to the CSE maps. If it
5394 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005395 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005396 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005397
5398 // If we just RAUW'd the root, take note.
5399 if (From == getRoot().getNode())
5400 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005401}
5402
Chris Lattner8b52f212005-08-26 18:36:28 +00005403/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5404/// This can cause recursive merging of nodes in the DAG.
5405///
5406/// This version can replace From with any result values. To must match the
5407/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00005408void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00005409 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005410 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005411 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00005412 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005413
Dan Gohmandbe664a2009-01-19 21:44:21 +00005414 // Iterate over just the existing users of From. See the comments in
5415 // the ReplaceAllUsesWith above.
5416 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005417 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005418 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005419 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005420
Chris Lattner7b2880c2005-08-24 22:44:39 +00005421 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005422 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005423
Dan Gohman39946102009-01-25 16:29:12 +00005424 // A user can appear in a use list multiple times, and when this
5425 // happens the uses are usually next to each other in the list.
5426 // To help reduce the number of CSE recomputations, process all
5427 // the uses of this user that we can find this way.
5428 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005429 SDUse &Use = UI.getUse();
5430 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005431 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005432 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005433 } while (UI != UE && *UI == User);
5434
5435 // Now that we have modified User, add it back to the CSE maps. If it
5436 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005437 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005438 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005439
5440 // If we just RAUW'd the root, take note.
5441 if (From == getRoot().getNode())
5442 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005443}
5444
Chris Lattner012f2412006-02-17 21:58:01 +00005445/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005446/// uses of other values produced by From.getNode() alone. The Deleted
5447/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005448void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005449 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005450 // Handle the really simple, really trivial case efficiently.
5451 if (From == To) return;
5452
Chris Lattner012f2412006-02-17 21:58:01 +00005453 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005454 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005455 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005456 return;
5457 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005458
Dan Gohman39946102009-01-25 16:29:12 +00005459 // Iterate over just the existing users of From. See the comments in
5460 // the ReplaceAllUsesWith above.
5461 SDNode::use_iterator UI = From.getNode()->use_begin(),
5462 UE = From.getNode()->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005463 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005464 while (UI != UE) {
5465 SDNode *User = *UI;
5466 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005467
Dan Gohman39946102009-01-25 16:29:12 +00005468 // A user can appear in a use list multiple times, and when this
5469 // happens the uses are usually next to each other in the list.
5470 // To help reduce the number of CSE recomputations, process all
5471 // the uses of this user that we can find this way.
5472 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005473 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005474
5475 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005476 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005477 ++UI;
5478 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005479 }
Dan Gohman39946102009-01-25 16:29:12 +00005480
5481 // If this node hasn't been modified yet, it's still in the CSE maps,
5482 // so remove its old self from the CSE maps.
5483 if (!UserRemovedFromCSEMaps) {
5484 RemoveNodeFromCSEMaps(User);
5485 UserRemovedFromCSEMaps = true;
5486 }
5487
5488 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005489 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005490 } while (UI != UE && *UI == User);
5491
5492 // We are iterating over all uses of the From node, so if a use
5493 // doesn't use the specific value, no changes are made.
5494 if (!UserRemovedFromCSEMaps)
5495 continue;
5496
Chris Lattner01d029b2007-10-15 06:10:22 +00005497 // Now that we have modified User, add it back to the CSE maps. If it
5498 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005499 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005500 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005501
5502 // If we just RAUW'd the root, take note.
5503 if (From == getRoot())
5504 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005505}
5506
Dan Gohman39946102009-01-25 16:29:12 +00005507namespace {
5508 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5509 /// to record information about a use.
5510 struct UseMemo {
5511 SDNode *User;
5512 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005513 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005514 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005515
5516 /// operator< - Sort Memos by User.
5517 bool operator<(const UseMemo &L, const UseMemo &R) {
5518 return (intptr_t)L.User < (intptr_t)R.User;
5519 }
Dan Gohman39946102009-01-25 16:29:12 +00005520}
5521
Dan Gohmane8be6c62008-07-17 19:10:17 +00005522/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005523/// uses of other values produced by From.getNode() alone. The same value
5524/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005525/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005526void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5527 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005528 unsigned Num,
5529 DAGUpdateListener *UpdateListener){
5530 // Handle the simple, trivial case efficiently.
5531 if (Num == 1)
5532 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5533
Dan Gohman39946102009-01-25 16:29:12 +00005534 // Read up all the uses and make records of them. This helps
5535 // processing new uses that are introduced during the
5536 // replacement process.
5537 SmallVector<UseMemo, 4> Uses;
5538 for (unsigned i = 0; i != Num; ++i) {
5539 unsigned FromResNo = From[i].getResNo();
5540 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005541 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005542 E = FromNode->use_end(); UI != E; ++UI) {
5543 SDUse &Use = UI.getUse();
5544 if (Use.getResNo() == FromResNo) {
5545 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005546 Uses.push_back(Memo);
5547 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005548 }
Dan Gohman39946102009-01-25 16:29:12 +00005549 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005550
Dan Gohmane7852d02009-01-26 04:35:06 +00005551 // Sort the uses, so that all the uses from a given User are together.
5552 std::sort(Uses.begin(), Uses.end());
5553
Dan Gohman39946102009-01-25 16:29:12 +00005554 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5555 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005556 // We know that this user uses some value of From. If it is the right
5557 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005558 SDNode *User = Uses[UseIndex].User;
5559
5560 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005561 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005562
Dan Gohmane7852d02009-01-26 04:35:06 +00005563 // The Uses array is sorted, so all the uses for a given User
5564 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005565 // To help reduce the number of CSE recomputations, process all
5566 // the uses of this user that we can find this way.
5567 do {
5568 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005569 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005570 ++UseIndex;
5571
Dan Gohmane7852d02009-01-26 04:35:06 +00005572 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005573 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5574
Dan Gohmane8be6c62008-07-17 19:10:17 +00005575 // Now that we have modified User, add it back to the CSE maps. If it
5576 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005577 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005578 }
5579}
5580
Evan Chenge6f35d82006-08-01 08:20:41 +00005581/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005582/// based on their topological order. It returns the maximum id and a vector
5583/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005584unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005585
Dan Gohmanf06c8352008-09-30 18:30:35 +00005586 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005587
Dan Gohmanf06c8352008-09-30 18:30:35 +00005588 // SortedPos tracks the progress of the algorithm. Nodes before it are
5589 // sorted, nodes after it are unsorted. When the algorithm completes
5590 // it is at the end of the list.
5591 allnodes_iterator SortedPos = allnodes_begin();
5592
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005593 // Visit all the nodes. Move nodes with no operands to the front of
5594 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005595 // operand count. Before we do this, the Node Id fields of the nodes
5596 // may contain arbitrary values. After, the Node Id fields for nodes
5597 // before SortedPos will contain the topological sort index, and the
5598 // Node Id fields for nodes At SortedPos and after will contain the
5599 // count of outstanding operands.
5600 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5601 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005602 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005603 unsigned Degree = N->getNumOperands();
5604 if (Degree == 0) {
5605 // A node with no uses, add it to the result array immediately.
5606 N->setNodeId(DAGSize++);
5607 allnodes_iterator Q = N;
5608 if (Q != SortedPos)
5609 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005610 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005611 ++SortedPos;
5612 } else {
5613 // Temporarily use the Node Id as scratch space for the degree count.
5614 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005615 }
5616 }
5617
Dan Gohmanf06c8352008-09-30 18:30:35 +00005618 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5619 // such that by the time the end is reached all nodes will be sorted.
5620 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5621 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005622 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005623 // N is in sorted position, so all its uses have one less operand
5624 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005625 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5626 UI != UE; ++UI) {
5627 SDNode *P = *UI;
5628 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005629 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005630 --Degree;
5631 if (Degree == 0) {
5632 // All of P's operands are sorted, so P may sorted now.
5633 P->setNodeId(DAGSize++);
5634 if (P != SortedPos)
5635 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005636 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005637 ++SortedPos;
5638 } else {
5639 // Update P's outstanding operand count.
5640 P->setNodeId(Degree);
5641 }
5642 }
David Greene221925e2010-01-20 00:59:23 +00005643 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005644#ifndef NDEBUG
5645 SDNode *S = ++I;
5646 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005647 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005648#endif
5649 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005650 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005651 }
5652
5653 assert(SortedPos == AllNodes.end() &&
5654 "Topological sort incomplete!");
5655 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5656 "First node in topological sort is not the entry token!");
5657 assert(AllNodes.front().getNodeId() == 0 &&
5658 "First node in topological sort has non-zero id!");
5659 assert(AllNodes.front().getNumOperands() == 0 &&
5660 "First node in topological sort has operands!");
5661 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5662 "Last node in topologic sort has unexpected id!");
5663 assert(AllNodes.back().use_empty() &&
5664 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005665 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005666 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005667}
5668
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005669/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005670void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005671 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005672 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005673}
Evan Chenge6f35d82006-08-01 08:20:41 +00005674
Bill Wendling0777e922009-12-21 21:59:52 +00005675/// GetOrdering - Get the order for the SDNode.
5676unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5677 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005678 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005679}
5680
Evan Chengbfcb3052010-03-25 01:38:16 +00005681/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5682/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005683void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5684 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005685 if (SD)
5686 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005687}
Evan Cheng091cba12006-07-27 06:39:06 +00005688
Devang Patela2e868d2011-01-25 23:27:42 +00005689/// TransferDbgValues - Transfer SDDbgValues.
5690void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5691 if (From == To || !From.getNode()->getHasDebugValue())
5692 return;
5693 SDNode *FromNode = From.getNode();
5694 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005695 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005696 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005697 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005698 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005699 SDDbgValue *Dbg = *I;
5700 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5701 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5702 Dbg->getOffset(), Dbg->getDebugLoc(),
5703 Dbg->getOrder());
5704 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005705 }
5706 }
Devang Patela778f5c2011-02-18 22:43:42 +00005707 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5708 E = ClonedDVs.end(); I != E; ++I)
5709 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005710}
5711
Jim Laskey58b968b2005-08-17 20:08:02 +00005712//===----------------------------------------------------------------------===//
5713// SDNode Class
5714//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005715
Chris Lattner48b85922007-02-04 02:41:42 +00005716HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005717 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005718}
5719
Devang Patel0d881da2010-07-06 22:08:15 +00005720GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5721 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005722 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005723 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005724 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005725}
Chris Lattner48b85922007-02-04 02:41:42 +00005726
Owen Andersone50ed302009-08-10 22:56:29 +00005727MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005728 MachineMemOperand *mmo)
5729 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005730 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005731 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005732 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005733 assert(isNonTemporal() == MMO->isNonTemporal() &&
5734 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005735 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005736}
5737
Scott Michelfdc40a02009-02-17 22:15:04 +00005738MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005739 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005740 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005741 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005742 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005743 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005744 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005745 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5746 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005747}
5748
Jim Laskey583bd472006-10-27 23:46:08 +00005749/// Profile - Gather unique data for the node.
5750///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005751void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005752 AddNodeIDNode(ID, this);
5753}
5754
Owen Andersond8110fb2009-08-25 22:27:22 +00005755namespace {
5756 struct EVTArray {
5757 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005758
Owen Andersond8110fb2009-08-25 22:27:22 +00005759 EVTArray() {
5760 VTs.reserve(MVT::LAST_VALUETYPE);
5761 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5762 VTs.push_back(MVT((MVT::SimpleValueType)i));
5763 }
5764 };
5765}
5766
Owen Andersone50ed302009-08-10 22:56:29 +00005767static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005768static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005769static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005770
Chris Lattnera3255112005-11-08 23:30:28 +00005771/// getValueTypeList - Return a pointer to the specified value type.
5772///
Owen Andersone50ed302009-08-10 22:56:29 +00005773const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005774 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005775 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005776 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005777 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005778 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005779 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005780 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005781 }
Chris Lattnera3255112005-11-08 23:30:28 +00005782}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005783
Chris Lattner5c884562005-01-12 18:37:47 +00005784/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5785/// indicated value. This method ignores uses of other values defined by this
5786/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005787bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005788 assert(Value < getNumValues() && "Bad value!");
5789
Roman Levensteindc1adac2008-04-07 10:06:32 +00005790 // TODO: Only iterate over uses of a given value of the node
5791 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005792 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005793 if (NUses == 0)
5794 return false;
5795 --NUses;
5796 }
Chris Lattner5c884562005-01-12 18:37:47 +00005797 }
5798
5799 // Found exactly the right number of uses?
5800 return NUses == 0;
5801}
5802
5803
Evan Cheng33d55952007-08-02 05:29:38 +00005804/// hasAnyUseOfValue - Return true if there are any use of the indicated
5805/// value. This method ignores uses of other values defined by this operation.
5806bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5807 assert(Value < getNumValues() && "Bad value!");
5808
Dan Gohman1373c1c2008-07-09 22:39:01 +00005809 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005810 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005811 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005812
5813 return false;
5814}
5815
5816
Dan Gohman2a629952008-07-27 18:06:42 +00005817/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005818///
Dan Gohman2a629952008-07-27 18:06:42 +00005819bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005820 bool Seen = false;
5821 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005822 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005823 if (User == this)
5824 Seen = true;
5825 else
5826 return false;
5827 }
5828
5829 return Seen;
5830}
5831
Evan Chenge6e97e62006-11-03 07:31:32 +00005832/// isOperand - Return true if this node is an operand of N.
5833///
Dan Gohman475871a2008-07-27 21:46:04 +00005834bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005835 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5836 if (*this == N->getOperand(i))
5837 return true;
5838 return false;
5839}
5840
Evan Cheng917be682008-03-04 00:41:45 +00005841bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005842 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005843 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005844 return true;
5845 return false;
5846}
Evan Cheng4ee62112006-02-05 06:29:23 +00005847
Chris Lattner572dee72008-01-16 05:49:24 +00005848/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005849/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005850/// side-effecting instructions on any chain path. In practice, this looks
5851/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005852/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005853bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005854 unsigned Depth) const {
5855 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005856
Chris Lattner572dee72008-01-16 05:49:24 +00005857 // Don't search too deeply, we just want to be able to see through
5858 // TokenFactor's etc.
5859 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005860
Chris Lattner572dee72008-01-16 05:49:24 +00005861 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005862 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005863 if (getOpcode() == ISD::TokenFactor) {
5864 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005865 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5866 return false;
5867 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005869
Chris Lattner572dee72008-01-16 05:49:24 +00005870 // Loads don't have side effects, look through them.
5871 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5872 if (!Ld->isVolatile())
5873 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5874 }
5875 return false;
5876}
5877
Lang Hames944520f2011-07-07 04:31:51 +00005878/// hasPredecessor - Return true if N is a predecessor of this node.
5879/// N is either an operand of this node, or can be reached by recursively
5880/// traversing up the operands.
5881/// NOTE: This is an expensive method. Use it carefully.
5882bool SDNode::hasPredecessor(const SDNode *N) const {
5883 SmallPtrSet<const SDNode *, 32> Visited;
5884 SmallVector<const SDNode *, 16> Worklist;
5885 return hasPredecessorHelper(N, Visited, Worklist);
5886}
Dan Gohman6688d612009-10-28 03:44:30 +00005887
Lang Hames944520f2011-07-07 04:31:51 +00005888bool SDNode::hasPredecessorHelper(const SDNode *N,
5889 SmallPtrSet<const SDNode *, 32> &Visited,
5890 SmallVector<const SDNode *, 16> &Worklist) const {
5891 if (Visited.empty()) {
5892 Worklist.push_back(this);
5893 } else {
5894 // Take a look in the visited set. If we've already encountered this node
5895 // we needn't search further.
5896 if (Visited.count(N))
5897 return true;
5898 }
5899
5900 // Haven't visited N yet. Continue the search.
5901 while (!Worklist.empty()) {
5902 const SDNode *M = Worklist.pop_back_val();
5903 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
5904 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00005905 if (Visited.insert(Op))
5906 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00005907 if (Op == N)
5908 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00005909 }
Lang Hames944520f2011-07-07 04:31:51 +00005910 }
Dan Gohman6688d612009-10-28 03:44:30 +00005911
5912 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005913}
5914
Evan Chengc5484282006-10-04 00:56:09 +00005915uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5916 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005917 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005918}
5919
Mon P Wangcd6e7252009-11-30 02:42:02 +00005920SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5921 assert(N->getNumValues() == 1 &&
5922 "Can't unroll a vector with multiple results!");
5923
5924 EVT VT = N->getValueType(0);
5925 unsigned NE = VT.getVectorNumElements();
5926 EVT EltVT = VT.getVectorElementType();
5927 DebugLoc dl = N->getDebugLoc();
5928
5929 SmallVector<SDValue, 8> Scalars;
5930 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5931
5932 // If ResNE is 0, fully unroll the vector op.
5933 if (ResNE == 0)
5934 ResNE = NE;
5935 else if (NE > ResNE)
5936 NE = ResNE;
5937
5938 unsigned i;
5939 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00005940 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00005941 SDValue Operand = N->getOperand(j);
5942 EVT OperandVT = Operand.getValueType();
5943 if (OperandVT.isVector()) {
5944 // A vector operand; extract a single element.
5945 EVT OperandEltVT = OperandVT.getVectorElementType();
5946 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5947 OperandEltVT,
5948 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00005949 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005950 } else {
5951 // A scalar operand; just use it as is.
5952 Operands[j] = Operand;
5953 }
5954 }
5955
5956 switch (N->getOpcode()) {
5957 default:
5958 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5959 &Operands[0], Operands.size()));
5960 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00005961 case ISD::VSELECT:
5962 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
5963 &Operands[0], Operands.size()));
5964 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00005965 case ISD::SHL:
5966 case ISD::SRA:
5967 case ISD::SRL:
5968 case ISD::ROTL:
5969 case ISD::ROTR:
5970 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00005971 getShiftAmountOperand(Operands[0].getValueType(),
5972 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005973 break;
Dan Gohmand1996362010-01-09 02:13:55 +00005974 case ISD::SIGN_EXTEND_INREG:
5975 case ISD::FP_ROUND_INREG: {
5976 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
5977 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5978 Operands[0],
5979 getValueType(ExtVT)));
5980 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00005981 }
5982 }
5983
5984 for (; i < ResNE; ++i)
5985 Scalars.push_back(getUNDEF(EltVT));
5986
5987 return getNode(ISD::BUILD_VECTOR, dl,
5988 EVT::getVectorVT(*getContext(), EltVT, ResNE),
5989 &Scalars[0], Scalars.size());
5990}
5991
Evan Cheng64fa4a92009-12-09 01:36:00 +00005992
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005993/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
5994/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00005995/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005996bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00005997 unsigned Bytes, int Dist) const {
5998 if (LD->getChain() != Base->getChain())
5999 return false;
6000 EVT VT = LD->getValueType(0);
6001 if (VT.getSizeInBits() / 8 != Bytes)
6002 return false;
6003
6004 SDValue Loc = LD->getOperand(1);
6005 SDValue BaseLoc = Base->getOperand(1);
6006 if (Loc.getOpcode() == ISD::FrameIndex) {
6007 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6008 return false;
6009 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6010 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6011 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6012 int FS = MFI->getObjectSize(FI);
6013 int BFS = MFI->getObjectSize(BFI);
6014 if (FS != BFS || FS != (int)Bytes) return false;
6015 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6016 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006017
6018 // Handle X+C
6019 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6020 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6021 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006022
Dan Gohman46510a72010-04-15 01:51:59 +00006023 const GlobalValue *GV1 = NULL;
6024 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006025 int64_t Offset1 = 0;
6026 int64_t Offset2 = 0;
6027 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6028 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6029 if (isGA1 && isGA2 && GV1 == GV2)
6030 return Offset1 == (Offset2 + Dist*Bytes);
6031 return false;
6032}
6033
6034
Evan Chengf2dc5c72009-12-09 01:04:59 +00006035/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6036/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006037unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006038 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006039 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006040 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006041 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006042 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
6043 APInt AllOnes = APInt::getAllOnesValue(PtrWidth);
6044 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
6045 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), AllOnes,
6046 KnownZero, KnownOne, TLI.getTargetData());
6047 unsigned AlignBits = KnownZero.countTrailingOnes();
6048 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6049 if (Align)
6050 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006051 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006052
Evan Chengf2dc5c72009-12-09 01:04:59 +00006053 // If this is a direct reference to a stack slot, use information about the
6054 // stack slot's alignment.
6055 int FrameIdx = 1 << 31;
6056 int64_t FrameOffset = 0;
6057 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6058 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006059 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006060 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006061 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006062 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6063 FrameOffset = Ptr.getConstantOperandVal(1);
6064 }
6065
6066 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006067 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006068 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6069 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006070 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006071 }
6072
6073 return 0;
6074}
6075
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006076// getAddressSpace - Return the address space this GlobalAddress belongs to.
6077unsigned GlobalAddressSDNode::getAddressSpace() const {
6078 return getGlobal()->getType()->getAddressSpace();
6079}
6080
6081
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006082Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006083 if (isMachineConstantPoolEntry())
6084 return Val.MachineCPVal->getType();
6085 return Val.ConstVal->getType();
6086}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006087
Bob Wilson24e338e2009-03-02 23:24:16 +00006088bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6089 APInt &SplatUndef,
6090 unsigned &SplatBitSize,
6091 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006092 unsigned MinSplatBits,
6093 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006094 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006095 assert(VT.isVector() && "Expected a vector type");
6096 unsigned sz = VT.getSizeInBits();
6097 if (MinSplatBits > sz)
6098 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006099
Bob Wilson24e338e2009-03-02 23:24:16 +00006100 SplatValue = APInt(sz, 0);
6101 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006102
Bob Wilson24e338e2009-03-02 23:24:16 +00006103 // Get the bits. Bits with undefined values (when the corresponding element
6104 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6105 // in SplatValue. If any of the values are not constant, give up and return
6106 // false.
6107 unsigned int nOps = getNumOperands();
6108 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6109 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006110
6111 for (unsigned j = 0; j < nOps; ++j) {
6112 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006113 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006114 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006115
Bob Wilson24e338e2009-03-02 23:24:16 +00006116 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006117 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006118 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006119 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006120 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006121 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006122 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006123 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006124 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006125 }
6126
Bob Wilson24e338e2009-03-02 23:24:16 +00006127 // The build_vector is all constants or undefs. Find the smallest element
6128 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006129
Bob Wilson24e338e2009-03-02 23:24:16 +00006130 HasAnyUndefs = (SplatUndef != 0);
6131 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006132
Bob Wilson24e338e2009-03-02 23:24:16 +00006133 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006134 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6135 APInt LowValue = SplatValue.trunc(HalfSize);
6136 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6137 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006138
Bob Wilson24e338e2009-03-02 23:24:16 +00006139 // If the two halves do not match (ignoring undef bits), stop here.
6140 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6141 MinSplatBits > HalfSize)
6142 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006143
Bob Wilson24e338e2009-03-02 23:24:16 +00006144 SplatValue = HighValue | LowValue;
6145 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006146
Bob Wilson24e338e2009-03-02 23:24:16 +00006147 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006148 }
6149
Bob Wilson24e338e2009-03-02 23:24:16 +00006150 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006151 return true;
6152}
Nate Begeman9008ca62009-04-27 18:41:29 +00006153
Owen Andersone50ed302009-08-10 22:56:29 +00006154bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006155 // Find the first non-undef value in the shuffle mask.
6156 unsigned i, e;
6157 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6158 /* search */;
6159
Nate Begemana6415752009-04-29 18:13:31 +00006160 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006161
Nate Begeman5a5ca152009-04-29 05:20:52 +00006162 // Make sure all remaining elements are either undef or the same as the first
6163 // non-undef value.
6164 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006165 if (Mask[i] >= 0 && Mask[i] != Idx)
6166 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006167 return true;
6168}
David Greenecf495bc2010-01-20 20:13:31 +00006169
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006170#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006171static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006172 SmallPtrSet<const SDNode*, 32> &Visited,
6173 SmallPtrSet<const SDNode*, 32> &Checked) {
6174 // If this node has already been checked, don't check it again.
6175 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006176 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006177
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006178 // If a node has already been visited on this depth-first walk, reject it as
6179 // a cycle.
6180 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006181 dbgs() << "Offending node:\n";
6182 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006183 errs() << "Detected cycle in SelectionDAG\n";
6184 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006185 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006186
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006187 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6188 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006189
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006190 Checked.insert(N);
6191 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006192}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006193#endif
David Greenecf495bc2010-01-20 20:13:31 +00006194
6195void llvm::checkForCycles(const llvm::SDNode *N) {
6196#ifdef XDEBUG
6197 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006198 SmallPtrSet<const SDNode*, 32> visited;
6199 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006200 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006201#endif
6202}
6203
6204void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6205 checkForCycles(DAG->getRoot().getNode());
6206}