blob: 4cfb212eaf97ef2c202aca4805d0a0baac5727b4 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineBasicBlock.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineModuleInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000028#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/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"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetIntrinsicInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetOptions.h"
49#include "llvm/Target/TargetRegisterInfo.h"
50#include "llvm/Target/TargetSelectionDAGInfo.h"
Chandler Carruth58a2cbe2013-01-02 10:22:59 +000051#include "llvm/TargetTransformInfo.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()),
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000888 TTI(0), OptLevel(OL), EntryNode(ISD::EntryToken, DebugLoc(),
889 getVTList(MVT::Other)),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000890 Root(getEntryNode()), Ordering(0), UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000891 AllNodes.push_back(&EntryNode);
Bill Wendling187361b2010-01-23 10:26:57 +0000892 Ordering = new SDNodeOrdering();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000893 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000894}
895
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000896void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000897 MF = &mf;
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000898 TTI = tti;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000899 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000900}
901
Chris Lattner78ec3112003-08-11 14:57:33 +0000902SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000903 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000904 allnodes_clear();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000905 delete Ordering;
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000906 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000907}
908
909void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000910 assert(&*AllNodes.begin() == &EntryNode);
911 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000912 while (!AllNodes.empty())
913 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000914}
915
Dan Gohman7c3234c2008-08-27 23:52:12 +0000916void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000917 allnodes_clear();
918 OperandAllocator.Reset();
919 CSEMap.clear();
920
921 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000922 ExternalSymbols.clear();
923 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000924 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
925 static_cast<CondCodeSDNode*>(0));
926 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
927 static_cast<SDNode*>(0));
928
Dan Gohmane7852d02009-01-26 04:35:06 +0000929 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000930 AllNodes.push_back(&EntryNode);
931 Root = getEntryNode();
Dan Gohman27445f02010-06-18 15:40:58 +0000932 Ordering->clear();
Evan Cheng31441b72010-03-29 20:48:30 +0000933 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000934}
935
Nadav Rotema3c42f32011-09-27 11:16:47 +0000936SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
937 return VT.bitsGT(Op.getValueType()) ?
938 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
939 getNode(ISD::TRUNCATE, DL, VT, Op);
940}
941
Duncan Sands3a66a682009-10-13 21:04:12 +0000942SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
943 return VT.bitsGT(Op.getValueType()) ?
944 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
945 getNode(ISD::TRUNCATE, DL, VT, Op);
946}
947
948SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
949 return VT.bitsGT(Op.getValueType()) ?
950 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
951 getNode(ISD::TRUNCATE, DL, VT, Op);
952}
953
Owen Andersone50ed302009-08-10 22:56:29 +0000954SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000955 assert(!VT.isVector() &&
956 "getZeroExtendInReg should use the vector element type instead of "
957 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000958 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000959 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
960 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000961 VT.getSizeInBits());
962 return getNode(ISD::AND, DL, Op.getValueType(), Op,
963 getConstant(Imm, Op.getValueType()));
964}
965
Bob Wilson4c245462009-01-22 17:39:32 +0000966/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
967///
Owen Andersone50ed302009-08-10 22:56:29 +0000968SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000969 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000970 SDValue NegOne =
971 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000972 return getNode(ISD::XOR, DL, VT, Val, NegOne);
973}
974
Owen Andersone50ed302009-08-10 22:56:29 +0000975SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000976 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000977 assert((EltVT.getSizeInBits() >= 64 ||
978 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
979 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000980 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000981}
982
Owen Andersone50ed302009-08-10 22:56:29 +0000983SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000984 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000985}
986
Owen Andersone50ed302009-08-10 22:56:29 +0000987SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000988 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000989
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000990 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000991 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000992
Duncan Sands28b77e92011-09-06 19:07:46 +0000993 // In some cases the vector type is legal but the element type is illegal and
994 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
995 // inserted value (the type does not need to match the vector element type).
996 // Any extra bits introduced will be truncated away.
997 if (VT.isVector() && TLI.getTypeAction(*getContext(), EltVT) ==
998 TargetLowering::TypePromoteInteger) {
999 EltVT = TLI.getTypeToTransformTo(*getContext(), EltVT);
1000 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
1001 Elt = ConstantInt::get(*getContext(), NewVal);
1002 }
1003
1004 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1005 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001006 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001007 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001008 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +00001009 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +00001010 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001011 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001012 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001013 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001014 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001015
Dan Gohman89081322007-12-12 22:21:26 +00001016 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001017 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001018 CSEMap.InsertNode(N, IP);
1019 AllNodes.push_back(N);
1020 }
1021
Dan Gohman475871a2008-07-27 21:46:04 +00001022 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001023 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001024 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001025 Ops.assign(VT.getVectorNumElements(), Result);
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001026 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001027 }
1028 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001029}
1030
Dan Gohman475871a2008-07-27 21:46:04 +00001031SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +00001032 return getConstant(Val, TLI.getPointerTy(), isTarget);
1033}
1034
1035
Owen Andersone50ed302009-08-10 22:56:29 +00001036SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001037 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001038}
1039
Owen Andersone50ed302009-08-10 22:56:29 +00001040SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001041 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001042
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001043 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044
Chris Lattnerd8658612005-02-17 20:17:32 +00001045 // Do the map lookup using the actual bit pattern for the floating point
1046 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1047 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001048 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001049 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001050 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001051 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001052 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001053 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001054 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001055 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001056 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001057
Evan Chengc908dcd2007-06-29 21:36:04 +00001058 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001059 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001060 CSEMap.InsertNode(N, IP);
1061 AllNodes.push_back(N);
1062 }
1063
Dan Gohman475871a2008-07-27 21:46:04 +00001064 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001065 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001066 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001067 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +00001068 // FIXME DebugLoc info might be appropriate here
Chris Lattnera4f2bb02010-04-02 20:17:23 +00001069 Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001070 }
1071 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001072}
1073
Owen Andersone50ed302009-08-10 22:56:29 +00001074SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001075 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001076 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001077 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001078 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001079 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001080 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1081 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001082 bool ignored;
1083 APFloat apf = APFloat(Val);
1084 apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
1085 &ignored);
1086 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001087 } else
1088 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001089}
1090
Devang Patel0d881da2010-07-06 22:08:15 +00001091SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001092 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001093 bool isTargetGA,
1094 unsigned char TargetFlags) {
1095 assert((TargetFlags == 0 || isTargetGA) &&
1096 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001097
Dan Gohman6520e202008-10-18 02:06:02 +00001098 // Truncate (with sign-extension) the offset value to the pointer size.
Richard Smith1144af32012-08-24 23:29:28 +00001099 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +00001100 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001101 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001102
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001103 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1104 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001105 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001106 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001107 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001108 }
1109
Chris Lattner2a4ed822009-06-25 21:21:14 +00001110 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001111 if (GVar && GVar->isThreadLocal())
1112 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1113 else
1114 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001115
Jim Laskey583bd472006-10-27 23:46:08 +00001116 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001117 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001118 ID.AddPointer(GV);
1119 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001120 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001121 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001122 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001123 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001124 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001125
Devang Patel0d881da2010-07-06 22:08:15 +00001126 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001127 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001128 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001129 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001130 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131}
1132
Owen Andersone50ed302009-08-10 22:56:29 +00001133SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001134 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001135 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001136 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001137 ID.AddInteger(FI);
1138 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001139 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001140 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001141
Dan Gohman95531882010-03-18 18:49:47 +00001142 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001143 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001144 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001145 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001146}
1147
Owen Andersone50ed302009-08-10 22:56:29 +00001148SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001149 unsigned char TargetFlags) {
1150 assert((TargetFlags == 0 || isTarget) &&
1151 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001152 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001153 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001154 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001155 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001156 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001157 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001158 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001159 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001160
Dan Gohman95531882010-03-18 18:49:47 +00001161 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1162 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001163 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001164 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001165 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001166}
1167
Dan Gohman46510a72010-04-15 01:51:59 +00001168SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001169 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001170 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001171 unsigned char TargetFlags) {
1172 assert((TargetFlags == 0 || isTarget) &&
1173 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001174 if (Alignment == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00001175 Alignment = TLI.getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001176 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001177 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001178 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001179 ID.AddInteger(Alignment);
1180 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001181 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001182 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001183 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001184 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001185 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001186
Dan Gohman95531882010-03-18 18:49:47 +00001187 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1188 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001189 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001190 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001191 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001192}
1193
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194
Owen Andersone50ed302009-08-10 22:56:29 +00001195SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001196 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001197 bool isTarget,
1198 unsigned char TargetFlags) {
1199 assert((TargetFlags == 0 || isTarget) &&
1200 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001201 if (Alignment == 0)
Micah Villmow3574eca2012-10-08 16:38:25 +00001202 Alignment = TLI.getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001203 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001204 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001205 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001206 ID.AddInteger(Alignment);
1207 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001208 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001209 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001210 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001211 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001212 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001213
Dan Gohman95531882010-03-18 18:49:47 +00001214 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1215 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001216 CSEMap.InsertNode(N, IP);
1217 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001218 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001219}
1220
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001221SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1222 unsigned char TargetFlags) {
1223 FoldingSetNodeID ID;
1224 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1225 ID.AddInteger(Index);
1226 ID.AddInteger(Offset);
1227 ID.AddInteger(TargetFlags);
1228 void *IP = 0;
1229 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1230 return SDValue(E, 0);
1231
1232 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1233 TargetFlags);
1234 CSEMap.InsertNode(N, IP);
1235 AllNodes.push_back(N);
1236 return SDValue(N, 0);
1237}
1238
Dan Gohman475871a2008-07-27 21:46:04 +00001239SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001240 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001242 ID.AddPointer(MBB);
1243 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001244 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001245 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001246
Dan Gohman95531882010-03-18 18:49:47 +00001247 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001248 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001250 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001251}
1252
Owen Andersone50ed302009-08-10 22:56:29 +00001253SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1255 ValueTypeNodes.size())
1256 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001257
Duncan Sands83ec4b62008-06-06 12:08:01 +00001258 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001259 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001260
Dan Gohman475871a2008-07-27 21:46:04 +00001261 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001262 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001263 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001264 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001265}
1266
Owen Andersone50ed302009-08-10 22:56:29 +00001267SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001268 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001269 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001270 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001271 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001272 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001273}
1274
Owen Andersone50ed302009-08-10 22:56:29 +00001275SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001276 unsigned char TargetFlags) {
1277 SDNode *&N =
1278 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1279 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001280 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001281 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001282 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001283 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284}
1285
Dan Gohman475871a2008-07-27 21:46:04 +00001286SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001287 if ((unsigned)Cond >= CondCodeNodes.size())
1288 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001289
Chris Lattner079a27a2005-08-09 20:40:02 +00001290 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001291 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001292 CondCodeNodes[Cond] = N;
1293 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001294 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001295
Dan Gohman475871a2008-07-27 21:46:04 +00001296 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001297}
1298
Nate Begeman5a5ca152009-04-29 05:20:52 +00001299// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1300// the shuffle mask M that point at N1 to point at N2, and indices that point
1301// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001302static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1303 std::swap(N1, N2);
1304 int NElts = M.size();
1305 for (int i = 0; i != NElts; ++i) {
1306 if (M[i] >= NElts)
1307 M[i] -= NElts;
1308 else if (M[i] >= 0)
1309 M[i] += NElts;
1310 }
1311}
1312
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001313SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001314 SDValue N2, const int *Mask) {
1315 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001316 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001317 "Vector Shuffle VTs must be a vectors");
1318 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1319 && "Vector Shuffle VTs must have same element type");
1320
1321 // Canonicalize shuffle undef, undef -> undef
1322 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001323 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001324
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001325 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001326 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001327 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001328 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001329 for (unsigned i = 0; i != NElts; ++i) {
1330 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001331 MaskVec.push_back(Mask[i]);
1332 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001333
Nate Begeman9008ca62009-04-27 18:41:29 +00001334 // Canonicalize shuffle v, v -> v, undef
1335 if (N1 == N2) {
1336 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001337 for (unsigned i = 0; i != NElts; ++i)
1338 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001339 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001340
Nate Begeman9008ca62009-04-27 18:41:29 +00001341 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1342 if (N1.getOpcode() == ISD::UNDEF)
1343 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001344
Nate Begeman9008ca62009-04-27 18:41:29 +00001345 // Canonicalize all index into lhs, -> shuffle lhs, undef
1346 // Canonicalize all index into rhs, -> shuffle rhs, undef
1347 bool AllLHS = true, AllRHS = true;
1348 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001349 for (unsigned i = 0; i != NElts; ++i) {
1350 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001351 if (N2Undef)
1352 MaskVec[i] = -1;
1353 else
1354 AllLHS = false;
1355 } else if (MaskVec[i] >= 0) {
1356 AllRHS = false;
1357 }
1358 }
1359 if (AllLHS && AllRHS)
1360 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001361 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001362 N2 = getUNDEF(VT);
1363 if (AllRHS) {
1364 N1 = getUNDEF(VT);
1365 commuteShuffle(N1, N2, MaskVec);
1366 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001367
Nate Begeman9008ca62009-04-27 18:41:29 +00001368 // If Identity shuffle, or all shuffle in to undef, return that node.
1369 bool AllUndef = true;
1370 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001371 for (unsigned i = 0; i != NElts; ++i) {
1372 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001373 if (MaskVec[i] >= 0) AllUndef = false;
1374 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001375 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001376 return N1;
1377 if (AllUndef)
1378 return getUNDEF(VT);
1379
1380 FoldingSetNodeID ID;
1381 SDValue Ops[2] = { N1, N2 };
1382 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001383 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001384 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001385
Nate Begeman9008ca62009-04-27 18:41:29 +00001386 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001387 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001388 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001389
Nate Begeman9008ca62009-04-27 18:41:29 +00001390 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1391 // SDNode doesn't have access to it. This memory will be "leaked" when
1392 // the node is deallocated, but recovered when the NodeAllocator is released.
1393 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1394 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001395
Dan Gohman95531882010-03-18 18:49:47 +00001396 ShuffleVectorSDNode *N =
1397 new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001398 CSEMap.InsertNode(N, IP);
1399 AllNodes.push_back(N);
1400 return SDValue(N, 0);
1401}
1402
Owen Andersone50ed302009-08-10 22:56:29 +00001403SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001404 SDValue Val, SDValue DTy,
1405 SDValue STy, SDValue Rnd, SDValue Sat,
1406 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001407 // If the src and dest types are the same and the conversion is between
1408 // integer types of the same sign or two floats, no conversion is necessary.
1409 if (DTy == STy &&
1410 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001411 return Val;
1412
1413 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001414 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1415 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001416 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001417 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001418 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001419
Dan Gohman95531882010-03-18 18:49:47 +00001420 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
1421 Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001422 CSEMap.InsertNode(N, IP);
1423 AllNodes.push_back(N);
1424 return SDValue(N, 0);
1425}
1426
Owen Andersone50ed302009-08-10 22:56:29 +00001427SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001428 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001429 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001430 ID.AddInteger(RegNo);
1431 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001432 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001433 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001434
Dan Gohman95531882010-03-18 18:49:47 +00001435 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001436 CSEMap.InsertNode(N, IP);
1437 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001438 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001439}
1440
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001441SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1442 FoldingSetNodeID ID;
1443 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1444 ID.AddPointer(RegMask);
1445 void *IP = 0;
1446 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1447 return SDValue(E, 0);
1448
1449 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1450 CSEMap.InsertNode(N, IP);
1451 AllNodes.push_back(N);
1452 return SDValue(N, 0);
1453}
1454
Chris Lattner7561d482010-03-14 02:33:54 +00001455SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001456 FoldingSetNodeID ID;
1457 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001458 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1459 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001460 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001461 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001462 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001463
Dan Gohman95531882010-03-18 18:49:47 +00001464 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001465 CSEMap.InsertNode(N, IP);
1466 AllNodes.push_back(N);
1467 return SDValue(N, 0);
1468}
1469
Chris Lattner7561d482010-03-14 02:33:54 +00001470
Dan Gohman46510a72010-04-15 01:51:59 +00001471SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001472 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001473 bool isTarget,
1474 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001475 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1476
1477 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001478 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001479 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001480 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001481 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001482 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001483 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001484 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001485
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001486 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1487 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001488 CSEMap.InsertNode(N, IP);
1489 AllNodes.push_back(N);
1490 return SDValue(N, 0);
1491}
1492
Dan Gohman475871a2008-07-27 21:46:04 +00001493SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001494 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001495 "SrcValue is not a pointer?");
1496
Jim Laskey583bd472006-10-27 23:46:08 +00001497 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001499 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001500
Chris Lattner61b09412006-08-11 21:01:22 +00001501 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001502 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001503 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001504
Dan Gohman95531882010-03-18 18:49:47 +00001505 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001506 CSEMap.InsertNode(N, IP);
1507 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001508 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001509}
1510
Chris Lattnerdecc2672010-04-07 05:20:54 +00001511/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1512SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1513 FoldingSetNodeID ID;
1514 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1515 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001516
Chris Lattnerdecc2672010-04-07 05:20:54 +00001517 void *IP = 0;
1518 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1519 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001520
Chris Lattnerdecc2672010-04-07 05:20:54 +00001521 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1522 CSEMap.InsertNode(N, IP);
1523 AllNodes.push_back(N);
1524 return SDValue(N, 0);
1525}
1526
1527
Duncan Sands92abc622009-01-31 15:50:11 +00001528/// getShiftAmountOperand - Return the specified value casted to
1529/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001530SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001531 EVT OpTy = Op.getValueType();
Owen Anderson6154f6c2011-03-07 18:29:47 +00001532 MVT ShTy = TLI.getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001533 if (OpTy == ShTy || OpTy.isVector()) return Op;
1534
1535 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001536 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001537}
1538
Chris Lattner37ce9df2007-10-15 17:47:20 +00001539/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1540/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001541SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001542 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001543 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001544 Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001545 unsigned StackAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00001546 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001547
David Greene3f2bf852009-11-12 20:49:22 +00001548 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001549 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1550}
1551
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001552/// CreateStackTemporary - Create a stack temporary suitable for holding
1553/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001554SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001555 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1556 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001557 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1558 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00001559 const DataLayout *TD = TLI.getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001560 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1561 TD->getPrefTypeAlignment(Ty2));
1562
1563 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001564 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001565 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1566}
1567
Owen Andersone50ed302009-08-10 22:56:29 +00001568SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001569 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001570 // These setcc operations always fold.
1571 switch (Cond) {
1572 default: break;
1573 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001574 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001575 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001576 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001577
Chris Lattnera83385f2006-04-27 05:01:07 +00001578 case ISD::SETOEQ:
1579 case ISD::SETOGT:
1580 case ISD::SETOGE:
1581 case ISD::SETOLT:
1582 case ISD::SETOLE:
1583 case ISD::SETONE:
1584 case ISD::SETO:
1585 case ISD::SETUO:
1586 case ISD::SETUEQ:
1587 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001588 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001589 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001590 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Gabor Greifba36cb52008-08-28 21:40:38 +00001592 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001593 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001594 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001595 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Chris Lattnerc3aae252005-01-07 07:46:32 +00001597 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001598 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001599 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1600 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001601 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1602 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1603 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1604 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1605 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1606 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1607 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1608 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001609 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001610 }
Chris Lattner67255a12005-04-07 18:14:58 +00001611 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001612 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1613 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001614 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001615 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001616 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001617 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001618 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001619 // fall through
1620 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001621 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001622 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001623 // fall through
1624 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001625 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001626 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001627 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001628 // fall through
1629 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001630 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001631 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001632 // fall through
1633 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001634 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001635 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001636 // fall through
1637 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001638 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001639 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001640 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001641 // fall through
1642 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001643 R==APFloat::cmpEqual, VT);
1644 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1645 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1646 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1647 R==APFloat::cmpEqual, VT);
1648 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1649 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1650 R==APFloat::cmpLessThan, VT);
1651 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1652 R==APFloat::cmpUnordered, VT);
1653 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1654 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001655 }
1656 } else {
1657 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001658 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001659 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001660 }
1661
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001662 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001663 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001664}
1665
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001666/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1667/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001668bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001669 // This predicate is not safe for vector operations.
1670 if (Op.getValueType().isVector())
1671 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001672
Dan Gohman87862e72009-12-11 21:31:27 +00001673 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001674 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1675}
1676
Dan Gohmanea859be2007-06-22 14:59:07 +00001677/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1678/// this predicate to simplify operations downstream. Mask is known to be zero
1679/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001680bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001681 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001682 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001683 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001684 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001685 return (KnownZero & Mask) == Mask;
1686}
1687
1688/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1689/// known to be either zero or one and return them in the KnownZero/KnownOne
1690/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1691/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001692void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1693 APInt &KnownOne, unsigned Depth) const {
1694 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001695
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001696 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001697 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001698 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001699
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001700 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001701
1702 switch (Op.getOpcode()) {
1703 case ISD::Constant:
1704 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001705 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1706 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001707 return;
1708 case ISD::AND:
1709 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001710 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1711 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1713 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001714
1715 // Output known-1 bits are only known if set in both the LHS & RHS.
1716 KnownOne &= KnownOne2;
1717 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1718 KnownZero |= KnownZero2;
1719 return;
1720 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001721 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1722 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001723 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1724 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1725
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 // Output known-0 bits are only known if clear in both the LHS & RHS.
1727 KnownZero &= KnownZero2;
1728 // Output known-1 are known to be set if set in either the LHS | RHS.
1729 KnownOne |= KnownOne2;
1730 return;
1731 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001732 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1733 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001734 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1735 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1736
Dan Gohmanea859be2007-06-22 14:59:07 +00001737 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001738 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001739 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1740 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1741 KnownZero = KnownZeroOut;
1742 return;
1743 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001744 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001745 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1746 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001747 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1748 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1749
1750 // If low bits are zero in either operand, output low known-0 bits.
1751 // Also compute a conserative estimate for high known-0 bits.
1752 // More trickiness is possible, but this is sufficient for the
1753 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001754 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001755 unsigned TrailZ = KnownZero.countTrailingOnes() +
1756 KnownZero2.countTrailingOnes();
1757 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001758 KnownZero2.countLeadingOnes(),
1759 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001760
1761 TrailZ = std::min(TrailZ, BitWidth);
1762 LeadZ = std::min(LeadZ, BitWidth);
1763 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1764 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001765 return;
1766 }
1767 case ISD::UDIV: {
1768 // For the purposes of computing leading zeros we can conservatively
1769 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001770 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001771 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001772 unsigned LeadZ = KnownZero2.countLeadingOnes();
1773
Jay Foad7a874dd2010-12-01 08:53:58 +00001774 KnownOne2.clearAllBits();
1775 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001776 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001777 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1778 if (RHSUnknownLeadingOnes != BitWidth)
1779 LeadZ = std::min(BitWidth,
1780 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001781
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001782 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001783 return;
1784 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001785 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001786 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1787 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001788 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1789 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1790
Dan Gohmanea859be2007-06-22 14:59:07 +00001791 // Only known if known in both the LHS and RHS.
1792 KnownOne &= KnownOne2;
1793 KnownZero &= KnownZero2;
1794 return;
1795 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001796 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1797 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001798 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1799 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1800
Dan Gohmanea859be2007-06-22 14:59:07 +00001801 // Only known if known in both the LHS and RHS.
1802 KnownOne &= KnownOne2;
1803 KnownZero &= KnownZero2;
1804 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001805 case ISD::SADDO:
1806 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001807 case ISD::SSUBO:
1808 case ISD::USUBO:
1809 case ISD::SMULO:
1810 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001811 if (Op.getResNo() != 1)
1812 return;
Duncan Sands03228082008-11-23 15:47:28 +00001813 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001814 case ISD::SETCC:
1815 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands28b77e92011-09-06 19:07:46 +00001816 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
1817 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001818 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001819 return;
1820 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001821 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001822 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001823 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001824
1825 // If the shift count is an invalid immediate, don't do anything.
1826 if (ShAmt >= BitWidth)
1827 return;
1828
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001829 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001830 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001831 KnownZero <<= ShAmt;
1832 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001833 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001834 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001835 }
1836 return;
1837 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001838 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001839 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001840 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001841
Dan Gohmand4cf9922008-02-26 18:50:50 +00001842 // If the shift count is an invalid immediate, don't do anything.
1843 if (ShAmt >= BitWidth)
1844 return;
1845
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001846 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001847 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001848 KnownZero = KnownZero.lshr(ShAmt);
1849 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001850
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001851 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001852 KnownZero |= HighBits; // High bits known zero.
1853 }
1854 return;
1855 case ISD::SRA:
1856 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001857 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001858
Dan Gohmand4cf9922008-02-26 18:50:50 +00001859 // If the shift count is an invalid immediate, don't do anything.
1860 if (ShAmt >= BitWidth)
1861 return;
1862
Dan Gohmanea859be2007-06-22 14:59:07 +00001863 // If any of the demanded bits are produced by the sign extension, we also
1864 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001865 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001866
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001867 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001868 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001869 KnownZero = KnownZero.lshr(ShAmt);
1870 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001871
Dan Gohmanea859be2007-06-22 14:59:07 +00001872 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001873 APInt SignBit = APInt::getSignBit(BitWidth);
1874 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
Dan Gohmanca93a432008-02-20 16:30:17 +00001876 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001877 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001878 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001879 KnownOne |= HighBits; // New bits are known one.
1880 }
1881 }
1882 return;
1883 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001884 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001885 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001886
1887 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001888 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001889 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001890
Dan Gohman977a76f2008-02-13 22:28:48 +00001891 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001892 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001893
Dan Gohmanea859be2007-06-22 14:59:07 +00001894 // If the sign extended bits are demanded, we know that the sign
1895 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001896 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001897 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001898 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001900 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1901 KnownOne &= InputDemandedBits;
1902 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001903 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1904
Dan Gohmanea859be2007-06-22 14:59:07 +00001905 // If the sign bit of the input is known set or clear, then we know the
1906 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001907 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001908 KnownZero |= NewBits;
1909 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001910 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001911 KnownOne |= NewBits;
1912 KnownZero &= ~NewBits;
1913 } else { // Input sign bit unknown
1914 KnownZero &= ~NewBits;
1915 KnownOne &= ~NewBits;
1916 }
1917 return;
1918 }
1919 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001920 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001921 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001922 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001923 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001924 unsigned LowBits = Log2_32(BitWidth)+1;
1925 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001926 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001927 return;
1928 }
1929 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001930 LoadSDNode *LD = cast<LoadSDNode>(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00001931 if (ISD::isZEXTLoad(Op.getNode())) {
Owen Andersone50ed302009-08-10 22:56:29 +00001932 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001933 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001934 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00001935 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001936 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00001937 }
1938 return;
1939 }
1940 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001941 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001942 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001943 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00001944 KnownZero = KnownZero.trunc(InBits);
1945 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001946 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00001947 KnownZero = KnownZero.zext(BitWidth);
1948 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001949 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001950 return;
1951 }
1952 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001953 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001954 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001955 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001956 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001957
Jay Foad40f8f622010-12-07 08:25:19 +00001958 KnownZero = KnownZero.trunc(InBits);
1959 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001960 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001961
1962 // Note if the sign bit is known to be zero or one.
1963 bool SignBitKnownZero = KnownZero.isNegative();
1964 bool SignBitKnownOne = KnownOne.isNegative();
1965 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1966 "Sign bit can't be known to be both zero and one!");
1967
Jay Foad40f8f622010-12-07 08:25:19 +00001968 KnownZero = KnownZero.zext(BitWidth);
1969 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001970
Dan Gohman977a76f2008-02-13 22:28:48 +00001971 // If the sign bit is known zero or one, the top bits match.
1972 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001973 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001974 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001975 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001976 return;
1977 }
1978 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00001979 EVT InVT = Op.getOperand(0).getValueType();
1980 unsigned InBits = InVT.getScalarType().getSizeInBits();
1981 KnownZero = KnownZero.trunc(InBits);
1982 KnownOne = KnownOne.trunc(InBits);
1983 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1984 KnownZero = KnownZero.zext(BitWidth);
1985 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001986 return;
1987 }
1988 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001989 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00001990 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00001991 KnownZero = KnownZero.zext(InBits);
1992 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001993 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001994 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00001995 KnownZero = KnownZero.trunc(BitWidth);
1996 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001997 break;
1998 }
1999 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002000 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002002 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2003 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002004 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002005 return;
2006 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002007 case ISD::FGETSIGN:
2008 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002009 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002010 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Dan Gohman23e8b712008-04-28 17:02:21 +00002012 case ISD::SUB: {
2013 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2014 // We know that the top bits of C-X are clear if X contains less bits
2015 // than C (i.e. no wrap-around can happen). For example, 20-X is
2016 // positive if we can prove that X is >= 0 and < 16.
2017 if (CLHS->getAPIntValue().isNonNegative()) {
2018 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2019 // NLZ can't be BitWidth with no sign bit
2020 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002021 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002022
2023 // If all of the MaskV bits are known to be zero, then we know the
2024 // output top bits are zero, because we now know that the output is
2025 // from [0-C].
2026 if ((KnownZero2 & MaskV) == MaskV) {
2027 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2028 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002029 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002030 }
2031 }
2032 }
2033 }
2034 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002035 case ISD::ADD:
2036 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002037 // Output known-0 bits are known if clear or set in both the low clear bits
2038 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2039 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002040 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002041 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002042 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2043
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002044 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002045 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002046 KnownZeroOut = std::min(KnownZeroOut,
2047 KnownZero2.countTrailingOnes());
2048
Chris Lattnerda605882010-12-19 20:38:28 +00002049 if (Op.getOpcode() == ISD::ADD) {
2050 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2051 return;
2052 }
2053
2054 // With ADDE, a carry bit may be added in, so we can only use this
2055 // information if we know (at least) that the low two bits are clear. We
2056 // then return to the caller that the low bit is unknown but that other bits
2057 // are known zero.
2058 if (KnownZeroOut >= 2) // ADDE
2059 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002060 return;
2061 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002062 case ISD::SREM:
2063 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002064 const APInt &RA = Rem->getAPIntValue().abs();
2065 if (RA.isPowerOf2()) {
2066 APInt LowBits = RA - 1;
Dan Gohman23e8b712008-04-28 17:02:21 +00002067 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002068 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002069
Duncan Sands5c2873a2010-01-29 09:45:26 +00002070 // The low bits of the first operand are unchanged by the srem.
2071 KnownZero = KnownZero2 & LowBits;
2072 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002073
Duncan Sands5c2873a2010-01-29 09:45:26 +00002074 // If the first operand is non-negative or has all low bits zero, then
2075 // the upper bits are all zero.
2076 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2077 KnownZero |= ~LowBits;
2078
2079 // If the first operand is negative and not all low bits are zero, then
2080 // the upper bits are all one.
2081 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2082 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002083 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002084 }
2085 }
2086 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002087 case ISD::UREM: {
2088 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002089 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002090 if (RA.isPowerOf2()) {
2091 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002092 KnownZero |= ~LowBits;
2093 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002094 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2095 break;
2096 }
2097 }
2098
2099 // Since the result is less than or equal to either operand, any leading
2100 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002101 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2102 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002103
2104 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2105 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002106 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002107 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002108 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002109 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002110 case ISD::FrameIndex:
2111 case ISD::TargetFrameIndex:
2112 if (unsigned Align = InferPtrAlignment(Op)) {
2113 // The low bits are known zero if the pointer is aligned.
2114 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2115 return;
2116 }
2117 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002118
Dan Gohmanea859be2007-06-22 14:59:07 +00002119 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002120 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2121 break;
2122 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002123 case ISD::INTRINSIC_WO_CHAIN:
2124 case ISD::INTRINSIC_W_CHAIN:
2125 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002126 // Allow the target to implement this method for its nodes.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002127 TLI.computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002128 return;
2129 }
2130}
2131
2132/// ComputeNumSignBits - Return the number of times the sign bit of the
2133/// register is replicated into the other bits. We know that at least 1 bit
2134/// is always equal to the sign bit (itself), but other cases can give us
2135/// information. For example, immediately after an "SRA X, 2", we know that
2136/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002137unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00002138 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002139 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002140 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002142 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 if (Depth == 6)
2145 return 1; // Limit search depth.
2146
2147 switch (Op.getOpcode()) {
2148 default: break;
2149 case ISD::AssertSext:
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+1;
2152 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002153 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002154 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002155
Dan Gohmanea859be2007-06-22 14:59:07 +00002156 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002157 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002158 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002159 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Dan Gohmanea859be2007-06-22 14:59:07 +00002161 case ISD::SIGN_EXTEND:
Dan Gohman87862e72009-12-11 21:31:27 +00002162 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002163 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002164
Dan Gohmanea859be2007-06-22 14:59:07 +00002165 case ISD::SIGN_EXTEND_INREG:
2166 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002167 Tmp =
2168 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002169 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002170
Dan Gohmanea859be2007-06-22 14:59:07 +00002171 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2172 return std::max(Tmp, Tmp2);
2173
2174 case ISD::SRA:
2175 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2176 // SRA X, C -> adds C sign bits.
2177 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002178 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002179 if (Tmp > VTBits) Tmp = VTBits;
2180 }
2181 return Tmp;
2182 case ISD::SHL:
2183 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2184 // shl destroys sign bits.
2185 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002186 if (C->getZExtValue() >= VTBits || // Bad shift.
2187 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2188 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002189 }
2190 break;
2191 case ISD::AND:
2192 case ISD::OR:
2193 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002194 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002195 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002196 if (Tmp != 1) {
2197 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2198 FirstAnswer = std::min(Tmp, Tmp2);
2199 // We computed what we know about the sign bits as our first
2200 // answer. Now proceed to the generic code that uses
2201 // ComputeMaskedBits, and pick whichever answer is better.
2202 }
2203 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002204
2205 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002206 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002207 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002208 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002209 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002210
2211 case ISD::SADDO:
2212 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002213 case ISD::SSUBO:
2214 case ISD::USUBO:
2215 case ISD::SMULO:
2216 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002217 if (Op.getResNo() != 1)
2218 break;
Duncan Sands03228082008-11-23 15:47:28 +00002219 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002220 case ISD::SETCC:
2221 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands28b77e92011-09-06 19:07:46 +00002222 if (TLI.getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002223 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002224 return VTBits;
2225 break;
2226 case ISD::ROTL:
2227 case ISD::ROTR:
2228 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002229 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002230
Dan Gohmanea859be2007-06-22 14:59:07 +00002231 // Handle rotate right by N like a rotate left by 32-N.
2232 if (Op.getOpcode() == ISD::ROTR)
2233 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2234
2235 // If we aren't rotating out all of the known-in sign bits, return the
2236 // number that are left. This handles rotl(sext(x), 1) for example.
2237 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2238 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2239 }
2240 break;
2241 case ISD::ADD:
2242 // Add can have at most one carry bit. Thus we know that the output
2243 // is, at worst, one more bit than the inputs.
2244 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2245 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002246
Dan Gohmanea859be2007-06-22 14:59:07 +00002247 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002248 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002249 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002250 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002251 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002252
Dan Gohmanea859be2007-06-22 14:59:07 +00002253 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2254 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002255 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002256 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002257
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 // If we are subtracting one from a positive number, there is no carry
2259 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002260 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002261 return Tmp;
2262 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002263
Dan Gohmanea859be2007-06-22 14:59:07 +00002264 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2265 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002266 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002267
Dan Gohmanea859be2007-06-22 14:59:07 +00002268 case ISD::SUB:
2269 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2270 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002271
Dan Gohmanea859be2007-06-22 14:59:07 +00002272 // Handle NEG.
2273 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002274 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002275 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002276 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002277 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2278 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002279 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002280 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002281
Dan Gohmanea859be2007-06-22 14:59:07 +00002282 // If the input is known to be positive (the sign bit is known clear),
2283 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002284 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002285 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002286
Dan Gohmanea859be2007-06-22 14:59:07 +00002287 // Otherwise, we treat this like a SUB.
2288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002289
Dan Gohmanea859be2007-06-22 14:59:07 +00002290 // Sub can have at most one carry bit. Thus we know that the output
2291 // is, at worst, one more bit than the inputs.
2292 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2293 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002294 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002295 case ISD::TRUNCATE:
2296 // FIXME: it's tricky to do anything useful for this, but it is an important
2297 // case for targets like X86.
2298 break;
2299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002300
Dan Gohmanea859be2007-06-22 14:59:07 +00002301 // Handle LOADX separately here. EXTLOAD case will fallthrough.
Jakub Staszakce00b442012-05-01 23:06:00 +00002302 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00002303 unsigned ExtType = LD->getExtensionType();
2304 switch (ExtType) {
2305 default: break;
2306 case ISD::SEXTLOAD: // '17' 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+1;
2309 case ISD::ZEXTLOAD: // '16' bits known
Dan Gohmand1996362010-01-09 02:13:55 +00002310 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002311 return VTBits-Tmp;
2312 }
2313 }
2314
2315 // Allow the target to implement this method for its nodes.
2316 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002317 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002318 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2319 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2320 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002321 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002322 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002323
Dan Gohmanea859be2007-06-22 14:59:07 +00002324 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2325 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002326 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002327 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002328
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002329 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002330 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002331 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002332 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002333 Mask = KnownOne;
2334 } else {
2335 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002336 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002337 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002338
Dan Gohmanea859be2007-06-22 14:59:07 +00002339 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2340 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002341 Mask = ~Mask;
2342 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002343 // Return # leading zeros. We use 'min' here in case Val was zero before
2344 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002345 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002346}
2347
Chris Lattner0a9481f2011-02-13 22:25:43 +00002348/// isBaseWithConstantOffset - Return true if the specified operand is an
2349/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2350/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2351/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002352/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002353bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2354 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2355 !isa<ConstantSDNode>(Op.getOperand(1)))
2356 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002357
2358 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002359 !MaskedValueIsZero(Op.getOperand(0),
2360 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2361 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002362
Chris Lattner0a9481f2011-02-13 22:25:43 +00002363 return true;
2364}
2365
2366
Dan Gohman8d44b282009-09-03 20:34:31 +00002367bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2368 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002369 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002370 return true;
2371
2372 // If the value is a constant, we can obviously see if it is a NaN or not.
2373 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2374 return !C->getValueAPF().isNaN();
2375
2376 // TODO: Recognize more cases here.
2377
2378 return false;
2379}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002380
Dan Gohmane8326932010-02-24 06:52:40 +00002381bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2382 // If the value is a constant, we can obviously see if it is a zero or not.
2383 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2384 return !C->isZero();
2385
2386 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002387 switch (Op.getOpcode()) {
2388 default: break;
2389 case ISD::OR:
2390 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2391 return !C->isNullValue();
2392 break;
2393 }
Dan Gohmane8326932010-02-24 06:52:40 +00002394
2395 return false;
2396}
2397
2398bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2399 // Check the obvious case.
2400 if (A == B) return true;
2401
2402 // For for negative and positive zero.
2403 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2404 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2405 if (CA->isZero() && CB->isZero()) return true;
2406
2407 // Otherwise they may not be equal.
2408 return false;
2409}
2410
Chris Lattnerc3aae252005-01-07 07:46:32 +00002411/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002412///
Owen Andersone50ed302009-08-10 22:56:29 +00002413SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002414 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002415 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002416 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002417 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002418 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002419
Dan Gohman95531882010-03-18 18:49:47 +00002420 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002421 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002422
Chris Lattner4a283e92006-08-11 18:38:11 +00002423 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002424#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002425 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002426#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002427 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002428}
2429
Bill Wendling7ade28c2009-01-28 22:17:52 +00002430SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002431 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002432 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002433 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002434 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002435 switch (Opcode) {
2436 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002437 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002438 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002439 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002440 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002441 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002442 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002443 case ISD::UINT_TO_FP:
2444 case ISD::SINT_TO_FP: {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +00002445 APFloat apf(APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002446 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002447 Opcode==ISD::SINT_TO_FP,
2448 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002449 return getConstantFP(apf, VT);
2450 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002451 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002452 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Kristof Beyls789efba2012-09-12 11:25:02 +00002453 return getConstantFP(APFloat(Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Kristof Beyls789efba2012-09-12 11:25:02 +00002455 return getConstantFP(APFloat(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002456 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002457 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002458 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002459 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002460 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002461 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002462 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002463 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002464 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002465 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002466 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002467 }
2468 }
2469
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002470 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002471 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002472 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002473 switch (Opcode) {
2474 case ISD::FNEG:
2475 V.changeSign();
2476 return getConstantFP(V, VT);
2477 case ISD::FABS:
2478 V.clearSign();
2479 return getConstantFP(V, VT);
2480 case ISD::FCEIL: {
2481 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2482 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002483 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002484 break;
2485 }
2486 case ISD::FTRUNC: {
2487 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2488 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002489 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002490 break;
2491 }
2492 case ISD::FFLOOR: {
2493 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2494 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002495 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002496 break;
2497 }
2498 case ISD::FP_EXTEND: {
2499 bool ignored;
2500 // This can return overflow, underflow, or inexact; we don't care.
2501 // FIXME need to be more flexible about rounding mode.
2502 (void)V.convert(*EVTToAPFloatSemantics(VT),
2503 APFloat::rmNearestTiesToEven, &ignored);
2504 return getConstantFP(V, VT);
2505 }
2506 case ISD::FP_TO_SINT:
2507 case ISD::FP_TO_UINT: {
2508 integerPart x[2];
2509 bool ignored;
2510 assert(integerPartWidth >= 64);
2511 // FIXME need to be more flexible about rounding mode.
2512 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2513 Opcode==ISD::FP_TO_SINT,
2514 APFloat::rmTowardZero, &ignored);
2515 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002516 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002517 APInt api(VT.getSizeInBits(), x);
2518 return getConstant(api, VT);
2519 }
2520 case ISD::BITCAST:
2521 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2522 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2523 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2524 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2525 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002526 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002527 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002528
Gabor Greifba36cb52008-08-28 21:40:38 +00002529 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002530 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002531 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002532 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002533 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002534 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002535 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002536 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002537 assert(VT.isFloatingPoint() &&
2538 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002539 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002540 assert((!VT.isVector() ||
2541 VT.getVectorNumElements() ==
2542 Operand.getValueType().getVectorNumElements()) &&
2543 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002544 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002545 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002546 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002547 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002548 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002549 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002550 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002551 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2552 "Invalid sext node, dst < src!");
2553 assert((!VT.isVector() ||
2554 VT.getVectorNumElements() ==
2555 Operand.getValueType().getVectorNumElements()) &&
2556 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002557 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002558 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002559 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002560 // sext(undef) = 0, because the top bits will all be the same.
2561 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002562 break;
2563 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002564 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002565 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002566 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002567 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2568 "Invalid zext node, dst < src!");
2569 assert((!VT.isVector() ||
2570 VT.getVectorNumElements() ==
2571 Operand.getValueType().getVectorNumElements()) &&
2572 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002573 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002574 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002575 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002576 else if (OpOpcode == ISD::UNDEF)
2577 // zext(undef) = 0, because the top bits will be zero.
2578 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002579 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002580 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002581 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002582 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002583 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002584 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2585 "Invalid anyext node, dst < src!");
2586 assert((!VT.isVector() ||
2587 VT.getVectorNumElements() ==
2588 Operand.getValueType().getVectorNumElements()) &&
2589 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002590
Dan Gohman9e86a732010-06-18 00:08:30 +00002591 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2592 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002593 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002594 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002595 else if (OpOpcode == ISD::UNDEF)
2596 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002597
2598 // (ext (trunx x)) -> x
2599 if (OpOpcode == ISD::TRUNCATE) {
2600 SDValue OpOp = Operand.getNode()->getOperand(0);
2601 if (OpOp.getValueType() == VT)
2602 return OpOp;
2603 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002604 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002605 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002607 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002608 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002609 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2610 "Invalid truncate node, src < dst!");
2611 assert((!VT.isVector() ||
2612 VT.getVectorNumElements() ==
2613 Operand.getValueType().getVectorNumElements()) &&
2614 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002615 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002616 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002617 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2618 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002619 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002620 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2621 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002622 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002623 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002624 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002625 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002626 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002627 if (OpOpcode == ISD::UNDEF)
2628 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002629 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002630 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002631 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002632 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002633 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002634 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002635 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2636 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002637 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002638 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002639 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002640 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002641 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002642 (VT.getVectorElementType() == Operand.getValueType() ||
2643 (VT.getVectorElementType().isInteger() &&
2644 Operand.getValueType().isInteger() &&
2645 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002646 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002647 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002648 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002649 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2650 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2651 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2652 Operand.getConstantOperandVal(1) == 0 &&
2653 Operand.getOperand(0).getValueType() == VT)
2654 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002655 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002656 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002657 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002658 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002659 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002660 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002661 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002662 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002663 break;
2664 case ISD::FABS:
2665 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002666 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002667 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002668 }
2669
Chris Lattner43247a12005-08-25 19:12:10 +00002670 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002671 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002672 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002673 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002674 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002675 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002676 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002677 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002678 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002679
Dan Gohman95531882010-03-18 18:49:47 +00002680 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002681 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002682 } else {
Dan Gohman95531882010-03-18 18:49:47 +00002683 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002684 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002685
Chris Lattnerc3aae252005-01-07 07:46:32 +00002686 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002687#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002688 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002689#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002690 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002691}
2692
Bill Wendling688d1c42008-09-24 10:16:24 +00002693SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002694 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002695 ConstantSDNode *Cst1,
2696 ConstantSDNode *Cst2) {
2697 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2698
2699 switch (Opcode) {
2700 case ISD::ADD: return getConstant(C1 + C2, VT);
2701 case ISD::SUB: return getConstant(C1 - C2, VT);
2702 case ISD::MUL: return getConstant(C1 * C2, VT);
2703 case ISD::UDIV:
2704 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2705 break;
2706 case ISD::UREM:
2707 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2708 break;
2709 case ISD::SDIV:
2710 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2711 break;
2712 case ISD::SREM:
2713 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2714 break;
2715 case ISD::AND: return getConstant(C1 & C2, VT);
2716 case ISD::OR: return getConstant(C1 | C2, VT);
2717 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2718 case ISD::SHL: return getConstant(C1 << C2, VT);
2719 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2720 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2721 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2722 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2723 default: break;
2724 }
2725
2726 return SDValue();
2727}
2728
Owen Andersone50ed302009-08-10 22:56:29 +00002729SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002730 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002731 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2732 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002733 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002734 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002735 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002736 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2737 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002738 // Fold trivial token factors.
2739 if (N1.getOpcode() == ISD::EntryToken) return N2;
2740 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002741 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002742 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002743 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002744 // Concat of UNDEFs is UNDEF.
2745 if (N1.getOpcode() == ISD::UNDEF &&
2746 N2.getOpcode() == ISD::UNDEF)
2747 return getUNDEF(VT);
2748
Dan Gohman7f8613e2008-08-14 20:04:46 +00002749 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2750 // one big BUILD_VECTOR.
2751 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2752 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002753 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2754 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002755 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002756 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002757 }
2758 break;
Chris Lattner76365122005-01-16 02:23:22 +00002759 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002760 assert(VT.isInteger() && "This operator does not apply to FP types!");
2761 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002762 N1.getValueType() == VT && "Binary operator types must match!");
2763 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2764 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002765 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002766 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002767 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2768 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002769 break;
Chris Lattner76365122005-01-16 02:23:22 +00002770 case ISD::OR:
2771 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002772 case ISD::ADD:
2773 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002774 assert(VT.isInteger() && "This operator does not apply to FP types!");
2775 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002776 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002777 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2778 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002779 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002780 return N1;
2781 break;
Chris Lattner76365122005-01-16 02:23:22 +00002782 case ISD::UDIV:
2783 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002784 case ISD::MULHU:
2785 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002786 case ISD::MUL:
2787 case ISD::SDIV:
2788 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002789 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002790 assert(N1.getValueType() == N2.getValueType() &&
2791 N1.getValueType() == VT && "Binary operator types must match!");
2792 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002793 case ISD::FADD:
2794 case ISD::FSUB:
2795 case ISD::FMUL:
2796 case ISD::FDIV:
2797 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002798 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002799 if (Opcode == ISD::FADD) {
2800 // 0+x --> x
2801 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2802 if (CFP->getValueAPF().isZero())
2803 return N2;
2804 // x+0 --> x
2805 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2806 if (CFP->getValueAPF().isZero())
2807 return N1;
2808 } else if (Opcode == ISD::FSUB) {
2809 // x-0 --> x
2810 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2811 if (CFP->getValueAPF().isZero())
2812 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00002813 } else if (Opcode == ISD::FMUL) {
2814 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2815 SDValue V = N2;
2816
2817 // If the first operand isn't the constant, try the second
2818 if (!CFP) {
2819 CFP = dyn_cast<ConstantFPSDNode>(N2);
2820 V = N1;
2821 }
2822
2823 if (CFP) {
2824 // 0*x --> 0
2825 if (CFP->isZero())
2826 return SDValue(CFP,0);
2827 // 1*x --> x
2828 if (CFP->isExactlyValue(1.0))
2829 return V;
2830 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00002831 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002832 }
Dale Johannesen78995512010-05-15 18:38:02 +00002833 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002834 assert(N1.getValueType() == N2.getValueType() &&
2835 N1.getValueType() == VT && "Binary operator types must match!");
2836 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002837 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2838 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002839 N1.getValueType().isFloatingPoint() &&
2840 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002841 "Invalid FCOPYSIGN!");
2842 break;
Chris Lattner76365122005-01-16 02:23:22 +00002843 case ISD::SHL:
2844 case ISD::SRA:
2845 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002846 case ISD::ROTL:
2847 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002848 assert(VT == N1.getValueType() &&
2849 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002851 "Shifts only work on integers");
Chris Lattnere0751182011-02-13 19:09:16 +00002852 // Verify that the shift amount VT is bit enough to hold valid shift
2853 // amounts. This catches things like trying to shift an i1024 value by an
2854 // i8, which is easy to fall into in generic code that uses
2855 // TLI.getShiftAmount().
2856 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00002857 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00002858 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00002859
2860 // Always fold shifts of i1 values so the code generator doesn't need to
2861 // handle them. Since we know the size of the shift has to be less than the
2862 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002863 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002864 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00002865 if (N2C && N2C->isNullValue())
2866 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002867 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002868 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002869 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002870 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002871 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002872 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00002873 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002874 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002875 "type is vector!");
2876 assert((!EVT.isVector() ||
2877 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2878 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002879 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00002880 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002881 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002882 break;
2883 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002884 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002885 assert(VT.isFloatingPoint() &&
2886 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002887 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002888 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002889 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002890 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002891 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002892 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002893 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002894 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002896 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00002897 assert(!EVT.isVector() &&
2898 "AssertSExt/AssertZExt type should be the vector element type "
2899 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002900 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002901 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002902 break;
2903 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002904 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002905 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002906 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002907 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002908 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00002909 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002910 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00002911 "type is vector!");
2912 assert((!EVT.isVector() ||
2913 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2914 "Vector element counts must match in SIGN_EXTEND_INREG");
2915 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002916 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002917
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002918 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002919 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00002920 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002921 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002922 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002923 return getConstant(Val, VT);
2924 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002925 break;
2926 }
2927 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002928 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2929 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002930 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002931
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002932 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2933 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002934 if (N2C &&
2935 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002936 N1.getNumOperands() > 0) {
2937 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002938 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002939 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002940 N1.getOperand(N2C->getZExtValue() / Factor),
2941 getConstant(N2C->getZExtValue() % Factor,
2942 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002943 }
2944
2945 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2946 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002947 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2948 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00002949
2950 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00002951 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00002952 // are promoted and implicitly truncated, and the result implicitly
2953 // extended. Make that explicit here.
2954 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00002955
Bob Wilsonb1303d02009-04-13 22:05:19 +00002956 return Elt;
2957 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002958
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002959 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2960 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002961 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00002962 // If the indices are the same, return the inserted element else
2963 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00002964 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00002965 SDValue N1Op2 = N1.getOperand(2);
2966 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
2967
2968 if (N1Op2C && N2C) {
2969 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
2970 if (VT == N1.getOperand(1).getValueType())
2971 return N1.getOperand(1);
2972 else
2973 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
2974 }
2975
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002976 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00002977 }
Dan Gohman6ab64222008-08-13 21:51:37 +00002978 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002979 break;
2980 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002981 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002982 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2983 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00002984 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00002985 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002986
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002987 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2988 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002989 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002990 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002991 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002992
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002993 // EXTRACT_ELEMENT of a constant int is also very common.
2994 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002995 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002996 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002997 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2998 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002999 }
3000 break;
David Greene91585092011-01-26 15:38:49 +00003001 case ISD::EXTRACT_SUBVECTOR: {
3002 SDValue Index = N2;
3003 if (VT.isSimple() && N1.getValueType().isSimple()) {
3004 assert(VT.isVector() && N1.getValueType().isVector() &&
3005 "Extract subvector VTs must be a vectors!");
3006 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
3007 "Extract subvector VTs must have the same element type!");
3008 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3009 "Extract subvector must be from larger vector to smaller vector!");
3010
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003011 if (isa<ConstantSDNode>(Index.getNode())) {
3012 assert((VT.getVectorNumElements() +
3013 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003014 <= N1.getValueType().getVectorNumElements())
3015 && "Extract subvector overflow!");
3016 }
3017
3018 // Trivial extraction.
3019 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
3020 return N1;
3021 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003022 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003023 }
David Greene91585092011-01-26 15:38:49 +00003024 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003025
3026 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003027 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00003028 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
3029 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003030 } else { // Cannonicalize constant to RHS if commutative
3031 if (isCommutativeBinOp(Opcode)) {
3032 std::swap(N1C, N2C);
3033 std::swap(N1, N2);
3034 }
3035 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003036 }
3037
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003038 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003039 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3040 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003041 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003042 if (!N2CFP && isCommutativeBinOp(Opcode)) {
3043 // Cannonicalize constant to RHS if commutative
3044 std::swap(N1CFP, N2CFP);
3045 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003046 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003047 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3048 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003049 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003050 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003051 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003052 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003053 return getConstantFP(V1, VT);
3054 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003055 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003056 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3057 if (s!=APFloat::opInvalidOp)
3058 return getConstantFP(V1, VT);
3059 break;
3060 case ISD::FMUL:
3061 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3062 if (s!=APFloat::opInvalidOp)
3063 return getConstantFP(V1, VT);
3064 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003065 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003066 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3067 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3068 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003069 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003070 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003071 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3072 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3073 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003074 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003075 case ISD::FCOPYSIGN:
3076 V1.copySign(V2);
3077 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003078 default: break;
3079 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003080 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003081
3082 if (Opcode == ISD::FP_ROUND) {
3083 APFloat V = N1CFP->getValueAPF(); // make copy
3084 bool ignored;
3085 // This can return overflow, underflow, or inexact; we don't care.
3086 // FIXME need to be more flexible about rounding mode.
3087 (void)V.convert(*EVTToAPFloatSemantics(VT),
3088 APFloat::rmNearestTiesToEven, &ignored);
3089 return getConstantFP(V, VT);
3090 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003092
Chris Lattner62b57722006-04-20 05:39:12 +00003093 // Canonicalize an UNDEF to the RHS, even over a constant.
3094 if (N1.getOpcode() == ISD::UNDEF) {
3095 if (isCommutativeBinOp(Opcode)) {
3096 std::swap(N1, N2);
3097 } else {
3098 switch (Opcode) {
3099 case ISD::FP_ROUND_INREG:
3100 case ISD::SIGN_EXTEND_INREG:
3101 case ISD::SUB:
3102 case ISD::FSUB:
3103 case ISD::FDIV:
3104 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003105 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003106 return N1; // fold op(undef, arg2) -> undef
3107 case ISD::UDIV:
3108 case ISD::SDIV:
3109 case ISD::UREM:
3110 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003111 case ISD::SRL:
3112 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003113 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003114 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3115 // For vectors, we can't easily build an all zero vector, just return
3116 // the LHS.
3117 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003118 }
3119 }
3120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003121
3122 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003123 if (N2.getOpcode() == ISD::UNDEF) {
3124 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003125 case ISD::XOR:
3126 if (N1.getOpcode() == ISD::UNDEF)
3127 // Handle undef ^ undef -> 0 special case. This is a common
3128 // idiom (misuse).
3129 return getConstant(0, VT);
3130 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003131 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003132 case ISD::ADDC:
3133 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003134 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003135 case ISD::UDIV:
3136 case ISD::SDIV:
3137 case ISD::UREM:
3138 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003139 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003140 case ISD::FADD:
3141 case ISD::FSUB:
3142 case ISD::FMUL:
3143 case ISD::FDIV:
3144 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003145 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003146 return N2;
3147 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003148 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003149 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003150 case ISD::SRL:
3151 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003152 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003153 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3154 // For vectors, we can't easily build an all zero vector, just return
3155 // the LHS.
3156 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003157 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003158 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003159 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003160 // For vectors, we can't easily build an all one vector, just return
3161 // the LHS.
3162 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003163 case ISD::SRA:
3164 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003165 }
3166 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003167
Chris Lattner27e9b412005-05-11 18:57:39 +00003168 // Memoize this node if possible.
3169 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003170 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003171 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003172 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003173 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003174 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003175 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003176 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003177 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003178
Dan Gohman95531882010-03-18 18:49:47 +00003179 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003180 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003181 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003182 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003183 }
3184
Chris Lattnerc3aae252005-01-07 07:46:32 +00003185 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003186#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003187 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003188#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003189 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003190}
3191
Owen Andersone50ed302009-08-10 22:56:29 +00003192SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003193 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003194 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003195 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003196 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00003197 case ISD::CONCAT_VECTORS:
3198 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3199 // one big BUILD_VECTOR.
3200 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3201 N2.getOpcode() == ISD::BUILD_VECTOR &&
3202 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003203 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3204 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003205 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3206 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003207 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003208 }
3209 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003210 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003211 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003212 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003213 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003214 break;
3215 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003216 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003217 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003218 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003219 return N2; // select true, X, Y -> X
3220 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003221 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003222
3223 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003224 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003225 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003226 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003227 case ISD::INSERT_SUBVECTOR: {
3228 SDValue Index = N3;
3229 if (VT.isSimple() && N1.getValueType().isSimple()
3230 && N2.getValueType().isSimple()) {
3231 assert(VT.isVector() && N1.getValueType().isVector() &&
3232 N2.getValueType().isVector() &&
3233 "Insert subvector VTs must be a vectors");
3234 assert(VT == N1.getValueType() &&
3235 "Dest and insert subvector source types must match!");
3236 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3237 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003238 if (isa<ConstantSDNode>(Index.getNode())) {
3239 assert((N2.getValueType().getVectorNumElements() +
3240 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003241 <= VT.getVectorNumElements())
3242 && "Insert subvector overflow!");
3243 }
3244
3245 // Trivial insertion.
3246 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3247 return N2;
3248 }
3249 break;
3250 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003251 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003252 // Fold bit_convert nodes from a type to themselves.
3253 if (N1.getValueType() == VT)
3254 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003255 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003256 }
3257
Chris Lattner43247a12005-08-25 19:12:10 +00003258 // Memoize node if it doesn't produce a flag.
3259 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003260 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003261 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003262 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003263 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003264 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003265 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003266 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003267 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003268
Dan Gohman95531882010-03-18 18:49:47 +00003269 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003270 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003271 } else {
Dan Gohman95531882010-03-18 18:49:47 +00003272 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003273 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003274
Chris Lattnerc3aae252005-01-07 07:46:32 +00003275 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003276#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003277 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003278#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003279 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003280}
3281
Owen Andersone50ed302009-08-10 22:56:29 +00003282SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003283 SDValue N1, SDValue N2, SDValue N3,
3284 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003285 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003286 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003287}
3288
Owen Andersone50ed302009-08-10 22:56:29 +00003289SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003290 SDValue N1, SDValue N2, SDValue N3,
3291 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003292 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003293 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003294}
3295
Dan Gohman98ca4f22009-08-05 01:29:28 +00003296/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3297/// the incoming stack arguments to be loaded from the stack.
3298SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3299 SmallVector<SDValue, 8> ArgChains;
3300
3301 // Include the original chain at the beginning of the list. When this is
3302 // used by target LowerCall hooks, this helps legalize find the
3303 // CALLSEQ_BEGIN node.
3304 ArgChains.push_back(Chain);
3305
3306 // Add a chain value for each stack argument.
3307 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3308 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3309 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3310 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3311 if (FI->getIndex() < 0)
3312 ArgChains.push_back(SDValue(L, 1));
3313
3314 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003316 &ArgChains[0], ArgChains.size());
3317}
3318
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003319/// SplatByte - Distribute ByteVal over NumBits bits.
3320static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3321 APInt Val = APInt(NumBits, ByteVal);
3322 unsigned Shift = 8;
3323 for (unsigned i = NumBits; i > 8; i >>= 1) {
3324 Val = (Val << Shift) | Val;
3325 Shift <<= 1;
3326 }
3327 return Val;
3328}
3329
Dan Gohman707e0182008-04-12 04:36:06 +00003330/// getMemsetValue - Vectorized representation of the memset value
3331/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003332static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003333 DebugLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003334 assert(Value.getOpcode() != ISD::UNDEF);
3335
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003336 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003337 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003338 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003339 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003340 return DAG.getConstant(Val, VT);
3341 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003342 }
Evan Chengf0df0312008-05-15 08:39:06 +00003343
Dale Johannesen0f502f62009-02-03 22:26:09 +00003344 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003345 if (NumBits > 8) {
3346 // Use a multiplication with 0x010101... to extend the input to the
3347 // required length.
3348 APInt Magic = SplatByte(NumBits, 0x01);
3349 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003350 }
3351
3352 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003353}
3354
Dan Gohman707e0182008-04-12 04:36:06 +00003355/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3356/// used when a memcpy is turned into a memset when the source is a constant
3357/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003358static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003359 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003360 // Handle vector with all elements zero.
3361 if (Str.empty()) {
3362 if (VT.isInteger())
3363 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003364 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003365 return DAG.getConstantFP(0.0, VT);
3366 else if (VT.isVector()) {
3367 unsigned NumElts = VT.getVectorNumElements();
3368 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003369 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003370 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3371 EltVT, NumElts)));
3372 } else
3373 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003374 }
3375
Duncan Sands83ec4b62008-06-06 12:08:01 +00003376 assert(!VT.isVector() && "Can't handle vector type here!");
Chris Lattner18c7f802012-02-05 02:29:43 +00003377 unsigned NumVTBytes = VT.getSizeInBits() / 8;
3378 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3379
Evan Cheng376642e2012-12-10 23:21:26 +00003380 APInt Val(NumBytes*8, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003381 if (TLI.isLittleEndian()) {
3382 for (unsigned i = 0; i != NumBytes; ++i)
3383 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3384 } else {
3385 for (unsigned i = 0; i != NumBytes; ++i)
3386 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003387 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003388
Evan Chenge07f85e2012-12-11 23:26:14 +00003389 // If the "cost" of materializing the integer immediate is 1 or free, then
3390 // it is cost effective to turn the load into the immediate.
Chandler Carruthe4b4edd2013-01-05 12:32:17 +00003391 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3392 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng376642e2012-12-10 23:21:26 +00003393 return DAG.getConstant(Val, VT);
3394 return SDValue(0, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003395}
3396
Scott Michelfdc40a02009-02-17 22:15:04 +00003397/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003398///
Dan Gohman475871a2008-07-27 21:46:04 +00003399static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003400 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003401 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003402 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003403 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003404}
3405
Evan Chengf0df0312008-05-15 08:39:06 +00003406/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3407///
Chris Lattner18c7f802012-02-05 02:29:43 +00003408static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003409 unsigned SrcDelta = 0;
3410 GlobalAddressSDNode *G = NULL;
3411 if (Src.getOpcode() == ISD::GlobalAddress)
3412 G = cast<GlobalAddressSDNode>(Src);
3413 else if (Src.getOpcode() == ISD::ADD &&
3414 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3415 Src.getOperand(1).getOpcode() == ISD::Constant) {
3416 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003417 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003418 }
3419 if (!G)
3420 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003421
Chris Lattner18c7f802012-02-05 02:29:43 +00003422 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003423}
Dan Gohman707e0182008-04-12 04:36:06 +00003424
Evan Cheng255f20f2010-04-01 06:04:33 +00003425/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3426/// to replace the memset / memcpy. Return true if the number of memory ops
3427/// is below the threshold. It returns the types of the sequence of
3428/// memory ops to perform memset / memcpy by reference.
3429static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003430 unsigned Limit, uint64_t Size,
3431 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003432 bool IsMemset,
3433 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003434 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003435 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003436 SelectionDAG &DAG,
3437 const TargetLowering &TLI) {
3438 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3439 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003440 // If 'SrcAlign' is zero, that means the memory operation does not need to
3441 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3442 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3443 // is the specified alignment of the memory operation. If it is zero, that
3444 // means it's possible to change the alignment of the destination.
3445 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3446 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003447 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003448 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003449 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003450
Chris Lattneracee6472010-03-07 07:45:08 +00003451 if (VT == MVT::Other) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00003452 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003453 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003454 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003455 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003456 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003457 case 0: VT = MVT::i64; break;
3458 case 4: VT = MVT::i32; break;
3459 case 2: VT = MVT::i16; break;
3460 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003461 }
3462 }
3463
Owen Anderson766b5ef2009-08-11 21:59:30 +00003464 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003465 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003466 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003467 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003468
Duncan Sands8e4eb092008-06-08 20:54:56 +00003469 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003470 VT = LVT;
3471 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003472
Dan Gohman707e0182008-04-12 04:36:06 +00003473 unsigned NumMemOps = 0;
3474 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003475 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003476 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003477 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003478 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003479 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003480
3481 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003482 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003483 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003484 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003485 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003486 Found = true;
3487 else if (NewVT == MVT::i64 &&
3488 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003489 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003490 // i64 is usually not legal on 32-bit targets, but f64 may be.
3491 NewVT = MVT::f64;
3492 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003493 }
Evan Cheng376642e2012-12-10 23:21:26 +00003494 }
3495
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003496 if (!Found) {
3497 do {
3498 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3499 if (NewVT == MVT::i8)
3500 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003501 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003502 }
3503 NewVTSize = NewVT.getSizeInBits() / 8;
3504
Evan Cheng376642e2012-12-10 23:21:26 +00003505 // If the new VT cannot cover all of the remaining bits, then consider
3506 // issuing a (or a pair of) unaligned and overlapping load / store.
3507 // FIXME: Only does this for 64-bit or more since we don't have proper
3508 // cost model for unaligned load / store.
3509 bool Fast;
Evan Chenga16e49d2012-12-12 20:43:23 +00003510 if (NumMemOps && AllowOverlap &&
3511 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng376642e2012-12-10 23:21:26 +00003512 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3513 VTSize = Size;
3514 else {
3515 VT = NewVT;
3516 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003517 }
Dan Gohman707e0182008-04-12 04:36:06 +00003518 }
Dan Gohman707e0182008-04-12 04:36:06 +00003519
Evan Chenga16e49d2012-12-12 20:43:23 +00003520 if (++NumMemOps > Limit)
3521 return false;
3522
Dan Gohman707e0182008-04-12 04:36:06 +00003523 MemOps.push_back(VT);
3524 Size -= VTSize;
3525 }
3526
3527 return true;
3528}
3529
Dale Johannesen0f502f62009-02-03 22:26:09 +00003530static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003531 SDValue Chain, SDValue Dst,
3532 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003533 unsigned Align, bool isVol,
3534 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003535 MachinePointerInfo DstPtrInfo,
3536 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003537 // Turn a memcpy of undef to nop.
3538 if (Src.getOpcode() == ISD::UNDEF)
3539 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003540
Dan Gohman21323f32008-05-29 19:42:22 +00003541 // Expand memcpy to a series of load and store ops if the size operand falls
3542 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003543 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3544 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003545 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003546 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003547 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003548 MachineFunction &MF = DAG.getMachineFunction();
3549 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003550 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003551 MF.getFunction()->getAttributes().
3552 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003553 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3554 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3555 DstAlignCanChange = true;
3556 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3557 if (Align > SrcAlign)
3558 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003559 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003560 bool CopyFromStr = isMemSrcFromString(Src, Str);
3561 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003562 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003563
Evan Cheng94107ba2010-04-01 18:19:11 +00003564 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003565 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003566 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003567 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003568 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003569
Evan Cheng255f20f2010-04-01 06:04:33 +00003570 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003571 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003572 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003573 if (NewAlign > Align) {
3574 // Give the stack frame object a larger alignment if needed.
3575 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3576 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3577 Align = NewAlign;
3578 }
3579 }
Dan Gohman707e0182008-04-12 04:36:06 +00003580
Dan Gohman475871a2008-07-27 21:46:04 +00003581 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003582 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003583 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003584 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003585 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003586 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003588
Evan Cheng376642e2012-12-10 23:21:26 +00003589 if (VTSize > Size) {
3590 // Issuing an unaligned load / store pair that overlaps with the previous
3591 // pair. Adjust the offset accordingly.
3592 assert(i == NumMemOps-1 && i != 0);
3593 SrcOff -= VTSize - Size;
3594 DstOff -= VTSize - Size;
3595 }
3596
Evan Cheng255f20f2010-04-01 06:04:33 +00003597 if (CopyFromStr &&
3598 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003599 // It's unlikely a store of a vector immediate can be done in a single
3600 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003601 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003602 // FIXME: Handle other cases where store of vector immediate is done in
3603 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003604 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003605 if (Value.getNode())
3606 Store = DAG.getStore(Chain, dl, Value,
3607 getMemBasePlusOffset(Dst, DstOff, DAG),
3608 DstPtrInfo.getWithOffset(DstOff), isVol,
3609 false, Align);
3610 }
3611
3612 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003613 // The type might not be legal for the target. This should only happen
3614 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003615 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3616 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003617 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003618 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003619 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003620 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003621 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003622 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003623 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003624 Store = DAG.getTruncStore(Chain, dl, Value,
David Greene1e559442010-02-15 17:00:31 +00003625 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003626 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3627 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003628 }
3629 OutChains.push_back(Store);
3630 SrcOff += VTSize;
3631 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003632 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003633 }
3634
Owen Anderson825b72b2009-08-11 20:47:22 +00003635 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003636 &OutChains[0], OutChains.size());
3637}
3638
Dale Johannesen0f502f62009-02-03 22:26:09 +00003639static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003640 SDValue Chain, SDValue Dst,
3641 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003642 unsigned Align, bool isVol,
3643 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003644 MachinePointerInfo DstPtrInfo,
3645 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003646 // Turn a memmove of undef to nop.
3647 if (Src.getOpcode() == ISD::UNDEF)
3648 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003649
3650 // Expand memmove to a series of load and store ops if the size operand falls
3651 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003652 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003653 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003654 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003655 MachineFunction &MF = DAG.getMachineFunction();
3656 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003657 bool OptSize = MF.getFunction()->getAttributes().
3658 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003659 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3660 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3661 DstAlignCanChange = true;
3662 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3663 if (Align > SrcAlign)
3664 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003665 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003666
Evan Cheng94107ba2010-04-01 18:19:11 +00003667 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00003668 (DstAlignCanChange ? 0 : Align), SrcAlign,
3669 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003670 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003671
Evan Cheng255f20f2010-04-01 06:04:33 +00003672 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003673 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003674 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003675 if (NewAlign > Align) {
3676 // Give the stack frame object a larger alignment if needed.
3677 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3678 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3679 Align = NewAlign;
3680 }
3681 }
Dan Gohman21323f32008-05-29 19:42:22 +00003682
Evan Cheng255f20f2010-04-01 06:04:33 +00003683 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003684 SmallVector<SDValue, 8> LoadValues;
3685 SmallVector<SDValue, 8> LoadChains;
3686 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003687 unsigned NumMemOps = MemOps.size();
3688 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003689 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003690 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003691 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003692
Dale Johannesen0f502f62009-02-03 22:26:09 +00003693 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003694 getMemBasePlusOffset(Src, SrcOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003695 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003696 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003697 LoadValues.push_back(Value);
3698 LoadChains.push_back(Value.getValue(1));
3699 SrcOff += VTSize;
3700 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003701 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003702 &LoadChains[0], LoadChains.size());
3703 OutChains.clear();
3704 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003705 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003706 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003707 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003708
Dale Johannesen0f502f62009-02-03 22:26:09 +00003709 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003710 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003711 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003712 OutChains.push_back(Store);
3713 DstOff += VTSize;
3714 }
3715
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003717 &OutChains[0], OutChains.size());
3718}
3719
Dale Johannesen0f502f62009-02-03 22:26:09 +00003720static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003721 SDValue Chain, SDValue Dst,
3722 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003723 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003724 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003725 // Turn a memset of undef to nop.
3726 if (Src.getOpcode() == ISD::UNDEF)
3727 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003728
3729 // Expand memset to a series of load/store ops if the size operand
3730 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003731 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003732 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003733 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003734 MachineFunction &MF = DAG.getMachineFunction();
3735 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003736 bool OptSize = MF.getFunction()->getAttributes().
3737 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003738 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3739 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3740 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003741 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003742 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003743 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003744 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00003745 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003746 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003747
Evan Cheng255f20f2010-04-01 06:04:33 +00003748 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003749 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003750 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003751 if (NewAlign > Align) {
3752 // Give the stack frame object a larger alignment if needed.
3753 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3754 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3755 Align = NewAlign;
3756 }
3757 }
3758
Dan Gohman475871a2008-07-27 21:46:04 +00003759 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003760 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003761 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003762
3763 // Find the largest store and generate the bit pattern for it.
3764 EVT LargestVT = MemOps[0];
3765 for (unsigned i = 1; i < NumMemOps; i++)
3766 if (MemOps[i].bitsGT(LargestVT))
3767 LargestVT = MemOps[i];
3768 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3769
Dan Gohman707e0182008-04-12 04:36:06 +00003770 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003771 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00003772 unsigned VTSize = VT.getSizeInBits() / 8;
3773 if (VTSize > Size) {
3774 // Issuing an unaligned load / store pair that overlaps with the previous
3775 // pair. Adjust the offset accordingly.
3776 assert(i == NumMemOps-1 && i != 0);
3777 DstOff -= VTSize - Size;
3778 }
Benjamin Kramer80220362011-01-02 19:57:05 +00003779
3780 // If this store is smaller than the largest store see whether we can get
3781 // the smaller value for free with a truncate.
3782 SDValue Value = MemSetValue;
3783 if (VT.bitsLT(LargestVT)) {
3784 if (!LargestVT.isVector() && !VT.isVector() &&
3785 TLI.isTruncateFree(LargestVT, VT))
3786 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3787 else
3788 Value = getMemsetValue(Src, VT, DAG, dl);
3789 }
3790 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003791 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003792 getMemBasePlusOffset(Dst, DstOff, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003793 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003794 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003795 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003796 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00003797 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003798 }
3799
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003801 &OutChains[0], OutChains.size());
3802}
3803
Dale Johannesen0f502f62009-02-03 22:26:09 +00003804SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003805 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003806 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003807 MachinePointerInfo DstPtrInfo,
3808 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003809
3810 // Check to see if we should lower the memcpy to loads and stores first.
3811 // For cases within the target-specified limits, this is the best choice.
3812 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3813 if (ConstantSize) {
3814 // Memcpy with size zero? Just return the original chain.
3815 if (ConstantSize->isNullValue())
3816 return Chain;
3817
Evan Cheng255f20f2010-04-01 06:04:33 +00003818 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3819 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00003820 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003821 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003822 return Result;
3823 }
3824
3825 // Then check to see if we should lower the memcpy with target-specific
3826 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003827 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003828 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003829 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003830 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003831 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003832 return Result;
3833
3834 // If we really need inline code and the target declined to provide it,
3835 // use a (potentially long) sequence of loads and stores.
3836 if (AlwaysInline) {
3837 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003838 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003839 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003840 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00003841 }
3842
Dan Gohman7b55d362010-04-05 20:24:08 +00003843 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3844 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3845 // respect volatile, so they may do things like read or write memory
3846 // beyond the given memory regions. But fixing this isn't easy, and most
3847 // people don't care.
3848
Dan Gohman707e0182008-04-12 04:36:06 +00003849 // Emit a library call.
3850 TargetLowering::ArgListTy Args;
3851 TargetLowering::ArgListEntry Entry;
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003852 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003853 Entry.Node = Dst; Args.push_back(Entry);
3854 Entry.Node = Src; Args.push_back(Entry);
3855 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003856 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003857 TargetLowering::
3858 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003859 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003860 TLI.getLibcallCallingConv(RTLIB::MEMCPY),
3861 /*isTailCall=*/false,
3862 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003863 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003864 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003865 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003866 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3867
Dan Gohman707e0182008-04-12 04:36:06 +00003868 return CallResult.second;
3869}
3870
Dale Johannesen0f502f62009-02-03 22:26:09 +00003871SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003873 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003874 MachinePointerInfo DstPtrInfo,
3875 MachinePointerInfo SrcPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003876
Dan Gohman21323f32008-05-29 19:42:22 +00003877 // Check to see if we should lower the memmove to loads and stores first.
3878 // For cases within the target-specified limits, this is the best choice.
3879 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3880 if (ConstantSize) {
3881 // Memmove with size zero? Just return the original chain.
3882 if (ConstantSize->isNullValue())
3883 return Chain;
3884
Dan Gohman475871a2008-07-27 21:46:04 +00003885 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003886 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003887 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003888 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003889 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003890 return Result;
3891 }
Dan Gohman707e0182008-04-12 04:36:06 +00003892
3893 // Then check to see if we should lower the memmove with target-specific
3894 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003895 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003896 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003897 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003898 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003899 return Result;
3900
Mon P Wang01f0e852010-04-06 08:27:51 +00003901 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3902 // not be safe. See memcpy above for more details.
3903
Dan Gohman707e0182008-04-12 04:36:06 +00003904 // Emit a library call.
3905 TargetLowering::ArgListTy Args;
3906 TargetLowering::ArgListEntry Entry;
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003907 Entry.Ty = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003908 Entry.Node = Dst; Args.push_back(Entry);
3909 Entry.Node = Src; Args.push_back(Entry);
3910 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003911 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003912 TargetLowering::
3913 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003914 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003915 TLI.getLibcallCallingConv(RTLIB::MEMMOVE),
3916 /*isTailCall=*/false,
3917 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003918 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003919 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003920 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003921 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3922
Dan Gohman707e0182008-04-12 04:36:06 +00003923 return CallResult.second;
3924}
3925
Dale Johannesen0f502f62009-02-03 22:26:09 +00003926SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003927 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003928 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003929 MachinePointerInfo DstPtrInfo) {
Dan Gohman707e0182008-04-12 04:36:06 +00003930
3931 // Check to see if we should lower the memset to stores first.
3932 // For cases within the target-specified limits, this is the best choice.
3933 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3934 if (ConstantSize) {
3935 // Memset with size zero? Just return the original chain.
3936 if (ConstantSize->isNullValue())
3937 return Chain;
3938
Mon P Wang20adc9d2010-04-04 03:10:48 +00003939 SDValue Result =
3940 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003941 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00003942
Gabor Greifba36cb52008-08-28 21:40:38 +00003943 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003944 return Result;
3945 }
3946
3947 // Then check to see if we should lower the memset with target-specific
3948 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003949 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00003950 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003951 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00003952 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003953 return Result;
3954
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003955 // Emit a library call.
Chandler Carruthece6c6b2012-11-01 08:07:29 +00003956 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003957 TargetLowering::ArgListTy Args;
3958 TargetLowering::ArgListEntry Entry;
3959 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3960 Args.push_back(Entry);
3961 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 if (Src.getValueType().bitsGT(MVT::i32))
3963 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003964 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003965 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003966 Entry.Node = Src;
3967 Entry.Ty = Type::getInt32Ty(*getContext());
3968 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003969 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003970 Entry.Node = Size;
3971 Entry.Ty = IntPtrTy;
3972 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003973 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003974 // FIXME: pass in DebugLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003975 TargetLowering::
3976 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003977 false, false, false, false, 0,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00003978 TLI.getLibcallCallingConv(RTLIB::MEMSET),
3979 /*isTailCall=*/false,
3980 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003981 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003982 TLI.getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00003983 Args, *this, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003984 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
3985
Dan Gohman707e0182008-04-12 04:36:06 +00003986 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003987}
3988
Owen Andersone50ed302009-08-10 22:56:29 +00003989SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00003990 SDValue Chain, SDValue Ptr, SDValue Cmp,
3991 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00003992 unsigned Alignment,
3993 AtomicOrdering Ordering,
Michael Ilseman06b96902012-09-10 16:56:31 +00003994 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003995 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3996 Alignment = getEVTAlignment(MemVT);
3997
Evan Chengff89dcb2009-10-18 18:16:27 +00003998 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003999
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004000 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004001 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004002 // FIXME: Volatile isn't really correct; we should keep track of atomic
4003 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004004 unsigned Flags = MachineMemOperand::MOVolatile;
4005 if (Opcode != ISD::ATOMIC_STORE)
4006 Flags |= MachineMemOperand::MOLoad;
4007 if (Opcode != ISD::ATOMIC_LOAD)
4008 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004009
4010 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004011 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004012
Eli Friedman55ba8162011-07-29 03:05:32 +00004013 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4014 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004015}
4016
4017SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4018 SDValue Chain,
4019 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004020 SDValue Swp, MachineMemOperand *MMO,
4021 AtomicOrdering Ordering,
4022 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004023 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4024 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4025
Owen Andersone50ed302009-08-10 22:56:29 +00004026 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004027
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004029 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004030 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004031 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
4032 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004033 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004034 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004035 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4036 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004037 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004038 }
Dan Gohman95531882010-03-18 18:49:47 +00004039 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004040 Ptr, Cmp, Swp, MMO, Ordering,
4041 SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004042 CSEMap.InsertNode(N, IP);
4043 AllNodes.push_back(N);
4044 return SDValue(N, 0);
4045}
4046
Owen Andersone50ed302009-08-10 22:56:29 +00004047SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004048 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004049 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004050 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004051 unsigned Alignment,
4052 AtomicOrdering Ordering,
4053 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004054 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4055 Alignment = getEVTAlignment(MemVT);
4056
Evan Chengff89dcb2009-10-18 18:16:27 +00004057 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004058 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004059 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004060 // For now, atomics are considered to be volatile always, and they are
4061 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004062 // FIXME: Volatile isn't really correct; we should keep track of atomic
4063 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004064 unsigned Flags = MachineMemOperand::MOVolatile;
4065 if (Opcode != ISD::ATOMIC_STORE)
4066 Flags |= MachineMemOperand::MOLoad;
4067 if (Opcode != ISD::ATOMIC_LOAD)
4068 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004069
4070 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004071 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004072 MemVT.getStoreSize(), Alignment);
4073
Eli Friedman55ba8162011-07-29 03:05:32 +00004074 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4075 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004076}
4077
4078SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4079 SDValue Chain,
4080 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004081 MachineMemOperand *MMO,
4082 AtomicOrdering Ordering,
4083 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004084 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4085 Opcode == ISD::ATOMIC_LOAD_SUB ||
4086 Opcode == ISD::ATOMIC_LOAD_AND ||
4087 Opcode == ISD::ATOMIC_LOAD_OR ||
4088 Opcode == ISD::ATOMIC_LOAD_XOR ||
4089 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004090 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004091 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004092 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004093 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004094 Opcode == ISD::ATOMIC_SWAP ||
4095 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004096 "Invalid Atomic Op");
4097
Owen Andersone50ed302009-08-10 22:56:29 +00004098 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004099
Eli Friedman327236c2011-08-24 20:50:09 +00004100 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4101 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004102 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00004103 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00004104 SDValue Ops[] = {Chain, Ptr, Val};
4105 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004106 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004107 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004108 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4109 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004110 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004111 }
Dan Gohman95531882010-03-18 18:49:47 +00004112 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
Eli Friedman55ba8162011-07-29 03:05:32 +00004113 Ptr, Val, MMO,
4114 Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004115 CSEMap.InsertNode(N, IP);
4116 AllNodes.push_back(N);
4117 return SDValue(N, 0);
4118}
4119
Eli Friedman327236c2011-08-24 20:50:09 +00004120SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4121 EVT VT, SDValue Chain,
4122 SDValue Ptr,
4123 const Value* PtrVal,
4124 unsigned Alignment,
4125 AtomicOrdering Ordering,
4126 SynchronizationScope SynchScope) {
4127 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4128 Alignment = getEVTAlignment(MemVT);
4129
4130 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004131 // An atomic store does not load. An atomic load does not store.
4132 // (An atomicrmw obviously both loads and stores.)
4133 // For now, atomics are considered to be volatile always, and they are
4134 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004135 // FIXME: Volatile isn't really correct; we should keep track of atomic
4136 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004137 unsigned Flags = MachineMemOperand::MOVolatile;
4138 if (Opcode != ISD::ATOMIC_STORE)
4139 Flags |= MachineMemOperand::MOLoad;
4140 if (Opcode != ISD::ATOMIC_LOAD)
4141 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004142
4143 MachineMemOperand *MMO =
4144 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4145 MemVT.getStoreSize(), Alignment);
4146
4147 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4148 Ordering, SynchScope);
4149}
4150
4151SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
4152 EVT VT, SDValue Chain,
4153 SDValue Ptr,
4154 MachineMemOperand *MMO,
4155 AtomicOrdering Ordering,
4156 SynchronizationScope SynchScope) {
4157 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4158
4159 SDVTList VTs = getVTList(VT, MVT::Other);
4160 FoldingSetNodeID ID;
4161 ID.AddInteger(MemVT.getRawBits());
4162 SDValue Ops[] = {Chain, Ptr};
4163 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004164 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Eli Friedman327236c2011-08-24 20:50:09 +00004165 void* IP = 0;
4166 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4167 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4168 return SDValue(E, 0);
4169 }
4170 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
4171 Ptr, MMO, Ordering, SynchScope);
4172 CSEMap.InsertNode(N, IP);
4173 AllNodes.push_back(N);
4174 return SDValue(N, 0);
4175}
4176
Duncan Sands4bdcb612008-07-02 17:40:58 +00004177/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004178SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
4179 DebugLoc dl) {
4180 if (NumOps == 1)
4181 return Ops[0];
4182
Owen Andersone50ed302009-08-10 22:56:29 +00004183 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004184 VTs.reserve(NumOps);
4185 for (unsigned i = 0; i < NumOps; ++i)
4186 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004187 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004188 Ops, NumOps);
4189}
4190
Dan Gohman475871a2008-07-27 21:46:04 +00004191SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004192SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004193 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004194 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) {
4198 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004199 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004200 ReadMem, WriteMem);
4201}
4202
4203SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004204SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4205 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004206 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004207 unsigned Align, bool Vol,
4208 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004209 if (Align == 0) // Ensure that codegen never sees alignment 0
4210 Align = getEVTAlignment(MemVT);
4211
4212 MachineFunction &MF = getMachineFunction();
4213 unsigned Flags = 0;
4214 if (WriteMem)
4215 Flags |= MachineMemOperand::MOStore;
4216 if (ReadMem)
4217 Flags |= MachineMemOperand::MOLoad;
4218 if (Vol)
4219 Flags |= MachineMemOperand::MOVolatile;
4220 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004221 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004222
4223 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4224}
4225
4226SDValue
4227SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
4228 const SDValue *Ops, unsigned NumOps,
4229 EVT MemVT, MachineMemOperand *MMO) {
4230 assert((Opcode == ISD::INTRINSIC_VOID ||
4231 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004232 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004233 Opcode == ISD::LIFETIME_START ||
4234 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004235 (Opcode <= INT_MAX &&
4236 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4237 "Opcode is not a memory-accessing opcode!");
4238
Dale Johannesene8c17332009-01-29 00:47:48 +00004239 // Memoize the node unless it returns a flag.
4240 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004241 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004242 FoldingSetNodeID ID;
4243 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004244 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004245 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004246 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4247 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004248 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004250
Dan Gohman95531882010-03-18 18:49:47 +00004251 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4252 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004253 CSEMap.InsertNode(N, IP);
4254 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004255 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
4256 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004257 }
4258 AllNodes.push_back(N);
4259 return SDValue(N, 0);
4260}
4261
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004262/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4263/// MachinePointerInfo record from it. This is particularly useful because the
4264/// code generator has many cases where it doesn't bother passing in a
4265/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4266static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4267 // If this is FI+Offset, we can model it.
4268 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4269 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4270
4271 // If this is (FI+Offset1)+Offset2, we can model it.
4272 if (Ptr.getOpcode() != ISD::ADD ||
4273 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4274 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4275 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004276
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004277 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4278 return MachinePointerInfo::getFixedStack(FI, Offset+
4279 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4280}
4281
4282/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4283/// MachinePointerInfo record from it. This is particularly useful because the
4284/// code generator has many cases where it doesn't bother passing in a
4285/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4286static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4287 // If the 'Offset' value isn't a constant, we can't handle this.
4288 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4289 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4290 if (OffsetOp.getOpcode() == ISD::UNDEF)
4291 return InferPointerInfo(Ptr);
4292 return MachinePointerInfo();
4293}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004294
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004295
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004296SDValue
4297SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
4298 EVT VT, DebugLoc dl, SDValue Chain,
4299 SDValue Ptr, SDValue Offset,
4300 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004301 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004302 unsigned Alignment, const MDNode *TBAAInfo,
4303 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004304 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004305 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004306 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4307 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004308
Dan Gohmanc76909a2009-09-25 20:36:54 +00004309 unsigned Flags = MachineMemOperand::MOLoad;
4310 if (isVolatile)
4311 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004312 if (isNonTemporal)
4313 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004314 if (isInvariant)
4315 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004316
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004317 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4318 // clients.
4319 if (PtrInfo.V == 0)
4320 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004321
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004322 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004323 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004324 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004325 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004326 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004327}
4328
4329SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004330SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Evan Chengbcc80172010-07-07 22:15:37 +00004331 EVT VT, DebugLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004332 SDValue Ptr, SDValue Offset, EVT MemVT,
4333 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004334 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004335 ExtType = ISD::NON_EXTLOAD;
4336 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004337 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004338 } else {
4339 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004340 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4341 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004342 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004343 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004344 assert(VT.isVector() == MemVT.isVector() &&
4345 "Cannot use trunc store to convert to or from a vector!");
4346 assert((!VT.isVector() ||
4347 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4348 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004349 }
4350
4351 bool Indexed = AM != ISD::UNINDEXED;
4352 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4353 "Unindexed load with an offset!");
4354
4355 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004356 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004357 SDValue Ops[] = { Chain, Ptr, Offset };
4358 FoldingSetNodeID ID;
4359 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004360 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004361 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004362 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004363 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004364 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004365 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004366 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4367 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004368 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004369 }
Dan Gohman95531882010-03-18 18:49:47 +00004370 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
4371 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004372 CSEMap.InsertNode(N, IP);
4373 AllNodes.push_back(N);
4374 return SDValue(N, 0);
4375}
4376
Owen Andersone50ed302009-08-10 22:56:29 +00004377SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004378 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004379 MachinePointerInfo PtrInfo,
4380 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004381 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004382 const MDNode *TBAAInfo,
4383 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004384 SDValue Undef = getUNDEF(Ptr.getValueType());
4385 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004386 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4387 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004388}
Dale Johannesene8c17332009-01-29 00:47:48 +00004389
Stuart Hastingsa9011292011-02-16 16:23:55 +00004390SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004391 SDValue Chain, SDValue Ptr,
4392 MachinePointerInfo PtrInfo, EVT MemVT,
4393 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004394 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004395 SDValue Undef = getUNDEF(Ptr.getValueType());
4396 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004397 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004398 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004399}
4400
4401
Dan Gohman475871a2008-07-27 21:46:04 +00004402SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004403SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
4404 SDValue Offset, ISD::MemIndexedMode AM) {
4405 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4406 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4407 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004408 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004409 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004410 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004411 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004412}
4413
Dale Johannesene8c17332009-01-29 00:47:48 +00004414SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004415 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004416 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004417 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004418 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004419 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004420 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004421 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004422
Dan Gohmanc76909a2009-09-25 20:36:54 +00004423 unsigned Flags = MachineMemOperand::MOStore;
4424 if (isVolatile)
4425 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004426 if (isNonTemporal)
4427 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004428
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004429 if (PtrInfo.V == 0)
4430 PtrInfo = InferPointerInfo(Ptr);
4431
4432 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004433 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004434 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004435 Val.getValueType().getStoreSize(), Alignment,
4436 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004437
4438 return getStore(Chain, dl, Val, Ptr, MMO);
4439}
4440
4441SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
4442 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004443 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004444 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004445 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004446 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004447 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004448 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4449 FoldingSetNodeID ID;
4450 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004451 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004452 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004453 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004454 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004455 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004456 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4457 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004458 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004459 }
Dan Gohman95531882010-03-18 18:49:47 +00004460 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4461 false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004462 CSEMap.InsertNode(N, IP);
4463 AllNodes.push_back(N);
4464 return SDValue(N, 0);
4465}
4466
Dale Johannesene8c17332009-01-29 00:47:48 +00004467SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004468 SDValue Ptr, MachinePointerInfo PtrInfo,
4469 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004470 unsigned Alignment,
4471 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004472 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004473 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004474 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4475 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004476
Dan Gohmanc76909a2009-09-25 20:36:54 +00004477 unsigned Flags = MachineMemOperand::MOStore;
4478 if (isVolatile)
4479 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004480 if (isNonTemporal)
4481 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004482
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004483 if (PtrInfo.V == 0)
4484 PtrInfo = InferPointerInfo(Ptr);
4485
4486 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004487 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004488 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4489 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004490
4491 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4492}
4493
4494SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
4495 SDValue Ptr, EVT SVT,
4496 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004497 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004498
Michael Ilseman06b96902012-09-10 16:56:31 +00004499 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004500 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004501 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004502 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004503
Dan Gohman2e141d72009-12-14 23:40:38 +00004504 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4505 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004506 assert(VT.isInteger() == SVT.isInteger() &&
4507 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004508 assert(VT.isVector() == SVT.isVector() &&
4509 "Cannot use trunc store to convert to or from a vector!");
4510 assert((!VT.isVector() ||
4511 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4512 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004513
Owen Anderson825b72b2009-08-11 20:47:22 +00004514 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004515 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004516 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4517 FoldingSetNodeID ID;
4518 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004519 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004520 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004521 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004522 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004523 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004524 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4525 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004526 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004527 }
Dan Gohman95531882010-03-18 18:49:47 +00004528 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
4529 true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004530 CSEMap.InsertNode(N, IP);
4531 AllNodes.push_back(N);
4532 return SDValue(N, 0);
4533}
4534
Dan Gohman475871a2008-07-27 21:46:04 +00004535SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00004536SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
4537 SDValue Offset, ISD::MemIndexedMode AM) {
4538 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4539 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4540 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004541 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004542 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4543 FoldingSetNodeID ID;
4544 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004545 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004546 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004547 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004548 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004549 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004550 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004551
Dan Gohman95531882010-03-18 18:49:47 +00004552 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
4553 ST->isTruncatingStore(),
4554 ST->getMemoryVT(),
4555 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004556 CSEMap.InsertNode(N, IP);
4557 AllNodes.push_back(N);
4558 return SDValue(N, 0);
4559}
4560
Owen Andersone50ed302009-08-10 22:56:29 +00004561SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004562 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004563 SDValue SV,
4564 unsigned Align) {
4565 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4566 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004567}
4568
Owen Andersone50ed302009-08-10 22:56:29 +00004569SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004570 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004571 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004572 case 0: return getNode(Opcode, DL, VT);
4573 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4574 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4575 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004576 default: break;
4577 }
4578
Dan Gohman475871a2008-07-27 21:46:04 +00004579 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004580 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004581 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004582 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004583}
4584
Owen Andersone50ed302009-08-10 22:56:29 +00004585SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004586 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004587 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004588 case 0: return getNode(Opcode, DL, VT);
4589 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4590 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4591 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004592 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004593 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004594
Chris Lattneref847df2005-04-09 03:27:28 +00004595 switch (Opcode) {
4596 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004597 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004598 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004599 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4600 "LHS and RHS of condition must have same type!");
4601 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4602 "True and False arms of SelectCC must have same type!");
4603 assert(Ops[2].getValueType() == VT &&
4604 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004605 break;
4606 }
4607 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004608 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004609 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4610 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004611 break;
4612 }
Chris Lattneref847df2005-04-09 03:27:28 +00004613 }
4614
Chris Lattner385328c2005-05-14 07:42:29 +00004615 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004616 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004617 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004618
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004619 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004620 FoldingSetNodeID ID;
4621 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004622 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004623
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004625 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004626
Dan Gohman95531882010-03-18 18:49:47 +00004627 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004628 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004629 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004630 N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004631 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004632
Chris Lattneref847df2005-04-09 03:27:28 +00004633 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004634#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004635 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004636#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004637 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004638}
4639
Bill Wendling7ade28c2009-01-28 22:17:52 +00004640SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004641 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004642 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004643 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004644 Ops, NumOps);
4645}
4646
Bill Wendling7ade28c2009-01-28 22:17:52 +00004647SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004648 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004649 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004650 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004651 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4652 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004653}
4654
Bill Wendling7ade28c2009-01-28 22:17:52 +00004655SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4656 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004657 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004658 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004659
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004660#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004661 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004662 // FIXME: figure out how to safely handle things like
4663 // int foo(int x) { return 1 << (x & 255); }
4664 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004665 case ISD::SRA_PARTS:
4666 case ISD::SRL_PARTS:
4667 case ISD::SHL_PARTS:
4668 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004669 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004670 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004671 else if (N3.getOpcode() == ISD::AND)
4672 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4673 // If the and is only masking out bits that cannot effect the shift,
4674 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004675 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004676 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004677 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004678 }
4679 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004680 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004681#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004682
Chris Lattner43247a12005-08-25 19:12:10 +00004683 // Memoize the node unless it returns a flag.
4684 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004685 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004686 FoldingSetNodeID ID;
4687 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004688 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004689 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004690 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004691
Dan Gohman0e5f1302008-07-07 23:02:41 +00004692 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004693 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004694 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004695 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004696 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004697 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4698 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004699 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004700 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004701 }
Chris Lattnera5682852006-08-07 23:03:03 +00004702 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004703 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004704 if (NumOps == 1) {
Dan Gohman95531882010-03-18 18:49:47 +00004705 N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004706 } else if (NumOps == 2) {
Dan Gohman95531882010-03-18 18:49:47 +00004707 N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004708 } else if (NumOps == 3) {
Dan Gohman95531882010-03-18 18:49:47 +00004709 N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
4710 Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004711 } else {
Dan Gohman95531882010-03-18 18:49:47 +00004712 N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004713 }
Chris Lattner43247a12005-08-25 19:12:10 +00004714 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004715 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004716#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004717 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004718#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004719 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004720}
4721
Bill Wendling7ade28c2009-01-28 22:17:52 +00004722SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4723 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004724}
4725
Bill Wendling7ade28c2009-01-28 22:17:52 +00004726SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4727 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004728 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004729 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004730}
4731
Bill Wendling7ade28c2009-01-28 22:17:52 +00004732SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4733 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004734 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004735 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004736}
4737
Bill Wendling7ade28c2009-01-28 22:17:52 +00004738SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4739 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004740 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004741 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004742}
4743
Bill Wendling7ade28c2009-01-28 22:17:52 +00004744SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4745 SDValue N1, SDValue N2, SDValue N3,
4746 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004747 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004748 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004749}
4750
Bill Wendling7ade28c2009-01-28 22:17:52 +00004751SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4752 SDValue N1, SDValue N2, SDValue N3,
4753 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004754 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004755 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004756}
4757
Owen Andersone50ed302009-08-10 22:56:29 +00004758SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004759 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004760}
4761
Owen Andersone50ed302009-08-10 22:56:29 +00004762SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004763 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4764 E = VTList.rend(); I != E; ++I)
4765 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4766 return *I;
4767
Owen Andersone50ed302009-08-10 22:56:29 +00004768 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004769 Array[0] = VT1;
4770 Array[1] = VT2;
4771 SDVTList Result = makeVTList(Array, 2);
4772 VTList.push_back(Result);
4773 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004774}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004775
Owen Andersone50ed302009-08-10 22:56:29 +00004776SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004777 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4778 E = VTList.rend(); I != E; ++I)
4779 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4780 I->VTs[2] == VT3)
4781 return *I;
4782
Owen Andersone50ed302009-08-10 22:56:29 +00004783 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004784 Array[0] = VT1;
4785 Array[1] = VT2;
4786 Array[2] = VT3;
4787 SDVTList Result = makeVTList(Array, 3);
4788 VTList.push_back(Result);
4789 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004790}
4791
Owen Andersone50ed302009-08-10 22:56:29 +00004792SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004793 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4794 E = VTList.rend(); I != E; ++I)
4795 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4796 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4797 return *I;
4798
Anton Korobeynikov60283f92009-12-13 01:00:59 +00004799 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling13d6d442008-12-01 23:28:22 +00004800 Array[0] = VT1;
4801 Array[1] = VT2;
4802 Array[2] = VT3;
4803 Array[3] = VT4;
4804 SDVTList Result = makeVTList(Array, 4);
4805 VTList.push_back(Result);
4806 return Result;
4807}
4808
Owen Andersone50ed302009-08-10 22:56:29 +00004809SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004810 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004811 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004812 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004813 case 2: return getVTList(VTs[0], VTs[1]);
4814 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikovcc62c3c2009-12-19 02:04:00 +00004815 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattner70046e92006-08-15 17:46:01 +00004816 default: break;
4817 }
4818
Dan Gohmane8be6c62008-07-17 19:10:17 +00004819 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4820 E = VTList.rend(); I != E; ++I) {
4821 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4822 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004823
Benjamin Kramerb26e2912012-07-19 10:46:05 +00004824 if (std::equal(&VTs[2], &VTs[NumVTs], &I->VTs[2]))
Dan Gohmane8be6c62008-07-17 19:10:17 +00004825 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004827
Owen Andersone50ed302009-08-10 22:56:29 +00004828 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004829 std::copy(VTs, VTs+NumVTs, Array);
4830 SDVTList Result = makeVTList(Array, NumVTs);
4831 VTList.push_back(Result);
4832 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004833}
4834
4835
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004836/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4837/// specified operands. If the resultant node already exists in the DAG,
4838/// this does not modify the specified node, instead it returns the node that
4839/// already exists. If the resultant node does not exist in the DAG, the
4840/// input node is returned. As a degenerate case, if you specify the same
4841/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00004842SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004843 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004844
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004845 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00004846 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004847
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004848 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004849 void *InsertPos = 0;
4850 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004851 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004852
Dan Gohman79acd2b2008-07-21 22:38:59 +00004853 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004854 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004855 if (!RemoveNodeFromCSEMaps(N))
4856 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004857
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004858 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004859 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004860
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004861 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004862 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004863 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004864}
4865
Dan Gohman027657d2010-06-18 15:30:29 +00004866SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004867 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004868
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004869 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004870 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00004871 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004872
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004873 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004874 void *InsertPos = 0;
4875 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004876 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004877
Dan Gohman79acd2b2008-07-21 22:38:59 +00004878 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004879 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004880 if (!RemoveNodeFromCSEMaps(N))
4881 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004882
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004883 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004884 if (N->OperandList[0] != Op1)
4885 N->OperandList[0].set(Op1);
4886 if (N->OperandList[1] != Op2)
4887 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004888
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004889 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004890 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004891 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004892}
4893
Dan Gohman027657d2010-06-18 15:30:29 +00004894SDNode *SelectionDAG::
4895UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004896 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004897 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004898}
4899
Dan Gohman027657d2010-06-18 15:30:29 +00004900SDNode *SelectionDAG::
4901UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004902 SDValue Op3, SDValue Op4) {
4903 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004904 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004905}
4906
Dan Gohman027657d2010-06-18 15:30:29 +00004907SDNode *SelectionDAG::
4908UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004909 SDValue Op3, SDValue Op4, SDValue Op5) {
4910 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004911 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004912}
4913
Dan Gohman027657d2010-06-18 15:30:29 +00004914SDNode *SelectionDAG::
4915UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004916 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004917 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004918
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004919 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004920 bool AnyChange = false;
4921 for (unsigned i = 0; i != NumOps; ++i) {
4922 if (Ops[i] != N->getOperand(i)) {
4923 AnyChange = true;
4924 break;
4925 }
4926 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004927
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004928 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00004929 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00004930
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004931 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004932 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004933 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00004934 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00004935
Dan Gohman7ceda162008-05-02 00:05:03 +00004936 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004937 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004938 if (!RemoveNodeFromCSEMaps(N))
4939 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004940
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004941 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004942 for (unsigned i = 0; i != NumOps; ++i)
4943 if (N->OperandList[i] != Ops[i])
4944 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004945
4946 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004947 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00004948 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004949}
4950
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004951/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004952/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004953void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004954 // Unlike the code in MorphNodeTo that does this, we don't need to
4955 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004956 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4957 SDUse &Use = *I++;
4958 Use.set(SDValue());
4959 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004960}
Chris Lattner149c58c2005-08-16 18:17:10 +00004961
Dan Gohmane8be6c62008-07-17 19:10:17 +00004962/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4963/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004964///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004965SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004966 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004967 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004968 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004969}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004970
Dan Gohmane8be6c62008-07-17 19:10:17 +00004971SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004972 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004973 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004974 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004975 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004976}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004977
Dan Gohmane8be6c62008-07-17 19:10:17 +00004978SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004979 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004980 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004981 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004982 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004983 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004984}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004985
Dan Gohmane8be6c62008-07-17 19:10:17 +00004986SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004987 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004988 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004989 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004990 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004991 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004992}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004993
Dan Gohmane8be6c62008-07-17 19:10:17 +00004994SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004995 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004996 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004997 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004998 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004999}
5000
Dan Gohmane8be6c62008-07-17 19:10:17 +00005001SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005002 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005003 unsigned NumOps) {
5004 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005005 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005006}
5007
Dan Gohmane8be6c62008-07-17 19:10:17 +00005008SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005009 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005010 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005011 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00005012}
5013
Dan Gohmane8be6c62008-07-17 19:10:17 +00005014SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005015 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00005016 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005017 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005018 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005019}
5020
Bill Wendling13d6d442008-12-01 23:28:22 +00005021SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005022 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00005023 const SDValue *Ops, unsigned NumOps) {
5024 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5025 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5026}
5027
Scott Michelfdc40a02009-02-17 22:15:04 +00005028SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005029 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005030 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005031 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005032 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005033 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005034}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005035
Scott Michelfdc40a02009-02-17 22:15:04 +00005036SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005037 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005038 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005039 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005040 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005041 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005042}
5043
Dan Gohmane8be6c62008-07-17 19:10:17 +00005044SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005045 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005046 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005047 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005048 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005049 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005050 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00005051}
5052
Dan Gohmane8be6c62008-07-17 19:10:17 +00005053SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005054 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005055 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005056 SDValue Op3) {
5057 SDVTList VTs = getVTList(VT1, VT2, VT3);
5058 SDValue Ops[] = { Op1, Op2, Op3 };
5059 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5060}
5061
5062SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00005063 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005064 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00005065 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5066 // Reset the NodeID to -1.
5067 N->setNodeId(-1);
5068 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005069}
5070
Devang Patel0508d042011-12-15 18:21:18 +00005071/// UpdadeDebugLocOnMergedSDNode - If the opt level is -O0 then it throws away
5072/// the line number information on the merged node since it is not possible to
5073/// preserve the information that operation is associated with multiple lines.
5074/// This will make the debugger working better at -O0, were there is a higher
5075/// probability having other instructions associated with that line.
5076///
5077SDNode *SelectionDAG::UpdadeDebugLocOnMergedSDNode(SDNode *N, DebugLoc OLoc) {
5078 DebugLoc NLoc = N->getDebugLoc();
5079 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) && (OLoc != NLoc)) {
5080 N->setDebugLoc(DebugLoc());
5081 }
5082 return N;
5083}
5084
Chris Lattner21221e32010-02-28 21:36:14 +00005085/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005086/// return type, opcode, and operands.
5087///
5088/// Note that MorphNodeTo returns the resultant node. If there is already a
5089/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00005090/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005091///
5092/// Using MorphNodeTo is faster than creating a new node and swapping it in
5093/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005094/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005095/// the node's users.
5096///
5097SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005098 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005099 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005100 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005101 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005102 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005103 FoldingSetNodeID ID;
5104 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005105 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Devang Patel0508d042011-12-15 18:21:18 +00005106 return UpdadeDebugLocOnMergedSDNode(ON, N->getDebugLoc());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005107 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005108
Dan Gohman095cc292008-09-13 01:54:27 +00005109 if (!RemoveNodeFromCSEMaps(N))
5110 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005111
Dan Gohmane8be6c62008-07-17 19:10:17 +00005112 // Start the morphing.
5113 N->NodeType = Opc;
5114 N->ValueList = VTs.VTs;
5115 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005116
Dan Gohmane8be6c62008-07-17 19:10:17 +00005117 // Clear the operands list, updating used nodes to remove this from their
5118 // use list. Keep track of any operands that become dead as a result.
5119 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005120 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5121 SDUse &Use = *I++;
5122 SDNode *Used = Use.getNode();
5123 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005124 if (Used->use_empty())
5125 DeadNodeSet.insert(Used);
5126 }
5127
Dan Gohmanc76909a2009-09-25 20:36:54 +00005128 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5129 // Initialize the memory references information.
5130 MN->setMemRefs(0, 0);
5131 // If NumOps is larger than the # of operands we can have in a
5132 // MachineSDNode, reallocate the operand list.
5133 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5134 if (MN->OperandsNeedDelete)
5135 delete[] MN->OperandList;
5136 if (NumOps > array_lengthof(MN->LocalOperands))
5137 // We're creating a final node that will live unmorphed for the
5138 // remainder of the current SelectionDAG iteration, so we can allocate
5139 // the operands directly out of a pool with no recycling metadata.
5140 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005141 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005142 else
5143 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5144 MN->OperandsNeedDelete = false;
5145 } else
5146 MN->InitOperands(MN->OperandList, Ops, NumOps);
5147 } else {
5148 // If NumOps is larger than the # of operands we currently have, reallocate
5149 // the operand list.
5150 if (NumOps > N->NumOperands) {
5151 if (N->OperandsNeedDelete)
5152 delete[] N->OperandList;
5153 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005154 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005155 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005156 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005157 }
5158
5159 // Delete any nodes that are still dead after adding the uses for the
5160 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005161 if (!DeadNodeSet.empty()) {
5162 SmallVector<SDNode *, 16> DeadNodes;
5163 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5164 E = DeadNodeSet.end(); I != E; ++I)
5165 if ((*I)->use_empty())
5166 DeadNodes.push_back(*I);
5167 RemoveDeadNodes(DeadNodes);
5168 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005169
Dan Gohmane8be6c62008-07-17 19:10:17 +00005170 if (IP)
5171 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005172 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005173}
5174
Chris Lattner0fb094f2005-11-19 01:44:53 +00005175
Dan Gohman602b0c82009-09-25 18:54:59 +00005176/// getMachineNode - These are used for target selectors to create a new node
5177/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005178///
Dan Gohman602b0c82009-09-25 18:54:59 +00005179/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005180/// node of the specified opcode and operands, it returns that node instead of
5181/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005182MachineSDNode *
5183SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005184 SDVTList VTs = getVTList(VT);
5185 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005186}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005187
Dan Gohmanc81b7832009-10-10 01:29:16 +00005188MachineSDNode *
5189SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005190 SDVTList VTs = getVTList(VT);
5191 SDValue Ops[] = { Op1 };
5192 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005193}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005194
Dan Gohmanc81b7832009-10-10 01:29:16 +00005195MachineSDNode *
5196SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5197 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005198 SDVTList VTs = getVTList(VT);
5199 SDValue Ops[] = { Op1, Op2 };
5200 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005201}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005202
Dan Gohmanc81b7832009-10-10 01:29:16 +00005203MachineSDNode *
5204SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5205 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005206 SDVTList VTs = getVTList(VT);
5207 SDValue Ops[] = { Op1, Op2, Op3 };
5208 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005209}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005210
Dan Gohmanc81b7832009-10-10 01:29:16 +00005211MachineSDNode *
5212SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
5213 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005214 SDVTList VTs = getVTList(VT);
5215 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005216}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005217
Dan Gohmanc81b7832009-10-10 01:29:16 +00005218MachineSDNode *
5219SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005220 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005221 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00005222}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005223
Dan Gohmanc81b7832009-10-10 01:29:16 +00005224MachineSDNode *
5225SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5226 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005227 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005228 SDValue Ops[] = { Op1 };
5229 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005230}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005231
Dan Gohmanc81b7832009-10-10 01:29:16 +00005232MachineSDNode *
5233SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5234 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005235 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005236 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005237 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005238}
5239
Dan Gohmanc81b7832009-10-10 01:29:16 +00005240MachineSDNode *
5241SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5242 EVT VT1, EVT VT2, SDValue Op1,
5243 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005244 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005245 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005246 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00005247}
5248
Dan Gohmanc81b7832009-10-10 01:29:16 +00005249MachineSDNode *
5250SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5251 EVT VT1, EVT VT2,
5252 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005253 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005254 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005255}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005256
Dan Gohmanc81b7832009-10-10 01:29:16 +00005257MachineSDNode *
5258SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5259 EVT VT1, EVT VT2, EVT VT3,
5260 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005261 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005262 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005263 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00005264}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005265
Dan Gohmanc81b7832009-10-10 01:29:16 +00005266MachineSDNode *
5267SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5268 EVT VT1, EVT VT2, EVT VT3,
5269 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005270 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005271 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005272 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00005273}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005274
Dan Gohmanc81b7832009-10-10 01:29:16 +00005275MachineSDNode *
5276SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5277 EVT VT1, EVT VT2, EVT VT3,
5278 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005279 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005280 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00005281}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005282
Dan Gohmanc81b7832009-10-10 01:29:16 +00005283MachineSDNode *
5284SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
5285 EVT VT2, EVT VT3, EVT VT4,
5286 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005287 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005288 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005289}
5290
Dan Gohmanc81b7832009-10-10 01:29:16 +00005291MachineSDNode *
5292SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
5293 const std::vector<EVT> &ResultTys,
5294 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005295 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
5296 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
5297}
5298
Dan Gohmanc81b7832009-10-10 01:29:16 +00005299MachineSDNode *
5300SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
5301 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005302 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005303 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005304 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005305
5306 if (DoCSE) {
5307 FoldingSetNodeID ID;
5308 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5309 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005310 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
5311 return cast<MachineSDNode>(UpdadeDebugLocOnMergedSDNode(E, DL));
5312 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005313 }
5314
5315 // Allocate a new MachineSDNode.
Dan Gohman95531882010-03-18 18:49:47 +00005316 N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005317
5318 // Initialize the operands list.
5319 if (NumOps > array_lengthof(N->LocalOperands))
5320 // We're creating a final node that will live unmorphed for the
5321 // remainder of the current SelectionDAG iteration, so we can allocate
5322 // the operands directly out of a pool with no recycling metadata.
5323 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5324 Ops, NumOps);
5325 else
5326 N->InitOperands(N->LocalOperands, Ops, NumOps);
5327 N->OperandsNeedDelete = false;
5328
5329 if (DoCSE)
5330 CSEMap.InsertNode(N, IP);
5331
5332 AllNodes.push_back(N);
5333#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005334 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005335#endif
5336 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005337}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005338
Dan Gohman6a402dc2009-08-19 18:16:17 +00005339/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005340/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005341SDValue
5342SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
5343 SDValue Operand) {
5344 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005345 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005346 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005347 return SDValue(Subreg, 0);
5348}
5349
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005350/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005351/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005352SDValue
5353SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
5354 SDValue Operand, SDValue Subreg) {
5355 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005356 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005357 VT, Operand, Subreg, SRIdxVal);
5358 return SDValue(Result, 0);
5359}
5360
Evan Cheng08b11732008-03-22 01:55:50 +00005361/// getNodeIfExists - Get the specified node if it's already available, or
5362/// else return NULL.
5363SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005364 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005365 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005366 FoldingSetNodeID ID;
5367 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5368 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005369 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005370 return E;
5371 }
5372 return NULL;
5373}
5374
Evan Cheng31441b72010-03-29 20:48:30 +00005375/// getDbgValue - Creates a SDDbgValue node.
5376///
5377SDDbgValue *
5378SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5379 DebugLoc DL, unsigned O) {
5380 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5381}
5382
5383SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005384SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005385 DebugLoc DL, unsigned O) {
5386 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5387}
5388
5389SDDbgValue *
5390SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5391 DebugLoc DL, unsigned O) {
5392 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5393}
5394
Dan Gohmana72d2a22010-03-03 21:33:37 +00005395namespace {
5396
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005397/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005398/// pointed to by a use iterator is deleted, increment the use iterator
5399/// so that it doesn't dangle.
5400///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005401class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005402 SDNode::use_iterator &UI;
5403 SDNode::use_iterator &UE;
5404
5405 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5406 // Increment the iterator as needed.
5407 while (UI != UE && N == *UI)
5408 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005409 }
5410
5411public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005412 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005413 SDNode::use_iterator &ui,
5414 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005415 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005416};
5417
5418}
5419
Evan Cheng99157a02006-08-07 22:13:29 +00005420/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005421/// This can cause recursive merging of nodes in the DAG.
5422///
Chris Lattner11d049c2008-02-03 03:35:22 +00005423/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005424///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005425void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005426 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005427 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005428 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005429 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005430
Dan Gohman39946102009-01-25 16:29:12 +00005431 // Iterate over all the existing uses of From. New uses will be added
5432 // to the beginning of the use list, which we avoid visiting.
5433 // This specifically avoids visiting uses of From that arise while the
5434 // replacement is happening, because any such uses would be the result
5435 // of CSE: If an existing node looks like From after one of its operands
5436 // is replaced by To, we don't want to replace of all its users with To
5437 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005438 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005439 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005440 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005441 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005442
Chris Lattner8b8749f2005-08-17 19:00:20 +00005443 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005444 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005445
Dan Gohman39946102009-01-25 16:29:12 +00005446 // A user can appear in a use list multiple times, and when this
5447 // happens the uses are usually next to each other in the list.
5448 // To help reduce the number of CSE recomputations, process all
5449 // the uses of this user that we can find this way.
5450 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005451 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005452 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005453 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005454 } while (UI != UE && *UI == User);
5455
5456 // Now that we have modified User, add it back to the CSE maps. If it
5457 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005458 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005459 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005460
5461 // If we just RAUW'd the root, take note.
5462 if (FromN == getRoot())
5463 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005464}
5465
5466/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5467/// This can cause recursive merging of nodes in the DAG.
5468///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005469/// This version assumes that for each value of From, there is a
5470/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005471///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005472void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005473#ifndef NDEBUG
5474 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5475 assert((!From->hasAnyUseOfValue(i) ||
5476 From->getValueType(i) == To->getValueType(i)) &&
5477 "Cannot use this version of ReplaceAllUsesWith!");
5478#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005479
5480 // Handle the trivial case.
5481 if (From == To)
5482 return;
5483
Dan Gohmandbe664a2009-01-19 21:44:21 +00005484 // Iterate over just the existing users of From. See the comments in
5485 // the ReplaceAllUsesWith above.
5486 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005487 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005488 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005489 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005490
Chris Lattner8b52f212005-08-26 18:36:28 +00005491 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005492 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005493
Dan Gohman39946102009-01-25 16:29:12 +00005494 // A user can appear in a use list multiple times, and when this
5495 // happens the uses are usually next to each other in the list.
5496 // To help reduce the number of CSE recomputations, process all
5497 // the uses of this user that we can find this way.
5498 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005499 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005500 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005501 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005502 } while (UI != UE && *UI == User);
5503
5504 // Now that we have modified User, add it back to the CSE maps. If it
5505 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005506 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005507 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005508
5509 // If we just RAUW'd the root, take note.
5510 if (From == getRoot().getNode())
5511 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005512}
5513
Chris Lattner8b52f212005-08-26 18:36:28 +00005514/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5515/// This can cause recursive merging of nodes in the DAG.
5516///
5517/// This version can replace From with any result values. To must match the
5518/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005519void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005520 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005521 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005522
Dan Gohmandbe664a2009-01-19 21:44:21 +00005523 // Iterate over just the existing users of From. See the comments in
5524 // the ReplaceAllUsesWith above.
5525 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005526 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005527 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005528 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005529
Chris Lattner7b2880c2005-08-24 22:44:39 +00005530 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005531 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005532
Dan Gohman39946102009-01-25 16:29:12 +00005533 // A user can appear in a use list multiple times, and when this
5534 // happens the uses are usually next to each other in the list.
5535 // To help reduce the number of CSE recomputations, process all
5536 // the uses of this user that we can find this way.
5537 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005538 SDUse &Use = UI.getUse();
5539 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005540 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005541 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005542 } while (UI != UE && *UI == User);
5543
5544 // Now that we have modified User, add it back to the CSE maps. If it
5545 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005546 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005547 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005548
5549 // If we just RAUW'd the root, take note.
5550 if (From == getRoot().getNode())
5551 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005552}
5553
Chris Lattner012f2412006-02-17 21:58:01 +00005554/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005555/// uses of other values produced by From.getNode() alone. The Deleted
5556/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005557void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005558 // Handle the really simple, really trivial case efficiently.
5559 if (From == To) return;
5560
Chris Lattner012f2412006-02-17 21:58:01 +00005561 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005562 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005563 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005564 return;
5565 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005566
Dan Gohman39946102009-01-25 16:29:12 +00005567 // Iterate over just the existing users of From. See the comments in
5568 // the ReplaceAllUsesWith above.
5569 SDNode::use_iterator UI = From.getNode()->use_begin(),
5570 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005571 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005572 while (UI != UE) {
5573 SDNode *User = *UI;
5574 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005575
Dan Gohman39946102009-01-25 16:29:12 +00005576 // A user can appear in a use list multiple times, and when this
5577 // happens the uses are usually next to each other in the list.
5578 // To help reduce the number of CSE recomputations, process all
5579 // the uses of this user that we can find this way.
5580 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005581 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005582
5583 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005584 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005585 ++UI;
5586 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005587 }
Dan Gohman39946102009-01-25 16:29:12 +00005588
5589 // If this node hasn't been modified yet, it's still in the CSE maps,
5590 // so remove its old self from the CSE maps.
5591 if (!UserRemovedFromCSEMaps) {
5592 RemoveNodeFromCSEMaps(User);
5593 UserRemovedFromCSEMaps = true;
5594 }
5595
5596 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005597 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005598 } while (UI != UE && *UI == User);
5599
5600 // We are iterating over all uses of the From node, so if a use
5601 // doesn't use the specific value, no changes are made.
5602 if (!UserRemovedFromCSEMaps)
5603 continue;
5604
Chris Lattner01d029b2007-10-15 06:10:22 +00005605 // Now that we have modified User, add it back to the CSE maps. If it
5606 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005607 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005608 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005609
5610 // If we just RAUW'd the root, take note.
5611 if (From == getRoot())
5612 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005613}
5614
Dan Gohman39946102009-01-25 16:29:12 +00005615namespace {
5616 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5617 /// to record information about a use.
5618 struct UseMemo {
5619 SDNode *User;
5620 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005621 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005622 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005623
5624 /// operator< - Sort Memos by User.
5625 bool operator<(const UseMemo &L, const UseMemo &R) {
5626 return (intptr_t)L.User < (intptr_t)R.User;
5627 }
Dan Gohman39946102009-01-25 16:29:12 +00005628}
5629
Dan Gohmane8be6c62008-07-17 19:10:17 +00005630/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005631/// uses of other values produced by From.getNode() alone. The same value
5632/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005633/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005634void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5635 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005636 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005637 // Handle the simple, trivial case efficiently.
5638 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005639 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005640
Dan Gohman39946102009-01-25 16:29:12 +00005641 // Read up all the uses and make records of them. This helps
5642 // processing new uses that are introduced during the
5643 // replacement process.
5644 SmallVector<UseMemo, 4> Uses;
5645 for (unsigned i = 0; i != Num; ++i) {
5646 unsigned FromResNo = From[i].getResNo();
5647 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005648 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005649 E = FromNode->use_end(); UI != E; ++UI) {
5650 SDUse &Use = UI.getUse();
5651 if (Use.getResNo() == FromResNo) {
5652 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005653 Uses.push_back(Memo);
5654 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005655 }
Dan Gohman39946102009-01-25 16:29:12 +00005656 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005657
Dan Gohmane7852d02009-01-26 04:35:06 +00005658 // Sort the uses, so that all the uses from a given User are together.
5659 std::sort(Uses.begin(), Uses.end());
5660
Dan Gohman39946102009-01-25 16:29:12 +00005661 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5662 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005663 // We know that this user uses some value of From. If it is the right
5664 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005665 SDNode *User = Uses[UseIndex].User;
5666
5667 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005668 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005669
Dan Gohmane7852d02009-01-26 04:35:06 +00005670 // The Uses array is sorted, so all the uses for a given User
5671 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005672 // To help reduce the number of CSE recomputations, process all
5673 // the uses of this user that we can find this way.
5674 do {
5675 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005676 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005677 ++UseIndex;
5678
Dan Gohmane7852d02009-01-26 04:35:06 +00005679 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005680 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5681
Dan Gohmane8be6c62008-07-17 19:10:17 +00005682 // Now that we have modified User, add it back to the CSE maps. If it
5683 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005684 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005685 }
5686}
5687
Evan Chenge6f35d82006-08-01 08:20:41 +00005688/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005689/// based on their topological order. It returns the maximum id and a vector
5690/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005691unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005692
Dan Gohmanf06c8352008-09-30 18:30:35 +00005693 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005694
Dan Gohmanf06c8352008-09-30 18:30:35 +00005695 // SortedPos tracks the progress of the algorithm. Nodes before it are
5696 // sorted, nodes after it are unsorted. When the algorithm completes
5697 // it is at the end of the list.
5698 allnodes_iterator SortedPos = allnodes_begin();
5699
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005700 // Visit all the nodes. Move nodes with no operands to the front of
5701 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005702 // operand count. Before we do this, the Node Id fields of the nodes
5703 // may contain arbitrary values. After, the Node Id fields for nodes
5704 // before SortedPos will contain the topological sort index, and the
5705 // Node Id fields for nodes At SortedPos and after will contain the
5706 // count of outstanding operands.
5707 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5708 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005709 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005710 unsigned Degree = N->getNumOperands();
5711 if (Degree == 0) {
5712 // A node with no uses, add it to the result array immediately.
5713 N->setNodeId(DAGSize++);
5714 allnodes_iterator Q = N;
5715 if (Q != SortedPos)
5716 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005717 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005718 ++SortedPos;
5719 } else {
5720 // Temporarily use the Node Id as scratch space for the degree count.
5721 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005722 }
5723 }
5724
Chad Rosierf496dea2012-05-21 17:13:41 +00005725 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005726 // such that by the time the end is reached all nodes will be sorted.
5727 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5728 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005729 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005730 // N is in sorted position, so all its uses have one less operand
5731 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005732 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5733 UI != UE; ++UI) {
5734 SDNode *P = *UI;
5735 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005736 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005737 --Degree;
5738 if (Degree == 0) {
5739 // All of P's operands are sorted, so P may sorted now.
5740 P->setNodeId(DAGSize++);
5741 if (P != SortedPos)
5742 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005743 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005744 ++SortedPos;
5745 } else {
5746 // Update P's outstanding operand count.
5747 P->setNodeId(Degree);
5748 }
5749 }
David Greene221925e2010-01-20 00:59:23 +00005750 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005751#ifndef NDEBUG
5752 SDNode *S = ++I;
5753 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005754 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005755#endif
5756 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005757 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005758 }
5759
5760 assert(SortedPos == AllNodes.end() &&
5761 "Topological sort incomplete!");
5762 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5763 "First node in topological sort is not the entry token!");
5764 assert(AllNodes.front().getNodeId() == 0 &&
5765 "First node in topological sort has non-zero id!");
5766 assert(AllNodes.front().getNumOperands() == 0 &&
5767 "First node in topological sort has operands!");
5768 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5769 "Last node in topologic sort has unexpected id!");
5770 assert(AllNodes.back().use_empty() &&
5771 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005772 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005773 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005774}
5775
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005776/// AssignOrdering - Assign an order to the SDNode.
Bill Wendling4533cac2010-01-28 21:51:40 +00005777void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005778 assert(SD && "Trying to assign an order to a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005779 Ordering->add(SD, Order);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005780}
Evan Chenge6f35d82006-08-01 08:20:41 +00005781
Bill Wendling0777e922009-12-21 21:59:52 +00005782/// GetOrdering - Get the order for the SDNode.
5783unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
5784 assert(SD && "Trying to get the order of a null node!");
Bill Wendling187361b2010-01-23 10:26:57 +00005785 return Ordering->getOrder(SD);
Bill Wendling0777e922009-12-21 21:59:52 +00005786}
5787
Evan Chengbfcb3052010-03-25 01:38:16 +00005788/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5789/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00005790void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5791 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00005792 if (SD)
5793 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00005794}
Evan Cheng091cba12006-07-27 06:39:06 +00005795
Devang Patela2e868d2011-01-25 23:27:42 +00005796/// TransferDbgValues - Transfer SDDbgValues.
5797void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5798 if (From == To || !From.getNode()->getHasDebugValue())
5799 return;
5800 SDNode *FromNode = From.getNode();
5801 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005802 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00005803 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00005804 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00005805 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00005806 SDDbgValue *Dbg = *I;
5807 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5808 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5809 Dbg->getOffset(), Dbg->getDebugLoc(),
5810 Dbg->getOrder());
5811 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00005812 }
5813 }
Devang Patela778f5c2011-02-18 22:43:42 +00005814 for (SmallVector<SDDbgValue *, 2>::iterator I = ClonedDVs.begin(),
5815 E = ClonedDVs.end(); I != E; ++I)
5816 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00005817}
5818
Jim Laskey58b968b2005-08-17 20:08:02 +00005819//===----------------------------------------------------------------------===//
5820// SDNode Class
5821//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005822
Chris Lattner48b85922007-02-04 02:41:42 +00005823HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005824 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005825}
5826
Devang Patel0d881da2010-07-06 22:08:15 +00005827GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
5828 const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005829 EVT VT, int64_t o, unsigned char TF)
Devang Patel0d881da2010-07-06 22:08:15 +00005830 : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00005831 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005832}
Chris Lattner48b85922007-02-04 02:41:42 +00005833
Owen Andersone50ed302009-08-10 22:56:29 +00005834MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005835 MachineMemOperand *mmo)
5836 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005837 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005838 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005839 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00005840 assert(isNonTemporal() == MMO->isNonTemporal() &&
5841 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00005842 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005843}
5844
Scott Michelfdc40a02009-02-17 22:15:04 +00005845MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005846 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005847 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005848 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005849 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00005850 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005851 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00005852 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5853 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005854}
5855
Jim Laskey583bd472006-10-27 23:46:08 +00005856/// Profile - Gather unique data for the node.
5857///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005858void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005859 AddNodeIDNode(ID, this);
5860}
5861
Owen Andersond8110fb2009-08-25 22:27:22 +00005862namespace {
5863 struct EVTArray {
5864 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005865
Owen Andersond8110fb2009-08-25 22:27:22 +00005866 EVTArray() {
5867 VTs.reserve(MVT::LAST_VALUETYPE);
5868 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5869 VTs.push_back(MVT((MVT::SimpleValueType)i));
5870 }
5871 };
5872}
5873
Owen Andersone50ed302009-08-10 22:56:29 +00005874static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005875static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005876static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005877
Chris Lattnera3255112005-11-08 23:30:28 +00005878/// getValueTypeList - Return a pointer to the specified value type.
5879///
Owen Andersone50ed302009-08-10 22:56:29 +00005880const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005881 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005882 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005883 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005884 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00005885 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00005886 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00005887 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005888 }
Chris Lattnera3255112005-11-08 23:30:28 +00005889}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005890
Chris Lattner5c884562005-01-12 18:37:47 +00005891/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5892/// indicated value. This method ignores uses of other values defined by this
5893/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005894bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005895 assert(Value < getNumValues() && "Bad value!");
5896
Roman Levensteindc1adac2008-04-07 10:06:32 +00005897 // TODO: Only iterate over uses of a given value of the node
5898 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005899 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005900 if (NUses == 0)
5901 return false;
5902 --NUses;
5903 }
Chris Lattner5c884562005-01-12 18:37:47 +00005904 }
5905
5906 // Found exactly the right number of uses?
5907 return NUses == 0;
5908}
5909
5910
Evan Cheng33d55952007-08-02 05:29:38 +00005911/// hasAnyUseOfValue - Return true if there are any use of the indicated
5912/// value. This method ignores uses of other values defined by this operation.
5913bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5914 assert(Value < getNumValues() && "Bad value!");
5915
Dan Gohman1373c1c2008-07-09 22:39:01 +00005916 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005917 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005918 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005919
5920 return false;
5921}
5922
5923
Dan Gohman2a629952008-07-27 18:06:42 +00005924/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005925///
Dan Gohman2a629952008-07-27 18:06:42 +00005926bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005927 bool Seen = false;
5928 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005929 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005930 if (User == this)
5931 Seen = true;
5932 else
5933 return false;
5934 }
5935
5936 return Seen;
5937}
5938
Evan Chenge6e97e62006-11-03 07:31:32 +00005939/// isOperand - Return true if this node is an operand of N.
5940///
Dan Gohman475871a2008-07-27 21:46:04 +00005941bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005942 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5943 if (*this == N->getOperand(i))
5944 return true;
5945 return false;
5946}
5947
Evan Cheng917be682008-03-04 00:41:45 +00005948bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005949 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005950 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005951 return true;
5952 return false;
5953}
Evan Cheng4ee62112006-02-05 06:29:23 +00005954
Chris Lattner572dee72008-01-16 05:49:24 +00005955/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005956/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005957/// side-effecting instructions on any chain path. In practice, this looks
5958/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005959/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005960bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005961 unsigned Depth) const {
5962 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005963
Chris Lattner572dee72008-01-16 05:49:24 +00005964 // Don't search too deeply, we just want to be able to see through
5965 // TokenFactor's etc.
5966 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005967
Chris Lattner572dee72008-01-16 05:49:24 +00005968 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005969 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00005970 if (getOpcode() == ISD::TokenFactor) {
5971 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00005972 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5973 return false;
5974 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00005975 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005976
Chris Lattner572dee72008-01-16 05:49:24 +00005977 // Loads don't have side effects, look through them.
5978 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5979 if (!Ld->isVolatile())
5980 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5981 }
5982 return false;
5983}
5984
Lang Hames944520f2011-07-07 04:31:51 +00005985/// hasPredecessor - Return true if N is a predecessor of this node.
5986/// N is either an operand of this node, or can be reached by recursively
5987/// traversing up the operands.
5988/// NOTE: This is an expensive method. Use it carefully.
5989bool SDNode::hasPredecessor(const SDNode *N) const {
5990 SmallPtrSet<const SDNode *, 32> Visited;
5991 SmallVector<const SDNode *, 16> Worklist;
5992 return hasPredecessorHelper(N, Visited, Worklist);
5993}
Dan Gohman6688d612009-10-28 03:44:30 +00005994
Lang Hames944520f2011-07-07 04:31:51 +00005995bool SDNode::hasPredecessorHelper(const SDNode *N,
5996 SmallPtrSet<const SDNode *, 32> &Visited,
5997 SmallVector<const SDNode *, 16> &Worklist) const {
5998 if (Visited.empty()) {
5999 Worklist.push_back(this);
6000 } else {
6001 // Take a look in the visited set. If we've already encountered this node
6002 // we needn't search further.
6003 if (Visited.count(N))
6004 return true;
6005 }
6006
6007 // Haven't visited N yet. Continue the search.
6008 while (!Worklist.empty()) {
6009 const SDNode *M = Worklist.pop_back_val();
6010 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6011 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006012 if (Visited.insert(Op))
6013 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006014 if (Op == N)
6015 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006016 }
Lang Hames944520f2011-07-07 04:31:51 +00006017 }
Dan Gohman6688d612009-10-28 03:44:30 +00006018
6019 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006020}
6021
Evan Chengc5484282006-10-04 00:56:09 +00006022uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6023 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006024 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006025}
6026
Mon P Wangcd6e7252009-11-30 02:42:02 +00006027SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6028 assert(N->getNumValues() == 1 &&
6029 "Can't unroll a vector with multiple results!");
6030
6031 EVT VT = N->getValueType(0);
6032 unsigned NE = VT.getVectorNumElements();
6033 EVT EltVT = VT.getVectorElementType();
6034 DebugLoc dl = N->getDebugLoc();
6035
6036 SmallVector<SDValue, 8> Scalars;
6037 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6038
6039 // If ResNE is 0, fully unroll the vector op.
6040 if (ResNE == 0)
6041 ResNE = NE;
6042 else if (NE > ResNE)
6043 NE = ResNE;
6044
6045 unsigned i;
6046 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006047 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006048 SDValue Operand = N->getOperand(j);
6049 EVT OperandVT = Operand.getValueType();
6050 if (OperandVT.isVector()) {
6051 // A vector operand; extract a single element.
6052 EVT OperandEltVT = OperandVT.getVectorElementType();
6053 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6054 OperandEltVT,
6055 Operand,
Owen Andersonf7710af2011-05-02 22:25:45 +00006056 getConstant(i, TLI.getPointerTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006057 } else {
6058 // A scalar operand; just use it as is.
6059 Operands[j] = Operand;
6060 }
6061 }
6062
6063 switch (N->getOpcode()) {
6064 default:
6065 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6066 &Operands[0], Operands.size()));
6067 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006068 case ISD::VSELECT:
6069 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6070 &Operands[0], Operands.size()));
6071 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006072 case ISD::SHL:
6073 case ISD::SRA:
6074 case ISD::SRL:
6075 case ISD::ROTL:
6076 case ISD::ROTR:
6077 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Anderson6154f6c2011-03-07 18:29:47 +00006078 getShiftAmountOperand(Operands[0].getValueType(),
6079 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006080 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006081 case ISD::SIGN_EXTEND_INREG:
6082 case ISD::FP_ROUND_INREG: {
6083 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6084 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6085 Operands[0],
6086 getValueType(ExtVT)));
6087 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006088 }
6089 }
6090
6091 for (; i < ResNE; ++i)
6092 Scalars.push_back(getUNDEF(EltVT));
6093
6094 return getNode(ISD::BUILD_VECTOR, dl,
6095 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6096 &Scalars[0], Scalars.size());
6097}
6098
Evan Cheng64fa4a92009-12-09 01:36:00 +00006099
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006100/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6101/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006102/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006103bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006104 unsigned Bytes, int Dist) const {
6105 if (LD->getChain() != Base->getChain())
6106 return false;
6107 EVT VT = LD->getValueType(0);
6108 if (VT.getSizeInBits() / 8 != Bytes)
6109 return false;
6110
6111 SDValue Loc = LD->getOperand(1);
6112 SDValue BaseLoc = Base->getOperand(1);
6113 if (Loc.getOpcode() == ISD::FrameIndex) {
6114 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6115 return false;
6116 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6117 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6118 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6119 int FS = MFI->getObjectSize(FI);
6120 int BFS = MFI->getObjectSize(BFI);
6121 if (FS != BFS || FS != (int)Bytes) return false;
6122 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6123 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006124
6125 // Handle X+C
6126 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6127 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6128 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006129
Dan Gohman46510a72010-04-15 01:51:59 +00006130 const GlobalValue *GV1 = NULL;
6131 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006132 int64_t Offset1 = 0;
6133 int64_t Offset2 = 0;
6134 bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6135 bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
6136 if (isGA1 && isGA2 && GV1 == GV2)
6137 return Offset1 == (Offset2 + Dist*Bytes);
6138 return false;
6139}
6140
6141
Evan Chengf2dc5c72009-12-09 01:04:59 +00006142/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6143/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006144unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006145 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006146 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006147 int64_t GVOffset = 0;
Evan Cheng255f20f2010-04-01 06:04:33 +00006148 if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006149 unsigned PtrWidth = TLI.getPointerTy().getSizeInBits();
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006150 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006151 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Micah Villmow3574eca2012-10-08 16:38:25 +00006152 TLI.getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006153 unsigned AlignBits = KnownZero.countTrailingOnes();
6154 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6155 if (Align)
6156 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006157 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006158
Evan Chengf2dc5c72009-12-09 01:04:59 +00006159 // If this is a direct reference to a stack slot, use information about the
6160 // stack slot's alignment.
6161 int FrameIdx = 1 << 31;
6162 int64_t FrameOffset = 0;
6163 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6164 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006165 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006166 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006167 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006168 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6169 FrameOffset = Ptr.getConstantOperandVal(1);
6170 }
6171
6172 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006173 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006174 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6175 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006176 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006177 }
6178
6179 return 0;
6180}
6181
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006182// getAddressSpace - Return the address space this GlobalAddress belongs to.
6183unsigned GlobalAddressSDNode::getAddressSpace() const {
6184 return getGlobal()->getType()->getAddressSpace();
6185}
6186
6187
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006188Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006189 if (isMachineConstantPoolEntry())
6190 return Val.MachineCPVal->getType();
6191 return Val.ConstVal->getType();
6192}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006193
Bob Wilson24e338e2009-03-02 23:24:16 +00006194bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6195 APInt &SplatUndef,
6196 unsigned &SplatBitSize,
6197 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006198 unsigned MinSplatBits,
6199 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006200 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006201 assert(VT.isVector() && "Expected a vector type");
6202 unsigned sz = VT.getSizeInBits();
6203 if (MinSplatBits > sz)
6204 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006205
Bob Wilson24e338e2009-03-02 23:24:16 +00006206 SplatValue = APInt(sz, 0);
6207 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006208
Bob Wilson24e338e2009-03-02 23:24:16 +00006209 // Get the bits. Bits with undefined values (when the corresponding element
6210 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6211 // in SplatValue. If any of the values are not constant, give up and return
6212 // false.
6213 unsigned int nOps = getNumOperands();
6214 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6215 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006216
6217 for (unsigned j = 0; j < nOps; ++j) {
6218 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006219 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006220 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006221
Bob Wilson24e338e2009-03-02 23:24:16 +00006222 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006223 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006224 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006225 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006226 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006227 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006228 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006229 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006230 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006231 }
6232
Bob Wilson24e338e2009-03-02 23:24:16 +00006233 // The build_vector is all constants or undefs. Find the smallest element
6234 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006235
Bob Wilson24e338e2009-03-02 23:24:16 +00006236 HasAnyUndefs = (SplatUndef != 0);
6237 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006238
Bob Wilson24e338e2009-03-02 23:24:16 +00006239 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006240 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6241 APInt LowValue = SplatValue.trunc(HalfSize);
6242 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6243 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006244
Bob Wilson24e338e2009-03-02 23:24:16 +00006245 // If the two halves do not match (ignoring undef bits), stop here.
6246 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6247 MinSplatBits > HalfSize)
6248 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006249
Bob Wilson24e338e2009-03-02 23:24:16 +00006250 SplatValue = HighValue | LowValue;
6251 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006252
Bob Wilson24e338e2009-03-02 23:24:16 +00006253 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006254 }
6255
Bob Wilson24e338e2009-03-02 23:24:16 +00006256 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006257 return true;
6258}
Nate Begeman9008ca62009-04-27 18:41:29 +00006259
Owen Andersone50ed302009-08-10 22:56:29 +00006260bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006261 // Find the first non-undef value in the shuffle mask.
6262 unsigned i, e;
6263 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6264 /* search */;
6265
Nate Begemana6415752009-04-29 18:13:31 +00006266 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006267
Nate Begeman5a5ca152009-04-29 05:20:52 +00006268 // Make sure all remaining elements are either undef or the same as the first
6269 // non-undef value.
6270 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006271 if (Mask[i] >= 0 && Mask[i] != Idx)
6272 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006273 return true;
6274}
David Greenecf495bc2010-01-20 20:13:31 +00006275
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006276#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006277static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006278 SmallPtrSet<const SDNode*, 32> &Visited,
6279 SmallPtrSet<const SDNode*, 32> &Checked) {
6280 // If this node has already been checked, don't check it again.
6281 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006282 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006283
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006284 // If a node has already been visited on this depth-first walk, reject it as
6285 // a cycle.
6286 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006287 dbgs() << "Offending node:\n";
6288 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006289 errs() << "Detected cycle in SelectionDAG\n";
6290 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006291 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006292
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006293 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6294 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006295
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006296 Checked.insert(N);
6297 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006298}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006299#endif
David Greenecf495bc2010-01-20 20:13:31 +00006300
6301void llvm::checkForCycles(const llvm::SDNode *N) {
6302#ifdef XDEBUG
6303 assert(N && "Checking nonexistant SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006304 SmallPtrSet<const SDNode*, 32> visited;
6305 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006306 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006307#endif
6308}
6309
6310void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6311 checkForCycles(DAG->getRoot().getNode());
6312}