blob: 305eee81cd97443599612d42ed550cde52ac1452 [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;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001630 ComputeMaskedBits(Op, 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.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001639void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1640 APInt &KnownOne, unsigned Depth) const {
1641 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001642
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001643 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001644 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001645 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001646
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001647 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001648
1649 switch (Op.getOpcode()) {
1650 case ISD::Constant:
1651 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001652 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1653 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001654 return;
1655 case ISD::AND:
1656 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001657 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1658 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001659 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1660 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001661
1662 // Output known-1 bits are only known if set in both the LHS & RHS.
1663 KnownOne &= KnownOne2;
1664 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1665 KnownZero |= KnownZero2;
1666 return;
1667 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001668 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1669 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001670 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1671 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1672
Dan Gohmanea859be2007-06-22 14:59:07 +00001673 // Output known-0 bits are only known if clear in both the LHS & RHS.
1674 KnownZero &= KnownZero2;
1675 // Output known-1 are known to be set if set in either the LHS | RHS.
1676 KnownOne |= KnownOne2;
1677 return;
1678 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001679 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1680 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001681 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1682 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1683
Dan Gohmanea859be2007-06-22 14:59:07 +00001684 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001685 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001686 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1687 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1688 KnownZero = KnownZeroOut;
1689 return;
1690 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001691 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001692 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1693 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001694 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1695 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1696
1697 // If low bits are zero in either operand, output low known-0 bits.
1698 // Also compute a conserative estimate for high known-0 bits.
1699 // More trickiness is possible, but this is sufficient for the
1700 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001701 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001702 unsigned TrailZ = KnownZero.countTrailingOnes() +
1703 KnownZero2.countTrailingOnes();
1704 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001705 KnownZero2.countLeadingOnes(),
1706 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001707
1708 TrailZ = std::min(TrailZ, BitWidth);
1709 LeadZ = std::min(LeadZ, BitWidth);
1710 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1711 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001712 return;
1713 }
1714 case ISD::UDIV: {
1715 // For the purposes of computing leading zeros we can conservatively
1716 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001717 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001718 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001719 unsigned LeadZ = KnownZero2.countLeadingOnes();
1720
Jay Foad7a874dd2010-12-01 08:53:58 +00001721 KnownOne2.clearAllBits();
1722 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001723 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001724 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1725 if (RHSUnknownLeadingOnes != BitWidth)
1726 LeadZ = std::min(BitWidth,
1727 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001728
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001729 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001730 return;
1731 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001732 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001733 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1734 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001735 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1736 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1737
Dan Gohmanea859be2007-06-22 14:59:07 +00001738 // Only known if known in both the LHS and RHS.
1739 KnownOne &= KnownOne2;
1740 KnownZero &= KnownZero2;
1741 return;
1742 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001743 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1744 ComputeMaskedBits(Op.getOperand(2), 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;
Bill Wendling253174b2008-11-22 07:24:01 +00001752 case ISD::SADDO:
1753 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001754 case ISD::SSUBO:
1755 case ISD::USUBO:
1756 case ISD::SMULO:
1757 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001758 if (Op.getResNo() != 1)
1759 return;
Duncan Sands03228082008-11-23 15:47:28 +00001760 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001761 case ISD::SETCC:
1762 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001763 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1764 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001765 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001766 return;
1767 case ISD::SHL:
1768 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1769 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001770 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001771
1772 // If the shift count is an invalid immediate, don't do anything.
1773 if (ShAmt >= BitWidth)
1774 return;
1775
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001776 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001777 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001778 KnownZero <<= ShAmt;
1779 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001780 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001781 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001782 }
1783 return;
1784 case ISD::SRL:
1785 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1786 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001787 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001788
Dan Gohmand4cf9922008-02-26 18:50:50 +00001789 // If the shift count is an invalid immediate, don't do anything.
1790 if (ShAmt >= BitWidth)
1791 return;
1792
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001793 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001794 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001795 KnownZero = KnownZero.lshr(ShAmt);
1796 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001797
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001798 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001799 KnownZero |= HighBits; // High bits known zero.
1800 }
1801 return;
1802 case ISD::SRA:
1803 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001804 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001805
Dan Gohmand4cf9922008-02-26 18:50:50 +00001806 // If the shift count is an invalid immediate, don't do anything.
1807 if (ShAmt >= BitWidth)
1808 return;
1809
Dan Gohmanea859be2007-06-22 14:59:07 +00001810 // If any of the demanded bits are produced by the sign extension, we also
1811 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001812 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001813
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001814 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001815 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001816 KnownZero = KnownZero.lshr(ShAmt);
1817 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001818
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001820 APInt SignBit = APInt::getSignBit(BitWidth);
1821 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001822
Dan Gohmanca93a432008-02-20 16:30:17 +00001823 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001824 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001825 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001826 KnownOne |= HighBits; // New bits are known one.
1827 }
1828 }
1829 return;
1830 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001831 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001832 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001833
1834 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001836 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001837
Dan Gohman977a76f2008-02-13 22:28:48 +00001838 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001839 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 // If the sign extended bits are demanded, we know that the sign
1842 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001843 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001844 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001845 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001846
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001847 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1848 KnownOne &= InputDemandedBits;
1849 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001850 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1851
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 // If the sign bit of the input is known set or clear, then we know the
1853 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001854 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001855 KnownZero |= NewBits;
1856 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001857 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001858 KnownOne |= NewBits;
1859 KnownZero &= ~NewBits;
1860 } else { // Input sign bit unknown
1861 KnownZero &= ~NewBits;
1862 KnownOne &= ~NewBits;
1863 }
1864 return;
1865 }
1866 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001867 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001868 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001869 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001870 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001871 unsigned LowBits = Log2_32(BitWidth)+1;
1872 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001873 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001874 return;
1875 }
1876 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001877 LoadSDNode *LD = cast<LoadSDNode>(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 if (ISD::isZEXTLoad(Op.getNode())) {
Owen Andersone50ed302009-08-10 22:56:29 +00001879 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001880 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001881 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001882 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001883 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001884 }
1885 return;
1886 }
1887 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001888 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001889 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001890 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001891 KnownZero = KnownZero.trunc(InBits);
1892 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001893 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001894 KnownZero = KnownZero.zext(BitWidth);
1895 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001896 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 return;
1898 }
1899 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001900 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001901 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001902 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001903 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001904
Jay Foad40f8f622010-12-07 08:25:19 +00001905 KnownZero = KnownZero.trunc(InBits);
1906 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001907 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001908
1909 // Note if the sign bit is known to be zero or one.
1910 bool SignBitKnownZero = KnownZero.isNegative();
1911 bool SignBitKnownOne = KnownOne.isNegative();
1912 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1913 "Sign bit can't be known to be both zero and one!");
1914
Jay Foad40f8f622010-12-07 08:25:19 +00001915 KnownZero = KnownZero.zext(BitWidth);
1916 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001917
Dan Gohman977a76f2008-02-13 22:28:48 +00001918 // If the sign bit is known zero or one, the top bits match.
1919 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001920 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001921 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001922 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001923 return;
1924 }
1925 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001926 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001927 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001928 KnownZero = KnownZero.trunc(InBits);
1929 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001930 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001931 KnownZero = KnownZero.zext(BitWidth);
1932 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001933 return;
1934 }
1935 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001937 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001938 KnownZero = KnownZero.zext(InBits);
1939 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001940 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001941 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001942 KnownZero = KnownZero.trunc(BitWidth);
1943 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001944 break;
1945 }
1946 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001947 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001948 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001949 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1950 KnownZero |= (~InMask);
Dan Gohmanea859be2007-06-22 14:59:07 +00001951 return;
1952 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001953 case ISD::FGETSIGN:
1954 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001955 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001956 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Dan Gohman23e8b712008-04-28 17:02:21 +00001958 case ISD::SUB: {
1959 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1960 // We know that the top bits of C-X are clear if X contains less bits
1961 // than C (i.e. no wrap-around can happen). For example, 20-X is
1962 // positive if we can prove that X is >= 0 and < 16.
1963 if (CLHS->getAPIntValue().isNonNegative()) {
1964 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1965 // NLZ can't be BitWidth with no sign bit
1966 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001967 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001968
1969 // If all of the MaskV bits are known to be zero, then we know the
1970 // output top bits are zero, because we now know that the output is
1971 // from [0-C].
1972 if ((KnownZero2 & MaskV) == MaskV) {
1973 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1974 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001975 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00001976 }
1977 }
1978 }
1979 }
1980 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00001981 case ISD::ADD:
1982 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001983 // Output known-0 bits are known if clear or set in both the low clear bits
1984 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1985 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001986 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001987 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001988 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1989
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001990 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001991 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001992 KnownZeroOut = std::min(KnownZeroOut,
1993 KnownZero2.countTrailingOnes());
1994
Chris Lattnerda605882010-12-19 20:38:28 +00001995 if (Op.getOpcode() == ISD::ADD) {
1996 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
1997 return;
1998 }
1999
2000 // With ADDE, a carry bit may be added in, so we can only use this
2001 // information if we know (at least) that the low two bits are clear. We
2002 // then return to the caller that the low bit is unknown but that other bits
2003 // are known zero.
2004 if (KnownZeroOut >= 2) // ADDE
2005 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002006 return;
2007 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002008 case ISD::SREM:
2009 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002010 const APInt &RA = Rem->getAPIntValue().abs();
2011 if (RA.isPowerOf2()) {
2012 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002013 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002014 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002015
Duncan Sands5c2873a2010-01-29 09:45:26 +00002016 // The low bits of the first operand are unchanged by the srem.
2017 KnownZero = KnownZero2 & LowBits;
2018 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002019
Duncan Sands5c2873a2010-01-29 09:45:26 +00002020 // If the first operand is non-negative or has all low bits zero, then
2021 // the upper bits are all zero.
2022 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2023 KnownZero |= ~LowBits;
2024
2025 // If the first operand is negative and not all low bits are zero, then
2026 // the upper bits are all one.
2027 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2028 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002029 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002030 }
2031 }
2032 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002033 case ISD::UREM: {
2034 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002035 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002036 if (RA.isPowerOf2()) {
2037 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002038 KnownZero |= ~LowBits;
2039 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002040 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2041 break;
2042 }
2043 }
2044
2045 // Since the result is less than or equal to either operand, any leading
2046 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002047 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2048 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002049
2050 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2051 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002052 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002053 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002054 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002055 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002056 case ISD::FrameIndex:
2057 case ISD::TargetFrameIndex:
2058 if (unsigned Align = InferPtrAlignment(Op)) {
2059 // The low bits are known zero if the pointer is aligned.
2060 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2061 return;
2062 }
2063 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002064
Dan Gohmanea859be2007-06-22 14:59:07 +00002065 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002066 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2067 break;
2068 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002069 case ISD::INTRINSIC_WO_CHAIN:
2070 case ISD::INTRINSIC_W_CHAIN:
2071 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002072 // Allow the target to implement this method for its nodes.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002073 TLI.computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002074 return;
2075 }
2076}
2077
2078/// ComputeNumSignBits - Return the number of times the sign bit of the
2079/// register is replicated into the other bits. We know that at least 1 bit
2080/// is always equal to the sign bit (itself), but other cases can give us
2081/// information. For example, immediately after an "SRA X, 2", we know that
2082/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002083unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002084 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002085 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002086 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002087 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002088 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002089
Dan Gohmanea859be2007-06-22 14:59:07 +00002090 if (Depth == 6)
2091 return 1; // Limit search depth.
2092
2093 switch (Op.getOpcode()) {
2094 default: break;
2095 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002097 return VTBits-Tmp+1;
2098 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002099 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002100 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002101
Dan Gohmanea859be2007-06-22 14:59:07 +00002102 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002103 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002104 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002106
Dan Gohmanea859be2007-06-22 14:59:07 +00002107 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002108 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002109 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002110
Dan Gohmanea859be2007-06-22 14:59:07 +00002111 case ISD::SIGN_EXTEND_INREG:
2112 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002113 Tmp =
2114 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002115 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002116
Dan Gohmanea859be2007-06-22 14:59:07 +00002117 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2118 return std::max(Tmp, Tmp2);
2119
2120 case ISD::SRA:
2121 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2122 // SRA X, C -> adds C sign bits.
2123 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002124 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002125 if (Tmp > VTBits) Tmp = VTBits;
2126 }
2127 return Tmp;
2128 case ISD::SHL:
2129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2130 // shl destroys sign bits.
2131 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002132 if (C->getZExtValue() >= VTBits || // Bad shift.
2133 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2134 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002135 }
2136 break;
2137 case ISD::AND:
2138 case ISD::OR:
2139 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002140 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002142 if (Tmp != 1) {
2143 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2144 FirstAnswer = std::min(Tmp, Tmp2);
2145 // We computed what we know about the sign bits as our first
2146 // answer. Now proceed to the generic code that uses
2147 // ComputeMaskedBits, and pick whichever answer is better.
2148 }
2149 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002150
2151 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002152 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002153 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002154 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002156
2157 case ISD::SADDO:
2158 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002159 case ISD::SSUBO:
2160 case ISD::USUBO:
2161 case ISD::SMULO:
2162 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002163 if (Op.getResNo() != 1)
2164 break;
Duncan Sands03228082008-11-23 15:47:28 +00002165 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002166 case ISD::SETCC:
2167 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002168 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002169 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002170 return VTBits;
2171 break;
2172 case ISD::ROTL:
2173 case ISD::ROTR:
2174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002175 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002176
Dan Gohmanea859be2007-06-22 14:59:07 +00002177 // Handle rotate right by N like a rotate left by 32-N.
2178 if (Op.getOpcode() == ISD::ROTR)
2179 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2180
2181 // If we aren't rotating out all of the known-in sign bits, return the
2182 // number that are left. This handles rotl(sext(x), 1) for example.
2183 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2184 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2185 }
2186 break;
2187 case ISD::ADD:
2188 // Add can have at most one carry bit. Thus we know that the output
2189 // is, at worst, one more bit than the inputs.
2190 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2191 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002192
Dan Gohmanea859be2007-06-22 14:59:07 +00002193 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002194 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002195 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002196 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002197 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002198
Dan Gohmanea859be2007-06-22 14:59:07 +00002199 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2200 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002201 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002202 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002203
Dan Gohmanea859be2007-06-22 14:59:07 +00002204 // If we are subtracting one from a positive number, there is no carry
2205 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002206 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002207 return Tmp;
2208 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002209
Dan Gohmanea859be2007-06-22 14:59:07 +00002210 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2211 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002212 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002213
Dan Gohmanea859be2007-06-22 14:59:07 +00002214 case ISD::SUB:
2215 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2216 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002217
Dan Gohmanea859be2007-06-22 14:59:07 +00002218 // Handle NEG.
2219 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002220 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002221 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002222 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002223 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2224 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002225 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002226 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Dan Gohmanea859be2007-06-22 14:59:07 +00002228 // If the input is known to be positive (the sign bit is known clear),
2229 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002230 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002231 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002232
Dan Gohmanea859be2007-06-22 14:59:07 +00002233 // Otherwise, we treat this like a SUB.
2234 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002235
Dan Gohmanea859be2007-06-22 14:59:07 +00002236 // Sub can have at most one carry bit. Thus we know that the output
2237 // is, at worst, one more bit than the inputs.
2238 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2239 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002240 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002241 case ISD::TRUNCATE:
2242 // FIXME: it's tricky to do anything useful for this, but it is an important
2243 // case for targets like X86.
2244 break;
2245 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002246
Dan Gohmanea859be2007-06-22 14:59:07 +00002247 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2248 if (Op.getOpcode() == ISD::LOAD) {
2249 LoadSDNode *LD = cast<LoadSDNode>(Op);
2250 unsigned ExtType = LD->getExtensionType();
2251 switch (ExtType) {
2252 default: break;
2253 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002254 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002255 return VTBits-Tmp+1;
2256 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002257 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 return VTBits-Tmp;
2259 }
2260 }
2261
2262 // Allow the target to implement this method for its nodes.
2263 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002265 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2266 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2267 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002268 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002269 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002270
Dan Gohmanea859be2007-06-22 14:59:07 +00002271 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2272 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002273 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002274 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002275
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002276 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002277 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002278 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002279 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002280 Mask = KnownOne;
2281 } else {
2282 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002283 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002284 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002285
Dan Gohmanea859be2007-06-22 14:59:07 +00002286 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2287 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002288 Mask = ~Mask;
2289 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002290 // Return # leading zeros. We use 'min' here in case Val was zero before
2291 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002292 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002293}
2294
Chris Lattner0a9481f2011-02-13 22:25:43 +00002295/// isBaseWithConstantOffset - Return true if the specified operand is an
2296/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2297/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2298/// semantics as an ADD. This handles the equivalence:
Chris Lattner463b3c22011-02-14 06:14:42 +00002299/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002300bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2301 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2302 !isa<ConstantSDNode>(Op.getOperand(1)))
2303 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002304
2305 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002306 !MaskedValueIsZero(Op.getOperand(0),
2307 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2308 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002309
Chris Lattner0a9481f2011-02-13 22:25:43 +00002310 return true;
2311}
2312
2313
Dan Gohman8d44b282009-09-03 20:34:31 +00002314bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2315 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002316 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002317 return true;
2318
2319 // If the value is a constant, we can obviously see if it is a NaN or not.
2320 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2321 return !C->getValueAPF().isNaN();
2322
2323 // TODO: Recognize more cases here.
2324
2325 return false;
2326}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002327
Dan Gohmane8326932010-02-24 06:52:40 +00002328bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2329 // If the value is a constant, we can obviously see if it is a zero or not.
2330 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2331 return !C->isZero();
2332
2333 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002334 switch (Op.getOpcode()) {
2335 default: break;
2336 case ISD::OR:
2337 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2338 return !C->isNullValue();
2339 break;
2340 }
Dan Gohmane8326932010-02-24 06:52:40 +00002341
2342 return false;
2343}
2344
2345bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2346 // Check the obvious case.
2347 if (A == B) return true;
2348
2349 // For for negative and positive zero.
2350 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2351 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2352 if (CA->isZero() && CB->isZero()) return true;
2353
2354 // Otherwise they may not be equal.
2355 return false;
2356}
2357
Chris Lattnerc3aae252005-01-07 07:46:32 +00002358/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002359///
Owen Andersone50ed302009-08-10 22:56:29 +00002360SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002361 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002362 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002363 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002364 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002365 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002366
Dan Gohman95531882010-03-18 18:49:47 +00002367 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002368 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002369
Chris Lattner4a283e92006-08-11 18:38:11 +00002370 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002371#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002372 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002373#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002374 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002375}
2376
Bill Wendling7ade28c2009-01-28 22:17:52 +00002377SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002378 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002379 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002380 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002381 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002382 switch (Opcode) {
2383 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002384 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002385 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002386 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002387 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002388 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002389 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002390 case ISD::UINT_TO_FP:
2391 case ISD::SINT_TO_FP: {
Chris Lattner7fb5c2d2010-03-15 16:05:15 +00002392 // No compile time operations on ppcf128.
2393 if (VT == MVT::ppcf128) break;
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002394 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002395 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002396 Opcode==ISD::SINT_TO_FP,
2397 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002398 return getConstantFP(apf, VT);
2399 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002400 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002401 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002402 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002403 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002404 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002405 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002406 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002407 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002408 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002409 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002410 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002411 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002412 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002413 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002414 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002415 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002416 }
2417 }
2418
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002419 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002420 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002421 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002422 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002423 switch (Opcode) {
2424 case ISD::FNEG:
2425 V.changeSign();
2426 return getConstantFP(V, VT);
2427 case ISD::FABS:
2428 V.clearSign();
2429 return getConstantFP(V, VT);
2430 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002431 case ISD::FP_EXTEND: {
2432 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002433 // This can return overflow, underflow, or inexact; we don't care.
2434 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002435 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002436 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002437 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002438 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002439 case ISD::FP_TO_SINT:
2440 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002441 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002442 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002443 assert(integerPartWidth >= 64);
2444 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002445 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002446 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002447 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002448 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2449 break;
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00002450 APInt api(VT.getSizeInBits(), x);
Dale Johannesen2f91f302009-04-24 21:34:13 +00002451 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002452 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002453 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002455 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002457 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002458 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002459 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002460 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002461 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002462
Gabor Greifba36cb52008-08-28 21:40:38 +00002463 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002464 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002465 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002466 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002467 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002468 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002469 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002470 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002471 assert(VT.isFloatingPoint() &&
2472 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002473 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002474 assert((!VT.isVector() ||
2475 VT.getVectorNumElements() ==
2476 Operand.getValueType().getVectorNumElements()) &&
2477 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002478 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002479 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002480 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002481 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002482 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002483 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002485 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2486 "Invalid sext node, dst < src!");
2487 assert((!VT.isVector() ||
2488 VT.getVectorNumElements() ==
2489 Operand.getValueType().getVectorNumElements()) &&
2490 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002491 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002492 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002493 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002494 // sext(undef) = 0, because the top bits will all be the same.
2495 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002496 break;
2497 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002498 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002499 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002501 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2502 "Invalid zext node, dst < src!");
2503 assert((!VT.isVector() ||
2504 VT.getVectorNumElements() ==
2505 Operand.getValueType().getVectorNumElements()) &&
2506 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002507 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002508 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002509 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002510 else if (OpOpcode == ISD::UNDEF)
2511 // zext(undef) = 0, because the top bits will be zero.
2512 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002513 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002514 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002515 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002516 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002517 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002518 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2519 "Invalid anyext node, dst < src!");
2520 assert((!VT.isVector() ||
2521 VT.getVectorNumElements() ==
2522 Operand.getValueType().getVectorNumElements()) &&
2523 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002524
Dan Gohman9e86a732010-06-18 00:08:30 +00002525 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2526 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002527 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002528 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002529 else if (OpOpcode == ISD::UNDEF)
2530 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002531
2532 // (ext (trunx x)) -> x
2533 if (OpOpcode == ISD::TRUNCATE) {
2534 SDValue OpOp = Operand.getNode()->getOperand(0);
2535 if (OpOp.getValueType() == VT)
2536 return OpOp;
2537 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002538 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002539 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002540 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002541 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002542 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002543 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2544 "Invalid truncate node, src < dst!");
2545 assert((!VT.isVector() ||
2546 VT.getVectorNumElements() ==
2547 Operand.getValueType().getVectorNumElements()) &&
2548 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002549 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002550 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002551 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2552 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002553 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002554 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2555 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002556 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002557 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002558 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002559 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002560 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002561 if (OpOpcode == ISD::UNDEF)
2562 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002563 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002564 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002565 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002566 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002567 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002568 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002569 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2570 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002571 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002572 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002573 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002574 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002576 (VT.getVectorElementType() == Operand.getValueType() ||
2577 (VT.getVectorElementType().isInteger() &&
2578 Operand.getValueType().isInteger() &&
2579 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002580 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002581 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002582 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002583 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2584 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2585 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2586 Operand.getConstantOperandVal(1) == 0 &&
2587 Operand.getOperand(0).getValueType() == VT)
2588 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002589 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002590 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002591 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002592 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002593 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002594 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002595 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002596 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002597 break;
2598 case ISD::FABS:
2599 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002600 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002601 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602 }
2603
Chris Lattner43247a12005-08-25 19:12:10 +00002604 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002605 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002606 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002607 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002608 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002609 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002610 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002611 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002612 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002613
Dan Gohman95531882010-03-18 18:49:47 +00002614 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002615 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002616 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002617 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002618 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002619
Chris Lattnerc3aae252005-01-07 07:46:32 +00002620 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002621#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002622 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002623#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002624 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002625}
2626
Bill Wendling688d1c42008-09-24 10:16:24 +00002627SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002628 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002629 ConstantSDNode *Cst1,
2630 ConstantSDNode *Cst2) {
2631 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2632
2633 switch (Opcode) {
2634 case ISD::ADD: return getConstant(C1 + C2, VT);
2635 case ISD::SUB: return getConstant(C1 - C2, VT);
2636 case ISD::MUL: return getConstant(C1 * C2, VT);
2637 case ISD::UDIV:
2638 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2639 break;
2640 case ISD::UREM:
2641 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2642 break;
2643 case ISD::SDIV:
2644 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2645 break;
2646 case ISD::SREM:
2647 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2648 break;
2649 case ISD::AND: return getConstant(C1 & C2, VT);
2650 case ISD::OR: return getConstant(C1 | C2, VT);
2651 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2652 case ISD::SHL: return getConstant(C1 << C2, VT);
2653 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2654 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2655 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2656 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2657 default: break;
2658 }
2659
2660 return SDValue();
2661}
2662
Owen Andersone50ed302009-08-10 22:56:29 +00002663SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002664 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002665 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2666 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002667 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002668 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002669 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2671 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002672 // Fold trivial token factors.
2673 if (N1.getOpcode() == ISD::EntryToken) return N2;
2674 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002675 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002676 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002677 case ISD::CONCAT_VECTORS:
2678 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2679 // one big BUILD_VECTOR.
2680 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2681 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002682 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2683 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002684 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002685 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002686 }
2687 break;
Chris Lattner76365122005-01-16 02:23:22 +00002688 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002689 assert(VT.isInteger() && "This operator does not apply to FP types!");
2690 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002691 N1.getValueType() == VT && "Binary operator types must match!");
2692 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2693 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002694 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002695 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002696 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2697 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002698 break;
Chris Lattner76365122005-01-16 02:23:22 +00002699 case ISD::OR:
2700 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002701 case ISD::ADD:
2702 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002703 assert(VT.isInteger() && "This operator does not apply to FP types!");
2704 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002705 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002706 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2707 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002708 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002709 return N1;
2710 break;
Chris Lattner76365122005-01-16 02:23:22 +00002711 case ISD::UDIV:
2712 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002713 case ISD::MULHU:
2714 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002715 case ISD::MUL:
2716 case ISD::SDIV:
2717 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002718 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002719 assert(N1.getValueType() == N2.getValueType() &&
2720 N1.getValueType() == VT && "Binary operator types must match!");
2721 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002722 case ISD::FADD:
2723 case ISD::FSUB:
2724 case ISD::FMUL:
2725 case ISD::FDIV:
2726 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002727 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002728 if (Opcode == ISD::FADD) {
2729 // 0+x --> x
2730 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2731 if (CFP->getValueAPF().isZero())
2732 return N2;
2733 // x+0 --> x
2734 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2735 if (CFP->getValueAPF().isZero())
2736 return N1;
2737 } else if (Opcode == ISD::FSUB) {
2738 // x-0 --> x
2739 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2740 if (CFP->getValueAPF().isZero())
2741 return N1;
2742 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002743 }
Dale Johannesen78995512010-05-15 18:38:02 +00002744 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002745 assert(N1.getValueType() == N2.getValueType() &&
2746 N1.getValueType() == VT && "Binary operator types must match!");
2747 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002748 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2749 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002750 N1.getValueType().isFloatingPoint() &&
2751 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002752 "Invalid FCOPYSIGN!");
2753 break;
Chris Lattner76365122005-01-16 02:23:22 +00002754 case ISD::SHL:
2755 case ISD::SRA:
2756 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002757 case ISD::ROTL:
2758 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002759 assert(VT == N1.getValueType() &&
2760 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002761 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002762 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002763 // Verify that the shift amount VT is bit enough to hold valid shift
2764 // amounts. This catches things like trying to shift an i1024 value by an
2765 // i8, which is easy to fall into in generic code that uses
2766 // TLI.getShiftAmount().
2767 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002768 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002769 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002770
2771 // Always fold shifts of i1 values so the code generator doesn't need to
2772 // handle them. Since we know the size of the shift has to be less than the
2773 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002774 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002775 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002776 if (N2C && N2C->isNullValue())
2777 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002778 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002779 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002780 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002781 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002782 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002783 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002784 assert(EVT.isVector() == VT.isVector() &&
2785 "FP_ROUND_INREG type should be vector iff the operand "
2786 "type is vector!");
2787 assert((!EVT.isVector() ||
2788 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2789 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002790 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002791 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002792 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002793 break;
2794 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002795 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002796 assert(VT.isFloatingPoint() &&
2797 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002798 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002799 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002800 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002801 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002802 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002803 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002804 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002805 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002806 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002807 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002808 assert(!EVT.isVector() &&
2809 "AssertSExt/AssertZExt type should be the vector element type "
2810 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002811 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002812 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002813 break;
2814 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002815 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002816 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002817 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002818 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002819 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002820 assert(EVT.isVector() == VT.isVector() &&
2821 "SIGN_EXTEND_INREG type should be vector iff the operand "
2822 "type is vector!");
2823 assert((!EVT.isVector() ||
2824 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2825 "Vector element counts must match in SIGN_EXTEND_INREG");
2826 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002827 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002828
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002829 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002830 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002831 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002832 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002833 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002834 return getConstant(Val, VT);
2835 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002836 break;
2837 }
2838 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002839 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2840 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002841 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002842
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002843 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2844 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002845 if (N2C &&
2846 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002847 N1.getNumOperands() > 0) {
2848 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002849 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002850 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002851 N1.getOperand(N2C->getZExtValue() / Factor),
2852 getConstant(N2C->getZExtValue() % Factor,
2853 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002854 }
2855
2856 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2857 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002858 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2859 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002860 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002861 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002862 // If the vector element type is not legal, the BUILD_VECTOR operands
2863 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002864 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2865 }
2866 if (VT != VEltTy) {
2867 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2868 // result is implicitly extended.
2869 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002870 }
2871 return Elt;
2872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002873
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002874 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2875 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002876 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002877 // If the indices are the same, return the inserted element else
2878 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002879 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002880 SDValue N1Op2 = N1.getOperand(2);
2881 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2882
2883 if (N1Op2C && N2C) {
2884 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2885 if (VT == N1.getOperand(1).getValueType())
2886 return N1.getOperand(1);
2887 else
2888 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2889 }
2890
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002891 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002892 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002893 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002894 break;
2895 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002896 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002897 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2898 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002899 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002900 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002901
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002902 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2903 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002904 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002905 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002906 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002907
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002908 // EXTRACT_ELEMENT of a constant int is also very common.
2909 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002910 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002911 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002912 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2913 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002914 }
2915 break;
David Greene91585092011-01-26 15:38:49 +00002916 case ISD::EXTRACT_SUBVECTOR: {
2917 SDValue Index = N2;
2918 if (VT.isSimple() && N1.getValueType().isSimple()) {
2919 assert(VT.isVector() && N1.getValueType().isVector() &&
2920 "Extract subvector VTs must be a vectors!");
2921 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
2922 "Extract subvector VTs must have the same element type!");
2923 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
2924 "Extract subvector must be from larger vector to smaller vector!");
2925
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00002926 if (isa<ConstantSDNode>(Index.getNode())) {
2927 assert((VT.getVectorNumElements() +
2928 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00002929 <= N1.getValueType().getVectorNumElements())
2930 && "Extract subvector overflow!");
2931 }
2932
2933 // Trivial extraction.
2934 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
2935 return N1;
2936 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002937 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002938 }
David Greene91585092011-01-26 15:38:49 +00002939 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002940
2941 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002942 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002943 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2944 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002945 } else { // Cannonicalize constant to RHS if commutative
2946 if (isCommutativeBinOp(Opcode)) {
2947 std::swap(N1C, N2C);
2948 std::swap(N1, N2);
2949 }
2950 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002951 }
2952
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002953 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002954 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2955 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002956 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002957 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2958 // Cannonicalize constant to RHS if commutative
2959 std::swap(N1CFP, N2CFP);
2960 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002961 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002962 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2963 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002964 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002965 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002966 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002967 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002968 return getConstantFP(V1, VT);
2969 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002970 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002971 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2972 if (s!=APFloat::opInvalidOp)
2973 return getConstantFP(V1, VT);
2974 break;
2975 case ISD::FMUL:
2976 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2977 if (s!=APFloat::opInvalidOp)
2978 return getConstantFP(V1, VT);
2979 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002980 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002981 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2982 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2983 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002984 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002985 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002986 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2987 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2988 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002989 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002990 case ISD::FCOPYSIGN:
2991 V1.copySign(V2);
2992 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002993 default: break;
2994 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002995 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002997
Chris Lattner62b57722006-04-20 05:39:12 +00002998 // Canonicalize an UNDEF to the RHS, even over a constant.
2999 if (N1.getOpcode() == ISD::UNDEF) {
3000 if (isCommutativeBinOp(Opcode)) {
3001 std::swap(N1, N2);
3002 } else {
3003 switch (Opcode) {
3004 case ISD::FP_ROUND_INREG:
3005 case ISD::SIGN_EXTEND_INREG:
3006 case ISD::SUB:
3007 case ISD::FSUB:
3008 case ISD::FDIV:
3009 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003010 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003011 return N1; // fold op(undef, arg2) -> undef
3012 case ISD::UDIV:
3013 case ISD::SDIV:
3014 case ISD::UREM:
3015 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003016 case ISD::SRL:
3017 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003018 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003019 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3020 // For vectors, we can't easily build an all zero vector, just return
3021 // the LHS.
3022 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003023 }
3024 }
3025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003026
3027 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003028 if (N2.getOpcode() == ISD::UNDEF) {
3029 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003030 case ISD::XOR:
3031 if (N1.getOpcode() == ISD::UNDEF)
3032 // Handle undef ^ undef -> 0 special case. This is a common
3033 // idiom (misuse).
3034 return getConstant(0, VT);
3035 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003036 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003037 case ISD::ADDC:
3038 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003039 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003040 case ISD::UDIV:
3041 case ISD::SDIV:
3042 case ISD::UREM:
3043 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003044 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003045 case ISD::FADD:
3046 case ISD::FSUB:
3047 case ISD::FMUL:
3048 case ISD::FDIV:
3049 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003050 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003051 return N2;
3052 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003053 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003054 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003055 case ISD::SRL:
3056 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003057 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003058 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3059 // For vectors, we can't easily build an all zero vector, just return
3060 // the LHS.
3061 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003062 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003064 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003065 // For vectors, we can't easily build an all one vector, just return
3066 // the LHS.
3067 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003068 case ISD::SRA:
3069 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003070 }
3071 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003072
Chris Lattner27e9b412005-05-11 18:57:39 +00003073 // Memoize this node if possible.
3074 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003075 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003076 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003078 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003079 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003080 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003081 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003082 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003083
Dan Gohman95531882010-03-18 18:49:47 +00003084 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003085 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003086 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003087 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003088 }
3089
Chris Lattnerc3aae252005-01-07 07:46:32 +00003090 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003091#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003092 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003093#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003094 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003095}
3096
Owen Andersone50ed302009-08-10 22:56:29 +00003097SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003098 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003099 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003100 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003101 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003102 case ISD::CONCAT_VECTORS:
3103 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3104 // one big BUILD_VECTOR.
3105 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3106 N2.getOpcode() == ISD::BUILD_VECTOR &&
3107 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003108 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3109 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003110 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3111 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003112 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003113 }
3114 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003115 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003116 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003117 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003118 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003119 break;
3120 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003121 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003122 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003123 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003124 return N2; // select true, X, Y -> X
3125 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003126 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003127
3128 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003129 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003130 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003131 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003132 case ISD::INSERT_SUBVECTOR: {
3133 SDValue Index = N3;
3134 if (VT.isSimple() && N1.getValueType().isSimple()
3135 && N2.getValueType().isSimple()) {
3136 assert(VT.isVector() && N1.getValueType().isVector() &&
3137 N2.getValueType().isVector() &&
3138 "Insert subvector VTs must be a vectors");
3139 assert(VT == N1.getValueType() &&
3140 "Dest and insert subvector source types must match!");
3141 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3142 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003143 if (isa<ConstantSDNode>(Index.getNode())) {
3144 assert((N2.getValueType().getVectorNumElements() +
3145 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003146 <= VT.getVectorNumElements())
3147 && "Insert subvector overflow!");
3148 }
3149
3150 // Trivial insertion.
3151 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3152 return N2;
3153 }
3154 break;
3155 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003156 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003157 // Fold bit_convert nodes from a type to themselves.
3158 if (N1.getValueType() == VT)
3159 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003160 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003161 }
3162
Chris Lattner43247a12005-08-25 19:12:10 +00003163 // Memoize node if it doesn't produce a flag.
3164 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003165 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003166 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003167 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003168 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003169 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003170 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003171 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003172 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003173
Dan Gohman95531882010-03-18 18:49:47 +00003174 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003175 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003176 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003177 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003178 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003179
Chris Lattnerc3aae252005-01-07 07:46:32 +00003180 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003181#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003182 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003183#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003184 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003185}
3186
Owen Andersone50ed302009-08-10 22:56:29 +00003187SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003188 SDValue N1, SDValue N2, SDValue N3,
3189 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003190 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003191 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003192}
3193
Owen Andersone50ed302009-08-10 22:56:29 +00003194SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003195 SDValue N1, SDValue N2, SDValue N3,
3196 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003197 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003198 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003199}
3200
Dan Gohman98ca4f22009-08-05 01:29:28 +00003201/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3202/// the incoming stack arguments to be loaded from the stack.
3203SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3204 SmallVector<SDValue, 8> ArgChains;
3205
3206 // Include the original chain at the beginning of the list. When this is
3207 // used by target LowerCall hooks, this helps legalize find the
3208 // CALLSEQ_BEGIN node.
3209 ArgChains.push_back(Chain);
3210
3211 // Add a chain value for each stack argument.
3212 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3213 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3214 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3215 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3216 if (FI->getIndex() < 0)
3217 ArgChains.push_back(SDValue(L, 1));
3218
3219 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003220 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003221 &ArgChains[0], ArgChains.size());
3222}
3223
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003224/// SplatByte - Distribute ByteVal over NumBits bits.
3225static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3226 APInt Val = APInt(NumBits, ByteVal);
3227 unsigned Shift = 8;
3228 for (unsigned i = NumBits; i > 8; i >>= 1) {
3229 Val = (Val << Shift) | Val;
3230 Shift <<= 1;
3231 }
3232 return Val;
3233}
3234
Dan Gohman707e0182008-04-12 04:36:06 +00003235/// getMemsetValue - Vectorized representation of the memset value
3236/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003237static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003238 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003239 assert(Value.getOpcode() != ISD::UNDEF);
3240
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003241 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003242 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003243 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003244 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003245 return DAG.getConstant(Val, VT);
3246 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003247 }
Evan Chengf0df0312008-05-15 08:39:06 +00003248
Dale Johannesen0f502f62009-02-03 22:26:09 +00003249 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003250 if (NumBits > 8) {
3251 // Use a multiplication with 0x010101... to extend the input to the
3252 // required length.
3253 APInt Magic = SplatByte(NumBits, 0x01);
3254 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003255 }
3256
3257 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003258}
3259
Dan Gohman707e0182008-04-12 04:36:06 +00003260/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3261/// used when a memcpy is turned into a memset when the source is a constant
3262/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003263static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003264 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003265 // Handle vector with all elements zero.
3266 if (Str.empty()) {
3267 if (VT.isInteger())
3268 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003269 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003270 return DAG.getConstantFP(0.0, VT);
3271 else if (VT.isVector()) {
3272 unsigned NumElts = VT.getVectorNumElements();
3273 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003274 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003275 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3276 EltVT, NumElts)));
3277 } else
3278 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003279 }
3280
Duncan Sands83ec4b62008-06-06 12:08:01 +00003281 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003282 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3283 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3284
Dan Gohman707e0182008-04-12 04:36:06 +00003285 uint64_t Val = 0;
Chris Lattner18c7f802012-02-05 02:29:43 +00003286 if (TLI.isLittleEndian()) {
3287 for (unsigned i = 0; i != NumBytes; ++i)
3288 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3289 } else {
3290 for (unsigned i = 0; i != NumBytes; ++i)
3291 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003292 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003293
Dan Gohman707e0182008-04-12 04:36:06 +00003294 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003295}
3296
Scott Michelfdc40a02009-02-17 22:15:04 +00003297/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003298///
Dan Gohman475871a2008-07-27 21:46:04 +00003299static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003300 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003301 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003302 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003303 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003304}
3305
Evan Chengf0df0312008-05-15 08:39:06 +00003306/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3307///
Chris Lattner18c7f802012-02-05 02:29:43 +00003308static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003309 unsigned SrcDelta = 0;
3310 GlobalAddressSDNode *G = NULL;
3311 if (Src.getOpcode() == ISD::GlobalAddress)
3312 G = cast<GlobalAddressSDNode>(Src);
3313 else if (Src.getOpcode() == ISD::ADD &&
3314 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3315 Src.getOperand(1).getOpcode() == ISD::Constant) {
3316 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003317 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003318 }
3319 if (!G)
3320 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003321
Chris Lattner18c7f802012-02-05 02:29:43 +00003322 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003323}
Dan Gohman707e0182008-04-12 04:36:06 +00003324
Evan Cheng255f20f2010-04-01 06:04:33 +00003325/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3326/// to replace the memset / memcpy. Return true if the number of memory ops
3327/// is below the threshold. It returns the types of the sequence of
3328/// memory ops to perform memset / memcpy by reference.
3329static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003330 unsigned Limit, uint64_t Size,
3331 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003332 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00003333 bool MemcpyStrSrc,
Evan Cheng255f20f2010-04-01 06:04:33 +00003334 SelectionDAG &DAG,
3335 const TargetLowering &TLI) {
3336 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3337 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003338 // If 'SrcAlign' is zero, that means the memory operation does not need to
3339 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3340 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3341 // is the specified alignment of the memory operation. If it is zero, that
3342 // means it's possible to change the alignment of the destination.
3343 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3344 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003345 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003346 IsZeroVal, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003347 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003348
Chris Lattneracee6472010-03-07 07:45:08 +00003349 if (VT == MVT::Other) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003350 if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003351 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003352 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003353 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003354 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 case 0: VT = MVT::i64; break;
3356 case 4: VT = MVT::i32; break;
3357 case 2: VT = MVT::i16; break;
3358 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003359 }
3360 }
3361
Owen Anderson766b5ef2009-08-11 21:59:30 +00003362 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003363 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003364 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003365 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003366
Duncan Sands8e4eb092008-06-08 20:54:56 +00003367 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003368 VT = LVT;
3369 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003370
Dan Gohman707e0182008-04-12 04:36:06 +00003371 unsigned NumMemOps = 0;
3372 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003373 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003374 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003375 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng255f20f2010-04-01 06:04:33 +00003376 if (VT.isVector() || VT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003378 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003380 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003381 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003382 // This can result in a type that is not legal on the target, e.g.
3383 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003385 VTSize >>= 1;
3386 }
Dan Gohman707e0182008-04-12 04:36:06 +00003387 }
Dan Gohman707e0182008-04-12 04:36:06 +00003388
3389 if (++NumMemOps > Limit)
3390 return false;
3391 MemOps.push_back(VT);
3392 Size -= VTSize;
3393 }
3394
3395 return true;
3396}
3397
Dale Johannesen0f502f62009-02-03 22:26:09 +00003398static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003399 SDValue Chain, SDValue Dst,
3400 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003401 unsigned Align, bool isVol,
3402 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003403 MachinePointerInfo DstPtrInfo,
3404 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003405 // Turn a memcpy of undef to nop.
3406 if (Src.getOpcode() == ISD::UNDEF)
3407 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003408
Dan Gohman21323f32008-05-29 19:42:22 +00003409 // Expand memcpy to a series of load and store ops if the size operand falls
3410 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003411 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3412 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003413 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003414 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003415 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003416 MachineFunction &MF = DAG.getMachineFunction();
3417 MachineFrameInfo *MFI = MF.getFrameInfo();
3418 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003419 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3420 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3421 DstAlignCanChange = true;
3422 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3423 if (Align > SrcAlign)
3424 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003425 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003426 bool CopyFromStr = isMemSrcFromString(Src, Str);
3427 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003428 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003429
Evan Cheng94107ba2010-04-01 18:19:11 +00003430 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003431 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003432 (isZeroStr ? 0 : SrcAlign),
3433 true, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003434 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003435
Evan Cheng255f20f2010-04-01 06:04:33 +00003436 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003437 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003438 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3439 if (NewAlign > Align) {
3440 // Give the stack frame object a larger alignment if needed.
3441 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3442 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3443 Align = NewAlign;
3444 }
3445 }
Dan Gohman707e0182008-04-12 04:36:06 +00003446
Dan Gohman475871a2008-07-27 21:46:04 +00003447 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003448 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003449 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003450 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003451 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003452 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003453 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003454
Evan Cheng255f20f2010-04-01 06:04:33 +00003455 if (CopyFromStr &&
3456 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003457 // It's unlikely a store of a vector immediate can be done in a single
3458 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003459 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003460 // FIXME: Handle other cases where store of vector immediate is done in
3461 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003462 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Dale Johannesen0f502f62009-02-03 22:26:09 +00003463 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003464 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003465 DstPtrInfo.getWithOffset(DstOff), isVol,
3466 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003467 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003468 // The type might not be legal for the target. This should only happen
3469 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003470 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3471 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003472 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003473 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003474 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003475 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003476 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003477 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003478 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003479 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003480 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003481 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3482 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003483 }
3484 OutChains.push_back(Store);
3485 SrcOff += VTSize;
3486 DstOff += VTSize;
3487 }
3488
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003490 &OutChains[0], OutChains.size());
3491}
3492
Dale Johannesen0f502f62009-02-03 22:26:09 +00003493static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003494 SDValue Chain, SDValue Dst,
3495 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003496 unsigned Align, bool isVol,
3497 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003498 MachinePointerInfo DstPtrInfo,
3499 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003500 // Turn a memmove of undef to nop.
3501 if (Src.getOpcode() == ISD::UNDEF)
3502 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003503
3504 // Expand memmove to a series of load and store ops if the size operand falls
3505 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003506 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003507 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003508 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003509 MachineFunction &MF = DAG.getMachineFunction();
3510 MachineFrameInfo *MFI = MF.getFrameInfo();
3511 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003512 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3513 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3514 DstAlignCanChange = true;
3515 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3516 if (Align > SrcAlign)
3517 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003518 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003519
Evan Cheng94107ba2010-04-01 18:19:11 +00003520 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003521 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003522 SrcAlign, true, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003523 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003524
Evan Cheng255f20f2010-04-01 06:04:33 +00003525 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003526 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003527 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3528 if (NewAlign > Align) {
3529 // Give the stack frame object a larger alignment if needed.
3530 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3531 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3532 Align = NewAlign;
3533 }
3534 }
Dan Gohman21323f32008-05-29 19:42:22 +00003535
Evan Cheng255f20f2010-04-01 06:04:33 +00003536 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003537 SmallVector<SDValue, 8> LoadValues;
3538 SmallVector<SDValue, 8> LoadChains;
3539 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003540 unsigned NumMemOps = MemOps.size();
3541 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003542 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003543 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003544 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003545
Dale Johannesen0f502f62009-02-03 22:26:09 +00003546 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003547 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003548 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003549 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003550 LoadValues.push_back(Value);
3551 LoadChains.push_back(Value.getValue(1));
3552 SrcOff += VTSize;
3553 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003555 &LoadChains[0], LoadChains.size());
3556 OutChains.clear();
3557 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003558 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003559 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003560 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003561
Dale Johannesen0f502f62009-02-03 22:26:09 +00003562 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003563 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003564 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003565 OutChains.push_back(Store);
3566 DstOff += VTSize;
3567 }
3568
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003570 &OutChains[0], OutChains.size());
3571}
3572
Dale Johannesen0f502f62009-02-03 22:26:09 +00003573static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003574 SDValue Chain, SDValue Dst,
3575 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003576 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003577 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003578 // Turn a memset of undef to nop.
3579 if (Src.getOpcode() == ISD::UNDEF)
3580 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003581
3582 // Expand memset to a series of load/store ops if the size operand
3583 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003584 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003585 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003586 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003587 MachineFunction &MF = DAG.getMachineFunction();
3588 MachineFrameInfo *MFI = MF.getFrameInfo();
3589 bool OptSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003590 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3591 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3592 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003593 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003594 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003595 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003596 Size, (DstAlignCanChange ? 0 : Align), 0,
Lang Hames15701f82011-10-26 23:50:43 +00003597 IsZeroVal, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003598 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003599
Evan Cheng255f20f2010-04-01 06:04:33 +00003600 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003601 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Evan Cheng255f20f2010-04-01 06:04:33 +00003602 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
3603 if (NewAlign > Align) {
3604 // Give the stack frame object a larger alignment if needed.
3605 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3606 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3607 Align = NewAlign;
3608 }
3609 }
3610
Dan Gohman475871a2008-07-27 21:46:04 +00003611 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003612 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003613 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003614
3615 // Find the largest store and generate the bit pattern for it.
3616 EVT LargestVT = MemOps[0];
3617 for (unsigned i = 1; i < NumMemOps; i++)
3618 if (MemOps[i].bitsGT(LargestVT))
3619 LargestVT = MemOps[i];
3620 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3621
Dan Gohman707e0182008-04-12 04:36:06 +00003622 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003623 EVT VT = MemOps[i];
Benjamin Kramer80220362011-01-02 19:57:05 +00003624
3625 // If this store is smaller than the largest store see whether we can get
3626 // the smaller value for free with a truncate.
3627 SDValue Value = MemSetValue;
3628 if (VT.bitsLT(LargestVT)) {
3629 if (!LargestVT.isVector() && !VT.isVector() &&
3630 TLI.isTruncateFree(LargestVT, VT))
3631 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3632 else
3633 Value = getMemsetValue(Src, VT, DAG, dl);
3634 }
3635 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003636 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003637 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003638 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003639 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003640 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003641 DstOff += VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003642 }
3643
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003645 &OutChains[0], OutChains.size());
3646}
3647
Dale Johannesen0f502f62009-02-03 22:26:09 +00003648SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003649 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003650 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003651 MachinePointerInfo DstPtrInfo,
3652 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003653
3654 // Check to see if we should lower the memcpy to loads and stores first.
3655 // For cases within the target-specified limits, this is the best choice.
3656 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3657 if (ConstantSize) {
3658 // Memcpy with size zero? Just return the original chain.
3659 if (ConstantSize->isNullValue())
3660 return Chain;
3661
Evan Cheng255f20f2010-04-01 06:04:33 +00003662 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3663 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003664 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003665 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003666 return Result;
3667 }
3668
3669 // Then check to see if we should lower the memcpy with target-specific
3670 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003671 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003672 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003673 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003674 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003675 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003676 return Result;
3677
3678 // If we really need inline code and the target declined to provide it,
3679 // use a (potentially long) sequence of loads and stores.
3680 if (AlwaysInline) {
3681 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003682 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003683 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003684 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003685 }
3686
Dan Gohman7b55d362010-04-05 20:24:08 +00003687 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3688 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3689 // respect volatile, so they may do things like read or write memory
3690 // beyond the given memory regions. But fixing this isn't easy, and most
3691 // people don't care.
3692
Dan Gohman707e0182008-04-12 04:36:06 +00003693 // Emit a library call.
3694 TargetLowering::ArgListTy Args;
3695 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003696 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003697 Entry.Node = Dst; Args.push_back(Entry);
3698 Entry.Node = Src; Args.push_back(Entry);
3699 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003700 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003701 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003702 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003703 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003704 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3705 /*isTailCall=*/false,
3706 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003707 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003708 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003709 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003710 return CallResult.second;
3711}
3712
Dale Johannesen0f502f62009-02-03 22:26:09 +00003713SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003714 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003715 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003716 MachinePointerInfo DstPtrInfo,
3717 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003718
Dan Gohman21323f32008-05-29 19:42:22 +00003719 // Check to see if we should lower the memmove to loads and stores first.
3720 // For cases within the target-specified limits, this is the best choice.
3721 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3722 if (ConstantSize) {
3723 // Memmove with size zero? Just return the original chain.
3724 if (ConstantSize->isNullValue())
3725 return Chain;
3726
Dan Gohman475871a2008-07-27 21:46:04 +00003727 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003728 getMemmoveLoadsAndStores(*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 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003731 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003732 return Result;
3733 }
Dan Gohman707e0182008-04-12 04:36:06 +00003734
3735 // Then check to see if we should lower the memmove with target-specific
3736 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003737 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003738 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003739 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003740 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003741 return Result;
3742
Mon P Wang01f0e852010-04-06 08:27:51 +00003743 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3744 // not be safe. See memcpy above for more details.
3745
Dan Gohman707e0182008-04-12 04:36:06 +00003746 // Emit a library call.
3747 TargetLowering::ArgListTy Args;
3748 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003749 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003750 Entry.Node = Dst; Args.push_back(Entry);
3751 Entry.Node = Src; Args.push_back(Entry);
3752 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003753 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003754 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003755 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003756 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003757 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3758 /*isTailCall=*/false,
3759 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003760 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003761 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003762 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003763 return CallResult.second;
3764}
3765
Dale Johannesen0f502f62009-02-03 22:26:09 +00003766SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003767 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003768 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003769 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003770
3771 // Check to see if we should lower the memset to stores first.
3772 // For cases within the target-specified limits, this is the best choice.
3773 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3774 if (ConstantSize) {
3775 // Memset with size zero? Just return the original chain.
3776 if (ConstantSize->isNullValue())
3777 return Chain;
3778
Mon P Wang20adc9d2010-04-04 03:10:48 +00003779 SDValue Result =
3780 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003781 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003782
Gabor Greifba36cb52008-08-28 21:40:38 +00003783 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003784 return Result;
3785 }
3786
3787 // Then check to see if we should lower the memset with target-specific
3788 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003790 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003791 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003792 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003793 return Result;
3794
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003795 // Emit a library call.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003796 Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003797 TargetLowering::ArgListTy Args;
3798 TargetLowering::ArgListEntry Entry;
3799 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3800 Args.push_back(Entry);
3801 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 if (Src.getValueType().bitsGT(MVT::i32))
3803 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003804 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003806 Entry.Node = Src;
3807 Entry.Ty = Type::getInt32Ty(*getContext());
3808 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003809 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003810 Entry.Node = Size;
3811 Entry.Ty = IntPtrTy;
3812 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003813 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003814 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003815 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003816 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003817 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003818 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3819 /*isTailCall=*/false,
3820 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003821 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003822 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003823 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003824 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003825}
3826
Owen Andersone50ed302009-08-10 22:56:29 +00003827SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003828 SDValue Chain, SDValue Ptr, SDValue Cmp,
3829 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003830 unsigned Alignment,
3831 AtomicOrdering Ordering,
3832 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003833 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3834 Alignment = getEVTAlignment(MemVT);
3835
Evan Chengff89dcb2009-10-18 18:16:27 +00003836 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003837 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3838
3839 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003840 // FIXME: Volatile isn't really correct; we should keep track of atomic
3841 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003842 Flags |= MachineMemOperand::MOVolatile;
3843
3844 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00003845 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003846
Eli Friedman55ba8162011-07-29 03:05:32 +00003847 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
3848 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003849}
3850
3851SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3852 SDValue Chain,
3853 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00003854 SDValue Swp, MachineMemOperand *MMO,
3855 AtomicOrdering Ordering,
3856 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003857 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3858 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3859
Owen Andersone50ed302009-08-10 22:56:29 +00003860 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003861
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003863 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003864 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003865 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3866 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3867 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003868 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3869 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003870 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003871 }
Dan Gohman95531882010-03-18 18:49:47 +00003872 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003873 Ptr, Cmp, Swp, MMO, Ordering,
3874 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003875 CSEMap.InsertNode(N, IP);
3876 AllNodes.push_back(N);
3877 return SDValue(N, 0);
3878}
3879
Owen Andersone50ed302009-08-10 22:56:29 +00003880SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003881 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003882 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003883 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00003884 unsigned Alignment,
3885 AtomicOrdering Ordering,
3886 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003887 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3888 Alignment = getEVTAlignment(MemVT);
3889
Evan Chengff89dcb2009-10-18 18:16:27 +00003890 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00003891 // A monotonic store does not load; a release store "loads" in the sense
3892 // that other stores cannot be sunk past it.
3893 // (An atomicrmw obviously both loads and stores.)
3894 unsigned Flags = MachineMemOperand::MOStore;
3895 if (Opcode != ISD::ATOMIC_STORE || Ordering > Monotonic)
3896 Flags |= MachineMemOperand::MOLoad;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003897
3898 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003899 // FIXME: Volatile isn't really correct; we should keep track of atomic
3900 // orderings in the memoperand.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003901 Flags |= MachineMemOperand::MOVolatile;
3902
3903 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00003904 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00003905 MemVT.getStoreSize(), Alignment);
3906
Eli Friedman55ba8162011-07-29 03:05:32 +00003907 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
3908 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003909}
3910
3911SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3912 SDValue Chain,
3913 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00003914 MachineMemOperand *MMO,
3915 AtomicOrdering Ordering,
3916 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003917 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3918 Opcode == ISD::ATOMIC_LOAD_SUB ||
3919 Opcode == ISD::ATOMIC_LOAD_AND ||
3920 Opcode == ISD::ATOMIC_LOAD_OR ||
3921 Opcode == ISD::ATOMIC_LOAD_XOR ||
3922 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003923 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003924 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003925 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003926 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00003927 Opcode == ISD::ATOMIC_SWAP ||
3928 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003929 "Invalid Atomic Op");
3930
Owen Andersone50ed302009-08-10 22:56:29 +00003931 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003932
Eli Friedman327236c2011-08-24 20:50:09 +00003933 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
3934 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003935 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003936 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003937 SDValue Ops[] = {Chain, Ptr, Val};
3938 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3939 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3941 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003942 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003943 }
Dan Gohman95531882010-03-18 18:49:47 +00003944 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003945 Ptr, Val, MMO,
3946 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00003947 CSEMap.InsertNode(N, IP);
3948 AllNodes.push_back(N);
3949 return SDValue(N, 0);
3950}
3951
Eli Friedman327236c2011-08-24 20:50:09 +00003952SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3953 EVT VT, SDValue Chain,
3954 SDValue Ptr,
3955 const Value* PtrVal,
3956 unsigned Alignment,
3957 AtomicOrdering Ordering,
3958 SynchronizationScope SynchScope) {
3959 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3960 Alignment = getEVTAlignment(MemVT);
3961
3962 MachineFunction &MF = getMachineFunction();
Eli Friedman981a0102011-09-07 02:23:42 +00003963 // A monotonic load does not store; an acquire load "stores" in the sense
3964 // that other loads cannot be hoisted past it.
3965 unsigned Flags = MachineMemOperand::MOLoad;
3966 if (Ordering > Monotonic)
3967 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00003968
3969 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003970 // FIXME: Volatile isn't really correct; we should keep track of atomic
3971 // orderings in the memoperand.
Eli Friedman327236c2011-08-24 20:50:09 +00003972 Flags |= MachineMemOperand::MOVolatile;
3973
3974 MachineMemOperand *MMO =
3975 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
3976 MemVT.getStoreSize(), Alignment);
3977
3978 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
3979 Ordering, SynchScope);
3980}
3981
3982SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3983 EVT VT, SDValue Chain,
3984 SDValue Ptr,
3985 MachineMemOperand *MMO,
3986 AtomicOrdering Ordering,
3987 SynchronizationScope SynchScope) {
3988 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
3989
3990 SDVTList VTs = getVTList(VT, MVT::Other);
3991 FoldingSetNodeID ID;
3992 ID.AddInteger(MemVT.getRawBits());
3993 SDValue Ops[] = {Chain, Ptr};
3994 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
3995 void* IP = 0;
3996 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3997 cast<AtomicSDNode>(E)->refineAlignment(MMO);
3998 return SDValue(E, 0);
3999 }
4000 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4001 Ptr, MMO, Ordering, SynchScope);
4002 CSEMap.InsertNode(N, IP);
4003 AllNodes.push_back(N);
4004 return SDValue(N, 0);
4005}
4006
Duncan Sands4bdcb612008-07-02 17:40:58 +00004007/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004008SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4009 DebugLoc dl) {
4010 if (NumOps == 1)
4011 return Ops[0];
4012
Owen Andersone50ed302009-08-10 22:56:29 +00004013 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004014 VTs.reserve(NumOps);
4015 for (unsigned i = 0; i < NumOps; ++i)
4016 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004017 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004018 Ops, NumOps);
4019}
4020
Dan Gohman475871a2008-07-27 21:46:04 +00004021SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004022SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004023 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004024 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004025 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004026 unsigned Align, bool Vol,
4027 bool ReadMem, bool WriteMem) {
4028 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004029 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004030 ReadMem, WriteMem);
4031}
4032
4033SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004034SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4035 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004036 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004037 unsigned Align, bool Vol,
4038 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004039 if (Align == 0) // Ensure that codegen never sees alignment 0
4040 Align = getEVTAlignment(MemVT);
4041
4042 MachineFunction &MF = getMachineFunction();
4043 unsigned Flags = 0;
4044 if (WriteMem)
4045 Flags |= MachineMemOperand::MOStore;
4046 if (ReadMem)
4047 Flags |= MachineMemOperand::MOLoad;
4048 if (Vol)
4049 Flags |= MachineMemOperand::MOVolatile;
4050 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004051 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004052
4053 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4054}
4055
4056SDValue
4057SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4058 const SDValue *Ops, unsigned NumOps,
4059 EVT MemVT, MachineMemOperand *MMO) {
4060 assert((Opcode == ISD::INTRINSIC_VOID ||
4061 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004062 Opcode == ISD::PREFETCH ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004063 (Opcode <= INT_MAX &&
4064 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4065 "Opcode is not a memory-accessing opcode!");
4066
Dale Johannesene8c17332009-01-29 00:47:48 +00004067 // Memoize the node unless it returns a flag.
4068 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004069 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004070 FoldingSetNodeID ID;
4071 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4072 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004073 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4074 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004075 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004077
Dan Gohman95531882010-03-18 18:49:47 +00004078 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4079 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004080 CSEMap.InsertNode(N, IP);
4081 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004082 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4083 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004084 }
4085 AllNodes.push_back(N);
4086 return SDValue(N, 0);
4087}
4088
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004089/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4090/// MachinePointerInfo record from it. This is particularly useful because the
4091/// code generator has many cases where it doesn't bother passing in a
4092/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4093static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4094 // If this is FI+Offset, we can model it.
4095 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4096 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4097
4098 // If this is (FI+Offset1)+Offset2, we can model it.
4099 if (Ptr.getOpcode() != ISD::ADD ||
4100 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4101 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4102 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004103
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004104 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4105 return MachinePointerInfo::getFixedStack(FI, Offset+
4106 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4107}
4108
4109/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4110/// MachinePointerInfo record from it. This is particularly useful because the
4111/// code generator has many cases where it doesn't bother passing in a
4112/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4113static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4114 // If the 'Offset' value isn't a constant, we can't handle this.
4115 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4116 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4117 if (OffsetOp.getOpcode() == ISD::UNDEF)
4118 return InferPointerInfo(Ptr);
4119 return MachinePointerInfo();
4120}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004121
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004122
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004123SDValue
4124SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4125 EVT VT, DebugLoc dl, SDValue Chain,
4126 SDValue Ptr, SDValue Offset,
4127 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004128 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004129 unsigned Alignment, const MDNode *TBAAInfo,
4130 const MDNode *Ranges) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004131 assert(Chain.getValueType() == MVT::Other &&
4132 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004133 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4134 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004135
Dan Gohmanc76909a2009-09-25 20:36:54 +00004136 unsigned Flags = MachineMemOperand::MOLoad;
4137 if (isVolatile)
4138 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004139 if (isNonTemporal)
4140 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004141 if (isInvariant)
4142 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004143
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004144 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4145 // clients.
4146 if (PtrInfo.V == 0)
4147 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004148
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004149 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004150 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004151 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004152 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004153 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004154}
4155
4156SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004157SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004158 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004159 SDValue Ptr, SDValue Offset, EVT MemVT,
4160 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004161 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004162 ExtType = ISD::NON_EXTLOAD;
4163 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004164 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004165 } else {
4166 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004167 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4168 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004169 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004170 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004171 assert(VT.isVector() == MemVT.isVector() &&
4172 "Cannot use trunc store to convert to or from a vector!");
4173 assert((!VT.isVector() ||
4174 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4175 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004176 }
4177
4178 bool Indexed = AM != ISD::UNINDEXED;
4179 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4180 "Unindexed load with an offset!");
4181
4182 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004184 SDValue Ops[] = { Chain, Ptr, Offset };
4185 FoldingSetNodeID ID;
4186 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004187 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004188 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004189 MMO->isNonTemporal(),
4190 MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004191 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004192 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4193 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004194 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004195 }
Dan Gohman95531882010-03-18 18:49:47 +00004196 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4197 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004198 CSEMap.InsertNode(N, IP);
4199 AllNodes.push_back(N);
4200 return SDValue(N, 0);
4201}
4202
Owen Andersone50ed302009-08-10 22:56:29 +00004203SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004204 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004205 MachinePointerInfo PtrInfo,
4206 bool isVolatile, bool isNonTemporal,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004207 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004208 const MDNode *TBAAInfo,
4209 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004210 SDValue Undef = getUNDEF(Ptr.getValueType());
4211 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004212 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4213 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004214}
Dale Johannesene8c17332009-01-29 00:47:48 +00004215
Stuart Hastingsa9011292011-02-16 16:23:55 +00004216SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004217 SDValue Chain, SDValue Ptr,
4218 MachinePointerInfo PtrInfo, EVT MemVT,
4219 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004220 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004221 SDValue Undef = getUNDEF(Ptr.getValueType());
4222 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004223 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004224 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004225}
4226
4227
Dan Gohman475871a2008-07-27 21:46:04 +00004228SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004229SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4230 SDValue Offset, ISD::MemIndexedMode AM) {
4231 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4232 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4233 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004234 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004235 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004236 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
4237 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004238}
4239
Dale Johannesene8c17332009-01-29 00:47:48 +00004240SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004241 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004242 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004243 unsigned Alignment, const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004244 assert(Chain.getValueType() == MVT::Other &&
4245 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004246 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004247 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004248
Dan Gohmanc76909a2009-09-25 20:36:54 +00004249 unsigned Flags = MachineMemOperand::MOStore;
4250 if (isVolatile)
4251 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004252 if (isNonTemporal)
4253 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004254
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004255 if (PtrInfo.V == 0)
4256 PtrInfo = InferPointerInfo(Ptr);
4257
4258 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004259 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004260 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004261 Val.getValueType().getStoreSize(), Alignment,
4262 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004263
4264 return getStore(Chain, dl, Val, Ptr, MMO);
4265}
4266
4267SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4268 SDValue Ptr, MachineMemOperand *MMO) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004269 assert(Chain.getValueType() == MVT::Other &&
4270 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004271 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004272 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004273 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004274 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4275 FoldingSetNodeID ID;
4276 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004277 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004278 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004279 MMO->isNonTemporal(), MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004280 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004281 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4282 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004283 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004284 }
Dan Gohman95531882010-03-18 18:49:47 +00004285 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4286 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004287 CSEMap.InsertNode(N, IP);
4288 AllNodes.push_back(N);
4289 return SDValue(N, 0);
4290}
4291
Dale Johannesene8c17332009-01-29 00:47:48 +00004292SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004293 SDValue Ptr, MachinePointerInfo PtrInfo,
4294 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004295 unsigned Alignment,
4296 const MDNode *TBAAInfo) {
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004297 assert(Chain.getValueType() == MVT::Other &&
4298 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004299 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4300 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004301
Dan Gohmanc76909a2009-09-25 20:36:54 +00004302 unsigned Flags = MachineMemOperand::MOStore;
4303 if (isVolatile)
4304 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004305 if (isNonTemporal)
4306 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004307
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004308 if (PtrInfo.V == 0)
4309 PtrInfo = InferPointerInfo(Ptr);
4310
4311 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004312 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004313 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4314 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004315
4316 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4317}
4318
4319SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4320 SDValue Ptr, EVT SVT,
4321 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004322 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004323
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004324 assert(Chain.getValueType() == MVT::Other &&
4325 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004326 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004327 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004328
Dan Gohman2e141d72009-12-14 23:40:38 +00004329 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4330 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004331 assert(VT.isInteger() == SVT.isInteger() &&
4332 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004333 assert(VT.isVector() == SVT.isVector() &&
4334 "Cannot use trunc store to convert to or from a vector!");
4335 assert((!VT.isVector() ||
4336 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4337 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004338
Owen Anderson825b72b2009-08-11 20:47:22 +00004339 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004340 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004341 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4342 FoldingSetNodeID ID;
4343 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004344 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004345 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004346 MMO->isNonTemporal(), MMO->isInvariant()));
Dale Johannesene8c17332009-01-29 00:47:48 +00004347 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004348 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4349 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004350 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004351 }
Dan Gohman95531882010-03-18 18:49:47 +00004352 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4353 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004354 CSEMap.InsertNode(N, IP);
4355 AllNodes.push_back(N);
4356 return SDValue(N, 0);
4357}
4358
Dan Gohman475871a2008-07-27 21:46:04 +00004359SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004360SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4361 SDValue Offset, ISD::MemIndexedMode AM) {
4362 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4363 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4364 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004365 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004366 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4367 FoldingSetNodeID ID;
4368 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004369 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004370 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00004371 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004372 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004373 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004374
Dan Gohman95531882010-03-18 18:49:47 +00004375 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4376 ST->isTruncatingStore(),
4377 ST->getMemoryVT(),
4378 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004379 CSEMap.InsertNode(N, IP);
4380 AllNodes.push_back(N);
4381 return SDValue(N, 0);
4382}
4383
Owen Andersone50ed302009-08-10 22:56:29 +00004384SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004385 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004386 SDValue SV,
4387 unsigned Align) {
4388 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4389 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004390}
4391
Owen Andersone50ed302009-08-10 22:56:29 +00004392SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004393 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004394 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004395 case 0: return getNode(Opcode, DL, VT);
4396 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4397 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4398 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004399 default: break;
4400 }
4401
Dan Gohman475871a2008-07-27 21:46:04 +00004402 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004403 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004404 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004405 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004406}
4407
Owen Andersone50ed302009-08-10 22:56:29 +00004408SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004409 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004410 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004411 case 0: return getNode(Opcode, DL, VT);
4412 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4413 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4414 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004415 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004416 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004417
Chris Lattneref847df2005-04-09 03:27:28 +00004418 switch (Opcode) {
4419 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004420 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004421 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004422 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4423 "LHS and RHS of condition must have same type!");
4424 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4425 "True and False arms of SelectCC must have same type!");
4426 assert(Ops[2].getValueType() == VT &&
4427 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004428 break;
4429 }
4430 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004431 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004432 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4433 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004434 break;
4435 }
Chris Lattneref847df2005-04-09 03:27:28 +00004436 }
4437
Chris Lattner385328c2005-05-14 07:42:29 +00004438 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004439 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004440 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004441
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004442 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004443 FoldingSetNodeID ID;
4444 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004445 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004446
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004448 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004449
Dan Gohman95531882010-03-18 18:49:47 +00004450 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004451 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004452 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004453 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004454 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004455
Chris Lattneref847df2005-04-09 03:27:28 +00004456 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004457#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004458 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004459#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004460 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004461}
4462
Bill Wendling7ade28c2009-01-28 22:17:52 +00004463SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004464 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004465 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004466 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004467 Ops, NumOps);
4468}
4469
Bill Wendling7ade28c2009-01-28 22:17:52 +00004470SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004471 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004472 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004473 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004474 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4475 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004476}
4477
Bill Wendling7ade28c2009-01-28 22:17:52 +00004478SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4479 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004480 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004481 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004482
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004483#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004484 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004485 // FIXME: figure out how to safely handle things like
4486 // int foo(int x) { return 1 << (x & 255); }
4487 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004488 case ISD::SRA_PARTS:
4489 case ISD::SRL_PARTS:
4490 case ISD::SHL_PARTS:
4491 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004492 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004493 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004494 else if (N3.getOpcode() == ISD::AND)
4495 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4496 // If the and is only masking out bits that cannot effect the shift,
4497 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004498 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004499 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004500 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004501 }
4502 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004503 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004504#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004505
Chris Lattner43247a12005-08-25 19:12:10 +00004506 // Memoize the node unless it returns a flag.
4507 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004508 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004509 FoldingSetNodeID ID;
4510 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004511 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004512 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004513 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004514
Dan Gohman0e5f1302008-07-07 23:02:41 +00004515 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004516 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004517 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004518 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004519 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004520 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4521 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004522 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004523 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004524 }
Chris Lattnera5682852006-08-07 23:03:03 +00004525 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004526 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004527 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004528 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004529 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004530 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004531 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004532 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4533 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004534 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004535 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004536 }
Chris Lattner43247a12005-08-25 19:12:10 +00004537 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004538 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004539#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004540 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004541#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004542 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004543}
4544
Bill Wendling7ade28c2009-01-28 22:17:52 +00004545SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4546 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004547}
4548
Bill Wendling7ade28c2009-01-28 22:17:52 +00004549SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4550 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004552 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004553}
4554
Bill Wendling7ade28c2009-01-28 22:17:52 +00004555SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4556 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004558 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004559}
4560
Bill Wendling7ade28c2009-01-28 22:17:52 +00004561SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4562 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004563 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004564 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004565}
4566
Bill Wendling7ade28c2009-01-28 22:17:52 +00004567SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4568 SDValue N1, SDValue N2, SDValue N3,
4569 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004570 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004571 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004572}
4573
Bill Wendling7ade28c2009-01-28 22:17:52 +00004574SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4575 SDValue N1, SDValue N2, SDValue N3,
4576 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004577 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004578 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004579}
4580
Owen Andersone50ed302009-08-10 22:56:29 +00004581SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004582 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004583}
4584
Owen Andersone50ed302009-08-10 22:56:29 +00004585SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004586 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4587 E = VTList.rend(); I != E; ++I)
4588 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4589 return *I;
4590
Owen Andersone50ed302009-08-10 22:56:29 +00004591 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004592 Array[0] = VT1;
4593 Array[1] = VT2;
4594 SDVTList Result = makeVTList(Array, 2);
4595 VTList.push_back(Result);
4596 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004597}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004598
Owen Andersone50ed302009-08-10 22:56:29 +00004599SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004600 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4601 E = VTList.rend(); I != E; ++I)
4602 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4603 I->VTs[2] == VT3)
4604 return *I;
4605
Owen Andersone50ed302009-08-10 22:56:29 +00004606 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004607 Array[0] = VT1;
4608 Array[1] = VT2;
4609 Array[2] = VT3;
4610 SDVTList Result = makeVTList(Array, 3);
4611 VTList.push_back(Result);
4612 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004613}
4614
Owen Andersone50ed302009-08-10 22:56:29 +00004615SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004616 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4617 E = VTList.rend(); I != E; ++I)
4618 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4619 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4620 return *I;
4621
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004622 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004623 Array[0] = VT1;
4624 Array[1] = VT2;
4625 Array[2] = VT3;
4626 Array[3] = VT4;
4627 SDVTList Result = makeVTList(Array, 4);
4628 VTList.push_back(Result);
4629 return Result;
4630}
4631
Owen Andersone50ed302009-08-10 22:56:29 +00004632SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004633 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004634 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004635 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004636 case 2: return getVTList(VTs[0], VTs[1]);
4637 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004638 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004639 default: break;
4640 }
4641
Dan Gohmane8be6c62008-07-17 19:10:17 +00004642 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4643 E = VTList.rend(); I != E; ++I) {
4644 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4645 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004646
Chris Lattner70046e92006-08-15 17:46:01 +00004647 bool NoMatch = false;
4648 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004649 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004650 NoMatch = true;
4651 break;
4652 }
4653 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004654 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004656
Owen Andersone50ed302009-08-10 22:56:29 +00004657 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004658 std::copy(VTs, VTs+NumVTs, Array);
4659 SDVTList Result = makeVTList(Array, NumVTs);
4660 VTList.push_back(Result);
4661 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004662}
4663
4664
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004665/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4666/// specified operands. If the resultant node already exists in the DAG,
4667/// this does not modify the specified node, instead it returns the node that
4668/// already exists. If the resultant node does not exist in the DAG, the
4669/// input node is returned. As a degenerate case, if you specify the same
4670/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004671SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004672 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004673
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004674 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004675 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004676
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004677 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004678 void *InsertPos = 0;
4679 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004680 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004681
Dan Gohman79acd2b2008-07-21 22:38:59 +00004682 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004683 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004684 if (!RemoveNodeFromCSEMaps(N))
4685 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004686
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004687 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004688 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004689
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004690 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004691 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004692 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004693}
4694
Dan Gohman027657d2010-06-18 15:30:29 +00004695SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004696 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004697
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004698 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004699 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004700 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004701
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004702 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004703 void *InsertPos = 0;
4704 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004705 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004706
Dan Gohman79acd2b2008-07-21 22:38:59 +00004707 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004708 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004709 if (!RemoveNodeFromCSEMaps(N))
4710 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004711
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004712 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004713 if (N->OperandList[0] != Op1)
4714 N->OperandList[0].set(Op1);
4715 if (N->OperandList[1] != Op2)
4716 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004717
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004718 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004719 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004720 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004721}
4722
Dan Gohman027657d2010-06-18 15:30:29 +00004723SDNode *SelectionDAG::
4724UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004725 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004726 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004727}
4728
Dan Gohman027657d2010-06-18 15:30:29 +00004729SDNode *SelectionDAG::
4730UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004731 SDValue Op3, SDValue Op4) {
4732 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004733 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004734}
4735
Dan Gohman027657d2010-06-18 15:30:29 +00004736SDNode *SelectionDAG::
4737UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004738 SDValue Op3, SDValue Op4, SDValue Op5) {
4739 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004740 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004741}
4742
Dan Gohman027657d2010-06-18 15:30:29 +00004743SDNode *SelectionDAG::
4744UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004745 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004746 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004747
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004748 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004749 bool AnyChange = false;
4750 for (unsigned i = 0; i != NumOps; ++i) {
4751 if (Ops[i] != N->getOperand(i)) {
4752 AnyChange = true;
4753 break;
4754 }
4755 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004756
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004757 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004758 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004759
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004760 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004761 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004762 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004763 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004764
Dan Gohman7ceda162008-05-02 00:05:03 +00004765 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004766 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004767 if (!RemoveNodeFromCSEMaps(N))
4768 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004769
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004770 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004771 for (unsigned i = 0; i != NumOps; ++i)
4772 if (N->OperandList[i] != Ops[i])
4773 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004774
4775 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004776 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004777 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004778}
4779
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004780/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004781/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004782void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004783 // Unlike the code in MorphNodeTo that does this, we don't need to
4784 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004785 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4786 SDUse &Use = *I++;
4787 Use.set(SDValue());
4788 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004789}
Chris Lattner149c58c2005-08-16 18:17:10 +00004790
Dan Gohmane8be6c62008-07-17 19:10:17 +00004791/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4792/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004793///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004794SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004795 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004796 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004797 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004798}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004799
Dan Gohmane8be6c62008-07-17 19:10:17 +00004800SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004801 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004802 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004803 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004804 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004805}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004806
Dan Gohmane8be6c62008-07-17 19:10:17 +00004807SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004808 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004809 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004810 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004812 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004813}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004814
Dan Gohmane8be6c62008-07-17 19:10:17 +00004815SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004816 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004818 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004819 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004820 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004821}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004822
Dan Gohmane8be6c62008-07-17 19:10:17 +00004823SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004824 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004825 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004826 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004827 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004828}
4829
Dan Gohmane8be6c62008-07-17 19:10:17 +00004830SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004831 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004832 unsigned NumOps) {
4833 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004834 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004835}
4836
Dan Gohmane8be6c62008-07-17 19:10:17 +00004837SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004838 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004839 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004841}
4842
Dan Gohmane8be6c62008-07-17 19:10:17 +00004843SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004844 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004845 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004846 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004847 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004848}
4849
Bill Wendling13d6d442008-12-01 23:28:22 +00004850SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004851 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004852 const SDValue *Ops, unsigned NumOps) {
4853 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4854 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4855}
4856
Scott Michelfdc40a02009-02-17 22:15:04 +00004857SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004858 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004860 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004861 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004862 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004863}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004864
Scott Michelfdc40a02009-02-17 22:15:04 +00004865SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004866 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004867 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004868 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004869 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004870 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004871}
4872
Dan Gohmane8be6c62008-07-17 19:10:17 +00004873SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004874 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004875 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004876 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004877 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004878 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004879 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004880}
4881
Dan Gohmane8be6c62008-07-17 19:10:17 +00004882SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004883 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004884 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004885 SDValue Op3) {
4886 SDVTList VTs = getVTList(VT1, VT2, VT3);
4887 SDValue Ops[] = { Op1, Op2, Op3 };
4888 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4889}
4890
4891SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004893 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00004894 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4895 // Reset the NodeID to -1.
4896 N->setNodeId(-1);
4897 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004898}
4899
Devang Patel0508d042011-12-15 18:21:18 +00004900/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
4901/// the line number information on the merged node since it is not possible to
4902/// preserve the information that operation is associated with multiple lines.
4903/// This will make the debugger working better at -O0, were there is a higher
4904/// probability having other instructions associated with that line.
4905///
4906SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
4907 DebugLoc NLoc = N->getDebugLoc();
4908 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
4909 N->setDebugLoc(DebugLoc());
4910 }
4911 return N;
4912}
4913
Chris Lattner21221e32010-02-28 21:36:14 +00004914/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00004915/// return type, opcode, and operands.
4916///
4917/// Note that MorphNodeTo returns the resultant node. If there is already a
4918/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004919/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004920///
4921/// Using MorphNodeTo is faster than creating a new node and swapping it in
4922/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004923/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004924/// the node's users.
4925///
4926SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004928 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004929 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004930 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004931 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004932 FoldingSetNodeID ID;
4933 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004934 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00004935 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004936 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004937
Dan Gohman095cc292008-09-13 01:54:27 +00004938 if (!RemoveNodeFromCSEMaps(N))
4939 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004940
Dan Gohmane8be6c62008-07-17 19:10:17 +00004941 // Start the morphing.
4942 N->NodeType = Opc;
4943 N->ValueList = VTs.VTs;
4944 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004945
Dan Gohmane8be6c62008-07-17 19:10:17 +00004946 // Clear the operands list, updating used nodes to remove this from their
4947 // use list. Keep track of any operands that become dead as a result.
4948 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004949 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4950 SDUse &Use = *I++;
4951 SDNode *Used = Use.getNode();
4952 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004953 if (Used->use_empty())
4954 DeadNodeSet.insert(Used);
4955 }
4956
Dan Gohmanc76909a2009-09-25 20:36:54 +00004957 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
4958 // Initialize the memory references information.
4959 MN->setMemRefs(0, 0);
4960 // If NumOps is larger than the # of operands we can have in a
4961 // MachineSDNode, reallocate the operand list.
4962 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
4963 if (MN->OperandsNeedDelete)
4964 delete[] MN->OperandList;
4965 if (NumOps > array_lengthof(MN->LocalOperands))
4966 // We're creating a final node that will live unmorphed for the
4967 // remainder of the current SelectionDAG iteration, so we can allocate
4968 // the operands directly out of a pool with no recycling metadata.
4969 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00004970 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004971 else
4972 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
4973 MN->OperandsNeedDelete = false;
4974 } else
4975 MN->InitOperands(MN->OperandList, Ops, NumOps);
4976 } else {
4977 // If NumOps is larger than the # of operands we currently have, reallocate
4978 // the operand list.
4979 if (NumOps > N->NumOperands) {
4980 if (N->OperandsNeedDelete)
4981 delete[] N->OperandList;
4982 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004983 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004984 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00004985 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004986 }
4987
4988 // Delete any nodes that are still dead after adding the uses for the
4989 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00004990 if (!DeadNodeSet.empty()) {
4991 SmallVector<SDNode *, 16> DeadNodes;
4992 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4993 E = DeadNodeSet.end(); I != E; ++I)
4994 if ((*I)->use_empty())
4995 DeadNodes.push_back(*I);
4996 RemoveDeadNodes(DeadNodes);
4997 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004998
Dan Gohmane8be6c62008-07-17 19:10:17 +00004999 if (IP)
5000 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005001 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005002}
5003
Chris Lattner0fb094f2005-11-19 01:44:53 +00005004
Dan Gohman602b0c82009-09-25 18:54:59 +00005005/// getMachineNode - These are used for target selectors to create a new node
5006/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005007///
Dan Gohman602b0c82009-09-25 18:54:59 +00005008/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005009/// node of the specified opcode and operands, it returns that node instead of
5010/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005011MachineSDNode *
5012SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005013 SDVTList VTs = getVTList(VT);
5014 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005015}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005016
Dan Gohmanc81b7832009-10-10 01:29:16 +00005017MachineSDNode *
5018SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005019 SDVTList VTs = getVTList(VT);
5020 SDValue Ops[] = { Op1 };
5021 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005022}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005023
Dan Gohmanc81b7832009-10-10 01:29:16 +00005024MachineSDNode *
5025SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5026 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005027 SDVTList VTs = getVTList(VT);
5028 SDValue Ops[] = { Op1, Op2 };
5029 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005030}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005031
Dan Gohmanc81b7832009-10-10 01:29:16 +00005032MachineSDNode *
5033SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5034 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005035 SDVTList VTs = getVTList(VT);
5036 SDValue Ops[] = { Op1, Op2, Op3 };
5037 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005038}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005039
Dan Gohmanc81b7832009-10-10 01:29:16 +00005040MachineSDNode *
5041SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5042 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005043 SDVTList VTs = getVTList(VT);
5044 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005045}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005046
Dan Gohmanc81b7832009-10-10 01:29:16 +00005047MachineSDNode *
5048SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005049 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005050 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005051}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005052
Dan Gohmanc81b7832009-10-10 01:29:16 +00005053MachineSDNode *
5054SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5055 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005056 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005057 SDValue Ops[] = { Op1 };
5058 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005059}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005060
Dan Gohmanc81b7832009-10-10 01:29:16 +00005061MachineSDNode *
5062SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5063 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005064 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005065 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005066 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005067}
5068
Dan Gohmanc81b7832009-10-10 01:29:16 +00005069MachineSDNode *
5070SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5071 EVT VT1, EVT VT2, SDValue Op1,
5072 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005073 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005074 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005075 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005076}
5077
Dan Gohmanc81b7832009-10-10 01:29:16 +00005078MachineSDNode *
5079SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5080 EVT VT1, EVT VT2,
5081 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005082 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005083 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
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,
5088 EVT VT1, EVT VT2, EVT VT3,
5089 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005090 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005091 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005092 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005093}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005094
Dan Gohmanc81b7832009-10-10 01:29:16 +00005095MachineSDNode *
5096SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5097 EVT VT1, EVT VT2, EVT VT3,
5098 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005099 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005100 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005101 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005102}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005103
Dan Gohmanc81b7832009-10-10 01:29:16 +00005104MachineSDNode *
5105SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5106 EVT VT1, EVT VT2, EVT VT3,
5107 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005108 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005109 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005110}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005111
Dan Gohmanc81b7832009-10-10 01:29:16 +00005112MachineSDNode *
5113SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5114 EVT VT2, EVT VT3, EVT VT4,
5115 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005116 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005117 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005118}
5119
Dan Gohmanc81b7832009-10-10 01:29:16 +00005120MachineSDNode *
5121SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5122 const std::vector<EVT> &ResultTys,
5123 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005124 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5125 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5126}
5127
Dan Gohmanc81b7832009-10-10 01:29:16 +00005128MachineSDNode *
5129SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5130 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005131 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005132 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005133 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005134
5135 if (DoCSE) {
5136 FoldingSetNodeID ID;
5137 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5138 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005139 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5140 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5141 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005142 }
5143
5144 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005145 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005146
5147 // Initialize the operands list.
5148 if (NumOps > array_lengthof(N->LocalOperands))
5149 // We're creating a final node that will live unmorphed for the
5150 // remainder of the current SelectionDAG iteration, so we can allocate
5151 // the operands directly out of a pool with no recycling metadata.
5152 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5153 Ops, NumOps);
5154 else
5155 N->InitOperands(N->LocalOperands, Ops, NumOps);
5156 N->OperandsNeedDelete = false;
5157
5158 if (DoCSE)
5159 CSEMap.InsertNode(N, IP);
5160
5161 AllNodes.push_back(N);
5162#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005163 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005164#endif
5165 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005166}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005167
Dan Gohman6a402dc2009-08-19 18:16:17 +00005168/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005169/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005170SDValue
5171SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5172 SDValue Operand) {
5173 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005174 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005175 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005176 return SDValue(Subreg, 0);
5177}
5178
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005179/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005180/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005181SDValue
5182SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5183 SDValue Operand, SDValue Subreg) {
5184 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005185 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005186 VT, Operand, Subreg, SRIdxVal);
5187 return SDValue(Result, 0);
5188}
5189
Evan Cheng08b11732008-03-22 01:55:50 +00005190/// getNodeIfExists - Get the specified node if it's already available, or
5191/// else return NULL.
5192SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005193 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005194 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005195 FoldingSetNodeID ID;
5196 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5197 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005198 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005199 return E;
5200 }
5201 return NULL;
5202}
5203
Evan Cheng31441b72010-03-29 20:48:30 +00005204/// getDbgValue - Creates a SDDbgValue node.
5205///
5206SDDbgValue *
5207SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5208 DebugLoc DL, unsigned O) {
5209 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5210}
5211
5212SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005213SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005214 DebugLoc DL, unsigned O) {
5215 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5216}
5217
5218SDDbgValue *
5219SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5220 DebugLoc DL, unsigned O) {
5221 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5222}
5223
Dan Gohmana72d2a22010-03-03 21:33:37 +00005224namespace {
5225
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005226/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005227/// pointed to by a use iterator is deleted, increment the use iterator
5228/// so that it doesn't dangle.
5229///
5230/// This class also manages a "downlink" DAGUpdateListener, to forward
5231/// messages to ReplaceAllUsesWith's callers.
5232///
5233class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
5234 SelectionDAG::DAGUpdateListener *DownLink;
5235 SDNode::use_iterator &UI;
5236 SDNode::use_iterator &UE;
5237
5238 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5239 // Increment the iterator as needed.
5240 while (UI != UE && N == *UI)
5241 ++UI;
5242
5243 // Then forward the message.
5244 if (DownLink) DownLink->NodeDeleted(N, E);
5245 }
5246
5247 virtual void NodeUpdated(SDNode *N) {
5248 // Just forward the message.
5249 if (DownLink) DownLink->NodeUpdated(N);
5250 }
5251
5252public:
5253 RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl,
5254 SDNode::use_iterator &ui,
5255 SDNode::use_iterator &ue)
5256 : DownLink(dl), UI(ui), UE(ue) {}
5257};
5258
5259}
5260
Evan Cheng99157a02006-08-07 22:13:29 +00005261/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005262/// This can cause recursive merging of nodes in the DAG.
5263///
Chris Lattner11d049c2008-02-03 03:35:22 +00005264/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005265///
Dan Gohman475871a2008-07-27 21:46:04 +00005266void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005267 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005268 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005269 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005270 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005271 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005272
Dan Gohman39946102009-01-25 16:29:12 +00005273 // Iterate over all the existing uses of From. New uses will be added
5274 // to the beginning of the use list, which we avoid visiting.
5275 // This specifically avoids visiting uses of From that arise while the
5276 // replacement is happening, because any such uses would be the result
5277 // of CSE: If an existing node looks like From after one of its operands
5278 // is replaced by To, we don't want to replace of all its users with To
5279 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005280 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005281 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005282 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005283 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005284
Chris Lattner8b8749f2005-08-17 19:00:20 +00005285 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005286 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005287
Dan Gohman39946102009-01-25 16:29:12 +00005288 // A user can appear in a use list multiple times, and when this
5289 // happens the uses are usually next to each other in the list.
5290 // To help reduce the number of CSE recomputations, process all
5291 // the uses of this user that we can find this way.
5292 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005293 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005294 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005295 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005296 } while (UI != UE && *UI == User);
5297
5298 // Now that we have modified User, add it back to the CSE maps. If it
5299 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005300 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner8b52f212005-08-26 18:36:28 +00005301 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005302
5303 // If we just RAUW'd the root, take note.
5304 if (FromN == getRoot())
5305 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005306}
5307
5308/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5309/// This can cause recursive merging of nodes in the DAG.
5310///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005311/// This version assumes that for each value of From, there is a
5312/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005313///
Chris Lattner1e111c72005-09-07 05:37:01 +00005314void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005315 DAGUpdateListener *UpdateListener) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005316#ifndef NDEBUG
5317 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5318 assert((!From->hasAnyUseOfValue(i) ||
5319 From->getValueType(i) == To->getValueType(i)) &&
5320 "Cannot use this version of ReplaceAllUsesWith!");
5321#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005322
5323 // Handle the trivial case.
5324 if (From == To)
5325 return;
5326
Dan Gohmandbe664a2009-01-19 21:44:21 +00005327 // Iterate over just the existing users of From. See the comments in
5328 // the ReplaceAllUsesWith above.
5329 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005330 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005331 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005332 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005333
Chris Lattner8b52f212005-08-26 18:36:28 +00005334 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005335 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005336
Dan Gohman39946102009-01-25 16:29:12 +00005337 // A user can appear in a use list multiple times, and when this
5338 // happens the uses are usually next to each other in the list.
5339 // To help reduce the number of CSE recomputations, process all
5340 // the uses of this user that we can find this way.
5341 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005342 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005343 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005344 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005345 } while (UI != UE && *UI == User);
5346
5347 // Now that we have modified User, add it back to the CSE maps. If it
5348 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005349 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005350 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005351
5352 // If we just RAUW'd the root, take note.
5353 if (From == getRoot().getNode())
5354 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005355}
5356
Chris Lattner8b52f212005-08-26 18:36:28 +00005357/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5358/// This can cause recursive merging of nodes in the DAG.
5359///
5360/// This version can replace From with any result values. To must match the
5361/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00005362void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00005363 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005364 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005365 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00005366 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005367
Dan Gohmandbe664a2009-01-19 21:44:21 +00005368 // Iterate over just the existing users of From. See the comments in
5369 // the ReplaceAllUsesWith above.
5370 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005371 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005372 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005373 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005374
Chris Lattner7b2880c2005-08-24 22:44:39 +00005375 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005376 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005377
Dan Gohman39946102009-01-25 16:29:12 +00005378 // A user can appear in a use list multiple times, and when this
5379 // happens the uses are usually next to each other in the list.
5380 // To help reduce the number of CSE recomputations, process all
5381 // the uses of this user that we can find this way.
5382 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005383 SDUse &Use = UI.getUse();
5384 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005385 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005386 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005387 } while (UI != UE && *UI == User);
5388
5389 // Now that we have modified User, add it back to the CSE maps. If it
5390 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005391 AddModifiedNodeToCSEMaps(User, &Listener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005392 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005393
5394 // If we just RAUW'd the root, take note.
5395 if (From == getRoot().getNode())
5396 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005397}
5398
Chris Lattner012f2412006-02-17 21:58:01 +00005399/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005400/// uses of other values produced by From.getNode() alone. The Deleted
5401/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005402void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005403 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005404 // Handle the really simple, really trivial case efficiently.
5405 if (From == To) return;
5406
Chris Lattner012f2412006-02-17 21:58:01 +00005407 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005408 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005409 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005410 return;
5411 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005412
Dan Gohman39946102009-01-25 16:29:12 +00005413 // Iterate over just the existing users of From. See the comments in
5414 // the ReplaceAllUsesWith above.
5415 SDNode::use_iterator UI = From.getNode()->use_begin(),
5416 UE = From.getNode()->use_end();
Dan Gohmana72d2a22010-03-03 21:33:37 +00005417 RAUWUpdateListener Listener(UpdateListener, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005418 while (UI != UE) {
5419 SDNode *User = *UI;
5420 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005421
Dan Gohman39946102009-01-25 16:29:12 +00005422 // A user can appear in a use list multiple times, and when this
5423 // happens the uses are usually next to each other in the list.
5424 // To help reduce the number of CSE recomputations, process all
5425 // the uses of this user that we can find this way.
5426 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005427 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005428
5429 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005430 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005431 ++UI;
5432 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005433 }
Dan Gohman39946102009-01-25 16:29:12 +00005434
5435 // If this node hasn't been modified yet, it's still in the CSE maps,
5436 // so remove its old self from the CSE maps.
5437 if (!UserRemovedFromCSEMaps) {
5438 RemoveNodeFromCSEMaps(User);
5439 UserRemovedFromCSEMaps = true;
5440 }
5441
5442 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005443 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005444 } while (UI != UE && *UI == User);
5445
5446 // We are iterating over all uses of the From node, so if a use
5447 // doesn't use the specific value, no changes are made.
5448 if (!UserRemovedFromCSEMaps)
5449 continue;
5450
Chris Lattner01d029b2007-10-15 06:10:22 +00005451 // Now that we have modified User, add it back to the CSE maps. If it
5452 // already exists there, recursively merge the results together.
Dan Gohmana72d2a22010-03-03 21:33:37 +00005453 AddModifiedNodeToCSEMaps(User, &Listener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005454 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005455
5456 // If we just RAUW'd the root, take note.
5457 if (From == getRoot())
5458 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005459}
5460
Dan Gohman39946102009-01-25 16:29:12 +00005461namespace {
5462 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5463 /// to record information about a use.
5464 struct UseMemo {
5465 SDNode *User;
5466 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005467 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005468 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005469
5470 /// operator< - Sort Memos by User.
5471 bool operator<(const UseMemo &L, const UseMemo &R) {
5472 return (intptr_t)L.User < (intptr_t)R.User;
5473 }
Dan Gohman39946102009-01-25 16:29:12 +00005474}
5475
Dan Gohmane8be6c62008-07-17 19:10:17 +00005476/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005477/// uses of other values produced by From.getNode() alone. The same value
5478/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005479/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005480void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5481 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005482 unsigned Num,
5483 DAGUpdateListener *UpdateListener){
5484 // Handle the simple, trivial case efficiently.
5485 if (Num == 1)
5486 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5487
Dan Gohman39946102009-01-25 16:29:12 +00005488 // Read up all the uses and make records of them. This helps
5489 // processing new uses that are introduced during the
5490 // replacement process.
5491 SmallVector<UseMemo, 4> Uses;
5492 for (unsigned i = 0; i != Num; ++i) {
5493 unsigned FromResNo = From[i].getResNo();
5494 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005495 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005496 E = FromNode->use_end(); UI != E; ++UI) {
5497 SDUse &Use = UI.getUse();
5498 if (Use.getResNo() == FromResNo) {
5499 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005500 Uses.push_back(Memo);
5501 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005502 }
Dan Gohman39946102009-01-25 16:29:12 +00005503 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005504
Dan Gohmane7852d02009-01-26 04:35:06 +00005505 // Sort the uses, so that all the uses from a given User are together.
5506 std::sort(Uses.begin(), Uses.end());
5507
Dan Gohman39946102009-01-25 16:29:12 +00005508 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5509 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005510 // We know that this user uses some value of From. If it is the right
5511 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005512 SDNode *User = Uses[UseIndex].User;
5513
5514 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005515 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005516
Dan Gohmane7852d02009-01-26 04:35:06 +00005517 // The Uses array is sorted, so all the uses for a given User
5518 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005519 // To help reduce the number of CSE recomputations, process all
5520 // the uses of this user that we can find this way.
5521 do {
5522 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005523 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005524 ++UseIndex;
5525
Dan Gohmane7852d02009-01-26 04:35:06 +00005526 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005527 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5528
Dan Gohmane8be6c62008-07-17 19:10:17 +00005529 // Now that we have modified User, add it back to the CSE maps. If it
5530 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005531 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005532 }
5533}
5534
Evan Chenge6f35d82006-08-01 08:20:41 +00005535/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005536/// based on their topological order. It returns the maximum id and a vector
5537/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005538unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005539
Dan Gohmanf06c8352008-09-30 18:30:35 +00005540 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005541
Dan Gohmanf06c8352008-09-30 18:30:35 +00005542 // SortedPos tracks the progress of the algorithm. Nodes before it are
5543 // sorted, nodes after it are unsorted. When the algorithm completes
5544 // it is at the end of the list.
5545 allnodes_iterator SortedPos = allnodes_begin();
5546
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005547 // Visit all the nodes. Move nodes with no operands to the front of
5548 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005549 // operand count. Before we do this, the Node Id fields of the nodes
5550 // may contain arbitrary values. After, the Node Id fields for nodes
5551 // before SortedPos will contain the topological sort index, and the
5552 // Node Id fields for nodes At SortedPos and after will contain the
5553 // count of outstanding operands.
5554 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5555 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005556 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005557 unsigned Degree = N->getNumOperands();
5558 if (Degree == 0) {
5559 // A node with no uses, add it to the result array immediately.
5560 N->setNodeId(DAGSize++);
5561 allnodes_iterator Q = N;
5562 if (Q != SortedPos)
5563 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005564 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005565 ++SortedPos;
5566 } else {
5567 // Temporarily use the Node Id as scratch space for the degree count.
5568 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005569 }
5570 }
5571
Dan Gohmanf06c8352008-09-30 18:30:35 +00005572 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5573 // such that by the time the end is reached all nodes will be sorted.
5574 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5575 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005576 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005577 // N is in sorted position, so all its uses have one less operand
5578 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005579 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5580 UI != UE; ++UI) {
5581 SDNode *P = *UI;
5582 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005583 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005584 --Degree;
5585 if (Degree == 0) {
5586 // All of P's operands are sorted, so P may sorted now.
5587 P->setNodeId(DAGSize++);
5588 if (P != SortedPos)
5589 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005590 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005591 ++SortedPos;
5592 } else {
5593 // Update P's outstanding operand count.
5594 P->setNodeId(Degree);
5595 }
5596 }
David Greene221925e2010-01-20 00:59:23 +00005597 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005598#ifndef NDEBUG
5599 SDNode *S = ++I;
5600 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005601 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005602#endif
5603 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005604 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005605 }
5606
5607 assert(SortedPos == AllNodes.end() &&
5608 "Topological sort incomplete!");
5609 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5610 "First node in topological sort is not the entry token!");
5611 assert(AllNodes.front().getNodeId() == 0 &&
5612 "First node in topological sort has non-zero id!");
5613 assert(AllNodes.front().getNumOperands() == 0 &&
5614 "First node in topological sort has operands!");
5615 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5616 "Last node in topologic sort has unexpected id!");
5617 assert(AllNodes.back().use_empty() &&
5618 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005619 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005620 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005621}
5622
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005623/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005624void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005625 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005626 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005627}
Evan Chenge6f35d82006-08-01 08:20:41 +00005628
Bill Wendling0777e922009-12-21 21:59:52 +00005629/// GetOrdering - Get the order for the SDNode.
5630unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5631 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005632 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005633}
5634
Evan Chengbfcb3052010-03-25 01:38:16 +00005635/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5636/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005637void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5638 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005639 if (SD)
5640 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005641}
Evan Cheng091cba12006-07-27 06:39:06 +00005642
Devang Patela2e868d2011-01-25 23:27:42 +00005643/// TransferDbgValues - Transfer SDDbgValues.
5644void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5645 if (From == To || !From.getNode()->getHasDebugValue())
5646 return;
5647 SDNode *FromNode = From.getNode();
5648 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005649 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005650 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005651 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005652 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005653 SDDbgValue *Dbg = *I;
5654 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5655 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5656 Dbg->getOffset(), Dbg->getDebugLoc(),
5657 Dbg->getOrder());
5658 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005659 }
5660 }
Devang Patela778f5c2011-02-18 22:43:42 +00005661 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5662 E = ClonedDVs.end(); I != E; ++I)
5663 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005664}
5665
Jim Laskey58b968b2005-08-17 20:08:02 +00005666//===----------------------------------------------------------------------===//
5667// SDNode Class
5668//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005669
Chris Lattner48b85922007-02-04 02:41:42 +00005670HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005671 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005672}
5673
Devang Patel0d881da2010-07-06 22:08:15 +00005674GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5675 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005676 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005677 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005678 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005679}
Chris Lattner48b85922007-02-04 02:41:42 +00005680
Owen Andersone50ed302009-08-10 22:56:29 +00005681MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005682 MachineMemOperand *mmo)
5683 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005684 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005685 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005686 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005687 assert(isNonTemporal() == MMO->isNonTemporal() &&
5688 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005689 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005690}
5691
Scott Michelfdc40a02009-02-17 22:15:04 +00005692MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005693 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005694 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005695 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005696 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005697 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005698 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005699 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5700 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005701}
5702
Jim Laskey583bd472006-10-27 23:46:08 +00005703/// Profile - Gather unique data for the node.
5704///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005705void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005706 AddNodeIDNode(ID, this);
5707}
5708
Owen Andersond8110fb2009-08-25 22:27:22 +00005709namespace {
5710 struct EVTArray {
5711 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005712
Owen Andersond8110fb2009-08-25 22:27:22 +00005713 EVTArray() {
5714 VTs.reserve(MVT::LAST_VALUETYPE);
5715 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5716 VTs.push_back(MVT((MVT::SimpleValueType)i));
5717 }
5718 };
5719}
5720
Owen Andersone50ed302009-08-10 22:56:29 +00005721static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005722static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005723static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005724
Chris Lattnera3255112005-11-08 23:30:28 +00005725/// getValueTypeList - Return a pointer to the specified value type.
5726///
Owen Andersone50ed302009-08-10 22:56:29 +00005727const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005728 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005729 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005730 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005731 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005732 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005733 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005734 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005735 }
Chris Lattnera3255112005-11-08 23:30:28 +00005736}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005737
Chris Lattner5c884562005-01-12 18:37:47 +00005738/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5739/// indicated value. This method ignores uses of other values defined by this
5740/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005741bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005742 assert(Value < getNumValues() && "Bad value!");
5743
Roman Levensteindc1adac2008-04-07 10:06:32 +00005744 // TODO: Only iterate over uses of a given value of the node
5745 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005746 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005747 if (NUses == 0)
5748 return false;
5749 --NUses;
5750 }
Chris Lattner5c884562005-01-12 18:37:47 +00005751 }
5752
5753 // Found exactly the right number of uses?
5754 return NUses == 0;
5755}
5756
5757
Evan Cheng33d55952007-08-02 05:29:38 +00005758/// hasAnyUseOfValue - Return true if there are any use of the indicated
5759/// value. This method ignores uses of other values defined by this operation.
5760bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5761 assert(Value < getNumValues() && "Bad value!");
5762
Dan Gohman1373c1c2008-07-09 22:39:01 +00005763 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005764 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005765 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005766
5767 return false;
5768}
5769
5770
Dan Gohman2a629952008-07-27 18:06:42 +00005771/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005772///
Dan Gohman2a629952008-07-27 18:06:42 +00005773bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005774 bool Seen = false;
5775 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005776 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005777 if (User == this)
5778 Seen = true;
5779 else
5780 return false;
5781 }
5782
5783 return Seen;
5784}
5785
Evan Chenge6e97e62006-11-03 07:31:32 +00005786/// isOperand - Return true if this node is an operand of N.
5787///
Dan Gohman475871a2008-07-27 21:46:04 +00005788bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005789 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5790 if (*this == N->getOperand(i))
5791 return true;
5792 return false;
5793}
5794
Evan Cheng917be682008-03-04 00:41:45 +00005795bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005796 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005797 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005798 return true;
5799 return false;
5800}
Evan Cheng4ee62112006-02-05 06:29:23 +00005801
Chris Lattner572dee72008-01-16 05:49:24 +00005802/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005803/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005804/// side-effecting instructions on any chain path. In practice, this looks
5805/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005806/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005807bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005808 unsigned Depth) const {
5809 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005810
Chris Lattner572dee72008-01-16 05:49:24 +00005811 // Don't search too deeply, we just want to be able to see through
5812 // TokenFactor's etc.
5813 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005814
Chris Lattner572dee72008-01-16 05:49:24 +00005815 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005816 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005817 if (getOpcode() == ISD::TokenFactor) {
5818 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005819 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5820 return false;
5821 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005823
Chris Lattner572dee72008-01-16 05:49:24 +00005824 // Loads don't have side effects, look through them.
5825 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5826 if (!Ld->isVolatile())
5827 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5828 }
5829 return false;
5830}
5831
Lang Hames944520f2011-07-07 04:31:51 +00005832/// hasPredecessor - Return true if N is a predecessor of this node.
5833/// N is either an operand of this node, or can be reached by recursively
5834/// traversing up the operands.
5835/// NOTE: This is an expensive method. Use it carefully.
5836bool SDNode::hasPredecessor(const SDNode *N) const {
5837 SmallPtrSet<const SDNode *, 32> Visited;
5838 SmallVector<const SDNode *, 16> Worklist;
5839 return hasPredecessorHelper(N, Visited, Worklist);
5840}
Dan Gohman6688d612009-10-28 03:44:30 +00005841
Lang Hames944520f2011-07-07 04:31:51 +00005842bool SDNode::hasPredecessorHelper(const SDNode *N,
5843 SmallPtrSet<const SDNode *, 32> &Visited,
5844 SmallVector<const SDNode *, 16> &Worklist) const {
5845 if (Visited.empty()) {
5846 Worklist.push_back(this);
5847 } else {
5848 // Take a look in the visited set. If we've already encountered this node
5849 // we needn't search further.
5850 if (Visited.count(N))
5851 return true;
5852 }
5853
5854 // Haven't visited N yet. Continue the search.
5855 while (!Worklist.empty()) {
5856 const SDNode *M = Worklist.pop_back_val();
5857 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
5858 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00005859 if (Visited.insert(Op))
5860 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00005861 if (Op == N)
5862 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00005863 }
Lang Hames944520f2011-07-07 04:31:51 +00005864 }
Dan Gohman6688d612009-10-28 03:44:30 +00005865
5866 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005867}
5868
Evan Chengc5484282006-10-04 00:56:09 +00005869uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5870 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005871 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005872}
5873
Mon P Wangcd6e7252009-11-30 02:42:02 +00005874SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5875 assert(N->getNumValues() == 1 &&
5876 "Can't unroll a vector with multiple results!");
5877
5878 EVT VT = N->getValueType(0);
5879 unsigned NE = VT.getVectorNumElements();
5880 EVT EltVT = VT.getVectorElementType();
5881 DebugLoc dl = N->getDebugLoc();
5882
5883 SmallVector<SDValue, 8> Scalars;
5884 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5885
5886 // If ResNE is 0, fully unroll the vector op.
5887 if (ResNE == 0)
5888 ResNE = NE;
5889 else if (NE > ResNE)
5890 NE = ResNE;
5891
5892 unsigned i;
5893 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00005894 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00005895 SDValue Operand = N->getOperand(j);
5896 EVT OperandVT = Operand.getValueType();
5897 if (OperandVT.isVector()) {
5898 // A vector operand; extract a single element.
5899 EVT OperandEltVT = OperandVT.getVectorElementType();
5900 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5901 OperandEltVT,
5902 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00005903 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005904 } else {
5905 // A scalar operand; just use it as is.
5906 Operands[j] = Operand;
5907 }
5908 }
5909
5910 switch (N->getOpcode()) {
5911 default:
5912 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5913 &Operands[0], Operands.size()));
5914 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00005915 case ISD::VSELECT:
5916 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
5917 &Operands[0], Operands.size()));
5918 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00005919 case ISD::SHL:
5920 case ISD::SRA:
5921 case ISD::SRL:
5922 case ISD::ROTL:
5923 case ISD::ROTR:
5924 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00005925 getShiftAmountOperand(Operands[0].getValueType(),
5926 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00005927 break;
Dan Gohmand1996362010-01-09 02:13:55 +00005928 case ISD::SIGN_EXTEND_INREG:
5929 case ISD::FP_ROUND_INREG: {
5930 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
5931 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5932 Operands[0],
5933 getValueType(ExtVT)));
5934 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00005935 }
5936 }
5937
5938 for (; i < ResNE; ++i)
5939 Scalars.push_back(getUNDEF(EltVT));
5940
5941 return getNode(ISD::BUILD_VECTOR, dl,
5942 EVT::getVectorVT(*getContext(), EltVT, ResNE),
5943 &Scalars[0], Scalars.size());
5944}
5945
Evan Cheng64fa4a92009-12-09 01:36:00 +00005946
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005947/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
5948/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00005949/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005950bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00005951 unsigned Bytes, int Dist) const {
5952 if (LD->getChain() != Base->getChain())
5953 return false;
5954 EVT VT = LD->getValueType(0);
5955 if (VT.getSizeInBits() / 8 != Bytes)
5956 return false;
5957
5958 SDValue Loc = LD->getOperand(1);
5959 SDValue BaseLoc = Base->getOperand(1);
5960 if (Loc.getOpcode() == ISD::FrameIndex) {
5961 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5962 return false;
5963 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
5964 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5965 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5966 int FS = MFI->getObjectSize(FI);
5967 int BFS = MFI->getObjectSize(BFI);
5968 if (FS != BFS || FS != (int)Bytes) return false;
5969 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
5970 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00005971
5972 // Handle X+C
5973 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
5974 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
5975 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00005976
Dan Gohman46510a72010-04-15 01:51:59 +00005977 const GlobalValue *GV1 = NULL;
5978 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00005979 int64_t Offset1 = 0;
5980 int64_t Offset2 = 0;
5981 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
5982 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
5983 if (isGA1 && isGA2 && GV1 == GV2)
5984 return Offset1 == (Offset2 + Dist*Bytes);
5985 return false;
5986}
5987
5988
Evan Chengf2dc5c72009-12-09 01:04:59 +00005989/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
5990/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00005991unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00005992 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00005993 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00005994 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00005995 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00005996 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00005997 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00005998 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
5999 TLI.getTargetData());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006000 unsigned AlignBits = KnownZero.countTrailingOnes();
6001 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6002 if (Align)
6003 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006004 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006005
Evan Chengf2dc5c72009-12-09 01:04:59 +00006006 // If this is a direct reference to a stack slot, use information about the
6007 // stack slot's alignment.
6008 int FrameIdx = 1 << 31;
6009 int64_t FrameOffset = 0;
6010 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6011 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006012 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006013 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006014 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006015 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6016 FrameOffset = Ptr.getConstantOperandVal(1);
6017 }
6018
6019 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006020 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006021 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6022 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006023 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006024 }
6025
6026 return 0;
6027}
6028
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006029// getAddressSpace - Return the address space this GlobalAddress belongs to.
6030unsigned GlobalAddressSDNode::getAddressSpace() const {
6031 return getGlobal()->getType()->getAddressSpace();
6032}
6033
6034
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006035Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006036 if (isMachineConstantPoolEntry())
6037 return Val.MachineCPVal->getType();
6038 return Val.ConstVal->getType();
6039}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006040
Bob Wilson24e338e2009-03-02 23:24:16 +00006041bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6042 APInt &SplatUndef,
6043 unsigned &SplatBitSize,
6044 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006045 unsigned MinSplatBits,
6046 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006047 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006048 assert(VT.isVector() && "Expected a vector type");
6049 unsigned sz = VT.getSizeInBits();
6050 if (MinSplatBits > sz)
6051 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006052
Bob Wilson24e338e2009-03-02 23:24:16 +00006053 SplatValue = APInt(sz, 0);
6054 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006055
Bob Wilson24e338e2009-03-02 23:24:16 +00006056 // Get the bits. Bits with undefined values (when the corresponding element
6057 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6058 // in SplatValue. If any of the values are not constant, give up and return
6059 // false.
6060 unsigned int nOps = getNumOperands();
6061 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6062 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006063
6064 for (unsigned j = 0; j < nOps; ++j) {
6065 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006066 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006067 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006068
Bob Wilson24e338e2009-03-02 23:24:16 +00006069 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006070 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006071 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006072 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006073 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006074 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006075 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006076 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006077 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006078 }
6079
Bob Wilson24e338e2009-03-02 23:24:16 +00006080 // The build_vector is all constants or undefs. Find the smallest element
6081 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006082
Bob Wilson24e338e2009-03-02 23:24:16 +00006083 HasAnyUndefs = (SplatUndef != 0);
6084 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006085
Bob Wilson24e338e2009-03-02 23:24:16 +00006086 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006087 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6088 APInt LowValue = SplatValue.trunc(HalfSize);
6089 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6090 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006091
Bob Wilson24e338e2009-03-02 23:24:16 +00006092 // If the two halves do not match (ignoring undef bits), stop here.
6093 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6094 MinSplatBits > HalfSize)
6095 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006096
Bob Wilson24e338e2009-03-02 23:24:16 +00006097 SplatValue = HighValue | LowValue;
6098 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006099
Bob Wilson24e338e2009-03-02 23:24:16 +00006100 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006101 }
6102
Bob Wilson24e338e2009-03-02 23:24:16 +00006103 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006104 return true;
6105}
Nate Begeman9008ca62009-04-27 18:41:29 +00006106
Owen Andersone50ed302009-08-10 22:56:29 +00006107bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006108 // Find the first non-undef value in the shuffle mask.
6109 unsigned i, e;
6110 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6111 /* search */;
6112
Nate Begemana6415752009-04-29 18:13:31 +00006113 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006114
Nate Begeman5a5ca152009-04-29 05:20:52 +00006115 // Make sure all remaining elements are either undef or the same as the first
6116 // non-undef value.
6117 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006118 if (Mask[i] >= 0 && Mask[i] != Idx)
6119 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006120 return true;
6121}
David Greenecf495bc2010-01-20 20:13:31 +00006122
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006123#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006124static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006125 SmallPtrSet<const SDNode*, 32> &Visited,
6126 SmallPtrSet<const SDNode*, 32> &Checked) {
6127 // If this node has already been checked, don't check it again.
6128 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006129 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006130
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006131 // If a node has already been visited on this depth-first walk, reject it as
6132 // a cycle.
6133 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006134 dbgs() << "Offending node:\n";
6135 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006136 errs() << "Detected cycle in SelectionDAG\n";
6137 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006138 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006139
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006140 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6141 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006142
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006143 Checked.insert(N);
6144 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006145}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006146#endif
David Greenecf495bc2010-01-20 20:13:31 +00006147
6148void llvm::checkForCycles(const llvm::SDNode *N) {
6149#ifdef XDEBUG
6150 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006151 SmallPtrSet<const SDNode*, 32> visited;
6152 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006153 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006154#endif
6155}
6156
6157void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6158 checkForCycles(DAG->getRoot().getNode());
6159}