blob: 15235c8ac307737bbd6867e6083d85d24dbbef39 [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"
Evan Chenga8efe282010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "SDNodeOrdering.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringExtras.h"
Chandler Carruthbe049292013-01-07 03:08:10 +000022#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Analysis/ValueTracking.h"
24#include "llvm/Assembly/Writer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineModuleInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000029#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/DerivedTypes.h"
34#include "llvm/IR/Function.h"
35#include "llvm/IR/GlobalAlias.h"
36#include "llvm/IR/GlobalVariable.h"
37#include "llvm/IR/Intrinsics.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000038#include "llvm/Support/CommandLine.h"
David Greene55d146e2010-01-05 01:24:36 +000039#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000041#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000042#include "llvm/Support/MathExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000043#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/Support/raw_ostream.h"
45#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetIntrinsicInfo.h"
47#include "llvm/Target/TargetLowering.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetOptions.h"
50#include "llvm/Target/TargetRegisterInfo.h"
51#include "llvm/Target/TargetSelectionDAGInfo.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000052#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000053#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000054using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000055
Chris Lattner0b3e5252006-08-15 19:11:05 +000056/// makeVTList - Return an instance of the SDVTList struct initialized with the
57/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000058static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000059 SDVTList Res = {VTs, NumVTs};
60 return Res;
61}
62
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000063// Default null implementations of the callbacks.
64void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
65void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000066
Jim Laskey58b968b2005-08-17 20:08:02 +000067//===----------------------------------------------------------------------===//
68// ConstantFPSDNode Class
69//===----------------------------------------------------------------------===//
70
71/// isExactlyValue - We don't rely on operator== working on double values, as
72/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
73/// As such, this method can be used to do an exact bit-for-bit comparison of
74/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000075bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000076 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000077}
78
Owen Andersone50ed302009-08-10 22:56:29 +000079bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000080 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000081 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000082
Dale Johannesenf04afdb2007-08-30 00:23:21 +000083 // convert modifies in place, so make a copy.
84 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000085 bool losesInfo;
Tim Northover0a29cb02013-01-22 09:46:31 +000086 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
87 APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000088 &losesInfo);
89 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000090}
91
Jim Laskey58b968b2005-08-17 20:08:02 +000092//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000093// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000094//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000095
Evan Chenga8df1662006-03-27 06:58:47 +000096/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000097/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000098bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000099 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000100 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000101 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000102
Evan Chenga8df1662006-03-27 06:58:47 +0000103 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000104
Chris Lattner61d43992006-03-25 22:57:01 +0000105 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000106
Chris Lattner61d43992006-03-25 22:57:01 +0000107 // Skip over all of the undef values.
108 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
109 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Chris Lattner61d43992006-03-25 22:57:01 +0000111 // Do not accept an all-undef vector.
112 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000113
Chris Lattner61d43992006-03-25 22:57:01 +0000114 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000115 // elements. We have to be a bit careful here, as the type of the constant
116 // may not be the same as the type of the vector elements due to type
117 // legalization (the elements are promoted to a legal type for the target and
118 // a vector of a type may be legal when the base element type is not).
119 // We only want to check enough bits to cover the vector elements, because
120 // we care if the resultant vector is all ones, not whether the individual
121 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000123 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000124 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
125 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000126 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000127 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
128 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000129 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000130 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000131 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000132
Chris Lattner61d43992006-03-25 22:57:01 +0000133 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000134 // undefs. Even with the above element type twiddling, this should be OK, as
135 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000136 for (++i; i != e; ++i)
137 if (N->getOperand(i) != NotZero &&
138 N->getOperand(i).getOpcode() != ISD::UNDEF)
139 return false;
140 return true;
141}
142
143
Evan Cheng4a147842006-03-26 09:50:58 +0000144/// isBuildVectorAllZeros - Return true if the specified node is a
145/// BUILD_VECTOR where all of the elements are 0 or undef.
146bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000147 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000148 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000149 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000150
Evan Cheng4a147842006-03-26 09:50:58 +0000151 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000152
Evan Chenga8df1662006-03-27 06:58:47 +0000153 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000154
Evan Chenga8df1662006-03-27 06:58:47 +0000155 // Skip over all of the undef values.
156 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
157 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000158
Evan Chenga8df1662006-03-27 06:58:47 +0000159 // Do not accept an all-undef vector.
160 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000161
Dan Gohman68f32cb2009-06-04 16:49:15 +0000162 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000163 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue Zero = N->getOperand(i);
Jakub Staszak554c6762012-09-30 21:24:57 +0000165 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
166 if (!CN->isNullValue())
Evan Chenga8df1662006-03-27 06:58:47 +0000167 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000168 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
169 if (!CFPN->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000170 return false;
171 } else
172 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000173
Dan Gohman68f32cb2009-06-04 16:49:15 +0000174 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000175 // undefs.
176 for (++i; i != e; ++i)
177 if (N->getOperand(i) != Zero &&
178 N->getOperand(i).getOpcode() != ISD::UNDEF)
179 return false;
180 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000181}
182
Evan Chengefec7512008-02-18 23:04:32 +0000183/// isScalarToVector - Return true if the specified node is a
184/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
185/// element is not an undef.
186bool ISD::isScalarToVector(const SDNode *N) {
187 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
188 return true;
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR)
191 return false;
192 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
193 return false;
194 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000195 if (NumElems == 1)
196 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000197 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000199 if (V.getOpcode() != ISD::UNDEF)
200 return false;
201 }
202 return true;
203}
204
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000205/// allOperandsUndef - Return true if the node has at least one operand
206/// and all operands of the specified node are ISD::UNDEF.
207bool ISD::allOperandsUndef(const SDNode *N) {
208 // Return false if the node has no operands.
209 // This is "logically inconsistent" with the definition of "all" but
210 // is probably the desired behavior.
211 if (N->getNumOperands() == 0)
212 return false;
213
214 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
215 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
216 return false;
217
218 return true;
219}
220
Chris Lattnerc3aae252005-01-07 07:46:32 +0000221/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
222/// when given the operation for (X op Y).
223ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
224 // To perform this operation, we just need to swap the L and G bits of the
225 // operation.
226 unsigned OldL = (Operation >> 2) & 1;
227 unsigned OldG = (Operation >> 1) & 1;
228 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
229 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000230 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000231}
232
233/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
234/// 'op' is a valid SetCC operation.
235ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
236 unsigned Operation = Op;
237 if (isInteger)
238 Operation ^= 7; // Flip L, G, E bits, but not U.
239 else
240 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000241
Chris Lattnerc3aae252005-01-07 07:46:32 +0000242 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000243 Operation &= ~8; // Don't let N and U bits get set.
244
Chris Lattnerc3aae252005-01-07 07:46:32 +0000245 return ISD::CondCode(Operation);
246}
247
248
249/// isSignedOp - For an integer comparison, return 1 if the comparison is a
250/// signed operation and 2 if the result is an unsigned comparison. Return zero
251/// if the operation does not depend on the sign of the input (setne and seteq).
252static int isSignedOp(ISD::CondCode Opcode) {
253 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000254 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000255 case ISD::SETEQ:
256 case ISD::SETNE: return 0;
257 case ISD::SETLT:
258 case ISD::SETLE:
259 case ISD::SETGT:
260 case ISD::SETGE: return 1;
261 case ISD::SETULT:
262 case ISD::SETULE:
263 case ISD::SETUGT:
264 case ISD::SETUGE: return 2;
265 }
266}
267
268/// getSetCCOrOperation - Return the result of a logical OR between different
269/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
270/// returns SETCC_INVALID if it is not possible to represent the resultant
271/// comparison.
272ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
273 bool isInteger) {
274 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
275 // Cannot fold a signed integer setcc with an unsigned integer setcc.
276 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000279
Chris Lattnerc3aae252005-01-07 07:46:32 +0000280 // If the N and U bits get set then the resultant comparison DOES suddenly
281 // care about orderedness, and is true when ordered.
282 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000283 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Chris Lattnere41102b2006-05-12 17:03:46 +0000285 // Canonicalize illegal integer setcc's.
286 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
287 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000288
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289 return ISD::CondCode(Op);
290}
291
292/// getSetCCAndOperation - Return the result of a logical AND between different
293/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
294/// function returns zero if it is not possible to represent the resultant
295/// comparison.
296ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
297 bool isInteger) {
298 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
299 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000300 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301
302 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000303 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000304
Chris Lattnera83385f2006-04-27 05:01:07 +0000305 // Canonicalize illegal integer setcc's.
306 if (isInteger) {
307 switch (Result) {
308 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000309 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000310 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000311 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
312 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
313 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000314 }
315 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000316
Chris Lattnera83385f2006-04-27 05:01:07 +0000317 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000318}
319
Jim Laskey58b968b2005-08-17 20:08:02 +0000320//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000321// SDNode Profile Support
322//===----------------------------------------------------------------------===//
323
Jim Laskeydef69b92006-10-27 23:52:51 +0000324/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
325///
Jim Laskey583bd472006-10-27 23:46:08 +0000326static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
327 ID.AddInteger(OpC);
328}
329
330/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
331/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000332static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000333 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000334}
335
Jim Laskeydef69b92006-10-27 23:52:51 +0000336/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
337///
Jim Laskey583bd472006-10-27 23:46:08 +0000338static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000339 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000340 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000341 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000342 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000343 }
Jim Laskey583bd472006-10-27 23:46:08 +0000344}
345
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000346/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
347///
348static void AddNodeIDOperands(FoldingSetNodeID &ID,
349 const SDUse *Ops, unsigned NumOps) {
350 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000351 ID.AddPointer(Ops->getNode());
352 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000353 }
354}
355
Jim Laskey583bd472006-10-27 23:46:08 +0000356static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000357 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000358 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000359 AddNodeIDOpcode(ID, OpC);
360 AddNodeIDValueTypes(ID, VTList);
361 AddNodeIDOperands(ID, OpList, N);
362}
363
Duncan Sands0dc40452008-10-27 15:30:53 +0000364/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
365/// the NodeID data.
366static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000367 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000368 case ISD::TargetExternalSymbol:
369 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000370 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000371 default: break; // Normal nodes don't need extra info.
372 case ISD::TargetConstant:
373 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000374 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000375 break;
376 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000377 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000378 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000379 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000380 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000381 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000382 case ISD::GlobalAddress:
383 case ISD::TargetGlobalTLSAddress:
384 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000385 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000386 ID.AddPointer(GA->getGlobal());
387 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000388 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000389 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000390 break;
391 }
392 case ISD::BasicBlock:
393 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
394 break;
395 case ISD::Register:
396 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
397 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000398 case ISD::RegisterMask:
399 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
400 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000401 case ISD::SRCVALUE:
402 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
403 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000404 case ISD::FrameIndex:
405 case ISD::TargetFrameIndex:
406 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
407 break;
408 case ISD::JumpTable:
409 case ISD::TargetJumpTable:
410 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000411 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000412 break;
413 case ISD::ConstantPool:
414 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000415 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000416 ID.AddInteger(CP->getAlignment());
417 ID.AddInteger(CP->getOffset());
418 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000419 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000420 else
421 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000422 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000423 break;
424 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000425 case ISD::TargetIndex: {
426 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
427 ID.AddInteger(TI->getIndex());
428 ID.AddInteger(TI->getOffset());
429 ID.AddInteger(TI->getTargetFlags());
430 break;
431 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000432 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000433 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000434 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000435 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000436 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000437 break;
438 }
439 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000440 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000441 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000442 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000443 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000444 break;
445 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000446 case ISD::ATOMIC_CMP_SWAP:
447 case ISD::ATOMIC_SWAP:
448 case ISD::ATOMIC_LOAD_ADD:
449 case ISD::ATOMIC_LOAD_SUB:
450 case ISD::ATOMIC_LOAD_AND:
451 case ISD::ATOMIC_LOAD_OR:
452 case ISD::ATOMIC_LOAD_XOR:
453 case ISD::ATOMIC_LOAD_NAND:
454 case ISD::ATOMIC_LOAD_MIN:
455 case ISD::ATOMIC_LOAD_MAX:
456 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000457 case ISD::ATOMIC_LOAD_UMAX:
458 case ISD::ATOMIC_LOAD:
459 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000460 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000461 ID.AddInteger(AT->getMemoryVT().getRawBits());
462 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000463 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
464 break;
465 }
466 case ISD::PREFETCH: {
467 const MemSDNode *PF = cast<MemSDNode>(N);
468 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000469 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000470 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000471 case ISD::VECTOR_SHUFFLE: {
472 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000473 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000474 i != e; ++i)
475 ID.AddInteger(SVN->getMaskElt(i));
476 break;
477 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000478 case ISD::TargetBlockAddress:
479 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000480 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
481 ID.AddPointer(BA->getBlockAddress());
482 ID.AddInteger(BA->getOffset());
483 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000484 break;
485 }
Mon P Wang28873102008-06-25 08:15:39 +0000486 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000487
488 // Target specific memory nodes could also have address spaces to check.
489 if (N->isTargetMemoryOpcode())
490 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000491}
492
Duncan Sands0dc40452008-10-27 15:30:53 +0000493/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
494/// data.
495static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
496 AddNodeIDOpcode(ID, N->getOpcode());
497 // Add the return value info.
498 AddNodeIDValueTypes(ID, N->getVTList());
499 // Add the operand info.
500 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
501
502 // Handle SDNode leafs with special info.
503 AddNodeIDCustom(ID, N);
504}
505
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000506/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000507/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000508/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000509///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000510static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000511encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000512 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000513 assert((ConvType & 3) == ConvType &&
514 "ConvType may not require more than 2 bits!");
515 assert((AM & 7) == AM &&
516 "AM may not require more than 3 bits!");
517 return ConvType |
518 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000519 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000520 (isNonTemporal << 6) |
521 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000522}
523
Jim Laskey583bd472006-10-27 23:46:08 +0000524//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000525// SelectionDAG Class
526//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000527
Duncan Sands0dc40452008-10-27 15:30:53 +0000528/// doNotCSE - Return true if CSE should not be performed for this node.
529static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000530 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000531 return true; // Never CSE anything that produces a flag.
532
533 switch (N->getOpcode()) {
534 default: break;
535 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000536 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000537 return true; // Never CSE these nodes.
538 }
539
540 // Check that remaining values produced are not flags.
541 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000542 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000543 return true; // Never CSE anything that produces a flag.
544
545 return false;
546}
547
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000548/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000549/// SelectionDAG.
550void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000551 // Create a dummy node (which is not added to allnodes), that adds a reference
552 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000553 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000554
Chris Lattner190a4182006-08-04 17:45:20 +0000555 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000556
Chris Lattner190a4182006-08-04 17:45:20 +0000557 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000558 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000559 if (I->use_empty())
560 DeadNodes.push_back(I);
561
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000562 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000563
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000564 // If the root changed (e.g. it was a dead load, update the root).
565 setRoot(Dummy.getValue());
566}
567
568/// RemoveDeadNodes - This method deletes the unreachable nodes in the
569/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000570void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000571
Chris Lattner190a4182006-08-04 17:45:20 +0000572 // Process the worklist, deleting the nodes and adding their uses to the
573 // worklist.
574 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000575 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000576
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000577 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
578 DUL->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000579
Chris Lattner190a4182006-08-04 17:45:20 +0000580 // Take the node out of the appropriate CSE map.
581 RemoveNodeFromCSEMaps(N);
582
583 // Next, brutally remove the operand list. This is safe to do, as there are
584 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000585 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
586 SDUse &Use = *I++;
587 SDNode *Operand = Use.getNode();
588 Use.set(SDValue());
589
Chris Lattner190a4182006-08-04 17:45:20 +0000590 // Now that we removed this operand, see if there are no uses of it left.
591 if (Operand->use_empty())
592 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000593 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000594
Dan Gohmanc5336122009-01-19 22:39:36 +0000595 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000596 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000597}
598
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000599void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000600 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000601
602 // Create a dummy node that adds a reference to the root node, preventing
603 // it from being deleted. (This matters if the root is an operand of the
604 // dead node.)
605 HandleSDNode Dummy(getRoot());
606
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000607 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000608}
609
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000610void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000611 // First take this out of the appropriate CSE map.
612 RemoveNodeFromCSEMaps(N);
613
Scott Michelfdc40a02009-02-17 22:15:04 +0000614 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000615 // AllNodes list, and delete the node.
616 DeleteNodeNotInCSEMaps(N);
617}
618
619void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000620 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
621 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000622
Dan Gohmanf06c8352008-09-30 18:30:35 +0000623 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000624 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000625
Dan Gohmanc5336122009-01-19 22:39:36 +0000626 DeallocateNode(N);
627}
628
629void SelectionDAG::DeallocateNode(SDNode *N) {
630 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000631 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000632
Dan Gohmanc5336122009-01-19 22:39:36 +0000633 // Set the opcode to DELETED_NODE to help catch bugs when node
634 // memory is reallocated.
635 N->NodeType = ISD::DELETED_NODE;
636
Dan Gohman11467282008-08-26 01:44:34 +0000637 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000638
639 // Remove the ordering of this node.
Bill Wendling187361b2010-01-23 10:26:57 +0000640 Ordering->remove(N);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000641
Evan Chengbfcb3052010-03-25 01:38:16 +0000642 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000643 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000644 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
645 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000646}
647
Chris Lattner149c58c2005-08-16 18:17:10 +0000648/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
649/// correspond to it. This is useful when we're about to delete or repurpose
650/// the node. We don't want future request for structurally identical nodes
651/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000652bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000653 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000654 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000655 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000656 case ISD::CONDCODE:
657 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
658 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000659 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000660 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
661 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000662 case ISD::ExternalSymbol:
663 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000664 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000665 case ISD::TargetExternalSymbol: {
666 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
667 Erased = TargetExternalSymbols.erase(
668 std::pair<std::string,unsigned char>(ESN->getSymbol(),
669 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000670 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000671 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000672 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000673 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000674 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000675 Erased = ExtendedValueTypeNodes.erase(VT);
676 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
678 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000679 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000680 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000681 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000682 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000683 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000684 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
685 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000686 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000687 break;
688 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000689#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000690 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000691 // flag result (which cannot be CSE'd) or is one of the special cases that are
692 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000693 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000694 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000695 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000696 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000697 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000698 }
699#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000700 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000701}
702
Dan Gohman39946102009-01-25 16:29:12 +0000703/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
704/// maps and modified in place. Add it back to the CSE maps, unless an identical
705/// node already exists, in which case transfer all its users to the existing
706/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000707///
Dan Gohman39946102009-01-25 16:29:12 +0000708void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000709SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000710 // For node types that aren't CSE'd, just act as if no identical node
711 // already exists.
712 if (!doNotCSE(N)) {
713 SDNode *Existing = CSEMap.GetOrInsertNode(N);
714 if (Existing != N) {
715 // If there was already an existing matching node, use ReplaceAllUsesWith
716 // to replace the dead one with the existing one. This can cause
717 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000718 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000719
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000720 // N is now dead. Inform the listeners and delete it.
721 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
722 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000723 DeleteNodeNotInCSEMaps(N);
724 return;
725 }
726 }
Evan Cheng71e86852008-07-08 20:06:39 +0000727
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000728 // If the node doesn't already exist, we updated it. Inform listeners.
729 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
730 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000731}
732
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000733/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000734/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000735/// return null, otherwise return a pointer to the slot it would take. If a
736/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000737SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000738 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000739 if (doNotCSE(N))
740 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000741
Dan Gohman475871a2008-07-27 21:46:04 +0000742 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000743 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000744 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000745 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000746 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000747 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000748}
749
750/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000751/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000752/// return null, otherwise return a pointer to the slot it would take. If a
753/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000754SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000755 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000756 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000757 if (doNotCSE(N))
758 return 0;
759
Dan Gohman475871a2008-07-27 21:46:04 +0000760 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000761 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000762 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000763 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000764 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000765 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000766}
767
768
769/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000770/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000771/// return null, otherwise return a pointer to the slot it would take. If a
772/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000773SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000774 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000775 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000776 if (doNotCSE(N))
777 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000778
Jim Laskey583bd472006-10-27 23:46:08 +0000779 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000780 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000781 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000782 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000783 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000784}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000785
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000786#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000787/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
788static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000789 switch (N->getOpcode()) {
790 default:
791 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000792 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000793 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000794 assert(N->getNumValues() == 1 && "Too many results!");
795 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
796 "Wrong return type!");
797 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
798 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
799 "Mismatched operand types!");
800 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
801 "Wrong operand type!");
802 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
803 "Wrong return type size");
804 break;
805 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000806 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000807 assert(N->getNumValues() == 1 && "Too many results!");
808 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000809 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000810 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000811 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000812 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000813 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000814 (EltVT.isInteger() && I->getValueType().isInteger() &&
815 EltVT.bitsLE(I->getValueType()))) &&
816 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000817 assert(I->getValueType() == N->getOperand(0).getValueType() &&
818 "Operands must all have the same type");
819 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000820 break;
821 }
822 }
823}
824
Duncan Sands59d2dad2010-11-20 11:25:00 +0000825/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
826static void VerifySDNode(SDNode *N) {
827 // The SDNode allocators cannot be used to allocate nodes with fields that are
828 // not present in an SDNode!
829 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
830 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
831 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
832 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
833 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
834 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
835 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
836 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
837 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
838 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
839 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
840 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
841 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
842 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
843 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
844 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
845 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
846 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
847 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
848
849 VerifyNodeCommon(N);
850}
851
852/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
853/// invalid.
854static void VerifyMachineNode(SDNode *N) {
855 // The MachineNode allocators cannot be used to allocate nodes with fields
856 // that are not present in a MachineNode!
857 // Currently there are no such nodes.
858
859 VerifyNodeCommon(N);
860}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000861#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000862
Owen Andersone50ed302009-08-10 22:56:29 +0000863/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000864/// given type.
865///
Owen Andersone50ed302009-08-10 22:56:29 +0000866unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000867 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000868 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000869 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000870
Micah Villmow3574eca2012-10-08 16:38:25 +0000871 return TLI.getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000872}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000873
Dale Johannesen92570c42009-02-07 02:15:05 +0000874// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000875SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Dan Gohmanff7a5622010-05-11 17:31:57 +0000876 : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000877 TTI(0), OptLevel(OL), EntryNode(ISD::EntryToken, DebugLoc(),
878 getVTList(MVT::Other)),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000879 Root(getEntryNode()), Ordering(0), UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000880 AllNodes.push_back(&EntryNode);
Bill Wendling187361b2010-01-23 10:26:57 +0000881 Ordering = new SDNodeOrdering();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000882 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000883}
884
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000885void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000886 MF = &mf;
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000887 TTI = tti;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000888 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000889}
890
Chris Lattner78ec3112003-08-11 14:57:33 +0000891SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000892 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000893 allnodes_clear();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000894 delete Ordering;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000895 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000896}
897
898void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000899 assert(&*AllNodes.begin() == &EntryNode);
900 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000901 while (!AllNodes.empty())
902 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000903}
904
Dan Gohman7c3234c2008-08-27 23:52:12 +0000905void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000906 allnodes_clear();
907 OperandAllocator.Reset();
908 CSEMap.clear();
909
910 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000911 ExternalSymbols.clear();
912 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000913 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
914 static_cast<CondCodeSDNode*>(0));
915 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
916 static_cast<SDNode*>(0));
917
Dan Gohmane7852d02009-01-26 04:35:06 +0000918 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000919 AllNodes.push_back(&EntryNode);
920 Root = getEntryNode();
Dan Gohman27445f02010-06-18 15:40:58 +0000921 Ordering->clear();
Evan Cheng31441b72010-03-29 20:48:30 +0000922 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000923}
924
Nadav Rotema3c42f32011-09-27 11:16:47 +0000925SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
926 return VT.bitsGT(Op.getValueType()) ?
927 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
928 getNode(ISD::TRUNCATE, DL, VT, Op);
929}
930
Duncan Sands3a66a682009-10-13 21:04:12 +0000931SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
932 return VT.bitsGT(Op.getValueType()) ?
933 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
934 getNode(ISD::TRUNCATE, DL, VT, Op);
935}
936
937SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
938 return VT.bitsGT(Op.getValueType()) ?
939 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
940 getNode(ISD::TRUNCATE, DL, VT, Op);
941}
942
Owen Andersone50ed302009-08-10 22:56:29 +0000943SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000944 assert(!VT.isVector() &&
945 "getZeroExtendInReg should use the vector element type instead of "
946 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000947 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000948 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
949 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000950 VT.getSizeInBits());
951 return getNode(ISD::AND, DL, Op.getValueType(), Op,
952 getConstant(Imm, Op.getValueType()));
953}
954
Bob Wilson4c245462009-01-22 17:39:32 +0000955/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
956///
Owen Andersone50ed302009-08-10 22:56:29 +0000957SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000958 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000959 SDValue NegOne =
960 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000961 return getNode(ISD::XOR, DL, VT, Val, NegOne);
962}
963
Owen Andersone50ed302009-08-10 22:56:29 +0000964SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000965 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000966 assert((EltVT.getSizeInBits() >= 64 ||
967 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
968 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000969 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000970}
971
Owen Andersone50ed302009-08-10 22:56:29 +0000972SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000973 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000974}
975
Owen Andersone50ed302009-08-10 22:56:29 +0000976SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000977 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000978
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000979 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000980 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000981
Duncan Sands28b77e92011-09-06 19:07:46 +0000982 // In some cases the vector type is legal but the element type is illegal and
983 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
984 // inserted value (the type does not need to match the vector element type).
985 // Any extra bits introduced will be truncated away.
986 if (VT.isVector() && TLI.getTypeAction(*getContext(), EltVT) ==
987 TargetLowering::TypePromoteInteger) {
988 EltVT = TLI.getTypeToTransformTo(*getContext(), EltVT);
989 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
990 Elt = ConstantInt::get(*getContext(), NewVal);
991 }
992
993 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
994 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000995 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000996 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000997 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +0000998 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +0000999 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001000 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001001 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001002 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001003 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001004
Dan Gohman89081322007-12-12 22:21:26 +00001005 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001006 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001007 CSEMap.InsertNode(N, IP);
1008 AllNodes.push_back(N);
1009 }
1010
Dan Gohman475871a2008-07-27 21:46:04 +00001011 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001012 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001013 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001014 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001015 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001016 }
1017 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001018}
1019
Dan Gohman475871a2008-07-27 21:46:04 +00001020SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +00001021 return getConstant(Val, TLI.getPointerTy(), isTarget);
1022}
1023
1024
Owen Andersone50ed302009-08-10 22:56:29 +00001025SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001026 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001027}
1028
Owen Andersone50ed302009-08-10 22:56:29 +00001029SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001030 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001032 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001033
Chris Lattnerd8658612005-02-17 20:17:32 +00001034 // Do the map lookup using the actual bit pattern for the floating point
1035 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1036 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001037 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001038 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001039 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001040 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001041 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001042 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001043 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001044 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001045 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001046
Evan Chengc908dcd2007-06-29 21:36:04 +00001047 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001048 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001049 CSEMap.InsertNode(N, IP);
1050 AllNodes.push_back(N);
1051 }
1052
Dan Gohman475871a2008-07-27 21:46:04 +00001053 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001054 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001055 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001056 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +00001057 // FIXME DebugLoc info might be appropriate here
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001058 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001059 }
1060 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061}
1062
Owen Andersone50ed302009-08-10 22:56:29 +00001063SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001064 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001065 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001066 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001067 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001068 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001069 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1070 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001071 bool ignored;
1072 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001073 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001074 &ignored);
1075 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001076 } else
1077 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001078}
1079
Devang Patel0d881da2010-07-06 22:08:15 +00001080SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001081 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001082 bool isTargetGA,
1083 unsigned char TargetFlags) {
1084 assert((TargetFlags == 0 || isTargetGA) &&
1085 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001086
Dan Gohman6520e202008-10-18 02:06:02 +00001087 // Truncate (with sign-extension) the offset value to the pointer size.
Richard Smith1144af32012-08-24 23:29:28 +00001088 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001089 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001090 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001091
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001092 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1093 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001094 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001095 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001096 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001097 }
1098
Chris Lattner2a4ed822009-06-25 21:21:14 +00001099 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001100 if (GVar && GVar->isThreadLocal())
1101 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1102 else
1103 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001104
Jim Laskey583bd472006-10-27 23:46:08 +00001105 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001106 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001107 ID.AddPointer(GV);
1108 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001109 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001110 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001111 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001112 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001113 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001114
Devang Patel0d881da2010-07-06 22:08:15 +00001115 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001116 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001117 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001118 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001119 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001120}
1121
Owen Andersone50ed302009-08-10 22:56:29 +00001122SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001123 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001124 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001125 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001126 ID.AddInteger(FI);
1127 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001128 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001130
Dan Gohman95531882010-03-18 18:49:47 +00001131 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001132 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001133 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001134 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001135}
1136
Owen Andersone50ed302009-08-10 22:56:29 +00001137SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001138 unsigned char TargetFlags) {
1139 assert((TargetFlags == 0 || isTarget) &&
1140 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001141 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001142 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001143 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001144 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001145 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001146 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001147 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001148 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001149
Dan Gohman95531882010-03-18 18:49:47 +00001150 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1151 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001152 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001153 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001154 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001155}
1156
Dan Gohman46510a72010-04-15 01:51:59 +00001157SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001158 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001159 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001160 unsigned char TargetFlags) {
1161 assert((TargetFlags == 0 || isTarget) &&
1162 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001163 if (Alignment == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00001164 Alignment = TLI.getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001165 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001166 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001167 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001168 ID.AddInteger(Alignment);
1169 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001170 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001171 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001172 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001173 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001174 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001175
Dan Gohman95531882010-03-18 18:49:47 +00001176 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1177 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001178 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001179 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001180 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001181}
1182
Chris Lattnerc3aae252005-01-07 07:46:32 +00001183
Owen Andersone50ed302009-08-10 22:56:29 +00001184SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001185 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001186 bool isTarget,
1187 unsigned char TargetFlags) {
1188 assert((TargetFlags == 0 || isTarget) &&
1189 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001190 if (Alignment == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00001191 Alignment = TLI.getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001192 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001193 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001194 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001195 ID.AddInteger(Alignment);
1196 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001197 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001198 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001199 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001200 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001201 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001202
Dan Gohman95531882010-03-18 18:49:47 +00001203 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1204 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001205 CSEMap.InsertNode(N, IP);
1206 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001207 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001208}
1209
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001210SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1211 unsigned char TargetFlags) {
1212 FoldingSetNodeID ID;
1213 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1214 ID.AddInteger(Index);
1215 ID.AddInteger(Offset);
1216 ID.AddInteger(TargetFlags);
1217 void *IP = 0;
1218 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1219 return SDValue(E, 0);
1220
1221 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1222 TargetFlags);
1223 CSEMap.InsertNode(N, IP);
1224 AllNodes.push_back(N);
1225 return SDValue(N, 0);
1226}
1227
Dan Gohman475871a2008-07-27 21:46:04 +00001228SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001229 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001231 ID.AddPointer(MBB);
1232 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001233 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001234 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001235
Dan Gohman95531882010-03-18 18:49:47 +00001236 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001237 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001239 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001240}
1241
Owen Andersone50ed302009-08-10 22:56:29 +00001242SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1244 ValueTypeNodes.size())
1245 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001246
Duncan Sands83ec4b62008-06-06 12:08:01 +00001247 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001248 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001249
Dan Gohman475871a2008-07-27 21:46:04 +00001250 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001251 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001252 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001253 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001254}
1255
Owen Andersone50ed302009-08-10 22:56:29 +00001256SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001257 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001258 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001259 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001260 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001261 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001262}
1263
Owen Andersone50ed302009-08-10 22:56:29 +00001264SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001265 unsigned char TargetFlags) {
1266 SDNode *&N =
1267 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1268 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001269 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001270 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001271 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001272 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001273}
1274
Dan Gohman475871a2008-07-27 21:46:04 +00001275SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001276 if ((unsigned)Cond >= CondCodeNodes.size())
1277 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001278
Chris Lattner079a27a2005-08-09 20:40:02 +00001279 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001280 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001281 CondCodeNodes[Cond] = N;
1282 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001283 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001284
Dan Gohman475871a2008-07-27 21:46:04 +00001285 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001286}
1287
Nate Begeman5a5ca152009-04-29 05:20:52 +00001288// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1289// the shuffle mask M that point at N1 to point at N2, and indices that point
1290// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001291static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1292 std::swap(N1, N2);
1293 int NElts = M.size();
1294 for (int i = 0; i != NElts; ++i) {
1295 if (M[i] >= NElts)
1296 M[i] -= NElts;
1297 else if (M[i] >= 0)
1298 M[i] += NElts;
1299 }
1300}
1301
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001302SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001303 SDValue N2, const int *Mask) {
1304 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001305 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001306 "Vector Shuffle VTs must be a vectors");
1307 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1308 && "Vector Shuffle VTs must have same element type");
1309
1310 // Canonicalize shuffle undef, undef -> undef
1311 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001312 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001313
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001314 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001315 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001316 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001317 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001318 for (unsigned i = 0; i != NElts; ++i) {
1319 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001320 MaskVec.push_back(Mask[i]);
1321 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001322
Nate Begeman9008ca62009-04-27 18:41:29 +00001323 // Canonicalize shuffle v, v -> v, undef
1324 if (N1 == N2) {
1325 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001326 for (unsigned i = 0; i != NElts; ++i)
1327 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001328 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001329
Nate Begeman9008ca62009-04-27 18:41:29 +00001330 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1331 if (N1.getOpcode() == ISD::UNDEF)
1332 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001333
Nate Begeman9008ca62009-04-27 18:41:29 +00001334 // Canonicalize all index into lhs, -> shuffle lhs, undef
1335 // Canonicalize all index into rhs, -> shuffle rhs, undef
1336 bool AllLHS = true, AllRHS = true;
1337 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001338 for (unsigned i = 0; i != NElts; ++i) {
1339 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001340 if (N2Undef)
1341 MaskVec[i] = -1;
1342 else
1343 AllLHS = false;
1344 } else if (MaskVec[i] >= 0) {
1345 AllRHS = false;
1346 }
1347 }
1348 if (AllLHS && AllRHS)
1349 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001350 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001351 N2 = getUNDEF(VT);
1352 if (AllRHS) {
1353 N1 = getUNDEF(VT);
1354 commuteShuffle(N1, N2, MaskVec);
1355 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001356
Nate Begeman9008ca62009-04-27 18:41:29 +00001357 // If Identity shuffle, or all shuffle in to undef, return that node.
1358 bool AllUndef = true;
1359 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001360 for (unsigned i = 0; i != NElts; ++i) {
1361 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001362 if (MaskVec[i] >= 0) AllUndef = false;
1363 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001364 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001365 return N1;
1366 if (AllUndef)
1367 return getUNDEF(VT);
1368
1369 FoldingSetNodeID ID;
1370 SDValue Ops[2] = { N1, N2 };
1371 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001372 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001373 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001374
Nate Begeman9008ca62009-04-27 18:41:29 +00001375 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001376 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001377 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001378
Nate Begeman9008ca62009-04-27 18:41:29 +00001379 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1380 // SDNode doesn't have access to it. This memory will be "leaked" when
1381 // the node is deallocated, but recovered when the NodeAllocator is released.
1382 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1383 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001384
Dan Gohman95531882010-03-18 18:49:47 +00001385 ShuffleVectorSDNode *N =
1386 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001387 CSEMap.InsertNode(N, IP);
1388 AllNodes.push_back(N);
1389 return SDValue(N, 0);
1390}
1391
Owen Andersone50ed302009-08-10 22:56:29 +00001392SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001393 SDValue Val, SDValue DTy,
1394 SDValue STy, SDValue Rnd, SDValue Sat,
1395 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001396 // If the src and dest types are the same and the conversion is between
1397 // integer types of the same sign or two floats, no conversion is necessary.
1398 if (DTy == STy &&
1399 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001400 return Val;
1401
1402 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001403 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1404 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001405 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001406 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001407 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001408
Dan Gohman95531882010-03-18 18:49:47 +00001409 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1410 Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001411 CSEMap.InsertNode(N, IP);
1412 AllNodes.push_back(N);
1413 return SDValue(N, 0);
1414}
1415
Owen Andersone50ed302009-08-10 22:56:29 +00001416SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001417 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001418 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001419 ID.AddInteger(RegNo);
1420 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001421 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001422 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001423
Dan Gohman95531882010-03-18 18:49:47 +00001424 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001425 CSEMap.InsertNode(N, IP);
1426 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001427 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001428}
1429
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001430SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1431 FoldingSetNodeID ID;
1432 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1433 ID.AddPointer(RegMask);
1434 void *IP = 0;
1435 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1436 return SDValue(E, 0);
1437
1438 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1439 CSEMap.InsertNode(N, IP);
1440 AllNodes.push_back(N);
1441 return SDValue(N, 0);
1442}
1443
Chris Lattner7561d482010-03-14 02:33:54 +00001444SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001445 FoldingSetNodeID ID;
1446 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001447 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1448 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001449 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001450 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001451 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001452
Dan Gohman95531882010-03-18 18:49:47 +00001453 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001454 CSEMap.InsertNode(N, IP);
1455 AllNodes.push_back(N);
1456 return SDValue(N, 0);
1457}
1458
Chris Lattner7561d482010-03-14 02:33:54 +00001459
Dan Gohman46510a72010-04-15 01:51:59 +00001460SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001461 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001462 bool isTarget,
1463 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001464 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1465
1466 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001467 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001468 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001469 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001470 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001471 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001472 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001473 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001474
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001475 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1476 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001477 CSEMap.InsertNode(N, IP);
1478 AllNodes.push_back(N);
1479 return SDValue(N, 0);
1480}
1481
Dan Gohman475871a2008-07-27 21:46:04 +00001482SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001483 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001484 "SrcValue is not a pointer?");
1485
Jim Laskey583bd472006-10-27 23:46:08 +00001486 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001487 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001488 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001489
Chris Lattner61b09412006-08-11 21:01:22 +00001490 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001491 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001492 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001493
Dan Gohman95531882010-03-18 18:49:47 +00001494 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001495 CSEMap.InsertNode(N, IP);
1496 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001497 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001498}
1499
Chris Lattnerdecc2672010-04-07 05:20:54 +00001500/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1501SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1502 FoldingSetNodeID ID;
1503 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1504 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001505
Chris Lattnerdecc2672010-04-07 05:20:54 +00001506 void *IP = 0;
1507 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1508 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001509
Chris Lattnerdecc2672010-04-07 05:20:54 +00001510 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1511 CSEMap.InsertNode(N, IP);
1512 AllNodes.push_back(N);
1513 return SDValue(N, 0);
1514}
1515
1516
Duncan Sands92abc622009-01-31 15:50:11 +00001517/// getShiftAmountOperand - Return the specified value casted to
1518/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001519SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001520 EVT OpTy = Op.getValueType();
Michael Liaoa6b20ce2013-03-01 18:40:30 +00001521 EVT ShTy = TLI.getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001522 if (OpTy == ShTy || OpTy.isVector()) return Op;
1523
1524 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001525 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001526}
1527
Chris Lattner37ce9df2007-10-15 17:47:20 +00001528/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1529/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001530SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001531 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001532 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001533 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001534 unsigned StackAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00001535 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001536
David Greene3f2bf852009-11-12 20:49:22 +00001537 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001538 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1539}
1540
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001541/// CreateStackTemporary - Create a stack temporary suitable for holding
1542/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001543SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001544 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1545 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001546 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1547 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00001548 const DataLayout *TD = TLI.getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001549 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1550 TD->getPrefTypeAlignment(Ty2));
1551
1552 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001553 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001554 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1555}
1556
Owen Andersone50ed302009-08-10 22:56:29 +00001557SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001558 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001559 // These setcc operations always fold.
1560 switch (Cond) {
1561 default: break;
1562 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001563 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001564 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001565 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Chris Lattnera83385f2006-04-27 05:01:07 +00001567 case ISD::SETOEQ:
1568 case ISD::SETOGT:
1569 case ISD::SETOGE:
1570 case ISD::SETOLT:
1571 case ISD::SETOLE:
1572 case ISD::SETONE:
1573 case ISD::SETO:
1574 case ISD::SETUO:
1575 case ISD::SETUEQ:
1576 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001577 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001578 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001580
Gabor Greifba36cb52008-08-28 21:40:38 +00001581 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001582 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001583 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001584 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001585
Chris Lattnerc3aae252005-01-07 07:46:32 +00001586 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001587 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001588 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1589 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001590 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1591 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1592 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1593 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1594 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1595 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1596 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1597 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001598 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001599 }
Chris Lattner67255a12005-04-07 18:14:58 +00001600 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001601 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1602 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001603 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001604 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001605 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001606 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001607 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001608 // fall through
1609 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001610 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001611 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001612 // fall through
1613 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001614 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001615 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001616 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001617 // fall through
1618 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001619 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001620 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001621 // fall through
1622 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001623 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001624 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001625 // fall through
1626 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001627 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001628 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001629 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001630 // fall through
1631 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001632 R==APFloat::cmpEqual, VT);
1633 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1634 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1635 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1636 R==APFloat::cmpEqual, VT);
1637 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1638 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1639 R==APFloat::cmpLessThan, VT);
1640 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1641 R==APFloat::cmpUnordered, VT);
1642 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1643 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001644 }
1645 } else {
1646 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001647 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001648 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001649 }
1650
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001651 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001652 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001653}
1654
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001655/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1656/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001657bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001658 // This predicate is not safe for vector operations.
1659 if (Op.getValueType().isVector())
1660 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001661
Dan Gohman87862e72009-12-11 21:31:27 +00001662 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001663 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1664}
1665
Dan Gohmanea859be2007-06-22 14:59:07 +00001666/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1667/// this predicate to simplify operations downstream. Mask is known to be zero
1668/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001669bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001670 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001671 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001672 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001673 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001674 return (KnownZero & Mask) == Mask;
1675}
1676
1677/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1678/// known to be either zero or one and return them in the KnownZero/KnownOne
1679/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1680/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001681void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1682 APInt &KnownOne, unsigned Depth) const {
1683 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001684
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001685 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001686 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001687 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001688
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001689 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001690
1691 switch (Op.getOpcode()) {
1692 case ISD::Constant:
1693 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001694 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1695 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001696 return;
1697 case ISD::AND:
1698 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001699 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1700 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001701 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1702 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001703
1704 // Output known-1 bits are only known if set in both the LHS & RHS.
1705 KnownOne &= KnownOne2;
1706 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1707 KnownZero |= KnownZero2;
1708 return;
1709 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001710 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1711 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1713 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1714
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 // Output known-0 bits are only known if clear in both the LHS & RHS.
1716 KnownZero &= KnownZero2;
1717 // Output known-1 are known to be set if set in either the LHS | RHS.
1718 KnownOne |= KnownOne2;
1719 return;
1720 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001721 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1722 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001723 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1724 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1725
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001727 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001728 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1729 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1730 KnownZero = KnownZeroOut;
1731 return;
1732 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001733 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001734 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1735 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001736 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1737 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1738
1739 // If low bits are zero in either operand, output low known-0 bits.
1740 // Also compute a conserative estimate for high known-0 bits.
1741 // More trickiness is possible, but this is sufficient for the
1742 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001743 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001744 unsigned TrailZ = KnownZero.countTrailingOnes() +
1745 KnownZero2.countTrailingOnes();
1746 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001747 KnownZero2.countLeadingOnes(),
1748 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001749
1750 TrailZ = std::min(TrailZ, BitWidth);
1751 LeadZ = std::min(LeadZ, BitWidth);
1752 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1753 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001754 return;
1755 }
1756 case ISD::UDIV: {
1757 // For the purposes of computing leading zeros we can conservatively
1758 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001759 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001760 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001761 unsigned LeadZ = KnownZero2.countLeadingOnes();
1762
Jay Foad7a874dd2010-12-01 08:53:58 +00001763 KnownOne2.clearAllBits();
1764 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001765 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001766 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1767 if (RHSUnknownLeadingOnes != BitWidth)
1768 LeadZ = std::min(BitWidth,
1769 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001770
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001771 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001772 return;
1773 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001774 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001775 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1776 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001777 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1778 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1779
Dan Gohmanea859be2007-06-22 14:59:07 +00001780 // Only known if known in both the LHS and RHS.
1781 KnownOne &= KnownOne2;
1782 KnownZero &= KnownZero2;
1783 return;
1784 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001785 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1786 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001787 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1788 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1789
Dan Gohmanea859be2007-06-22 14:59:07 +00001790 // Only known if known in both the LHS and RHS.
1791 KnownOne &= KnownOne2;
1792 KnownZero &= KnownZero2;
1793 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001794 case ISD::SADDO:
1795 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001796 case ISD::SSUBO:
1797 case ISD::USUBO:
1798 case ISD::SMULO:
1799 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001800 if (Op.getResNo() != 1)
1801 return;
Duncan Sands03228082008-11-23 15:47:28 +00001802 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001803 case ISD::SETCC:
1804 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001805 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1806 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001807 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001808 return;
1809 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001810 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001812 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001813
1814 // If the shift count is an invalid immediate, don't do anything.
1815 if (ShAmt >= BitWidth)
1816 return;
1817
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001818 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001819 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001820 KnownZero <<= ShAmt;
1821 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001822 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001823 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001824 }
1825 return;
1826 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001827 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001828 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001829 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001830
Dan Gohmand4cf9922008-02-26 18:50:50 +00001831 // If the shift count is an invalid immediate, don't do anything.
1832 if (ShAmt >= BitWidth)
1833 return;
1834
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001835 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001836 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001837 KnownZero = KnownZero.lshr(ShAmt);
1838 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001839
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001840 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001841 KnownZero |= HighBits; // High bits known zero.
1842 }
1843 return;
1844 case ISD::SRA:
1845 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001846 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001847
Dan Gohmand4cf9922008-02-26 18:50:50 +00001848 // If the shift count is an invalid immediate, don't do anything.
1849 if (ShAmt >= BitWidth)
1850 return;
1851
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 // If any of the demanded bits are produced by the sign extension, we also
1853 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001854 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001855
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001856 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001857 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001858 KnownZero = KnownZero.lshr(ShAmt);
1859 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001860
Dan Gohmanea859be2007-06-22 14:59:07 +00001861 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001862 APInt SignBit = APInt::getSignBit(BitWidth);
1863 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001864
Dan Gohmanca93a432008-02-20 16:30:17 +00001865 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001866 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001867 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001868 KnownOne |= HighBits; // New bits are known one.
1869 }
1870 }
1871 return;
1872 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001873 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001874 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
1876 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001877 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001878 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001879
Dan Gohman977a76f2008-02-13 22:28:48 +00001880 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001881 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001882
Dan Gohmanea859be2007-06-22 14:59:07 +00001883 // If the sign extended bits are demanded, we know that the sign
1884 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001885 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001886 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001887 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001888
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001889 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1890 KnownOne &= InputDemandedBits;
1891 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001892 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1893
Dan Gohmanea859be2007-06-22 14:59:07 +00001894 // If the sign bit of the input is known set or clear, then we know the
1895 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001896 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 KnownZero |= NewBits;
1898 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001899 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001900 KnownOne |= NewBits;
1901 KnownZero &= ~NewBits;
1902 } else { // Input sign bit unknown
1903 KnownZero &= ~NewBits;
1904 KnownOne &= ~NewBits;
1905 }
1906 return;
1907 }
1908 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001909 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001910 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001911 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001912 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001913 unsigned LowBits = Log2_32(BitWidth)+1;
1914 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001915 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001916 return;
1917 }
1918 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001919 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00001920 // If this is a ZEXTLoad and we are looking at the loaded value.
1921 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001923 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001924 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001925 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001926 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001927 }
1928 return;
1929 }
1930 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001931 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001932 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001933 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001934 KnownZero = KnownZero.trunc(InBits);
1935 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001936 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001937 KnownZero = KnownZero.zext(BitWidth);
1938 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001939 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001940 return;
1941 }
1942 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001943 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001944 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001945 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001946 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001947
Jay Foad40f8f622010-12-07 08:25:19 +00001948 KnownZero = KnownZero.trunc(InBits);
1949 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001950 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001951
1952 // Note if the sign bit is known to be zero or one.
1953 bool SignBitKnownZero = KnownZero.isNegative();
1954 bool SignBitKnownOne = KnownOne.isNegative();
1955 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1956 "Sign bit can't be known to be both zero and one!");
1957
Jay Foad40f8f622010-12-07 08:25:19 +00001958 KnownZero = KnownZero.zext(BitWidth);
1959 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001960
Dan Gohman977a76f2008-02-13 22:28:48 +00001961 // If the sign bit is known zero or one, the top bits match.
1962 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001963 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001964 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001965 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001966 return;
1967 }
1968 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00001969 EVT InVT = Op.getOperand(0).getValueType();
1970 unsigned InBits = InVT.getScalarType().getSizeInBits();
1971 KnownZero = KnownZero.trunc(InBits);
1972 KnownOne = KnownOne.trunc(InBits);
1973 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1974 KnownZero = KnownZero.zext(BitWidth);
1975 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001976 return;
1977 }
1978 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001979 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001980 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001981 KnownZero = KnownZero.zext(InBits);
1982 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001983 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001984 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001985 KnownZero = KnownZero.trunc(BitWidth);
1986 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 break;
1988 }
1989 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001990 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001991 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001992 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1993 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00001994 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001995 return;
1996 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001997 case ISD::FGETSIGN:
1998 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001999 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002000 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002001
Dan Gohman23e8b712008-04-28 17:02:21 +00002002 case ISD::SUB: {
2003 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2004 // We know that the top bits of C-X are clear if X contains less bits
2005 // than C (i.e. no wrap-around can happen). For example, 20-X is
2006 // positive if we can prove that X is >= 0 and < 16.
2007 if (CLHS->getAPIntValue().isNonNegative()) {
2008 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2009 // NLZ can't be BitWidth with no sign bit
2010 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002011 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002012
2013 // If all of the MaskV bits are known to be zero, then we know the
2014 // output top bits are zero, because we now know that the output is
2015 // from [0-C].
2016 if ((KnownZero2 & MaskV) == MaskV) {
2017 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2018 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002019 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002020 }
2021 }
2022 }
2023 }
2024 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002025 case ISD::ADD:
2026 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002027 // Output known-0 bits are known if clear or set in both the low clear bits
2028 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2029 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002030 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002031 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002032 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2033
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002034 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002035 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002036 KnownZeroOut = std::min(KnownZeroOut,
2037 KnownZero2.countTrailingOnes());
2038
Chris Lattnerda605882010-12-19 20:38:28 +00002039 if (Op.getOpcode() == ISD::ADD) {
2040 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2041 return;
2042 }
2043
2044 // With ADDE, a carry bit may be added in, so we can only use this
2045 // information if we know (at least) that the low two bits are clear. We
2046 // then return to the caller that the low bit is unknown but that other bits
2047 // are known zero.
2048 if (KnownZeroOut >= 2) // ADDE
2049 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002050 return;
2051 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002052 case ISD::SREM:
2053 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002054 const APInt &RA = Rem->getAPIntValue().abs();
2055 if (RA.isPowerOf2()) {
2056 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002057 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002058 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002059
Duncan Sands5c2873a2010-01-29 09:45:26 +00002060 // The low bits of the first operand are unchanged by the srem.
2061 KnownZero = KnownZero2 & LowBits;
2062 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002063
Duncan Sands5c2873a2010-01-29 09:45:26 +00002064 // If the first operand is non-negative or has all low bits zero, then
2065 // the upper bits are all zero.
2066 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2067 KnownZero |= ~LowBits;
2068
2069 // If the first operand is negative and not all low bits are zero, then
2070 // the upper bits are all one.
2071 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2072 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002073 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002074 }
2075 }
2076 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002077 case ISD::UREM: {
2078 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002079 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002080 if (RA.isPowerOf2()) {
2081 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002082 KnownZero |= ~LowBits;
2083 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002084 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2085 break;
2086 }
2087 }
2088
2089 // Since the result is less than or equal to either operand, any leading
2090 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002091 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2092 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002093
2094 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2095 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002096 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002097 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002098 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002099 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002100 case ISD::FrameIndex:
2101 case ISD::TargetFrameIndex:
2102 if (unsigned Align = InferPtrAlignment(Op)) {
2103 // The low bits are known zero if the pointer is aligned.
2104 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2105 return;
2106 }
2107 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002108
Dan Gohmanea859be2007-06-22 14:59:07 +00002109 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002110 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2111 break;
2112 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002113 case ISD::INTRINSIC_WO_CHAIN:
2114 case ISD::INTRINSIC_W_CHAIN:
2115 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002116 // Allow the target to implement this method for its nodes.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002117 TLI.computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002118 return;
2119 }
2120}
2121
2122/// ComputeNumSignBits - Return the number of times the sign bit of the
2123/// register is replicated into the other bits. We know that at least 1 bit
2124/// is always equal to the sign bit (itself), but other cases can give us
2125/// information. For example, immediately after an "SRA X, 2", we know that
2126/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002127unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002129 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002130 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002132 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Dan Gohmanea859be2007-06-22 14:59:07 +00002134 if (Depth == 6)
2135 return 1; // Limit search depth.
2136
2137 switch (Op.getOpcode()) {
2138 default: break;
2139 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 return VTBits-Tmp+1;
2142 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002143 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Dan Gohmanea859be2007-06-22 14:59:07 +00002146 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002147 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002148 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002150
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002152 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002153 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002154
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 case ISD::SIGN_EXTEND_INREG:
2156 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002157 Tmp =
2158 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002159 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Dan Gohmanea859be2007-06-22 14:59:07 +00002161 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2162 return std::max(Tmp, Tmp2);
2163
2164 case ISD::SRA:
2165 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2166 // SRA X, C -> adds C sign bits.
2167 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002168 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002169 if (Tmp > VTBits) Tmp = VTBits;
2170 }
2171 return Tmp;
2172 case ISD::SHL:
2173 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2174 // shl destroys sign bits.
2175 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002176 if (C->getZExtValue() >= VTBits || // Bad shift.
2177 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2178 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002179 }
2180 break;
2181 case ISD::AND:
2182 case ISD::OR:
2183 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002184 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002185 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002186 if (Tmp != 1) {
2187 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2188 FirstAnswer = std::min(Tmp, Tmp2);
2189 // We computed what we know about the sign bits as our first
2190 // answer. Now proceed to the generic code that uses
2191 // ComputeMaskedBits, and pick whichever answer is better.
2192 }
2193 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002194
2195 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002196 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002197 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002198 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002199 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002200
2201 case ISD::SADDO:
2202 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002203 case ISD::SSUBO:
2204 case ISD::USUBO:
2205 case ISD::SMULO:
2206 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002207 if (Op.getResNo() != 1)
2208 break;
Duncan Sands03228082008-11-23 15:47:28 +00002209 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002210 case ISD::SETCC:
2211 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002212 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002213 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002214 return VTBits;
2215 break;
2216 case ISD::ROTL:
2217 case ISD::ROTR:
2218 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002219 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002220
Dan Gohmanea859be2007-06-22 14:59:07 +00002221 // Handle rotate right by N like a rotate left by 32-N.
2222 if (Op.getOpcode() == ISD::ROTR)
2223 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2224
2225 // If we aren't rotating out all of the known-in sign bits, return the
2226 // number that are left. This handles rotl(sext(x), 1) for example.
2227 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2228 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2229 }
2230 break;
2231 case ISD::ADD:
2232 // Add can have at most one carry bit. Thus we know that the output
2233 // is, at worst, one more bit than the inputs.
2234 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2235 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002236
Dan Gohmanea859be2007-06-22 14:59:07 +00002237 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002238 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002239 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002240 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002241 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002242
Dan Gohmanea859be2007-06-22 14:59:07 +00002243 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2244 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002245 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002246 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002247
Dan Gohmanea859be2007-06-22 14:59:07 +00002248 // If we are subtracting one from a positive number, there is no carry
2249 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002250 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002251 return Tmp;
2252 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002253
Dan Gohmanea859be2007-06-22 14:59:07 +00002254 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2255 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002256 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002257
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 case ISD::SUB:
2259 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2260 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002261
Dan Gohmanea859be2007-06-22 14:59:07 +00002262 // Handle NEG.
2263 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002264 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002265 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002266 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002267 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2268 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002269 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002270 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002271
Dan Gohmanea859be2007-06-22 14:59:07 +00002272 // If the input is known to be positive (the sign bit is known clear),
2273 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002274 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002275 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002276
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 // Otherwise, we treat this like a SUB.
2278 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002279
Dan Gohmanea859be2007-06-22 14:59:07 +00002280 // Sub can have at most one carry bit. Thus we know that the output
2281 // is, at worst, one more bit than the inputs.
2282 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2283 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002284 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002285 case ISD::TRUNCATE:
2286 // FIXME: it's tricky to do anything useful for this, but it is an important
2287 // case for targets like X86.
2288 break;
2289 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002290
Nadav Rotem77451752013-03-20 22:53:44 +00002291 // If we are looking at the loaded value of the SDNode.
2292 if (Op.getResNo() == 0) {
2293 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2294 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2295 unsigned ExtType = LD->getExtensionType();
2296 switch (ExtType) {
2297 default: break;
2298 case ISD::SEXTLOAD: // '17' bits known
2299 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2300 return VTBits-Tmp+1;
2301 case ISD::ZEXTLOAD: // '16' bits known
2302 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2303 return VTBits-Tmp;
2304 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002305 }
2306 }
2307
2308 // Allow the target to implement this method for its nodes.
2309 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002310 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002311 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2312 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2313 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002314 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002315 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002316
Dan Gohmanea859be2007-06-22 14:59:07 +00002317 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2318 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002319 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002320 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002321
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002322 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002323 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002324 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002325 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002326 Mask = KnownOne;
2327 } else {
2328 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002329 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002331
Dan Gohmanea859be2007-06-22 14:59:07 +00002332 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2333 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002334 Mask = ~Mask;
2335 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002336 // Return # leading zeros. We use 'min' here in case Val was zero before
2337 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002338 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002339}
2340
Chris Lattner0a9481f2011-02-13 22:25:43 +00002341/// isBaseWithConstantOffset - Return true if the specified operand is an
2342/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2343/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2344/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002345/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002346bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2347 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2348 !isa<ConstantSDNode>(Op.getOperand(1)))
2349 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002350
2351 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002352 !MaskedValueIsZero(Op.getOperand(0),
2353 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2354 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002355
Chris Lattner0a9481f2011-02-13 22:25:43 +00002356 return true;
2357}
2358
2359
Dan Gohman8d44b282009-09-03 20:34:31 +00002360bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2361 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002362 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002363 return true;
2364
2365 // If the value is a constant, we can obviously see if it is a NaN or not.
2366 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2367 return !C->getValueAPF().isNaN();
2368
2369 // TODO: Recognize more cases here.
2370
2371 return false;
2372}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002373
Dan Gohmane8326932010-02-24 06:52:40 +00002374bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2375 // If the value is a constant, we can obviously see if it is a zero or not.
2376 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2377 return !C->isZero();
2378
2379 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002380 switch (Op.getOpcode()) {
2381 default: break;
2382 case ISD::OR:
2383 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2384 return !C->isNullValue();
2385 break;
2386 }
Dan Gohmane8326932010-02-24 06:52:40 +00002387
2388 return false;
2389}
2390
2391bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2392 // Check the obvious case.
2393 if (A == B) return true;
2394
2395 // For for negative and positive zero.
2396 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2397 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2398 if (CA->isZero() && CB->isZero()) return true;
2399
2400 // Otherwise they may not be equal.
2401 return false;
2402}
2403
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002405///
Owen Andersone50ed302009-08-10 22:56:29 +00002406SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002407 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002408 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002409 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002410 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002411 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002412
Dan Gohman95531882010-03-18 18:49:47 +00002413 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002414 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002415
Chris Lattner4a283e92006-08-11 18:38:11 +00002416 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002417#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002418 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002419#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002420 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002421}
2422
Bill Wendling7ade28c2009-01-28 22:17:52 +00002423SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002424 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002425 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002426 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002427 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002428 switch (Opcode) {
2429 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002430 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002431 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002432 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002433 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002434 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002435 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002436 case ISD::UINT_TO_FP:
2437 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002438 APFloat apf(EVTToAPFloatSemantics(VT),
2439 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002440 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002441 Opcode==ISD::SINT_TO_FP,
2442 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002443 return getConstantFP(apf, VT);
2444 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002447 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002448 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002449 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002450 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002451 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002452 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002453 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002454 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002455 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002456 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002457 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002458 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002459 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002460 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002461 }
2462 }
2463
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002464 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002465 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002466 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002467 switch (Opcode) {
2468 case ISD::FNEG:
2469 V.changeSign();
2470 return getConstantFP(V, VT);
2471 case ISD::FABS:
2472 V.clearSign();
2473 return getConstantFP(V, VT);
2474 case ISD::FCEIL: {
2475 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2476 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002477 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002478 break;
2479 }
2480 case ISD::FTRUNC: {
2481 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2482 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002483 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002484 break;
2485 }
2486 case ISD::FFLOOR: {
2487 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2488 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002489 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002490 break;
2491 }
2492 case ISD::FP_EXTEND: {
2493 bool ignored;
2494 // This can return overflow, underflow, or inexact; we don't care.
2495 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002496 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002497 APFloat::rmNearestTiesToEven, &ignored);
2498 return getConstantFP(V, VT);
2499 }
2500 case ISD::FP_TO_SINT:
2501 case ISD::FP_TO_UINT: {
2502 integerPart x[2];
2503 bool ignored;
2504 assert(integerPartWidth >= 64);
2505 // FIXME need to be more flexible about rounding mode.
2506 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2507 Opcode==ISD::FP_TO_SINT,
2508 APFloat::rmTowardZero, &ignored);
2509 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002510 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002511 APInt api(VT.getSizeInBits(), x);
2512 return getConstant(api, VT);
2513 }
2514 case ISD::BITCAST:
2515 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2516 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2517 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2518 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2519 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002520 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002521 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002522
Gabor Greifba36cb52008-08-28 21:40:38 +00002523 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002524 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002525 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002526 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002527 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002528 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002529 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002530 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002531 assert(VT.isFloatingPoint() &&
2532 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002533 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002534 assert((!VT.isVector() ||
2535 VT.getVectorNumElements() ==
2536 Operand.getValueType().getVectorNumElements()) &&
2537 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002538 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002539 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002540 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002541 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002543 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002544 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002545 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2546 "Invalid sext node, dst < src!");
2547 assert((!VT.isVector() ||
2548 VT.getVectorNumElements() ==
2549 Operand.getValueType().getVectorNumElements()) &&
2550 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002551 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2552 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2553 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002554 // sext(undef) = 0, because the top bits will all be the same.
2555 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002556 break;
2557 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002558 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002559 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002560 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002561 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2562 "Invalid zext node, dst < src!");
2563 assert((!VT.isVector() ||
2564 VT.getVectorNumElements() ==
2565 Operand.getValueType().getVectorNumElements()) &&
2566 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002567 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002568 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002569 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002570 else if (OpOpcode == ISD::UNDEF)
2571 // zext(undef) = 0, because the top bits will be zero.
2572 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002573 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002574 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002576 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002577 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002578 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2579 "Invalid anyext node, dst < src!");
2580 assert((!VT.isVector() ||
2581 VT.getVectorNumElements() ==
2582 Operand.getValueType().getVectorNumElements()) &&
2583 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002584
Dan Gohman9e86a732010-06-18 00:08:30 +00002585 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2586 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002587 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002588 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002589 else if (OpOpcode == ISD::UNDEF)
2590 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002591
2592 // (ext (trunx x)) -> x
2593 if (OpOpcode == ISD::TRUNCATE) {
2594 SDValue OpOp = Operand.getNode()->getOperand(0);
2595 if (OpOp.getValueType() == VT)
2596 return OpOp;
2597 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002598 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002599 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002600 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002601 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002603 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2604 "Invalid truncate node, src < dst!");
2605 assert((!VT.isVector() ||
2606 VT.getVectorNumElements() ==
2607 Operand.getValueType().getVectorNumElements()) &&
2608 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002609 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002610 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002611 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2612 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002613 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002614 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2615 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002616 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002617 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002618 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002619 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002620 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002621 if (OpOpcode == ISD::UNDEF)
2622 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002623 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002624 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002625 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002626 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002627 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002628 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002629 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2630 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002631 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002632 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002633 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002634 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002635 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002636 (VT.getVectorElementType() == Operand.getValueType() ||
2637 (VT.getVectorElementType().isInteger() &&
2638 Operand.getValueType().isInteger() &&
2639 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002640 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002641 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002642 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002643 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2644 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2645 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2646 Operand.getConstantOperandVal(1) == 0 &&
2647 Operand.getOperand(0).getValueType() == VT)
2648 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002649 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002650 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002651 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002652 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002653 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002654 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002655 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002656 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002657 break;
2658 case ISD::FABS:
2659 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002660 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002661 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002662 }
2663
Chris Lattner43247a12005-08-25 19:12:10 +00002664 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002665 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002666 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002667 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002668 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002669 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002670 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002671 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002672 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002673
Dan Gohman95531882010-03-18 18:49:47 +00002674 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002675 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002676 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002677 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002678 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002679
Chris Lattnerc3aae252005-01-07 07:46:32 +00002680 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002681#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002682 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002683#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002684 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002685}
2686
Benjamin Kramer49693102013-02-04 15:19:18 +00002687SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2688 SDNode *Cst1, SDNode *Cst2) {
2689 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2690 SmallVector<SDValue, 4> Outputs;
2691 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002692
Benjamin Kramer49693102013-02-04 15:19:18 +00002693 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2694 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
2695 if (Scalar1 && Scalar2) {
2696 // Scalar instruction.
2697 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
2698 } else {
2699 // For vectors extract each constant element into Inputs so we can constant
2700 // fold them individually.
2701 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2702 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2703 if (!BV1 || !BV2)
2704 return SDValue();
2705
2706 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2707
2708 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2709 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2710 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2711 if (!V1 || !V2) // Not a constant, bail.
2712 return SDValue();
2713
2714 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2715 // FIXME: This is valid and could be handled by truncating the APInts.
2716 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2717 return SDValue();
2718
2719 Inputs.push_back(std::make_pair(V1, V2));
2720 }
Bill Wendling688d1c42008-09-24 10:16:24 +00002721 }
2722
Benjamin Kramer49693102013-02-04 15:19:18 +00002723 // We have a number of constant values, constant fold them element by element.
2724 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2725 const APInt &C1 = Inputs[I].first->getAPIntValue();
2726 const APInt &C2 = Inputs[I].second->getAPIntValue();
2727
2728 switch (Opcode) {
2729 case ISD::ADD:
2730 Outputs.push_back(getConstant(C1 + C2, SVT));
2731 break;
2732 case ISD::SUB:
2733 Outputs.push_back(getConstant(C1 - C2, SVT));
2734 break;
2735 case ISD::MUL:
2736 Outputs.push_back(getConstant(C1 * C2, SVT));
2737 break;
2738 case ISD::UDIV:
2739 if (!C2.getBoolValue())
2740 return SDValue();
2741 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2742 break;
2743 case ISD::UREM:
2744 if (!C2.getBoolValue())
2745 return SDValue();
2746 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2747 break;
2748 case ISD::SDIV:
2749 if (!C2.getBoolValue())
2750 return SDValue();
2751 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2752 break;
2753 case ISD::SREM:
2754 if (!C2.getBoolValue())
2755 return SDValue();
2756 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2757 break;
2758 case ISD::AND:
2759 Outputs.push_back(getConstant(C1 & C2, SVT));
2760 break;
2761 case ISD::OR:
2762 Outputs.push_back(getConstant(C1 | C2, SVT));
2763 break;
2764 case ISD::XOR:
2765 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2766 break;
2767 case ISD::SHL:
2768 Outputs.push_back(getConstant(C1 << C2, SVT));
2769 break;
2770 case ISD::SRL:
2771 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2772 break;
2773 case ISD::SRA:
2774 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2775 break;
2776 case ISD::ROTL:
2777 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2778 break;
2779 case ISD::ROTR:
2780 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2781 break;
2782 default:
2783 return SDValue();
2784 }
2785 }
2786
2787 // Handle the scalar case first.
Silviu Baranga02066832013-04-25 09:32:33 +00002788 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002789 return Outputs.back();
2790
2791 // Otherwise build a big vector out of the scalar elements we generated.
2792 return getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, Outputs.data(),
2793 Outputs.size());
Bill Wendling688d1c42008-09-24 10:16:24 +00002794}
2795
Benjamin Kramer49693102013-02-04 15:19:18 +00002796SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT, SDValue N1,
2797 SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002798 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2799 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002800 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002801 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002802 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002803 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2804 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002805 // Fold trivial token factors.
2806 if (N1.getOpcode() == ISD::EntryToken) return N2;
2807 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002808 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002809 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002810 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002811 // Concat of UNDEFs is UNDEF.
2812 if (N1.getOpcode() == ISD::UNDEF &&
2813 N2.getOpcode() == ISD::UNDEF)
2814 return getUNDEF(VT);
2815
Dan Gohman7f8613e2008-08-14 20:04:46 +00002816 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2817 // one big BUILD_VECTOR.
2818 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2819 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002820 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2821 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002822 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002823 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002824 }
2825 break;
Chris Lattner76365122005-01-16 02:23:22 +00002826 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002827 assert(VT.isInteger() && "This operator does not apply to FP types!");
2828 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002829 N1.getValueType() == VT && "Binary operator types must match!");
2830 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2831 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002832 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002833 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002834 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2835 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002836 break;
Chris Lattner76365122005-01-16 02:23:22 +00002837 case ISD::OR:
2838 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002839 case ISD::ADD:
2840 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002841 assert(VT.isInteger() && "This operator does not apply to FP types!");
2842 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002843 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002844 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2845 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002846 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002847 return N1;
2848 break;
Chris Lattner76365122005-01-16 02:23:22 +00002849 case ISD::UDIV:
2850 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002851 case ISD::MULHU:
2852 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002853 case ISD::MUL:
2854 case ISD::SDIV:
2855 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002856 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002857 assert(N1.getValueType() == N2.getValueType() &&
2858 N1.getValueType() == VT && "Binary operator types must match!");
2859 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002860 case ISD::FADD:
2861 case ISD::FSUB:
2862 case ISD::FMUL:
2863 case ISD::FDIV:
2864 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002865 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002866 if (Opcode == ISD::FADD) {
2867 // 0+x --> x
2868 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2869 if (CFP->getValueAPF().isZero())
2870 return N2;
2871 // x+0 --> x
2872 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2873 if (CFP->getValueAPF().isZero())
2874 return N1;
2875 } else if (Opcode == ISD::FSUB) {
2876 // x-0 --> x
2877 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2878 if (CFP->getValueAPF().isZero())
2879 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00002880 } else if (Opcode == ISD::FMUL) {
2881 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2882 SDValue V = N2;
2883
2884 // If the first operand isn't the constant, try the second
2885 if (!CFP) {
2886 CFP = dyn_cast<ConstantFPSDNode>(N2);
2887 V = N1;
2888 }
2889
2890 if (CFP) {
2891 // 0*x --> 0
2892 if (CFP->isZero())
2893 return SDValue(CFP,0);
2894 // 1*x --> x
2895 if (CFP->isExactlyValue(1.0))
2896 return V;
2897 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00002898 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002899 }
Dale Johannesen78995512010-05-15 18:38:02 +00002900 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002901 assert(N1.getValueType() == N2.getValueType() &&
2902 N1.getValueType() == VT && "Binary operator types must match!");
2903 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002904 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2905 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002906 N1.getValueType().isFloatingPoint() &&
2907 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002908 "Invalid FCOPYSIGN!");
2909 break;
Chris Lattner76365122005-01-16 02:23:22 +00002910 case ISD::SHL:
2911 case ISD::SRA:
2912 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002913 case ISD::ROTL:
2914 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002915 assert(VT == N1.getValueType() &&
2916 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002917 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002918 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00002919 assert((!VT.isVector() || VT == N2.getValueType()) &&
2920 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00002921 // Verify that the shift amount VT is bit enough to hold valid shift
2922 // amounts. This catches things like trying to shift an i1024 value by an
2923 // i8, which is easy to fall into in generic code that uses
2924 // TLI.getShiftAmount().
2925 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002926 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002927 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002928
2929 // Always fold shifts of i1 values so the code generator doesn't need to
2930 // handle them. Since we know the size of the shift has to be less than the
2931 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002932 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002933 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002934 if (N2C && N2C->isNullValue())
2935 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002936 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002937 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002938 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002939 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002940 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002941 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002942 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002943 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002944 "type is vector!");
2945 assert((!EVT.isVector() ||
2946 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2947 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002948 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002949 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002950 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002951 break;
2952 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002953 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002954 assert(VT.isFloatingPoint() &&
2955 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002956 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002957 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002958 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002959 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002960 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002961 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002962 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002963 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002964 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002965 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002966 assert(!EVT.isVector() &&
2967 "AssertSExt/AssertZExt type should be the vector element type "
2968 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002969 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002970 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002971 break;
2972 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002973 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002974 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002975 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002976 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002977 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002978 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002979 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002980 "type is vector!");
2981 assert((!EVT.isVector() ||
2982 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2983 "Vector element counts must match in SIGN_EXTEND_INREG");
2984 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002985 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002986
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002987 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002988 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002989 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002990 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002991 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002992 return getConstant(Val, VT);
2993 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002994 break;
2995 }
2996 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002997 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2998 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002999 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003000
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003001 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3002 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003003 if (N2C &&
3004 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003005 N1.getNumOperands() > 0) {
3006 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003007 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003008 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003009 N1.getOperand(N2C->getZExtValue() / Factor),
3010 getConstant(N2C->getZExtValue() % Factor,
3011 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003012 }
3013
3014 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3015 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003016 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3017 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003018
3019 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003020 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003021 // are promoted and implicitly truncated, and the result implicitly
3022 // extended. Make that explicit here.
3023 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003024
Bob Wilsonb1303d02009-04-13 22:05:19 +00003025 return Elt;
3026 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003027
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003028 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3029 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003030 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003031 // If the indices are the same, return the inserted element else
3032 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003033 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003034 SDValue N1Op2 = N1.getOperand(2);
3035 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3036
3037 if (N1Op2C && N2C) {
3038 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3039 if (VT == N1.getOperand(1).getValueType())
3040 return N1.getOperand(1);
3041 else
3042 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3043 }
3044
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003045 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003046 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003047 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003048 break;
3049 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003050 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003051 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3052 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003053 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003054 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003055
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003056 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3057 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003058 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003059 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003060 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003061
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003062 // EXTRACT_ELEMENT of a constant int is also very common.
3063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003064 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003065 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003066 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3067 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003068 }
3069 break;
David Greene91585092011-01-26 15:38:49 +00003070 case ISD::EXTRACT_SUBVECTOR: {
3071 SDValue Index = N2;
3072 if (VT.isSimple() && N1.getValueType().isSimple()) {
3073 assert(VT.isVector() && N1.getValueType().isVector() &&
3074 "Extract subvector VTs must be a vectors!");
3075 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
3076 "Extract subvector VTs must have the same element type!");
3077 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3078 "Extract subvector must be from larger vector to smaller vector!");
3079
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003080 if (isa<ConstantSDNode>(Index.getNode())) {
3081 assert((VT.getVectorNumElements() +
3082 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003083 <= N1.getValueType().getVectorNumElements())
3084 && "Extract subvector overflow!");
3085 }
3086
3087 // Trivial extraction.
3088 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
3089 return N1;
3090 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003091 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003092 }
David Greene91585092011-01-26 15:38:49 +00003093 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003094
Benjamin Kramer49693102013-02-04 15:19:18 +00003095 // Perform trivial constant folding.
3096 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3097 if (SV.getNode()) return SV;
3098
3099 // Canonicalize constant to RHS if commutative.
3100 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3101 std::swap(N1C, N2C);
3102 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003103 }
3104
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003105 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003106 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3107 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003108 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003109 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003110 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003111 std::swap(N1CFP, N2CFP);
3112 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003113 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003114 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3115 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003116 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003117 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003118 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003119 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003120 return getConstantFP(V1, VT);
3121 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003122 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003123 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3124 if (s!=APFloat::opInvalidOp)
3125 return getConstantFP(V1, VT);
3126 break;
3127 case ISD::FMUL:
3128 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3129 if (s!=APFloat::opInvalidOp)
3130 return getConstantFP(V1, VT);
3131 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003132 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003133 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3134 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3135 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003136 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003137 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003138 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3139 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3140 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003141 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003142 case ISD::FCOPYSIGN:
3143 V1.copySign(V2);
3144 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003145 default: break;
3146 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003147 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003148
3149 if (Opcode == ISD::FP_ROUND) {
3150 APFloat V = N1CFP->getValueAPF(); // make copy
3151 bool ignored;
3152 // This can return overflow, underflow, or inexact; we don't care.
3153 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003154 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003155 APFloat::rmNearestTiesToEven, &ignored);
3156 return getConstantFP(V, VT);
3157 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003158 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003159
Chris Lattner62b57722006-04-20 05:39:12 +00003160 // Canonicalize an UNDEF to the RHS, even over a constant.
3161 if (N1.getOpcode() == ISD::UNDEF) {
3162 if (isCommutativeBinOp(Opcode)) {
3163 std::swap(N1, N2);
3164 } else {
3165 switch (Opcode) {
3166 case ISD::FP_ROUND_INREG:
3167 case ISD::SIGN_EXTEND_INREG:
3168 case ISD::SUB:
3169 case ISD::FSUB:
3170 case ISD::FDIV:
3171 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003172 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003173 return N1; // fold op(undef, arg2) -> undef
3174 case ISD::UDIV:
3175 case ISD::SDIV:
3176 case ISD::UREM:
3177 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003178 case ISD::SRL:
3179 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003180 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003181 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3182 // For vectors, we can't easily build an all zero vector, just return
3183 // the LHS.
3184 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003185 }
3186 }
3187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003188
3189 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003190 if (N2.getOpcode() == ISD::UNDEF) {
3191 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003192 case ISD::XOR:
3193 if (N1.getOpcode() == ISD::UNDEF)
3194 // Handle undef ^ undef -> 0 special case. This is a common
3195 // idiom (misuse).
3196 return getConstant(0, VT);
3197 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003198 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003199 case ISD::ADDC:
3200 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003201 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003202 case ISD::UDIV:
3203 case ISD::SDIV:
3204 case ISD::UREM:
3205 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003206 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003207 case ISD::FADD:
3208 case ISD::FSUB:
3209 case ISD::FMUL:
3210 case ISD::FDIV:
3211 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003212 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003213 return N2;
3214 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003215 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003216 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003217 case ISD::SRL:
3218 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003219 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003220 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3221 // For vectors, we can't easily build an all zero vector, just return
3222 // the LHS.
3223 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003224 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003225 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003226 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003227 // For vectors, we can't easily build an all one vector, just return
3228 // the LHS.
3229 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003230 case ISD::SRA:
3231 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003232 }
3233 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003234
Chris Lattner27e9b412005-05-11 18:57:39 +00003235 // Memoize this node if possible.
3236 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003237 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003238 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003239 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003240 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003241 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003242 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003243 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003244 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003245
Dan Gohman95531882010-03-18 18:49:47 +00003246 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003247 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003248 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003249 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003250 }
3251
Chris Lattnerc3aae252005-01-07 07:46:32 +00003252 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003253#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003254 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003255#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003256 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003257}
3258
Owen Andersone50ed302009-08-10 22:56:29 +00003259SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003260 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003261 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003262 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003263 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003264 case ISD::CONCAT_VECTORS:
3265 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3266 // one big BUILD_VECTOR.
3267 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3268 N2.getOpcode() == ISD::BUILD_VECTOR &&
3269 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003270 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3271 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003272 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3273 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003274 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003275 }
3276 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003277 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003278 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003279 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003280 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003281 break;
3282 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003283 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003284 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003285 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003286 return N2; // select true, X, Y -> X
3287 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003288 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003289
3290 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003291 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003292 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003293 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003294 case ISD::INSERT_SUBVECTOR: {
3295 SDValue Index = N3;
3296 if (VT.isSimple() && N1.getValueType().isSimple()
3297 && N2.getValueType().isSimple()) {
3298 assert(VT.isVector() && N1.getValueType().isVector() &&
3299 N2.getValueType().isVector() &&
3300 "Insert subvector VTs must be a vectors");
3301 assert(VT == N1.getValueType() &&
3302 "Dest and insert subvector source types must match!");
3303 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3304 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003305 if (isa<ConstantSDNode>(Index.getNode())) {
3306 assert((N2.getValueType().getVectorNumElements() +
3307 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003308 <= VT.getVectorNumElements())
3309 && "Insert subvector overflow!");
3310 }
3311
3312 // Trivial insertion.
3313 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3314 return N2;
3315 }
3316 break;
3317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003318 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003319 // Fold bit_convert nodes from a type to themselves.
3320 if (N1.getValueType() == VT)
3321 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003322 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003323 }
3324
Chris Lattner43247a12005-08-25 19:12:10 +00003325 // Memoize node if it doesn't produce a flag.
3326 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003327 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003328 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003329 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003330 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003331 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003332 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003333 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003334 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003335
Dan Gohman95531882010-03-18 18:49:47 +00003336 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003337 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003338 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003339 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003340 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003341
Chris Lattnerc3aae252005-01-07 07:46:32 +00003342 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003343#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003344 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003345#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003346 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003347}
3348
Owen Andersone50ed302009-08-10 22:56:29 +00003349SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003350 SDValue N1, SDValue N2, SDValue N3,
3351 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003352 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003353 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003354}
3355
Owen Andersone50ed302009-08-10 22:56:29 +00003356SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003357 SDValue N1, SDValue N2, SDValue N3,
3358 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003359 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003360 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003361}
3362
Dan Gohman98ca4f22009-08-05 01:29:28 +00003363/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3364/// the incoming stack arguments to be loaded from the stack.
3365SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3366 SmallVector<SDValue, 8> ArgChains;
3367
3368 // Include the original chain at the beginning of the list. When this is
3369 // used by target LowerCall hooks, this helps legalize find the
3370 // CALLSEQ_BEGIN node.
3371 ArgChains.push_back(Chain);
3372
3373 // Add a chain value for each stack argument.
3374 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3375 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3376 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3377 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3378 if (FI->getIndex() < 0)
3379 ArgChains.push_back(SDValue(L, 1));
3380
3381 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003383 &ArgChains[0], ArgChains.size());
3384}
3385
Dan Gohman707e0182008-04-12 04:36:06 +00003386/// getMemsetValue - Vectorized representation of the memset value
3387/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003388static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003389 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003390 assert(Value.getOpcode() != ISD::UNDEF);
3391
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003392 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003393 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003394 assert(C->getAPIntValue().getBitWidth() == 8);
3395 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003396 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003397 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003398 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003399 }
Evan Chengf0df0312008-05-15 08:39:06 +00003400
Dale Johannesen0f502f62009-02-03 22:26:09 +00003401 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003402 if (NumBits > 8) {
3403 // Use a multiplication with 0x010101... to extend the input to the
3404 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003405 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003406 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003407 }
3408
3409 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003410}
3411
Dan Gohman707e0182008-04-12 04:36:06 +00003412/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3413/// used when a memcpy is turned into a memset when the source is a constant
3414/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003415static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003416 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003417 // Handle vector with all elements zero.
3418 if (Str.empty()) {
3419 if (VT.isInteger())
3420 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003421 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003422 return DAG.getConstantFP(0.0, VT);
3423 else if (VT.isVector()) {
3424 unsigned NumElts = VT.getVectorNumElements();
3425 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003426 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003427 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3428 EltVT, NumElts)));
3429 } else
3430 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003431 }
3432
Duncan Sands83ec4b62008-06-06 12:08:01 +00003433 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003434 unsigned NumVTBits = VT.getSizeInBits();
3435 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003436 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3437
Evan Cheng4ff23d02013-01-10 22:13:27 +00003438 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003439 if (TLI.isLittleEndian()) {
3440 for (unsigned i = 0; i != NumBytes; ++i)
3441 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3442 } else {
3443 for (unsigned i = 0; i != NumBytes; ++i)
3444 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003445 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003446
Evan Chenge07f85e2012-12-11 23:26:14 +00003447 // If the "cost" of materializing the integer immediate is 1 or free, then
3448 // it is cost effective to turn the load into the immediate.
Chandler Carruthe4b4edd2013-01-05 12:32:17 +00003449 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3450 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng376642e2012-12-10 23:21:26 +00003451 return DAG.getConstant(Val, VT);
3452 return SDValue(0, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003453}
3454
Scott Michelfdc40a02009-02-17 22:15:04 +00003455/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003456///
Dan Gohman475871a2008-07-27 21:46:04 +00003457static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003458 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003459 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003460 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003461 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003462}
3463
Evan Chengf0df0312008-05-15 08:39:06 +00003464/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3465///
Chris Lattner18c7f802012-02-05 02:29:43 +00003466static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003467 unsigned SrcDelta = 0;
3468 GlobalAddressSDNode *G = NULL;
3469 if (Src.getOpcode() == ISD::GlobalAddress)
3470 G = cast<GlobalAddressSDNode>(Src);
3471 else if (Src.getOpcode() == ISD::ADD &&
3472 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3473 Src.getOperand(1).getOpcode() == ISD::Constant) {
3474 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003475 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003476 }
3477 if (!G)
3478 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003479
Chris Lattner18c7f802012-02-05 02:29:43 +00003480 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003481}
Dan Gohman707e0182008-04-12 04:36:06 +00003482
Evan Cheng255f20f2010-04-01 06:04:33 +00003483/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3484/// to replace the memset / memcpy. Return true if the number of memory ops
3485/// is below the threshold. It returns the types of the sequence of
3486/// memory ops to perform memset / memcpy by reference.
3487static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003488 unsigned Limit, uint64_t Size,
3489 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003490 bool IsMemset,
3491 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003492 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003493 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003494 SelectionDAG &DAG,
3495 const TargetLowering &TLI) {
3496 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3497 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003498 // If 'SrcAlign' is zero, that means the memory operation does not need to
3499 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3500 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3501 // is the specified alignment of the memory operation. If it is zero, that
3502 // means it's possible to change the alignment of the destination.
3503 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3504 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003505 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003506 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003507 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003508
Chris Lattneracee6472010-03-07 07:45:08 +00003509 if (VT == MVT::Other) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00003510 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003511 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003512 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003513 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003514 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 case 0: VT = MVT::i64; break;
3516 case 4: VT = MVT::i32; break;
3517 case 2: VT = MVT::i16; break;
3518 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003519 }
3520 }
3521
Owen Anderson766b5ef2009-08-11 21:59:30 +00003522 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003523 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003524 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003526
Duncan Sands8e4eb092008-06-08 20:54:56 +00003527 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003528 VT = LVT;
3529 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003530
Dan Gohman707e0182008-04-12 04:36:06 +00003531 unsigned NumMemOps = 0;
3532 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003534 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003535 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003536 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003537 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003538
3539 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003540 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003541 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003542 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003543 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003544 Found = true;
3545 else if (NewVT == MVT::i64 &&
3546 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003547 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003548 // i64 is usually not legal on 32-bit targets, but f64 may be.
3549 NewVT = MVT::f64;
3550 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003551 }
Evan Cheng376642e2012-12-10 23:21:26 +00003552 }
3553
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003554 if (!Found) {
3555 do {
3556 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3557 if (NewVT == MVT::i8)
3558 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003559 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003560 }
3561 NewVTSize = NewVT.getSizeInBits() / 8;
3562
Evan Cheng376642e2012-12-10 23:21:26 +00003563 // If the new VT cannot cover all of the remaining bits, then consider
3564 // issuing a (or a pair of) unaligned and overlapping load / store.
3565 // FIXME: Only does this for 64-bit or more since we don't have proper
3566 // cost model for unaligned load / store.
3567 bool Fast;
Evan Chenga16e49d2012-12-12 20:43:23 +00003568 if (NumMemOps && AllowOverlap &&
3569 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng376642e2012-12-10 23:21:26 +00003570 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3571 VTSize = Size;
3572 else {
3573 VT = NewVT;
3574 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003575 }
Dan Gohman707e0182008-04-12 04:36:06 +00003576 }
Dan Gohman707e0182008-04-12 04:36:06 +00003577
Evan Chenga16e49d2012-12-12 20:43:23 +00003578 if (++NumMemOps > Limit)
3579 return false;
3580
Dan Gohman707e0182008-04-12 04:36:06 +00003581 MemOps.push_back(VT);
3582 Size -= VTSize;
3583 }
3584
3585 return true;
3586}
3587
Dale Johannesen0f502f62009-02-03 22:26:09 +00003588static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003589 SDValue Chain, SDValue Dst,
3590 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003591 unsigned Align, bool isVol,
3592 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003593 MachinePointerInfo DstPtrInfo,
3594 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003595 // Turn a memcpy of undef to nop.
3596 if (Src.getOpcode() == ISD::UNDEF)
3597 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003598
Dan Gohman21323f32008-05-29 19:42:22 +00003599 // Expand memcpy to a series of load and store ops if the size operand falls
3600 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003601 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3602 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003603 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003604 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003605 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003606 MachineFunction &MF = DAG.getMachineFunction();
3607 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003608 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003609 MF.getFunction()->getAttributes().
3610 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003611 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3612 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3613 DstAlignCanChange = true;
3614 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3615 if (Align > SrcAlign)
3616 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003617 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003618 bool CopyFromStr = isMemSrcFromString(Src, Str);
3619 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003620 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003621
Evan Cheng94107ba2010-04-01 18:19:11 +00003622 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003623 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003624 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003625 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003626 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003627
Evan Cheng255f20f2010-04-01 06:04:33 +00003628 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003629 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003630 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003631
3632 // Don't promote to an alignment that would require dynamic stack
3633 // realignment.
3634 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3635 if (!TRI->needsStackRealignment(MF))
3636 while (NewAlign > Align &&
3637 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3638 NewAlign /= 2;
3639
Evan Cheng255f20f2010-04-01 06:04:33 +00003640 if (NewAlign > Align) {
3641 // Give the stack frame object a larger alignment if needed.
3642 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3643 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3644 Align = NewAlign;
3645 }
3646 }
Dan Gohman707e0182008-04-12 04:36:06 +00003647
Dan Gohman475871a2008-07-27 21:46:04 +00003648 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003649 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003650 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003651 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003652 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003653 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003654 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003655
Evan Cheng376642e2012-12-10 23:21:26 +00003656 if (VTSize > Size) {
3657 // Issuing an unaligned load / store pair that overlaps with the previous
3658 // pair. Adjust the offset accordingly.
3659 assert(i == NumMemOps-1 && i != 0);
3660 SrcOff -= VTSize - Size;
3661 DstOff -= VTSize - Size;
3662 }
3663
Evan Cheng255f20f2010-04-01 06:04:33 +00003664 if (CopyFromStr &&
3665 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003666 // It's unlikely a store of a vector immediate can be done in a single
3667 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003668 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003669 // FIXME: Handle other cases where store of vector immediate is done in
3670 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003671 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003672 if (Value.getNode())
3673 Store = DAG.getStore(Chain, dl, Value,
3674 getMemBasePlusOffset(Dst, DstOff, DAG),
3675 DstPtrInfo.getWithOffset(DstOff), isVol,
3676 false, Align);
3677 }
3678
3679 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003680 // The type might not be legal for the target. This should only happen
3681 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003682 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3683 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003684 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003685 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003686 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003687 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003688 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003689 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003690 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003691 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003692 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003693 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3694 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003695 }
3696 OutChains.push_back(Store);
3697 SrcOff += VTSize;
3698 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003699 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003700 }
3701
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003703 &OutChains[0], OutChains.size());
3704}
3705
Dale Johannesen0f502f62009-02-03 22:26:09 +00003706static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003707 SDValue Chain, SDValue Dst,
3708 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003709 unsigned Align, bool isVol,
3710 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003711 MachinePointerInfo DstPtrInfo,
3712 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003713 // Turn a memmove of undef to nop.
3714 if (Src.getOpcode() == ISD::UNDEF)
3715 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003716
3717 // Expand memmove to a series of load and store ops if the size operand falls
3718 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003719 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003720 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003721 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003722 MachineFunction &MF = DAG.getMachineFunction();
3723 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003724 bool OptSize = MF.getFunction()->getAttributes().
3725 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003726 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3727 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3728 DstAlignCanChange = true;
3729 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3730 if (Align > SrcAlign)
3731 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003732 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003733
Evan Cheng94107ba2010-04-01 18:19:11 +00003734 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00003735 (DstAlignCanChange ? 0 : Align), SrcAlign,
3736 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003737 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003738
Evan Cheng255f20f2010-04-01 06:04:33 +00003739 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003740 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003741 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003742 if (NewAlign > Align) {
3743 // Give the stack frame object a larger alignment if needed.
3744 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3745 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3746 Align = NewAlign;
3747 }
3748 }
Dan Gohman21323f32008-05-29 19:42:22 +00003749
Evan Cheng255f20f2010-04-01 06:04:33 +00003750 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003751 SmallVector<SDValue, 8> LoadValues;
3752 SmallVector<SDValue, 8> LoadChains;
3753 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003754 unsigned NumMemOps = MemOps.size();
3755 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003756 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003757 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003758 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003759
Dale Johannesen0f502f62009-02-03 22:26:09 +00003760 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003761 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003762 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003763 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003764 LoadValues.push_back(Value);
3765 LoadChains.push_back(Value.getValue(1));
3766 SrcOff += VTSize;
3767 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003769 &LoadChains[0], LoadChains.size());
3770 OutChains.clear();
3771 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003772 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003773 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003774 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003775
Dale Johannesen0f502f62009-02-03 22:26:09 +00003776 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003777 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003778 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003779 OutChains.push_back(Store);
3780 DstOff += VTSize;
3781 }
3782
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003784 &OutChains[0], OutChains.size());
3785}
3786
Dale Johannesen0f502f62009-02-03 22:26:09 +00003787static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003788 SDValue Chain, SDValue Dst,
3789 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003790 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003791 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003792 // Turn a memset of undef to nop.
3793 if (Src.getOpcode() == ISD::UNDEF)
3794 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003795
3796 // Expand memset to a series of load/store ops if the size operand
3797 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003798 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003799 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003800 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003801 MachineFunction &MF = DAG.getMachineFunction();
3802 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003803 bool OptSize = MF.getFunction()->getAttributes().
3804 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003805 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3806 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3807 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003808 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003809 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003810 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003811 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00003812 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003813 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003814
Evan Cheng255f20f2010-04-01 06:04:33 +00003815 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003816 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003817 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003818 if (NewAlign > Align) {
3819 // Give the stack frame object a larger alignment if needed.
3820 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3821 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3822 Align = NewAlign;
3823 }
3824 }
3825
Dan Gohman475871a2008-07-27 21:46:04 +00003826 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003827 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003828 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003829
3830 // Find the largest store and generate the bit pattern for it.
3831 EVT LargestVT = MemOps[0];
3832 for (unsigned i = 1; i < NumMemOps; i++)
3833 if (MemOps[i].bitsGT(LargestVT))
3834 LargestVT = MemOps[i];
3835 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3836
Dan Gohman707e0182008-04-12 04:36:06 +00003837 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003838 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00003839 unsigned VTSize = VT.getSizeInBits() / 8;
3840 if (VTSize > Size) {
3841 // Issuing an unaligned load / store pair that overlaps with the previous
3842 // pair. Adjust the offset accordingly.
3843 assert(i == NumMemOps-1 && i != 0);
3844 DstOff -= VTSize - Size;
3845 }
Benjamin Kramer80220362011-01-02 19:57:05 +00003846
3847 // If this store is smaller than the largest store see whether we can get
3848 // the smaller value for free with a truncate.
3849 SDValue Value = MemSetValue;
3850 if (VT.bitsLT(LargestVT)) {
3851 if (!LargestVT.isVector() && !VT.isVector() &&
3852 TLI.isTruncateFree(LargestVT, VT))
3853 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3854 else
3855 Value = getMemsetValue(Src, VT, DAG, dl);
3856 }
3857 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003858 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003859 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003860 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003861 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003862 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003863 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00003864 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003865 }
3866
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003868 &OutChains[0], OutChains.size());
3869}
3870
Dale Johannesen0f502f62009-02-03 22:26:09 +00003871SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003873 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003874 MachinePointerInfo DstPtrInfo,
3875 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00003876 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00003877
3878 // Check to see if we should lower the memcpy to loads and stores first.
3879 // For cases within the target-specified limits, this is the best choice.
3880 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3881 if (ConstantSize) {
3882 // Memcpy with size zero? Just return the original chain.
3883 if (ConstantSize->isNullValue())
3884 return Chain;
3885
Evan Cheng255f20f2010-04-01 06:04:33 +00003886 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3887 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003888 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003889 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003890 return Result;
3891 }
3892
3893 // Then check to see if we should lower the memcpy with target-specific
3894 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003895 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003896 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003897 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003898 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003899 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003900 return Result;
3901
3902 // If we really need inline code and the target declined to provide it,
3903 // use a (potentially long) sequence of loads and stores.
3904 if (AlwaysInline) {
3905 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003906 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003907 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003908 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003909 }
3910
Dan Gohman7b55d362010-04-05 20:24:08 +00003911 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3912 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3913 // respect volatile, so they may do things like read or write memory
3914 // beyond the given memory regions. But fixing this isn't easy, and most
3915 // people don't care.
3916
Dan Gohman707e0182008-04-12 04:36:06 +00003917 // Emit a library call.
3918 TargetLowering::ArgListTy Args;
3919 TargetLowering::ArgListEntry Entry;
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003920 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003921 Entry.Node = Dst; Args.push_back(Entry);
3922 Entry.Node = Src; Args.push_back(Entry);
3923 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003924 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003925 TargetLowering::
3926 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003927 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003928 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3929 /*isTailCall=*/false,
3930 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003931 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003932 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003933 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003934 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3935
Dan Gohman707e0182008-04-12 04:36:06 +00003936 return CallResult.second;
3937}
3938
Dale Johannesen0f502f62009-02-03 22:26:09 +00003939SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003940 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003941 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003942 MachinePointerInfo DstPtrInfo,
3943 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00003944 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00003945
Dan Gohman21323f32008-05-29 19:42:22 +00003946 // Check to see if we should lower the memmove to loads and stores first.
3947 // For cases within the target-specified limits, this is the best choice.
3948 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3949 if (ConstantSize) {
3950 // Memmove with size zero? Just return the original chain.
3951 if (ConstantSize->isNullValue())
3952 return Chain;
3953
Dan Gohman475871a2008-07-27 21:46:04 +00003954 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003955 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003956 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003957 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003958 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003959 return Result;
3960 }
Dan Gohman707e0182008-04-12 04:36:06 +00003961
3962 // Then check to see if we should lower the memmove with target-specific
3963 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003964 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003965 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003966 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003967 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003968 return Result;
3969
Mon P Wang01f0e852010-04-06 08:27:51 +00003970 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3971 // not be safe. See memcpy above for more details.
3972
Dan Gohman707e0182008-04-12 04:36:06 +00003973 // Emit a library call.
3974 TargetLowering::ArgListTy Args;
3975 TargetLowering::ArgListEntry Entry;
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003976 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003977 Entry.Node = Dst; Args.push_back(Entry);
3978 Entry.Node = Src; Args.push_back(Entry);
3979 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003980 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003981 TargetLowering::
3982 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003983 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003984 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3985 /*isTailCall=*/false,
3986 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003987 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003988 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003989 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003990 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3991
Dan Gohman707e0182008-04-12 04:36:06 +00003992 return CallResult.second;
3993}
3994
Dale Johannesen0f502f62009-02-03 22:26:09 +00003995SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003996 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003997 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003998 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00003999 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004000
4001 // Check to see if we should lower the memset to stores first.
4002 // For cases within the target-specified limits, this is the best choice.
4003 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4004 if (ConstantSize) {
4005 // Memset with size zero? Just return the original chain.
4006 if (ConstantSize->isNullValue())
4007 return Chain;
4008
Mon P Wang20adc9d2010-04-04 03:10:48 +00004009 SDValue Result =
4010 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004011 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004012
Gabor Greifba36cb52008-08-28 21:40:38 +00004013 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004014 return Result;
4015 }
4016
4017 // Then check to see if we should lower the memset with target-specific
4018 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004019 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004020 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004021 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004022 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004023 return Result;
4024
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004025 // Emit a library call.
Chandler Carruthece6c6b2012-11-01 08:07:29 +00004026 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004027 TargetLowering::ArgListTy Args;
4028 TargetLowering::ArgListEntry Entry;
4029 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4030 Args.push_back(Entry);
4031 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00004032 if (Src.getValueType().bitsGT(MVT::i32))
4033 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00004034 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00004036 Entry.Node = Src;
4037 Entry.Ty = Type::getInt32Ty(*getContext());
4038 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00004039 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004040 Entry.Node = Size;
4041 Entry.Ty = IntPtrTy;
4042 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00004043 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00004044 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004045 TargetLowering::
4046 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004047 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004048 TLI.getLibcallCallingConv(RTLIB::MEMSET),
4049 /*isTailCall=*/false,
4050 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00004051 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00004052 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004053 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004054 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
4055
Dan Gohman707e0182008-04-12 04:36:06 +00004056 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004057}
4058
Owen Andersone50ed302009-08-10 22:56:29 +00004059SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00004060 SDValue Chain, SDValue Ptr, SDValue Cmp,
4061 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00004062 unsigned Alignment,
4063 AtomicOrdering Ordering,
Michael Ilseman06b96902012-09-10 16:56:31 +00004064 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004065 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4066 Alignment = getEVTAlignment(MemVT);
4067
Evan Chengff89dcb2009-10-18 18:16:27 +00004068 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004069
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004070 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004071 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004072 // FIXME: Volatile isn't really correct; we should keep track of atomic
4073 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004074 unsigned Flags = MachineMemOperand::MOVolatile;
4075 if (Opcode != ISD::ATOMIC_STORE)
4076 Flags |= MachineMemOperand::MOLoad;
4077 if (Opcode != ISD::ATOMIC_LOAD)
4078 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004079
4080 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004081 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004082
Eli Friedman55ba8162011-07-29 03:05:32 +00004083 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4084 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004085}
4086
4087SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4088 SDValue Chain,
4089 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004090 SDValue Swp, MachineMemOperand *MMO,
4091 AtomicOrdering Ordering,
4092 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004093 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4094 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4095
Owen Andersone50ed302009-08-10 22:56:29 +00004096 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004097
Owen Anderson825b72b2009-08-11 20:47:22 +00004098 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004099 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004100 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004101 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
4102 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004103 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004104 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004105 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4106 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004107 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004108 }
Dan Gohman95531882010-03-18 18:49:47 +00004109 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004110 Ptr, Cmp, Swp, MMO, Ordering,
4111 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004112 CSEMap.InsertNode(N, IP);
4113 AllNodes.push_back(N);
4114 return SDValue(N, 0);
4115}
4116
Owen Andersone50ed302009-08-10 22:56:29 +00004117SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004118 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004119 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004120 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004121 unsigned Alignment,
4122 AtomicOrdering Ordering,
4123 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004124 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4125 Alignment = getEVTAlignment(MemVT);
4126
Evan Chengff89dcb2009-10-18 18:16:27 +00004127 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004128 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004129 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004130 // For now, atomics are considered to be volatile always, and they are
4131 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004132 // FIXME: Volatile isn't really correct; we should keep track of atomic
4133 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004134 unsigned Flags = MachineMemOperand::MOVolatile;
4135 if (Opcode != ISD::ATOMIC_STORE)
4136 Flags |= MachineMemOperand::MOLoad;
4137 if (Opcode != ISD::ATOMIC_LOAD)
4138 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004139
4140 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004141 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004142 MemVT.getStoreSize(), Alignment);
4143
Eli Friedman55ba8162011-07-29 03:05:32 +00004144 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4145 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004146}
4147
4148SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4149 SDValue Chain,
4150 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004151 MachineMemOperand *MMO,
4152 AtomicOrdering Ordering,
4153 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004154 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4155 Opcode == ISD::ATOMIC_LOAD_SUB ||
4156 Opcode == ISD::ATOMIC_LOAD_AND ||
4157 Opcode == ISD::ATOMIC_LOAD_OR ||
4158 Opcode == ISD::ATOMIC_LOAD_XOR ||
4159 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004160 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004161 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004162 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004163 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004164 Opcode == ISD::ATOMIC_SWAP ||
4165 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004166 "Invalid Atomic Op");
4167
Owen Andersone50ed302009-08-10 22:56:29 +00004168 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004169
Eli Friedman327236c2011-08-24 20:50:09 +00004170 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4171 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004172 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004173 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004174 SDValue Ops[] = {Chain, Ptr, Val};
4175 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004176 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004177 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004178 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4179 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004180 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004181 }
Dan Gohman95531882010-03-18 18:49:47 +00004182 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004183 Ptr, Val, MMO,
4184 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004185 CSEMap.InsertNode(N, IP);
4186 AllNodes.push_back(N);
4187 return SDValue(N, 0);
4188}
4189
Eli Friedman327236c2011-08-24 20:50:09 +00004190SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4191 EVT VT, SDValue Chain,
4192 SDValue Ptr,
4193 const Value* PtrVal,
4194 unsigned Alignment,
4195 AtomicOrdering Ordering,
4196 SynchronizationScope SynchScope) {
4197 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4198 Alignment = getEVTAlignment(MemVT);
4199
4200 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004201 // An atomic store does not load. An atomic load does not store.
4202 // (An atomicrmw obviously both loads and stores.)
4203 // For now, atomics are considered to be volatile always, and they are
4204 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004205 // FIXME: Volatile isn't really correct; we should keep track of atomic
4206 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004207 unsigned Flags = MachineMemOperand::MOVolatile;
4208 if (Opcode != ISD::ATOMIC_STORE)
4209 Flags |= MachineMemOperand::MOLoad;
4210 if (Opcode != ISD::ATOMIC_LOAD)
4211 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004212
4213 MachineMemOperand *MMO =
4214 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4215 MemVT.getStoreSize(), Alignment);
4216
4217 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4218 Ordering, SynchScope);
4219}
4220
4221SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4222 EVT VT, SDValue Chain,
4223 SDValue Ptr,
4224 MachineMemOperand *MMO,
4225 AtomicOrdering Ordering,
4226 SynchronizationScope SynchScope) {
4227 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4228
4229 SDVTList VTs = getVTList(VT, MVT::Other);
4230 FoldingSetNodeID ID;
4231 ID.AddInteger(MemVT.getRawBits());
4232 SDValue Ops[] = {Chain, Ptr};
4233 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004234 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Eli Friedman327236c2011-08-24 20:50:09 +00004235 void* IP = 0;
4236 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4237 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4238 return SDValue(E, 0);
4239 }
4240 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4241 Ptr, MMO, Ordering, SynchScope);
4242 CSEMap.InsertNode(N, IP);
4243 AllNodes.push_back(N);
4244 return SDValue(N, 0);
4245}
4246
Duncan Sands4bdcb612008-07-02 17:40:58 +00004247/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004248SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4249 DebugLoc dl) {
4250 if (NumOps == 1)
4251 return Ops[0];
4252
Owen Andersone50ed302009-08-10 22:56:29 +00004253 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004254 VTs.reserve(NumOps);
4255 for (unsigned i = 0; i < NumOps; ++i)
4256 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004257 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004258 Ops, NumOps);
4259}
4260
Dan Gohman475871a2008-07-27 21:46:04 +00004261SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004262SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004263 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004264 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004265 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004266 unsigned Align, bool Vol,
4267 bool ReadMem, bool WriteMem) {
4268 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004269 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004270 ReadMem, WriteMem);
4271}
4272
4273SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004274SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4275 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004276 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004277 unsigned Align, bool Vol,
4278 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004279 if (Align == 0) // Ensure that codegen never sees alignment 0
4280 Align = getEVTAlignment(MemVT);
4281
4282 MachineFunction &MF = getMachineFunction();
4283 unsigned Flags = 0;
4284 if (WriteMem)
4285 Flags |= MachineMemOperand::MOStore;
4286 if (ReadMem)
4287 Flags |= MachineMemOperand::MOLoad;
4288 if (Vol)
4289 Flags |= MachineMemOperand::MOVolatile;
4290 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004291 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004292
4293 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4294}
4295
4296SDValue
4297SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4298 const SDValue *Ops, unsigned NumOps,
4299 EVT MemVT, MachineMemOperand *MMO) {
4300 assert((Opcode == ISD::INTRINSIC_VOID ||
4301 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004302 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004303 Opcode == ISD::LIFETIME_START ||
4304 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004305 (Opcode <= INT_MAX &&
4306 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4307 "Opcode is not a memory-accessing opcode!");
4308
Dale Johannesene8c17332009-01-29 00:47:48 +00004309 // Memoize the node unless it returns a flag.
4310 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004311 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004312 FoldingSetNodeID ID;
4313 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004314 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004315 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004316 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4317 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004318 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004320
Dan Gohman95531882010-03-18 18:49:47 +00004321 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4322 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004323 CSEMap.InsertNode(N, IP);
4324 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004325 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4326 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004327 }
4328 AllNodes.push_back(N);
4329 return SDValue(N, 0);
4330}
4331
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004332/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4333/// MachinePointerInfo record from it. This is particularly useful because the
4334/// code generator has many cases where it doesn't bother passing in a
4335/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4336static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4337 // If this is FI+Offset, we can model it.
4338 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4339 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4340
4341 // If this is (FI+Offset1)+Offset2, we can model it.
4342 if (Ptr.getOpcode() != ISD::ADD ||
4343 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4344 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4345 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004346
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004347 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4348 return MachinePointerInfo::getFixedStack(FI, Offset+
4349 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4350}
4351
4352/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4353/// MachinePointerInfo record from it. This is particularly useful because the
4354/// code generator has many cases where it doesn't bother passing in a
4355/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4356static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4357 // If the 'Offset' value isn't a constant, we can't handle this.
4358 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4359 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4360 if (OffsetOp.getOpcode() == ISD::UNDEF)
4361 return InferPointerInfo(Ptr);
4362 return MachinePointerInfo();
4363}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004364
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004365
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004366SDValue
4367SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4368 EVT VT, DebugLoc dl, SDValue Chain,
4369 SDValue Ptr, SDValue Offset,
4370 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004371 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004372 unsigned Alignment, const MDNode *TBAAInfo,
4373 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004374 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004375 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004376 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4377 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004378
Dan Gohmanc76909a2009-09-25 20:36:54 +00004379 unsigned Flags = MachineMemOperand::MOLoad;
4380 if (isVolatile)
4381 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004382 if (isNonTemporal)
4383 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004384 if (isInvariant)
4385 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004387 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4388 // clients.
4389 if (PtrInfo.V == 0)
4390 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004391
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004392 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004393 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004394 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004395 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004396 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004397}
4398
4399SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004400SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004401 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004402 SDValue Ptr, SDValue Offset, EVT MemVT,
4403 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004404 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004405 ExtType = ISD::NON_EXTLOAD;
4406 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004407 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004408 } else {
4409 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004410 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4411 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004412 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004413 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004414 assert(VT.isVector() == MemVT.isVector() &&
4415 "Cannot use trunc store to convert to or from a vector!");
4416 assert((!VT.isVector() ||
4417 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4418 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004419 }
4420
4421 bool Indexed = AM != ISD::UNINDEXED;
4422 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4423 "Unindexed load with an offset!");
4424
4425 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004426 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004427 SDValue Ops[] = { Chain, Ptr, Offset };
4428 FoldingSetNodeID ID;
4429 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004430 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004431 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004432 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004433 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004434 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004435 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004436 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4437 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004438 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004439 }
Dan Gohman95531882010-03-18 18:49:47 +00004440 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4441 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004442 CSEMap.InsertNode(N, IP);
4443 AllNodes.push_back(N);
4444 return SDValue(N, 0);
4445}
4446
Owen Andersone50ed302009-08-10 22:56:29 +00004447SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004448 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004449 MachinePointerInfo PtrInfo,
4450 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004451 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004452 const MDNode *TBAAInfo,
4453 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004454 SDValue Undef = getUNDEF(Ptr.getValueType());
4455 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004456 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4457 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004458}
Dale Johannesene8c17332009-01-29 00:47:48 +00004459
Stuart Hastingsa9011292011-02-16 16:23:55 +00004460SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004461 SDValue Chain, SDValue Ptr,
4462 MachinePointerInfo PtrInfo, EVT MemVT,
4463 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004464 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004465 SDValue Undef = getUNDEF(Ptr.getValueType());
4466 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004467 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004468 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004469}
4470
4471
Dan Gohman475871a2008-07-27 21:46:04 +00004472SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004473SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4474 SDValue Offset, ISD::MemIndexedMode AM) {
4475 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4476 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4477 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004478 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004479 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004480 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004481 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004482}
4483
Dale Johannesene8c17332009-01-29 00:47:48 +00004484SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004485 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004486 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004487 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004488 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004489 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004490 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004491 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004492
Dan Gohmanc76909a2009-09-25 20:36:54 +00004493 unsigned Flags = MachineMemOperand::MOStore;
4494 if (isVolatile)
4495 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004496 if (isNonTemporal)
4497 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004498
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004499 if (PtrInfo.V == 0)
4500 PtrInfo = InferPointerInfo(Ptr);
4501
4502 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004503 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004504 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004505 Val.getValueType().getStoreSize(), Alignment,
4506 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004507
4508 return getStore(Chain, dl, Val, Ptr, MMO);
4509}
4510
4511SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4512 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004513 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004514 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004515 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004516 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004517 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004518 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4519 FoldingSetNodeID ID;
4520 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004521 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004522 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004523 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004524 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004525 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004526 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4527 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004528 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004529 }
Dan Gohman95531882010-03-18 18:49:47 +00004530 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4531 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004532 CSEMap.InsertNode(N, IP);
4533 AllNodes.push_back(N);
4534 return SDValue(N, 0);
4535}
4536
Dale Johannesene8c17332009-01-29 00:47:48 +00004537SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004538 SDValue Ptr, MachinePointerInfo PtrInfo,
4539 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004540 unsigned Alignment,
4541 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004542 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004543 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004544 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4545 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004546
Dan Gohmanc76909a2009-09-25 20:36:54 +00004547 unsigned Flags = MachineMemOperand::MOStore;
4548 if (isVolatile)
4549 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004550 if (isNonTemporal)
4551 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004552
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004553 if (PtrInfo.V == 0)
4554 PtrInfo = InferPointerInfo(Ptr);
4555
4556 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004557 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004558 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4559 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004560
4561 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4562}
4563
4564SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4565 SDValue Ptr, EVT SVT,
4566 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004567 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004568
Michael Ilseman06b96902012-09-10 16:56:31 +00004569 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004570 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004571 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004572 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004573
Dan Gohman2e141d72009-12-14 23:40:38 +00004574 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4575 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004576 assert(VT.isInteger() == SVT.isInteger() &&
4577 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004578 assert(VT.isVector() == SVT.isVector() &&
4579 "Cannot use trunc store to convert to or from a vector!");
4580 assert((!VT.isVector() ||
4581 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4582 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004583
Owen Anderson825b72b2009-08-11 20:47:22 +00004584 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004585 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004586 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4587 FoldingSetNodeID ID;
4588 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004589 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004590 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004591 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004592 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004593 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004594 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4595 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004596 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004597 }
Dan Gohman95531882010-03-18 18:49:47 +00004598 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4599 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004600 CSEMap.InsertNode(N, IP);
4601 AllNodes.push_back(N);
4602 return SDValue(N, 0);
4603}
4604
Dan Gohman475871a2008-07-27 21:46:04 +00004605SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004606SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4607 SDValue Offset, ISD::MemIndexedMode AM) {
4608 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4609 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4610 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004612 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4613 FoldingSetNodeID ID;
4614 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004615 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004616 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004617 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004618 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004619 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004620 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004621
Dan Gohman95531882010-03-18 18:49:47 +00004622 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4623 ST->isTruncatingStore(),
4624 ST->getMemoryVT(),
4625 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004626 CSEMap.InsertNode(N, IP);
4627 AllNodes.push_back(N);
4628 return SDValue(N, 0);
4629}
4630
Owen Andersone50ed302009-08-10 22:56:29 +00004631SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004632 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004633 SDValue SV,
4634 unsigned Align) {
4635 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4636 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004637}
4638
Owen Andersone50ed302009-08-10 22:56:29 +00004639SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004640 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004641 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004642 case 0: return getNode(Opcode, DL, VT);
4643 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4644 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4645 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004646 default: break;
4647 }
4648
Dan Gohman475871a2008-07-27 21:46:04 +00004649 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004650 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004651 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004652 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004653}
4654
Owen Andersone50ed302009-08-10 22:56:29 +00004655SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004656 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004657 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004658 case 0: return getNode(Opcode, DL, VT);
4659 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4660 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4661 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004662 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004663 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004664
Chris Lattneref847df2005-04-09 03:27:28 +00004665 switch (Opcode) {
4666 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004667 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004668 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004669 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4670 "LHS and RHS of condition must have same type!");
4671 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4672 "True and False arms of SelectCC must have same type!");
4673 assert(Ops[2].getValueType() == VT &&
4674 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004675 break;
4676 }
4677 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004678 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004679 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4680 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004681 break;
4682 }
Chris Lattneref847df2005-04-09 03:27:28 +00004683 }
4684
Chris Lattner385328c2005-05-14 07:42:29 +00004685 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004686 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004687 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004688
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004689 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004690 FoldingSetNodeID ID;
4691 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004692 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004693
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004694 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004695 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004696
Dan Gohman95531882010-03-18 18:49:47 +00004697 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004698 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004699 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004700 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004701 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004702
Chris Lattneref847df2005-04-09 03:27:28 +00004703 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004704#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004705 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004706#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004707 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004708}
4709
Bill Wendling7ade28c2009-01-28 22:17:52 +00004710SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Benjamin Kramer3853f742013-03-07 20:33:29 +00004711 ArrayRef<EVT> ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004712 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004713 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004714 Ops, NumOps);
4715}
4716
Bill Wendling7ade28c2009-01-28 22:17:52 +00004717SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004718 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004719 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004720 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004721 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4722 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004723}
4724
Bill Wendling7ade28c2009-01-28 22:17:52 +00004725SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4726 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004727 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004728 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004729
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004730#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004731 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004732 // FIXME: figure out how to safely handle things like
4733 // int foo(int x) { return 1 << (x & 255); }
4734 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004735 case ISD::SRA_PARTS:
4736 case ISD::SRL_PARTS:
4737 case ISD::SHL_PARTS:
4738 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004740 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004741 else if (N3.getOpcode() == ISD::AND)
4742 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4743 // If the and is only masking out bits that cannot effect the shift,
4744 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004745 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004746 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004747 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004748 }
4749 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004750 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004751#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004752
Chris Lattner43247a12005-08-25 19:12:10 +00004753 // Memoize the node unless it returns a flag.
4754 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004755 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004756 FoldingSetNodeID ID;
4757 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004758 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004759 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004760 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004761
Dan Gohman0e5f1302008-07-07 23:02:41 +00004762 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004763 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004764 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004765 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004766 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004767 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4768 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004769 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004770 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004771 }
Chris Lattnera5682852006-08-07 23:03:03 +00004772 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004773 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004774 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004775 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004776 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004777 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004778 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004779 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4780 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004781 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004782 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004783 }
Chris Lattner43247a12005-08-25 19:12:10 +00004784 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004785 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004786#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004787 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004788#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004789 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004790}
4791
Bill Wendling7ade28c2009-01-28 22:17:52 +00004792SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4793 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004794}
4795
Bill Wendling7ade28c2009-01-28 22:17:52 +00004796SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4797 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004798 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004799 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004800}
4801
Bill Wendling7ade28c2009-01-28 22:17:52 +00004802SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4803 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004804 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004805 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004806}
4807
Bill Wendling7ade28c2009-01-28 22:17:52 +00004808SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4809 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004810 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004811 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004812}
4813
Bill Wendling7ade28c2009-01-28 22:17:52 +00004814SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4815 SDValue N1, SDValue N2, SDValue N3,
4816 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004818 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004819}
4820
Bill Wendling7ade28c2009-01-28 22:17:52 +00004821SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4822 SDValue N1, SDValue N2, SDValue N3,
4823 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004824 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004825 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004826}
4827
Owen Andersone50ed302009-08-10 22:56:29 +00004828SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004829 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004830}
4831
Owen Andersone50ed302009-08-10 22:56:29 +00004832SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004833 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4834 E = VTList.rend(); I != E; ++I)
4835 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4836 return *I;
4837
Owen Andersone50ed302009-08-10 22:56:29 +00004838 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004839 Array[0] = VT1;
4840 Array[1] = VT2;
4841 SDVTList Result = makeVTList(Array, 2);
4842 VTList.push_back(Result);
4843 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004844}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004845
Owen Andersone50ed302009-08-10 22:56:29 +00004846SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004847 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4848 E = VTList.rend(); I != E; ++I)
4849 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4850 I->VTs[2] == VT3)
4851 return *I;
4852
Owen Andersone50ed302009-08-10 22:56:29 +00004853 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004854 Array[0] = VT1;
4855 Array[1] = VT2;
4856 Array[2] = VT3;
4857 SDVTList Result = makeVTList(Array, 3);
4858 VTList.push_back(Result);
4859 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004860}
4861
Owen Andersone50ed302009-08-10 22:56:29 +00004862SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004863 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4864 E = VTList.rend(); I != E; ++I)
4865 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4866 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4867 return *I;
4868
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004869 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004870 Array[0] = VT1;
4871 Array[1] = VT2;
4872 Array[2] = VT3;
4873 Array[3] = VT4;
4874 SDVTList Result = makeVTList(Array, 4);
4875 VTList.push_back(Result);
4876 return Result;
4877}
4878
Owen Andersone50ed302009-08-10 22:56:29 +00004879SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004880 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004881 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004882 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004883 case 2: return getVTList(VTs[0], VTs[1]);
4884 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004885 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004886 default: break;
4887 }
4888
Dan Gohmane8be6c62008-07-17 19:10:17 +00004889 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4890 E = VTList.rend(); I != E; ++I) {
4891 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4892 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004893
Benjamin Kramerb26e2912012-07-19 10:46:05 +00004894 if (std::equal(&VTs[2], &VTs[NumVTs], &I->VTs[2]))
Dan Gohmane8be6c62008-07-17 19:10:17 +00004895 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004897
Owen Andersone50ed302009-08-10 22:56:29 +00004898 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004899 std::copy(VTs, VTs+NumVTs, Array);
4900 SDVTList Result = makeVTList(Array, NumVTs);
4901 VTList.push_back(Result);
4902 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004903}
4904
4905
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004906/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4907/// specified operands. If the resultant node already exists in the DAG,
4908/// this does not modify the specified node, instead it returns the node that
4909/// already exists. If the resultant node does not exist in the DAG, the
4910/// input node is returned. As a degenerate case, if you specify the same
4911/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004912SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004913 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004914
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004915 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004916 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004917
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004918 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004919 void *InsertPos = 0;
4920 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004921 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004922
Dan Gohman79acd2b2008-07-21 22:38:59 +00004923 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004924 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004925 if (!RemoveNodeFromCSEMaps(N))
4926 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004927
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004928 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004929 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004930
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004931 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004932 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004933 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004934}
4935
Dan Gohman027657d2010-06-18 15:30:29 +00004936SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004937 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004938
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004939 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004940 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004941 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004942
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004943 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004944 void *InsertPos = 0;
4945 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004946 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004947
Dan Gohman79acd2b2008-07-21 22:38:59 +00004948 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004949 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004950 if (!RemoveNodeFromCSEMaps(N))
4951 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004952
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004953 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004954 if (N->OperandList[0] != Op1)
4955 N->OperandList[0].set(Op1);
4956 if (N->OperandList[1] != Op2)
4957 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004958
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004959 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004960 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004961 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004962}
4963
Dan Gohman027657d2010-06-18 15:30:29 +00004964SDNode *SelectionDAG::
4965UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004966 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004967 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004968}
4969
Dan Gohman027657d2010-06-18 15:30:29 +00004970SDNode *SelectionDAG::
4971UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004972 SDValue Op3, SDValue Op4) {
4973 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004974 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004975}
4976
Dan Gohman027657d2010-06-18 15:30:29 +00004977SDNode *SelectionDAG::
4978UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004979 SDValue Op3, SDValue Op4, SDValue Op5) {
4980 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004981 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004982}
4983
Dan Gohman027657d2010-06-18 15:30:29 +00004984SDNode *SelectionDAG::
4985UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004986 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004987 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004988
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004989 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004990 bool AnyChange = false;
4991 for (unsigned i = 0; i != NumOps; ++i) {
4992 if (Ops[i] != N->getOperand(i)) {
4993 AnyChange = true;
4994 break;
4995 }
4996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004997
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004998 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004999 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005000
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005001 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005002 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005003 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005004 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005005
Dan Gohman7ceda162008-05-02 00:05:03 +00005006 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005007 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005008 if (!RemoveNodeFromCSEMaps(N))
5009 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005010
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005011 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005012 for (unsigned i = 0; i != NumOps; ++i)
5013 if (N->OperandList[i] != Ops[i])
5014 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005015
5016 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005017 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005018 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005019}
5020
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005021/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005022/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005023void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005024 // Unlike the code in MorphNodeTo that does this, we don't need to
5025 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005026 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5027 SDUse &Use = *I++;
5028 Use.set(SDValue());
5029 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005030}
Chris Lattner149c58c2005-08-16 18:17:10 +00005031
Dan Gohmane8be6c62008-07-17 19:10:17 +00005032/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5033/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005034///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005035SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005036 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005037 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005038 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00005039}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005040
Dan Gohmane8be6c62008-07-17 19:10:17 +00005041SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005042 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005043 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005044 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005045 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00005046}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005047
Dan Gohmane8be6c62008-07-17 19:10:17 +00005048SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005049 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005050 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005051 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005052 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005053 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00005054}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005055
Dan Gohmane8be6c62008-07-17 19:10:17 +00005056SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005057 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005058 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005059 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005060 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005061 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00005062}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005063
Dan Gohmane8be6c62008-07-17 19:10:17 +00005064SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005065 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00005066 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005067 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005068 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005069}
5070
Dan Gohmane8be6c62008-07-17 19:10:17 +00005071SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005072 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005073 unsigned NumOps) {
5074 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005075 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005076}
5077
Dan Gohmane8be6c62008-07-17 19:10:17 +00005078SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005079 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005080 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005081 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00005082}
5083
Dan Gohmane8be6c62008-07-17 19:10:17 +00005084SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005085 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00005086 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005087 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005088 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005089}
5090
Bill Wendling13d6d442008-12-01 23:28:22 +00005091SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005092 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00005093 const SDValue *Ops, unsigned NumOps) {
5094 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5095 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5096}
5097
Scott Michelfdc40a02009-02-17 22:15:04 +00005098SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005099 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005100 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005101 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005102 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005103 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005104}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005105
Scott Michelfdc40a02009-02-17 22:15:04 +00005106SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005107 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005108 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005109 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005110 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005111 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005112}
5113
Dan Gohmane8be6c62008-07-17 19:10:17 +00005114SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005115 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005116 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005117 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005118 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005119 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005120 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00005121}
5122
Dan Gohmane8be6c62008-07-17 19:10:17 +00005123SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005124 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005125 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005126 SDValue Op3) {
5127 SDVTList VTs = getVTList(VT1, VT2, VT3);
5128 SDValue Ops[] = { Op1, Op2, Op3 };
5129 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5130}
5131
5132SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00005133 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005134 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00005135 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5136 // Reset the NodeID to -1.
5137 N->setNodeId(-1);
5138 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005139}
5140
Devang Patel0508d042011-12-15 18:21:18 +00005141/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
5142/// the line number information on the merged node since it is not possible to
5143/// preserve the information that operation is associated with multiple lines.
5144/// This will make the debugger working better at -O0, were there is a higher
5145/// probability having other instructions associated with that line.
5146///
5147SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
5148 DebugLoc NLoc = N->getDebugLoc();
5149 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
5150 N->setDebugLoc(DebugLoc());
5151 }
5152 return N;
5153}
5154
Chris Lattner21221e32010-02-28 21:36:14 +00005155/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005156/// return type, opcode, and operands.
5157///
5158/// Note that MorphNodeTo returns the resultant node. If there is already a
5159/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00005160/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005161///
5162/// Using MorphNodeTo is faster than creating a new node and swapping it in
5163/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005164/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005165/// the node's users.
5166///
5167SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005168 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005169 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005170 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005171 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005172 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005173 FoldingSetNodeID ID;
5174 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005175 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00005176 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005177 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005178
Dan Gohman095cc292008-09-13 01:54:27 +00005179 if (!RemoveNodeFromCSEMaps(N))
5180 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005181
Dan Gohmane8be6c62008-07-17 19:10:17 +00005182 // Start the morphing.
5183 N->NodeType = Opc;
5184 N->ValueList = VTs.VTs;
5185 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005186
Dan Gohmane8be6c62008-07-17 19:10:17 +00005187 // Clear the operands list, updating used nodes to remove this from their
5188 // use list. Keep track of any operands that become dead as a result.
5189 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005190 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5191 SDUse &Use = *I++;
5192 SDNode *Used = Use.getNode();
5193 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005194 if (Used->use_empty())
5195 DeadNodeSet.insert(Used);
5196 }
5197
Dan Gohmanc76909a2009-09-25 20:36:54 +00005198 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5199 // Initialize the memory references information.
5200 MN->setMemRefs(0, 0);
5201 // If NumOps is larger than the # of operands we can have in a
5202 // MachineSDNode, reallocate the operand list.
5203 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5204 if (MN->OperandsNeedDelete)
5205 delete[] MN->OperandList;
5206 if (NumOps > array_lengthof(MN->LocalOperands))
5207 // We're creating a final node that will live unmorphed for the
5208 // remainder of the current SelectionDAG iteration, so we can allocate
5209 // the operands directly out of a pool with no recycling metadata.
5210 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005211 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005212 else
5213 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5214 MN->OperandsNeedDelete = false;
5215 } else
5216 MN->InitOperands(MN->OperandList, Ops, NumOps);
5217 } else {
5218 // If NumOps is larger than the # of operands we currently have, reallocate
5219 // the operand list.
5220 if (NumOps > N->NumOperands) {
5221 if (N->OperandsNeedDelete)
5222 delete[] N->OperandList;
5223 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005224 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005225 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005226 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005227 }
5228
5229 // Delete any nodes that are still dead after adding the uses for the
5230 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005231 if (!DeadNodeSet.empty()) {
5232 SmallVector<SDNode *, 16> DeadNodes;
5233 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5234 E = DeadNodeSet.end(); I != E; ++I)
5235 if ((*I)->use_empty())
5236 DeadNodes.push_back(*I);
5237 RemoveDeadNodes(DeadNodes);
5238 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005239
Dan Gohmane8be6c62008-07-17 19:10:17 +00005240 if (IP)
5241 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005242 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005243}
5244
Chris Lattner0fb094f2005-11-19 01:44:53 +00005245
Dan Gohman602b0c82009-09-25 18:54:59 +00005246/// getMachineNode - These are used for target selectors to create a new node
5247/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005248///
Dan Gohman602b0c82009-09-25 18:54:59 +00005249/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005250/// node of the specified opcode and operands, it returns that node instead of
5251/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005252MachineSDNode *
5253SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005254 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005255 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005256}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005257
Dan Gohmanc81b7832009-10-10 01:29:16 +00005258MachineSDNode *
5259SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005260 SDVTList VTs = getVTList(VT);
5261 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005262 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005263}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005264
Dan Gohmanc81b7832009-10-10 01:29:16 +00005265MachineSDNode *
5266SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5267 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005268 SDVTList VTs = getVTList(VT);
5269 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005270 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005271}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005272
Dan Gohmanc81b7832009-10-10 01:29:16 +00005273MachineSDNode *
5274SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5275 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005276 SDVTList VTs = getVTList(VT);
5277 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005278 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005279}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005280
Dan Gohmanc81b7832009-10-10 01:29:16 +00005281MachineSDNode *
5282SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005283 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005284 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005285 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005286}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005287
Dan Gohmanc81b7832009-10-10 01:29:16 +00005288MachineSDNode *
5289SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005290 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005291 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005292}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005293
Dan Gohmanc81b7832009-10-10 01:29:16 +00005294MachineSDNode *
5295SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5296 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005297 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005298 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005299 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005300}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005301
Dan Gohmanc81b7832009-10-10 01:29:16 +00005302MachineSDNode *
5303SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5304 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005305 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005306 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005307 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005308}
5309
Dan Gohmanc81b7832009-10-10 01:29:16 +00005310MachineSDNode *
5311SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5312 EVT VT1, EVT VT2, SDValue Op1,
5313 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005314 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005315 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005316 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005317}
5318
Dan Gohmanc81b7832009-10-10 01:29:16 +00005319MachineSDNode *
5320SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5321 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005322 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005323 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005324 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005325}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005326
Dan Gohmanc81b7832009-10-10 01:29:16 +00005327MachineSDNode *
5328SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5329 EVT VT1, EVT VT2, EVT VT3,
5330 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005331 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005332 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005333 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005334}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005335
Dan Gohmanc81b7832009-10-10 01:29:16 +00005336MachineSDNode *
5337SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5338 EVT VT1, EVT VT2, EVT VT3,
5339 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005340 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005341 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005342 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005343}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005344
Dan Gohmanc81b7832009-10-10 01:29:16 +00005345MachineSDNode *
5346SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5347 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005348 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005349 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005350 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005351}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005352
Dan Gohmanc81b7832009-10-10 01:29:16 +00005353MachineSDNode *
5354SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5355 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005356 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005357 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005358 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005359}
5360
Dan Gohmanc81b7832009-10-10 01:29:16 +00005361MachineSDNode *
5362SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005363 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005364 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005365 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liao2a8bea72013-04-19 22:22:57 +00005366 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005367}
5368
Dan Gohmanc81b7832009-10-10 01:29:16 +00005369MachineSDNode *
5370SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005371 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005372 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005373 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005374 void *IP = 0;
Michael Liao2a8bea72013-04-19 22:22:57 +00005375 const SDValue *Ops = OpsArray.data();
5376 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005377
5378 if (DoCSE) {
5379 FoldingSetNodeID ID;
5380 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5381 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005382 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5383 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5384 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005385 }
5386
5387 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005388 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005389
5390 // Initialize the operands list.
5391 if (NumOps > array_lengthof(N->LocalOperands))
5392 // We're creating a final node that will live unmorphed for the
5393 // remainder of the current SelectionDAG iteration, so we can allocate
5394 // the operands directly out of a pool with no recycling metadata.
5395 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5396 Ops, NumOps);
5397 else
5398 N->InitOperands(N->LocalOperands, Ops, NumOps);
5399 N->OperandsNeedDelete = false;
5400
5401 if (DoCSE)
5402 CSEMap.InsertNode(N, IP);
5403
5404 AllNodes.push_back(N);
5405#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005406 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005407#endif
5408 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005409}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005410
Dan Gohman6a402dc2009-08-19 18:16:17 +00005411/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005412/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005413SDValue
5414SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5415 SDValue Operand) {
5416 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005417 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005418 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005419 return SDValue(Subreg, 0);
5420}
5421
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005422/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005423/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005424SDValue
5425SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5426 SDValue Operand, SDValue Subreg) {
5427 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005428 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005429 VT, Operand, Subreg, SRIdxVal);
5430 return SDValue(Result, 0);
5431}
5432
Evan Cheng08b11732008-03-22 01:55:50 +00005433/// getNodeIfExists - Get the specified node if it's already available, or
5434/// else return NULL.
5435SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005436 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005437 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005438 FoldingSetNodeID ID;
5439 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5440 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005441 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005442 return E;
5443 }
5444 return NULL;
5445}
5446
Evan Cheng31441b72010-03-29 20:48:30 +00005447/// getDbgValue - Creates a SDDbgValue node.
5448///
5449SDDbgValue *
5450SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5451 DebugLoc DL, unsigned O) {
5452 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5453}
5454
5455SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005456SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005457 DebugLoc DL, unsigned O) {
5458 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5459}
5460
5461SDDbgValue *
5462SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5463 DebugLoc DL, unsigned O) {
5464 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5465}
5466
Dan Gohmana72d2a22010-03-03 21:33:37 +00005467namespace {
5468
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005469/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005470/// pointed to by a use iterator is deleted, increment the use iterator
5471/// so that it doesn't dangle.
5472///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005473class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005474 SDNode::use_iterator &UI;
5475 SDNode::use_iterator &UE;
5476
5477 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5478 // Increment the iterator as needed.
5479 while (UI != UE && N == *UI)
5480 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005481 }
5482
5483public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005484 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005485 SDNode::use_iterator &ui,
5486 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005487 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005488};
5489
5490}
5491
Evan Cheng99157a02006-08-07 22:13:29 +00005492/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005493/// This can cause recursive merging of nodes in the DAG.
5494///
Chris Lattner11d049c2008-02-03 03:35:22 +00005495/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005496///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005497void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005498 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005499 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005500 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005501 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005502
Dan Gohman39946102009-01-25 16:29:12 +00005503 // Iterate over all the existing uses of From. New uses will be added
5504 // to the beginning of the use list, which we avoid visiting.
5505 // This specifically avoids visiting uses of From that arise while the
5506 // replacement is happening, because any such uses would be the result
5507 // of CSE: If an existing node looks like From after one of its operands
5508 // is replaced by To, we don't want to replace of all its users with To
5509 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005510 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005511 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005512 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005513 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005514
Chris Lattner8b8749f2005-08-17 19:00:20 +00005515 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005516 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005517
Dan Gohman39946102009-01-25 16:29:12 +00005518 // A user can appear in a use list multiple times, and when this
5519 // happens the uses are usually next to each other in the list.
5520 // To help reduce the number of CSE recomputations, process all
5521 // the uses of this user that we can find this way.
5522 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005523 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005524 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005525 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005526 } while (UI != UE && *UI == User);
5527
5528 // Now that we have modified User, add it back to the CSE maps. If it
5529 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005530 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005531 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005532
5533 // If we just RAUW'd the root, take note.
5534 if (FromN == getRoot())
5535 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005536}
5537
5538/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5539/// This can cause recursive merging of nodes in the DAG.
5540///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005541/// This version assumes that for each value of From, there is a
5542/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005543///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005544void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005545#ifndef NDEBUG
5546 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5547 assert((!From->hasAnyUseOfValue(i) ||
5548 From->getValueType(i) == To->getValueType(i)) &&
5549 "Cannot use this version of ReplaceAllUsesWith!");
5550#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005551
5552 // Handle the trivial case.
5553 if (From == To)
5554 return;
5555
Dan Gohmandbe664a2009-01-19 21:44:21 +00005556 // Iterate over just the existing users of From. See the comments in
5557 // the ReplaceAllUsesWith above.
5558 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005559 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005560 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005561 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005562
Chris Lattner8b52f212005-08-26 18:36:28 +00005563 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005564 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005565
Dan Gohman39946102009-01-25 16:29:12 +00005566 // A user can appear in a use list multiple times, and when this
5567 // happens the uses are usually next to each other in the list.
5568 // To help reduce the number of CSE recomputations, process all
5569 // the uses of this user that we can find this way.
5570 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005571 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005572 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005573 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005574 } while (UI != UE && *UI == User);
5575
5576 // Now that we have modified User, add it back to the CSE maps. If it
5577 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005578 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005579 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005580
5581 // If we just RAUW'd the root, take note.
5582 if (From == getRoot().getNode())
5583 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005584}
5585
Chris Lattner8b52f212005-08-26 18:36:28 +00005586/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5587/// This can cause recursive merging of nodes in the DAG.
5588///
5589/// This version can replace From with any result values. To must match the
5590/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005591void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005592 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005593 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005594
Dan Gohmandbe664a2009-01-19 21:44:21 +00005595 // Iterate over just the existing users of From. See the comments in
5596 // the ReplaceAllUsesWith above.
5597 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005598 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005599 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005600 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005601
Chris Lattner7b2880c2005-08-24 22:44:39 +00005602 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005603 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005604
Dan Gohman39946102009-01-25 16:29:12 +00005605 // A user can appear in a use list multiple times, and when this
5606 // happens the uses are usually next to each other in the list.
5607 // To help reduce the number of CSE recomputations, process all
5608 // the uses of this user that we can find this way.
5609 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005610 SDUse &Use = UI.getUse();
5611 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005612 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005613 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005614 } while (UI != UE && *UI == User);
5615
5616 // Now that we have modified User, add it back to the CSE maps. If it
5617 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005618 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005619 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005620
5621 // If we just RAUW'd the root, take note.
5622 if (From == getRoot().getNode())
5623 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005624}
5625
Chris Lattner012f2412006-02-17 21:58:01 +00005626/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005627/// uses of other values produced by From.getNode() alone. The Deleted
5628/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005629void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005630 // Handle the really simple, really trivial case efficiently.
5631 if (From == To) return;
5632
Chris Lattner012f2412006-02-17 21:58:01 +00005633 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005634 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005635 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005636 return;
5637 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005638
Dan Gohman39946102009-01-25 16:29:12 +00005639 // Iterate over just the existing users of From. See the comments in
5640 // the ReplaceAllUsesWith above.
5641 SDNode::use_iterator UI = From.getNode()->use_begin(),
5642 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005643 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005644 while (UI != UE) {
5645 SDNode *User = *UI;
5646 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005647
Dan Gohman39946102009-01-25 16:29:12 +00005648 // A user can appear in a use list multiple times, and when this
5649 // happens the uses are usually next to each other in the list.
5650 // To help reduce the number of CSE recomputations, process all
5651 // the uses of this user that we can find this way.
5652 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005653 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005654
5655 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005656 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005657 ++UI;
5658 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005659 }
Dan Gohman39946102009-01-25 16:29:12 +00005660
5661 // If this node hasn't been modified yet, it's still in the CSE maps,
5662 // so remove its old self from the CSE maps.
5663 if (!UserRemovedFromCSEMaps) {
5664 RemoveNodeFromCSEMaps(User);
5665 UserRemovedFromCSEMaps = true;
5666 }
5667
5668 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005669 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005670 } while (UI != UE && *UI == User);
5671
5672 // We are iterating over all uses of the From node, so if a use
5673 // doesn't use the specific value, no changes are made.
5674 if (!UserRemovedFromCSEMaps)
5675 continue;
5676
Chris Lattner01d029b2007-10-15 06:10:22 +00005677 // Now that we have modified User, add it back to the CSE maps. If it
5678 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005679 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005680 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005681
5682 // If we just RAUW'd the root, take note.
5683 if (From == getRoot())
5684 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005685}
5686
Dan Gohman39946102009-01-25 16:29:12 +00005687namespace {
5688 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5689 /// to record information about a use.
5690 struct UseMemo {
5691 SDNode *User;
5692 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005693 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005694 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005695
5696 /// operator< - Sort Memos by User.
5697 bool operator<(const UseMemo &L, const UseMemo &R) {
5698 return (intptr_t)L.User < (intptr_t)R.User;
5699 }
Dan Gohman39946102009-01-25 16:29:12 +00005700}
5701
Dan Gohmane8be6c62008-07-17 19:10:17 +00005702/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005703/// uses of other values produced by From.getNode() alone. The same value
5704/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005705/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005706void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5707 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005708 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005709 // Handle the simple, trivial case efficiently.
5710 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005711 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005712
Dan Gohman39946102009-01-25 16:29:12 +00005713 // Read up all the uses and make records of them. This helps
5714 // processing new uses that are introduced during the
5715 // replacement process.
5716 SmallVector<UseMemo, 4> Uses;
5717 for (unsigned i = 0; i != Num; ++i) {
5718 unsigned FromResNo = From[i].getResNo();
5719 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005720 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005721 E = FromNode->use_end(); UI != E; ++UI) {
5722 SDUse &Use = UI.getUse();
5723 if (Use.getResNo() == FromResNo) {
5724 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005725 Uses.push_back(Memo);
5726 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005727 }
Dan Gohman39946102009-01-25 16:29:12 +00005728 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005729
Dan Gohmane7852d02009-01-26 04:35:06 +00005730 // Sort the uses, so that all the uses from a given User are together.
5731 std::sort(Uses.begin(), Uses.end());
5732
Dan Gohman39946102009-01-25 16:29:12 +00005733 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5734 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005735 // We know that this user uses some value of From. If it is the right
5736 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005737 SDNode *User = Uses[UseIndex].User;
5738
5739 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005740 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005741
Dan Gohmane7852d02009-01-26 04:35:06 +00005742 // The Uses array is sorted, so all the uses for a given User
5743 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005744 // To help reduce the number of CSE recomputations, process all
5745 // the uses of this user that we can find this way.
5746 do {
5747 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005748 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005749 ++UseIndex;
5750
Dan Gohmane7852d02009-01-26 04:35:06 +00005751 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005752 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5753
Dan Gohmane8be6c62008-07-17 19:10:17 +00005754 // Now that we have modified User, add it back to the CSE maps. If it
5755 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005756 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005757 }
5758}
5759
Evan Chenge6f35d82006-08-01 08:20:41 +00005760/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005761/// based on their topological order. It returns the maximum id and a vector
5762/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005763unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005764
Dan Gohmanf06c8352008-09-30 18:30:35 +00005765 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005766
Dan Gohmanf06c8352008-09-30 18:30:35 +00005767 // SortedPos tracks the progress of the algorithm. Nodes before it are
5768 // sorted, nodes after it are unsorted. When the algorithm completes
5769 // it is at the end of the list.
5770 allnodes_iterator SortedPos = allnodes_begin();
5771
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005772 // Visit all the nodes. Move nodes with no operands to the front of
5773 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005774 // operand count. Before we do this, the Node Id fields of the nodes
5775 // may contain arbitrary values. After, the Node Id fields for nodes
5776 // before SortedPos will contain the topological sort index, and the
5777 // Node Id fields for nodes At SortedPos and after will contain the
5778 // count of outstanding operands.
5779 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5780 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005781 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005782 unsigned Degree = N->getNumOperands();
5783 if (Degree == 0) {
5784 // A node with no uses, add it to the result array immediately.
5785 N->setNodeId(DAGSize++);
5786 allnodes_iterator Q = N;
5787 if (Q != SortedPos)
5788 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005789 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005790 ++SortedPos;
5791 } else {
5792 // Temporarily use the Node Id as scratch space for the degree count.
5793 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005794 }
5795 }
5796
Chad Rosierf496dea2012-05-21 17:13:41 +00005797 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005798 // such that by the time the end is reached all nodes will be sorted.
5799 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5800 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005801 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005802 // N is in sorted position, so all its uses have one less operand
5803 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005804 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5805 UI != UE; ++UI) {
5806 SDNode *P = *UI;
5807 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005808 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005809 --Degree;
5810 if (Degree == 0) {
5811 // All of P's operands are sorted, so P may sorted now.
5812 P->setNodeId(DAGSize++);
5813 if (P != SortedPos)
5814 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005815 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005816 ++SortedPos;
5817 } else {
5818 // Update P's outstanding operand count.
5819 P->setNodeId(Degree);
5820 }
5821 }
David Greene221925e2010-01-20 00:59:23 +00005822 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005823#ifndef NDEBUG
5824 SDNode *S = ++I;
5825 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005826 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005827#endif
5828 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005829 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005830 }
5831
5832 assert(SortedPos == AllNodes.end() &&
5833 "Topological sort incomplete!");
5834 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5835 "First node in topological sort is not the entry token!");
5836 assert(AllNodes.front().getNodeId() == 0 &&
5837 "First node in topological sort has non-zero id!");
5838 assert(AllNodes.front().getNumOperands() == 0 &&
5839 "First node in topological sort has operands!");
5840 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5841 "Last node in topologic sort has unexpected id!");
5842 assert(AllNodes.back().use_empty() &&
5843 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005844 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005845 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005846}
5847
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005848/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005849void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005850 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005851 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005852}
Evan Chenge6f35d82006-08-01 08:20:41 +00005853
Bill Wendling0777e922009-12-21 21:59:52 +00005854/// GetOrdering - Get the order for the SDNode.
5855unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5856 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005857 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005858}
5859
Evan Chengbfcb3052010-03-25 01:38:16 +00005860/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5861/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005862void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5863 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005864 if (SD)
5865 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005866}
Evan Cheng091cba12006-07-27 06:39:06 +00005867
Devang Patela2e868d2011-01-25 23:27:42 +00005868/// TransferDbgValues - Transfer SDDbgValues.
5869void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5870 if (From == To || !From.getNode()->getHasDebugValue())
5871 return;
5872 SDNode *FromNode = From.getNode();
5873 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005874 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005875 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005876 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005877 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005878 SDDbgValue *Dbg = *I;
5879 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5880 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5881 Dbg->getOffset(), Dbg->getDebugLoc(),
5882 Dbg->getOrder());
5883 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005884 }
5885 }
Devang Patela778f5c2011-02-18 22:43:42 +00005886 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5887 E = ClonedDVs.end(); I != E; ++I)
5888 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005889}
5890
Jim Laskey58b968b2005-08-17 20:08:02 +00005891//===----------------------------------------------------------------------===//
5892// SDNode Class
5893//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005894
Chris Lattner48b85922007-02-04 02:41:42 +00005895HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005896 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005897}
5898
Devang Patel0d881da2010-07-06 22:08:15 +00005899GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5900 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005901 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005902 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005903 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005904}
Chris Lattner48b85922007-02-04 02:41:42 +00005905
Owen Andersone50ed302009-08-10 22:56:29 +00005906MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005907 MachineMemOperand *mmo)
5908 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005909 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005910 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005911 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005912 assert(isNonTemporal() == MMO->isNonTemporal() &&
5913 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005914 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005915}
5916
Scott Michelfdc40a02009-02-17 22:15:04 +00005917MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005918 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005919 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005920 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005921 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005922 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005923 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005924 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5925 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005926}
5927
Jim Laskey583bd472006-10-27 23:46:08 +00005928/// Profile - Gather unique data for the node.
5929///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005930void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005931 AddNodeIDNode(ID, this);
5932}
5933
Owen Andersond8110fb2009-08-25 22:27:22 +00005934namespace {
5935 struct EVTArray {
5936 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005937
Owen Andersond8110fb2009-08-25 22:27:22 +00005938 EVTArray() {
5939 VTs.reserve(MVT::LAST_VALUETYPE);
5940 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5941 VTs.push_back(MVT((MVT::SimpleValueType)i));
5942 }
5943 };
5944}
5945
Owen Andersone50ed302009-08-10 22:56:29 +00005946static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005947static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005948static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005949
Chris Lattnera3255112005-11-08 23:30:28 +00005950/// getValueTypeList - Return a pointer to the specified value type.
5951///
Owen Andersone50ed302009-08-10 22:56:29 +00005952const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005953 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005954 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005955 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005956 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005957 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005958 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005959 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005960 }
Chris Lattnera3255112005-11-08 23:30:28 +00005961}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005962
Chris Lattner5c884562005-01-12 18:37:47 +00005963/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5964/// indicated value. This method ignores uses of other values defined by this
5965/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005966bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005967 assert(Value < getNumValues() && "Bad value!");
5968
Roman Levensteindc1adac2008-04-07 10:06:32 +00005969 // TODO: Only iterate over uses of a given value of the node
5970 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005971 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005972 if (NUses == 0)
5973 return false;
5974 --NUses;
5975 }
Chris Lattner5c884562005-01-12 18:37:47 +00005976 }
5977
5978 // Found exactly the right number of uses?
5979 return NUses == 0;
5980}
5981
5982
Evan Cheng33d55952007-08-02 05:29:38 +00005983/// hasAnyUseOfValue - Return true if there are any use of the indicated
5984/// value. This method ignores uses of other values defined by this operation.
5985bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5986 assert(Value < getNumValues() && "Bad value!");
5987
Dan Gohman1373c1c2008-07-09 22:39:01 +00005988 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005989 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005990 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005991
5992 return false;
5993}
5994
5995
Dan Gohman2a629952008-07-27 18:06:42 +00005996/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005997///
Dan Gohman2a629952008-07-27 18:06:42 +00005998bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005999 bool Seen = false;
6000 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006001 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006002 if (User == this)
6003 Seen = true;
6004 else
6005 return false;
6006 }
6007
6008 return Seen;
6009}
6010
Evan Chenge6e97e62006-11-03 07:31:32 +00006011/// isOperand - Return true if this node is an operand of N.
6012///
Dan Gohman475871a2008-07-27 21:46:04 +00006013bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006014 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6015 if (*this == N->getOperand(i))
6016 return true;
6017 return false;
6018}
6019
Evan Cheng917be682008-03-04 00:41:45 +00006020bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006021 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006022 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006023 return true;
6024 return false;
6025}
Evan Cheng4ee62112006-02-05 06:29:23 +00006026
Chris Lattner572dee72008-01-16 05:49:24 +00006027/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006028/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006029/// side-effecting instructions on any chain path. In practice, this looks
6030/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006031/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006032bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006033 unsigned Depth) const {
6034 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006035
Chris Lattner572dee72008-01-16 05:49:24 +00006036 // Don't search too deeply, we just want to be able to see through
6037 // TokenFactor's etc.
6038 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006039
Chris Lattner572dee72008-01-16 05:49:24 +00006040 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006041 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006042 if (getOpcode() == ISD::TokenFactor) {
6043 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006044 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6045 return false;
6046 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006047 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006048
Chris Lattner572dee72008-01-16 05:49:24 +00006049 // Loads don't have side effects, look through them.
6050 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6051 if (!Ld->isVolatile())
6052 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6053 }
6054 return false;
6055}
6056
Lang Hames944520f2011-07-07 04:31:51 +00006057/// hasPredecessor - Return true if N is a predecessor of this node.
6058/// N is either an operand of this node, or can be reached by recursively
6059/// traversing up the operands.
6060/// NOTE: This is an expensive method. Use it carefully.
6061bool SDNode::hasPredecessor(const SDNode *N) const {
6062 SmallPtrSet<const SDNode *, 32> Visited;
6063 SmallVector<const SDNode *, 16> Worklist;
6064 return hasPredecessorHelper(N, Visited, Worklist);
6065}
Dan Gohman6688d612009-10-28 03:44:30 +00006066
Lang Hames944520f2011-07-07 04:31:51 +00006067bool SDNode::hasPredecessorHelper(const SDNode *N,
6068 SmallPtrSet<const SDNode *, 32> &Visited,
6069 SmallVector<const SDNode *, 16> &Worklist) const {
6070 if (Visited.empty()) {
6071 Worklist.push_back(this);
6072 } else {
6073 // Take a look in the visited set. If we've already encountered this node
6074 // we needn't search further.
6075 if (Visited.count(N))
6076 return true;
6077 }
6078
6079 // Haven't visited N yet. Continue the search.
6080 while (!Worklist.empty()) {
6081 const SDNode *M = Worklist.pop_back_val();
6082 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6083 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006084 if (Visited.insert(Op))
6085 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006086 if (Op == N)
6087 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006088 }
Lang Hames944520f2011-07-07 04:31:51 +00006089 }
Dan Gohman6688d612009-10-28 03:44:30 +00006090
6091 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006092}
6093
Evan Chengc5484282006-10-04 00:56:09 +00006094uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6095 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006096 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006097}
6098
Mon P Wangcd6e7252009-11-30 02:42:02 +00006099SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6100 assert(N->getNumValues() == 1 &&
6101 "Can't unroll a vector with multiple results!");
6102
6103 EVT VT = N->getValueType(0);
6104 unsigned NE = VT.getVectorNumElements();
6105 EVT EltVT = VT.getVectorElementType();
6106 DebugLoc dl = N->getDebugLoc();
6107
6108 SmallVector<SDValue, 8> Scalars;
6109 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6110
6111 // If ResNE is 0, fully unroll the vector op.
6112 if (ResNE == 0)
6113 ResNE = NE;
6114 else if (NE > ResNE)
6115 NE = ResNE;
6116
6117 unsigned i;
6118 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006119 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006120 SDValue Operand = N->getOperand(j);
6121 EVT OperandVT = Operand.getValueType();
6122 if (OperandVT.isVector()) {
6123 // A vector operand; extract a single element.
6124 EVT OperandEltVT = OperandVT.getVectorElementType();
6125 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6126 OperandEltVT,
6127 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00006128 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006129 } else {
6130 // A scalar operand; just use it as is.
6131 Operands[j] = Operand;
6132 }
6133 }
6134
6135 switch (N->getOpcode()) {
6136 default:
6137 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6138 &Operands[0], Operands.size()));
6139 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006140 case ISD::VSELECT:
6141 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6142 &Operands[0], Operands.size()));
6143 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006144 case ISD::SHL:
6145 case ISD::SRA:
6146 case ISD::SRL:
6147 case ISD::ROTL:
6148 case ISD::ROTR:
6149 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00006150 getShiftAmountOperand(Operands[0].getValueType(),
6151 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006152 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006153 case ISD::SIGN_EXTEND_INREG:
6154 case ISD::FP_ROUND_INREG: {
6155 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6156 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6157 Operands[0],
6158 getValueType(ExtVT)));
6159 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006160 }
6161 }
6162
6163 for (; i < ResNE; ++i)
6164 Scalars.push_back(getUNDEF(EltVT));
6165
6166 return getNode(ISD::BUILD_VECTOR, dl,
6167 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6168 &Scalars[0], Scalars.size());
6169}
6170
Evan Cheng64fa4a92009-12-09 01:36:00 +00006171
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006172/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6173/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006174/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006175bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006176 unsigned Bytes, int Dist) const {
6177 if (LD->getChain() != Base->getChain())
6178 return false;
6179 EVT VT = LD->getValueType(0);
6180 if (VT.getSizeInBits() / 8 != Bytes)
6181 return false;
6182
6183 SDValue Loc = LD->getOperand(1);
6184 SDValue BaseLoc = Base->getOperand(1);
6185 if (Loc.getOpcode() == ISD::FrameIndex) {
6186 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6187 return false;
6188 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6189 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6190 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6191 int FS = MFI->getObjectSize(FI);
6192 int BFS = MFI->getObjectSize(BFI);
6193 if (FS != BFS || FS != (int)Bytes) return false;
6194 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6195 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006196
6197 // Handle X+C
6198 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6199 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6200 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006201
Dan Gohman46510a72010-04-15 01:51:59 +00006202 const GlobalValue *GV1 = NULL;
6203 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006204 int64_t Offset1 = 0;
6205 int64_t Offset2 = 0;
6206 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6207 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6208 if (isGA1 && isGA2 && GV1 == GV2)
6209 return Offset1 == (Offset2 + Dist*Bytes);
6210 return false;
6211}
6212
6213
Evan Chengf2dc5c72009-12-09 01:04:59 +00006214/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6215/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006216unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006217 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006218 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006219 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006220 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006221 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006222 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006223 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Micah Villmow3574eca2012-10-08 16:38:25 +00006224 TLI.getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006225 unsigned AlignBits = KnownZero.countTrailingOnes();
6226 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6227 if (Align)
6228 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006229 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006230
Evan Chengf2dc5c72009-12-09 01:04:59 +00006231 // If this is a direct reference to a stack slot, use information about the
6232 // stack slot's alignment.
6233 int FrameIdx = 1 << 31;
6234 int64_t FrameOffset = 0;
6235 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6236 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006237 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006238 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006239 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006240 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6241 FrameOffset = Ptr.getConstantOperandVal(1);
6242 }
6243
6244 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006245 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006246 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6247 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006248 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006249 }
6250
6251 return 0;
6252}
6253
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006254// getAddressSpace - Return the address space this GlobalAddress belongs to.
6255unsigned GlobalAddressSDNode::getAddressSpace() const {
6256 return getGlobal()->getType()->getAddressSpace();
6257}
6258
6259
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006260Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006261 if (isMachineConstantPoolEntry())
6262 return Val.MachineCPVal->getType();
6263 return Val.ConstVal->getType();
6264}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006265
Bob Wilson24e338e2009-03-02 23:24:16 +00006266bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6267 APInt &SplatUndef,
6268 unsigned &SplatBitSize,
6269 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006270 unsigned MinSplatBits,
6271 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006272 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006273 assert(VT.isVector() && "Expected a vector type");
6274 unsigned sz = VT.getSizeInBits();
6275 if (MinSplatBits > sz)
6276 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006277
Bob Wilson24e338e2009-03-02 23:24:16 +00006278 SplatValue = APInt(sz, 0);
6279 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006280
Bob Wilson24e338e2009-03-02 23:24:16 +00006281 // Get the bits. Bits with undefined values (when the corresponding element
6282 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6283 // in SplatValue. If any of the values are not constant, give up and return
6284 // false.
6285 unsigned int nOps = getNumOperands();
6286 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6287 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006288
6289 for (unsigned j = 0; j < nOps; ++j) {
6290 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006291 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006292 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006293
Bob Wilson24e338e2009-03-02 23:24:16 +00006294 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006295 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006296 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006297 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006298 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006299 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006300 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006301 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006302 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006303 }
6304
Bob Wilson24e338e2009-03-02 23:24:16 +00006305 // The build_vector is all constants or undefs. Find the smallest element
6306 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006307
Bob Wilson24e338e2009-03-02 23:24:16 +00006308 HasAnyUndefs = (SplatUndef != 0);
6309 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006310
Bob Wilson24e338e2009-03-02 23:24:16 +00006311 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006312 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6313 APInt LowValue = SplatValue.trunc(HalfSize);
6314 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6315 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006316
Bob Wilson24e338e2009-03-02 23:24:16 +00006317 // If the two halves do not match (ignoring undef bits), stop here.
6318 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6319 MinSplatBits > HalfSize)
6320 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006321
Bob Wilson24e338e2009-03-02 23:24:16 +00006322 SplatValue = HighValue | LowValue;
6323 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006324
Bob Wilson24e338e2009-03-02 23:24:16 +00006325 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006326 }
6327
Bob Wilson24e338e2009-03-02 23:24:16 +00006328 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006329 return true;
6330}
Nate Begeman9008ca62009-04-27 18:41:29 +00006331
Owen Andersone50ed302009-08-10 22:56:29 +00006332bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006333 // Find the first non-undef value in the shuffle mask.
6334 unsigned i, e;
6335 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6336 /* search */;
6337
Nate Begemana6415752009-04-29 18:13:31 +00006338 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006339
Nate Begeman5a5ca152009-04-29 05:20:52 +00006340 // Make sure all remaining elements are either undef or the same as the first
6341 // non-undef value.
6342 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006343 if (Mask[i] >= 0 && Mask[i] != Idx)
6344 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006345 return true;
6346}
David Greenecf495bc2010-01-20 20:13:31 +00006347
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006348#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006349static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006350 SmallPtrSet<const SDNode*, 32> &Visited,
6351 SmallPtrSet<const SDNode*, 32> &Checked) {
6352 // If this node has already been checked, don't check it again.
6353 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006354 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006355
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006356 // If a node has already been visited on this depth-first walk, reject it as
6357 // a cycle.
6358 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006359 dbgs() << "Offending node:\n";
6360 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006361 errs() << "Detected cycle in SelectionDAG\n";
6362 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006363 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006364
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006365 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6366 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006367
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006368 Checked.insert(N);
6369 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006370}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006371#endif
David Greenecf495bc2010-01-20 20:13:31 +00006372
6373void llvm::checkForCycles(const llvm::SDNode *N) {
6374#ifdef XDEBUG
6375 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006376 SmallPtrSet<const SDNode*, 32> visited;
6377 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006378 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006379#endif
6380}
6381
6382void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6383 checkForCycles(DAG->getRoot().getNode());
6384}