blob: 4cb63ce9a66c6f20e908438d2f7f0198fa59e59f [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"
22#include "llvm/Analysis/ValueTracking.h"
23#include "llvm/Assembly/Writer.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000024#include "llvm/CallingConv.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"
Chris Lattnerc3aae252005-01-07 07:46:32 +000029#include "llvm/Constants.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/DataLayout.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000031#include "llvm/DebugInfo.h"
32#include "llvm/DerivedTypes.h"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000033#include "llvm/Function.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000034#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000035#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000036#include "llvm/Intrinsics.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000037#include "llvm/Support/CommandLine.h"
David Greene55d146e2010-01-05 01:24:36 +000038#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000040#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000041#include "llvm/Support/MathExtras.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000042#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000043#include "llvm/Support/raw_ostream.h"
Evan Chenge07f85e2012-12-11 23:26:14 +000044#include "llvm/TargetTransformInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000045#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
Owen Andersone50ed302009-08-10 22:56:29 +000063static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000064 switch (VT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +000065 default: llvm_unreachable("Unknown FP format");
Owen Andersond7484e52012-04-05 18:50:32 +000066 case MVT::f16: return &APFloat::IEEEhalf;
Owen Anderson825b72b2009-08-11 20:47:22 +000067 case MVT::f32: return &APFloat::IEEEsingle;
68 case MVT::f64: return &APFloat::IEEEdouble;
69 case MVT::f80: return &APFloat::x87DoubleExtended;
70 case MVT::f128: return &APFloat::IEEEquad;
71 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerec4a5672008-03-05 06:48:13 +000072 }
73}
74
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000075// Default null implementations of the callbacks.
76void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
77void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000078
Jim Laskey58b968b2005-08-17 20:08:02 +000079//===----------------------------------------------------------------------===//
80// ConstantFPSDNode Class
81//===----------------------------------------------------------------------===//
82
83/// isExactlyValue - We don't rely on operator== working on double values, as
84/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
85/// As such, this method can be used to do an exact bit-for-bit comparison of
86/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000087bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000088 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000089}
90
Owen Andersone50ed302009-08-10 22:56:29 +000091bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000092 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000093 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000094
Dale Johannesenf04afdb2007-08-30 00:23:21 +000095 // convert modifies in place, so make a copy.
96 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000097 bool losesInfo;
Owen Andersone50ed302009-08-10 22:56:29 +000098 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000099 &losesInfo);
100 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000101}
102
Jim Laskey58b968b2005-08-17 20:08:02 +0000103//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +0000104// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +0000105//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +0000106
Evan Chenga8df1662006-03-27 06:58:47 +0000107/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +0000108/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000109bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000110 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000111 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000112 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000113
Evan Chenga8df1662006-03-27 06:58:47 +0000114 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
Chris Lattner61d43992006-03-25 22:57:01 +0000116 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Chris Lattner61d43992006-03-25 22:57:01 +0000118 // Skip over all of the undef values.
119 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
120 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000121
Chris Lattner61d43992006-03-25 22:57:01 +0000122 // Do not accept an all-undef vector.
123 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000124
Chris Lattner61d43992006-03-25 22:57:01 +0000125 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000126 // elements. We have to be a bit careful here, as the type of the constant
127 // may not be the same as the type of the vector elements due to type
128 // legalization (the elements are promoted to a legal type for the target and
129 // a vector of a type may be legal when the base element type is not).
130 // We only want to check enough bits to cover the vector elements, because
131 // we care if the resultant vector is all ones, not whether the individual
132 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000133 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000134 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000135 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
136 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000137 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000138 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
139 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000140 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000141 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000142 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000143
Chris Lattner61d43992006-03-25 22:57:01 +0000144 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000145 // undefs. Even with the above element type twiddling, this should be OK, as
146 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000147 for (++i; i != e; ++i)
148 if (N->getOperand(i) != NotZero &&
149 N->getOperand(i).getOpcode() != ISD::UNDEF)
150 return false;
151 return true;
152}
153
154
Evan Cheng4a147842006-03-26 09:50:58 +0000155/// isBuildVectorAllZeros - Return true if the specified node is a
156/// BUILD_VECTOR where all of the elements are 0 or undef.
157bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000158 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000159 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000160 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000161
Evan Cheng4a147842006-03-26 09:50:58 +0000162 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000163
Evan Chenga8df1662006-03-27 06:58:47 +0000164 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000165
Evan Chenga8df1662006-03-27 06:58:47 +0000166 // Skip over all of the undef values.
167 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
168 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000169
Evan Chenga8df1662006-03-27 06:58:47 +0000170 // Do not accept an all-undef vector.
171 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000172
Dan Gohman68f32cb2009-06-04 16:49:15 +0000173 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000174 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000175 SDValue Zero = N->getOperand(i);
Jakub Staszak554c6762012-09-30 21:24:57 +0000176 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
177 if (!CN->isNullValue())
Evan Chenga8df1662006-03-27 06:58:47 +0000178 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000179 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
180 if (!CFPN->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000181 return false;
182 } else
183 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000184
Dan Gohman68f32cb2009-06-04 16:49:15 +0000185 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000186 // undefs.
187 for (++i; i != e; ++i)
188 if (N->getOperand(i) != Zero &&
189 N->getOperand(i).getOpcode() != ISD::UNDEF)
190 return false;
191 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000192}
193
Evan Chengefec7512008-02-18 23:04:32 +0000194/// isScalarToVector - Return true if the specified node is a
195/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
196/// element is not an undef.
197bool ISD::isScalarToVector(const SDNode *N) {
198 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
199 return true;
200
201 if (N->getOpcode() != ISD::BUILD_VECTOR)
202 return false;
203 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
204 return false;
205 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000206 if (NumElems == 1)
207 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000208 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000210 if (V.getOpcode() != ISD::UNDEF)
211 return false;
212 }
213 return true;
214}
215
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000216/// allOperandsUndef - Return true if the node has at least one operand
217/// and all operands of the specified node are ISD::UNDEF.
218bool ISD::allOperandsUndef(const SDNode *N) {
219 // Return false if the node has no operands.
220 // This is "logically inconsistent" with the definition of "all" but
221 // is probably the desired behavior.
222 if (N->getNumOperands() == 0)
223 return false;
224
225 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
226 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
227 return false;
228
229 return true;
230}
231
Chris Lattnerc3aae252005-01-07 07:46:32 +0000232/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
233/// when given the operation for (X op Y).
234ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
235 // To perform this operation, we just need to swap the L and G bits of the
236 // operation.
237 unsigned OldL = (Operation >> 2) & 1;
238 unsigned OldG = (Operation >> 1) & 1;
239 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
240 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000241 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000242}
243
244/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
245/// 'op' is a valid SetCC operation.
246ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
247 unsigned Operation = Op;
248 if (isInteger)
249 Operation ^= 7; // Flip L, G, E bits, but not U.
250 else
251 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000252
Chris Lattnerc3aae252005-01-07 07:46:32 +0000253 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000254 Operation &= ~8; // Don't let N and U bits get set.
255
Chris Lattnerc3aae252005-01-07 07:46:32 +0000256 return ISD::CondCode(Operation);
257}
258
259
260/// isSignedOp - For an integer comparison, return 1 if the comparison is a
261/// signed operation and 2 if the result is an unsigned comparison. Return zero
262/// if the operation does not depend on the sign of the input (setne and seteq).
263static int isSignedOp(ISD::CondCode Opcode) {
264 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000265 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000266 case ISD::SETEQ:
267 case ISD::SETNE: return 0;
268 case ISD::SETLT:
269 case ISD::SETLE:
270 case ISD::SETGT:
271 case ISD::SETGE: return 1;
272 case ISD::SETULT:
273 case ISD::SETULE:
274 case ISD::SETUGT:
275 case ISD::SETUGE: return 2;
276 }
277}
278
279/// getSetCCOrOperation - Return the result of a logical OR between different
280/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
281/// returns SETCC_INVALID if it is not possible to represent the resultant
282/// comparison.
283ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
284 bool isInteger) {
285 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
286 // Cannot fold a signed integer setcc with an unsigned integer setcc.
287 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000288
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000290
Chris Lattnerc3aae252005-01-07 07:46:32 +0000291 // If the N and U bits get set then the resultant comparison DOES suddenly
292 // care about orderedness, and is true when ordered.
293 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000294 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000295
Chris Lattnere41102b2006-05-12 17:03:46 +0000296 // Canonicalize illegal integer setcc's.
297 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
298 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000299
Chris Lattnerc3aae252005-01-07 07:46:32 +0000300 return ISD::CondCode(Op);
301}
302
303/// getSetCCAndOperation - Return the result of a logical AND between different
304/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
305/// function returns zero if it is not possible to represent the resultant
306/// comparison.
307ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
308 bool isInteger) {
309 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
310 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000311 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000312
313 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000314 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattnera83385f2006-04-27 05:01:07 +0000316 // Canonicalize illegal integer setcc's.
317 if (isInteger) {
318 switch (Result) {
319 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000320 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000321 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000322 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
323 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
324 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000325 }
326 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000327
Chris Lattnera83385f2006-04-27 05:01:07 +0000328 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000329}
330
Jim Laskey58b968b2005-08-17 20:08:02 +0000331//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000332// SDNode Profile Support
333//===----------------------------------------------------------------------===//
334
Jim Laskeydef69b92006-10-27 23:52:51 +0000335/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
336///
Jim Laskey583bd472006-10-27 23:46:08 +0000337static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
338 ID.AddInteger(OpC);
339}
340
341/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
342/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000343static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000344 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000345}
346
Jim Laskeydef69b92006-10-27 23:52:51 +0000347/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
348///
Jim Laskey583bd472006-10-27 23:46:08 +0000349static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000350 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000351 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000352 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000353 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000354 }
Jim Laskey583bd472006-10-27 23:46:08 +0000355}
356
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000357/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
358///
359static void AddNodeIDOperands(FoldingSetNodeID &ID,
360 const SDUse *Ops, unsigned NumOps) {
361 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000362 ID.AddPointer(Ops->getNode());
363 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000364 }
365}
366
Jim Laskey583bd472006-10-27 23:46:08 +0000367static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000368 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000369 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000370 AddNodeIDOpcode(ID, OpC);
371 AddNodeIDValueTypes(ID, VTList);
372 AddNodeIDOperands(ID, OpList, N);
373}
374
Duncan Sands0dc40452008-10-27 15:30:53 +0000375/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
376/// the NodeID data.
377static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000378 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000379 case ISD::TargetExternalSymbol:
380 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000381 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000382 default: break; // Normal nodes don't need extra info.
383 case ISD::TargetConstant:
384 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000385 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000386 break;
387 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000388 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000389 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000390 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000391 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000392 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000393 case ISD::GlobalAddress:
394 case ISD::TargetGlobalTLSAddress:
395 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000396 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000397 ID.AddPointer(GA->getGlobal());
398 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000399 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000400 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000401 break;
402 }
403 case ISD::BasicBlock:
404 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
405 break;
406 case ISD::Register:
407 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
408 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000409 case ISD::RegisterMask:
410 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
411 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000412 case ISD::SRCVALUE:
413 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
414 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000415 case ISD::FrameIndex:
416 case ISD::TargetFrameIndex:
417 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
418 break;
419 case ISD::JumpTable:
420 case ISD::TargetJumpTable:
421 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000422 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000423 break;
424 case ISD::ConstantPool:
425 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000426 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000427 ID.AddInteger(CP->getAlignment());
428 ID.AddInteger(CP->getOffset());
429 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000430 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000431 else
432 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000433 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 break;
435 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000436 case ISD::TargetIndex: {
437 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
438 ID.AddInteger(TI->getIndex());
439 ID.AddInteger(TI->getOffset());
440 ID.AddInteger(TI->getTargetFlags());
441 break;
442 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000444 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000445 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000446 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000447 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000448 break;
449 }
450 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000451 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000452 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000453 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000454 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000455 break;
456 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000457 case ISD::ATOMIC_CMP_SWAP:
458 case ISD::ATOMIC_SWAP:
459 case ISD::ATOMIC_LOAD_ADD:
460 case ISD::ATOMIC_LOAD_SUB:
461 case ISD::ATOMIC_LOAD_AND:
462 case ISD::ATOMIC_LOAD_OR:
463 case ISD::ATOMIC_LOAD_XOR:
464 case ISD::ATOMIC_LOAD_NAND:
465 case ISD::ATOMIC_LOAD_MIN:
466 case ISD::ATOMIC_LOAD_MAX:
467 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000468 case ISD::ATOMIC_LOAD_UMAX:
469 case ISD::ATOMIC_LOAD:
470 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000471 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000472 ID.AddInteger(AT->getMemoryVT().getRawBits());
473 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000474 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
475 break;
476 }
477 case ISD::PREFETCH: {
478 const MemSDNode *PF = cast<MemSDNode>(N);
479 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000480 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000481 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000482 case ISD::VECTOR_SHUFFLE: {
483 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000484 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000485 i != e; ++i)
486 ID.AddInteger(SVN->getMaskElt(i));
487 break;
488 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000489 case ISD::TargetBlockAddress:
490 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000491 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
492 ID.AddPointer(BA->getBlockAddress());
493 ID.AddInteger(BA->getOffset());
494 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000495 break;
496 }
Mon P Wang28873102008-06-25 08:15:39 +0000497 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000498
499 // Target specific memory nodes could also have address spaces to check.
500 if (N->isTargetMemoryOpcode())
501 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000502}
503
Duncan Sands0dc40452008-10-27 15:30:53 +0000504/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
505/// data.
506static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
507 AddNodeIDOpcode(ID, N->getOpcode());
508 // Add the return value info.
509 AddNodeIDValueTypes(ID, N->getVTList());
510 // Add the operand info.
511 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
512
513 // Handle SDNode leafs with special info.
514 AddNodeIDCustom(ID, N);
515}
516
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000517/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000518/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000519/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000520///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000521static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000522encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000523 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000524 assert((ConvType & 3) == ConvType &&
525 "ConvType may not require more than 2 bits!");
526 assert((AM & 7) == AM &&
527 "AM may not require more than 3 bits!");
528 return ConvType |
529 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000530 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000531 (isNonTemporal << 6) |
532 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000533}
534
Jim Laskey583bd472006-10-27 23:46:08 +0000535//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000536// SelectionDAG Class
537//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000538
Duncan Sands0dc40452008-10-27 15:30:53 +0000539/// doNotCSE - Return true if CSE should not be performed for this node.
540static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000541 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000542 return true; // Never CSE anything that produces a flag.
543
544 switch (N->getOpcode()) {
545 default: break;
546 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000547 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000548 return true; // Never CSE these nodes.
549 }
550
551 // Check that remaining values produced are not flags.
552 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000553 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000554 return true; // Never CSE anything that produces a flag.
555
556 return false;
557}
558
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000559/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000560/// SelectionDAG.
561void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000562 // Create a dummy node (which is not added to allnodes), that adds a reference
563 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000564 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000565
Chris Lattner190a4182006-08-04 17:45:20 +0000566 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000567
Chris Lattner190a4182006-08-04 17:45:20 +0000568 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000569 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000570 if (I->use_empty())
571 DeadNodes.push_back(I);
572
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000573 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000574
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000575 // If the root changed (e.g. it was a dead load, update the root).
576 setRoot(Dummy.getValue());
577}
578
579/// RemoveDeadNodes - This method deletes the unreachable nodes in the
580/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000581void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000582
Chris Lattner190a4182006-08-04 17:45:20 +0000583 // Process the worklist, deleting the nodes and adding their uses to the
584 // worklist.
585 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000586 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000587
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000588 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
589 DUL->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000590
Chris Lattner190a4182006-08-04 17:45:20 +0000591 // Take the node out of the appropriate CSE map.
592 RemoveNodeFromCSEMaps(N);
593
594 // Next, brutally remove the operand list. This is safe to do, as there are
595 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000596 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
597 SDUse &Use = *I++;
598 SDNode *Operand = Use.getNode();
599 Use.set(SDValue());
600
Chris Lattner190a4182006-08-04 17:45:20 +0000601 // Now that we removed this operand, see if there are no uses of it left.
602 if (Operand->use_empty())
603 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000604 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000605
Dan Gohmanc5336122009-01-19 22:39:36 +0000606 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000607 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000608}
609
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000610void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000611 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000612
613 // Create a dummy node that adds a reference to the root node, preventing
614 // it from being deleted. (This matters if the root is an operand of the
615 // dead node.)
616 HandleSDNode Dummy(getRoot());
617
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000618 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000619}
620
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000621void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000622 // First take this out of the appropriate CSE map.
623 RemoveNodeFromCSEMaps(N);
624
Scott Michelfdc40a02009-02-17 22:15:04 +0000625 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000626 // AllNodes list, and delete the node.
627 DeleteNodeNotInCSEMaps(N);
628}
629
630void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000631 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
632 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000633
Dan Gohmanf06c8352008-09-30 18:30:35 +0000634 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000635 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000636
Dan Gohmanc5336122009-01-19 22:39:36 +0000637 DeallocateNode(N);
638}
639
640void SelectionDAG::DeallocateNode(SDNode *N) {
641 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000642 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000643
Dan Gohmanc5336122009-01-19 22:39:36 +0000644 // Set the opcode to DELETED_NODE to help catch bugs when node
645 // memory is reallocated.
646 N->NodeType = ISD::DELETED_NODE;
647
Dan Gohman11467282008-08-26 01:44:34 +0000648 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000649
650 // Remove the ordering of this node.
Bill Wendling187361b2010-01-23 10:26:57 +0000651 Ordering->remove(N);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000652
Evan Chengbfcb3052010-03-25 01:38:16 +0000653 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000654 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000655 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
656 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000657}
658
Chris Lattner149c58c2005-08-16 18:17:10 +0000659/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
660/// correspond to it. This is useful when we're about to delete or repurpose
661/// the node. We don't want future request for structurally identical nodes
662/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000663bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000664 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000665 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000666 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000667 case ISD::CONDCODE:
668 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
669 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000670 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000671 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
672 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000673 case ISD::ExternalSymbol:
674 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000675 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000676 case ISD::TargetExternalSymbol: {
677 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
678 Erased = TargetExternalSymbols.erase(
679 std::pair<std::string,unsigned char>(ESN->getSymbol(),
680 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000681 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000682 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000683 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000684 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000685 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000686 Erased = ExtendedValueTypeNodes.erase(VT);
687 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
689 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000690 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000691 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000692 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000693 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000694 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000695 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
696 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000697 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000698 break;
699 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000700#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000701 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000702 // flag result (which cannot be CSE'd) or is one of the special cases that are
703 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000704 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000705 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000706 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000707 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000708 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000709 }
710#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000711 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000712}
713
Dan Gohman39946102009-01-25 16:29:12 +0000714/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
715/// maps and modified in place. Add it back to the CSE maps, unless an identical
716/// node already exists, in which case transfer all its users to the existing
717/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000718///
Dan Gohman39946102009-01-25 16:29:12 +0000719void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000720SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000721 // For node types that aren't CSE'd, just act as if no identical node
722 // already exists.
723 if (!doNotCSE(N)) {
724 SDNode *Existing = CSEMap.GetOrInsertNode(N);
725 if (Existing != N) {
726 // If there was already an existing matching node, use ReplaceAllUsesWith
727 // to replace the dead one with the existing one. This can cause
728 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000729 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000730
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000731 // N is now dead. Inform the listeners and delete it.
732 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
733 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000734 DeleteNodeNotInCSEMaps(N);
735 return;
736 }
737 }
Evan Cheng71e86852008-07-08 20:06:39 +0000738
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000739 // If the node doesn't already exist, we updated it. Inform listeners.
740 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
741 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000742}
743
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000744/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000745/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000746/// return null, otherwise return a pointer to the slot it would take. If a
747/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000748SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000749 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000750 if (doNotCSE(N))
751 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000752
Dan Gohman475871a2008-07-27 21:46:04 +0000753 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000754 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000755 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000756 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000757 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000758 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000759}
760
761/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000762/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000763/// return null, otherwise return a pointer to the slot it would take. If a
764/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000765SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000766 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000767 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000768 if (doNotCSE(N))
769 return 0;
770
Dan Gohman475871a2008-07-27 21:46:04 +0000771 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000772 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000773 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000774 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000775 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000776 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000777}
778
779
780/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000781/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000782/// return null, otherwise return a pointer to the slot it would take. If a
783/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000784SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000785 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000786 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000787 if (doNotCSE(N))
788 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000789
Jim Laskey583bd472006-10-27 23:46:08 +0000790 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000791 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000792 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000793 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000794 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000795}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000796
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000797#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000798/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
799static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000800 switch (N->getOpcode()) {
801 default:
802 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000803 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000804 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000805 assert(N->getNumValues() == 1 && "Too many results!");
806 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
807 "Wrong return type!");
808 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
809 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
810 "Mismatched operand types!");
811 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
812 "Wrong operand type!");
813 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
814 "Wrong return type size");
815 break;
816 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000817 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000818 assert(N->getNumValues() == 1 && "Too many results!");
819 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000820 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000821 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000822 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000823 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000824 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000825 (EltVT.isInteger() && I->getValueType().isInteger() &&
826 EltVT.bitsLE(I->getValueType()))) &&
827 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000828 assert(I->getValueType() == N->getOperand(0).getValueType() &&
829 "Operands must all have the same type");
830 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000831 break;
832 }
833 }
834}
835
Duncan Sands59d2dad2010-11-20 11:25:00 +0000836/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
837static void VerifySDNode(SDNode *N) {
838 // The SDNode allocators cannot be used to allocate nodes with fields that are
839 // not present in an SDNode!
840 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
841 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
842 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
843 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
844 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
845 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
846 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
847 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
848 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
849 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
850 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
851 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
852 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
853 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
854 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
855 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
856 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
857 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
858 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
859
860 VerifyNodeCommon(N);
861}
862
863/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
864/// invalid.
865static void VerifyMachineNode(SDNode *N) {
866 // The MachineNode allocators cannot be used to allocate nodes with fields
867 // that are not present in a MachineNode!
868 // Currently there are no such nodes.
869
870 VerifyNodeCommon(N);
871}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000872#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000873
Owen Andersone50ed302009-08-10 22:56:29 +0000874/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000875/// given type.
876///
Owen Andersone50ed302009-08-10 22:56:29 +0000877unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000878 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000879 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000880 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000881
Micah Villmow3574eca2012-10-08 16:38:25 +0000882 return TLI.getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000883}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000884
Dale Johannesen92570c42009-02-07 02:15:05 +0000885// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000886SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Dan Gohmanff7a5622010-05-11 17:31:57 +0000887 : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
Devang Patel0508d042011-12-15 18:21:18 +0000888 OptLevel(OL), EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000889 Root(getEntryNode()), Ordering(0), UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000890 AllNodes.push_back(&EntryNode);
Bill Wendling187361b2010-01-23 10:26:57 +0000891 Ordering = new SDNodeOrdering();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000892 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000893}
894
Chris Lattnerde6e7832010-04-05 06:10:13 +0000895void SelectionDAG::init(MachineFunction &mf) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000896 MF = &mf;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000897 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000898}
899
Chris Lattner78ec3112003-08-11 14:57:33 +0000900SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000901 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000902 allnodes_clear();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000903 delete Ordering;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000904 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000905}
906
907void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000908 assert(&*AllNodes.begin() == &EntryNode);
909 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000910 while (!AllNodes.empty())
911 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000912}
913
Dan Gohman7c3234c2008-08-27 23:52:12 +0000914void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000915 allnodes_clear();
916 OperandAllocator.Reset();
917 CSEMap.clear();
918
919 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000920 ExternalSymbols.clear();
921 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000922 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
923 static_cast<CondCodeSDNode*>(0));
924 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
925 static_cast<SDNode*>(0));
926
Dan Gohmane7852d02009-01-26 04:35:06 +0000927 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000928 AllNodes.push_back(&EntryNode);
929 Root = getEntryNode();
Dan Gohman27445f02010-06-18 15:40:58 +0000930 Ordering->clear();
Evan Cheng31441b72010-03-29 20:48:30 +0000931 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000932}
933
Nadav Rotema3c42f32011-09-27 11:16:47 +0000934SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
935 return VT.bitsGT(Op.getValueType()) ?
936 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
937 getNode(ISD::TRUNCATE, DL, VT, Op);
938}
939
Duncan Sands3a66a682009-10-13 21:04:12 +0000940SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
941 return VT.bitsGT(Op.getValueType()) ?
942 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
943 getNode(ISD::TRUNCATE, DL, VT, Op);
944}
945
946SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
947 return VT.bitsGT(Op.getValueType()) ?
948 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
949 getNode(ISD::TRUNCATE, DL, VT, Op);
950}
951
Owen Andersone50ed302009-08-10 22:56:29 +0000952SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000953 assert(!VT.isVector() &&
954 "getZeroExtendInReg should use the vector element type instead of "
955 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000956 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000957 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
958 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000959 VT.getSizeInBits());
960 return getNode(ISD::AND, DL, Op.getValueType(), Op,
961 getConstant(Imm, Op.getValueType()));
962}
963
Bob Wilson4c245462009-01-22 17:39:32 +0000964/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
965///
Owen Andersone50ed302009-08-10 22:56:29 +0000966SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000967 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000968 SDValue NegOne =
969 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000970 return getNode(ISD::XOR, DL, VT, Val, NegOne);
971}
972
Owen Andersone50ed302009-08-10 22:56:29 +0000973SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000974 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000975 assert((EltVT.getSizeInBits() >= 64 ||
976 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
977 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000978 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000979}
980
Owen Andersone50ed302009-08-10 22:56:29 +0000981SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000982 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000983}
984
Owen Andersone50ed302009-08-10 22:56:29 +0000985SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000986 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000987
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000988 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000989 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000990
Duncan Sands28b77e92011-09-06 19:07:46 +0000991 // In some cases the vector type is legal but the element type is illegal and
992 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
993 // inserted value (the type does not need to match the vector element type).
994 // Any extra bits introduced will be truncated away.
995 if (VT.isVector() && TLI.getTypeAction(*getContext(), EltVT) ==
996 TargetLowering::TypePromoteInteger) {
997 EltVT = TLI.getTypeToTransformTo(*getContext(), EltVT);
998 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
999 Elt = ConstantInt::get(*getContext(), NewVal);
1000 }
1001
1002 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1003 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001004 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001005 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001006 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +00001007 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +00001008 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001009 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001010 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001011 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001012 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001013
Dan Gohman89081322007-12-12 22:21:26 +00001014 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001015 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001016 CSEMap.InsertNode(N, IP);
1017 AllNodes.push_back(N);
1018 }
1019
Dan Gohman475871a2008-07-27 21:46:04 +00001020 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001021 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001022 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001023 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001024 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001025 }
1026 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001027}
1028
Dan Gohman475871a2008-07-27 21:46:04 +00001029SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +00001030 return getConstant(Val, TLI.getPointerTy(), isTarget);
1031}
1032
1033
Owen Andersone50ed302009-08-10 22:56:29 +00001034SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001035 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001036}
1037
Owen Andersone50ed302009-08-10 22:56:29 +00001038SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001039 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001040
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001041 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001042
Chris Lattnerd8658612005-02-17 20:17:32 +00001043 // Do the map lookup using the actual bit pattern for the floating point
1044 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1045 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001046 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001047 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001048 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001049 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001050 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001051 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001052 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001054 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001055
Evan Chengc908dcd2007-06-29 21:36:04 +00001056 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001057 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001058 CSEMap.InsertNode(N, IP);
1059 AllNodes.push_back(N);
1060 }
1061
Dan Gohman475871a2008-07-27 21:46:04 +00001062 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001063 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001064 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001065 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +00001066 // FIXME DebugLoc info might be appropriate here
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001067 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001068 }
1069 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070}
1071
Owen Andersone50ed302009-08-10 22:56:29 +00001072SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001073 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001074 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001075 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001076 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001077 return getConstantFP(APFloat(Val), VT, isTarget);
Owen Andersond7484e52012-04-05 18:50:32 +00001078 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001079 bool ignored;
1080 APFloat apf = APFloat(Val);
1081 apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1082 &ignored);
1083 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001084 } else
1085 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001086}
1087
Devang Patel0d881da2010-07-06 22:08:15 +00001088SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001089 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001090 bool isTargetGA,
1091 unsigned char TargetFlags) {
1092 assert((TargetFlags == 0 || isTargetGA) &&
1093 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001094
Dan Gohman6520e202008-10-18 02:06:02 +00001095 // Truncate (with sign-extension) the offset value to the pointer size.
Richard Smith1144af32012-08-24 23:29:28 +00001096 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001097 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001098 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001099
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001100 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1101 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001102 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001103 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001104 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001105 }
1106
Chris Lattner2a4ed822009-06-25 21:21:14 +00001107 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001108 if (GVar && GVar->isThreadLocal())
1109 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1110 else
1111 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001112
Jim Laskey583bd472006-10-27 23:46:08 +00001113 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001114 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001115 ID.AddPointer(GV);
1116 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001117 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001118 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001119 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001120 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001121 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001122
Devang Patel0d881da2010-07-06 22:08:15 +00001123 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001124 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001125 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001127 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128}
1129
Owen Andersone50ed302009-08-10 22:56:29 +00001130SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001131 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001132 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001133 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001134 ID.AddInteger(FI);
1135 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001136 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001137 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001138
Dan Gohman95531882010-03-18 18:49:47 +00001139 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001140 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001141 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001142 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001143}
1144
Owen Andersone50ed302009-08-10 22:56:29 +00001145SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001146 unsigned char TargetFlags) {
1147 assert((TargetFlags == 0 || isTarget) &&
1148 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001149 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001150 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001151 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001152 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001153 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001154 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001155 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001156 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001157
Dan Gohman95531882010-03-18 18:49:47 +00001158 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1159 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001160 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001161 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001162 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001163}
1164
Dan Gohman46510a72010-04-15 01:51:59 +00001165SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001166 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001167 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001168 unsigned char TargetFlags) {
1169 assert((TargetFlags == 0 || isTarget) &&
1170 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001171 if (Alignment == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00001172 Alignment = TLI.getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001173 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001174 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001175 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001176 ID.AddInteger(Alignment);
1177 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001178 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001179 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001180 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001181 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001182 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001183
Dan Gohman95531882010-03-18 18:49:47 +00001184 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1185 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001186 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001187 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001188 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001189}
1190
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191
Owen Andersone50ed302009-08-10 22:56:29 +00001192SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001193 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001194 bool isTarget,
1195 unsigned char TargetFlags) {
1196 assert((TargetFlags == 0 || isTarget) &&
1197 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001198 if (Alignment == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00001199 Alignment = TLI.getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001200 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001201 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001202 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001203 ID.AddInteger(Alignment);
1204 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001205 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001206 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001207 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001208 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001209 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001210
Dan Gohman95531882010-03-18 18:49:47 +00001211 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1212 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001213 CSEMap.InsertNode(N, IP);
1214 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001215 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001216}
1217
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001218SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1219 unsigned char TargetFlags) {
1220 FoldingSetNodeID ID;
1221 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1222 ID.AddInteger(Index);
1223 ID.AddInteger(Offset);
1224 ID.AddInteger(TargetFlags);
1225 void *IP = 0;
1226 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1227 return SDValue(E, 0);
1228
1229 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1230 TargetFlags);
1231 CSEMap.InsertNode(N, IP);
1232 AllNodes.push_back(N);
1233 return SDValue(N, 0);
1234}
1235
Dan Gohman475871a2008-07-27 21:46:04 +00001236SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001237 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001238 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001239 ID.AddPointer(MBB);
1240 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001241 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001242 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001243
Dan Gohman95531882010-03-18 18:49:47 +00001244 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001245 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001247 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001248}
1249
Owen Andersone50ed302009-08-10 22:56:29 +00001250SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1252 ValueTypeNodes.size())
1253 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001254
Duncan Sands83ec4b62008-06-06 12:08:01 +00001255 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001257
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) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001260 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001261 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001262}
1263
Owen Andersone50ed302009-08-10 22:56:29 +00001264SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001265 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001266 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001267 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001268 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001269 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001270}
1271
Owen Andersone50ed302009-08-10 22:56:29 +00001272SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001273 unsigned char TargetFlags) {
1274 SDNode *&N =
1275 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1276 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001277 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001278 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001279 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001280 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001281}
1282
Dan Gohman475871a2008-07-27 21:46:04 +00001283SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001284 if ((unsigned)Cond >= CondCodeNodes.size())
1285 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001286
Chris Lattner079a27a2005-08-09 20:40:02 +00001287 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001288 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001289 CondCodeNodes[Cond] = N;
1290 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001291 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001292
Dan Gohman475871a2008-07-27 21:46:04 +00001293 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001294}
1295
Nate Begeman5a5ca152009-04-29 05:20:52 +00001296// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1297// the shuffle mask M that point at N1 to point at N2, and indices that point
1298// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001299static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1300 std::swap(N1, N2);
1301 int NElts = M.size();
1302 for (int i = 0; i != NElts; ++i) {
1303 if (M[i] >= NElts)
1304 M[i] -= NElts;
1305 else if (M[i] >= 0)
1306 M[i] += NElts;
1307 }
1308}
1309
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001310SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001311 SDValue N2, const int *Mask) {
1312 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001313 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001314 "Vector Shuffle VTs must be a vectors");
1315 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1316 && "Vector Shuffle VTs must have same element type");
1317
1318 // Canonicalize shuffle undef, undef -> undef
1319 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001320 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001321
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001322 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001323 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001324 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001325 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001326 for (unsigned i = 0; i != NElts; ++i) {
1327 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001328 MaskVec.push_back(Mask[i]);
1329 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001330
Nate Begeman9008ca62009-04-27 18:41:29 +00001331 // Canonicalize shuffle v, v -> v, undef
1332 if (N1 == N2) {
1333 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001334 for (unsigned i = 0; i != NElts; ++i)
1335 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001336 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001337
Nate Begeman9008ca62009-04-27 18:41:29 +00001338 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1339 if (N1.getOpcode() == ISD::UNDEF)
1340 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001341
Nate Begeman9008ca62009-04-27 18:41:29 +00001342 // Canonicalize all index into lhs, -> shuffle lhs, undef
1343 // Canonicalize all index into rhs, -> shuffle rhs, undef
1344 bool AllLHS = true, AllRHS = true;
1345 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001346 for (unsigned i = 0; i != NElts; ++i) {
1347 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001348 if (N2Undef)
1349 MaskVec[i] = -1;
1350 else
1351 AllLHS = false;
1352 } else if (MaskVec[i] >= 0) {
1353 AllRHS = false;
1354 }
1355 }
1356 if (AllLHS && AllRHS)
1357 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001358 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001359 N2 = getUNDEF(VT);
1360 if (AllRHS) {
1361 N1 = getUNDEF(VT);
1362 commuteShuffle(N1, N2, MaskVec);
1363 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001364
Nate Begeman9008ca62009-04-27 18:41:29 +00001365 // If Identity shuffle, or all shuffle in to undef, return that node.
1366 bool AllUndef = true;
1367 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001368 for (unsigned i = 0; i != NElts; ++i) {
1369 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001370 if (MaskVec[i] >= 0) AllUndef = false;
1371 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001372 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001373 return N1;
1374 if (AllUndef)
1375 return getUNDEF(VT);
1376
1377 FoldingSetNodeID ID;
1378 SDValue Ops[2] = { N1, N2 };
1379 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001380 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001381 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001382
Nate Begeman9008ca62009-04-27 18:41:29 +00001383 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001384 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001385 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001386
Nate Begeman9008ca62009-04-27 18:41:29 +00001387 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1388 // SDNode doesn't have access to it. This memory will be "leaked" when
1389 // the node is deallocated, but recovered when the NodeAllocator is released.
1390 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1391 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001392
Dan Gohman95531882010-03-18 18:49:47 +00001393 ShuffleVectorSDNode *N =
1394 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001395 CSEMap.InsertNode(N, IP);
1396 AllNodes.push_back(N);
1397 return SDValue(N, 0);
1398}
1399
Owen Andersone50ed302009-08-10 22:56:29 +00001400SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001401 SDValue Val, SDValue DTy,
1402 SDValue STy, SDValue Rnd, SDValue Sat,
1403 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001404 // If the src and dest types are the same and the conversion is between
1405 // integer types of the same sign or two floats, no conversion is necessary.
1406 if (DTy == STy &&
1407 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001408 return Val;
1409
1410 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001411 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1412 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001413 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001414 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001415 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001416
Dan Gohman95531882010-03-18 18:49:47 +00001417 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1418 Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001419 CSEMap.InsertNode(N, IP);
1420 AllNodes.push_back(N);
1421 return SDValue(N, 0);
1422}
1423
Owen Andersone50ed302009-08-10 22:56:29 +00001424SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001425 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001426 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001427 ID.AddInteger(RegNo);
1428 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001429 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001430 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001431
Dan Gohman95531882010-03-18 18:49:47 +00001432 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001433 CSEMap.InsertNode(N, IP);
1434 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001435 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001436}
1437
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001438SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1439 FoldingSetNodeID ID;
1440 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1441 ID.AddPointer(RegMask);
1442 void *IP = 0;
1443 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1444 return SDValue(E, 0);
1445
1446 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1447 CSEMap.InsertNode(N, IP);
1448 AllNodes.push_back(N);
1449 return SDValue(N, 0);
1450}
1451
Chris Lattner7561d482010-03-14 02:33:54 +00001452SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001453 FoldingSetNodeID ID;
1454 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001455 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1456 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001457 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001458 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001459 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001460
Dan Gohman95531882010-03-18 18:49:47 +00001461 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001462 CSEMap.InsertNode(N, IP);
1463 AllNodes.push_back(N);
1464 return SDValue(N, 0);
1465}
1466
Chris Lattner7561d482010-03-14 02:33:54 +00001467
Dan Gohman46510a72010-04-15 01:51:59 +00001468SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001469 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001470 bool isTarget,
1471 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001472 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1473
1474 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001475 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001476 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001477 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001478 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001479 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001480 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001481 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001482
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001483 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1484 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001485 CSEMap.InsertNode(N, IP);
1486 AllNodes.push_back(N);
1487 return SDValue(N, 0);
1488}
1489
Dan Gohman475871a2008-07-27 21:46:04 +00001490SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001491 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001492 "SrcValue is not a pointer?");
1493
Jim Laskey583bd472006-10-27 23:46:08 +00001494 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001495 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001496 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001497
Chris Lattner61b09412006-08-11 21:01:22 +00001498 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001499 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001500 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001501
Dan Gohman95531882010-03-18 18:49:47 +00001502 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001503 CSEMap.InsertNode(N, IP);
1504 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001505 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001506}
1507
Chris Lattnerdecc2672010-04-07 05:20:54 +00001508/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1509SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1510 FoldingSetNodeID ID;
1511 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1512 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001513
Chris Lattnerdecc2672010-04-07 05:20:54 +00001514 void *IP = 0;
1515 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1516 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001517
Chris Lattnerdecc2672010-04-07 05:20:54 +00001518 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1519 CSEMap.InsertNode(N, IP);
1520 AllNodes.push_back(N);
1521 return SDValue(N, 0);
1522}
1523
1524
Duncan Sands92abc622009-01-31 15:50:11 +00001525/// getShiftAmountOperand - Return the specified value casted to
1526/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001527SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001528 EVT OpTy = Op.getValueType();
Owen Anderson6154f6c2011-03-07 18:29:47 +00001529 MVT ShTy = TLI.getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001530 if (OpTy == ShTy || OpTy.isVector()) return Op;
1531
1532 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001533 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001534}
1535
Chris Lattner37ce9df2007-10-15 17:47:20 +00001536/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1537/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001538SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001539 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001540 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001541 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001542 unsigned StackAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00001543 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
David Greene3f2bf852009-11-12 20:49:22 +00001545 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001546 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1547}
1548
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001549/// CreateStackTemporary - Create a stack temporary suitable for holding
1550/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001551SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001552 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1553 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001554 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1555 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00001556 const DataLayout *TD = TLI.getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001557 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1558 TD->getPrefTypeAlignment(Ty2));
1559
1560 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001561 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001562 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1563}
1564
Owen Andersone50ed302009-08-10 22:56:29 +00001565SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001566 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 // These setcc operations always fold.
1568 switch (Cond) {
1569 default: break;
1570 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001571 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001573 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001574
Chris Lattnera83385f2006-04-27 05:01:07 +00001575 case ISD::SETOEQ:
1576 case ISD::SETOGT:
1577 case ISD::SETOGE:
1578 case ISD::SETOLT:
1579 case ISD::SETOLE:
1580 case ISD::SETONE:
1581 case ISD::SETO:
1582 case ISD::SETUO:
1583 case ISD::SETUEQ:
1584 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001586 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001588
Gabor Greifba36cb52008-08-28 21:40:38 +00001589 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001590 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001591 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001592 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
Chris Lattnerc3aae252005-01-07 07:46:32 +00001594 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001595 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001596 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1597 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001598 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1599 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1600 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1601 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1602 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1603 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1604 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1605 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001606 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001607 }
Chris Lattner67255a12005-04-07 18:14:58 +00001608 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001609 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1610 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001611 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001612 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001613 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001614 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001615 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001616 // fall through
1617 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001618 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001619 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001620 // fall through
1621 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001622 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001623 case ISD::SETLT: 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::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001627 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001628 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001629 // fall through
1630 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001631 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001632 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001633 // fall through
1634 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001635 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001636 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001637 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001638 // fall through
1639 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001640 R==APFloat::cmpEqual, VT);
1641 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1642 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1643 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1644 R==APFloat::cmpEqual, VT);
1645 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1646 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1647 R==APFloat::cmpLessThan, VT);
1648 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1649 R==APFloat::cmpUnordered, VT);
1650 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1651 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001652 }
1653 } else {
1654 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001655 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001656 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001657 }
1658
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001659 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001660 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001661}
1662
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001663/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1664/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001665bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001666 // This predicate is not safe for vector operations.
1667 if (Op.getValueType().isVector())
1668 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001669
Dan Gohman87862e72009-12-11 21:31:27 +00001670 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001671 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1672}
1673
Dan Gohmanea859be2007-06-22 14:59:07 +00001674/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1675/// this predicate to simplify operations downstream. Mask is known to be zero
1676/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001677bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001678 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001679 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001680 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001681 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001682 return (KnownZero & Mask) == Mask;
1683}
1684
1685/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1686/// known to be either zero or one and return them in the KnownZero/KnownOne
1687/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1688/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001689void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1690 APInt &KnownOne, unsigned Depth) const {
1691 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001692
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001693 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001694 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001695 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001696
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001697 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001698
1699 switch (Op.getOpcode()) {
1700 case ISD::Constant:
1701 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001702 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1703 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001704 return;
1705 case ISD::AND:
1706 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001707 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1708 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001709 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1710 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001711
1712 // Output known-1 bits are only known if set in both the LHS & RHS.
1713 KnownOne &= KnownOne2;
1714 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1715 KnownZero |= KnownZero2;
1716 return;
1717 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001718 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1719 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001720 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1721 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1722
Dan Gohmanea859be2007-06-22 14:59:07 +00001723 // Output known-0 bits are only known if clear in both the LHS & RHS.
1724 KnownZero &= KnownZero2;
1725 // Output known-1 are known to be set if set in either the LHS | RHS.
1726 KnownOne |= KnownOne2;
1727 return;
1728 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001729 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1730 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001731 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1732 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1733
Dan Gohmanea859be2007-06-22 14:59:07 +00001734 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001735 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001736 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1737 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1738 KnownZero = KnownZeroOut;
1739 return;
1740 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001741 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001742 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1743 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001744 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1745 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1746
1747 // If low bits are zero in either operand, output low known-0 bits.
1748 // Also compute a conserative estimate for high known-0 bits.
1749 // More trickiness is possible, but this is sufficient for the
1750 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001751 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001752 unsigned TrailZ = KnownZero.countTrailingOnes() +
1753 KnownZero2.countTrailingOnes();
1754 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001755 KnownZero2.countLeadingOnes(),
1756 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001757
1758 TrailZ = std::min(TrailZ, BitWidth);
1759 LeadZ = std::min(LeadZ, BitWidth);
1760 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1761 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001762 return;
1763 }
1764 case ISD::UDIV: {
1765 // For the purposes of computing leading zeros we can conservatively
1766 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001767 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001768 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001769 unsigned LeadZ = KnownZero2.countLeadingOnes();
1770
Jay Foad7a874dd2010-12-01 08:53:58 +00001771 KnownOne2.clearAllBits();
1772 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001773 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001774 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1775 if (RHSUnknownLeadingOnes != BitWidth)
1776 LeadZ = std::min(BitWidth,
1777 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001778
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001779 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001780 return;
1781 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001782 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001783 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1784 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001785 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1786 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1787
Dan Gohmanea859be2007-06-22 14:59:07 +00001788 // Only known if known in both the LHS and RHS.
1789 KnownOne &= KnownOne2;
1790 KnownZero &= KnownZero2;
1791 return;
1792 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001793 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1794 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001795 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1796 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1797
Dan Gohmanea859be2007-06-22 14:59:07 +00001798 // Only known if known in both the LHS and RHS.
1799 KnownOne &= KnownOne2;
1800 KnownZero &= KnownZero2;
1801 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001802 case ISD::SADDO:
1803 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001804 case ISD::SSUBO:
1805 case ISD::USUBO:
1806 case ISD::SMULO:
1807 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001808 if (Op.getResNo() != 1)
1809 return;
Duncan Sands03228082008-11-23 15:47:28 +00001810 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 case ISD::SETCC:
1812 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001813 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1814 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001815 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001816 return;
1817 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001818 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001820 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001821
1822 // If the shift count is an invalid immediate, don't do anything.
1823 if (ShAmt >= BitWidth)
1824 return;
1825
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001826 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001827 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001828 KnownZero <<= ShAmt;
1829 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001830 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001831 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 }
1833 return;
1834 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001835 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001836 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001837 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001838
Dan Gohmand4cf9922008-02-26 18:50:50 +00001839 // If the shift count is an invalid immediate, don't do anything.
1840 if (ShAmt >= BitWidth)
1841 return;
1842
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001843 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001844 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001845 KnownZero = KnownZero.lshr(ShAmt);
1846 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001847
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001848 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001849 KnownZero |= HighBits; // High bits known zero.
1850 }
1851 return;
1852 case ISD::SRA:
1853 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001854 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001855
Dan Gohmand4cf9922008-02-26 18:50:50 +00001856 // If the shift count is an invalid immediate, don't do anything.
1857 if (ShAmt >= BitWidth)
1858 return;
1859
Dan Gohmanea859be2007-06-22 14:59:07 +00001860 // If any of the demanded bits are produced by the sign extension, we also
1861 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001862 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001863
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001864 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001865 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001866 KnownZero = KnownZero.lshr(ShAmt);
1867 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001868
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001870 APInt SignBit = APInt::getSignBit(BitWidth);
1871 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Dan Gohmanca93a432008-02-20 16:30:17 +00001873 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001874 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001875 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001876 KnownOne |= HighBits; // New bits are known one.
1877 }
1878 }
1879 return;
1880 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001881 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001882 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001883
1884 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001885 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001886 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001887
Dan Gohman977a76f2008-02-13 22:28:48 +00001888 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001889 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001890
Dan Gohmanea859be2007-06-22 14:59:07 +00001891 // If the sign extended bits are demanded, we know that the sign
1892 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001893 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001894 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001895 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001896
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001897 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1898 KnownOne &= InputDemandedBits;
1899 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001900 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1901
Dan Gohmanea859be2007-06-22 14:59:07 +00001902 // If the sign bit of the input is known set or clear, then we know the
1903 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001904 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001905 KnownZero |= NewBits;
1906 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001907 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001908 KnownOne |= NewBits;
1909 KnownZero &= ~NewBits;
1910 } else { // Input sign bit unknown
1911 KnownZero &= ~NewBits;
1912 KnownOne &= ~NewBits;
1913 }
1914 return;
1915 }
1916 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001917 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001918 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001919 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001920 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001921 unsigned LowBits = Log2_32(BitWidth)+1;
1922 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001923 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001924 return;
1925 }
1926 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001927 LoadSDNode *LD = cast<LoadSDNode>(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00001928 if (ISD::isZEXTLoad(Op.getNode())) {
Owen Andersone50ed302009-08-10 22:56:29 +00001929 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001930 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001931 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001932 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001933 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001934 }
1935 return;
1936 }
1937 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001938 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001939 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001940 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001941 KnownZero = KnownZero.trunc(InBits);
1942 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001943 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001944 KnownZero = KnownZero.zext(BitWidth);
1945 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001946 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 return;
1948 }
1949 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001950 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001951 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001952 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001953 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001954
Jay Foad40f8f622010-12-07 08:25:19 +00001955 KnownZero = KnownZero.trunc(InBits);
1956 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001957 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001958
1959 // Note if the sign bit is known to be zero or one.
1960 bool SignBitKnownZero = KnownZero.isNegative();
1961 bool SignBitKnownOne = KnownOne.isNegative();
1962 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1963 "Sign bit can't be known to be both zero and one!");
1964
Jay Foad40f8f622010-12-07 08:25:19 +00001965 KnownZero = KnownZero.zext(BitWidth);
1966 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001967
Dan Gohman977a76f2008-02-13 22:28:48 +00001968 // If the sign bit is known zero or one, the top bits match.
1969 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001970 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001971 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001972 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001973 return;
1974 }
1975 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00001976 EVT InVT = Op.getOperand(0).getValueType();
1977 unsigned InBits = InVT.getScalarType().getSizeInBits();
1978 KnownZero = KnownZero.trunc(InBits);
1979 KnownOne = KnownOne.trunc(InBits);
1980 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1981 KnownZero = KnownZero.zext(BitWidth);
1982 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001983 return;
1984 }
1985 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001986 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001987 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001988 KnownZero = KnownZero.zext(InBits);
1989 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001990 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001991 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001992 KnownZero = KnownZero.trunc(BitWidth);
1993 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001994 break;
1995 }
1996 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001997 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001998 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001999 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2000 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002001 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002002 return;
2003 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002004 case ISD::FGETSIGN:
2005 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002006 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002007 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002008
Dan Gohman23e8b712008-04-28 17:02:21 +00002009 case ISD::SUB: {
2010 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2011 // We know that the top bits of C-X are clear if X contains less bits
2012 // than C (i.e. no wrap-around can happen). For example, 20-X is
2013 // positive if we can prove that X is >= 0 and < 16.
2014 if (CLHS->getAPIntValue().isNonNegative()) {
2015 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2016 // NLZ can't be BitWidth with no sign bit
2017 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002018 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002019
2020 // If all of the MaskV bits are known to be zero, then we know the
2021 // output top bits are zero, because we now know that the output is
2022 // from [0-C].
2023 if ((KnownZero2 & MaskV) == MaskV) {
2024 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2025 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002026 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002027 }
2028 }
2029 }
2030 }
2031 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002032 case ISD::ADD:
2033 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002034 // Output known-0 bits are known if clear or set in both the low clear bits
2035 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2036 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002037 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002038 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002039 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2040
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002041 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002042 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002043 KnownZeroOut = std::min(KnownZeroOut,
2044 KnownZero2.countTrailingOnes());
2045
Chris Lattnerda605882010-12-19 20:38:28 +00002046 if (Op.getOpcode() == ISD::ADD) {
2047 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2048 return;
2049 }
2050
2051 // With ADDE, a carry bit may be added in, so we can only use this
2052 // information if we know (at least) that the low two bits are clear. We
2053 // then return to the caller that the low bit is unknown but that other bits
2054 // are known zero.
2055 if (KnownZeroOut >= 2) // ADDE
2056 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002057 return;
2058 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002059 case ISD::SREM:
2060 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002061 const APInt &RA = Rem->getAPIntValue().abs();
2062 if (RA.isPowerOf2()) {
2063 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002064 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002065 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002066
Duncan Sands5c2873a2010-01-29 09:45:26 +00002067 // The low bits of the first operand are unchanged by the srem.
2068 KnownZero = KnownZero2 & LowBits;
2069 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002070
Duncan Sands5c2873a2010-01-29 09:45:26 +00002071 // If the first operand is non-negative or has all low bits zero, then
2072 // the upper bits are all zero.
2073 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2074 KnownZero |= ~LowBits;
2075
2076 // If the first operand is negative and not all low bits are zero, then
2077 // the upper bits are all one.
2078 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2079 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002080 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002081 }
2082 }
2083 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002084 case ISD::UREM: {
2085 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002086 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002087 if (RA.isPowerOf2()) {
2088 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002089 KnownZero |= ~LowBits;
2090 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002091 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2092 break;
2093 }
2094 }
2095
2096 // Since the result is less than or equal to either operand, any leading
2097 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002098 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2099 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002100
2101 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2102 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002103 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002104 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002105 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002106 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002107 case ISD::FrameIndex:
2108 case ISD::TargetFrameIndex:
2109 if (unsigned Align = InferPtrAlignment(Op)) {
2110 // The low bits are known zero if the pointer is aligned.
2111 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2112 return;
2113 }
2114 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002115
Dan Gohmanea859be2007-06-22 14:59:07 +00002116 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002117 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2118 break;
2119 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002120 case ISD::INTRINSIC_WO_CHAIN:
2121 case ISD::INTRINSIC_W_CHAIN:
2122 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002123 // Allow the target to implement this method for its nodes.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002124 TLI.computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002125 return;
2126 }
2127}
2128
2129/// ComputeNumSignBits - Return the number of times the sign bit of the
2130/// register is replicated into the other bits. We know that at least 1 bit
2131/// is always equal to the sign bit (itself), but other cases can give us
2132/// information. For example, immediately after an "SRA X, 2", we know that
2133/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002134unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002135 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002136 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002137 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002138 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002139 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002140
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 if (Depth == 6)
2142 return 1; // Limit search depth.
2143
2144 switch (Op.getOpcode()) {
2145 default: break;
2146 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002147 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002148 return VTBits-Tmp+1;
2149 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002150 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002152
Dan Gohmanea859be2007-06-22 14:59:07 +00002153 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002154 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002155 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002156 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002157
Dan Gohmanea859be2007-06-22 14:59:07 +00002158 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002159 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002160 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002161
Dan Gohmanea859be2007-06-22 14:59:07 +00002162 case ISD::SIGN_EXTEND_INREG:
2163 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002164 Tmp =
2165 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002166 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002167
Dan Gohmanea859be2007-06-22 14:59:07 +00002168 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2169 return std::max(Tmp, Tmp2);
2170
2171 case ISD::SRA:
2172 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2173 // SRA X, C -> adds C sign bits.
2174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002175 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002176 if (Tmp > VTBits) Tmp = VTBits;
2177 }
2178 return Tmp;
2179 case ISD::SHL:
2180 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2181 // shl destroys sign bits.
2182 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002183 if (C->getZExtValue() >= VTBits || // Bad shift.
2184 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2185 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002186 }
2187 break;
2188 case ISD::AND:
2189 case ISD::OR:
2190 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002191 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002192 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002193 if (Tmp != 1) {
2194 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2195 FirstAnswer = std::min(Tmp, Tmp2);
2196 // We computed what we know about the sign bits as our first
2197 // answer. Now proceed to the generic code that uses
2198 // ComputeMaskedBits, and pick whichever answer is better.
2199 }
2200 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002201
2202 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002203 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002204 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002205 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002206 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002207
2208 case ISD::SADDO:
2209 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002210 case ISD::SSUBO:
2211 case ISD::USUBO:
2212 case ISD::SMULO:
2213 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002214 if (Op.getResNo() != 1)
2215 break;
Duncan Sands03228082008-11-23 15:47:28 +00002216 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002217 case ISD::SETCC:
2218 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002219 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002220 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002221 return VTBits;
2222 break;
2223 case ISD::ROTL:
2224 case ISD::ROTR:
2225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002226 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002227
Dan Gohmanea859be2007-06-22 14:59:07 +00002228 // Handle rotate right by N like a rotate left by 32-N.
2229 if (Op.getOpcode() == ISD::ROTR)
2230 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2231
2232 // If we aren't rotating out all of the known-in sign bits, return the
2233 // number that are left. This handles rotl(sext(x), 1) for example.
2234 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2235 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2236 }
2237 break;
2238 case ISD::ADD:
2239 // Add can have at most one carry bit. Thus we know that the output
2240 // is, at worst, one more bit than the inputs.
2241 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2242 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002243
Dan Gohmanea859be2007-06-22 14:59:07 +00002244 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002245 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002246 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002247 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002248 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002249
Dan Gohmanea859be2007-06-22 14:59:07 +00002250 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2251 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002252 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002253 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002254
Dan Gohmanea859be2007-06-22 14:59:07 +00002255 // If we are subtracting one from a positive number, there is no carry
2256 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002257 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 return Tmp;
2259 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002260
Dan Gohmanea859be2007-06-22 14:59:07 +00002261 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2262 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002263 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002264
Dan Gohmanea859be2007-06-22 14:59:07 +00002265 case ISD::SUB:
2266 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2267 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002268
Dan Gohmanea859be2007-06-22 14:59:07 +00002269 // Handle NEG.
2270 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002271 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002272 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002273 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002274 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2275 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002276 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002278
Dan Gohmanea859be2007-06-22 14:59:07 +00002279 // If the input is known to be positive (the sign bit is known clear),
2280 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002281 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002282 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002283
Dan Gohmanea859be2007-06-22 14:59:07 +00002284 // Otherwise, we treat this like a SUB.
2285 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dan Gohmanea859be2007-06-22 14:59:07 +00002287 // Sub can have at most one carry bit. Thus we know that the output
2288 // is, at worst, one more bit than the inputs.
2289 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2290 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002291 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002292 case ISD::TRUNCATE:
2293 // FIXME: it's tricky to do anything useful for this, but it is an important
2294 // case for targets like X86.
2295 break;
2296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002297
Dan Gohmanea859be2007-06-22 14:59:07 +00002298 // Handle LOADX separately here. EXTLOAD case will fallthrough.
Jakub Staszakce00b442012-05-01 23:06:00 +00002299 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002300 unsigned ExtType = LD->getExtensionType();
2301 switch (ExtType) {
2302 default: break;
2303 case ISD::SEXTLOAD: // '17' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002304 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002305 return VTBits-Tmp+1;
2306 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002307 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002308 return VTBits-Tmp;
2309 }
2310 }
2311
2312 // Allow the target to implement this method for its nodes.
2313 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002314 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002315 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2316 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2317 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002318 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002320
Dan Gohmanea859be2007-06-22 14:59:07 +00002321 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2322 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002323 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002324 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002326 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002327 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002328 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002329 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002330 Mask = KnownOne;
2331 } else {
2332 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002333 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002334 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002335
Dan Gohmanea859be2007-06-22 14:59:07 +00002336 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2337 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002338 Mask = ~Mask;
2339 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002340 // Return # leading zeros. We use 'min' here in case Val was zero before
2341 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002342 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002343}
2344
Chris Lattner0a9481f2011-02-13 22:25:43 +00002345/// isBaseWithConstantOffset - Return true if the specified operand is an
2346/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2347/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2348/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002349/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002350bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2351 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2352 !isa<ConstantSDNode>(Op.getOperand(1)))
2353 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002354
2355 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002356 !MaskedValueIsZero(Op.getOperand(0),
2357 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2358 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002359
Chris Lattner0a9481f2011-02-13 22:25:43 +00002360 return true;
2361}
2362
2363
Dan Gohman8d44b282009-09-03 20:34:31 +00002364bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2365 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002366 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002367 return true;
2368
2369 // If the value is a constant, we can obviously see if it is a NaN or not.
2370 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2371 return !C->getValueAPF().isNaN();
2372
2373 // TODO: Recognize more cases here.
2374
2375 return false;
2376}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002377
Dan Gohmane8326932010-02-24 06:52:40 +00002378bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2379 // If the value is a constant, we can obviously see if it is a zero or not.
2380 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2381 return !C->isZero();
2382
2383 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002384 switch (Op.getOpcode()) {
2385 default: break;
2386 case ISD::OR:
2387 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2388 return !C->isNullValue();
2389 break;
2390 }
Dan Gohmane8326932010-02-24 06:52:40 +00002391
2392 return false;
2393}
2394
2395bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2396 // Check the obvious case.
2397 if (A == B) return true;
2398
2399 // For for negative and positive zero.
2400 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2401 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2402 if (CA->isZero() && CB->isZero()) return true;
2403
2404 // Otherwise they may not be equal.
2405 return false;
2406}
2407
Chris Lattnerc3aae252005-01-07 07:46:32 +00002408/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002409///
Owen Andersone50ed302009-08-10 22:56:29 +00002410SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002411 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002412 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002413 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002414 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002415 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002416
Dan Gohman95531882010-03-18 18:49:47 +00002417 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002418 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Chris Lattner4a283e92006-08-11 18:38:11 +00002420 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002421#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002422 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002423#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002424 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002425}
2426
Bill Wendling7ade28c2009-01-28 22:17:52 +00002427SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002428 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002429 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002430 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002431 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002432 switch (Opcode) {
2433 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002434 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002435 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002436 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002437 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002438 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002439 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002440 case ISD::UINT_TO_FP:
2441 case ISD::SINT_TO_FP: {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002442 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002443 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002444 Opcode==ISD::SINT_TO_FP,
2445 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002446 return getConstantFP(apf, VT);
2447 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002448 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002449 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Kristof Beyls789efba2012-09-12 11:25:02 +00002450 return getConstantFP(APFloat(Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002451 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Kristof Beyls789efba2012-09-12 11:25:02 +00002452 return getConstantFP(APFloat(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002453 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002454 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002455 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002456 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002457 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002458 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002459 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002460 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002461 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002462 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002463 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002464 }
2465 }
2466
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002467 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002468 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002469 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002470 switch (Opcode) {
2471 case ISD::FNEG:
2472 V.changeSign();
2473 return getConstantFP(V, VT);
2474 case ISD::FABS:
2475 V.clearSign();
2476 return getConstantFP(V, VT);
2477 case ISD::FCEIL: {
2478 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2479 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002480 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002481 break;
2482 }
2483 case ISD::FTRUNC: {
2484 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2485 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002486 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002487 break;
2488 }
2489 case ISD::FFLOOR: {
2490 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2491 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002492 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002493 break;
2494 }
2495 case ISD::FP_EXTEND: {
2496 bool ignored;
2497 // This can return overflow, underflow, or inexact; we don't care.
2498 // FIXME need to be more flexible about rounding mode.
2499 (void)V.convert(*EVTToAPFloatSemantics(VT),
2500 APFloat::rmNearestTiesToEven, &ignored);
2501 return getConstantFP(V, VT);
2502 }
2503 case ISD::FP_TO_SINT:
2504 case ISD::FP_TO_UINT: {
2505 integerPart x[2];
2506 bool ignored;
2507 assert(integerPartWidth >= 64);
2508 // FIXME need to be more flexible about rounding mode.
2509 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2510 Opcode==ISD::FP_TO_SINT,
2511 APFloat::rmTowardZero, &ignored);
2512 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002513 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002514 APInt api(VT.getSizeInBits(), x);
2515 return getConstant(api, VT);
2516 }
2517 case ISD::BITCAST:
2518 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2519 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2520 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2521 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2522 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002523 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002524 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002525
Gabor Greifba36cb52008-08-28 21:40:38 +00002526 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002527 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002528 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002529 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002530 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002531 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002532 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002533 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002534 assert(VT.isFloatingPoint() &&
2535 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002536 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002537 assert((!VT.isVector() ||
2538 VT.getVectorNumElements() ==
2539 Operand.getValueType().getVectorNumElements()) &&
2540 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002541 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002542 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002543 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002544 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002545 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002546 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002547 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002548 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2549 "Invalid sext node, dst < src!");
2550 assert((!VT.isVector() ||
2551 VT.getVectorNumElements() ==
2552 Operand.getValueType().getVectorNumElements()) &&
2553 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002554 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002555 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002556 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002557 // sext(undef) = 0, because the top bits will all be the same.
2558 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002559 break;
2560 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002561 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002562 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002563 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002564 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2565 "Invalid zext node, dst < src!");
2566 assert((!VT.isVector() ||
2567 VT.getVectorNumElements() ==
2568 Operand.getValueType().getVectorNumElements()) &&
2569 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002570 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002571 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002572 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002573 else if (OpOpcode == ISD::UNDEF)
2574 // zext(undef) = 0, because the top bits will be zero.
2575 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002576 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002577 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002578 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002579 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002580 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002581 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2582 "Invalid anyext node, dst < src!");
2583 assert((!VT.isVector() ||
2584 VT.getVectorNumElements() ==
2585 Operand.getValueType().getVectorNumElements()) &&
2586 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002587
Dan Gohman9e86a732010-06-18 00:08:30 +00002588 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2589 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002590 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002591 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002592 else if (OpOpcode == ISD::UNDEF)
2593 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002594
2595 // (ext (trunx x)) -> x
2596 if (OpOpcode == ISD::TRUNCATE) {
2597 SDValue OpOp = Operand.getNode()->getOperand(0);
2598 if (OpOp.getValueType() == VT)
2599 return OpOp;
2600 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002601 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002604 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002605 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002606 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2607 "Invalid truncate node, src < dst!");
2608 assert((!VT.isVector() ||
2609 VT.getVectorNumElements() ==
2610 Operand.getValueType().getVectorNumElements()) &&
2611 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002612 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002613 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002614 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2615 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002616 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002617 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2618 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002619 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002620 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002621 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002622 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002623 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002624 if (OpOpcode == ISD::UNDEF)
2625 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002626 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002627 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002628 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002629 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002630 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002631 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002632 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2633 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002634 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002635 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002636 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002637 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002638 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002639 (VT.getVectorElementType() == Operand.getValueType() ||
2640 (VT.getVectorElementType().isInteger() &&
2641 Operand.getValueType().isInteger() &&
2642 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002643 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002644 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002645 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002646 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2647 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2648 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2649 Operand.getConstantOperandVal(1) == 0 &&
2650 Operand.getOperand(0).getValueType() == VT)
2651 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002652 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002653 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002654 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002655 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002656 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002657 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002658 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002659 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002660 break;
2661 case ISD::FABS:
2662 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002663 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002664 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002665 }
2666
Chris Lattner43247a12005-08-25 19:12:10 +00002667 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002668 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002669 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002670 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002671 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002672 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002673 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002674 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002675 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002676
Dan Gohman95531882010-03-18 18:49:47 +00002677 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002678 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002679 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002680 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002681 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002682
Chris Lattnerc3aae252005-01-07 07:46:32 +00002683 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002684#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002685 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002686#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002687 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002688}
2689
Bill Wendling688d1c42008-09-24 10:16:24 +00002690SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002691 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002692 ConstantSDNode *Cst1,
2693 ConstantSDNode *Cst2) {
2694 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2695
2696 switch (Opcode) {
2697 case ISD::ADD: return getConstant(C1 + C2, VT);
2698 case ISD::SUB: return getConstant(C1 - C2, VT);
2699 case ISD::MUL: return getConstant(C1 * C2, VT);
2700 case ISD::UDIV:
2701 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2702 break;
2703 case ISD::UREM:
2704 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2705 break;
2706 case ISD::SDIV:
2707 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2708 break;
2709 case ISD::SREM:
2710 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2711 break;
2712 case ISD::AND: return getConstant(C1 & C2, VT);
2713 case ISD::OR: return getConstant(C1 | C2, VT);
2714 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2715 case ISD::SHL: return getConstant(C1 << C2, VT);
2716 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2717 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2718 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2719 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2720 default: break;
2721 }
2722
2723 return SDValue();
2724}
2725
Owen Andersone50ed302009-08-10 22:56:29 +00002726SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002727 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002728 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2729 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002730 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002731 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002732 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002733 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2734 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002735 // Fold trivial token factors.
2736 if (N1.getOpcode() == ISD::EntryToken) return N2;
2737 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002738 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002739 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002740 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002741 // Concat of UNDEFs is UNDEF.
2742 if (N1.getOpcode() == ISD::UNDEF &&
2743 N2.getOpcode() == ISD::UNDEF)
2744 return getUNDEF(VT);
2745
Dan Gohman7f8613e2008-08-14 20:04:46 +00002746 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2747 // one big BUILD_VECTOR.
2748 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2749 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002750 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2751 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002752 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002753 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002754 }
2755 break;
Chris Lattner76365122005-01-16 02:23:22 +00002756 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002757 assert(VT.isInteger() && "This operator does not apply to FP types!");
2758 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002759 N1.getValueType() == VT && "Binary operator types must match!");
2760 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2761 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002762 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002763 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002764 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2765 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002766 break;
Chris Lattner76365122005-01-16 02:23:22 +00002767 case ISD::OR:
2768 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002769 case ISD::ADD:
2770 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002771 assert(VT.isInteger() && "This operator does not apply to FP types!");
2772 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002773 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002774 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2775 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002776 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002777 return N1;
2778 break;
Chris Lattner76365122005-01-16 02:23:22 +00002779 case ISD::UDIV:
2780 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002781 case ISD::MULHU:
2782 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002783 case ISD::MUL:
2784 case ISD::SDIV:
2785 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002786 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002787 assert(N1.getValueType() == N2.getValueType() &&
2788 N1.getValueType() == VT && "Binary operator types must match!");
2789 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002790 case ISD::FADD:
2791 case ISD::FSUB:
2792 case ISD::FMUL:
2793 case ISD::FDIV:
2794 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002795 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002796 if (Opcode == ISD::FADD) {
2797 // 0+x --> x
2798 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2799 if (CFP->getValueAPF().isZero())
2800 return N2;
2801 // x+0 --> x
2802 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2803 if (CFP->getValueAPF().isZero())
2804 return N1;
2805 } else if (Opcode == ISD::FSUB) {
2806 // x-0 --> x
2807 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2808 if (CFP->getValueAPF().isZero())
2809 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00002810 } else if (Opcode == ISD::FMUL) {
2811 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2812 SDValue V = N2;
2813
2814 // If the first operand isn't the constant, try the second
2815 if (!CFP) {
2816 CFP = dyn_cast<ConstantFPSDNode>(N2);
2817 V = N1;
2818 }
2819
2820 if (CFP) {
2821 // 0*x --> 0
2822 if (CFP->isZero())
2823 return SDValue(CFP,0);
2824 // 1*x --> x
2825 if (CFP->isExactlyValue(1.0))
2826 return V;
2827 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00002828 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002829 }
Dale Johannesen78995512010-05-15 18:38:02 +00002830 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002831 assert(N1.getValueType() == N2.getValueType() &&
2832 N1.getValueType() == VT && "Binary operator types must match!");
2833 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002834 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2835 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002836 N1.getValueType().isFloatingPoint() &&
2837 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002838 "Invalid FCOPYSIGN!");
2839 break;
Chris Lattner76365122005-01-16 02:23:22 +00002840 case ISD::SHL:
2841 case ISD::SRA:
2842 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002843 case ISD::ROTL:
2844 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002845 assert(VT == N1.getValueType() &&
2846 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002847 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002848 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002849 // Verify that the shift amount VT is bit enough to hold valid shift
2850 // amounts. This catches things like trying to shift an i1024 value by an
2851 // i8, which is easy to fall into in generic code that uses
2852 // TLI.getShiftAmount().
2853 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002854 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002855 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002856
2857 // Always fold shifts of i1 values so the code generator doesn't need to
2858 // handle them. Since we know the size of the shift has to be less than the
2859 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002860 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002861 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002862 if (N2C && N2C->isNullValue())
2863 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002864 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002865 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002866 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002867 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002869 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002870 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002871 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002872 "type is vector!");
2873 assert((!EVT.isVector() ||
2874 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2875 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002876 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002877 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002878 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002879 break;
2880 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002881 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002882 assert(VT.isFloatingPoint() &&
2883 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002884 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002885 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002886 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002887 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002888 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002889 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002890 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002891 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002892 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002893 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002894 assert(!EVT.isVector() &&
2895 "AssertSExt/AssertZExt type should be the vector element type "
2896 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002897 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002898 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002899 break;
2900 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002901 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002902 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002903 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002904 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002905 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002906 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002907 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002908 "type is vector!");
2909 assert((!EVT.isVector() ||
2910 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2911 "Vector element counts must match in SIGN_EXTEND_INREG");
2912 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002913 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002914
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002915 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002916 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002917 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002918 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002919 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002920 return getConstant(Val, VT);
2921 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002922 break;
2923 }
2924 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002925 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2926 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002927 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002928
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002929 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2930 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002931 if (N2C &&
2932 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002933 N1.getNumOperands() > 0) {
2934 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002935 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002936 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002937 N1.getOperand(N2C->getZExtValue() / Factor),
2938 getConstant(N2C->getZExtValue() % Factor,
2939 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002940 }
2941
2942 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2943 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002944 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2945 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00002946
2947 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00002948 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00002949 // are promoted and implicitly truncated, and the result implicitly
2950 // extended. Make that explicit here.
2951 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00002952
Bob Wilsonb1303d02009-04-13 22:05:19 +00002953 return Elt;
2954 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002955
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002956 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2957 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002958 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002959 // If the indices are the same, return the inserted element else
2960 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002961 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002962 SDValue N1Op2 = N1.getOperand(2);
2963 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2964
2965 if (N1Op2C && N2C) {
2966 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2967 if (VT == N1.getOperand(1).getValueType())
2968 return N1.getOperand(1);
2969 else
2970 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2971 }
2972
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002973 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002974 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002975 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002976 break;
2977 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002978 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002979 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2980 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002981 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002982 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002983
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002984 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2985 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002986 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002987 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002988 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002989
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002990 // EXTRACT_ELEMENT of a constant int is also very common.
2991 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002992 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002993 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002994 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2995 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002996 }
2997 break;
David Greene91585092011-01-26 15:38:49 +00002998 case ISD::EXTRACT_SUBVECTOR: {
2999 SDValue Index = N2;
3000 if (VT.isSimple() && N1.getValueType().isSimple()) {
3001 assert(VT.isVector() && N1.getValueType().isVector() &&
3002 "Extract subvector VTs must be a vectors!");
3003 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
3004 "Extract subvector VTs must have the same element type!");
3005 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3006 "Extract subvector must be from larger vector to smaller vector!");
3007
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003008 if (isa<ConstantSDNode>(Index.getNode())) {
3009 assert((VT.getVectorNumElements() +
3010 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003011 <= N1.getValueType().getVectorNumElements())
3012 && "Extract subvector overflow!");
3013 }
3014
3015 // Trivial extraction.
3016 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
3017 return N1;
3018 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003019 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003020 }
David Greene91585092011-01-26 15:38:49 +00003021 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003022
3023 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003024 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00003025 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
3026 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003027 } else { // Cannonicalize constant to RHS if commutative
3028 if (isCommutativeBinOp(Opcode)) {
3029 std::swap(N1C, N2C);
3030 std::swap(N1, N2);
3031 }
3032 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003033 }
3034
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003035 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003036 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3037 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003038 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003039 if (!N2CFP && isCommutativeBinOp(Opcode)) {
3040 // Cannonicalize constant to RHS if commutative
3041 std::swap(N1CFP, N2CFP);
3042 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003043 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003044 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3045 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003046 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003047 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003048 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003049 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003050 return getConstantFP(V1, VT);
3051 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003052 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003053 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3054 if (s!=APFloat::opInvalidOp)
3055 return getConstantFP(V1, VT);
3056 break;
3057 case ISD::FMUL:
3058 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3059 if (s!=APFloat::opInvalidOp)
3060 return getConstantFP(V1, VT);
3061 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003062 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003063 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3064 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3065 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003066 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003067 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003068 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3069 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3070 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003071 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003072 case ISD::FCOPYSIGN:
3073 V1.copySign(V2);
3074 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003075 default: break;
3076 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003077 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003078
3079 if (Opcode == ISD::FP_ROUND) {
3080 APFloat V = N1CFP->getValueAPF(); // make copy
3081 bool ignored;
3082 // This can return overflow, underflow, or inexact; we don't care.
3083 // FIXME need to be more flexible about rounding mode.
3084 (void)V.convert(*EVTToAPFloatSemantics(VT),
3085 APFloat::rmNearestTiesToEven, &ignored);
3086 return getConstantFP(V, VT);
3087 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003088 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003089
Chris Lattner62b57722006-04-20 05:39:12 +00003090 // Canonicalize an UNDEF to the RHS, even over a constant.
3091 if (N1.getOpcode() == ISD::UNDEF) {
3092 if (isCommutativeBinOp(Opcode)) {
3093 std::swap(N1, N2);
3094 } else {
3095 switch (Opcode) {
3096 case ISD::FP_ROUND_INREG:
3097 case ISD::SIGN_EXTEND_INREG:
3098 case ISD::SUB:
3099 case ISD::FSUB:
3100 case ISD::FDIV:
3101 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003102 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003103 return N1; // fold op(undef, arg2) -> undef
3104 case ISD::UDIV:
3105 case ISD::SDIV:
3106 case ISD::UREM:
3107 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003108 case ISD::SRL:
3109 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003111 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3112 // For vectors, we can't easily build an all zero vector, just return
3113 // the LHS.
3114 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003115 }
3116 }
3117 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003118
3119 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003120 if (N2.getOpcode() == ISD::UNDEF) {
3121 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003122 case ISD::XOR:
3123 if (N1.getOpcode() == ISD::UNDEF)
3124 // Handle undef ^ undef -> 0 special case. This is a common
3125 // idiom (misuse).
3126 return getConstant(0, VT);
3127 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003128 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003129 case ISD::ADDC:
3130 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003131 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003132 case ISD::UDIV:
3133 case ISD::SDIV:
3134 case ISD::UREM:
3135 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003136 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003137 case ISD::FADD:
3138 case ISD::FSUB:
3139 case ISD::FMUL:
3140 case ISD::FDIV:
3141 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003142 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003143 return N2;
3144 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003145 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003146 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003147 case ISD::SRL:
3148 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003150 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3151 // For vectors, we can't easily build an all zero vector, just return
3152 // the LHS.
3153 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003154 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003155 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003156 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003157 // For vectors, we can't easily build an all one vector, just return
3158 // the LHS.
3159 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003160 case ISD::SRA:
3161 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003162 }
3163 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003164
Chris Lattner27e9b412005-05-11 18:57:39 +00003165 // Memoize this node if possible.
3166 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003167 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003168 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003169 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003170 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003171 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003172 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003173 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003174 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003175
Dan Gohman95531882010-03-18 18:49:47 +00003176 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003177 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003178 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003179 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003180 }
3181
Chris Lattnerc3aae252005-01-07 07:46:32 +00003182 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003183#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003184 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003185#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003186 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003187}
3188
Owen Andersone50ed302009-08-10 22:56:29 +00003189SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003190 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003191 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003192 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003193 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003194 case ISD::CONCAT_VECTORS:
3195 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3196 // one big BUILD_VECTOR.
3197 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3198 N2.getOpcode() == ISD::BUILD_VECTOR &&
3199 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003200 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3201 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003202 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3203 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003204 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003205 }
3206 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003207 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003208 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003209 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003210 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003211 break;
3212 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003213 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003214 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003215 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003216 return N2; // select true, X, Y -> X
3217 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003218 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003219
3220 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003221 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003222 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003223 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003224 case ISD::INSERT_SUBVECTOR: {
3225 SDValue Index = N3;
3226 if (VT.isSimple() && N1.getValueType().isSimple()
3227 && N2.getValueType().isSimple()) {
3228 assert(VT.isVector() && N1.getValueType().isVector() &&
3229 N2.getValueType().isVector() &&
3230 "Insert subvector VTs must be a vectors");
3231 assert(VT == N1.getValueType() &&
3232 "Dest and insert subvector source types must match!");
3233 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3234 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003235 if (isa<ConstantSDNode>(Index.getNode())) {
3236 assert((N2.getValueType().getVectorNumElements() +
3237 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003238 <= VT.getVectorNumElements())
3239 && "Insert subvector overflow!");
3240 }
3241
3242 // Trivial insertion.
3243 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3244 return N2;
3245 }
3246 break;
3247 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003248 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003249 // Fold bit_convert nodes from a type to themselves.
3250 if (N1.getValueType() == VT)
3251 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003252 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003253 }
3254
Chris Lattner43247a12005-08-25 19:12:10 +00003255 // Memoize node if it doesn't produce a flag.
3256 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003257 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003258 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003259 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003260 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003261 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003262 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003263 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003264 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003265
Dan Gohman95531882010-03-18 18:49:47 +00003266 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003267 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003268 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003269 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003270 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003271
Chris Lattnerc3aae252005-01-07 07:46:32 +00003272 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003273#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003274 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003275#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003276 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003277}
3278
Owen Andersone50ed302009-08-10 22:56:29 +00003279SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003280 SDValue N1, SDValue N2, SDValue N3,
3281 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003282 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003283 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003284}
3285
Owen Andersone50ed302009-08-10 22:56:29 +00003286SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003287 SDValue N1, SDValue N2, SDValue N3,
3288 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003289 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003290 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003291}
3292
Dan Gohman98ca4f22009-08-05 01:29:28 +00003293/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3294/// the incoming stack arguments to be loaded from the stack.
3295SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3296 SmallVector<SDValue, 8> ArgChains;
3297
3298 // Include the original chain at the beginning of the list. When this is
3299 // used by target LowerCall hooks, this helps legalize find the
3300 // CALLSEQ_BEGIN node.
3301 ArgChains.push_back(Chain);
3302
3303 // Add a chain value for each stack argument.
3304 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3305 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3306 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3307 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3308 if (FI->getIndex() < 0)
3309 ArgChains.push_back(SDValue(L, 1));
3310
3311 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003313 &ArgChains[0], ArgChains.size());
3314}
3315
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003316/// SplatByte - Distribute ByteVal over NumBits bits.
3317static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3318 APInt Val = APInt(NumBits, ByteVal);
3319 unsigned Shift = 8;
3320 for (unsigned i = NumBits; i > 8; i >>= 1) {
3321 Val = (Val << Shift) | Val;
3322 Shift <<= 1;
3323 }
3324 return Val;
3325}
3326
Dan Gohman707e0182008-04-12 04:36:06 +00003327/// getMemsetValue - Vectorized representation of the memset value
3328/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003329static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003330 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003331 assert(Value.getOpcode() != ISD::UNDEF);
3332
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003333 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003335 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003336 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003337 return DAG.getConstant(Val, VT);
3338 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003339 }
Evan Chengf0df0312008-05-15 08:39:06 +00003340
Dale Johannesen0f502f62009-02-03 22:26:09 +00003341 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003342 if (NumBits > 8) {
3343 // Use a multiplication with 0x010101... to extend the input to the
3344 // required length.
3345 APInt Magic = SplatByte(NumBits, 0x01);
3346 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003347 }
3348
3349 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003350}
3351
Dan Gohman707e0182008-04-12 04:36:06 +00003352/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3353/// used when a memcpy is turned into a memset when the source is a constant
3354/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003355static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003356 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003357 // Handle vector with all elements zero.
3358 if (Str.empty()) {
3359 if (VT.isInteger())
3360 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003361 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003362 return DAG.getConstantFP(0.0, VT);
3363 else if (VT.isVector()) {
3364 unsigned NumElts = VT.getVectorNumElements();
3365 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003366 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003367 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3368 EltVT, NumElts)));
3369 } else
3370 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003371 }
3372
Duncan Sands83ec4b62008-06-06 12:08:01 +00003373 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003374 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3375 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3376
Evan Cheng376642e2012-12-10 23:21:26 +00003377 APInt Val(NumBytes*8, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003378 if (TLI.isLittleEndian()) {
3379 for (unsigned i = 0; i != NumBytes; ++i)
3380 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3381 } else {
3382 for (unsigned i = 0; i != NumBytes; ++i)
3383 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003384 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003385
Evan Chenge07f85e2012-12-11 23:26:14 +00003386 // If the "cost" of materializing the integer immediate is 1 or free, then
3387 // it is cost effective to turn the load into the immediate.
3388 if (DAG.getTarget().getScalarTargetTransformInfo()->
3389 getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng376642e2012-12-10 23:21:26 +00003390 return DAG.getConstant(Val, VT);
3391 return SDValue(0, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003392}
3393
Scott Michelfdc40a02009-02-17 22:15:04 +00003394/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003395///
Dan Gohman475871a2008-07-27 21:46:04 +00003396static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003397 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003398 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003399 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003400 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003401}
3402
Evan Chengf0df0312008-05-15 08:39:06 +00003403/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3404///
Chris Lattner18c7f802012-02-05 02:29:43 +00003405static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003406 unsigned SrcDelta = 0;
3407 GlobalAddressSDNode *G = NULL;
3408 if (Src.getOpcode() == ISD::GlobalAddress)
3409 G = cast<GlobalAddressSDNode>(Src);
3410 else if (Src.getOpcode() == ISD::ADD &&
3411 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3412 Src.getOperand(1).getOpcode() == ISD::Constant) {
3413 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003414 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003415 }
3416 if (!G)
3417 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003418
Chris Lattner18c7f802012-02-05 02:29:43 +00003419 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003420}
Dan Gohman707e0182008-04-12 04:36:06 +00003421
Evan Cheng255f20f2010-04-01 06:04:33 +00003422/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3423/// to replace the memset / memcpy. Return true if the number of memory ops
3424/// is below the threshold. It returns the types of the sequence of
3425/// memory ops to perform memset / memcpy by reference.
3426static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003427 unsigned Limit, uint64_t Size,
3428 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003429 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00003430 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003431 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003432 SelectionDAG &DAG,
3433 const TargetLowering &TLI) {
3434 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3435 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003436 // If 'SrcAlign' is zero, that means the memory operation does not need to
3437 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3438 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3439 // is the specified alignment of the memory operation. If it is zero, that
3440 // means it's possible to change the alignment of the destination.
3441 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3442 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003443 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00003444 IsZeroVal, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003445 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003446
Chris Lattneracee6472010-03-07 07:45:08 +00003447 if (VT == MVT::Other) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00003448 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003449 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003450 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003451 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003452 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 case 0: VT = MVT::i64; break;
3454 case 4: VT = MVT::i32; break;
3455 case 2: VT = MVT::i16; break;
3456 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003457 }
3458 }
3459
Owen Anderson766b5ef2009-08-11 21:59:30 +00003460 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003461 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003462 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003463 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003464
Duncan Sands8e4eb092008-06-08 20:54:56 +00003465 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003466 VT = LVT;
3467 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003468
Dan Gohman707e0182008-04-12 04:36:06 +00003469 unsigned NumMemOps = 0;
3470 while (Size != 0) {
Evan Cheng376642e2012-12-10 23:21:26 +00003471 if (++NumMemOps > Limit)
3472 return false;
3473
Duncan Sands83ec4b62008-06-06 12:08:01 +00003474 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003475 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003476 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng376642e2012-12-10 23:21:26 +00003477 EVT NewVT;
3478 unsigned NewVTSize;
Evan Cheng255f20f2010-04-01 06:04:33 +00003479 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003480 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
3481 while (!TLI.isOperationLegalOrCustom(ISD::STORE, NewVT)) {
3482 if (NewVT == MVT::i64 &&
3483 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64)) {
3484 // i64 is usually not legal on 32-bit targets, but f64 may be.
3485 NewVT = MVT::f64;
3486 break;
3487 }
3488 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3489 }
3490 NewVTSize = NewVT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003491 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003492 // This can result in a type that is not legal on the target, e.g.
3493 // 1 or 2 bytes on PPC.
Evan Cheng376642e2012-12-10 23:21:26 +00003494 NewVT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
3495 NewVTSize = VTSize >> 1;
3496 }
3497
3498 // If the new VT cannot cover all of the remaining bits, then consider
3499 // issuing a (or a pair of) unaligned and overlapping load / store.
3500 // FIXME: Only does this for 64-bit or more since we don't have proper
3501 // cost model for unaligned load / store.
3502 bool Fast;
3503 if (AllowOverlap && VTSize >= 8 && NewVTSize < Size &&
3504 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3505 VTSize = Size;
3506 else {
3507 VT = NewVT;
3508 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003509 }
Dan Gohman707e0182008-04-12 04:36:06 +00003510 }
Dan Gohman707e0182008-04-12 04:36:06 +00003511
Dan Gohman707e0182008-04-12 04:36:06 +00003512 MemOps.push_back(VT);
3513 Size -= VTSize;
3514 }
3515
3516 return true;
3517}
3518
Dale Johannesen0f502f62009-02-03 22:26:09 +00003519static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003520 SDValue Chain, SDValue Dst,
3521 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003522 unsigned Align, bool isVol,
3523 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003524 MachinePointerInfo DstPtrInfo,
3525 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003526 // Turn a memcpy of undef to nop.
3527 if (Src.getOpcode() == ISD::UNDEF)
3528 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003529
Dan Gohman21323f32008-05-29 19:42:22 +00003530 // Expand memcpy to a series of load and store ops if the size operand falls
3531 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003532 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3533 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003534 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003535 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003536 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003537 MachineFunction &MF = DAG.getMachineFunction();
3538 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003539 bool OptSize =
3540 MF.getFunction()->getFnAttributes().
3541 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003542 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3543 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3544 DstAlignCanChange = true;
3545 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3546 if (Align > SrcAlign)
3547 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003548 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003549 bool CopyFromStr = isMemSrcFromString(Src, Str);
3550 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003551 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003552
Evan Cheng94107ba2010-04-01 18:19:11 +00003553 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003554 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003555 (isZeroStr ? 0 : SrcAlign),
Evan Cheng376642e2012-12-10 23:21:26 +00003556 true, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003557 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003558
Evan Cheng255f20f2010-04-01 06:04:33 +00003559 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003560 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003561 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003562 if (NewAlign > Align) {
3563 // Give the stack frame object a larger alignment if needed.
3564 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3565 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3566 Align = NewAlign;
3567 }
3568 }
Dan Gohman707e0182008-04-12 04:36:06 +00003569
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003571 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003572 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003573 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003574 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003575 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003576 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003577
Evan Cheng376642e2012-12-10 23:21:26 +00003578 if (VTSize > Size) {
3579 // Issuing an unaligned load / store pair that overlaps with the previous
3580 // pair. Adjust the offset accordingly.
3581 assert(i == NumMemOps-1 && i != 0);
3582 SrcOff -= VTSize - Size;
3583 DstOff -= VTSize - Size;
3584 }
3585
Evan Cheng255f20f2010-04-01 06:04:33 +00003586 if (CopyFromStr &&
3587 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003588 // It's unlikely a store of a vector immediate can be done in a single
3589 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003590 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003591 // FIXME: Handle other cases where store of vector immediate is done in
3592 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003593 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003594 if (Value.getNode())
3595 Store = DAG.getStore(Chain, dl, Value,
3596 getMemBasePlusOffset(Dst, DstOff, DAG),
3597 DstPtrInfo.getWithOffset(DstOff), isVol,
3598 false, Align);
3599 }
3600
3601 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003602 // The type might not be legal for the target. This should only happen
3603 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003604 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3605 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003606 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003607 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003608 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003609 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003610 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003611 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003612 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003613 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003614 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003615 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3616 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003617 }
3618 OutChains.push_back(Store);
3619 SrcOff += VTSize;
3620 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003621 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003622 }
3623
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003625 &OutChains[0], OutChains.size());
3626}
3627
Dale Johannesen0f502f62009-02-03 22:26:09 +00003628static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003629 SDValue Chain, SDValue Dst,
3630 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003631 unsigned Align, bool isVol,
3632 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003633 MachinePointerInfo DstPtrInfo,
3634 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003635 // Turn a memmove of undef to nop.
3636 if (Src.getOpcode() == ISD::UNDEF)
3637 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003638
3639 // Expand memmove to a series of load and store ops if the size operand falls
3640 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003641 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003642 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003643 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003644 MachineFunction &MF = DAG.getMachineFunction();
3645 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003646 bool OptSize = MF.getFunction()->getFnAttributes().
3647 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003648 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3649 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3650 DstAlignCanChange = true;
3651 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3652 if (Align > SrcAlign)
3653 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003654 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003655
Evan Cheng94107ba2010-04-01 18:19:11 +00003656 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003657 (DstAlignCanChange ? 0 : Align),
Evan Cheng376642e2012-12-10 23:21:26 +00003658 SrcAlign, true, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003659 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003660
Evan Cheng255f20f2010-04-01 06:04:33 +00003661 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003662 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003663 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003664 if (NewAlign > Align) {
3665 // Give the stack frame object a larger alignment if needed.
3666 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3667 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3668 Align = NewAlign;
3669 }
3670 }
Dan Gohman21323f32008-05-29 19:42:22 +00003671
Evan Cheng255f20f2010-04-01 06:04:33 +00003672 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003673 SmallVector<SDValue, 8> LoadValues;
3674 SmallVector<SDValue, 8> LoadChains;
3675 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003676 unsigned NumMemOps = MemOps.size();
3677 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003678 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003679 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003680 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003681
Dale Johannesen0f502f62009-02-03 22:26:09 +00003682 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003683 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003684 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003685 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003686 LoadValues.push_back(Value);
3687 LoadChains.push_back(Value.getValue(1));
3688 SrcOff += VTSize;
3689 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003690 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003691 &LoadChains[0], LoadChains.size());
3692 OutChains.clear();
3693 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003694 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003695 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003696 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003697
Dale Johannesen0f502f62009-02-03 22:26:09 +00003698 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003699 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003700 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003701 OutChains.push_back(Store);
3702 DstOff += VTSize;
3703 }
3704
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003706 &OutChains[0], OutChains.size());
3707}
3708
Dale Johannesen0f502f62009-02-03 22:26:09 +00003709static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003710 SDValue Chain, SDValue Dst,
3711 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003712 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003713 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003714 // Turn a memset of undef to nop.
3715 if (Src.getOpcode() == ISD::UNDEF)
3716 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003717
3718 // Expand memset to a series of load/store ops if the size operand
3719 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003720 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003721 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003722 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003723 MachineFunction &MF = DAG.getMachineFunction();
3724 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003725 bool OptSize = MF.getFunction()->getFnAttributes().
3726 hasAttribute(Attributes::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003727 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3728 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3729 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003730 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003731 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003732 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003733 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng376642e2012-12-10 23:21:26 +00003734 IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003735 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003736
Evan Cheng255f20f2010-04-01 06:04:33 +00003737 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003738 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003739 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003740 if (NewAlign > Align) {
3741 // Give the stack frame object a larger alignment if needed.
3742 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3743 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3744 Align = NewAlign;
3745 }
3746 }
3747
Dan Gohman475871a2008-07-27 21:46:04 +00003748 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003749 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003750 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003751
3752 // Find the largest store and generate the bit pattern for it.
3753 EVT LargestVT = MemOps[0];
3754 for (unsigned i = 1; i < NumMemOps; i++)
3755 if (MemOps[i].bitsGT(LargestVT))
3756 LargestVT = MemOps[i];
3757 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3758
Dan Gohman707e0182008-04-12 04:36:06 +00003759 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003760 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00003761 unsigned VTSize = VT.getSizeInBits() / 8;
3762 if (VTSize > Size) {
3763 // Issuing an unaligned load / store pair that overlaps with the previous
3764 // pair. Adjust the offset accordingly.
3765 assert(i == NumMemOps-1 && i != 0);
3766 DstOff -= VTSize - Size;
3767 }
Benjamin Kramer80220362011-01-02 19:57:05 +00003768
3769 // If this store is smaller than the largest store see whether we can get
3770 // the smaller value for free with a truncate.
3771 SDValue Value = MemSetValue;
3772 if (VT.bitsLT(LargestVT)) {
3773 if (!LargestVT.isVector() && !VT.isVector() &&
3774 TLI.isTruncateFree(LargestVT, VT))
3775 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3776 else
3777 Value = getMemsetValue(Src, VT, DAG, dl);
3778 }
3779 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003780 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003781 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003782 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003783 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003784 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003785 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00003786 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003787 }
3788
Owen Anderson825b72b2009-08-11 20:47:22 +00003789 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003790 &OutChains[0], OutChains.size());
3791}
3792
Dale Johannesen0f502f62009-02-03 22:26:09 +00003793SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003794 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003795 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003796 MachinePointerInfo DstPtrInfo,
3797 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003798
3799 // Check to see if we should lower the memcpy to loads and stores first.
3800 // For cases within the target-specified limits, this is the best choice.
3801 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3802 if (ConstantSize) {
3803 // Memcpy with size zero? Just return the original chain.
3804 if (ConstantSize->isNullValue())
3805 return Chain;
3806
Evan Cheng255f20f2010-04-01 06:04:33 +00003807 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3808 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003809 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003810 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003811 return Result;
3812 }
3813
3814 // Then check to see if we should lower the memcpy with target-specific
3815 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003816 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003817 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003818 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003819 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003820 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003821 return Result;
3822
3823 // If we really need inline code and the target declined to provide it,
3824 // use a (potentially long) sequence of loads and stores.
3825 if (AlwaysInline) {
3826 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003827 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003828 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003829 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003830 }
3831
Dan Gohman7b55d362010-04-05 20:24:08 +00003832 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3833 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3834 // respect volatile, so they may do things like read or write memory
3835 // beyond the given memory regions. But fixing this isn't easy, and most
3836 // people don't care.
3837
Dan Gohman707e0182008-04-12 04:36:06 +00003838 // Emit a library call.
3839 TargetLowering::ArgListTy Args;
3840 TargetLowering::ArgListEntry Entry;
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003841 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003842 Entry.Node = Dst; Args.push_back(Entry);
3843 Entry.Node = Src; Args.push_back(Entry);
3844 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003845 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003846 TargetLowering::
3847 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003848 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003849 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3850 /*isTailCall=*/false,
3851 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003852 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003853 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003854 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003855 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3856
Dan Gohman707e0182008-04-12 04:36:06 +00003857 return CallResult.second;
3858}
3859
Dale Johannesen0f502f62009-02-03 22:26:09 +00003860SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003861 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003862 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003863 MachinePointerInfo DstPtrInfo,
3864 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003865
Dan Gohman21323f32008-05-29 19:42:22 +00003866 // Check to see if we should lower the memmove to loads and stores first.
3867 // For cases within the target-specified limits, this is the best choice.
3868 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3869 if (ConstantSize) {
3870 // Memmove with size zero? Just return the original chain.
3871 if (ConstantSize->isNullValue())
3872 return Chain;
3873
Dan Gohman475871a2008-07-27 21:46:04 +00003874 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003875 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003876 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003877 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003878 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003879 return Result;
3880 }
Dan Gohman707e0182008-04-12 04:36:06 +00003881
3882 // Then check to see if we should lower the memmove with target-specific
3883 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003884 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003885 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003886 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003887 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003888 return Result;
3889
Mon P Wang01f0e852010-04-06 08:27:51 +00003890 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3891 // not be safe. See memcpy above for more details.
3892
Dan Gohman707e0182008-04-12 04:36:06 +00003893 // Emit a library call.
3894 TargetLowering::ArgListTy Args;
3895 TargetLowering::ArgListEntry Entry;
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003896 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003897 Entry.Node = Dst; Args.push_back(Entry);
3898 Entry.Node = Src; Args.push_back(Entry);
3899 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003900 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003901 TargetLowering::
3902 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003903 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003904 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3905 /*isTailCall=*/false,
3906 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003907 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003908 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003909 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003910 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3911
Dan Gohman707e0182008-04-12 04:36:06 +00003912 return CallResult.second;
3913}
3914
Dale Johannesen0f502f62009-02-03 22:26:09 +00003915SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003916 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003917 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003918 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003919
3920 // Check to see if we should lower the memset to stores first.
3921 // For cases within the target-specified limits, this is the best choice.
3922 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3923 if (ConstantSize) {
3924 // Memset with size zero? Just return the original chain.
3925 if (ConstantSize->isNullValue())
3926 return Chain;
3927
Mon P Wang20adc9d2010-04-04 03:10:48 +00003928 SDValue Result =
3929 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003930 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003931
Gabor Greifba36cb52008-08-28 21:40:38 +00003932 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003933 return Result;
3934 }
3935
3936 // Then check to see if we should lower the memset with target-specific
3937 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003938 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003939 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003940 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003941 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003942 return Result;
3943
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003944 // Emit a library call.
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003945 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003946 TargetLowering::ArgListTy Args;
3947 TargetLowering::ArgListEntry Entry;
3948 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3949 Args.push_back(Entry);
3950 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 if (Src.getValueType().bitsGT(MVT::i32))
3952 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003953 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003955 Entry.Node = Src;
3956 Entry.Ty = Type::getInt32Ty(*getContext());
3957 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003958 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003959 Entry.Node = Size;
3960 Entry.Ty = IntPtrTy;
3961 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003962 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003963 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003964 TargetLowering::
3965 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003966 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003967 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3968 /*isTailCall=*/false,
3969 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003970 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003971 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003972 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003973 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3974
Dan Gohman707e0182008-04-12 04:36:06 +00003975 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003976}
3977
Owen Andersone50ed302009-08-10 22:56:29 +00003978SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003979 SDValue Chain, SDValue Ptr, SDValue Cmp,
3980 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003981 unsigned Alignment,
3982 AtomicOrdering Ordering,
Michael Ilseman06b96902012-09-10 16:56:31 +00003983 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003984 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3985 Alignment = getEVTAlignment(MemVT);
3986
Evan Chengff89dcb2009-10-18 18:16:27 +00003987 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003988
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00003989 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003990 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00003991 // FIXME: Volatile isn't really correct; we should keep track of atomic
3992 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00003993 unsigned Flags = MachineMemOperand::MOVolatile;
3994 if (Opcode != ISD::ATOMIC_STORE)
3995 Flags |= MachineMemOperand::MOLoad;
3996 if (Opcode != ISD::ATOMIC_LOAD)
3997 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003998
3999 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004000 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004001
Eli Friedman55ba8162011-07-29 03:05:32 +00004002 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4003 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004004}
4005
4006SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4007 SDValue Chain,
4008 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004009 SDValue Swp, MachineMemOperand *MMO,
4010 AtomicOrdering Ordering,
4011 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004012 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4013 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4014
Owen Andersone50ed302009-08-10 22:56:29 +00004015 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004016
Owen Anderson825b72b2009-08-11 20:47:22 +00004017 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004018 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004019 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004020 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
4021 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004022 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004023 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004024 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4025 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004026 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004027 }
Dan Gohman95531882010-03-18 18:49:47 +00004028 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004029 Ptr, Cmp, Swp, MMO, Ordering,
4030 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004031 CSEMap.InsertNode(N, IP);
4032 AllNodes.push_back(N);
4033 return SDValue(N, 0);
4034}
4035
Owen Andersone50ed302009-08-10 22:56:29 +00004036SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004037 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004038 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004039 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004040 unsigned Alignment,
4041 AtomicOrdering Ordering,
4042 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004043 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4044 Alignment = getEVTAlignment(MemVT);
4045
Evan Chengff89dcb2009-10-18 18:16:27 +00004046 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004047 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004048 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004049 // For now, atomics are considered to be volatile always, and they are
4050 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004051 // FIXME: Volatile isn't really correct; we should keep track of atomic
4052 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004053 unsigned Flags = MachineMemOperand::MOVolatile;
4054 if (Opcode != ISD::ATOMIC_STORE)
4055 Flags |= MachineMemOperand::MOLoad;
4056 if (Opcode != ISD::ATOMIC_LOAD)
4057 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004058
4059 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004060 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004061 MemVT.getStoreSize(), Alignment);
4062
Eli Friedman55ba8162011-07-29 03:05:32 +00004063 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4064 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004065}
4066
4067SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4068 SDValue Chain,
4069 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004070 MachineMemOperand *MMO,
4071 AtomicOrdering Ordering,
4072 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004073 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4074 Opcode == ISD::ATOMIC_LOAD_SUB ||
4075 Opcode == ISD::ATOMIC_LOAD_AND ||
4076 Opcode == ISD::ATOMIC_LOAD_OR ||
4077 Opcode == ISD::ATOMIC_LOAD_XOR ||
4078 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004079 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004080 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004081 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004082 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004083 Opcode == ISD::ATOMIC_SWAP ||
4084 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004085 "Invalid Atomic Op");
4086
Owen Andersone50ed302009-08-10 22:56:29 +00004087 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004088
Eli Friedman327236c2011-08-24 20:50:09 +00004089 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4090 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004091 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004092 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004093 SDValue Ops[] = {Chain, Ptr, Val};
4094 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004095 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004096 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004097 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4098 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004099 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004100 }
Dan Gohman95531882010-03-18 18:49:47 +00004101 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004102 Ptr, Val, MMO,
4103 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004104 CSEMap.InsertNode(N, IP);
4105 AllNodes.push_back(N);
4106 return SDValue(N, 0);
4107}
4108
Eli Friedman327236c2011-08-24 20:50:09 +00004109SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4110 EVT VT, SDValue Chain,
4111 SDValue Ptr,
4112 const Value* PtrVal,
4113 unsigned Alignment,
4114 AtomicOrdering Ordering,
4115 SynchronizationScope SynchScope) {
4116 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4117 Alignment = getEVTAlignment(MemVT);
4118
4119 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004120 // An atomic store does not load. An atomic load does not store.
4121 // (An atomicrmw obviously both loads and stores.)
4122 // For now, atomics are considered to be volatile always, and they are
4123 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004124 // FIXME: Volatile isn't really correct; we should keep track of atomic
4125 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004126 unsigned Flags = MachineMemOperand::MOVolatile;
4127 if (Opcode != ISD::ATOMIC_STORE)
4128 Flags |= MachineMemOperand::MOLoad;
4129 if (Opcode != ISD::ATOMIC_LOAD)
4130 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004131
4132 MachineMemOperand *MMO =
4133 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4134 MemVT.getStoreSize(), Alignment);
4135
4136 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4137 Ordering, SynchScope);
4138}
4139
4140SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4141 EVT VT, SDValue Chain,
4142 SDValue Ptr,
4143 MachineMemOperand *MMO,
4144 AtomicOrdering Ordering,
4145 SynchronizationScope SynchScope) {
4146 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4147
4148 SDVTList VTs = getVTList(VT, MVT::Other);
4149 FoldingSetNodeID ID;
4150 ID.AddInteger(MemVT.getRawBits());
4151 SDValue Ops[] = {Chain, Ptr};
4152 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004153 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Eli Friedman327236c2011-08-24 20:50:09 +00004154 void* IP = 0;
4155 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4156 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4157 return SDValue(E, 0);
4158 }
4159 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4160 Ptr, MMO, Ordering, SynchScope);
4161 CSEMap.InsertNode(N, IP);
4162 AllNodes.push_back(N);
4163 return SDValue(N, 0);
4164}
4165
Duncan Sands4bdcb612008-07-02 17:40:58 +00004166/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004167SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4168 DebugLoc dl) {
4169 if (NumOps == 1)
4170 return Ops[0];
4171
Owen Andersone50ed302009-08-10 22:56:29 +00004172 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004173 VTs.reserve(NumOps);
4174 for (unsigned i = 0; i < NumOps; ++i)
4175 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004176 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004177 Ops, NumOps);
4178}
4179
Dan Gohman475871a2008-07-27 21:46:04 +00004180SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004181SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004182 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004183 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004184 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004185 unsigned Align, bool Vol,
4186 bool ReadMem, bool WriteMem) {
4187 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004188 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004189 ReadMem, WriteMem);
4190}
4191
4192SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004193SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4194 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004195 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004196 unsigned Align, bool Vol,
4197 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004198 if (Align == 0) // Ensure that codegen never sees alignment 0
4199 Align = getEVTAlignment(MemVT);
4200
4201 MachineFunction &MF = getMachineFunction();
4202 unsigned Flags = 0;
4203 if (WriteMem)
4204 Flags |= MachineMemOperand::MOStore;
4205 if (ReadMem)
4206 Flags |= MachineMemOperand::MOLoad;
4207 if (Vol)
4208 Flags |= MachineMemOperand::MOVolatile;
4209 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004210 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004211
4212 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4213}
4214
4215SDValue
4216SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4217 const SDValue *Ops, unsigned NumOps,
4218 EVT MemVT, MachineMemOperand *MMO) {
4219 assert((Opcode == ISD::INTRINSIC_VOID ||
4220 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004221 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004222 Opcode == ISD::LIFETIME_START ||
4223 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004224 (Opcode <= INT_MAX &&
4225 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4226 "Opcode is not a memory-accessing opcode!");
4227
Dale Johannesene8c17332009-01-29 00:47:48 +00004228 // Memoize the node unless it returns a flag.
4229 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004230 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004231 FoldingSetNodeID ID;
4232 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004233 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004234 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004235 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4236 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004237 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004239
Dan Gohman95531882010-03-18 18:49:47 +00004240 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4241 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004242 CSEMap.InsertNode(N, IP);
4243 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004244 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4245 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004246 }
4247 AllNodes.push_back(N);
4248 return SDValue(N, 0);
4249}
4250
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004251/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4252/// MachinePointerInfo record from it. This is particularly useful because the
4253/// code generator has many cases where it doesn't bother passing in a
4254/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4255static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4256 // If this is FI+Offset, we can model it.
4257 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4258 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4259
4260 // If this is (FI+Offset1)+Offset2, we can model it.
4261 if (Ptr.getOpcode() != ISD::ADD ||
4262 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4263 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4264 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004265
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004266 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4267 return MachinePointerInfo::getFixedStack(FI, Offset+
4268 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4269}
4270
4271/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4272/// MachinePointerInfo record from it. This is particularly useful because the
4273/// code generator has many cases where it doesn't bother passing in a
4274/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4275static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4276 // If the 'Offset' value isn't a constant, we can't handle this.
4277 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4278 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4279 if (OffsetOp.getOpcode() == ISD::UNDEF)
4280 return InferPointerInfo(Ptr);
4281 return MachinePointerInfo();
4282}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004283
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004284
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004285SDValue
4286SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4287 EVT VT, DebugLoc dl, SDValue Chain,
4288 SDValue Ptr, SDValue Offset,
4289 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004290 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004291 unsigned Alignment, const MDNode *TBAAInfo,
4292 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004293 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004294 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004295 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4296 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004297
Dan Gohmanc76909a2009-09-25 20:36:54 +00004298 unsigned Flags = MachineMemOperand::MOLoad;
4299 if (isVolatile)
4300 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004301 if (isNonTemporal)
4302 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004303 if (isInvariant)
4304 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004305
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004306 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4307 // clients.
4308 if (PtrInfo.V == 0)
4309 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004310
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004311 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004312 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004313 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004314 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004315 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004316}
4317
4318SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004319SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004320 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004321 SDValue Ptr, SDValue Offset, EVT MemVT,
4322 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004323 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004324 ExtType = ISD::NON_EXTLOAD;
4325 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004326 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004327 } else {
4328 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004329 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4330 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004331 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004332 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004333 assert(VT.isVector() == MemVT.isVector() &&
4334 "Cannot use trunc store to convert to or from a vector!");
4335 assert((!VT.isVector() ||
4336 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4337 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004338 }
4339
4340 bool Indexed = AM != ISD::UNINDEXED;
4341 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4342 "Unindexed load with an offset!");
4343
4344 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004345 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004346 SDValue Ops[] = { Chain, Ptr, Offset };
4347 FoldingSetNodeID ID;
4348 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004349 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004350 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004351 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004352 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004353 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004354 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004355 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4356 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004357 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004358 }
Dan Gohman95531882010-03-18 18:49:47 +00004359 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4360 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004361 CSEMap.InsertNode(N, IP);
4362 AllNodes.push_back(N);
4363 return SDValue(N, 0);
4364}
4365
Owen Andersone50ed302009-08-10 22:56:29 +00004366SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004367 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004368 MachinePointerInfo PtrInfo,
4369 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004370 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004371 const MDNode *TBAAInfo,
4372 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004373 SDValue Undef = getUNDEF(Ptr.getValueType());
4374 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004375 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4376 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004377}
Dale Johannesene8c17332009-01-29 00:47:48 +00004378
Stuart Hastingsa9011292011-02-16 16:23:55 +00004379SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004380 SDValue Chain, SDValue Ptr,
4381 MachinePointerInfo PtrInfo, EVT MemVT,
4382 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004383 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004384 SDValue Undef = getUNDEF(Ptr.getValueType());
4385 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004386 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004387 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004388}
4389
4390
Dan Gohman475871a2008-07-27 21:46:04 +00004391SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004392SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4393 SDValue Offset, ISD::MemIndexedMode AM) {
4394 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4395 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4396 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004397 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004398 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004399 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004400 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004401}
4402
Dale Johannesene8c17332009-01-29 00:47:48 +00004403SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004404 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004405 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004406 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004407 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004408 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004409 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004410 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004411
Dan Gohmanc76909a2009-09-25 20:36:54 +00004412 unsigned Flags = MachineMemOperand::MOStore;
4413 if (isVolatile)
4414 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004415 if (isNonTemporal)
4416 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004417
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004418 if (PtrInfo.V == 0)
4419 PtrInfo = InferPointerInfo(Ptr);
4420
4421 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004422 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004423 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004424 Val.getValueType().getStoreSize(), Alignment,
4425 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004426
4427 return getStore(Chain, dl, Val, Ptr, MMO);
4428}
4429
4430SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4431 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004432 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004433 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004434 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004436 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004437 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4438 FoldingSetNodeID ID;
4439 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004440 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004441 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004442 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004443 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004444 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004445 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4446 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004447 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004448 }
Dan Gohman95531882010-03-18 18:49:47 +00004449 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4450 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004451 CSEMap.InsertNode(N, IP);
4452 AllNodes.push_back(N);
4453 return SDValue(N, 0);
4454}
4455
Dale Johannesene8c17332009-01-29 00:47:48 +00004456SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004457 SDValue Ptr, MachinePointerInfo PtrInfo,
4458 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004459 unsigned Alignment,
4460 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004461 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004462 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004463 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4464 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004465
Dan Gohmanc76909a2009-09-25 20:36:54 +00004466 unsigned Flags = MachineMemOperand::MOStore;
4467 if (isVolatile)
4468 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004469 if (isNonTemporal)
4470 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004471
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004472 if (PtrInfo.V == 0)
4473 PtrInfo = InferPointerInfo(Ptr);
4474
4475 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004476 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004477 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4478 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004479
4480 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4481}
4482
4483SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4484 SDValue Ptr, EVT SVT,
4485 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004486 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004487
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 (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004491 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004492
Dan Gohman2e141d72009-12-14 23:40:38 +00004493 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4494 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004495 assert(VT.isInteger() == SVT.isInteger() &&
4496 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004497 assert(VT.isVector() == SVT.isVector() &&
4498 "Cannot use trunc store to convert to or from a vector!");
4499 assert((!VT.isVector() ||
4500 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4501 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004502
Owen Anderson825b72b2009-08-11 20:47:22 +00004503 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004504 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004505 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4506 FoldingSetNodeID ID;
4507 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004508 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004509 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004510 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004511 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004512 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004513 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4514 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004515 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004516 }
Dan Gohman95531882010-03-18 18:49:47 +00004517 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4518 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004519 CSEMap.InsertNode(N, IP);
4520 AllNodes.push_back(N);
4521 return SDValue(N, 0);
4522}
4523
Dan Gohman475871a2008-07-27 21:46:04 +00004524SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004525SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4526 SDValue Offset, ISD::MemIndexedMode AM) {
4527 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4528 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4529 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004530 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004531 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4532 FoldingSetNodeID ID;
4533 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004534 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004535 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004536 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004537 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004538 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004539 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004540
Dan Gohman95531882010-03-18 18:49:47 +00004541 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4542 ST->isTruncatingStore(),
4543 ST->getMemoryVT(),
4544 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004545 CSEMap.InsertNode(N, IP);
4546 AllNodes.push_back(N);
4547 return SDValue(N, 0);
4548}
4549
Owen Andersone50ed302009-08-10 22:56:29 +00004550SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004551 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004552 SDValue SV,
4553 unsigned Align) {
4554 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4555 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004556}
4557
Owen Andersone50ed302009-08-10 22:56:29 +00004558SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004559 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004560 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004561 case 0: return getNode(Opcode, DL, VT);
4562 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4563 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4564 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004565 default: break;
4566 }
4567
Dan Gohman475871a2008-07-27 21:46:04 +00004568 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004569 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004570 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004571 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004572}
4573
Owen Andersone50ed302009-08-10 22:56:29 +00004574SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004575 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004576 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004577 case 0: return getNode(Opcode, DL, VT);
4578 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4579 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4580 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004581 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004582 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004583
Chris Lattneref847df2005-04-09 03:27:28 +00004584 switch (Opcode) {
4585 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004586 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004587 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004588 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4589 "LHS and RHS of condition must have same type!");
4590 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4591 "True and False arms of SelectCC must have same type!");
4592 assert(Ops[2].getValueType() == VT &&
4593 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004594 break;
4595 }
4596 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004597 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004598 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4599 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004600 break;
4601 }
Chris Lattneref847df2005-04-09 03:27:28 +00004602 }
4603
Chris Lattner385328c2005-05-14 07:42:29 +00004604 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004605 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004606 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004607
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004608 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004609 FoldingSetNodeID ID;
4610 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004611 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004612
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004613 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004614 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004615
Dan Gohman95531882010-03-18 18:49:47 +00004616 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004617 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004618 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004619 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004620 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004621
Chris Lattneref847df2005-04-09 03:27:28 +00004622 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004623#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004624 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004625#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004626 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004627}
4628
Bill Wendling7ade28c2009-01-28 22:17:52 +00004629SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004630 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004631 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004632 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004633 Ops, NumOps);
4634}
4635
Bill Wendling7ade28c2009-01-28 22:17:52 +00004636SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004637 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004638 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004639 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004640 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4641 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004642}
4643
Bill Wendling7ade28c2009-01-28 22:17:52 +00004644SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4645 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004646 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004647 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004648
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004649#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004650 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004651 // FIXME: figure out how to safely handle things like
4652 // int foo(int x) { return 1 << (x & 255); }
4653 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004654 case ISD::SRA_PARTS:
4655 case ISD::SRL_PARTS:
4656 case ISD::SHL_PARTS:
4657 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004658 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004659 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004660 else if (N3.getOpcode() == ISD::AND)
4661 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4662 // If the and is only masking out bits that cannot effect the shift,
4663 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004664 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004665 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004666 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004667 }
4668 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004669 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004670#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004671
Chris Lattner43247a12005-08-25 19:12:10 +00004672 // Memoize the node unless it returns a flag.
4673 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004674 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004675 FoldingSetNodeID ID;
4676 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004677 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004678 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004679 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004680
Dan Gohman0e5f1302008-07-07 23:02:41 +00004681 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004682 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004683 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004684 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004685 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004686 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4687 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004688 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004689 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004690 }
Chris Lattnera5682852006-08-07 23:03:03 +00004691 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004692 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004693 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004694 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004695 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004696 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004697 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004698 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4699 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004700 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004701 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004702 }
Chris Lattner43247a12005-08-25 19:12:10 +00004703 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004704 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004705#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004706 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004707#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004708 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004709}
4710
Bill Wendling7ade28c2009-01-28 22:17:52 +00004711SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4712 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004713}
4714
Bill Wendling7ade28c2009-01-28 22:17:52 +00004715SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4716 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004717 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004718 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004719}
4720
Bill Wendling7ade28c2009-01-28 22:17:52 +00004721SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4722 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004723 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004724 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004725}
4726
Bill Wendling7ade28c2009-01-28 22:17:52 +00004727SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4728 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004729 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004730 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004731}
4732
Bill Wendling7ade28c2009-01-28 22:17:52 +00004733SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4734 SDValue N1, SDValue N2, SDValue N3,
4735 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004736 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004737 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004738}
4739
Bill Wendling7ade28c2009-01-28 22:17:52 +00004740SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4741 SDValue N1, SDValue N2, SDValue N3,
4742 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004743 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004744 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004745}
4746
Owen Andersone50ed302009-08-10 22:56:29 +00004747SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004748 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004749}
4750
Owen Andersone50ed302009-08-10 22:56:29 +00004751SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004752 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4753 E = VTList.rend(); I != E; ++I)
4754 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4755 return *I;
4756
Owen Andersone50ed302009-08-10 22:56:29 +00004757 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004758 Array[0] = VT1;
4759 Array[1] = VT2;
4760 SDVTList Result = makeVTList(Array, 2);
4761 VTList.push_back(Result);
4762 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004763}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004764
Owen Andersone50ed302009-08-10 22:56:29 +00004765SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004766 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4767 E = VTList.rend(); I != E; ++I)
4768 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4769 I->VTs[2] == VT3)
4770 return *I;
4771
Owen Andersone50ed302009-08-10 22:56:29 +00004772 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004773 Array[0] = VT1;
4774 Array[1] = VT2;
4775 Array[2] = VT3;
4776 SDVTList Result = makeVTList(Array, 3);
4777 VTList.push_back(Result);
4778 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004779}
4780
Owen Andersone50ed302009-08-10 22:56:29 +00004781SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004782 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4783 E = VTList.rend(); I != E; ++I)
4784 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4785 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4786 return *I;
4787
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004788 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004789 Array[0] = VT1;
4790 Array[1] = VT2;
4791 Array[2] = VT3;
4792 Array[3] = VT4;
4793 SDVTList Result = makeVTList(Array, 4);
4794 VTList.push_back(Result);
4795 return Result;
4796}
4797
Owen Andersone50ed302009-08-10 22:56:29 +00004798SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004799 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004800 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004801 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004802 case 2: return getVTList(VTs[0], VTs[1]);
4803 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004804 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004805 default: break;
4806 }
4807
Dan Gohmane8be6c62008-07-17 19:10:17 +00004808 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4809 E = VTList.rend(); I != E; ++I) {
4810 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4811 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004812
Benjamin Kramerb26e2912012-07-19 10:46:05 +00004813 if (std::equal(&VTs[2], &VTs[NumVTs], &I->VTs[2]))
Dan Gohmane8be6c62008-07-17 19:10:17 +00004814 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004816
Owen Andersone50ed302009-08-10 22:56:29 +00004817 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004818 std::copy(VTs, VTs+NumVTs, Array);
4819 SDVTList Result = makeVTList(Array, NumVTs);
4820 VTList.push_back(Result);
4821 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004822}
4823
4824
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004825/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4826/// specified operands. If the resultant node already exists in the DAG,
4827/// this does not modify the specified node, instead it returns the node that
4828/// already exists. If the resultant node does not exist in the DAG, the
4829/// input node is returned. As a degenerate case, if you specify the same
4830/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004831SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004832 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004833
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004834 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004835 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004836
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004837 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004838 void *InsertPos = 0;
4839 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004840 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004841
Dan Gohman79acd2b2008-07-21 22:38:59 +00004842 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004843 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004844 if (!RemoveNodeFromCSEMaps(N))
4845 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004846
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004847 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004848 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004849
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004850 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004851 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004852 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004853}
4854
Dan Gohman027657d2010-06-18 15:30:29 +00004855SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004856 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004857
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004858 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004859 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004860 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004861
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004862 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004863 void *InsertPos = 0;
4864 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004865 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004866
Dan Gohman79acd2b2008-07-21 22:38:59 +00004867 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004868 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004869 if (!RemoveNodeFromCSEMaps(N))
4870 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004871
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004872 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004873 if (N->OperandList[0] != Op1)
4874 N->OperandList[0].set(Op1);
4875 if (N->OperandList[1] != Op2)
4876 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004877
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004878 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004879 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004880 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004881}
4882
Dan Gohman027657d2010-06-18 15:30:29 +00004883SDNode *SelectionDAG::
4884UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004885 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004886 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004887}
4888
Dan Gohman027657d2010-06-18 15:30:29 +00004889SDNode *SelectionDAG::
4890UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004891 SDValue Op3, SDValue Op4) {
4892 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004893 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004894}
4895
Dan Gohman027657d2010-06-18 15:30:29 +00004896SDNode *SelectionDAG::
4897UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004898 SDValue Op3, SDValue Op4, SDValue Op5) {
4899 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004900 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004901}
4902
Dan Gohman027657d2010-06-18 15:30:29 +00004903SDNode *SelectionDAG::
4904UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004905 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004906 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004907
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004908 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004909 bool AnyChange = false;
4910 for (unsigned i = 0; i != NumOps; ++i) {
4911 if (Ops[i] != N->getOperand(i)) {
4912 AnyChange = true;
4913 break;
4914 }
4915 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004916
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004917 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004918 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004919
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004920 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004921 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004922 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004923 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004924
Dan Gohman7ceda162008-05-02 00:05:03 +00004925 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004926 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004927 if (!RemoveNodeFromCSEMaps(N))
4928 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004929
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004930 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004931 for (unsigned i = 0; i != NumOps; ++i)
4932 if (N->OperandList[i] != Ops[i])
4933 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004934
4935 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004936 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004937 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004938}
4939
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004940/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004941/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004942void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004943 // Unlike the code in MorphNodeTo that does this, we don't need to
4944 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004945 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4946 SDUse &Use = *I++;
4947 Use.set(SDValue());
4948 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004949}
Chris Lattner149c58c2005-08-16 18:17:10 +00004950
Dan Gohmane8be6c62008-07-17 19:10:17 +00004951/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4952/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004953///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004954SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004955 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004956 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004957 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004958}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004959
Dan Gohmane8be6c62008-07-17 19:10:17 +00004960SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004961 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004962 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004963 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004964 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004965}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004966
Dan Gohmane8be6c62008-07-17 19:10:17 +00004967SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004968 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004969 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004970 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004971 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004972 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004973}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004974
Dan Gohmane8be6c62008-07-17 19:10:17 +00004975SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004976 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004977 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004978 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004979 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004980 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004981}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004982
Dan Gohmane8be6c62008-07-17 19:10:17 +00004983SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004984 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004985 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004986 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004987 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004988}
4989
Dan Gohmane8be6c62008-07-17 19:10:17 +00004990SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004991 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004992 unsigned NumOps) {
4993 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004994 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004995}
4996
Dan Gohmane8be6c62008-07-17 19:10:17 +00004997SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004998 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004999 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005000 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00005001}
5002
Dan Gohmane8be6c62008-07-17 19:10:17 +00005003SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005004 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00005005 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005006 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005007 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005008}
5009
Bill Wendling13d6d442008-12-01 23:28:22 +00005010SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005011 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00005012 const SDValue *Ops, unsigned NumOps) {
5013 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5014 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5015}
5016
Scott Michelfdc40a02009-02-17 22:15:04 +00005017SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005018 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005019 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005020 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005021 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005022 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005023}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005024
Scott Michelfdc40a02009-02-17 22:15:04 +00005025SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005026 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005027 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005028 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005030 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005031}
5032
Dan Gohmane8be6c62008-07-17 19:10:17 +00005033SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005034 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005035 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005036 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005037 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005038 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005039 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00005040}
5041
Dan Gohmane8be6c62008-07-17 19:10:17 +00005042SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005043 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005044 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005045 SDValue Op3) {
5046 SDVTList VTs = getVTList(VT1, VT2, VT3);
5047 SDValue Ops[] = { Op1, Op2, Op3 };
5048 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5049}
5050
5051SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00005052 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005053 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00005054 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5055 // Reset the NodeID to -1.
5056 N->setNodeId(-1);
5057 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005058}
5059
Devang Patel0508d042011-12-15 18:21:18 +00005060/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
5061/// the line number information on the merged node since it is not possible to
5062/// preserve the information that operation is associated with multiple lines.
5063/// This will make the debugger working better at -O0, were there is a higher
5064/// probability having other instructions associated with that line.
5065///
5066SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
5067 DebugLoc NLoc = N->getDebugLoc();
5068 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
5069 N->setDebugLoc(DebugLoc());
5070 }
5071 return N;
5072}
5073
Chris Lattner21221e32010-02-28 21:36:14 +00005074/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005075/// return type, opcode, and operands.
5076///
5077/// Note that MorphNodeTo returns the resultant node. If there is already a
5078/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00005079/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005080///
5081/// Using MorphNodeTo is faster than creating a new node and swapping it in
5082/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005083/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005084/// the node's users.
5085///
5086SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005087 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005088 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005089 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005090 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005091 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005092 FoldingSetNodeID ID;
5093 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005094 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00005095 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005096 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005097
Dan Gohman095cc292008-09-13 01:54:27 +00005098 if (!RemoveNodeFromCSEMaps(N))
5099 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005100
Dan Gohmane8be6c62008-07-17 19:10:17 +00005101 // Start the morphing.
5102 N->NodeType = Opc;
5103 N->ValueList = VTs.VTs;
5104 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005105
Dan Gohmane8be6c62008-07-17 19:10:17 +00005106 // Clear the operands list, updating used nodes to remove this from their
5107 // use list. Keep track of any operands that become dead as a result.
5108 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005109 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5110 SDUse &Use = *I++;
5111 SDNode *Used = Use.getNode();
5112 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005113 if (Used->use_empty())
5114 DeadNodeSet.insert(Used);
5115 }
5116
Dan Gohmanc76909a2009-09-25 20:36:54 +00005117 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5118 // Initialize the memory references information.
5119 MN->setMemRefs(0, 0);
5120 // If NumOps is larger than the # of operands we can have in a
5121 // MachineSDNode, reallocate the operand list.
5122 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5123 if (MN->OperandsNeedDelete)
5124 delete[] MN->OperandList;
5125 if (NumOps > array_lengthof(MN->LocalOperands))
5126 // We're creating a final node that will live unmorphed for the
5127 // remainder of the current SelectionDAG iteration, so we can allocate
5128 // the operands directly out of a pool with no recycling metadata.
5129 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005130 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005131 else
5132 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5133 MN->OperandsNeedDelete = false;
5134 } else
5135 MN->InitOperands(MN->OperandList, Ops, NumOps);
5136 } else {
5137 // If NumOps is larger than the # of operands we currently have, reallocate
5138 // the operand list.
5139 if (NumOps > N->NumOperands) {
5140 if (N->OperandsNeedDelete)
5141 delete[] N->OperandList;
5142 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005143 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005144 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005145 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005146 }
5147
5148 // Delete any nodes that are still dead after adding the uses for the
5149 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005150 if (!DeadNodeSet.empty()) {
5151 SmallVector<SDNode *, 16> DeadNodes;
5152 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5153 E = DeadNodeSet.end(); I != E; ++I)
5154 if ((*I)->use_empty())
5155 DeadNodes.push_back(*I);
5156 RemoveDeadNodes(DeadNodes);
5157 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005158
Dan Gohmane8be6c62008-07-17 19:10:17 +00005159 if (IP)
5160 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005161 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005162}
5163
Chris Lattner0fb094f2005-11-19 01:44:53 +00005164
Dan Gohman602b0c82009-09-25 18:54:59 +00005165/// getMachineNode - These are used for target selectors to create a new node
5166/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005167///
Dan Gohman602b0c82009-09-25 18:54:59 +00005168/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005169/// node of the specified opcode and operands, it returns that node instead of
5170/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005171MachineSDNode *
5172SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005173 SDVTList VTs = getVTList(VT);
5174 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005175}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005176
Dan Gohmanc81b7832009-10-10 01:29:16 +00005177MachineSDNode *
5178SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005179 SDVTList VTs = getVTList(VT);
5180 SDValue Ops[] = { Op1 };
5181 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005182}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005183
Dan Gohmanc81b7832009-10-10 01:29:16 +00005184MachineSDNode *
5185SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5186 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005187 SDVTList VTs = getVTList(VT);
5188 SDValue Ops[] = { Op1, Op2 };
5189 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005190}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005191
Dan Gohmanc81b7832009-10-10 01:29:16 +00005192MachineSDNode *
5193SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5194 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005195 SDVTList VTs = getVTList(VT);
5196 SDValue Ops[] = { Op1, Op2, Op3 };
5197 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005198}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005199
Dan Gohmanc81b7832009-10-10 01:29:16 +00005200MachineSDNode *
5201SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5202 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005203 SDVTList VTs = getVTList(VT);
5204 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005205}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005206
Dan Gohmanc81b7832009-10-10 01:29:16 +00005207MachineSDNode *
5208SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005209 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005210 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005211}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005212
Dan Gohmanc81b7832009-10-10 01:29:16 +00005213MachineSDNode *
5214SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5215 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005216 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005217 SDValue Ops[] = { Op1 };
5218 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005219}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005220
Dan Gohmanc81b7832009-10-10 01:29:16 +00005221MachineSDNode *
5222SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5223 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005224 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005225 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005226 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005227}
5228
Dan Gohmanc81b7832009-10-10 01:29:16 +00005229MachineSDNode *
5230SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5231 EVT VT1, EVT VT2, SDValue Op1,
5232 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005233 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005234 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005235 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005236}
5237
Dan Gohmanc81b7832009-10-10 01:29:16 +00005238MachineSDNode *
5239SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5240 EVT VT1, EVT VT2,
5241 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005242 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005243 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005244}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005245
Dan Gohmanc81b7832009-10-10 01:29:16 +00005246MachineSDNode *
5247SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5248 EVT VT1, EVT VT2, EVT VT3,
5249 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005250 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005251 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005252 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005253}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005254
Dan Gohmanc81b7832009-10-10 01:29:16 +00005255MachineSDNode *
5256SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5257 EVT VT1, EVT VT2, EVT VT3,
5258 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005259 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005260 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005261 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005262}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005263
Dan Gohmanc81b7832009-10-10 01:29:16 +00005264MachineSDNode *
5265SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5266 EVT VT1, EVT VT2, EVT VT3,
5267 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005268 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005269 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005270}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005271
Dan Gohmanc81b7832009-10-10 01:29:16 +00005272MachineSDNode *
5273SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5274 EVT VT2, EVT VT3, EVT VT4,
5275 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005276 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005277 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005278}
5279
Dan Gohmanc81b7832009-10-10 01:29:16 +00005280MachineSDNode *
5281SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5282 const std::vector<EVT> &ResultTys,
5283 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005284 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5285 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5286}
5287
Dan Gohmanc81b7832009-10-10 01:29:16 +00005288MachineSDNode *
5289SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5290 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005291 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005292 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005293 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005294
5295 if (DoCSE) {
5296 FoldingSetNodeID ID;
5297 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5298 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005299 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5300 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5301 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005302 }
5303
5304 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005305 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005306
5307 // Initialize the operands list.
5308 if (NumOps > array_lengthof(N->LocalOperands))
5309 // We're creating a final node that will live unmorphed for the
5310 // remainder of the current SelectionDAG iteration, so we can allocate
5311 // the operands directly out of a pool with no recycling metadata.
5312 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5313 Ops, NumOps);
5314 else
5315 N->InitOperands(N->LocalOperands, Ops, NumOps);
5316 N->OperandsNeedDelete = false;
5317
5318 if (DoCSE)
5319 CSEMap.InsertNode(N, IP);
5320
5321 AllNodes.push_back(N);
5322#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005323 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005324#endif
5325 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005326}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005327
Dan Gohman6a402dc2009-08-19 18:16:17 +00005328/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005329/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005330SDValue
5331SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5332 SDValue Operand) {
5333 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005334 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005335 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005336 return SDValue(Subreg, 0);
5337}
5338
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005339/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005340/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005341SDValue
5342SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5343 SDValue Operand, SDValue Subreg) {
5344 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005345 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005346 VT, Operand, Subreg, SRIdxVal);
5347 return SDValue(Result, 0);
5348}
5349
Evan Cheng08b11732008-03-22 01:55:50 +00005350/// getNodeIfExists - Get the specified node if it's already available, or
5351/// else return NULL.
5352SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005353 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005354 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005355 FoldingSetNodeID ID;
5356 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5357 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005358 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005359 return E;
5360 }
5361 return NULL;
5362}
5363
Evan Cheng31441b72010-03-29 20:48:30 +00005364/// getDbgValue - Creates a SDDbgValue node.
5365///
5366SDDbgValue *
5367SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5368 DebugLoc DL, unsigned O) {
5369 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5370}
5371
5372SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005373SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005374 DebugLoc DL, unsigned O) {
5375 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5376}
5377
5378SDDbgValue *
5379SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5380 DebugLoc DL, unsigned O) {
5381 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5382}
5383
Dan Gohmana72d2a22010-03-03 21:33:37 +00005384namespace {
5385
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005386/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005387/// pointed to by a use iterator is deleted, increment the use iterator
5388/// so that it doesn't dangle.
5389///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005390class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005391 SDNode::use_iterator &UI;
5392 SDNode::use_iterator &UE;
5393
5394 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5395 // Increment the iterator as needed.
5396 while (UI != UE && N == *UI)
5397 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005398 }
5399
5400public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005401 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005402 SDNode::use_iterator &ui,
5403 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005404 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005405};
5406
5407}
5408
Evan Cheng99157a02006-08-07 22:13:29 +00005409/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005410/// This can cause recursive merging of nodes in the DAG.
5411///
Chris Lattner11d049c2008-02-03 03:35:22 +00005412/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005413///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005414void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005415 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005416 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005417 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005418 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005419
Dan Gohman39946102009-01-25 16:29:12 +00005420 // Iterate over all the existing uses of From. New uses will be added
5421 // to the beginning of the use list, which we avoid visiting.
5422 // This specifically avoids visiting uses of From that arise while the
5423 // replacement is happening, because any such uses would be the result
5424 // of CSE: If an existing node looks like From after one of its operands
5425 // is replaced by To, we don't want to replace of all its users with To
5426 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005427 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005428 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005429 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005430 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005431
Chris Lattner8b8749f2005-08-17 19:00:20 +00005432 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005433 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005434
Dan Gohman39946102009-01-25 16:29:12 +00005435 // A user can appear in a use list multiple times, and when this
5436 // happens the uses are usually next to each other in the list.
5437 // To help reduce the number of CSE recomputations, process all
5438 // the uses of this user that we can find this way.
5439 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005440 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005441 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005442 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005443 } while (UI != UE && *UI == User);
5444
5445 // Now that we have modified User, add it back to the CSE maps. If it
5446 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005447 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005448 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005449
5450 // If we just RAUW'd the root, take note.
5451 if (FromN == getRoot())
5452 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005453}
5454
5455/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5456/// This can cause recursive merging of nodes in the DAG.
5457///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005458/// This version assumes that for each value of From, there is a
5459/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005460///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005461void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005462#ifndef NDEBUG
5463 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5464 assert((!From->hasAnyUseOfValue(i) ||
5465 From->getValueType(i) == To->getValueType(i)) &&
5466 "Cannot use this version of ReplaceAllUsesWith!");
5467#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005468
5469 // Handle the trivial case.
5470 if (From == To)
5471 return;
5472
Dan Gohmandbe664a2009-01-19 21:44:21 +00005473 // Iterate over just the existing users of From. See the comments in
5474 // the ReplaceAllUsesWith above.
5475 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005476 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005477 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005478 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005479
Chris Lattner8b52f212005-08-26 18:36:28 +00005480 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005481 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005482
Dan Gohman39946102009-01-25 16:29:12 +00005483 // A user can appear in a use list multiple times, and when this
5484 // happens the uses are usually next to each other in the list.
5485 // To help reduce the number of CSE recomputations, process all
5486 // the uses of this user that we can find this way.
5487 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005488 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005489 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005490 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005491 } while (UI != UE && *UI == User);
5492
5493 // Now that we have modified User, add it back to the CSE maps. If it
5494 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005495 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005496 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005497
5498 // If we just RAUW'd the root, take note.
5499 if (From == getRoot().getNode())
5500 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005501}
5502
Chris Lattner8b52f212005-08-26 18:36:28 +00005503/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5504/// This can cause recursive merging of nodes in the DAG.
5505///
5506/// This version can replace From with any result values. To must match the
5507/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005508void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005509 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005510 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005511
Dan Gohmandbe664a2009-01-19 21:44:21 +00005512 // Iterate over just the existing users of From. See the comments in
5513 // the ReplaceAllUsesWith above.
5514 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005515 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005516 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005517 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005518
Chris Lattner7b2880c2005-08-24 22:44:39 +00005519 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005520 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005521
Dan Gohman39946102009-01-25 16:29:12 +00005522 // A user can appear in a use list multiple times, and when this
5523 // happens the uses are usually next to each other in the list.
5524 // To help reduce the number of CSE recomputations, process all
5525 // the uses of this user that we can find this way.
5526 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005527 SDUse &Use = UI.getUse();
5528 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005529 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005530 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005531 } while (UI != UE && *UI == User);
5532
5533 // Now that we have modified User, add it back to the CSE maps. If it
5534 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005535 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005536 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005537
5538 // If we just RAUW'd the root, take note.
5539 if (From == getRoot().getNode())
5540 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005541}
5542
Chris Lattner012f2412006-02-17 21:58:01 +00005543/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005544/// uses of other values produced by From.getNode() alone. The Deleted
5545/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005546void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005547 // Handle the really simple, really trivial case efficiently.
5548 if (From == To) return;
5549
Chris Lattner012f2412006-02-17 21:58:01 +00005550 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005551 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005552 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005553 return;
5554 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005555
Dan Gohman39946102009-01-25 16:29:12 +00005556 // Iterate over just the existing users of From. See the comments in
5557 // the ReplaceAllUsesWith above.
5558 SDNode::use_iterator UI = From.getNode()->use_begin(),
5559 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005560 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005561 while (UI != UE) {
5562 SDNode *User = *UI;
5563 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005564
Dan Gohman39946102009-01-25 16:29:12 +00005565 // A user can appear in a use list multiple times, and when this
5566 // happens the uses are usually next to each other in the list.
5567 // To help reduce the number of CSE recomputations, process all
5568 // the uses of this user that we can find this way.
5569 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005570 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005571
5572 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005573 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005574 ++UI;
5575 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005576 }
Dan Gohman39946102009-01-25 16:29:12 +00005577
5578 // If this node hasn't been modified yet, it's still in the CSE maps,
5579 // so remove its old self from the CSE maps.
5580 if (!UserRemovedFromCSEMaps) {
5581 RemoveNodeFromCSEMaps(User);
5582 UserRemovedFromCSEMaps = true;
5583 }
5584
5585 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005586 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005587 } while (UI != UE && *UI == User);
5588
5589 // We are iterating over all uses of the From node, so if a use
5590 // doesn't use the specific value, no changes are made.
5591 if (!UserRemovedFromCSEMaps)
5592 continue;
5593
Chris Lattner01d029b2007-10-15 06:10:22 +00005594 // Now that we have modified User, add it back to the CSE maps. If it
5595 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005596 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005597 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005598
5599 // If we just RAUW'd the root, take note.
5600 if (From == getRoot())
5601 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005602}
5603
Dan Gohman39946102009-01-25 16:29:12 +00005604namespace {
5605 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5606 /// to record information about a use.
5607 struct UseMemo {
5608 SDNode *User;
5609 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005610 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005611 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005612
5613 /// operator< - Sort Memos by User.
5614 bool operator<(const UseMemo &L, const UseMemo &R) {
5615 return (intptr_t)L.User < (intptr_t)R.User;
5616 }
Dan Gohman39946102009-01-25 16:29:12 +00005617}
5618
Dan Gohmane8be6c62008-07-17 19:10:17 +00005619/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005620/// uses of other values produced by From.getNode() alone. The same value
5621/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005622/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005623void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5624 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005625 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005626 // Handle the simple, trivial case efficiently.
5627 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005628 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005629
Dan Gohman39946102009-01-25 16:29:12 +00005630 // Read up all the uses and make records of them. This helps
5631 // processing new uses that are introduced during the
5632 // replacement process.
5633 SmallVector<UseMemo, 4> Uses;
5634 for (unsigned i = 0; i != Num; ++i) {
5635 unsigned FromResNo = From[i].getResNo();
5636 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005637 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005638 E = FromNode->use_end(); UI != E; ++UI) {
5639 SDUse &Use = UI.getUse();
5640 if (Use.getResNo() == FromResNo) {
5641 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005642 Uses.push_back(Memo);
5643 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005644 }
Dan Gohman39946102009-01-25 16:29:12 +00005645 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005646
Dan Gohmane7852d02009-01-26 04:35:06 +00005647 // Sort the uses, so that all the uses from a given User are together.
5648 std::sort(Uses.begin(), Uses.end());
5649
Dan Gohman39946102009-01-25 16:29:12 +00005650 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5651 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005652 // We know that this user uses some value of From. If it is the right
5653 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005654 SDNode *User = Uses[UseIndex].User;
5655
5656 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005657 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005658
Dan Gohmane7852d02009-01-26 04:35:06 +00005659 // The Uses array is sorted, so all the uses for a given User
5660 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005661 // To help reduce the number of CSE recomputations, process all
5662 // the uses of this user that we can find this way.
5663 do {
5664 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005665 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005666 ++UseIndex;
5667
Dan Gohmane7852d02009-01-26 04:35:06 +00005668 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005669 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5670
Dan Gohmane8be6c62008-07-17 19:10:17 +00005671 // Now that we have modified User, add it back to the CSE maps. If it
5672 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005673 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005674 }
5675}
5676
Evan Chenge6f35d82006-08-01 08:20:41 +00005677/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005678/// based on their topological order. It returns the maximum id and a vector
5679/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005680unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005681
Dan Gohmanf06c8352008-09-30 18:30:35 +00005682 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005683
Dan Gohmanf06c8352008-09-30 18:30:35 +00005684 // SortedPos tracks the progress of the algorithm. Nodes before it are
5685 // sorted, nodes after it are unsorted. When the algorithm completes
5686 // it is at the end of the list.
5687 allnodes_iterator SortedPos = allnodes_begin();
5688
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005689 // Visit all the nodes. Move nodes with no operands to the front of
5690 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005691 // operand count. Before we do this, the Node Id fields of the nodes
5692 // may contain arbitrary values. After, the Node Id fields for nodes
5693 // before SortedPos will contain the topological sort index, and the
5694 // Node Id fields for nodes At SortedPos and after will contain the
5695 // count of outstanding operands.
5696 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5697 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005698 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005699 unsigned Degree = N->getNumOperands();
5700 if (Degree == 0) {
5701 // A node with no uses, add it to the result array immediately.
5702 N->setNodeId(DAGSize++);
5703 allnodes_iterator Q = N;
5704 if (Q != SortedPos)
5705 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005706 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005707 ++SortedPos;
5708 } else {
5709 // Temporarily use the Node Id as scratch space for the degree count.
5710 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005711 }
5712 }
5713
Chad Rosierf496dea2012-05-21 17:13:41 +00005714 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005715 // such that by the time the end is reached all nodes will be sorted.
5716 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5717 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005718 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005719 // N is in sorted position, so all its uses have one less operand
5720 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005721 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5722 UI != UE; ++UI) {
5723 SDNode *P = *UI;
5724 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005725 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005726 --Degree;
5727 if (Degree == 0) {
5728 // All of P's operands are sorted, so P may sorted now.
5729 P->setNodeId(DAGSize++);
5730 if (P != SortedPos)
5731 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005732 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005733 ++SortedPos;
5734 } else {
5735 // Update P's outstanding operand count.
5736 P->setNodeId(Degree);
5737 }
5738 }
David Greene221925e2010-01-20 00:59:23 +00005739 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005740#ifndef NDEBUG
5741 SDNode *S = ++I;
5742 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005743 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005744#endif
5745 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005746 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005747 }
5748
5749 assert(SortedPos == AllNodes.end() &&
5750 "Topological sort incomplete!");
5751 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5752 "First node in topological sort is not the entry token!");
5753 assert(AllNodes.front().getNodeId() == 0 &&
5754 "First node in topological sort has non-zero id!");
5755 assert(AllNodes.front().getNumOperands() == 0 &&
5756 "First node in topological sort has operands!");
5757 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5758 "Last node in topologic sort has unexpected id!");
5759 assert(AllNodes.back().use_empty() &&
5760 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005761 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005762 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005763}
5764
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005765/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005766void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005767 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005768 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005769}
Evan Chenge6f35d82006-08-01 08:20:41 +00005770
Bill Wendling0777e922009-12-21 21:59:52 +00005771/// GetOrdering - Get the order for the SDNode.
5772unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5773 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005774 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005775}
5776
Evan Chengbfcb3052010-03-25 01:38:16 +00005777/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5778/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005779void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5780 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005781 if (SD)
5782 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005783}
Evan Cheng091cba12006-07-27 06:39:06 +00005784
Devang Patela2e868d2011-01-25 23:27:42 +00005785/// TransferDbgValues - Transfer SDDbgValues.
5786void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5787 if (From == To || !From.getNode()->getHasDebugValue())
5788 return;
5789 SDNode *FromNode = From.getNode();
5790 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005791 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005792 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005793 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005794 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005795 SDDbgValue *Dbg = *I;
5796 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5797 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5798 Dbg->getOffset(), Dbg->getDebugLoc(),
5799 Dbg->getOrder());
5800 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005801 }
5802 }
Devang Patela778f5c2011-02-18 22:43:42 +00005803 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5804 E = ClonedDVs.end(); I != E; ++I)
5805 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005806}
5807
Jim Laskey58b968b2005-08-17 20:08:02 +00005808//===----------------------------------------------------------------------===//
5809// SDNode Class
5810//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005811
Chris Lattner48b85922007-02-04 02:41:42 +00005812HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005813 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005814}
5815
Devang Patel0d881da2010-07-06 22:08:15 +00005816GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5817 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005818 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005819 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005820 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005821}
Chris Lattner48b85922007-02-04 02:41:42 +00005822
Owen Andersone50ed302009-08-10 22:56:29 +00005823MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005824 MachineMemOperand *mmo)
5825 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005826 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005827 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005828 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005829 assert(isNonTemporal() == MMO->isNonTemporal() &&
5830 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005831 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005832}
5833
Scott Michelfdc40a02009-02-17 22:15:04 +00005834MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005835 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005836 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005837 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005838 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005839 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005840 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005841 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5842 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005843}
5844
Jim Laskey583bd472006-10-27 23:46:08 +00005845/// Profile - Gather unique data for the node.
5846///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005847void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005848 AddNodeIDNode(ID, this);
5849}
5850
Owen Andersond8110fb2009-08-25 22:27:22 +00005851namespace {
5852 struct EVTArray {
5853 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005854
Owen Andersond8110fb2009-08-25 22:27:22 +00005855 EVTArray() {
5856 VTs.reserve(MVT::LAST_VALUETYPE);
5857 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5858 VTs.push_back(MVT((MVT::SimpleValueType)i));
5859 }
5860 };
5861}
5862
Owen Andersone50ed302009-08-10 22:56:29 +00005863static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005864static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005865static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005866
Chris Lattnera3255112005-11-08 23:30:28 +00005867/// getValueTypeList - Return a pointer to the specified value type.
5868///
Owen Andersone50ed302009-08-10 22:56:29 +00005869const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005870 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005871 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005872 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005873 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005874 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005875 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005876 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005877 }
Chris Lattnera3255112005-11-08 23:30:28 +00005878}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005879
Chris Lattner5c884562005-01-12 18:37:47 +00005880/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5881/// indicated value. This method ignores uses of other values defined by this
5882/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005883bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005884 assert(Value < getNumValues() && "Bad value!");
5885
Roman Levensteindc1adac2008-04-07 10:06:32 +00005886 // TODO: Only iterate over uses of a given value of the node
5887 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005888 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005889 if (NUses == 0)
5890 return false;
5891 --NUses;
5892 }
Chris Lattner5c884562005-01-12 18:37:47 +00005893 }
5894
5895 // Found exactly the right number of uses?
5896 return NUses == 0;
5897}
5898
5899
Evan Cheng33d55952007-08-02 05:29:38 +00005900/// hasAnyUseOfValue - Return true if there are any use of the indicated
5901/// value. This method ignores uses of other values defined by this operation.
5902bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5903 assert(Value < getNumValues() && "Bad value!");
5904
Dan Gohman1373c1c2008-07-09 22:39:01 +00005905 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005906 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005907 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005908
5909 return false;
5910}
5911
5912
Dan Gohman2a629952008-07-27 18:06:42 +00005913/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005914///
Dan Gohman2a629952008-07-27 18:06:42 +00005915bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005916 bool Seen = false;
5917 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005918 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005919 if (User == this)
5920 Seen = true;
5921 else
5922 return false;
5923 }
5924
5925 return Seen;
5926}
5927
Evan Chenge6e97e62006-11-03 07:31:32 +00005928/// isOperand - Return true if this node is an operand of N.
5929///
Dan Gohman475871a2008-07-27 21:46:04 +00005930bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005931 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5932 if (*this == N->getOperand(i))
5933 return true;
5934 return false;
5935}
5936
Evan Cheng917be682008-03-04 00:41:45 +00005937bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005938 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005939 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005940 return true;
5941 return false;
5942}
Evan Cheng4ee62112006-02-05 06:29:23 +00005943
Chris Lattner572dee72008-01-16 05:49:24 +00005944/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005945/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005946/// side-effecting instructions on any chain path. In practice, this looks
5947/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005948/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005949bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005950 unsigned Depth) const {
5951 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005952
Chris Lattner572dee72008-01-16 05:49:24 +00005953 // Don't search too deeply, we just want to be able to see through
5954 // TokenFactor's etc.
5955 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005956
Chris Lattner572dee72008-01-16 05:49:24 +00005957 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005958 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005959 if (getOpcode() == ISD::TokenFactor) {
5960 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005961 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5962 return false;
5963 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005964 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005965
Chris Lattner572dee72008-01-16 05:49:24 +00005966 // Loads don't have side effects, look through them.
5967 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5968 if (!Ld->isVolatile())
5969 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5970 }
5971 return false;
5972}
5973
Lang Hames944520f2011-07-07 04:31:51 +00005974/// hasPredecessor - Return true if N is a predecessor of this node.
5975/// N is either an operand of this node, or can be reached by recursively
5976/// traversing up the operands.
5977/// NOTE: This is an expensive method. Use it carefully.
5978bool SDNode::hasPredecessor(const SDNode *N) const {
5979 SmallPtrSet<const SDNode *, 32> Visited;
5980 SmallVector<const SDNode *, 16> Worklist;
5981 return hasPredecessorHelper(N, Visited, Worklist);
5982}
Dan Gohman6688d612009-10-28 03:44:30 +00005983
Lang Hames944520f2011-07-07 04:31:51 +00005984bool SDNode::hasPredecessorHelper(const SDNode *N,
5985 SmallPtrSet<const SDNode *, 32> &Visited,
5986 SmallVector<const SDNode *, 16> &Worklist) const {
5987 if (Visited.empty()) {
5988 Worklist.push_back(this);
5989 } else {
5990 // Take a look in the visited set. If we've already encountered this node
5991 // we needn't search further.
5992 if (Visited.count(N))
5993 return true;
5994 }
5995
5996 // Haven't visited N yet. Continue the search.
5997 while (!Worklist.empty()) {
5998 const SDNode *M = Worklist.pop_back_val();
5999 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6000 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006001 if (Visited.insert(Op))
6002 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006003 if (Op == N)
6004 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006005 }
Lang Hames944520f2011-07-07 04:31:51 +00006006 }
Dan Gohman6688d612009-10-28 03:44:30 +00006007
6008 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006009}
6010
Evan Chengc5484282006-10-04 00:56:09 +00006011uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6012 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006013 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006014}
6015
Mon P Wangcd6e7252009-11-30 02:42:02 +00006016SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6017 assert(N->getNumValues() == 1 &&
6018 "Can't unroll a vector with multiple results!");
6019
6020 EVT VT = N->getValueType(0);
6021 unsigned NE = VT.getVectorNumElements();
6022 EVT EltVT = VT.getVectorElementType();
6023 DebugLoc dl = N->getDebugLoc();
6024
6025 SmallVector<SDValue, 8> Scalars;
6026 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6027
6028 // If ResNE is 0, fully unroll the vector op.
6029 if (ResNE == 0)
6030 ResNE = NE;
6031 else if (NE > ResNE)
6032 NE = ResNE;
6033
6034 unsigned i;
6035 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006036 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006037 SDValue Operand = N->getOperand(j);
6038 EVT OperandVT = Operand.getValueType();
6039 if (OperandVT.isVector()) {
6040 // A vector operand; extract a single element.
6041 EVT OperandEltVT = OperandVT.getVectorElementType();
6042 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6043 OperandEltVT,
6044 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00006045 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006046 } else {
6047 // A scalar operand; just use it as is.
6048 Operands[j] = Operand;
6049 }
6050 }
6051
6052 switch (N->getOpcode()) {
6053 default:
6054 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6055 &Operands[0], Operands.size()));
6056 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006057 case ISD::VSELECT:
6058 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6059 &Operands[0], Operands.size()));
6060 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006061 case ISD::SHL:
6062 case ISD::SRA:
6063 case ISD::SRL:
6064 case ISD::ROTL:
6065 case ISD::ROTR:
6066 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00006067 getShiftAmountOperand(Operands[0].getValueType(),
6068 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006069 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006070 case ISD::SIGN_EXTEND_INREG:
6071 case ISD::FP_ROUND_INREG: {
6072 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6073 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6074 Operands[0],
6075 getValueType(ExtVT)));
6076 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006077 }
6078 }
6079
6080 for (; i < ResNE; ++i)
6081 Scalars.push_back(getUNDEF(EltVT));
6082
6083 return getNode(ISD::BUILD_VECTOR, dl,
6084 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6085 &Scalars[0], Scalars.size());
6086}
6087
Evan Cheng64fa4a92009-12-09 01:36:00 +00006088
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006089/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6090/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006091/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006092bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006093 unsigned Bytes, int Dist) const {
6094 if (LD->getChain() != Base->getChain())
6095 return false;
6096 EVT VT = LD->getValueType(0);
6097 if (VT.getSizeInBits() / 8 != Bytes)
6098 return false;
6099
6100 SDValue Loc = LD->getOperand(1);
6101 SDValue BaseLoc = Base->getOperand(1);
6102 if (Loc.getOpcode() == ISD::FrameIndex) {
6103 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6104 return false;
6105 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6106 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6107 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6108 int FS = MFI->getObjectSize(FI);
6109 int BFS = MFI->getObjectSize(BFI);
6110 if (FS != BFS || FS != (int)Bytes) return false;
6111 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6112 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006113
6114 // Handle X+C
6115 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6116 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6117 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006118
Dan Gohman46510a72010-04-15 01:51:59 +00006119 const GlobalValue *GV1 = NULL;
6120 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006121 int64_t Offset1 = 0;
6122 int64_t Offset2 = 0;
6123 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6124 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6125 if (isGA1 && isGA2 && GV1 == GV2)
6126 return Offset1 == (Offset2 + Dist*Bytes);
6127 return false;
6128}
6129
6130
Evan Chengf2dc5c72009-12-09 01:04:59 +00006131/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6132/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006133unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006134 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006135 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006136 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006137 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006138 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006139 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006140 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Micah Villmow3574eca2012-10-08 16:38:25 +00006141 TLI.getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006142 unsigned AlignBits = KnownZero.countTrailingOnes();
6143 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6144 if (Align)
6145 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006146 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006147
Evan Chengf2dc5c72009-12-09 01:04:59 +00006148 // If this is a direct reference to a stack slot, use information about the
6149 // stack slot's alignment.
6150 int FrameIdx = 1 << 31;
6151 int64_t FrameOffset = 0;
6152 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6153 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006154 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006155 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006156 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006157 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6158 FrameOffset = Ptr.getConstantOperandVal(1);
6159 }
6160
6161 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006162 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006163 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6164 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006165 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006166 }
6167
6168 return 0;
6169}
6170
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006171// getAddressSpace - Return the address space this GlobalAddress belongs to.
6172unsigned GlobalAddressSDNode::getAddressSpace() const {
6173 return getGlobal()->getType()->getAddressSpace();
6174}
6175
6176
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006177Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006178 if (isMachineConstantPoolEntry())
6179 return Val.MachineCPVal->getType();
6180 return Val.ConstVal->getType();
6181}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006182
Bob Wilson24e338e2009-03-02 23:24:16 +00006183bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6184 APInt &SplatUndef,
6185 unsigned &SplatBitSize,
6186 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006187 unsigned MinSplatBits,
6188 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006189 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006190 assert(VT.isVector() && "Expected a vector type");
6191 unsigned sz = VT.getSizeInBits();
6192 if (MinSplatBits > sz)
6193 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006194
Bob Wilson24e338e2009-03-02 23:24:16 +00006195 SplatValue = APInt(sz, 0);
6196 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006197
Bob Wilson24e338e2009-03-02 23:24:16 +00006198 // Get the bits. Bits with undefined values (when the corresponding element
6199 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6200 // in SplatValue. If any of the values are not constant, give up and return
6201 // false.
6202 unsigned int nOps = getNumOperands();
6203 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6204 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006205
6206 for (unsigned j = 0; j < nOps; ++j) {
6207 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006208 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006209 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006210
Bob Wilson24e338e2009-03-02 23:24:16 +00006211 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006212 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006213 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006214 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006215 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006216 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006217 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006218 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006219 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006220 }
6221
Bob Wilson24e338e2009-03-02 23:24:16 +00006222 // The build_vector is all constants or undefs. Find the smallest element
6223 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006224
Bob Wilson24e338e2009-03-02 23:24:16 +00006225 HasAnyUndefs = (SplatUndef != 0);
6226 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006227
Bob Wilson24e338e2009-03-02 23:24:16 +00006228 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006229 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6230 APInt LowValue = SplatValue.trunc(HalfSize);
6231 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6232 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006233
Bob Wilson24e338e2009-03-02 23:24:16 +00006234 // If the two halves do not match (ignoring undef bits), stop here.
6235 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6236 MinSplatBits > HalfSize)
6237 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006238
Bob Wilson24e338e2009-03-02 23:24:16 +00006239 SplatValue = HighValue | LowValue;
6240 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006241
Bob Wilson24e338e2009-03-02 23:24:16 +00006242 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006243 }
6244
Bob Wilson24e338e2009-03-02 23:24:16 +00006245 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006246 return true;
6247}
Nate Begeman9008ca62009-04-27 18:41:29 +00006248
Owen Andersone50ed302009-08-10 22:56:29 +00006249bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006250 // Find the first non-undef value in the shuffle mask.
6251 unsigned i, e;
6252 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6253 /* search */;
6254
Nate Begemana6415752009-04-29 18:13:31 +00006255 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006256
Nate Begeman5a5ca152009-04-29 05:20:52 +00006257 // Make sure all remaining elements are either undef or the same as the first
6258 // non-undef value.
6259 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006260 if (Mask[i] >= 0 && Mask[i] != Idx)
6261 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006262 return true;
6263}
David Greenecf495bc2010-01-20 20:13:31 +00006264
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006265#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006266static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006267 SmallPtrSet<const SDNode*, 32> &Visited,
6268 SmallPtrSet<const SDNode*, 32> &Checked) {
6269 // If this node has already been checked, don't check it again.
6270 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006271 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006272
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006273 // If a node has already been visited on this depth-first walk, reject it as
6274 // a cycle.
6275 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006276 dbgs() << "Offending node:\n";
6277 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006278 errs() << "Detected cycle in SelectionDAG\n";
6279 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006280 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006281
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006282 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6283 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006284
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006285 Checked.insert(N);
6286 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006287}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006288#endif
David Greenecf495bc2010-01-20 20:13:31 +00006289
6290void llvm::checkForCycles(const llvm::SDNode *N) {
6291#ifdef XDEBUG
6292 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006293 SmallPtrSet<const SDNode*, 32> visited;
6294 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006295 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006296#endif
6297}
6298
6299void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6300 checkForCycles(DAG->getRoot().getNode());
6301}