blob: 45d5a4fa69e8384dece2c9f82bdba59805234a05 [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 "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
Chandler Carruthbe049292013-01-07 03:08:10 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#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"
Jeff Cohenfd161e92005-01-09 20:41:56 +000051#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000052#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000054
Chris Lattner0b3e5252006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +000065
Jim Laskey58b968b2005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000076}
77
Owen Andersone50ed302009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000081
Dale Johannesenf04afdb2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover0a29cb02013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089}
90
Jim Laskey58b968b2005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000094
Evan Chenga8df1662006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000098 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000099 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Evan Chenga8df1662006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000103
Chris Lattner61d43992006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000105
Chris Lattner61d43992006-03-25 22:57:01 +0000106 // Skip over all of the undef values.
107 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
108 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000109
Chris Lattner61d43992006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattner61d43992006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000114 // elements. We have to be a bit careful here, as the type of the constant
115 // may not be the same as the type of the vector elements due to type
116 // legalization (the elements are promoted to a legal type for the target and
117 // a vector of a type may be legal when the base element type is not).
118 // We only want to check enough bits to cover the vector elements, because
119 // we care if the resultant vector is all ones, not whether the individual
120 // constants are.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszak554c6762012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chenga8df1662006-03-27 06:58:47 +0000125 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohman6c231502008-02-29 01:47:35 +0000128 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000130 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Chris Lattner61d43992006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbach331ff3b2012-03-21 17:48:04 +0000133 // undefs. Even with the above element type twiddling, this should be OK, as
134 // the same type legalization should have applied to all the elements.
Chris Lattner61d43992006-03-25 22:57:01 +0000135 for (++i; i != e; ++i)
136 if (N->getOperand(i) != NotZero &&
137 N->getOperand(i).getOpcode() != ISD::UNDEF)
138 return false;
139 return true;
140}
141
142
Evan Cheng4a147842006-03-26 09:50:58 +0000143/// isBuildVectorAllZeros - Return true if the specified node is a
144/// BUILD_VECTOR where all of the elements are 0 or undef.
145bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000146 // Look through a bit convert.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000147 if (N->getOpcode() == ISD::BITCAST)
Gabor Greifba36cb52008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Evan Cheng4a147842006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Evan Chenga8df1662006-03-27 06:58:47 +0000152 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000153
Evan Chenga8df1662006-03-27 06:58:47 +0000154 // Skip over all of the undef values.
155 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
156 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000157
Evan Chenga8df1662006-03-27 06:58:47 +0000158 // Do not accept an all-undef vector.
159 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000160
Dan Gohman68f32cb2009-06-04 16:49:15 +0000161 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000162 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000163 SDValue Zero = N->getOperand(i);
Jakub Staszak554c6762012-09-30 21:24:57 +0000164 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
165 if (!CN->isNullValue())
Evan Chenga8df1662006-03-27 06:58:47 +0000166 return false;
Jakub Staszak554c6762012-09-30 21:24:57 +0000167 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
168 if (!CFPN->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000169 return false;
170 } else
171 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000172
Dan Gohman68f32cb2009-06-04 16:49:15 +0000173 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000174 // undefs.
175 for (++i; i != e; ++i)
176 if (N->getOperand(i) != Zero &&
177 N->getOperand(i).getOpcode() != ISD::UNDEF)
178 return false;
179 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000180}
181
Evan Chengefec7512008-02-18 23:04:32 +0000182/// isScalarToVector - Return true if the specified node is a
183/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
184/// element is not an undef.
185bool ISD::isScalarToVector(const SDNode *N) {
186 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
187 return true;
188
189 if (N->getOpcode() != ISD::BUILD_VECTOR)
190 return false;
191 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
192 return false;
193 unsigned NumElems = N->getNumOperands();
Mon P Wangcab98e32010-11-19 19:08:12 +0000194 if (NumElems == 1)
195 return false;
Evan Chengefec7512008-02-18 23:04:32 +0000196 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000198 if (V.getOpcode() != ISD::UNDEF)
199 return false;
200 }
201 return true;
202}
203
Nadav Rotemb87bdac2012-07-15 08:38:23 +0000204/// allOperandsUndef - Return true if the node has at least one operand
205/// and all operands of the specified node are ISD::UNDEF.
206bool ISD::allOperandsUndef(const SDNode *N) {
207 // Return false if the node has no operands.
208 // This is "logically inconsistent" with the definition of "all" but
209 // is probably the desired behavior.
210 if (N->getNumOperands() == 0)
211 return false;
212
213 for (unsigned i = 0, e = N->getNumOperands(); i != e ; ++i)
214 if (N->getOperand(i).getOpcode() != ISD::UNDEF)
215 return false;
216
217 return true;
218}
219
Chris Lattnerc3aae252005-01-07 07:46:32 +0000220/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
221/// when given the operation for (X op Y).
222ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
223 // To perform this operation, we just need to swap the L and G bits of the
224 // operation.
225 unsigned OldL = (Operation >> 2) & 1;
226 unsigned OldG = (Operation >> 1) & 1;
227 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
228 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000229 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000230}
231
232/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
233/// 'op' is a valid SetCC operation.
234ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
235 unsigned Operation = Op;
236 if (isInteger)
237 Operation ^= 7; // Flip L, G, E bits, but not U.
238 else
239 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000240
Chris Lattnerc3aae252005-01-07 07:46:32 +0000241 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000242 Operation &= ~8; // Don't let N and U bits get set.
243
Chris Lattnerc3aae252005-01-07 07:46:32 +0000244 return ISD::CondCode(Operation);
245}
246
247
248/// isSignedOp - For an integer comparison, return 1 if the comparison is a
249/// signed operation and 2 if the result is an unsigned comparison. Return zero
250/// if the operation does not depend on the sign of the input (setne and seteq).
251static int isSignedOp(ISD::CondCode Opcode) {
252 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000253 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000254 case ISD::SETEQ:
255 case ISD::SETNE: return 0;
256 case ISD::SETLT:
257 case ISD::SETLE:
258 case ISD::SETGT:
259 case ISD::SETGE: return 1;
260 case ISD::SETULT:
261 case ISD::SETULE:
262 case ISD::SETUGT:
263 case ISD::SETUGE: return 2;
264 }
265}
266
267/// getSetCCOrOperation - Return the result of a logical OR between different
268/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
269/// returns SETCC_INVALID if it is not possible to represent the resultant
270/// comparison.
271ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
272 bool isInteger) {
273 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
274 // Cannot fold a signed integer setcc with an unsigned integer setcc.
275 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000276
Chris Lattnerc3aae252005-01-07 07:46:32 +0000277 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000278
Chris Lattnerc3aae252005-01-07 07:46:32 +0000279 // If the N and U bits get set then the resultant comparison DOES suddenly
280 // care about orderedness, and is true when ordered.
281 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000282 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Chris Lattnere41102b2006-05-12 17:03:46 +0000284 // Canonicalize illegal integer setcc's.
285 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
286 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000287
Chris Lattnerc3aae252005-01-07 07:46:32 +0000288 return ISD::CondCode(Op);
289}
290
291/// getSetCCAndOperation - Return the result of a logical AND between different
292/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
293/// function returns zero if it is not possible to represent the resultant
294/// comparison.
295ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
296 bool isInteger) {
297 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
298 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000299 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000300
301 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000302 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000303
Chris Lattnera83385f2006-04-27 05:01:07 +0000304 // Canonicalize illegal integer setcc's.
305 if (isInteger) {
306 switch (Result) {
307 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000308 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000309 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000310 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
311 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
312 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000313 }
314 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattnera83385f2006-04-27 05:01:07 +0000316 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000317}
318
Jim Laskey58b968b2005-08-17 20:08:02 +0000319//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000320// SDNode Profile Support
321//===----------------------------------------------------------------------===//
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
326 ID.AddInteger(OpC);
327}
328
329/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
330/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000331static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000332 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000333}
334
Jim Laskeydef69b92006-10-27 23:52:51 +0000335/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
336///
Jim Laskey583bd472006-10-27 23:46:08 +0000337static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000338 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000339 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000340 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000341 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000342 }
Jim Laskey583bd472006-10-27 23:46:08 +0000343}
344
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000345/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
346///
347static void AddNodeIDOperands(FoldingSetNodeID &ID,
348 const SDUse *Ops, unsigned NumOps) {
349 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000350 ID.AddPointer(Ops->getNode());
351 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000352 }
353}
354
Jim Laskey583bd472006-10-27 23:46:08 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000356 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000357 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000358 AddNodeIDOpcode(ID, OpC);
359 AddNodeIDValueTypes(ID, VTList);
360 AddNodeIDOperands(ID, OpList, N);
361}
362
Duncan Sands0dc40452008-10-27 15:30:53 +0000363/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
364/// the NodeID data.
365static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000367 case ISD::TargetExternalSymbol:
368 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000369 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000370 default: break; // Normal nodes don't need extra info.
371 case ISD::TargetConstant:
372 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000373 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 break;
375 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000376 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000377 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000378 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000379 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000380 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000381 case ISD::GlobalAddress:
382 case ISD::TargetGlobalTLSAddress:
383 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000384 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000385 ID.AddPointer(GA->getGlobal());
386 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000387 ID.AddInteger(GA->getTargetFlags());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000388 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000389 break;
390 }
391 case ISD::BasicBlock:
392 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
393 break;
394 case ISD::Register:
395 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
396 break;
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +0000397 case ISD::RegisterMask:
398 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
399 break;
Dan Gohman69de1932008-02-06 22:27:42 +0000400 case ISD::SRCVALUE:
401 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
402 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000403 case ISD::FrameIndex:
404 case ISD::TargetFrameIndex:
405 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
406 break;
407 case ISD::JumpTable:
408 case ISD::TargetJumpTable:
409 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000410 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000411 break;
412 case ISD::ConstantPool:
413 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000414 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000415 ID.AddInteger(CP->getAlignment());
416 ID.AddInteger(CP->getOffset());
417 if (CP->isMachineConstantPoolEntry())
Jim Grosbach5405d582011-09-27 20:59:33 +0000418 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000419 else
420 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000421 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000422 break;
423 }
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +0000424 case ISD::TargetIndex: {
425 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
426 ID.AddInteger(TI->getIndex());
427 ID.AddInteger(TI->getOffset());
428 ID.AddInteger(TI->getTargetFlags());
429 break;
430 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000431 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000432 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000434 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000435 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000439 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000440 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000441 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000442 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 break;
444 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000445 case ISD::ATOMIC_CMP_SWAP:
446 case ISD::ATOMIC_SWAP:
447 case ISD::ATOMIC_LOAD_ADD:
448 case ISD::ATOMIC_LOAD_SUB:
449 case ISD::ATOMIC_LOAD_AND:
450 case ISD::ATOMIC_LOAD_OR:
451 case ISD::ATOMIC_LOAD_XOR:
452 case ISD::ATOMIC_LOAD_NAND:
453 case ISD::ATOMIC_LOAD_MIN:
454 case ISD::ATOMIC_LOAD_MAX:
455 case ISD::ATOMIC_LOAD_UMIN:
Eli Friedman327236c2011-08-24 20:50:09 +0000456 case ISD::ATOMIC_LOAD_UMAX:
457 case ISD::ATOMIC_LOAD:
458 case ISD::ATOMIC_STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000459 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000460 ID.AddInteger(AT->getMemoryVT().getRawBits());
461 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +0000462 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
463 break;
464 }
465 case ISD::PREFETCH: {
466 const MemSDNode *PF = cast<MemSDNode>(N);
467 ID.AddInteger(PF->getPointerInfo().getAddrSpace());
Mon P Wang28873102008-06-25 08:15:39 +0000468 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000469 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000470 case ISD::VECTOR_SHUFFLE: {
471 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000472 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000473 i != e; ++i)
474 ID.AddInteger(SVN->getMaskElt(i));
475 break;
476 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000477 case ISD::TargetBlockAddress:
478 case ISD::BlockAddress: {
Michael Liao6c7ccaa2012-09-12 21:43:09 +0000479 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
480 ID.AddPointer(BA->getBlockAddress());
481 ID.AddInteger(BA->getOffset());
482 ID.AddInteger(BA->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000483 break;
484 }
Mon P Wang28873102008-06-25 08:15:39 +0000485 } // end switch (N->getOpcode())
Pete Cooper32ecfb42012-07-30 20:23:19 +0000486
487 // Target specific memory nodes could also have address spaces to check.
488 if (N->isTargetMemoryOpcode())
489 ID.AddInteger(cast<MemSDNode>(N)->getPointerInfo().getAddrSpace());
Jim Laskey583bd472006-10-27 23:46:08 +0000490}
491
Duncan Sands0dc40452008-10-27 15:30:53 +0000492/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
493/// data.
494static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
495 AddNodeIDOpcode(ID, N->getOpcode());
496 // Add the return value info.
497 AddNodeIDValueTypes(ID, N->getVTList());
498 // Add the operand info.
499 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
500
501 // Handle SDNode leafs with special info.
502 AddNodeIDCustom(ID, N);
503}
504
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000505/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greene1157f792010-02-17 20:21:42 +0000506/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000507/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000508///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000509static inline unsigned
David Greene1157f792010-02-17 20:21:42 +0000510encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000511 bool isNonTemporal, bool isInvariant) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000512 assert((ConvType & 3) == ConvType &&
513 "ConvType may not require more than 2 bits!");
514 assert((AM & 7) == AM &&
515 "AM may not require more than 3 bits!");
516 return ConvType |
517 (AM << 2) |
David Greene1157f792010-02-17 20:21:42 +0000518 (isVolatile << 5) |
Pete Cooperd752e0f2011-11-08 18:42:53 +0000519 (isNonTemporal << 6) |
520 (isInvariant << 7);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000521}
522
Jim Laskey583bd472006-10-27 23:46:08 +0000523//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000524// SelectionDAG Class
525//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000526
Duncan Sands0dc40452008-10-27 15:30:53 +0000527/// doNotCSE - Return true if CSE should not be performed for this node.
528static bool doNotCSE(SDNode *N) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000529 if (N->getValueType(0) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000530 return true; // Never CSE anything that produces a flag.
531
532 switch (N->getOpcode()) {
533 default: break;
534 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000535 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000536 return true; // Never CSE these nodes.
537 }
538
539 // Check that remaining values produced are not flags.
540 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000541 if (N->getValueType(i) == MVT::Glue)
Duncan Sands0dc40452008-10-27 15:30:53 +0000542 return true; // Never CSE anything that produces a flag.
543
544 return false;
545}
546
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000547/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000548/// SelectionDAG.
549void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000550 // Create a dummy node (which is not added to allnodes), that adds a reference
551 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000552 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000553
Chris Lattner190a4182006-08-04 17:45:20 +0000554 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Chris Lattner190a4182006-08-04 17:45:20 +0000556 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000557 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000558 if (I->use_empty())
559 DeadNodes.push_back(I);
560
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000561 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000562
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000563 // If the root changed (e.g. it was a dead load, update the root).
564 setRoot(Dummy.getValue());
565}
566
567/// RemoveDeadNodes - This method deletes the unreachable nodes in the
568/// given list, and any nodes that become unreachable as a result.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000569void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000570
Chris Lattner190a4182006-08-04 17:45:20 +0000571 // Process the worklist, deleting the nodes and adding their uses to the
572 // worklist.
573 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000574 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000575
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000576 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
577 DUL->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000578
Chris Lattner190a4182006-08-04 17:45:20 +0000579 // Take the node out of the appropriate CSE map.
580 RemoveNodeFromCSEMaps(N);
581
582 // Next, brutally remove the operand list. This is safe to do, as there are
583 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000584 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
585 SDUse &Use = *I++;
586 SDNode *Operand = Use.getNode();
587 Use.set(SDValue());
588
Chris Lattner190a4182006-08-04 17:45:20 +0000589 // Now that we removed this operand, see if there are no uses of it left.
590 if (Operand->use_empty())
591 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000592 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000593
Dan Gohmanc5336122009-01-19 22:39:36 +0000594 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000595 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000596}
597
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000598void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000599 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedman2efa35f2011-11-08 01:25:24 +0000600
601 // Create a dummy node that adds a reference to the root node, preventing
602 // it from being deleted. (This matters if the root is an operand of the
603 // dead node.)
604 HandleSDNode Dummy(getRoot());
605
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000606 RemoveDeadNodes(DeadNodes);
Evan Cheng130a6472006-10-12 20:34:05 +0000607}
608
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000609void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000610 // First take this out of the appropriate CSE map.
611 RemoveNodeFromCSEMaps(N);
612
Scott Michelfdc40a02009-02-17 22:15:04 +0000613 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000614 // AllNodes list, and delete the node.
615 DeleteNodeNotInCSEMaps(N);
616}
617
618void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000619 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
620 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000621
Dan Gohmanf06c8352008-09-30 18:30:35 +0000622 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000623 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000624
Dan Gohmanc5336122009-01-19 22:39:36 +0000625 DeallocateNode(N);
626}
627
628void SelectionDAG::DeallocateNode(SDNode *N) {
629 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000630 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000631
Dan Gohmanc5336122009-01-19 22:39:36 +0000632 // Set the opcode to DELETED_NODE to help catch bugs when node
633 // memory is reallocated.
634 N->NodeType = ISD::DELETED_NODE;
635
Dan Gohman11467282008-08-26 01:44:34 +0000636 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbar819309e2009-12-16 20:10:05 +0000637
Evan Chengbfcb3052010-03-25 01:38:16 +0000638 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramer22a54c12011-06-18 13:13:44 +0000639 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Chengbfcb3052010-03-25 01:38:16 +0000640 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
641 DbgVals[i]->setIsInvalidated();
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000642}
643
Chris Lattner149c58c2005-08-16 18:17:10 +0000644/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
645/// correspond to it. This is useful when we're about to delete or repurpose
646/// the node. We don't want future request for structurally identical nodes
647/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000648bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000649 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000650 switch (N->getOpcode()) {
Dan Gohman095cc292008-09-13 01:54:27 +0000651 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000652 case ISD::CONDCODE:
653 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
654 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000655 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000656 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
657 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000658 case ISD::ExternalSymbol:
659 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000660 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000661 case ISD::TargetExternalSymbol: {
662 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
663 Erased = TargetExternalSymbols.erase(
664 std::pair<std::string,unsigned char>(ESN->getSymbol(),
665 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000666 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000667 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000668 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000669 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000670 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000671 Erased = ExtendedValueTypeNodes.erase(VT);
672 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
674 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000675 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000676 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000677 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000678 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000679 // Remove it from the CSE Map.
Duncan Sandsa30b7d22010-12-12 13:22:50 +0000680 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
681 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
Chris Lattner4a283e92006-08-11 18:38:11 +0000682 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000683 break;
684 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000685#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000686 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000687 // flag result (which cannot be CSE'd) or is one of the special cases that are
688 // not subject to CSE.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000689 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000690 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000691 N->dump(this);
David Greene55d146e2010-01-05 01:24:36 +0000692 dbgs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000693 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000694 }
695#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000696 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000697}
698
Dan Gohman39946102009-01-25 16:29:12 +0000699/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
700/// maps and modified in place. Add it back to the CSE maps, unless an identical
701/// node already exists, in which case transfer all its users to the existing
702/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000703///
Dan Gohman39946102009-01-25 16:29:12 +0000704void
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000705SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohman39946102009-01-25 16:29:12 +0000706 // For node types that aren't CSE'd, just act as if no identical node
707 // already exists.
708 if (!doNotCSE(N)) {
709 SDNode *Existing = CSEMap.GetOrInsertNode(N);
710 if (Existing != N) {
711 // If there was already an existing matching node, use ReplaceAllUsesWith
712 // to replace the dead one with the existing one. This can cause
713 // recursive merging of other unrelated nodes down the line.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000714 ReplaceAllUsesWith(N, Existing);
Evan Cheng71e86852008-07-08 20:06:39 +0000715
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000716 // N is now dead. Inform the listeners and delete it.
717 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
718 DUL->NodeDeleted(N, Existing);
Dan Gohman39946102009-01-25 16:29:12 +0000719 DeleteNodeNotInCSEMaps(N);
720 return;
721 }
722 }
Evan Cheng71e86852008-07-08 20:06:39 +0000723
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000724 // If the node doesn't already exist, we updated it. Inform listeners.
725 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
726 DUL->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000727}
728
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000729/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000730/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000731/// return null, otherwise return a pointer to the slot it would take. If a
732/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000733SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000734 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000735 if (doNotCSE(N))
736 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000737
Dan Gohman475871a2008-07-27 21:46:04 +0000738 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000739 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000740 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000741 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000742 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000743 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000744}
745
746/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000747/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000748/// return null, otherwise return a pointer to the slot it would take. If a
749/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000750SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000751 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000752 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000753 if (doNotCSE(N))
754 return 0;
755
Dan Gohman475871a2008-07-27 21:46:04 +0000756 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000757 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000758 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000759 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000760 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000761 return Node;
Chris Lattnera5682852006-08-07 23:03:03 +0000762}
763
764
765/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000766/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000767/// return null, otherwise return a pointer to the slot it would take. If a
768/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000769SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000770 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000771 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000772 if (doNotCSE(N))
773 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000774
Jim Laskey583bd472006-10-27 23:46:08 +0000775 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000776 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000777 AddNodeIDCustom(ID, N);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000778 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbar819309e2009-12-16 20:10:05 +0000779 return Node;
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000780}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000781
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000782#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000783/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
784static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsd038e042008-07-21 10:20:31 +0000785 switch (N->getOpcode()) {
786 default:
787 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000788 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000789 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000790 assert(N->getNumValues() == 1 && "Too many results!");
791 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
792 "Wrong return type!");
793 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
794 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
795 "Mismatched operand types!");
796 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
797 "Wrong operand type!");
798 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
799 "Wrong return type size");
800 break;
801 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000802 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000803 assert(N->getNumValues() == 1 && "Too many results!");
804 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000805 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000806 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000807 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedman9db817f2011-09-09 21:04:06 +0000808 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindola15684b22009-04-24 12:40:33 +0000809 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000810 (EltVT.isInteger() && I->getValueType().isInteger() &&
811 EltVT.bitsLE(I->getValueType()))) &&
812 "Wrong operand type!");
Eli Friedman9db817f2011-09-09 21:04:06 +0000813 assert(I->getValueType() == N->getOperand(0).getValueType() &&
814 "Operands must all have the same type");
815 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000816 break;
817 }
818 }
819}
820
Duncan Sands59d2dad2010-11-20 11:25:00 +0000821/// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
822static void VerifySDNode(SDNode *N) {
823 // The SDNode allocators cannot be used to allocate nodes with fields that are
824 // not present in an SDNode!
825 assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
826 assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
827 assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
828 assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
829 assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
830 assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
831 assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
832 assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
833 assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
834 assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
835 assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
836 assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
837 assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
838 assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
839 assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
840 assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
841 assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
842 assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
843 assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
844
845 VerifyNodeCommon(N);
846}
847
848/// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
849/// invalid.
850static void VerifyMachineNode(SDNode *N) {
851 // The MachineNode allocators cannot be used to allocate nodes with fields
852 // that are not present in a MachineNode!
853 // Currently there are no such nodes.
854
855 VerifyNodeCommon(N);
856}
Benjamin Kramer3ca13632010-11-20 15:53:24 +0000857#endif // NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +0000858
Owen Andersone50ed302009-08-10 22:56:29 +0000859/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000860/// given type.
861///
Owen Andersone50ed302009-08-10 22:56:29 +0000862unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000863 Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000864 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000865 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000866
Bill Wendlingba54bca2013-06-19 21:36:55 +0000867 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000868}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000869
Dale Johannesen92570c42009-02-07 02:15:05 +0000870// EntryNode could meaningfully have debug info if we can find it...
Devang Patel0508d042011-12-15 18:21:18 +0000871SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Bill Wendlingee287ca2013-11-22 05:17:44 +0000872 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TTI(0), TLI(0), OptLevel(OL),
Bill Wendlingba54bca2013-06-19 21:36:55 +0000873 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Daniel Sandersea28aaf2013-11-15 12:56:49 +0000874 Root(getEntryNode()), NewNodesMustHaveLegalTypes(false),
875 UpdateListeners(0) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000876 AllNodes.push_back(&EntryNode);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000877 DbgInfo = new SDDbgInfo();
Dan Gohman6f179662008-08-23 00:50:30 +0000878}
879
Bill Wendlingee287ca2013-11-22 05:17:44 +0000880void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti,
881 const TargetLowering *tli) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000882 MF = &mf;
Chandler Carruthe4b4edd2013-01-05 12:32:17 +0000883 TTI = tti;
Bill Wendlingee287ca2013-11-22 05:17:44 +0000884 TLI = tli;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000885 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000886}
887
Chris Lattner78ec3112003-08-11 14:57:33 +0000888SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000889 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmanf350b272008-08-23 02:25:05 +0000890 allnodes_clear();
Dale Johannesenbfdf7f32010-03-10 22:13:47 +0000891 delete DbgInfo;
Dan Gohmanf350b272008-08-23 02:25:05 +0000892}
893
894void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000895 assert(&*AllNodes.begin() == &EntryNode);
896 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000897 while (!AllNodes.empty())
898 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000899}
900
Dan Gohman7c3234c2008-08-27 23:52:12 +0000901void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000902 allnodes_clear();
903 OperandAllocator.Reset();
904 CSEMap.clear();
905
906 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000907 ExternalSymbols.clear();
908 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000909 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
910 static_cast<CondCodeSDNode*>(0));
911 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
912 static_cast<SDNode*>(0));
913
Dan Gohmane7852d02009-01-26 04:35:06 +0000914 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000915 AllNodes.push_back(&EntryNode);
916 Root = getEntryNode();
Evan Cheng31441b72010-03-29 20:48:30 +0000917 DbgInfo->clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000918}
919
Andrew Trickac6d9be2013-05-25 02:42:55 +0000920SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotema3c42f32011-09-27 11:16:47 +0000921 return VT.bitsGT(Op.getValueType()) ?
922 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
923 getNode(ISD::TRUNCATE, DL, VT, Op);
924}
925
Andrew Trickac6d9be2013-05-25 02:42:55 +0000926SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000927 return VT.bitsGT(Op.getValueType()) ?
928 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
929 getNode(ISD::TRUNCATE, DL, VT, Op);
930}
931
Andrew Trickac6d9be2013-05-25 02:42:55 +0000932SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands3a66a682009-10-13 21:04:12 +0000933 return VT.bitsGT(Op.getValueType()) ?
934 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
935 getNode(ISD::TRUNCATE, DL, VT, Op);
936}
937
Andrew Trickac6d9be2013-05-25 02:42:55 +0000938SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman87862e72009-12-11 21:31:27 +0000939 assert(!VT.isVector() &&
940 "getZeroExtendInReg should use the vector element type instead of "
941 "the vector type!");
Bill Wendling6ce610f2009-01-30 22:23:15 +0000942 if (Op.getValueType() == VT) return Op;
Dan Gohman87862e72009-12-11 21:31:27 +0000943 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
944 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendling6ce610f2009-01-30 22:23:15 +0000945 VT.getSizeInBits());
946 return getNode(ISD::AND, DL, Op.getValueType(), Op,
947 getConstant(Imm, Op.getValueType()));
948}
949
Bob Wilson4c245462009-01-22 17:39:32 +0000950/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
951///
Andrew Trickac6d9be2013-05-25 02:42:55 +0000952SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000953 EVT EltVT = VT.getScalarType();
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000954 SDValue NegOne =
955 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000956 return getNode(ISD::XOR, DL, VT, Val, NegOne);
957}
958
Owen Andersone50ed302009-08-10 22:56:29 +0000959SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000960 EVT EltVT = VT.getScalarType();
Dan Gohmance9bc122009-01-27 20:39:34 +0000961 assert((EltVT.getSizeInBits() >= 64 ||
962 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
963 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000964 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000965}
966
Owen Andersone50ed302009-08-10 22:56:29 +0000967SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000968 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000969}
970
Owen Andersone50ed302009-08-10 22:56:29 +0000971SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000972 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000973
Chris Lattner5cf0b6e2010-02-24 22:44:06 +0000974 EVT EltVT = VT.getScalarType();
Duncan Sands28b77e92011-09-06 19:07:46 +0000975 const ConstantInt *Elt = &Val;
Chris Lattner61b09412006-08-11 21:01:22 +0000976
Bill Wendlingba54bca2013-06-19 21:36:55 +0000977 const TargetLowering *TLI = TM.getTargetLowering();
978
Duncan Sands28b77e92011-09-06 19:07:46 +0000979 // In some cases the vector type is legal but the element type is illegal and
980 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
981 // inserted value (the type does not need to match the vector element type).
982 // Any extra bits introduced will be truncated away.
Bill Wendlingba54bca2013-06-19 21:36:55 +0000983 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sands28b77e92011-09-06 19:07:46 +0000984 TargetLowering::TypePromoteInteger) {
Bill Wendlingba54bca2013-06-19 21:36:55 +0000985 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sands28b77e92011-09-06 19:07:46 +0000986 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
987 Elt = ConstantInt::get(*getContext(), NewVal);
988 }
Daniel Sandersea28aaf2013-11-15 12:56:49 +0000989 // In other cases the element type is illegal and needs to be expanded, for
990 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
991 // the value into n parts and use a vector type with n-times the elements.
992 // Then bitcast to the type requested.
993 // Legalizing constants too early makes the DAGCombiner's job harder so we
994 // only legalize if the DAG tells us we must produce legal types.
995 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
996 TLI->getTypeAction(*getContext(), EltVT) ==
997 TargetLowering::TypeExpandInteger) {
998 APInt NewVal = Elt->getValue();
999 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1000 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1001 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1002 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1003
1004 // Check the temporary vector is the correct size. If this fails then
1005 // getTypeToTransformTo() probably returned a type whose size (in bits)
1006 // isn't a power-of-2 factor of the requested type size.
1007 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1008
1009 SmallVector<SDValue, 2> EltParts;
1010 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i) {
1011 EltParts.push_back(getConstant(NewVal.lshr(i * ViaEltSizeInBits)
1012 .trunc(ViaEltSizeInBits),
1013 ViaEltVT, isT));
1014 }
1015
1016 // EltParts is currently in little endian order. If we actually want
1017 // big-endian order then reverse it now.
1018 if (TLI->isBigEndian())
1019 std::reverse(EltParts.begin(), EltParts.end());
1020
1021 // The elements must be reversed when the element order is different
1022 // to the endianness of the elements (because the BITCAST is itself a
1023 // vector shuffle in this situation). However, we do not need any code to
1024 // perform this reversal because getConstant() is producing a vector
1025 // splat.
1026 // This situation occurs in MIPS MSA.
1027
1028 SmallVector<SDValue, 8> Ops;
1029 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i)
1030 Ops.insert(Ops.end(), EltParts.begin(), EltParts.end());
1031
1032 SDValue Result = getNode(ISD::BITCAST, SDLoc(), VT,
1033 getNode(ISD::BUILD_VECTOR, SDLoc(), ViaVecVT,
1034 &Ops[0], Ops.size()));
1035 return Result;
1036 }
Duncan Sands28b77e92011-09-06 19:07:46 +00001037
1038 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1039 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +00001040 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +00001041 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001042 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sands28b77e92011-09-06 19:07:46 +00001043 ID.AddPointer(Elt);
Chris Lattner61b09412006-08-11 21:01:22 +00001044 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +00001045 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001046 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001047 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001048 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001049
Dan Gohman89081322007-12-12 22:21:26 +00001050 if (!N) {
Duncan Sands28b77e92011-09-06 19:07:46 +00001051 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +00001052 CSEMap.InsertNode(N, IP);
1053 AllNodes.push_back(N);
1054 }
1055
Dan Gohman475871a2008-07-27 21:46:04 +00001056 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001057 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001058 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001059 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001060 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +00001061 }
1062 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +00001063}
1064
Dan Gohman475871a2008-07-27 21:46:04 +00001065SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001066 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner0bd48932008-01-17 07:00:52 +00001067}
1068
1069
Owen Andersone50ed302009-08-10 22:56:29 +00001070SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001071 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001072}
1073
Owen Andersone50ed302009-08-10 22:56:29 +00001074SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +00001075 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +00001076
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001077 EVT EltVT = VT.getScalarType();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078
Chris Lattnerd8658612005-02-17 20:17:32 +00001079 // Do the map lookup using the actual bit pattern for the floating point
1080 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1081 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001082 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +00001083 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001084 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +00001085 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001086 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +00001087 SDNode *N = NULL;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001088 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00001089 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +00001090 return SDValue(N, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001091
Evan Chengc908dcd2007-06-29 21:36:04 +00001092 if (!N) {
Dan Gohman95531882010-03-18 18:49:47 +00001093 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +00001094 CSEMap.InsertNode(N, IP);
1095 AllNodes.push_back(N);
1096 }
1097
Dan Gohman475871a2008-07-27 21:46:04 +00001098 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001099 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001100 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001101 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001102 // FIXME SDLoc info might be appropriate here
1103 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00001104 }
1105 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106}
1107
Owen Andersone50ed302009-08-10 22:56:29 +00001108SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00001109 EVT EltVT = VT.getScalarType();
Owen Anderson825b72b2009-08-11 20:47:22 +00001110 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001111 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001112 else if (EltVT==MVT::f64)
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001113 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6eb7a422012-12-30 19:03:32 +00001114 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1115 EltVT==MVT::f16) {
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001116 bool ignored;
1117 APFloat apf = APFloat(Val);
Tim Northover0a29cb02013-01-22 09:46:31 +00001118 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen0a406ae2010-05-07 21:35:53 +00001119 &ignored);
1120 return getConstantFP(apf, VT, isTarget);
Craig Topper5e25ee82012-02-05 08:31:47 +00001121 } else
1122 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesenf04afdb2007-08-30 00:23:21 +00001123}
1124
Andrew Trickac6d9be2013-05-25 02:42:55 +00001125SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00001126 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +00001127 bool isTargetGA,
1128 unsigned char TargetFlags) {
1129 assert((TargetFlags == 0 || isTargetGA) &&
1130 "Cannot set target flags on target-independent globals");
Matt Arsenault94437c92013-11-17 00:06:39 +00001131 const TargetLowering *TLI = TM.getTargetLowering();
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001132
Dan Gohman6520e202008-10-18 02:06:02 +00001133 // Truncate (with sign-extension) the offset value to the pointer size.
Matt Arsenault94437c92013-11-17 00:06:39 +00001134 unsigned BitWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Dan Gohman6520e202008-10-18 02:06:02 +00001135 if (BitWidth < 64)
Richard Smith1144af32012-08-24 23:29:28 +00001136 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman6520e202008-10-18 02:06:02 +00001137
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001138 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1139 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +00001140 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001141 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +00001142 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001143 }
1144
Chris Lattner2a4ed822009-06-25 21:21:14 +00001145 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001146 if (GVar && GVar->isThreadLocal())
1147 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1148 else
1149 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +00001150
Jim Laskey583bd472006-10-27 23:46:08 +00001151 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001152 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001153 ID.AddPointer(GV);
1154 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +00001155 ID.AddInteger(TargetFlags);
Pete Cooper32ecfb42012-07-30 20:23:19 +00001156 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner61b09412006-08-11 21:01:22 +00001157 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001158 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +00001159 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001160
Andrew Trickac6d9be2013-05-25 02:42:55 +00001161 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1162 DL.getDebugLoc(), GV, VT,
Dan Gohman95531882010-03-18 18:49:47 +00001163 Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +00001164 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001166 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001167}
1168
Owen Andersone50ed302009-08-10 22:56:29 +00001169SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001170 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001171 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001172 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001173 ID.AddInteger(FI);
1174 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001175 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001176 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001177
Dan Gohman95531882010-03-18 18:49:47 +00001178 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001179 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001180 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001181 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001182}
1183
Owen Andersone50ed302009-08-10 22:56:29 +00001184SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001185 unsigned char TargetFlags) {
1186 assert((TargetFlags == 0 || isTarget) &&
1187 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001188 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001189 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001190 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001191 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001192 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001193 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001194 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001195 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001196
Dan Gohman95531882010-03-18 18:49:47 +00001197 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1198 TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001199 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001200 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001201 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001202}
1203
Dan Gohman46510a72010-04-15 01:51:59 +00001204SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001205 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001206 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001207 unsigned char TargetFlags) {
1208 assert((TargetFlags == 0 || isTarget) &&
1209 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001210 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001211 Alignment =
1212 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001213 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001214 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001215 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001216 ID.AddInteger(Alignment);
1217 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001218 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001219 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001220 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001221 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001222 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001223
Dan Gohman95531882010-03-18 18:49:47 +00001224 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1225 Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001226 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001227 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001228 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001229}
1230
Chris Lattnerc3aae252005-01-07 07:46:32 +00001231
Owen Andersone50ed302009-08-10 22:56:29 +00001232SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001233 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001234 bool isTarget,
1235 unsigned char TargetFlags) {
1236 assert((TargetFlags == 0 || isTarget) &&
1237 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001238 if (Alignment == 0)
Bill Wendlingba54bca2013-06-19 21:36:55 +00001239 Alignment =
1240 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001241 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001242 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001243 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001244 ID.AddInteger(Alignment);
1245 ID.AddInteger(Offset);
Jim Grosbach5405d582011-09-27 20:59:33 +00001246 C->addSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001247 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001248 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001249 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001250 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001251
Dan Gohman95531882010-03-18 18:49:47 +00001252 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1253 Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001254 CSEMap.InsertNode(N, IP);
1255 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001256 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001257}
1258
Jakob Stoklund Olesen74500bd2012-08-07 22:37:05 +00001259SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1260 unsigned char TargetFlags) {
1261 FoldingSetNodeID ID;
1262 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1263 ID.AddInteger(Index);
1264 ID.AddInteger(Offset);
1265 ID.AddInteger(TargetFlags);
1266 void *IP = 0;
1267 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1268 return SDValue(E, 0);
1269
1270 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1271 TargetFlags);
1272 CSEMap.InsertNode(N, IP);
1273 AllNodes.push_back(N);
1274 return SDValue(N, 0);
1275}
1276
Dan Gohman475871a2008-07-27 21:46:04 +00001277SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001278 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001279 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001280 ID.AddPointer(MBB);
1281 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001282 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001283 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001284
Dan Gohman95531882010-03-18 18:49:47 +00001285 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001286 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001287 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001288 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001289}
1290
Owen Andersone50ed302009-08-10 22:56:29 +00001291SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1293 ValueTypeNodes.size())
1294 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001295
Duncan Sands83ec4b62008-06-06 12:08:01 +00001296 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001297 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001298
Dan Gohman475871a2008-07-27 21:46:04 +00001299 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001300 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001301 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001302 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001303}
1304
Owen Andersone50ed302009-08-10 22:56:29 +00001305SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001306 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001307 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001308 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001309 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001310 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001311}
1312
Owen Andersone50ed302009-08-10 22:56:29 +00001313SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001314 unsigned char TargetFlags) {
1315 SDNode *&N =
1316 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1317 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001318 if (N) return SDValue(N, 0);
Dan Gohman95531882010-03-18 18:49:47 +00001319 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001320 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001321 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001322}
1323
Dan Gohman475871a2008-07-27 21:46:04 +00001324SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001325 if ((unsigned)Cond >= CondCodeNodes.size())
1326 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001327
Chris Lattner079a27a2005-08-09 20:40:02 +00001328 if (CondCodeNodes[Cond] == 0) {
Dan Gohman95531882010-03-18 18:49:47 +00001329 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman0e5f1302008-07-07 23:02:41 +00001330 CondCodeNodes[Cond] = N;
1331 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001332 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001333
Dan Gohman475871a2008-07-27 21:46:04 +00001334 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001335}
1336
Nate Begeman5a5ca152009-04-29 05:20:52 +00001337// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1338// the shuffle mask M that point at N1 to point at N2, and indices that point
1339// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001340static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1341 std::swap(N1, N2);
1342 int NElts = M.size();
1343 for (int i = 0; i != NElts; ++i) {
1344 if (M[i] >= NElts)
1345 M[i] -= NElts;
1346 else if (M[i] >= 0)
1347 M[i] += NElts;
1348 }
1349}
1350
Andrew Trickac6d9be2013-05-25 02:42:55 +00001351SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001352 SDValue N2, const int *Mask) {
Craig Topperad445a62013-08-09 04:37:24 +00001353 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1354 "Invalid VECTOR_SHUFFLE");
Nate Begeman9008ca62009-04-27 18:41:29 +00001355
1356 // Canonicalize shuffle undef, undef -> undef
1357 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001358 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001359
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001360 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001361 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001362 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001363 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001364 for (unsigned i = 0; i != NElts; ++i) {
1365 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001366 MaskVec.push_back(Mask[i]);
1367 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001368
Nate Begeman9008ca62009-04-27 18:41:29 +00001369 // Canonicalize shuffle v, v -> v, undef
1370 if (N1 == N2) {
1371 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001372 for (unsigned i = 0; i != NElts; ++i)
1373 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001374 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001375
Nate Begeman9008ca62009-04-27 18:41:29 +00001376 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1377 if (N1.getOpcode() == ISD::UNDEF)
1378 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001379
Nate Begeman9008ca62009-04-27 18:41:29 +00001380 // Canonicalize all index into lhs, -> shuffle lhs, undef
1381 // Canonicalize all index into rhs, -> shuffle rhs, undef
1382 bool AllLHS = true, AllRHS = true;
1383 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001384 for (unsigned i = 0; i != NElts; ++i) {
1385 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001386 if (N2Undef)
1387 MaskVec[i] = -1;
1388 else
1389 AllLHS = false;
1390 } else if (MaskVec[i] >= 0) {
1391 AllRHS = false;
1392 }
1393 }
1394 if (AllLHS && AllRHS)
1395 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001396 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001397 N2 = getUNDEF(VT);
1398 if (AllRHS) {
1399 N1 = getUNDEF(VT);
1400 commuteShuffle(N1, N2, MaskVec);
1401 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001402
Craig Toppereee2a112013-08-08 08:03:12 +00001403 // If Identity shuffle return that node.
Nate Begeman9008ca62009-04-27 18:41:29 +00001404 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001405 for (unsigned i = 0; i != NElts; ++i) {
1406 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001407 }
Craig Topperad445a62013-08-09 04:37:24 +00001408 if (Identity && NElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00001409 return N1;
Nate Begeman9008ca62009-04-27 18:41:29 +00001410
1411 FoldingSetNodeID ID;
1412 SDValue Ops[2] = { N1, N2 };
1413 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001414 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001415 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001416
Nate Begeman9008ca62009-04-27 18:41:29 +00001417 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001418 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman9008ca62009-04-27 18:41:29 +00001419 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001420
Nate Begeman9008ca62009-04-27 18:41:29 +00001421 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1422 // SDNode doesn't have access to it. This memory will be "leaked" when
1423 // the node is deallocated, but recovered when the NodeAllocator is released.
1424 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1425 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001426
Dan Gohman95531882010-03-18 18:49:47 +00001427 ShuffleVectorSDNode *N =
Jack Carter3af4d252013-09-09 22:02:08 +00001428 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(),
1429 dl.getDebugLoc(), N1, N2,
1430 MaskAlloc);
Nate Begeman9008ca62009-04-27 18:41:29 +00001431 CSEMap.InsertNode(N, IP);
1432 AllNodes.push_back(N);
1433 return SDValue(N, 0);
1434}
1435
Andrew Trickac6d9be2013-05-25 02:42:55 +00001436SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001437 SDValue Val, SDValue DTy,
1438 SDValue STy, SDValue Rnd, SDValue Sat,
1439 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001440 // If the src and dest types are the same and the conversion is between
1441 // integer types of the same sign or two floats, no conversion is necessary.
1442 if (DTy == STy &&
1443 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001444 return Val;
1445
1446 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001447 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1448 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001449 void* IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001450 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesena04b7572009-02-03 23:04:43 +00001451 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001452
Jack Carter3af4d252013-09-09 22:02:08 +00001453 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(),
1454 dl.getDebugLoc(),
1455 Ops, 5, Code);
Dale Johannesena04b7572009-02-03 23:04:43 +00001456 CSEMap.InsertNode(N, IP);
1457 AllNodes.push_back(N);
1458 return SDValue(N, 0);
1459}
1460
Owen Andersone50ed302009-08-10 22:56:29 +00001461SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001462 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001463 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001464 ID.AddInteger(RegNo);
1465 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001466 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001467 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001468
Dan Gohman95531882010-03-18 18:49:47 +00001469 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001470 CSEMap.InsertNode(N, IP);
1471 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001472 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001473}
1474
Jakob Stoklund Olesen9cf37e82012-01-18 23:52:12 +00001475SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1476 FoldingSetNodeID ID;
1477 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1478 ID.AddPointer(RegMask);
1479 void *IP = 0;
1480 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1481 return SDValue(E, 0);
1482
1483 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1484 CSEMap.InsertNode(N, IP);
1485 AllNodes.push_back(N);
1486 return SDValue(N, 0);
1487}
1488
Andrew Trickac6d9be2013-05-25 02:42:55 +00001489SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesene8c17332009-01-29 00:47:48 +00001490 FoldingSetNodeID ID;
1491 SDValue Ops[] = { Root };
Chris Lattner7561d482010-03-14 02:33:54 +00001492 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1493 ID.AddPointer(Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001494 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001495 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00001496 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001497
Jack Carter3af4d252013-09-09 22:02:08 +00001498 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(),
1499 dl.getDebugLoc(), Root, Label);
Dale Johannesene8c17332009-01-29 00:47:48 +00001500 CSEMap.InsertNode(N, IP);
1501 AllNodes.push_back(N);
1502 return SDValue(N, 0);
1503}
1504
Chris Lattner7561d482010-03-14 02:33:54 +00001505
Dan Gohman46510a72010-04-15 01:51:59 +00001506SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001507 int64_t Offset,
Dan Gohman29cbade2009-11-20 23:18:13 +00001508 bool isTarget,
1509 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001510 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1511
1512 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001513 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001514 ID.AddPointer(BA);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001515 ID.AddInteger(Offset);
Dan Gohman29cbade2009-11-20 23:18:13 +00001516 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001517 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001518 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8c2b5252009-10-30 01:27:03 +00001519 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001520
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001521 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1522 TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001523 CSEMap.InsertNode(N, IP);
1524 AllNodes.push_back(N);
1525 return SDValue(N, 0);
1526}
1527
Dan Gohman475871a2008-07-27 21:46:04 +00001528SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands1df98592010-02-16 11:11:14 +00001529 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner61b09412006-08-11 21:01:22 +00001530 "SrcValue is not a pointer?");
1531
Jim Laskey583bd472006-10-27 23:46:08 +00001532 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001533 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001534 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001535
Chris Lattner61b09412006-08-11 21:01:22 +00001536 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00001537 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001538 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001539
Dan Gohman95531882010-03-18 18:49:47 +00001540 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001541 CSEMap.InsertNode(N, IP);
1542 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001543 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001544}
1545
Chris Lattnerdecc2672010-04-07 05:20:54 +00001546/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1547SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1548 FoldingSetNodeID ID;
1549 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1550 ID.AddPointer(MD);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001551
Chris Lattnerdecc2672010-04-07 05:20:54 +00001552 void *IP = 0;
1553 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1554 return SDValue(E, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001555
Chris Lattnerdecc2672010-04-07 05:20:54 +00001556 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1557 CSEMap.InsertNode(N, IP);
1558 AllNodes.push_back(N);
1559 return SDValue(N, 0);
1560}
1561
Matt Arsenault59d3ae62013-11-15 01:34:59 +00001562/// getAddrSpaceCast - Return an AddrSpaceCastSDNode.
1563SDValue SelectionDAG::getAddrSpaceCast(SDLoc dl, EVT VT, SDValue Ptr,
1564 unsigned SrcAS, unsigned DestAS) {
1565 SDValue Ops[] = {Ptr};
1566 FoldingSetNodeID ID;
1567 AddNodeIDNode(ID, ISD::ADDRSPACECAST, getVTList(VT), &Ops[0], 1);
1568 ID.AddInteger(SrcAS);
1569 ID.AddInteger(DestAS);
1570
1571 void *IP = 0;
1572 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1573 return SDValue(E, 0);
1574
1575 SDNode *N = new (NodeAllocator) AddrSpaceCastSDNode(dl.getIROrder(),
1576 dl.getDebugLoc(),
1577 VT, Ptr, SrcAS, DestAS);
1578 CSEMap.InsertNode(N, IP);
1579 AllNodes.push_back(N);
1580 return SDValue(N, 0);
1581}
Chris Lattnerdecc2672010-04-07 05:20:54 +00001582
Duncan Sands92abc622009-01-31 15:50:11 +00001583/// getShiftAmountOperand - Return the specified value casted to
1584/// the target's desired shift amount type.
Owen Anderson6154f6c2011-03-07 18:29:47 +00001585SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001586 EVT OpTy = Op.getValueType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001587 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands92abc622009-01-31 15:50:11 +00001588 if (OpTy == ShTy || OpTy.isVector()) return Op;
1589
1590 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001591 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001592}
1593
Chris Lattner37ce9df2007-10-15 17:47:20 +00001594/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1595/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001596SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001597 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001598 unsigned ByteSize = VT.getStoreSize();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001599 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001600 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang364d73d2008-07-05 20:40:31 +00001601 unsigned StackAlign =
Bill Wendlingba54bca2013-06-19 21:36:55 +00001602 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001603
David Greene3f2bf852009-11-12 20:49:22 +00001604 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001605 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner37ce9df2007-10-15 17:47:20 +00001606}
1607
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001608/// CreateStackTemporary - Create a stack temporary suitable for holding
1609/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001610SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001611 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1612 VT2.getStoreSizeInBits())/8;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001613 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1614 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlingba54bca2013-06-19 21:36:55 +00001615 const TargetLowering *TLI = TM.getTargetLowering();
1616 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001617 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1618 TD->getPrefTypeAlignment(Ty2));
1619
1620 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001621 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001622 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001623}
1624
Owen Andersone50ed302009-08-10 22:56:29 +00001625SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001626 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001627 // These setcc operations always fold.
1628 switch (Cond) {
1629 default: break;
1630 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001631 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001632 case ISD::SETTRUE:
Tim Northovera5eeb9d2013-09-06 12:38:12 +00001633 case ISD::SETTRUE2: {
1634 const TargetLowering *TLI = TM.getTargetLowering();
1635 TargetLowering::BooleanContent Cnt = TLI->getBooleanContents(VT.isVector());
1636 return getConstant(
1637 Cnt == TargetLowering::ZeroOrNegativeOneBooleanContent ? -1ULL : 1, VT);
1638 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001639
Chris Lattnera83385f2006-04-27 05:01:07 +00001640 case ISD::SETOEQ:
1641 case ISD::SETOGT:
1642 case ISD::SETOGE:
1643 case ISD::SETOLT:
1644 case ISD::SETOLE:
1645 case ISD::SETONE:
1646 case ISD::SETO:
1647 case ISD::SETUO:
1648 case ISD::SETUEQ:
1649 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001650 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001651 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001652 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001653
Gabor Greifba36cb52008-08-28 21:40:38 +00001654 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001655 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001656 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001657 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001658
Chris Lattnerc3aae252005-01-07 07:46:32 +00001659 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001660 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001661 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1662 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001663 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1664 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1665 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1666 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1667 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1668 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1669 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1670 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001671 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001672 }
Chris Lattner67255a12005-04-07 18:14:58 +00001673 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001674 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1675 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001676 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001677 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001678 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001679 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001680 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001681 // fall through
1682 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001683 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001684 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001685 // fall through
1686 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001687 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001688 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001689 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001690 // fall through
1691 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001692 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001693 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001694 // fall through
1695 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001696 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001697 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001698 // fall through
1699 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001700 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001701 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001702 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001703 // fall through
1704 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001705 R==APFloat::cmpEqual, VT);
1706 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1707 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1708 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1709 R==APFloat::cmpEqual, VT);
1710 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1711 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1712 R==APFloat::cmpLessThan, VT);
1713 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1714 R==APFloat::cmpUnordered, VT);
1715 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1716 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001717 }
1718 } else {
1719 // Ensure that the constant occurs on the RHS.
Tom Stellard12d43f92013-09-28 02:50:38 +00001720 ISD::CondCode SwappedCond = ISD::getSetCCSwappedOperands(Cond);
1721 MVT CompVT = N1.getValueType().getSimpleVT();
1722 if (!TM.getTargetLowering()->isCondCodeLegal(SwappedCond, CompVT))
1723 return SDValue();
1724
1725 return getSetCC(dl, VT, N2, N1, SwappedCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001726 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001727 }
1728
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001729 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001730 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001731}
1732
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001733/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1734/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001735bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001736 // This predicate is not safe for vector operations.
1737 if (Op.getValueType().isVector())
1738 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001739
Dan Gohman87862e72009-12-11 21:31:27 +00001740 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001741 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1742}
1743
Dan Gohmanea859be2007-06-22 14:59:07 +00001744/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1745/// this predicate to simplify operations downstream. Mask is known to be zero
1746/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001747bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001748 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001749 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001750 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001751 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001752 return (KnownZero & Mask) == Mask;
1753}
1754
1755/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1756/// known to be either zero or one and return them in the KnownZero/KnownOne
1757/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1758/// processing.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001759void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1760 APInt &KnownOne, unsigned Depth) const {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001761 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001762 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001763
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001764 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001765 if (Depth == 6)
Dan Gohmanea859be2007-06-22 14:59:07 +00001766 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001767
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001768 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001769
1770 switch (Op.getOpcode()) {
1771 case ISD::Constant:
1772 // We know all of the bits for a constant!
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001773 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1774 KnownZero = ~KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001775 return;
1776 case ISD::AND:
1777 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001778 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1779 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001780 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1781 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001782
1783 // Output known-1 bits are only known if set in both the LHS & RHS.
1784 KnownOne &= KnownOne2;
1785 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1786 KnownZero |= KnownZero2;
1787 return;
1788 case ISD::OR:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001789 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1790 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001791 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1792 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1793
Dan Gohmanea859be2007-06-22 14:59:07 +00001794 // Output known-0 bits are only known if clear in both the LHS & RHS.
1795 KnownZero &= KnownZero2;
1796 // Output known-1 are known to be set if set in either the LHS | RHS.
1797 KnownOne |= KnownOne2;
1798 return;
1799 case ISD::XOR: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001800 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1801 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001802 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1803 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1804
Dan Gohmanea859be2007-06-22 14:59:07 +00001805 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001806 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001807 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1808 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1809 KnownZero = KnownZeroOut;
1810 return;
1811 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001812 case ISD::MUL: {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001813 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1814 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001815 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1816 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1817
1818 // If low bits are zero in either operand, output low known-0 bits.
1819 // Also compute a conserative estimate for high known-0 bits.
1820 // More trickiness is possible, but this is sufficient for the
1821 // interesting case of alignment computation.
Jay Foad7a874dd2010-12-01 08:53:58 +00001822 KnownOne.clearAllBits();
Dan Gohman23e8b712008-04-28 17:02:21 +00001823 unsigned TrailZ = KnownZero.countTrailingOnes() +
1824 KnownZero2.countTrailingOnes();
1825 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001826 KnownZero2.countLeadingOnes(),
1827 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001828
1829 TrailZ = std::min(TrailZ, BitWidth);
1830 LeadZ = std::min(LeadZ, BitWidth);
1831 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1832 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001833 return;
1834 }
1835 case ISD::UDIV: {
1836 // For the purposes of computing leading zeros we can conservatively
1837 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001838 // be less than the denominator.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001839 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001840 unsigned LeadZ = KnownZero2.countLeadingOnes();
1841
Jay Foad7a874dd2010-12-01 08:53:58 +00001842 KnownOne2.clearAllBits();
1843 KnownZero2.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001844 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001845 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1846 if (RHSUnknownLeadingOnes != BitWidth)
1847 LeadZ = std::min(BitWidth,
1848 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001849
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001850 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman23e8b712008-04-28 17:02:21 +00001851 return;
1852 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001853 case ISD::SELECT:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001854 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1855 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001856 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1857 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1858
Dan Gohmanea859be2007-06-22 14:59:07 +00001859 // Only known if known in both the LHS and RHS.
1860 KnownOne &= KnownOne2;
1861 KnownZero &= KnownZero2;
1862 return;
1863 case ISD::SELECT_CC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001864 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1865 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001866 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1867 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1868
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 // Only known if known in both the LHS and RHS.
1870 KnownOne &= KnownOne2;
1871 KnownZero &= KnownZero2;
1872 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001873 case ISD::SADDO:
1874 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001875 case ISD::SSUBO:
1876 case ISD::USUBO:
1877 case ISD::SMULO:
1878 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001879 if (Op.getResNo() != 1)
1880 return;
Duncan Sands03228082008-11-23 15:47:28 +00001881 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001882 case ISD::SETCC:
1883 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001884 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands28b77e92011-09-06 19:07:46 +00001885 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001886 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001887 return;
1888 case ISD::SHL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001889 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001890 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001891 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001892
1893 // If the shift count is an invalid immediate, don't do anything.
1894 if (ShAmt >= BitWidth)
1895 return;
1896
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001897 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001898 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001899 KnownZero <<= ShAmt;
1900 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001901 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001902 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001903 }
1904 return;
1905 case ISD::SRL:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001906 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohmanea859be2007-06-22 14:59:07 +00001907 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001908 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001909
Dan Gohmand4cf9922008-02-26 18:50:50 +00001910 // If the shift count is an invalid immediate, don't do anything.
1911 if (ShAmt >= BitWidth)
1912 return;
1913
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001914 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001915 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001916 KnownZero = KnownZero.lshr(ShAmt);
1917 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001918
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001919 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001920 KnownZero |= HighBits; // High bits known zero.
1921 }
1922 return;
1923 case ISD::SRA:
1924 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001925 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001926
Dan Gohmand4cf9922008-02-26 18:50:50 +00001927 // If the shift count is an invalid immediate, don't do anything.
1928 if (ShAmt >= BitWidth)
1929 return;
1930
Dan Gohmanea859be2007-06-22 14:59:07 +00001931 // If any of the demanded bits are produced by the sign extension, we also
1932 // demand the input sign bit.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001933 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001934
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001935 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001936 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001937 KnownZero = KnownZero.lshr(ShAmt);
1938 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001939
Dan Gohmanea859be2007-06-22 14:59:07 +00001940 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001941 APInt SignBit = APInt::getSignBit(BitWidth);
1942 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001943
Dan Gohmanca93a432008-02-20 16:30:17 +00001944 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001945 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001946 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001947 KnownOne |= HighBits; // New bits are known one.
1948 }
1949 }
1950 return;
1951 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001952 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohmand1996362010-01-09 02:13:55 +00001953 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001954
1955 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001956 // present in the input.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001957 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001958
Dan Gohman977a76f2008-02-13 22:28:48 +00001959 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001960 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001961
Dan Gohmanea859be2007-06-22 14:59:07 +00001962 // If the sign extended bits are demanded, we know that the sign
1963 // bit is demanded.
Jay Foad40f8f622010-12-07 08:25:19 +00001964 InSignBit = InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001965 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001966 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001967
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001968 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1969 KnownOne &= InputDemandedBits;
1970 KnownZero &= InputDemandedBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00001971 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1972
Dan Gohmanea859be2007-06-22 14:59:07 +00001973 // If the sign bit of the input is known set or clear, then we know the
1974 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001975 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001976 KnownZero |= NewBits;
1977 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001978 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001979 KnownOne |= NewBits;
1980 KnownZero &= ~NewBits;
1981 } else { // Input sign bit unknown
1982 KnownZero &= ~NewBits;
1983 KnownOne &= ~NewBits;
1984 }
1985 return;
1986 }
1987 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001988 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001989 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00001990 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanea859be2007-06-22 14:59:07 +00001991 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001992 unsigned LowBits = Log2_32(BitWidth)+1;
1993 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad7a874dd2010-12-01 08:53:58 +00001994 KnownOne.clearAllBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001995 return;
1996 }
1997 case ISD::LOAD: {
Rafael Espindola95d594c2012-03-31 18:14:00 +00001998 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem77451752013-03-20 22:53:44 +00001999 // If this is a ZEXTLoad and we are looking at the loaded value.
2000 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00002001 EVT VT = LD->getMemoryVT();
Dan Gohmand1996362010-01-09 02:13:55 +00002002 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002003 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola95d594c2012-03-31 18:14:00 +00002004 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002005 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002006 }
2007 return;
2008 }
2009 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002010 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002011 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002012 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad40f8f622010-12-07 08:25:19 +00002013 KnownZero = KnownZero.trunc(InBits);
2014 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002015 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad40f8f622010-12-07 08:25:19 +00002016 KnownZero = KnownZero.zext(BitWidth);
2017 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002018 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002019 return;
2020 }
2021 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00002022 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002023 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002024 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002025
Jay Foad40f8f622010-12-07 08:25:19 +00002026 KnownZero = KnownZero.trunc(InBits);
2027 KnownOne = KnownOne.trunc(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002028 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00002029
2030 // Note if the sign bit is known to be zero or one.
2031 bool SignBitKnownZero = KnownZero.isNegative();
2032 bool SignBitKnownOne = KnownOne.isNegative();
2033 assert(!(SignBitKnownZero && SignBitKnownOne) &&
2034 "Sign bit can't be known to be both zero and one!");
2035
Jay Foad40f8f622010-12-07 08:25:19 +00002036 KnownZero = KnownZero.zext(BitWidth);
2037 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002038
Dan Gohman977a76f2008-02-13 22:28:48 +00002039 // If the sign bit is known zero or one, the top bits match.
2040 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00002041 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00002042 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00002043 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002044 return;
2045 }
2046 case ISD::ANY_EXTEND: {
Evan Cheng2766a472012-12-06 19:13:27 +00002047 EVT InVT = Op.getOperand(0).getValueType();
2048 unsigned InBits = InVT.getScalarType().getSizeInBits();
2049 KnownZero = KnownZero.trunc(InBits);
2050 KnownOne = KnownOne.trunc(InBits);
2051 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2052 KnownZero = KnownZero.zext(BitWidth);
2053 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002054 return;
2055 }
2056 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00002057 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00002058 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad40f8f622010-12-07 08:25:19 +00002059 KnownZero = KnownZero.zext(InBits);
2060 KnownOne = KnownOne.zext(InBits);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002061 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002062 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad40f8f622010-12-07 08:25:19 +00002063 KnownZero = KnownZero.trunc(BitWidth);
2064 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002065 break;
2066 }
2067 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002068 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002069 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002070 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2071 KnownZero |= (~InMask);
Nadav Rotem7ee0e5a2012-07-16 18:34:53 +00002072 KnownOne &= (~KnownZero);
Dan Gohmanea859be2007-06-22 14:59:07 +00002073 return;
2074 }
Chris Lattnerd268a492007-12-22 21:26:52 +00002075 case ISD::FGETSIGN:
2076 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00002077 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00002078 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00002079
Dan Gohman23e8b712008-04-28 17:02:21 +00002080 case ISD::SUB: {
2081 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2082 // We know that the top bits of C-X are clear if X contains less bits
2083 // than C (i.e. no wrap-around can happen). For example, 20-X is
2084 // positive if we can prove that X is >= 0 and < 16.
2085 if (CLHS->getAPIntValue().isNonNegative()) {
2086 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2087 // NLZ can't be BitWidth with no sign bit
2088 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002089 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002090
2091 // If all of the MaskV bits are known to be zero, then we know the
2092 // output top bits are zero, because we now know that the output is
2093 // from [0-C].
2094 if ((KnownZero2 & MaskV) == MaskV) {
2095 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2096 // Top bits known zero.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002097 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman23e8b712008-04-28 17:02:21 +00002098 }
2099 }
2100 }
2101 }
2102 // fall through
Chris Lattnerda605882010-12-19 20:38:28 +00002103 case ISD::ADD:
2104 case ISD::ADDE: {
Dan Gohmanea859be2007-06-22 14:59:07 +00002105 // Output known-0 bits are known if clear or set in both the low clear bits
2106 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2107 // low 3 bits clear.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002108 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002109 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002110 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2111
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002112 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002113 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00002114 KnownZeroOut = std::min(KnownZeroOut,
2115 KnownZero2.countTrailingOnes());
2116
Chris Lattnerda605882010-12-19 20:38:28 +00002117 if (Op.getOpcode() == ISD::ADD) {
2118 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2119 return;
2120 }
2121
2122 // With ADDE, a carry bit may be added in, so we can only use this
2123 // information if we know (at least) that the low two bits are clear. We
2124 // then return to the caller that the low bit is unknown but that other bits
2125 // are known zero.
2126 if (KnownZeroOut >= 2) // ADDE
2127 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00002128 return;
2129 }
Dan Gohman23e8b712008-04-28 17:02:21 +00002130 case ISD::SREM:
2131 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands5c2873a2010-01-29 09:45:26 +00002132 const APInt &RA = Rem->getAPIntValue().abs();
2133 if (RA.isPowerOf2()) {
2134 APInt LowBits = RA - 1;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002135 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002136
Duncan Sands5c2873a2010-01-29 09:45:26 +00002137 // The low bits of the first operand are unchanged by the srem.
2138 KnownZero = KnownZero2 & LowBits;
2139 KnownOne = KnownOne2 & LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002140
Duncan Sands5c2873a2010-01-29 09:45:26 +00002141 // If the first operand is non-negative or has all low bits zero, then
2142 // the upper bits are all zero.
2143 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2144 KnownZero |= ~LowBits;
2145
2146 // If the first operand is negative and not all low bits are zero, then
2147 // the upper bits are all one.
2148 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2149 KnownOne |= ~LowBits;
Dan Gohman23e8b712008-04-28 17:02:21 +00002150 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 }
2152 }
2153 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00002154 case ISD::UREM: {
2155 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00002156 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00002157 if (RA.isPowerOf2()) {
2158 APInt LowBits = (RA - 1);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002159 KnownZero |= ~LowBits;
2160 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002161 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2162 break;
2163 }
2164 }
2165
2166 // Since the result is less than or equal to either operand, any leading
2167 // zero bits in either operand must also exist in the result.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002168 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2169 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman23e8b712008-04-28 17:02:21 +00002170
2171 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2172 KnownZero2.countLeadingOnes());
Jay Foad7a874dd2010-12-01 08:53:58 +00002173 KnownOne.clearAllBits();
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002174 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman23e8b712008-04-28 17:02:21 +00002175 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00002176 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00002177 case ISD::FrameIndex:
2178 case ISD::TargetFrameIndex:
2179 if (unsigned Align = InferPtrAlignment(Op)) {
2180 // The low bits are known zero if the pointer is aligned.
2181 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2182 return;
2183 }
2184 break;
Owen Anderson95771af2011-02-25 21:41:48 +00002185
Dan Gohmanea859be2007-06-22 14:59:07 +00002186 default:
Evan Chengb5a55d92011-05-24 01:48:22 +00002187 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2188 break;
2189 // Fallthrough
Dan Gohmanea859be2007-06-22 14:59:07 +00002190 case ISD::INTRINSIC_WO_CHAIN:
2191 case ISD::INTRINSIC_W_CHAIN:
2192 case ISD::INTRINSIC_VOID:
Evan Chengb5a55d92011-05-24 01:48:22 +00002193 // Allow the target to implement this method for its nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002194 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00002195 return;
2196 }
2197}
2198
2199/// ComputeNumSignBits - Return the number of times the sign bit of the
2200/// register is replicated into the other bits. We know that at least 1 bit
2201/// is always equal to the sign bit (itself), but other cases can give us
2202/// information. For example, immediately after an "SRA X, 2", we know that
2203/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00002204unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlingba54bca2013-06-19 21:36:55 +00002205 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002207 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman87862e72009-12-11 21:31:27 +00002208 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002209 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00002210 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002211
Dan Gohmanea859be2007-06-22 14:59:07 +00002212 if (Depth == 6)
2213 return 1; // Limit search depth.
2214
2215 switch (Op.getOpcode()) {
2216 default: break;
2217 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002218 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002219 return VTBits-Tmp+1;
2220 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002221 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002222 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002223
Dan Gohmanea859be2007-06-22 14:59:07 +00002224 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002225 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich9b6af8d2011-02-24 10:00:20 +00002226 return Val.getNumSignBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002227 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002228
Dan Gohmanea859be2007-06-22 14:59:07 +00002229 case ISD::SIGN_EXTEND:
Jack Carter3af4d252013-09-09 22:02:08 +00002230 Tmp =
2231 VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002232 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002233
Dan Gohmanea859be2007-06-22 14:59:07 +00002234 case ISD::SIGN_EXTEND_INREG:
2235 // Max of the input and what this extends.
Dan Gohmand1996362010-01-09 02:13:55 +00002236 Tmp =
2237 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002238 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002239
Dan Gohmanea859be2007-06-22 14:59:07 +00002240 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2241 return std::max(Tmp, Tmp2);
2242
2243 case ISD::SRA:
2244 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2245 // SRA X, C -> adds C sign bits.
2246 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002247 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002248 if (Tmp > VTBits) Tmp = VTBits;
2249 }
2250 return Tmp;
2251 case ISD::SHL:
2252 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2253 // shl destroys sign bits.
2254 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002255 if (C->getZExtValue() >= VTBits || // Bad shift.
2256 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2257 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002258 }
2259 break;
2260 case ISD::AND:
2261 case ISD::OR:
2262 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002263 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002264 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002265 if (Tmp != 1) {
2266 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2267 FirstAnswer = std::min(Tmp, Tmp2);
2268 // We computed what we know about the sign bits as our first
2269 // answer. Now proceed to the generic code that uses
2270 // ComputeMaskedBits, and pick whichever answer is better.
2271 }
2272 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002273
2274 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002275 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002276 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002277 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002278 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002279
2280 case ISD::SADDO:
2281 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002282 case ISD::SSUBO:
2283 case ISD::USUBO:
2284 case ISD::SMULO:
2285 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002286 if (Op.getResNo() != 1)
2287 break;
Duncan Sands03228082008-11-23 15:47:28 +00002288 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002289 case ISD::SETCC:
2290 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002291 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands03228082008-11-23 15:47:28 +00002292 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002293 return VTBits;
2294 break;
2295 case ISD::ROTL:
2296 case ISD::ROTR:
2297 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002298 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002299
Dan Gohmanea859be2007-06-22 14:59:07 +00002300 // Handle rotate right by N like a rotate left by 32-N.
2301 if (Op.getOpcode() == ISD::ROTR)
2302 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2303
2304 // If we aren't rotating out all of the known-in sign bits, return the
2305 // number that are left. This handles rotl(sext(x), 1) for example.
2306 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2307 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2308 }
2309 break;
2310 case ISD::ADD:
2311 // Add can have at most one carry bit. Thus we know that the output
2312 // is, at worst, one more bit than the inputs.
2313 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2314 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002315
Dan Gohmanea859be2007-06-22 14:59:07 +00002316 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002317 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002318 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002319 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002320 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002321
Dan Gohmanea859be2007-06-22 14:59:07 +00002322 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2323 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002324 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002325 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002326
Dan Gohmanea859be2007-06-22 14:59:07 +00002327 // If we are subtracting one from a positive number, there is no carry
2328 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002329 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002330 return Tmp;
2331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002332
Dan Gohmanea859be2007-06-22 14:59:07 +00002333 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2334 if (Tmp2 == 1) return 1;
David Blaikie4d6ccb52012-01-20 21:51:11 +00002335 return std::min(Tmp, Tmp2)-1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002336
Dan Gohmanea859be2007-06-22 14:59:07 +00002337 case ISD::SUB:
2338 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2339 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Dan Gohmanea859be2007-06-22 14:59:07 +00002341 // Handle NEG.
2342 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002343 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002344 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002345 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002346 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2347 // sign bits set.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002348 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00002349 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002350
Dan Gohmanea859be2007-06-22 14:59:07 +00002351 // If the input is known to be positive (the sign bit is known clear),
2352 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002353 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002354 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002355
Dan Gohmanea859be2007-06-22 14:59:07 +00002356 // Otherwise, we treat this like a SUB.
2357 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002358
Dan Gohmanea859be2007-06-22 14:59:07 +00002359 // Sub can have at most one carry bit. Thus we know that the output
2360 // is, at worst, one more bit than the inputs.
2361 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2362 if (Tmp == 1) return 1; // Early out.
David Blaikie4d6ccb52012-01-20 21:51:11 +00002363 return std::min(Tmp, Tmp2)-1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002364 case ISD::TRUNCATE:
2365 // FIXME: it's tricky to do anything useful for this, but it is an important
2366 // case for targets like X86.
2367 break;
2368 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002369
Nadav Rotem77451752013-03-20 22:53:44 +00002370 // If we are looking at the loaded value of the SDNode.
2371 if (Op.getResNo() == 0) {
2372 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2373 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2374 unsigned ExtType = LD->getExtensionType();
2375 switch (ExtType) {
2376 default: break;
2377 case ISD::SEXTLOAD: // '17' bits known
2378 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2379 return VTBits-Tmp+1;
2380 case ISD::ZEXTLOAD: // '16' bits known
2381 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2382 return VTBits-Tmp;
2383 }
Dan Gohmanea859be2007-06-22 14:59:07 +00002384 }
2385 }
2386
2387 // Allow the target to implement this method for its nodes.
2388 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002389 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002390 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2391 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002392 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002393 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002394 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002395
Dan Gohmanea859be2007-06-22 14:59:07 +00002396 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2397 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002398 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002399 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002400
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00002401 APInt Mask;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002402 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002403 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002404 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002405 Mask = KnownOne;
2406 } else {
2407 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002408 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002409 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002410
Dan Gohmanea859be2007-06-22 14:59:07 +00002411 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2412 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002413 Mask = ~Mask;
2414 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002415 // Return # leading zeros. We use 'min' here in case Val was zero before
2416 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002417 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002418}
2419
Chris Lattner0a9481f2011-02-13 22:25:43 +00002420/// isBaseWithConstantOffset - Return true if the specified operand is an
2421/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2422/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2423/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002424/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner0a9481f2011-02-13 22:25:43 +00002425bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2426 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2427 !isa<ConstantSDNode>(Op.getOperand(1)))
2428 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002429
2430 if (Op.getOpcode() == ISD::OR &&
Chris Lattner0a9481f2011-02-13 22:25:43 +00002431 !MaskedValueIsZero(Op.getOperand(0),
2432 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2433 return false;
Owen Anderson95771af2011-02-25 21:41:48 +00002434
Chris Lattner0a9481f2011-02-13 22:25:43 +00002435 return true;
2436}
2437
2438
Dan Gohman8d44b282009-09-03 20:34:31 +00002439bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2440 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002441 if (getTarget().Options.NoNaNsFPMath)
Dan Gohman8d44b282009-09-03 20:34:31 +00002442 return true;
2443
2444 // If the value is a constant, we can obviously see if it is a NaN or not.
2445 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2446 return !C->getValueAPF().isNaN();
2447
2448 // TODO: Recognize more cases here.
2449
2450 return false;
2451}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002452
Dan Gohmane8326932010-02-24 06:52:40 +00002453bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2454 // If the value is a constant, we can obviously see if it is a zero or not.
2455 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2456 return !C->isZero();
2457
2458 // TODO: Recognize more cases here.
Evan Chengb5a55d92011-05-24 01:48:22 +00002459 switch (Op.getOpcode()) {
2460 default: break;
2461 case ISD::OR:
2462 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2463 return !C->isNullValue();
2464 break;
2465 }
Dan Gohmane8326932010-02-24 06:52:40 +00002466
2467 return false;
2468}
2469
2470bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2471 // Check the obvious case.
2472 if (A == B) return true;
2473
2474 // For for negative and positive zero.
2475 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2476 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2477 if (CA->isZero() && CB->isZero()) return true;
2478
2479 // Otherwise they may not be equal.
2480 return false;
2481}
2482
Chris Lattnerc3aae252005-01-07 07:46:32 +00002483/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002484///
Andrew Trickac6d9be2013-05-25 02:42:55 +00002485SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002486 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002487 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002488 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002489 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002490 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002491
Jack Carter3af4d252013-09-09 22:02:08 +00002492 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(),
2493 DL.getDebugLoc(), getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002494 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002495
Chris Lattner4a283e92006-08-11 18:38:11 +00002496 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002497#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002498 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002499#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002500 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002501}
2502
Andrew Trickac6d9be2013-05-25 02:42:55 +00002503SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002504 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002505 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002506 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002507 const APInt &Val = C->getAPIntValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002508 switch (Opcode) {
2509 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002510 case ISD::SIGN_EXTEND:
Jay Foad40f8f622010-12-07 08:25:19 +00002511 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002512 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002513 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002514 case ISD::TRUNCATE:
Jay Foad40f8f622010-12-07 08:25:19 +00002515 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002516 case ISD::UINT_TO_FP:
2517 case ISD::SINT_TO_FP: {
Tim Northover0a29cb02013-01-22 09:46:31 +00002518 APFloat apf(EVTToAPFloatSemantics(VT),
2519 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002520 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002521 Opcode==ISD::SINT_TO_FP,
2522 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002523 return getConstantFP(apf, VT);
2524 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002525 case ISD::BITCAST:
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover0a29cb02013-01-22 09:46:31 +00002527 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002528 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover0a29cb02013-01-22 09:46:31 +00002529 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002530 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002531 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002532 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002533 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002534 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002535 case ISD::CTLZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002536 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002537 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002538 case ISD::CTTZ:
Chandler Carruth63974b22011-12-13 01:56:10 +00002539 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman6c231502008-02-29 01:47:35 +00002540 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002541 }
2542 }
2543
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002544 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002545 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002546 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigande669c932012-10-29 18:35:49 +00002547 switch (Opcode) {
2548 case ISD::FNEG:
2549 V.changeSign();
2550 return getConstantFP(V, VT);
2551 case ISD::FABS:
2552 V.clearSign();
2553 return getConstantFP(V, VT);
2554 case ISD::FCEIL: {
2555 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2556 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002557 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002558 break;
2559 }
2560 case ISD::FTRUNC: {
2561 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2562 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002563 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002564 break;
2565 }
2566 case ISD::FFLOOR: {
2567 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2568 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002569 return getConstantFP(V, VT);
Ulrich Weigande669c932012-10-29 18:35:49 +00002570 break;
2571 }
2572 case ISD::FP_EXTEND: {
2573 bool ignored;
2574 // This can return overflow, underflow, or inexact; we don't care.
2575 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00002576 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigande669c932012-10-29 18:35:49 +00002577 APFloat::rmNearestTiesToEven, &ignored);
2578 return getConstantFP(V, VT);
2579 }
2580 case ISD::FP_TO_SINT:
2581 case ISD::FP_TO_UINT: {
2582 integerPart x[2];
2583 bool ignored;
2584 assert(integerPartWidth >= 64);
2585 // FIXME need to be more flexible about rounding mode.
2586 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2587 Opcode==ISD::FP_TO_SINT,
2588 APFloat::rmTowardZero, &ignored);
2589 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002590 break;
Ulrich Weigande669c932012-10-29 18:35:49 +00002591 APInt api(VT.getSizeInBits(), x);
2592 return getConstant(api, VT);
2593 }
2594 case ISD::BITCAST:
2595 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2596 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2597 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2598 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2599 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002600 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002601 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002602
Gabor Greifba36cb52008-08-28 21:40:38 +00002603 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002604 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002605 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002606 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002607 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002608 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002609 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002610 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002611 assert(VT.isFloatingPoint() &&
2612 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002613 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohman2e141d72009-12-14 23:40:38 +00002614 assert((!VT.isVector() ||
2615 VT.getVectorNumElements() ==
2616 Operand.getValueType().getVectorNumElements()) &&
2617 "Vector element count mismatch!");
Chris Lattner5d03f212008-03-11 06:21:08 +00002618 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002619 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002620 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002621 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002622 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002623 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002624 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002625 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2626 "Invalid sext node, dst < src!");
2627 assert((!VT.isVector() ||
2628 VT.getVectorNumElements() ==
2629 Operand.getValueType().getVectorNumElements()) &&
2630 "Vector element count mismatch!");
Nadav Rotem0c8607b2013-01-20 08:35:56 +00002631 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2632 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2633 else if (OpOpcode == ISD::UNDEF)
Evan Chengbf34a5e2011-03-15 02:22:10 +00002634 // sext(undef) = 0, because the top bits will all be the same.
2635 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002636 break;
2637 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002638 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002639 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002640 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002641 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2642 "Invalid zext node, dst < src!");
2643 assert((!VT.isVector() ||
2644 VT.getVectorNumElements() ==
2645 Operand.getValueType().getVectorNumElements()) &&
2646 "Vector element count mismatch!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002647 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002648 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002649 Operand.getNode()->getOperand(0));
Evan Chengbf34a5e2011-03-15 02:22:10 +00002650 else if (OpOpcode == ISD::UNDEF)
2651 // zext(undef) = 0, because the top bits will be zero.
2652 return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002653 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002654 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002655 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002656 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002657 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohman2e141d72009-12-14 23:40:38 +00002658 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2659 "Invalid anyext node, dst < src!");
2660 assert((!VT.isVector() ||
2661 VT.getVectorNumElements() ==
2662 Operand.getValueType().getVectorNumElements()) &&
2663 "Vector element count mismatch!");
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002664
Dan Gohman9e86a732010-06-18 00:08:30 +00002665 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2666 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner4ed11b42005-09-02 00:17:32 +00002667 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002668 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Cheng5ae1da92011-03-14 18:15:55 +00002669 else if (OpOpcode == ISD::UNDEF)
2670 return getUNDEF(VT);
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002671
2672 // (ext (trunx x)) -> x
2673 if (OpOpcode == ISD::TRUNCATE) {
2674 SDValue OpOp = Operand.getNode()->getOperand(0);
2675 if (OpOp.getValueType() == VT)
2676 return OpOp;
2677 }
Chris Lattner4ed11b42005-09-02 00:17:32 +00002678 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002679 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002680 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002681 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002682 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohman2e141d72009-12-14 23:40:38 +00002683 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2684 "Invalid truncate node, src < dst!");
2685 assert((!VT.isVector() ||
2686 VT.getVectorNumElements() ==
2687 Operand.getValueType().getVectorNumElements()) &&
2688 "Vector element count mismatch!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002689 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002690 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002691 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2692 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002693 // If the source is smaller than the dest, we still need an extend.
Dan Gohman2e141d72009-12-14 23:40:38 +00002694 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2695 .bitsLT(VT.getScalarType()))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002696 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002697 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002698 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper799ea5c2012-01-15 01:05:11 +00002699 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002700 }
Craig Topper799ea5c2012-01-15 01:05:11 +00002701 if (OpOpcode == ISD::UNDEF)
2702 return getUNDEF(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002703 break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002704 case ISD::BITCAST:
Chris Lattner35481892005-12-23 00:16:34 +00002705 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002706 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002707 && "Cannot BITCAST between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002708 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002709 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2710 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002711 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002712 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002713 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002714 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002715 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002716 (VT.getVectorElementType() == Operand.getValueType() ||
2717 (VT.getVectorElementType().isInteger() &&
2718 Operand.getValueType().isInteger() &&
2719 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002720 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002721 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002722 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002723 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2724 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2725 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2726 Operand.getConstantOperandVal(1) == 0 &&
2727 Operand.getOperand(0).getValueType() == VT)
2728 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002729 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002730 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002731 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002732 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002733 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002734 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002735 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002736 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002737 break;
2738 case ISD::FABS:
2739 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002740 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002741 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002742 }
2743
Chris Lattner43247a12005-08-25 19:12:10 +00002744 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002745 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002746 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002747 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002748 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002749 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002750 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002751 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002752 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00002753
Jack Carter3af4d252013-09-09 22:02:08 +00002754 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2755 DL.getDebugLoc(), VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002756 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002757 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00002758 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
2759 DL.getDebugLoc(), VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002760 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002761
Chris Lattnerc3aae252005-01-07 07:46:32 +00002762 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002763#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00002764 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002765#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002766 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002767}
2768
Benjamin Kramer49693102013-02-04 15:19:18 +00002769SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2770 SDNode *Cst1, SDNode *Cst2) {
2771 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2772 SmallVector<SDValue, 4> Outputs;
2773 EVT SVT = VT.getScalarType();
Bill Wendling688d1c42008-09-24 10:16:24 +00002774
Benjamin Kramer49693102013-02-04 15:19:18 +00002775 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2776 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
2777 if (Scalar1 && Scalar2) {
2778 // Scalar instruction.
2779 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
2780 } else {
2781 // For vectors extract each constant element into Inputs so we can constant
2782 // fold them individually.
2783 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2784 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2785 if (!BV1 || !BV2)
2786 return SDValue();
2787
2788 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2789
2790 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2791 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2792 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2793 if (!V1 || !V2) // Not a constant, bail.
2794 return SDValue();
2795
2796 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2797 // FIXME: This is valid and could be handled by truncating the APInts.
2798 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2799 return SDValue();
2800
2801 Inputs.push_back(std::make_pair(V1, V2));
2802 }
Bill Wendling688d1c42008-09-24 10:16:24 +00002803 }
2804
Benjamin Kramer49693102013-02-04 15:19:18 +00002805 // We have a number of constant values, constant fold them element by element.
2806 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2807 const APInt &C1 = Inputs[I].first->getAPIntValue();
2808 const APInt &C2 = Inputs[I].second->getAPIntValue();
2809
2810 switch (Opcode) {
2811 case ISD::ADD:
2812 Outputs.push_back(getConstant(C1 + C2, SVT));
2813 break;
2814 case ISD::SUB:
2815 Outputs.push_back(getConstant(C1 - C2, SVT));
2816 break;
2817 case ISD::MUL:
2818 Outputs.push_back(getConstant(C1 * C2, SVT));
2819 break;
2820 case ISD::UDIV:
2821 if (!C2.getBoolValue())
2822 return SDValue();
2823 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2824 break;
2825 case ISD::UREM:
2826 if (!C2.getBoolValue())
2827 return SDValue();
2828 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2829 break;
2830 case ISD::SDIV:
2831 if (!C2.getBoolValue())
2832 return SDValue();
2833 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2834 break;
2835 case ISD::SREM:
2836 if (!C2.getBoolValue())
2837 return SDValue();
2838 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2839 break;
2840 case ISD::AND:
2841 Outputs.push_back(getConstant(C1 & C2, SVT));
2842 break;
2843 case ISD::OR:
2844 Outputs.push_back(getConstant(C1 | C2, SVT));
2845 break;
2846 case ISD::XOR:
2847 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2848 break;
2849 case ISD::SHL:
2850 Outputs.push_back(getConstant(C1 << C2, SVT));
2851 break;
2852 case ISD::SRL:
2853 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2854 break;
2855 case ISD::SRA:
2856 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2857 break;
2858 case ISD::ROTL:
2859 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2860 break;
2861 case ISD::ROTR:
2862 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2863 break;
2864 default:
2865 return SDValue();
2866 }
2867 }
2868
2869 // Handle the scalar case first.
Silviu Baranga02066832013-04-25 09:32:33 +00002870 if (Scalar1 && Scalar2)
Benjamin Kramer49693102013-02-04 15:19:18 +00002871 return Outputs.back();
2872
2873 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002874 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer49693102013-02-04 15:19:18 +00002875 Outputs.size());
Bill Wendling688d1c42008-09-24 10:16:24 +00002876}
2877
Andrew Trickac6d9be2013-05-25 02:42:55 +00002878SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer49693102013-02-04 15:19:18 +00002879 SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002880 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2881 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002882 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002883 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002884 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002885 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2886 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002887 // Fold trivial token factors.
2888 if (N1.getOpcode() == ISD::EntryToken) return N2;
2889 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002890 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002891 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002892 case ISD::CONCAT_VECTORS:
Nadav Rotemb87bdac2012-07-15 08:38:23 +00002893 // Concat of UNDEFs is UNDEF.
2894 if (N1.getOpcode() == ISD::UNDEF &&
2895 N2.getOpcode() == ISD::UNDEF)
2896 return getUNDEF(VT);
2897
Dan Gohman7f8613e2008-08-14 20:04:46 +00002898 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2899 // one big BUILD_VECTOR.
2900 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2901 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00002902 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2903 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00002904 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002905 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002906 }
2907 break;
Chris Lattner76365122005-01-16 02:23:22 +00002908 case ISD::AND:
Dale Johannesen78995512010-05-15 18:38:02 +00002909 assert(VT.isInteger() && "This operator does not apply to FP types!");
2910 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002911 N1.getValueType() == VT && "Binary operator types must match!");
2912 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2913 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002914 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002915 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002916 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2917 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002918 break;
Chris Lattner76365122005-01-16 02:23:22 +00002919 case ISD::OR:
2920 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002921 case ISD::ADD:
2922 case ISD::SUB:
Dale Johannesen78995512010-05-15 18:38:02 +00002923 assert(VT.isInteger() && "This operator does not apply to FP types!");
2924 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002925 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002926 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2927 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002928 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002929 return N1;
2930 break;
Chris Lattner76365122005-01-16 02:23:22 +00002931 case ISD::UDIV:
2932 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002933 case ISD::MULHU:
2934 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002935 case ISD::MUL:
2936 case ISD::SDIV:
2937 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002938 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesen78995512010-05-15 18:38:02 +00002939 assert(N1.getValueType() == N2.getValueType() &&
2940 N1.getValueType() == VT && "Binary operator types must match!");
2941 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002942 case ISD::FADD:
2943 case ISD::FSUB:
2944 case ISD::FMUL:
2945 case ISD::FDIV:
2946 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002947 if (getTarget().Options.UnsafeFPMath) {
Dan Gohmana90c8e62009-01-23 19:10:37 +00002948 if (Opcode == ISD::FADD) {
2949 // 0+x --> x
2950 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2951 if (CFP->getValueAPF().isZero())
2952 return N2;
2953 // x+0 --> x
2954 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2955 if (CFP->getValueAPF().isZero())
2956 return N1;
2957 } else if (Opcode == ISD::FSUB) {
2958 // x-0 --> x
2959 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2960 if (CFP->getValueAPF().isZero())
2961 return N1;
Michael Ilseman10def392012-09-10 17:00:37 +00002962 } else if (Opcode == ISD::FMUL) {
2963 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2964 SDValue V = N2;
2965
2966 // If the first operand isn't the constant, try the second
2967 if (!CFP) {
2968 CFP = dyn_cast<ConstantFPSDNode>(N2);
2969 V = N1;
2970 }
2971
2972 if (CFP) {
2973 // 0*x --> 0
2974 if (CFP->isZero())
2975 return SDValue(CFP,0);
2976 // 1*x --> x
2977 if (CFP->isExactlyValue(1.0))
2978 return V;
2979 }
Dan Gohmana90c8e62009-01-23 19:10:37 +00002980 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002981 }
Dale Johannesen78995512010-05-15 18:38:02 +00002982 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002983 assert(N1.getValueType() == N2.getValueType() &&
2984 N1.getValueType() == VT && "Binary operator types must match!");
2985 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002986 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2987 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002988 N1.getValueType().isFloatingPoint() &&
2989 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002990 "Invalid FCOPYSIGN!");
2991 break;
Chris Lattner76365122005-01-16 02:23:22 +00002992 case ISD::SHL:
2993 case ISD::SRA:
2994 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002995 case ISD::ROTL:
2996 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002997 assert(VT == N1.getValueType() &&
2998 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002999 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00003000 "Shifts only work on integers");
Michael Liaoa6b20ce2013-03-01 18:40:30 +00003001 assert((!VT.isVector() || VT == N2.getValueType()) &&
3002 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere0751182011-02-13 19:09:16 +00003003 // Verify that the shift amount VT is bit enough to hold valid shift
3004 // amounts. This catches things like trying to shift an i1024 value by an
3005 // i8, which is easy to fall into in generic code that uses
3006 // TLI.getShiftAmount().
3007 assert(N2.getValueType().getSizeInBits() >=
Owen Anderson95771af2011-02-25 21:41:48 +00003008 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere0751182011-02-13 19:09:16 +00003009 "Invalid use of small shift amount with oversized value!");
Chris Lattner349db172008-07-02 17:01:57 +00003010
3011 // Always fold shifts of i1 values so the code generator doesn't need to
3012 // handle them. Since we know the size of the shift has to be less than the
3013 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00003014 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00003015 return N1;
Evan Chengd40d03e2010-01-06 19:38:29 +00003016 if (N2C && N2C->isNullValue())
3017 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00003018 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00003019 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003020 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003021 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003022 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003023 "Cannot FP_ROUND_INREG integer types");
Dan Gohmand1996362010-01-09 02:13:55 +00003024 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003025 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003026 "type is vector!");
3027 assert((!EVT.isVector() ||
3028 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3029 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003030 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sands17001ce2011-10-18 12:44:00 +00003031 (void)EVT;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003032 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00003033 break;
3034 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003035 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003036 assert(VT.isFloatingPoint() &&
3037 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003038 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00003039 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003040 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00003041 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00003042 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003043 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00003044 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003045 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003046 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003047 "Cannot *_EXTEND_INREG FP types");
Dan Gohman87862e72009-12-11 21:31:27 +00003048 assert(!EVT.isVector() &&
3049 "AssertSExt/AssertZExt type should be the vector element type "
3050 "rather than the vector type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00003051 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00003052 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003053 break;
3054 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003055 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00003056 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00003057 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003058 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003059 "Cannot *_EXTEND_INREG FP types");
Dan Gohmand1996362010-01-09 02:13:55 +00003060 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003061 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohmand1996362010-01-09 02:13:55 +00003062 "type is vector!");
3063 assert((!EVT.isVector() ||
3064 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
3065 "Vector element counts must match in SIGN_EXTEND_INREG");
3066 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003067 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00003068
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003069 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00003070 APInt Val = N1C->getAPIntValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003071 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00003072 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00003073 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00003074 return getConstant(Val, VT);
3075 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003076 break;
3077 }
3078 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00003079 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
3080 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00003081 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003082
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003083 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3084 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00003085 if (N2C &&
3086 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003087 N1.getNumOperands() > 0) {
3088 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00003089 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003090 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003091 N1.getOperand(N2C->getZExtValue() / Factor),
3092 getConstant(N2C->getZExtValue() % Factor,
3093 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003094 }
3095
3096 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3097 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00003098 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3099 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy8cd08bf2012-09-10 14:01:21 +00003100
3101 if (VT != Elt.getValueType())
Bob Wilsonb1303d02009-04-13 22:05:19 +00003102 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy8cd08bf2012-09-10 14:01:21 +00003103 // are promoted and implicitly truncated, and the result implicitly
3104 // extended. Make that explicit here.
3105 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilseman06b96902012-09-10 16:56:31 +00003106
Bob Wilsonb1303d02009-04-13 22:05:19 +00003107 return Elt;
3108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003109
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003110 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3111 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00003112 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wang87c46d82010-02-01 22:15:09 +00003113 // If the indices are the same, return the inserted element else
3114 // if the indices are known different, extract the element from
Dan Gohman197e88f2009-01-29 16:10:46 +00003115 // the original vector.
Bill Wendlingd71bb562010-04-30 22:19:17 +00003116 SDValue N1Op2 = N1.getOperand(2);
3117 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3118
3119 if (N1Op2C && N2C) {
3120 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3121 if (VT == N1.getOperand(1).getValueType())
3122 return N1.getOperand(1);
3123 else
3124 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3125 }
3126
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003127 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingd71bb562010-04-30 22:19:17 +00003128 }
Dan Gohman6ab64222008-08-13 21:51:37 +00003129 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003130 break;
3131 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003132 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00003133 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3134 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman6cdc1f42011-08-02 18:38:35 +00003135 N1.getValueType() != VT &&
Duncan Sands041cde22008-06-25 20:24:48 +00003136 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00003137
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003138 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3139 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00003140 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003141 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003142 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00003143
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003144 // EXTRACT_ELEMENT of a constant int is also very common.
3145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003146 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003147 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00003148 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3149 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003150 }
3151 break;
David Greene91585092011-01-26 15:38:49 +00003152 case ISD::EXTRACT_SUBVECTOR: {
3153 SDValue Index = N2;
3154 if (VT.isSimple() && N1.getValueType().isSimple()) {
3155 assert(VT.isVector() && N1.getValueType().isVector() &&
3156 "Extract subvector VTs must be a vectors!");
Jack Carter3af4d252013-09-09 22:02:08 +00003157 assert(VT.getVectorElementType() ==
3158 N1.getValueType().getVectorElementType() &&
David Greene91585092011-01-26 15:38:49 +00003159 "Extract subvector VTs must have the same element type!");
Craig Topper0ff11902013-08-15 02:44:19 +00003160 assert(VT.getSimpleVT() <= N1.getSimpleValueType() &&
David Greene91585092011-01-26 15:38:49 +00003161 "Extract subvector must be from larger vector to smaller vector!");
3162
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003163 if (isa<ConstantSDNode>(Index.getNode())) {
3164 assert((VT.getVectorNumElements() +
3165 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greene91585092011-01-26 15:38:49 +00003166 <= N1.getValueType().getVectorNumElements())
3167 && "Extract subvector overflow!");
3168 }
3169
3170 // Trivial extraction.
Craig Topper0ff11902013-08-15 02:44:19 +00003171 if (VT.getSimpleVT() == N1.getSimpleValueType())
David Greene91585092011-01-26 15:38:49 +00003172 return N1;
3173 }
Duncan Sandsf83b1f62008-02-20 17:38:09 +00003174 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003175 }
David Greene91585092011-01-26 15:38:49 +00003176 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003177
Benjamin Kramer49693102013-02-04 15:19:18 +00003178 // Perform trivial constant folding.
3179 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3180 if (SV.getNode()) return SV;
3181
3182 // Canonicalize constant to RHS if commutative.
3183 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3184 std::swap(N1C, N2C);
3185 std::swap(N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003186 }
3187
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003188 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003189 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3190 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00003191 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003192 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer49693102013-02-04 15:19:18 +00003193 // Canonicalize constant to RHS if commutative.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003194 std::swap(N1CFP, N2CFP);
3195 std::swap(N1, N2);
Ulrich Weigande669c932012-10-29 18:35:49 +00003196 } else if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003197 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3198 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003199 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003200 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003201 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00003202 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003203 return getConstantFP(V1, VT);
3204 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003205 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003206 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3207 if (s!=APFloat::opInvalidOp)
3208 return getConstantFP(V1, VT);
3209 break;
3210 case ISD::FMUL:
3211 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3212 if (s!=APFloat::opInvalidOp)
3213 return getConstantFP(V1, VT);
3214 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003215 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003216 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3217 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3218 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003219 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00003220 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003221 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3222 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3223 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003224 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003225 case ISD::FCOPYSIGN:
3226 V1.copySign(V2);
3227 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003228 default: break;
3229 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003230 }
Owen Anderson06886aa2012-04-10 22:46:53 +00003231
3232 if (Opcode == ISD::FP_ROUND) {
3233 APFloat V = N1CFP->getValueAPF(); // make copy
3234 bool ignored;
3235 // This can return overflow, underflow, or inexact; we don't care.
3236 // FIXME need to be more flexible about rounding mode.
Tim Northover0a29cb02013-01-22 09:46:31 +00003237 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson06886aa2012-04-10 22:46:53 +00003238 APFloat::rmNearestTiesToEven, &ignored);
3239 return getConstantFP(V, VT);
3240 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003242
Chris Lattner62b57722006-04-20 05:39:12 +00003243 // Canonicalize an UNDEF to the RHS, even over a constant.
3244 if (N1.getOpcode() == ISD::UNDEF) {
3245 if (isCommutativeBinOp(Opcode)) {
3246 std::swap(N1, N2);
3247 } else {
3248 switch (Opcode) {
3249 case ISD::FP_ROUND_INREG:
3250 case ISD::SIGN_EXTEND_INREG:
3251 case ISD::SUB:
3252 case ISD::FSUB:
3253 case ISD::FDIV:
3254 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003255 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00003256 return N1; // fold op(undef, arg2) -> undef
3257 case ISD::UDIV:
3258 case ISD::SDIV:
3259 case ISD::UREM:
3260 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003261 case ISD::SRL:
3262 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003263 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003264 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3265 // For vectors, we can't easily build an all zero vector, just return
3266 // the LHS.
3267 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00003268 }
3269 }
3270 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003271
3272 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00003273 if (N2.getOpcode() == ISD::UNDEF) {
3274 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00003275 case ISD::XOR:
3276 if (N1.getOpcode() == ISD::UNDEF)
3277 // Handle undef ^ undef -> 0 special case. This is a common
3278 // idiom (misuse).
3279 return getConstant(0, VT);
3280 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00003281 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00003282 case ISD::ADDC:
3283 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00003284 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00003285 case ISD::UDIV:
3286 case ISD::SDIV:
3287 case ISD::UREM:
3288 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00003289 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00003290 case ISD::FADD:
3291 case ISD::FSUB:
3292 case ISD::FMUL:
3293 case ISD::FDIV:
3294 case ISD::FREM:
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003295 if (getTarget().Options.UnsafeFPMath)
Dan Gohman77b81fe2009-06-04 17:12:12 +00003296 return N2;
3297 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003298 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00003299 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00003300 case ISD::SRL:
3301 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003302 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00003303 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3304 // For vectors, we can't easily build an all zero vector, just return
3305 // the LHS.
3306 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003307 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00003308 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003309 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00003310 // For vectors, we can't easily build an all one vector, just return
3311 // the LHS.
3312 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00003313 case ISD::SRA:
3314 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00003315 }
3316 }
Chris Lattner15e4b012005-07-10 00:07:11 +00003317
Chris Lattner27e9b412005-05-11 18:57:39 +00003318 // Memoize this node if possible.
3319 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003320 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003321 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003322 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00003323 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003324 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003325 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003326 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003327 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003328
Jack Carter3af4d252013-09-09 22:02:08 +00003329 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3330 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00003331 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00003332 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003333 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
3334 DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00003335 }
3336
Chris Lattnerc3aae252005-01-07 07:46:32 +00003337 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003338#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003339 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003340#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003341 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003342}
3343
Andrew Trickac6d9be2013-05-25 02:42:55 +00003344SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003345 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003346 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00003347 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003348 switch (Opcode) {
Owen Anderson58dcd202013-05-09 22:27:13 +00003349 case ISD::FMA: {
3350 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3351 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3352 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3353 if (N1CFP && N2CFP && N3CFP) {
3354 APFloat V1 = N1CFP->getValueAPF();
3355 const APFloat &V2 = N2CFP->getValueAPF();
3356 const APFloat &V3 = N3CFP->getValueAPF();
3357 APFloat::opStatus s =
3358 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3359 if (s != APFloat::opInvalidOp)
3360 return getConstantFP(V1, VT);
3361 }
3362 break;
3363 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00003364 case ISD::CONCAT_VECTORS:
3365 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3366 // one big BUILD_VECTOR.
3367 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3368 N2.getOpcode() == ISD::BUILD_VECTOR &&
3369 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif481c4c02010-07-22 17:18:03 +00003370 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3371 N1.getNode()->op_end());
Dan Gohman403a8cd2010-06-21 19:47:52 +00003372 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3373 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00003374 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00003375 }
3376 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003377 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00003378 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00003379 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003380 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003381 break;
3382 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003383 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003384 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003385 if (N1C->getZExtValue())
David Blaikie4d6ccb52012-01-20 21:51:11 +00003386 return N2; // select true, X, Y -> X
3387 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003388 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003389
3390 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00003391 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00003392 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00003393 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenecfe33c42011-01-26 19:13:22 +00003394 case ISD::INSERT_SUBVECTOR: {
3395 SDValue Index = N3;
3396 if (VT.isSimple() && N1.getValueType().isSimple()
3397 && N2.getValueType().isSimple()) {
3398 assert(VT.isVector() && N1.getValueType().isVector() &&
3399 N2.getValueType().isVector() &&
3400 "Insert subvector VTs must be a vectors");
3401 assert(VT == N1.getValueType() &&
3402 "Dest and insert subvector source types must match!");
Craig Topper0ff11902013-08-15 02:44:19 +00003403 assert(N2.getSimpleValueType() <= N1.getSimpleValueType() &&
David Greenecfe33c42011-01-26 19:13:22 +00003404 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay9a14a362011-02-01 22:12:50 +00003405 if (isa<ConstantSDNode>(Index.getNode())) {
3406 assert((N2.getValueType().getVectorNumElements() +
3407 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenecfe33c42011-01-26 19:13:22 +00003408 <= VT.getVectorNumElements())
3409 && "Insert subvector overflow!");
3410 }
3411
3412 // Trivial insertion.
Craig Topper0ff11902013-08-15 02:44:19 +00003413 if (VT.getSimpleVT() == N2.getSimpleValueType())
David Greenecfe33c42011-01-26 19:13:22 +00003414 return N2;
3415 }
3416 break;
3417 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003418 case ISD::BITCAST:
Dan Gohman7f321562007-06-25 16:23:39 +00003419 // Fold bit_convert nodes from a type to themselves.
3420 if (N1.getValueType() == VT)
3421 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00003422 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003423 }
3424
Chris Lattner43247a12005-08-25 19:12:10 +00003425 // Memoize node if it doesn't produce a flag.
3426 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003427 SDVTList VTs = getVTList(VT);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003428 if (VT != MVT::Glue) {
Dan Gohman475871a2008-07-27 21:46:04 +00003429 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003430 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003431 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003432 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003433 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003434 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00003435
Jack Carter3af4d252013-09-09 22:02:08 +00003436 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3437 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00003438 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003439 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00003440 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
3441 DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00003442 }
Daniel Dunbar819309e2009-12-16 20:10:05 +00003443
Chris Lattnerc3aae252005-01-07 07:46:32 +00003444 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003445#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00003446 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003447#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003448 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003449}
3450
Andrew Trickac6d9be2013-05-25 02:42:55 +00003451SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003452 SDValue N1, SDValue N2, SDValue N3,
3453 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003454 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003455 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003456}
3457
Andrew Trickac6d9be2013-05-25 02:42:55 +00003458SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003459 SDValue N1, SDValue N2, SDValue N3,
3460 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003461 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003462 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00003463}
3464
Dan Gohman98ca4f22009-08-05 01:29:28 +00003465/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3466/// the incoming stack arguments to be loaded from the stack.
3467SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3468 SmallVector<SDValue, 8> ArgChains;
3469
3470 // Include the original chain at the beginning of the list. When this is
3471 // used by target LowerCall hooks, this helps legalize find the
3472 // CALLSEQ_BEGIN node.
3473 ArgChains.push_back(Chain);
3474
3475 // Add a chain value for each stack argument.
3476 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3477 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3478 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3479 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3480 if (FI->getIndex() < 0)
3481 ArgChains.push_back(SDValue(L, 1));
3482
3483 // Build a tokenfactor for all the chains.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003484 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003485 &ArgChains[0], ArgChains.size());
3486}
3487
Dan Gohman707e0182008-04-12 04:36:06 +00003488/// getMemsetValue - Vectorized representation of the memset value
3489/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00003490static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003491 SDLoc dl) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003492 assert(Value.getOpcode() != ISD::UNDEF);
3493
Chris Lattner5cf0b6e2010-02-24 22:44:06 +00003494 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003496 assert(C->getAPIntValue().getBitWidth() == 8);
3497 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands83ec4b62008-06-06 12:08:01 +00003498 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003499 return DAG.getConstant(Val, VT);
Tim Northover0a29cb02013-01-22 09:46:31 +00003500 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003501 }
Evan Chengf0df0312008-05-15 08:39:06 +00003502
Dale Johannesen0f502f62009-02-03 22:26:09 +00003503 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003504 if (NumBits > 8) {
3505 // Use a multiplication with 0x010101... to extend the input to the
3506 // required length.
Benjamin Kramerad4da0f2013-02-20 13:00:06 +00003507 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer8c06aa12011-01-02 19:44:58 +00003508 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengf0df0312008-05-15 08:39:06 +00003509 }
3510
3511 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003512}
3513
Dan Gohman707e0182008-04-12 04:36:06 +00003514/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3515/// used when a memcpy is turned into a memset when the source is a constant
3516/// string ptr.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003517static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattner18c7f802012-02-05 02:29:43 +00003518 const TargetLowering &TLI, StringRef Str) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003519 // Handle vector with all elements zero.
3520 if (Str.empty()) {
3521 if (VT.isInteger())
3522 return DAG.getConstant(0, VT);
Duncan Sandscdfad362010-11-03 12:17:33 +00003523 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng255f20f2010-04-01 06:04:33 +00003524 return DAG.getConstantFP(0.0, VT);
3525 else if (VT.isVector()) {
3526 unsigned NumElts = VT.getVectorNumElements();
3527 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003528 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng255f20f2010-04-01 06:04:33 +00003529 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3530 EltVT, NumElts)));
3531 } else
3532 llvm_unreachable("Expected type!");
Evan Cheng0ff39b32008-06-30 07:31:25 +00003533 }
3534
Duncan Sands83ec4b62008-06-06 12:08:01 +00003535 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Cheng4ff23d02013-01-10 22:13:27 +00003536 unsigned NumVTBits = VT.getSizeInBits();
3537 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattner18c7f802012-02-05 02:29:43 +00003538 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3539
Evan Cheng4ff23d02013-01-10 22:13:27 +00003540 APInt Val(NumVTBits, 0);
Chris Lattner18c7f802012-02-05 02:29:43 +00003541 if (TLI.isLittleEndian()) {
3542 for (unsigned i = 0; i != NumBytes; ++i)
3543 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3544 } else {
3545 for (unsigned i = 0; i != NumBytes; ++i)
3546 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman707e0182008-04-12 04:36:06 +00003547 }
Chris Lattner18c7f802012-02-05 02:29:43 +00003548
Evan Chenge07f85e2012-12-11 23:26:14 +00003549 // If the "cost" of materializing the integer immediate is 1 or free, then
3550 // it is cost effective to turn the load into the immediate.
Chandler Carruthe4b4edd2013-01-05 12:32:17 +00003551 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3552 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng376642e2012-12-10 23:21:26 +00003553 return DAG.getConstant(Val, VT);
3554 return SDValue(0, 0);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003555}
3556
Scott Michelfdc40a02009-02-17 22:15:04 +00003557/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003558///
Andrew Trickdd0fb012013-05-25 03:08:10 +00003559static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman707e0182008-04-12 04:36:06 +00003560 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003561 EVT VT = Base.getValueType();
Andrew Trickdd0fb012013-05-25 03:08:10 +00003562 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003563 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003564}
3565
Evan Chengf0df0312008-05-15 08:39:06 +00003566/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3567///
Chris Lattner18c7f802012-02-05 02:29:43 +00003568static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003569 unsigned SrcDelta = 0;
3570 GlobalAddressSDNode *G = NULL;
3571 if (Src.getOpcode() == ISD::GlobalAddress)
3572 G = cast<GlobalAddressSDNode>(Src);
3573 else if (Src.getOpcode() == ISD::ADD &&
3574 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3575 Src.getOperand(1).getOpcode() == ISD::Constant) {
3576 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003577 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003578 }
3579 if (!G)
3580 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003581
Chris Lattner18c7f802012-02-05 02:29:43 +00003582 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengf0df0312008-05-15 08:39:06 +00003583}
Dan Gohman707e0182008-04-12 04:36:06 +00003584
Evan Cheng255f20f2010-04-01 06:04:33 +00003585/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3586/// to replace the memset / memcpy. Return true if the number of memory ops
3587/// is below the threshold. It returns the types of the sequence of
3588/// memory ops to perform memset / memcpy by reference.
3589static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng255f20f2010-04-01 06:04:33 +00003590 unsigned Limit, uint64_t Size,
3591 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003592 bool IsMemset,
3593 bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00003594 bool MemcpyStrSrc,
Evan Cheng376642e2012-12-10 23:21:26 +00003595 bool AllowOverlap,
Evan Cheng255f20f2010-04-01 06:04:33 +00003596 SelectionDAG &DAG,
3597 const TargetLowering &TLI) {
3598 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3599 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopher882e1e12011-07-06 22:41:18 +00003600 // If 'SrcAlign' is zero, that means the memory operation does not need to
3601 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3602 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3603 // is the specified alignment of the memory operation. If it is zero, that
3604 // means it's possible to change the alignment of the destination.
3605 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3606 // not need to be loaded.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003607 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00003608 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4bcf0a92010-04-16 20:22:43 +00003609 DAG.getMachineFunction());
Evan Chengf0df0312008-05-15 08:39:06 +00003610
Chris Lattneracee6472010-03-07 07:45:08 +00003611 if (VT == MVT::Other) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00003612 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng255f20f2010-04-01 06:04:33 +00003613 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng18141ee2010-04-05 23:33:29 +00003614 VT = TLI.getPointerTy();
Evan Chengf0df0312008-05-15 08:39:06 +00003615 } else {
Evan Cheng255f20f2010-04-01 06:04:33 +00003616 switch (DstAlign & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 case 0: VT = MVT::i64; break;
3618 case 4: VT = MVT::i32; break;
3619 case 2: VT = MVT::i16; break;
3620 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003621 }
3622 }
3623
Owen Anderson766b5ef2009-08-11 21:59:30 +00003624 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003625 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003626 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003627 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003628
Duncan Sands8e4eb092008-06-08 20:54:56 +00003629 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003630 VT = LVT;
3631 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003632
Dan Gohman707e0182008-04-12 04:36:06 +00003633 unsigned NumMemOps = 0;
3634 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003635 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003636 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003637 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003638 EVT NewVT = VT;
Evan Cheng376642e2012-12-10 23:21:26 +00003639 unsigned NewVTSize;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003640
3641 bool Found = false;
Evan Cheng255f20f2010-04-01 06:04:33 +00003642 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng376642e2012-12-10 23:21:26 +00003643 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003644 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003645 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003646 Found = true;
3647 else if (NewVT == MVT::i64 &&
3648 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Cheng7d342672012-12-12 01:32:07 +00003649 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003650 // i64 is usually not legal on 32-bit targets, but f64 may be.
3651 NewVT = MVT::f64;
3652 Found = true;
Evan Cheng376642e2012-12-10 23:21:26 +00003653 }
Evan Cheng376642e2012-12-10 23:21:26 +00003654 }
3655
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003656 if (!Found) {
3657 do {
3658 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3659 if (NewVT == MVT::i8)
3660 break;
Evan Cheng7d342672012-12-12 01:32:07 +00003661 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng61f4dfe2012-12-12 00:42:09 +00003662 }
3663 NewVTSize = NewVT.getSizeInBits() / 8;
3664
Evan Cheng376642e2012-12-10 23:21:26 +00003665 // If the new VT cannot cover all of the remaining bits, then consider
3666 // issuing a (or a pair of) unaligned and overlapping load / store.
3667 // FIXME: Only does this for 64-bit or more since we don't have proper
3668 // cost model for unaligned load / store.
3669 bool Fast;
Evan Chenga16e49d2012-12-12 20:43:23 +00003670 if (NumMemOps && AllowOverlap &&
3671 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng376642e2012-12-10 23:21:26 +00003672 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3673 VTSize = Size;
3674 else {
3675 VT = NewVT;
3676 VTSize = NewVTSize;
Evan Chengf0df0312008-05-15 08:39:06 +00003677 }
Dan Gohman707e0182008-04-12 04:36:06 +00003678 }
Dan Gohman707e0182008-04-12 04:36:06 +00003679
Evan Chenga16e49d2012-12-12 20:43:23 +00003680 if (++NumMemOps > Limit)
3681 return false;
3682
Dan Gohman707e0182008-04-12 04:36:06 +00003683 MemOps.push_back(VT);
3684 Size -= VTSize;
3685 }
3686
3687 return true;
3688}
3689
Andrew Trickac6d9be2013-05-25 02:42:55 +00003690static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng87bd1912010-03-30 18:08:53 +00003691 SDValue Chain, SDValue Dst,
3692 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003693 unsigned Align, bool isVol,
3694 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003695 MachinePointerInfo DstPtrInfo,
3696 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003697 // Turn a memcpy of undef to nop.
3698 if (Src.getOpcode() == ISD::UNDEF)
3699 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003700
Dan Gohman21323f32008-05-29 19:42:22 +00003701 // Expand memcpy to a series of load and store ops if the size operand falls
3702 // below a certain threshold.
Duncan Sands69300a22010-11-05 15:20:29 +00003703 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3704 // rather than maybe a humongous number of loads and stores.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003705 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003706 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003707 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003708 MachineFunction &MF = DAG.getMachineFunction();
3709 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling67658342012-10-09 07:45:08 +00003710 bool OptSize =
Bill Wendling831737d2012-12-30 10:32:01 +00003711 MF.getFunction()->getAttributes().
3712 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003713 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3714 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3715 DstAlignCanChange = true;
3716 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3717 if (Align > SrcAlign)
3718 SrcAlign = Align;
Chris Lattner18c7f802012-02-05 02:29:43 +00003719 StringRef Str;
Evan Cheng255f20f2010-04-01 06:04:33 +00003720 bool CopyFromStr = isMemSrcFromString(Src, Str);
3721 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng05219282011-01-06 06:52:41 +00003722 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003723
Evan Cheng94107ba2010-04-01 18:19:11 +00003724 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng255f20f2010-04-01 06:04:33 +00003725 (DstAlignCanChange ? 0 : Align),
Evan Chengc3b0c342010-04-08 07:37:57 +00003726 (isZeroStr ? 0 : SrcAlign),
Evan Cheng946a3a92012-12-12 02:34:41 +00003727 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003728 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003729
Evan Cheng255f20f2010-04-01 06:04:33 +00003730 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003731 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003732 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hames2d95e432013-01-31 20:23:43 +00003733
3734 // Don't promote to an alignment that would require dynamic stack
Stephen Lin155615d2013-07-08 00:37:03 +00003735 // realignment.
Lang Hames2d95e432013-01-31 20:23:43 +00003736 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3737 if (!TRI->needsStackRealignment(MF))
3738 while (NewAlign > Align &&
3739 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3740 NewAlign /= 2;
3741
Evan Cheng255f20f2010-04-01 06:04:33 +00003742 if (NewAlign > Align) {
3743 // Give the stack frame object a larger alignment if needed.
3744 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3745 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3746 Align = NewAlign;
3747 }
3748 }
Dan Gohman707e0182008-04-12 04:36:06 +00003749
Dan Gohman475871a2008-07-27 21:46:04 +00003750 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003751 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003752 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003753 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003754 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003756 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003757
Evan Cheng376642e2012-12-10 23:21:26 +00003758 if (VTSize > Size) {
3759 // Issuing an unaligned load / store pair that overlaps with the previous
3760 // pair. Adjust the offset accordingly.
3761 assert(i == NumMemOps-1 && i != 0);
3762 SrcOff -= VTSize - Size;
3763 DstOff -= VTSize - Size;
3764 }
3765
Evan Cheng255f20f2010-04-01 06:04:33 +00003766 if (CopyFromStr &&
3767 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengf0df0312008-05-15 08:39:06 +00003768 // It's unlikely a store of a vector immediate can be done in a single
3769 // instruction. It would require a load from a constantpool first.
Evan Cheng255f20f2010-04-01 06:04:33 +00003770 // We only handle zero vectors here.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003771 // FIXME: Handle other cases where store of vector immediate is done in
3772 // a single instruction.
Chris Lattner18c7f802012-02-05 02:29:43 +00003773 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng376642e2012-12-10 23:21:26 +00003774 if (Value.getNode())
3775 Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003776 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng376642e2012-12-10 23:21:26 +00003777 DstPtrInfo.getWithOffset(DstOff), isVol,
3778 false, Align);
3779 }
3780
3781 if (!Store.getNode()) {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003782 // The type might not be legal for the target. This should only happen
3783 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003784 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3785 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003786 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003787 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003788 assert(NVT.bitsGE(VT));
Stuart Hastingsa9011292011-02-16 16:23:55 +00003789 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003790 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003791 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng255f20f2010-04-01 06:04:33 +00003792 MinAlign(SrcAlign, SrcOff));
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003793 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003794 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003795 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3796 false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003797 }
3798 OutChains.push_back(Store);
3799 SrcOff += VTSize;
3800 DstOff += VTSize;
Evan Cheng376642e2012-12-10 23:21:26 +00003801 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003802 }
3803
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003805 &OutChains[0], OutChains.size());
3806}
3807
Andrew Trickac6d9be2013-05-25 02:42:55 +00003808static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003809 SDValue Chain, SDValue Dst,
3810 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003811 unsigned Align, bool isVol,
3812 bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003813 MachinePointerInfo DstPtrInfo,
3814 MachinePointerInfo SrcPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003815 // Turn a memmove of undef to nop.
3816 if (Src.getOpcode() == ISD::UNDEF)
3817 return Chain;
Dan Gohman21323f32008-05-29 19:42:22 +00003818
3819 // Expand memmove to a series of load and store ops if the size operand falls
3820 // below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003821 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003822 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003823 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003824 MachineFunction &MF = DAG.getMachineFunction();
3825 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003826 bool OptSize = MF.getFunction()->getAttributes().
3827 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003828 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3829 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3830 DstAlignCanChange = true;
3831 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3832 if (Align > SrcAlign)
3833 SrcAlign = Align;
Evan Cheng05219282011-01-06 06:52:41 +00003834 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003835
Evan Cheng94107ba2010-04-01 18:19:11 +00003836 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng946a3a92012-12-12 02:34:41 +00003837 (DstAlignCanChange ? 0 : Align), SrcAlign,
3838 false, false, false, false, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003839 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003840
Evan Cheng255f20f2010-04-01 06:04:33 +00003841 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003842 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003843 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003844 if (NewAlign > Align) {
3845 // Give the stack frame object a larger alignment if needed.
3846 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3847 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3848 Align = NewAlign;
3849 }
3850 }
Dan Gohman21323f32008-05-29 19:42:22 +00003851
Evan Cheng255f20f2010-04-01 06:04:33 +00003852 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003853 SmallVector<SDValue, 8> LoadValues;
3854 SmallVector<SDValue, 8> LoadChains;
3855 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003856 unsigned NumMemOps = MemOps.size();
3857 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003858 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003859 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003860 SDValue Value;
Dan Gohman21323f32008-05-29 19:42:22 +00003861
Dale Johannesen0f502f62009-02-03 22:26:09 +00003862 Value = DAG.getLoad(VT, dl, Chain,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003863 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003864 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003865 false, false, SrcAlign);
Dan Gohman21323f32008-05-29 19:42:22 +00003866 LoadValues.push_back(Value);
3867 LoadChains.push_back(Value.getValue(1));
3868 SrcOff += VTSize;
3869 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003871 &LoadChains[0], LoadChains.size());
3872 OutChains.clear();
3873 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003874 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003875 unsigned VTSize = VT.getSizeInBits() / 8;
Rafael Espindola8819c842013-10-01 13:32:03 +00003876 SDValue Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003877
Dale Johannesen0f502f62009-02-03 22:26:09 +00003878 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Trickdd0fb012013-05-25 03:08:10 +00003879 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003880 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman21323f32008-05-29 19:42:22 +00003881 OutChains.push_back(Store);
3882 DstOff += VTSize;
3883 }
3884
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003886 &OutChains[0], OutChains.size());
3887}
3888
Serge Pavlov496f0242013-09-17 16:24:42 +00003889/// \brief Lower the call to 'memset' intrinsic function into a series of store
3890/// operations.
3891///
3892/// \param DAG Selection DAG where lowered code is placed.
3893/// \param dl Link to corresponding IR location.
3894/// \param Chain Control flow dependency.
3895/// \param Dst Pointer to destination memory location.
3896/// \param Src Value of byte to write into the memory.
3897/// \param Size Number of bytes to write.
3898/// \param Align Alignment of the destination in bytes.
3899/// \param isVol True if destination is volatile.
3900/// \param DstPtrInfo IR information on the memory pointer.
3901/// \returns New head in the control flow, if lowering was successful, empty
3902/// SDValue otherwise.
3903///
3904/// The function tries to replace 'llvm.memset' intrinsic with several store
3905/// operations and value calculation code. This is usually profitable for small
3906/// memory size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003907static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng255f20f2010-04-01 06:04:33 +00003908 SDValue Chain, SDValue Dst,
3909 SDValue Src, uint64_t Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003910 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00003911 MachinePointerInfo DstPtrInfo) {
Evan Chengf28f8bc2010-04-02 19:36:14 +00003912 // Turn a memset of undef to nop.
3913 if (Src.getOpcode() == ISD::UNDEF)
3914 return Chain;
Dan Gohman707e0182008-04-12 04:36:06 +00003915
3916 // Expand memset to a series of load/store ops if the size operand
3917 // falls below a certain threshold.
Evan Chengf28f8bc2010-04-02 19:36:14 +00003918 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003919 std::vector<EVT> MemOps;
Evan Cheng255f20f2010-04-01 06:04:33 +00003920 bool DstAlignCanChange = false;
Evan Cheng05219282011-01-06 06:52:41 +00003921 MachineFunction &MF = DAG.getMachineFunction();
3922 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling831737d2012-12-30 10:32:01 +00003923 bool OptSize = MF.getFunction()->getAttributes().
3924 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng255f20f2010-04-01 06:04:33 +00003925 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3926 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3927 DstAlignCanChange = true;
Lang Hames15701f82011-10-26 23:50:43 +00003928 bool IsZeroVal =
Evan Chengf28f8bc2010-04-02 19:36:14 +00003929 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng05219282011-01-06 06:52:41 +00003930 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng255f20f2010-04-01 06:04:33 +00003931 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng946a3a92012-12-12 02:34:41 +00003932 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003933 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003934
Evan Cheng255f20f2010-04-01 06:04:33 +00003935 if (DstAlignCanChange) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003936 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00003937 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng255f20f2010-04-01 06:04:33 +00003938 if (NewAlign > Align) {
3939 // Give the stack frame object a larger alignment if needed.
3940 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3941 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3942 Align = NewAlign;
3943 }
3944 }
3945
Dan Gohman475871a2008-07-27 21:46:04 +00003946 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003947 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003948 unsigned NumMemOps = MemOps.size();
Benjamin Kramer80220362011-01-02 19:57:05 +00003949
3950 // Find the largest store and generate the bit pattern for it.
3951 EVT LargestVT = MemOps[0];
3952 for (unsigned i = 1; i < NumMemOps; i++)
3953 if (MemOps[i].bitsGT(LargestVT))
3954 LargestVT = MemOps[i];
3955 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3956
Dan Gohman707e0182008-04-12 04:36:06 +00003957 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003958 EVT VT = MemOps[i];
Evan Cheng376642e2012-12-10 23:21:26 +00003959 unsigned VTSize = VT.getSizeInBits() / 8;
3960 if (VTSize > Size) {
3961 // Issuing an unaligned load / store pair that overlaps with the previous
3962 // pair. Adjust the offset accordingly.
3963 assert(i == NumMemOps-1 && i != 0);
3964 DstOff -= VTSize - Size;
3965 }
Benjamin Kramer80220362011-01-02 19:57:05 +00003966
3967 // If this store is smaller than the largest store see whether we can get
3968 // the smaller value for free with a truncate.
3969 SDValue Value = MemSetValue;
3970 if (VT.bitsLT(LargestVT)) {
3971 if (!LargestVT.isVector() && !VT.isVector() &&
3972 TLI.isTruncateFree(LargestVT, VT))
3973 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3974 else
3975 Value = getMemsetValue(Src, VT, DAG, dl);
3976 }
3977 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003978 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Trickdd0fb012013-05-25 03:08:10 +00003979 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattnere72f2022010-09-21 05:40:29 +00003980 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesenb4ac2852010-11-18 01:35:23 +00003981 isVol, false, Align);
Dan Gohman707e0182008-04-12 04:36:06 +00003982 OutChains.push_back(Store);
Benjamin Kramer80220362011-01-02 19:57:05 +00003983 DstOff += VT.getSizeInBits() / 8;
Evan Cheng376642e2012-12-10 23:21:26 +00003984 Size -= VTSize;
Dan Gohman707e0182008-04-12 04:36:06 +00003985 }
3986
Owen Anderson825b72b2009-08-11 20:47:22 +00003987 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003988 &OutChains[0], OutChains.size());
3989}
3990
Andrew Trickac6d9be2013-05-25 02:42:55 +00003991SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003992 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00003993 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00003994 MachinePointerInfo DstPtrInfo,
3995 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00003996 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00003997
3998 // Check to see if we should lower the memcpy to loads and stores first.
3999 // For cases within the target-specified limits, this is the best choice.
4000 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4001 if (ConstantSize) {
4002 // Memcpy with size zero? Just return the original chain.
4003 if (ConstantSize->isNullValue())
4004 return Chain;
4005
Evan Cheng255f20f2010-04-01 06:04:33 +00004006 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
4007 ConstantSize->getZExtValue(),Align,
Chris Lattnere72f2022010-09-21 05:40:29 +00004008 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004009 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004010 return Result;
4011 }
4012
4013 // Then check to see if we should lower the memcpy with target-specific
4014 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004015 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004016 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004017 isVol, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +00004018 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004019 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004020 return Result;
4021
4022 // If we really need inline code and the target declined to provide it,
4023 // use a (potentially long) sequence of loads and stores.
4024 if (AlwaysInline) {
4025 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00004026 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004027 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004028 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman707e0182008-04-12 04:36:06 +00004029 }
4030
Dan Gohman7b55d362010-04-05 20:24:08 +00004031 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
4032 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
4033 // respect volatile, so they may do things like read or write memory
4034 // beyond the given memory regions. But fixing this isn't easy, and most
4035 // people don't care.
4036
Bill Wendlingba54bca2013-06-19 21:36:55 +00004037 const TargetLowering *TLI = TM.getTargetLowering();
4038
Dan Gohman707e0182008-04-12 04:36:06 +00004039 // Emit a library call.
4040 TargetLowering::ArgListTy Args;
4041 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004042 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004043 Entry.Node = Dst; Args.push_back(Entry);
4044 Entry.Node = Src; Args.push_back(Entry);
4045 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004046 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004047 TargetLowering::
4048 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004049 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004050 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004051 /*isTailCall=*/false,
4052 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004053 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
4054 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004055 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004056 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004057
Dan Gohman707e0182008-04-12 04:36:06 +00004058 return CallResult.second;
4059}
4060
Andrew Trickac6d9be2013-05-25 02:42:55 +00004061SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004062 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004063 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004064 MachinePointerInfo DstPtrInfo,
4065 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004066 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004067
Dan Gohman21323f32008-05-29 19:42:22 +00004068 // Check to see if we should lower the memmove to loads and stores first.
4069 // For cases within the target-specified limits, this is the best choice.
4070 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4071 if (ConstantSize) {
4072 // Memmove with size zero? Just return the original chain.
4073 if (ConstantSize->isNullValue())
4074 return Chain;
4075
Dan Gohman475871a2008-07-27 21:46:04 +00004076 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00004077 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004078 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004079 false, DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004080 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00004081 return Result;
4082 }
Dan Gohman707e0182008-04-12 04:36:06 +00004083
4084 // Then check to see if we should lower the memmove with target-specific
4085 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004086 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004087 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004088 DstPtrInfo, SrcPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004089 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004090 return Result;
4091
Mon P Wang01f0e852010-04-06 08:27:51 +00004092 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
4093 // not be safe. See memcpy above for more details.
4094
Bill Wendlingba54bca2013-06-19 21:36:55 +00004095 const TargetLowering *TLI = TM.getTargetLowering();
4096
Dan Gohman707e0182008-04-12 04:36:06 +00004097 // Emit a library call.
4098 TargetLowering::ArgListTy Args;
4099 TargetLowering::ArgListEntry Entry;
Bill Wendlingba54bca2013-06-19 21:36:55 +00004100 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004101 Entry.Node = Dst; Args.push_back(Entry);
4102 Entry.Node = Src; Args.push_back(Entry);
4103 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004104 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004105 TargetLowering::
4106 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004107 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004108 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004109 /*isTailCall=*/false,
4110 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004111 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4112 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004113 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004114 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004115
Dan Gohman707e0182008-04-12 04:36:06 +00004116 return CallResult.second;
4117}
4118
Andrew Trickac6d9be2013-05-25 02:42:55 +00004119SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00004120 SDValue Src, SDValue Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +00004121 unsigned Align, bool isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004122 MachinePointerInfo DstPtrInfo) {
Chandler Carruth7e6ffac2013-02-25 14:29:38 +00004123 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman707e0182008-04-12 04:36:06 +00004124
4125 // Check to see if we should lower the memset to stores first.
4126 // For cases within the target-specified limits, this is the best choice.
4127 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4128 if (ConstantSize) {
4129 // Memset with size zero? Just return the original chain.
4130 if (ConstantSize->isNullValue())
4131 return Chain;
4132
Mon P Wang20adc9d2010-04-04 03:10:48 +00004133 SDValue Result =
4134 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattnere72f2022010-09-21 05:40:29 +00004135 Align, isVol, DstPtrInfo);
Mon P Wang20adc9d2010-04-04 03:10:48 +00004136
Gabor Greifba36cb52008-08-28 21:40:38 +00004137 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004138 return Result;
4139 }
4140
4141 // Then check to see if we should lower the memset with target-specific
4142 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00004143 SDValue Result =
Dan Gohmanff7a5622010-05-11 17:31:57 +00004144 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004145 DstPtrInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004146 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00004147 return Result;
4148
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004149 // Emit a library call.
Bill Wendlingba54bca2013-06-19 21:36:55 +00004150 const TargetLowering *TLI = TM.getTargetLowering();
4151 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00004152 TargetLowering::ArgListTy Args;
4153 TargetLowering::ArgListEntry Entry;
4154 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4155 Args.push_back(Entry);
4156 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 if (Src.getValueType().bitsGT(MVT::i32))
4158 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00004159 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00004161 Entry.Node = Src;
4162 Entry.Ty = Type::getInt32Ty(*getContext());
4163 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00004164 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00004165 Entry.Node = Size;
4166 Entry.Ty = IntPtrTy;
4167 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00004168 Args.push_back(Entry);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004169 // FIXME: pass in SDLoc
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004170 TargetLowering::
4171 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00004172 false, false, false, false, 0,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004173 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00004174 /*isTailCall=*/false,
4175 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004176 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4177 TLI->getPointerTy()),
Bill Wendling46ada192010-03-02 01:55:18 +00004178 Args, *this, dl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004179 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00004180
Dan Gohman707e0182008-04-12 04:36:06 +00004181 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00004182}
4183
Andrew Trickac6d9be2013-05-25 02:42:55 +00004184SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Amara Emerson268c7432013-09-26 12:22:36 +00004185 SDVTList VTList, SDValue* Ops, unsigned NumOps,
4186 MachineMemOperand *MMO,
4187 AtomicOrdering Ordering,
4188 SynchronizationScope SynchScope) {
4189 FoldingSetNodeID ID;
4190 ID.AddInteger(MemVT.getRawBits());
4191 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4192 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
4193 void* IP = 0;
4194 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4195 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4196 return SDValue(E, 0);
4197 }
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004198
4199 // Allocate the operands array for the node out of the BumpPtrAllocator, since
4200 // SDNode doesn't have access to it. This memory will be "leaked" when
4201 // the node is deallocated, but recovered when the allocator is released.
4202 // If the number of operands is less than 5 we use AtomicSDNode's internal
4203 // storage.
4204 SDUse *DynOps = NumOps > 4 ? OperandAllocator.Allocate<SDUse>(NumOps) : 0;
4205
Amara Emerson268c7432013-09-26 12:22:36 +00004206 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(),
4207 dl.getDebugLoc(), VTList, MemVT,
Benjamin Kramerfd40d512013-09-29 11:18:56 +00004208 Ops, DynOps, NumOps, MMO,
4209 Ordering, SynchScope);
Amara Emerson268c7432013-09-26 12:22:36 +00004210 CSEMap.InsertNode(N, IP);
4211 AllNodes.push_back(N);
4212 return SDValue(N, 0);
4213}
4214
4215SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner60bddc82010-09-21 04:53:42 +00004216 SDValue Chain, SDValue Ptr, SDValue Cmp,
4217 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedman55ba8162011-07-29 03:05:32 +00004218 unsigned Alignment,
4219 AtomicOrdering Ordering,
Michael Ilseman06b96902012-09-10 16:56:31 +00004220 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004221 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4222 Alignment = getEVTAlignment(MemVT);
4223
Evan Chengff89dcb2009-10-18 18:16:27 +00004224 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004225
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004226 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004227 // For now, atomics are considered to be volatile always.
Eli Friedman981a0102011-09-07 02:23:42 +00004228 // FIXME: Volatile isn't really correct; we should keep track of atomic
4229 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004230 unsigned Flags = MachineMemOperand::MOVolatile;
4231 if (Opcode != ISD::ATOMIC_STORE)
4232 Flags |= MachineMemOperand::MOLoad;
4233 if (Opcode != ISD::ATOMIC_LOAD)
4234 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004235
4236 MachineMemOperand *MMO =
Chris Lattner60bddc82010-09-21 04:53:42 +00004237 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004238
Eli Friedman55ba8162011-07-29 03:05:32 +00004239 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4240 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004241}
4242
Andrew Trickac6d9be2013-05-25 02:42:55 +00004243SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004244 SDValue Chain,
4245 SDValue Ptr, SDValue Cmp,
Eli Friedman55ba8162011-07-29 03:05:32 +00004246 SDValue Swp, MachineMemOperand *MMO,
4247 AtomicOrdering Ordering,
4248 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004249 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4250 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4251
Owen Andersone50ed302009-08-10 22:56:29 +00004252 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004253
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004255 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Amara Emerson268c7432013-09-26 12:22:36 +00004256 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 4, MMO, Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004257}
4258
Andrew Trickac6d9be2013-05-25 02:42:55 +00004259SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004260 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00004261 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00004262 const Value* PtrVal,
Eli Friedman55ba8162011-07-29 03:05:32 +00004263 unsigned Alignment,
4264 AtomicOrdering Ordering,
4265 SynchronizationScope SynchScope) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004266 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4267 Alignment = getEVTAlignment(MemVT);
4268
Evan Chengff89dcb2009-10-18 18:16:27 +00004269 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004270 // An atomic store does not load. An atomic load does not store.
Eli Friedman981a0102011-09-07 02:23:42 +00004271 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004272 // For now, atomics are considered to be volatile always, and they are
4273 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004274 // FIXME: Volatile isn't really correct; we should keep track of atomic
4275 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004276 unsigned Flags = MachineMemOperand::MOVolatile;
4277 if (Opcode != ISD::ATOMIC_STORE)
4278 Flags |= MachineMemOperand::MOLoad;
4279 if (Opcode != ISD::ATOMIC_LOAD)
4280 Flags |= MachineMemOperand::MOStore;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004281
4282 MachineMemOperand *MMO =
Chris Lattner93a95ae2010-09-21 04:46:39 +00004283 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004284 MemVT.getStoreSize(), Alignment);
4285
Eli Friedman55ba8162011-07-29 03:05:32 +00004286 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4287 Ordering, SynchScope);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004288}
4289
Andrew Trickac6d9be2013-05-25 02:42:55 +00004290SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004291 SDValue Chain,
4292 SDValue Ptr, SDValue Val,
Eli Friedman55ba8162011-07-29 03:05:32 +00004293 MachineMemOperand *MMO,
4294 AtomicOrdering Ordering,
4295 SynchronizationScope SynchScope) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004296 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4297 Opcode == ISD::ATOMIC_LOAD_SUB ||
4298 Opcode == ISD::ATOMIC_LOAD_AND ||
4299 Opcode == ISD::ATOMIC_LOAD_OR ||
4300 Opcode == ISD::ATOMIC_LOAD_XOR ||
4301 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004302 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004303 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00004304 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00004305 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman327236c2011-08-24 20:50:09 +00004306 Opcode == ISD::ATOMIC_SWAP ||
4307 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004308 "Invalid Atomic Op");
4309
Owen Andersone50ed302009-08-10 22:56:29 +00004310 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004311
Eli Friedman327236c2011-08-24 20:50:09 +00004312 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4313 getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004314 SDValue Ops[] = {Chain, Ptr, Val};
Amara Emerson268c7432013-09-26 12:22:36 +00004315 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 3, MMO, Ordering, SynchScope);
Dale Johannesene8c17332009-01-29 00:47:48 +00004316}
4317
Andrew Trickac6d9be2013-05-25 02:42:55 +00004318SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004319 EVT VT, SDValue Chain,
4320 SDValue Ptr,
4321 const Value* PtrVal,
4322 unsigned Alignment,
4323 AtomicOrdering Ordering,
4324 SynchronizationScope SynchScope) {
4325 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4326 Alignment = getEVTAlignment(MemVT);
4327
4328 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004329 // An atomic store does not load. An atomic load does not store.
4330 // (An atomicrmw obviously both loads and stores.)
4331 // For now, atomics are considered to be volatile always, and they are
4332 // chained as such.
Eli Friedman981a0102011-09-07 02:23:42 +00004333 // FIXME: Volatile isn't really correct; we should keep track of atomic
4334 // orderings in the memoperand.
Jakob Stoklund Olesen36d29bc2012-08-28 03:11:32 +00004335 unsigned Flags = MachineMemOperand::MOVolatile;
4336 if (Opcode != ISD::ATOMIC_STORE)
4337 Flags |= MachineMemOperand::MOLoad;
4338 if (Opcode != ISD::ATOMIC_LOAD)
4339 Flags |= MachineMemOperand::MOStore;
Eli Friedman327236c2011-08-24 20:50:09 +00004340
4341 MachineMemOperand *MMO =
4342 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4343 MemVT.getStoreSize(), Alignment);
4344
4345 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4346 Ordering, SynchScope);
4347}
4348
Andrew Trickac6d9be2013-05-25 02:42:55 +00004349SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman327236c2011-08-24 20:50:09 +00004350 EVT VT, SDValue Chain,
4351 SDValue Ptr,
4352 MachineMemOperand *MMO,
4353 AtomicOrdering Ordering,
4354 SynchronizationScope SynchScope) {
4355 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4356
4357 SDVTList VTs = getVTList(VT, MVT::Other);
Eli Friedman327236c2011-08-24 20:50:09 +00004358 SDValue Ops[] = {Chain, Ptr};
Amara Emerson268c7432013-09-26 12:22:36 +00004359 return getAtomic(Opcode, dl, MemVT, VTs, Ops, 2, MMO, Ordering, SynchScope);
Eli Friedman327236c2011-08-24 20:50:09 +00004360}
4361
Duncan Sands4bdcb612008-07-02 17:40:58 +00004362/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesen54c94522009-02-02 20:47:48 +00004363SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004364 SDLoc dl) {
Dale Johannesen54c94522009-02-02 20:47:48 +00004365 if (NumOps == 1)
4366 return Ops[0];
4367
Owen Andersone50ed302009-08-10 22:56:29 +00004368 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00004369 VTs.reserve(NumOps);
4370 for (unsigned i = 0; i < NumOps; ++i)
4371 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00004372 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00004373 Ops, NumOps);
4374}
4375
Dan Gohman475871a2008-07-27 21:46:04 +00004376SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004377SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00004378 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00004379 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004380 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004381 unsigned Align, bool Vol,
4382 bool ReadMem, bool WriteMem) {
4383 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004384 MemVT, PtrInfo, Align, Vol,
Dale Johannesene8c17332009-01-29 00:47:48 +00004385 ReadMem, WriteMem);
4386}
4387
4388SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004389SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesene8c17332009-01-29 00:47:48 +00004390 const SDValue *Ops, unsigned NumOps,
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004391 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesene8c17332009-01-29 00:47:48 +00004392 unsigned Align, bool Vol,
4393 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004394 if (Align == 0) // Ensure that codegen never sees alignment 0
4395 Align = getEVTAlignment(MemVT);
4396
4397 MachineFunction &MF = getMachineFunction();
4398 unsigned Flags = 0;
4399 if (WriteMem)
4400 Flags |= MachineMemOperand::MOStore;
4401 if (ReadMem)
4402 Flags |= MachineMemOperand::MOLoad;
4403 if (Vol)
4404 Flags |= MachineMemOperand::MOVolatile;
4405 MachineMemOperand *MMO =
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00004406 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004407
4408 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4409}
4410
4411SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004412SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004413 const SDValue *Ops, unsigned NumOps,
4414 EVT MemVT, MachineMemOperand *MMO) {
4415 assert((Opcode == ISD::INTRINSIC_VOID ||
4416 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesen1de4aa92010-10-26 23:11:10 +00004417 Opcode == ISD::PREFETCH ||
Nadav Rotemc05d3062012-09-06 09:17:37 +00004418 Opcode == ISD::LIFETIME_START ||
4419 Opcode == ISD::LIFETIME_END ||
Dan Gohmanc76909a2009-09-25 20:36:54 +00004420 (Opcode <= INT_MAX &&
4421 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4422 "Opcode is not a memory-accessing opcode!");
4423
Dale Johannesene8c17332009-01-29 00:47:48 +00004424 // Memoize the node unless it returns a flag.
4425 MemIntrinsicSDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004426 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004427 FoldingSetNodeID ID;
4428 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper32ecfb42012-07-30 20:23:19 +00004429 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004430 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004431 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4432 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004433 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004434 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004435
Jack Carter3af4d252013-09-09 22:02:08 +00004436 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4437 dl.getDebugLoc(), VTList, Ops,
4438 NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004439 CSEMap.InsertNode(N, IP);
4440 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004441 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(),
4442 dl.getDebugLoc(), VTList, Ops,
4443 NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004444 }
4445 AllNodes.push_back(N);
4446 return SDValue(N, 0);
4447}
4448
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004449/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4450/// MachinePointerInfo record from it. This is particularly useful because the
4451/// code generator has many cases where it doesn't bother passing in a
4452/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4453static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4454 // If this is FI+Offset, we can model it.
4455 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4456 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4457
4458 // If this is (FI+Offset1)+Offset2, we can model it.
4459 if (Ptr.getOpcode() != ISD::ADD ||
4460 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4461 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4462 return MachinePointerInfo();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004463
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004464 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4465 return MachinePointerInfo::getFixedStack(FI, Offset+
4466 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4467}
4468
4469/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4470/// MachinePointerInfo record from it. This is particularly useful because the
4471/// code generator has many cases where it doesn't bother passing in a
4472/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4473static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4474 // If the 'Offset' value isn't a constant, we can't handle this.
4475 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4476 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4477 if (OffsetOp.getOpcode() == ISD::UNDEF)
4478 return InferPointerInfo(Ptr);
4479 return MachinePointerInfo();
4480}
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004481
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004482
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004483SDValue
4484SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004485 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004486 SDValue Ptr, SDValue Offset,
4487 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004488 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004489 unsigned Alignment, const MDNode *TBAAInfo,
4490 const MDNode *Ranges) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004491 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004492 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004493 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4494 Alignment = getEVTAlignment(VT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004495
Dan Gohmanc76909a2009-09-25 20:36:54 +00004496 unsigned Flags = MachineMemOperand::MOLoad;
4497 if (isVolatile)
4498 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004499 if (isNonTemporal)
4500 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooperd752e0f2011-11-08 18:42:53 +00004501 if (isInvariant)
4502 Flags |= MachineMemOperand::MOInvariant;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004503
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004504 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4505 // clients.
4506 if (PtrInfo.V == 0)
4507 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004508
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004509 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004510 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004511 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004512 TBAAInfo, Ranges);
Evan Chengbcc80172010-07-07 22:15:37 +00004513 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004514}
4515
4516SDValue
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004517SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004518 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004519 SDValue Ptr, SDValue Offset, EVT MemVT,
4520 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004521 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00004522 ExtType = ISD::NON_EXTLOAD;
4523 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004524 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004525 } else {
4526 // Extending load.
Dan Gohman2e141d72009-12-14 23:40:38 +00004527 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4528 "Should only be an extending load, not truncating!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00004529 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00004530 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004531 assert(VT.isVector() == MemVT.isVector() &&
4532 "Cannot use trunc store to convert to or from a vector!");
4533 assert((!VT.isVector() ||
4534 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4535 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004536 }
4537
4538 bool Indexed = AM != ISD::UNINDEXED;
4539 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4540 "Unindexed load with an offset!");
4541
4542 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00004543 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004544 SDValue Ops[] = { Chain, Ptr, Offset };
4545 FoldingSetNodeID ID;
4546 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004547 ID.AddInteger(MemVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004548 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004549 MMO->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004550 MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004551 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004552 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004553 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4554 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004555 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004556 }
Jack Carter3af4d252013-09-09 22:02:08 +00004557 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(),
4558 dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman95531882010-03-18 18:49:47 +00004559 MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004560 CSEMap.InsertNode(N, IP);
4561 AllNodes.push_back(N);
4562 return SDValue(N, 0);
4563}
4564
Andrew Trickac6d9be2013-05-25 02:42:55 +00004565SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004566 SDValue Chain, SDValue Ptr,
Chris Lattnere72f2022010-09-21 05:40:29 +00004567 MachinePointerInfo PtrInfo,
4568 bool isVolatile, bool isNonTemporal,
Michael Ilseman06b96902012-09-10 16:56:31 +00004569 bool isInvariant, unsigned Alignment,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004570 const MDNode *TBAAInfo,
4571 const MDNode *Ranges) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004572 SDValue Undef = getUNDEF(Ptr.getValueType());
4573 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola95d594c2012-03-31 18:14:00 +00004574 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4575 TBAAInfo, Ranges);
Chris Lattnere72f2022010-09-21 05:40:29 +00004576}
Dale Johannesene8c17332009-01-29 00:47:48 +00004577
Richard Sandiford66589dc2013-10-28 11:17:59 +00004578SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
4579 SDValue Chain, SDValue Ptr,
4580 MachineMemOperand *MMO) {
4581 SDValue Undef = getUNDEF(Ptr.getValueType());
4582 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
4583 VT, MMO);
4584}
4585
Andrew Trickac6d9be2013-05-25 02:42:55 +00004586SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattnere72f2022010-09-21 05:40:29 +00004587 SDValue Chain, SDValue Ptr,
4588 MachinePointerInfo PtrInfo, EVT MemVT,
4589 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004590 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattnere72f2022010-09-21 05:40:29 +00004591 SDValue Undef = getUNDEF(Ptr.getValueType());
4592 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004593 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004594 TBAAInfo);
Chris Lattnere72f2022010-09-21 05:40:29 +00004595}
4596
4597
Richard Sandiford66589dc2013-10-28 11:17:59 +00004598SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
4599 SDValue Chain, SDValue Ptr, EVT MemVT,
4600 MachineMemOperand *MMO) {
4601 SDValue Undef = getUNDEF(Ptr.getValueType());
4602 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
4603 MemVT, MMO);
4604}
4605
Dan Gohman475871a2008-07-27 21:46:04 +00004606SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004607SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004608 SDValue Offset, ISD::MemIndexedMode AM) {
4609 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4610 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4611 "Load is already a indexed load!");
Evan Chengbcc80172010-07-07 22:15:37 +00004612 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004613 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilseman06b96902012-09-10 16:56:31 +00004614 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004615 false, LD->getAlignment());
Dale Johannesene8c17332009-01-29 00:47:48 +00004616}
4617
Andrew Trickac6d9be2013-05-25 02:42:55 +00004618SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004619 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene1e559442010-02-15 17:00:31 +00004620 bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004621 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004622 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004623 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004624 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00004625 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004626
Dan Gohmanc76909a2009-09-25 20:36:54 +00004627 unsigned Flags = MachineMemOperand::MOStore;
4628 if (isVolatile)
4629 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004630 if (isNonTemporal)
4631 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004632
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004633 if (PtrInfo.V == 0)
4634 PtrInfo = InferPointerInfo(Ptr);
4635
4636 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004637 MachineMemOperand *MMO =
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004638 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004639 Val.getValueType().getStoreSize(), Alignment,
4640 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004641
4642 return getStore(Chain, dl, Val, Ptr, MMO);
4643}
4644
Andrew Trickac6d9be2013-05-25 02:42:55 +00004645SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004646 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004647 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004648 "Invalid chain type");
Dan Gohmanc76909a2009-09-25 20:36:54 +00004649 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00004650 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004651 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004652 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4653 FoldingSetNodeID ID;
4654 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004655 ID.AddInteger(VT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004656 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004657 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004658 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004659 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4661 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004662 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004663 }
Jack Carter3af4d252013-09-09 22:02:08 +00004664 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4665 dl.getDebugLoc(), VTs,
4666 ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004667 CSEMap.InsertNode(N, IP);
4668 AllNodes.push_back(N);
4669 return SDValue(N, 0);
4670}
4671
Andrew Trickac6d9be2013-05-25 02:42:55 +00004672SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004673 SDValue Ptr, MachinePointerInfo PtrInfo,
4674 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004675 unsigned Alignment,
4676 const MDNode *TBAAInfo) {
Michael Ilseman06b96902012-09-10 16:56:31 +00004677 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004678 "Invalid chain type");
Chris Lattner5c5cb2a2010-09-21 05:10:45 +00004679 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4680 Alignment = getEVTAlignment(SVT);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004681
Dan Gohmanc76909a2009-09-25 20:36:54 +00004682 unsigned Flags = MachineMemOperand::MOStore;
4683 if (isVolatile)
4684 Flags |= MachineMemOperand::MOVolatile;
David Greene1e559442010-02-15 17:00:31 +00004685 if (isNonTemporal)
4686 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004687
Chris Lattnerd0e139f2010-09-21 17:24:05 +00004688 if (PtrInfo.V == 0)
4689 PtrInfo = InferPointerInfo(Ptr);
4690
4691 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00004692 MachineMemOperand *MMO =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00004693 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4694 TBAAInfo);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004695
4696 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4697}
4698
Andrew Trickac6d9be2013-05-25 02:42:55 +00004699SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohmanc76909a2009-09-25 20:36:54 +00004700 SDValue Ptr, EVT SVT,
4701 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00004702 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00004703
Michael Ilseman06b96902012-09-10 16:56:31 +00004704 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemaeb86fa2011-07-14 10:37:54 +00004705 "Invalid chain type");
Dale Johannesene8c17332009-01-29 00:47:48 +00004706 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00004707 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004708
Dan Gohman2e141d72009-12-14 23:40:38 +00004709 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4710 "Should only be a truncating store, not extending!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004711 assert(VT.isInteger() == SVT.isInteger() &&
4712 "Can't do FP-INT conversion!");
Dan Gohman2e141d72009-12-14 23:40:38 +00004713 assert(VT.isVector() == SVT.isVector() &&
4714 "Cannot use trunc store to convert to or from a vector!");
4715 assert((!VT.isVector() ||
4716 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4717 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesene8c17332009-01-29 00:47:48 +00004718
Owen Anderson825b72b2009-08-11 20:47:22 +00004719 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00004720 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00004721 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4722 FoldingSetNodeID ID;
4723 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004724 ID.AddInteger(SVT.getRawBits());
David Greene1157f792010-02-17 20:21:42 +00004725 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004726 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper32ecfb42012-07-30 20:23:19 +00004727 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004728 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004729 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4730 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004731 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004732 }
Jack Carter3af4d252013-09-09 22:02:08 +00004733 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4734 dl.getDebugLoc(), VTs,
4735 ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00004736 CSEMap.InsertNode(N, IP);
4737 AllNodes.push_back(N);
4738 return SDValue(N, 0);
4739}
4740
Dan Gohman475871a2008-07-27 21:46:04 +00004741SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00004742SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesene8c17332009-01-29 00:47:48 +00004743 SDValue Offset, ISD::MemIndexedMode AM) {
4744 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4745 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4746 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004747 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00004748 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4749 FoldingSetNodeID ID;
4750 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00004751 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00004752 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper32ecfb42012-07-30 20:23:19 +00004753 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesene8c17332009-01-29 00:47:48 +00004754 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004755 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesene8c17332009-01-29 00:47:48 +00004756 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004757
Jack Carter3af4d252013-09-09 22:02:08 +00004758 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(),
4759 dl.getDebugLoc(), VTs, AM,
Dan Gohman95531882010-03-18 18:49:47 +00004760 ST->isTruncatingStore(),
4761 ST->getMemoryVT(),
4762 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00004763 CSEMap.InsertNode(N, IP);
4764 AllNodes.push_back(N);
4765 return SDValue(N, 0);
4766}
4767
Andrew Trickac6d9be2013-05-25 02:42:55 +00004768SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00004769 SDValue Chain, SDValue Ptr,
Rafael Espindola72d13ff2010-06-26 18:22:20 +00004770 SDValue SV,
4771 unsigned Align) {
4772 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4773 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesen0f502f62009-02-03 22:26:09 +00004774}
4775
Andrew Trickac6d9be2013-05-25 02:42:55 +00004776SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004777 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004778 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004779 case 0: return getNode(Opcode, DL, VT);
4780 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4781 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4782 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004783 default: break;
4784 }
4785
Dan Gohman475871a2008-07-27 21:46:04 +00004786 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004787 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00004788 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004789 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004790}
4791
Andrew Trickac6d9be2013-05-25 02:42:55 +00004792SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004793 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004794 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004795 case 0: return getNode(Opcode, DL, VT);
4796 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4797 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4798 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00004799 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00004800 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004801
Chris Lattneref847df2005-04-09 03:27:28 +00004802 switch (Opcode) {
4803 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00004804 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004805 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004806 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4807 "LHS and RHS of condition must have same type!");
4808 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4809 "True and False arms of SelectCC must have same type!");
4810 assert(Ops[2].getValueType() == VT &&
4811 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004812 break;
4813 }
4814 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004815 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004816 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4817 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00004818 break;
4819 }
Chris Lattneref847df2005-04-09 03:27:28 +00004820 }
4821
Chris Lattner385328c2005-05-14 07:42:29 +00004822 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00004823 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00004824 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004825
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004826 if (VT != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004827 FoldingSetNodeID ID;
4828 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004829 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00004830
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004831 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004832 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00004833
Jack Carter3af4d252013-09-09 22:02:08 +00004834 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4835 VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004836 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004837 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004838 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4839 VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00004840 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00004841
Chris Lattneref847df2005-04-09 03:27:28 +00004842 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004843#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004844 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004845#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004846 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004847}
4848
Andrew Trickac6d9be2013-05-25 02:42:55 +00004849SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramer3853f742013-03-07 20:33:29 +00004850 ArrayRef<EVT> ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004851 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004852 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004853 Ops, NumOps);
4854}
4855
Andrew Trickac6d9be2013-05-25 02:42:55 +00004856SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004857 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004858 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004859 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004860 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4861 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004862}
4863
Andrew Trickac6d9be2013-05-25 02:42:55 +00004864SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004865 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004866 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004867 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004868
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004869#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004870 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004871 // FIXME: figure out how to safely handle things like
4872 // int foo(int x) { return 1 << (x & 255); }
4873 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004874 case ISD::SRA_PARTS:
4875 case ISD::SRL_PARTS:
4876 case ISD::SHL_PARTS:
4877 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004879 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004880 else if (N3.getOpcode() == ISD::AND)
4881 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4882 // If the and is only masking out bits that cannot effect the shift,
4883 // eliminate the and.
Dan Gohmand1996362010-01-09 02:13:55 +00004884 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004885 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004886 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004887 }
4888 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004889 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004890#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004891
Chris Lattner43247a12005-08-25 19:12:10 +00004892 // Memoize the node unless it returns a flag.
4893 SDNode *N;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004894 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskey583bd472006-10-27 23:46:08 +00004895 FoldingSetNodeID ID;
4896 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004897 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004898 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004899 return SDValue(E, 0);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00004900
Dan Gohman0e5f1302008-07-07 23:02:41 +00004901 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004902 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4903 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004904 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004905 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4906 DL.getDebugLoc(), VTList, Ops[0],
4907 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004908 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004909 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4910 DL.getDebugLoc(), VTList, Ops[0],
4911 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004912 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004913 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4914 VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004915 }
Chris Lattnera5682852006-08-07 23:03:03 +00004916 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004917 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004918 if (NumOps == 1) {
Jack Carter3af4d252013-09-09 22:02:08 +00004919 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(),
4920 DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004921 } else if (NumOps == 2) {
Jack Carter3af4d252013-09-09 22:02:08 +00004922 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(),
4923 DL.getDebugLoc(), VTList, Ops[0],
4924 Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004925 } else if (NumOps == 3) {
Jack Carter3af4d252013-09-09 22:02:08 +00004926 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(),
4927 DL.getDebugLoc(), VTList, Ops[0],
4928 Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004929 } else {
Jack Carter3af4d252013-09-09 22:02:08 +00004930 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(),
4931 VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004932 }
Chris Lattner43247a12005-08-25 19:12:10 +00004933 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004934 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004935#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00004936 VerifySDNode(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004937#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004938 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004939}
4940
Andrew Trickac6d9be2013-05-25 02:42:55 +00004941SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00004942 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004943}
4944
Andrew Trickac6d9be2013-05-25 02:42:55 +00004945SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004946 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004947 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004948 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004949}
4950
Andrew Trickac6d9be2013-05-25 02:42:55 +00004951SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004952 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004953 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004954 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004955}
4956
Andrew Trickac6d9be2013-05-25 02:42:55 +00004957SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004958 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004959 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004960 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004961}
4962
Andrew Trickac6d9be2013-05-25 02:42:55 +00004963SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004964 SDValue N1, SDValue N2, SDValue N3,
4965 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004966 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004967 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004968}
4969
Andrew Trickac6d9be2013-05-25 02:42:55 +00004970SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004971 SDValue N1, SDValue N2, SDValue N3,
4972 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004973 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004974 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004975}
4976
Owen Andersone50ed302009-08-10 22:56:29 +00004977SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004978 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004979}
4980
Owen Andersone50ed302009-08-10 22:56:29 +00004981SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004982 FoldingSetNodeID ID;
4983 ID.AddInteger(2U);
4984 ID.AddInteger(VT1.getRawBits());
4985 ID.AddInteger(VT2.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004986
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00004987 void *IP = 0;
4988 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
4989 if (Result == NULL) {
4990 EVT *Array = Allocator.Allocate<EVT>(2);
4991 Array[0] = VT1;
4992 Array[1] = VT2;
4993 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
4994 VTListMap.InsertNode(Result, IP);
4995 }
4996 return Result->getSDVTList();
Chris Lattnera3255112005-11-08 23:30:28 +00004997}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004998
Owen Andersone50ed302009-08-10 22:56:29 +00004999SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005000 FoldingSetNodeID ID;
5001 ID.AddInteger(3U);
5002 ID.AddInteger(VT1.getRawBits());
5003 ID.AddInteger(VT2.getRawBits());
5004 ID.AddInteger(VT3.getRawBits());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005005
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005006 void *IP = 0;
5007 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5008 if (Result == NULL) {
5009 EVT *Array = Allocator.Allocate<EVT>(3);
5010 Array[0] = VT1;
5011 Array[1] = VT2;
5012 Array[2] = VT3;
5013 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
5014 VTListMap.InsertNode(Result, IP);
5015 }
5016 return Result->getSDVTList();
Chris Lattner70046e92006-08-15 17:46:01 +00005017}
5018
Owen Andersone50ed302009-08-10 22:56:29 +00005019SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005020 FoldingSetNodeID ID;
5021 ID.AddInteger(4U);
5022 ID.AddInteger(VT1.getRawBits());
5023 ID.AddInteger(VT2.getRawBits());
5024 ID.AddInteger(VT3.getRawBits());
5025 ID.AddInteger(VT4.getRawBits());
Bill Wendling13d6d442008-12-01 23:28:22 +00005026
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005027 void *IP = 0;
5028 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5029 if (Result == NULL) {
5030 EVT *Array = Allocator.Allocate<EVT>(4);
5031 Array[0] = VT1;
5032 Array[1] = VT2;
5033 Array[2] = VT3;
5034 Array[3] = VT4;
5035 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
5036 VTListMap.InsertNode(Result, IP);
5037 }
5038 return Result->getSDVTList();
Bill Wendling13d6d442008-12-01 23:28:22 +00005039}
5040
Owen Andersone50ed302009-08-10 22:56:29 +00005041SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005042 FoldingSetNodeID ID;
5043 ID.AddInteger(NumVTs);
5044 for (unsigned index = 0; index < NumVTs; index++) {
5045 ID.AddInteger(VTs[index].getRawBits());
Chris Lattner70046e92006-08-15 17:46:01 +00005046 }
5047
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005048 void *IP = 0;
5049 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
5050 if (Result == NULL) {
5051 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
5052 std::copy(VTs, VTs + NumVTs, Array);
5053 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
5054 VTListMap.InsertNode(Result, IP);
Chris Lattner70046e92006-08-15 17:46:01 +00005055 }
Wan Xiaofei8c955ea2013-10-22 08:02:02 +00005056 return Result->getSDVTList();
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00005057}
5058
5059
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005060/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
5061/// specified operands. If the resultant node already exists in the DAG,
5062/// this does not modify the specified node, instead it returns the node that
5063/// already exists. If the resultant node does not exist in the DAG, the
5064/// input node is returned. As a degenerate case, if you specify the same
5065/// input operands as the node already has, the input node is returned.
Dan Gohman027657d2010-06-18 15:30:29 +00005066SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005067 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005068
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005069 // Check to see if there is no change.
Dan Gohman027657d2010-06-18 15:30:29 +00005070 if (Op == N->getOperand(0)) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005071
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005072 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005073 void *InsertPos = 0;
5074 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005075 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005076
Dan Gohman79acd2b2008-07-21 22:38:59 +00005077 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005078 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005079 if (!RemoveNodeFromCSEMaps(N))
5080 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005081
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005082 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005083 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00005084
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005085 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005086 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005087 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005088}
5089
Dan Gohman027657d2010-06-18 15:30:29 +00005090SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005091 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005092
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005093 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005094 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman027657d2010-06-18 15:30:29 +00005095 return N; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00005096
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005097 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005098 void *InsertPos = 0;
5099 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005100 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005101
Dan Gohman79acd2b2008-07-21 22:38:59 +00005102 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005103 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005104 if (!RemoveNodeFromCSEMaps(N))
5105 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005106
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005107 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005108 if (N->OperandList[0] != Op1)
5109 N->OperandList[0].set(Op1);
5110 if (N->OperandList[1] != Op2)
5111 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00005112
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005113 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005114 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005115 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005116}
5117
Dan Gohman027657d2010-06-18 15:30:29 +00005118SDNode *SelectionDAG::
5119UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman475871a2008-07-27 21:46:04 +00005120 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005121 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005122}
5123
Dan Gohman027657d2010-06-18 15:30:29 +00005124SDNode *SelectionDAG::
5125UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue Op3, SDValue Op4) {
5127 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005128 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005129}
5130
Dan Gohman027657d2010-06-18 15:30:29 +00005131SDNode *SelectionDAG::
5132UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005133 SDValue Op3, SDValue Op4, SDValue Op5) {
5134 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005135 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00005136}
5137
Dan Gohman027657d2010-06-18 15:30:29 +00005138SDNode *SelectionDAG::
5139UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005140 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005141 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00005142
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005143 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005144 bool AnyChange = false;
5145 for (unsigned i = 0; i != NumOps; ++i) {
5146 if (Ops[i] != N->getOperand(i)) {
5147 AnyChange = true;
5148 break;
5149 }
5150 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005151
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005152 // No operands changed, just return the input node.
Dan Gohman027657d2010-06-18 15:30:29 +00005153 if (!AnyChange) return N;
Scott Michelfdc40a02009-02-17 22:15:04 +00005154
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005155 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00005156 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00005157 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman027657d2010-06-18 15:30:29 +00005158 return Existing;
Scott Michelfdc40a02009-02-17 22:15:04 +00005159
Dan Gohman7ceda162008-05-02 00:05:03 +00005160 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00005161 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00005162 if (!RemoveNodeFromCSEMaps(N))
5163 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005164
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005165 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00005166 for (unsigned i = 0; i != NumOps; ++i)
5167 if (N->OperandList[i] != Ops[i])
5168 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005169
5170 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00005171 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman027657d2010-06-18 15:30:29 +00005172 return N;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00005173}
5174
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005175/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00005176/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005177void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005178 // Unlike the code in MorphNodeTo that does this, we don't need to
5179 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00005180 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5181 SDUse &Use = *I++;
5182 Use.set(SDValue());
5183 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00005184}
Chris Lattner149c58c2005-08-16 18:17:10 +00005185
Dan Gohmane8be6c62008-07-17 19:10:17 +00005186/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5187/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005188///
Dan Gohmane8be6c62008-07-17 19:10:17 +00005189SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005190 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005191 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005192 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00005193}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005194
Dan Gohmane8be6c62008-07-17 19:10:17 +00005195SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005196 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005197 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005199 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00005200}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005201
Dan Gohmane8be6c62008-07-17 19:10:17 +00005202SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005203 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005204 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005205 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005206 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005207 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00005208}
Chris Lattner0fb094f2005-11-19 01:44:53 +00005209
Dan Gohmane8be6c62008-07-17 19:10:17 +00005210SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005211 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00005212 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005213 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00005214 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005215 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00005216}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00005217
Dan Gohmane8be6c62008-07-17 19:10:17 +00005218SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005219 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00005220 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005221 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005222 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005223}
5224
Dan Gohmane8be6c62008-07-17 19:10:17 +00005225SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005226 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005227 unsigned NumOps) {
5228 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005229 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005230}
5231
Dan Gohmane8be6c62008-07-17 19:10:17 +00005232SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005233 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005234 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005235 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00005236}
5237
Dan Gohmane8be6c62008-07-17 19:10:17 +00005238SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005239 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00005240 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005241 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005242 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00005243}
5244
Bill Wendling13d6d442008-12-01 23:28:22 +00005245SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005246 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00005247 const SDValue *Ops, unsigned NumOps) {
5248 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5249 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5250}
5251
Scott Michelfdc40a02009-02-17 22:15:04 +00005252SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005253 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005254 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005255 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005256 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005257 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00005258}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00005259
Scott Michelfdc40a02009-02-17 22:15:04 +00005260SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005261 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00005262 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005263 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005264 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005265 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00005266}
5267
Dan Gohmane8be6c62008-07-17 19:10:17 +00005268SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005269 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00005270 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00005271 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00005272 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00005273 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00005274 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00005275}
5276
Dan Gohmane8be6c62008-07-17 19:10:17 +00005277SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00005278 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00005279 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00005280 SDValue Op3) {
5281 SDVTList VTs = getVTList(VT1, VT2, VT3);
5282 SDValue Ops[] = { Op1, Op2, Op3 };
5283 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5284}
5285
5286SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00005287 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00005288 unsigned NumOps) {
Chris Lattner5857e0a2010-02-23 23:01:35 +00005289 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5290 // Reset the NodeID to -1.
5291 N->setNodeId(-1);
5292 return N;
Dan Gohmane8be6c62008-07-17 19:10:17 +00005293}
5294
Andrew Trickac6d9be2013-05-25 02:42:55 +00005295/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel0508d042011-12-15 18:21:18 +00005296/// the line number information on the merged node since it is not possible to
5297/// preserve the information that operation is associated with multiple lines.
5298/// This will make the debugger working better at -O0, were there is a higher
5299/// probability having other instructions associated with that line.
5300///
Andrew Trickac6d9be2013-05-25 02:42:55 +00005301/// For IROrder, we keep the smaller of the two
5302SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel0508d042011-12-15 18:21:18 +00005303 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005304 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5305 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel0508d042011-12-15 18:21:18 +00005306 N->setDebugLoc(DebugLoc());
5307 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005308 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5309 N->setIROrder(Order);
Devang Patel0508d042011-12-15 18:21:18 +00005310 return N;
5311}
5312
Chris Lattner21221e32010-02-28 21:36:14 +00005313/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohmane8be6c62008-07-17 19:10:17 +00005314/// return type, opcode, and operands.
5315///
5316/// Note that MorphNodeTo returns the resultant node. If there is already a
5317/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickac6d9be2013-05-25 02:42:55 +00005318/// the current one. Note that the SDLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005319///
5320/// Using MorphNodeTo is faster than creating a new node and swapping it in
5321/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00005322/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00005323/// the node's users.
5324///
5325SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00005326 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005327 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00005328 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00005329 void *IP = 0;
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005330 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005331 FoldingSetNodeID ID;
5332 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005333 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005334 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohmane8be6c62008-07-17 19:10:17 +00005335 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00005336
Dan Gohman095cc292008-09-13 01:54:27 +00005337 if (!RemoveNodeFromCSEMaps(N))
5338 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00005339
Dan Gohmane8be6c62008-07-17 19:10:17 +00005340 // Start the morphing.
5341 N->NodeType = Opc;
5342 N->ValueList = VTs.VTs;
5343 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00005344
Dan Gohmane8be6c62008-07-17 19:10:17 +00005345 // Clear the operands list, updating used nodes to remove this from their
5346 // use list. Keep track of any operands that become dead as a result.
5347 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00005348 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5349 SDUse &Use = *I++;
5350 SDNode *Used = Use.getNode();
5351 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00005352 if (Used->use_empty())
5353 DeadNodeSet.insert(Used);
5354 }
5355
Dan Gohmanc76909a2009-09-25 20:36:54 +00005356 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5357 // Initialize the memory references information.
5358 MN->setMemRefs(0, 0);
5359 // If NumOps is larger than the # of operands we can have in a
5360 // MachineSDNode, reallocate the operand list.
5361 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5362 if (MN->OperandsNeedDelete)
5363 delete[] MN->OperandList;
5364 if (NumOps > array_lengthof(MN->LocalOperands))
5365 // We're creating a final node that will live unmorphed for the
5366 // remainder of the current SelectionDAG iteration, so we can allocate
5367 // the operands directly out of a pool with no recycling metadata.
5368 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman95531882010-03-18 18:49:47 +00005369 Ops, NumOps);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005370 else
5371 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5372 MN->OperandsNeedDelete = false;
5373 } else
5374 MN->InitOperands(MN->OperandList, Ops, NumOps);
5375 } else {
5376 // If NumOps is larger than the # of operands we currently have, reallocate
5377 // the operand list.
5378 if (NumOps > N->NumOperands) {
5379 if (N->OperandsNeedDelete)
5380 delete[] N->OperandList;
5381 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005382 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005383 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00005384 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005385 }
5386
5387 // Delete any nodes that are still dead after adding the uses for the
5388 // new operands.
Chris Lattner7d892d62010-03-01 07:43:08 +00005389 if (!DeadNodeSet.empty()) {
5390 SmallVector<SDNode *, 16> DeadNodes;
5391 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5392 E = DeadNodeSet.end(); I != E; ++I)
5393 if ((*I)->use_empty())
5394 DeadNodes.push_back(*I);
5395 RemoveDeadNodes(DeadNodes);
5396 }
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005397
Dan Gohmane8be6c62008-07-17 19:10:17 +00005398 if (IP)
5399 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00005400 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00005401}
5402
Chris Lattner0fb094f2005-11-19 01:44:53 +00005403
Dan Gohman602b0c82009-09-25 18:54:59 +00005404/// getMachineNode - These are used for target selectors to create a new node
5405/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00005406///
Dan Gohman602b0c82009-09-25 18:54:59 +00005407/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00005408/// node of the specified opcode and operands, it returns that node instead of
5409/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00005410MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005411SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005412 SDVTList VTs = getVTList(VT);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005413 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005414}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005415
Dan Gohmanc81b7832009-10-10 01:29:16 +00005416MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005417SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005418 SDVTList VTs = getVTList(VT);
5419 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005420 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005421}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005422
Dan Gohmanc81b7832009-10-10 01:29:16 +00005423MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005424SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005425 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005426 SDVTList VTs = getVTList(VT);
5427 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005428 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005429}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005430
Dan Gohmanc81b7832009-10-10 01:29:16 +00005431MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005432SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005433 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005434 SDVTList VTs = getVTList(VT);
5435 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005436 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005437}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005438
Dan Gohmanc81b7832009-10-10 01:29:16 +00005439MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005440SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liao2a8bea72013-04-19 22:22:57 +00005441 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005442 SDVTList VTs = getVTList(VT);
Michael Liao2a8bea72013-04-19 22:22:57 +00005443 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005444}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005445
Dan Gohmanc81b7832009-10-10 01:29:16 +00005446MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005447SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005448 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko5c332db2013-05-05 00:40:33 +00005449 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesene8c17332009-01-29 00:47:48 +00005450}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005451
Dan Gohmanc81b7832009-10-10 01:29:16 +00005452MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005453SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005454 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005455 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005456 SDValue Ops[] = { Op1 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005457 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005458}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005459
Dan Gohmanc81b7832009-10-10 01:29:16 +00005460MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005461SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005462 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005463 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005464 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005465 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005466}
5467
Dan Gohmanc81b7832009-10-10 01:29:16 +00005468MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005469SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005470 EVT VT1, EVT VT2, SDValue Op1,
5471 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005472 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005473 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005474 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005475}
5476
Dan Gohmanc81b7832009-10-10 01:29:16 +00005477MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005478SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005479 EVT VT1, EVT VT2,
Michael Liao2a8bea72013-04-19 22:22:57 +00005480 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005481 SDVTList VTs = getVTList(VT1, VT2);
Michael Liao2a8bea72013-04-19 22:22:57 +00005482 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005483}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005484
Dan Gohmanc81b7832009-10-10 01:29:16 +00005485MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005486SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005487 EVT VT1, EVT VT2, EVT VT3,
5488 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005489 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00005490 SDValue Ops[] = { Op1, Op2 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005491 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005492}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005493
Dan Gohmanc81b7832009-10-10 01:29:16 +00005494MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005495SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005496 EVT VT1, EVT VT2, EVT VT3,
5497 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005498 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005499 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liao2a8bea72013-04-19 22:22:57 +00005500 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Cheng6ae46c42006-02-09 07:15:23 +00005501}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005502
Dan Gohmanc81b7832009-10-10 01:29:16 +00005503MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005504SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005505 EVT VT1, EVT VT2, EVT VT3,
Michael Liao2a8bea72013-04-19 22:22:57 +00005506 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005507 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liao2a8bea72013-04-19 22:22:57 +00005508 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesene8c17332009-01-29 00:47:48 +00005509}
Bill Wendling56ab1a22009-01-29 09:01:55 +00005510
Dan Gohmanc81b7832009-10-10 01:29:16 +00005511MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005512SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmanc81b7832009-10-10 01:29:16 +00005513 EVT VT2, EVT VT3, EVT VT4,
Michael Liao2a8bea72013-04-19 22:22:57 +00005514 ArrayRef<SDValue> Ops) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005515 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liao2a8bea72013-04-19 22:22:57 +00005516 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendling56ab1a22009-01-29 09:01:55 +00005517}
5518
Dan Gohmanc81b7832009-10-10 01:29:16 +00005519MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005520SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramer3853f742013-03-07 20:33:29 +00005521 ArrayRef<EVT> ResultTys,
Michael Liao2a8bea72013-04-19 22:22:57 +00005522 ArrayRef<SDValue> Ops) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005523 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liao2a8bea72013-04-19 22:22:57 +00005524 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005525}
5526
Dan Gohmanc81b7832009-10-10 01:29:16 +00005527MachineSDNode *
Andrew Trickac6d9be2013-05-25 02:42:55 +00005528SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liao2a8bea72013-04-19 22:22:57 +00005529 ArrayRef<SDValue> OpsArray) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005530 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005531 MachineSDNode *N;
Ted Kremenek584520e2011-01-23 17:05:06 +00005532 void *IP = 0;
Michael Liao2a8bea72013-04-19 22:22:57 +00005533 const SDValue *Ops = OpsArray.data();
5534 unsigned NumOps = OpsArray.size();
Dan Gohmanc76909a2009-09-25 20:36:54 +00005535
5536 if (DoCSE) {
5537 FoldingSetNodeID ID;
5538 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5539 IP = 0;
Devang Patel0508d042011-12-15 18:21:18 +00005540 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005541 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel0508d042011-12-15 18:21:18 +00005542 }
Dan Gohmanc76909a2009-09-25 20:36:54 +00005543 }
5544
5545 // Allocate a new MachineSDNode.
Jack Carter3af4d252013-09-09 22:02:08 +00005546 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(),
5547 DL.getDebugLoc(), VTs);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005548
5549 // Initialize the operands list.
5550 if (NumOps > array_lengthof(N->LocalOperands))
5551 // We're creating a final node that will live unmorphed for the
5552 // remainder of the current SelectionDAG iteration, so we can allocate
5553 // the operands directly out of a pool with no recycling metadata.
5554 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5555 Ops, NumOps);
5556 else
5557 N->InitOperands(N->LocalOperands, Ops, NumOps);
5558 N->OperandsNeedDelete = false;
5559
5560 if (DoCSE)
5561 CSEMap.InsertNode(N, IP);
5562
5563 AllNodes.push_back(N);
5564#ifndef NDEBUG
Duncan Sands59d2dad2010-11-20 11:25:00 +00005565 VerifyMachineNode(N);
Dan Gohmanc76909a2009-09-25 20:36:54 +00005566#endif
5567 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00005568}
Evan Cheng6ae46c42006-02-09 07:15:23 +00005569
Dan Gohman6a402dc2009-08-19 18:16:17 +00005570/// getTargetExtractSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005571/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohman6a402dc2009-08-19 18:16:17 +00005572SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005573SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohman6a402dc2009-08-19 18:16:17 +00005574 SDValue Operand) {
5575 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005576 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman602b0c82009-09-25 18:54:59 +00005577 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00005578 return SDValue(Subreg, 0);
5579}
5580
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005581/// getTargetInsertSubreg - A convenience function for creating
Chris Lattner518bb532010-02-09 19:54:29 +00005582/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005583SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00005584SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005585 SDValue Operand, SDValue Subreg) {
5586 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattner518bb532010-02-09 19:54:29 +00005587 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00005588 VT, Operand, Subreg, SRIdxVal);
5589 return SDValue(Result, 0);
5590}
5591
Evan Cheng08b11732008-03-22 01:55:50 +00005592/// getNodeIfExists - Get the specified node if it's already available, or
5593/// else return NULL.
5594SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00005595 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005596 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng08b11732008-03-22 01:55:50 +00005597 FoldingSetNodeID ID;
5598 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5599 void *IP = 0;
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +00005600 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng08b11732008-03-22 01:55:50 +00005601 return E;
5602 }
5603 return NULL;
5604}
5605
Evan Cheng31441b72010-03-29 20:48:30 +00005606/// getDbgValue - Creates a SDDbgValue node.
5607///
5608SDDbgValue *
5609SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5610 DebugLoc DL, unsigned O) {
5611 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5612}
5613
5614SDDbgValue *
Dan Gohman46510a72010-04-15 01:51:59 +00005615SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng31441b72010-03-29 20:48:30 +00005616 DebugLoc DL, unsigned O) {
5617 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5618}
5619
5620SDDbgValue *
5621SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5622 DebugLoc DL, unsigned O) {
5623 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5624}
5625
Dan Gohmana72d2a22010-03-03 21:33:37 +00005626namespace {
5627
Dan Gohmanba72b0c2010-03-04 19:11:28 +00005628/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohmana72d2a22010-03-03 21:33:37 +00005629/// pointed to by a use iterator is deleted, increment the use iterator
5630/// so that it doesn't dangle.
5631///
Dan Gohmana72d2a22010-03-03 21:33:37 +00005632class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohmana72d2a22010-03-03 21:33:37 +00005633 SDNode::use_iterator &UI;
5634 SDNode::use_iterator &UE;
5635
5636 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5637 // Increment the iterator as needed.
5638 while (UI != UE && N == *UI)
5639 ++UI;
Dan Gohmana72d2a22010-03-03 21:33:37 +00005640 }
5641
5642public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005643 RAUWUpdateListener(SelectionDAG &d,
Dan Gohmana72d2a22010-03-03 21:33:37 +00005644 SDNode::use_iterator &ui,
5645 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005646 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohmana72d2a22010-03-03 21:33:37 +00005647};
5648
5649}
5650
Evan Cheng99157a02006-08-07 22:13:29 +00005651/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00005652/// This can cause recursive merging of nodes in the DAG.
5653///
Chris Lattner11d049c2008-02-03 03:35:22 +00005654/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00005655///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005656void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005657 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005658 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00005659 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00005660 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00005661
Dan Gohman39946102009-01-25 16:29:12 +00005662 // Iterate over all the existing uses of From. New uses will be added
5663 // to the beginning of the use list, which we avoid visiting.
5664 // This specifically avoids visiting uses of From that arise while the
5665 // replacement is happening, because any such uses would be the result
5666 // of CSE: If an existing node looks like From after one of its operands
5667 // is replaced by To, we don't want to replace of all its users with To
5668 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00005669 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005670 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005671 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005672 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005673
Chris Lattner8b8749f2005-08-17 19:00:20 +00005674 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005675 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005676
Dan Gohman39946102009-01-25 16:29:12 +00005677 // A user can appear in a use list multiple times, and when this
5678 // happens the uses are usually next to each other in the list.
5679 // To help reduce the number of CSE recomputations, process all
5680 // the uses of this user that we can find this way.
5681 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005682 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005683 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005684 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005685 } while (UI != UE && *UI == User);
5686
5687 // Now that we have modified User, add it back to the CSE maps. If it
5688 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005689 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00005690 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005691
5692 // If we just RAUW'd the root, take note.
5693 if (FromN == getRoot())
5694 setRoot(To);
Chris Lattner8b52f212005-08-26 18:36:28 +00005695}
5696
5697/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5698/// This can cause recursive merging of nodes in the DAG.
5699///
Dan Gohmanc23e4962009-04-15 20:06:30 +00005700/// This version assumes that for each value of From, there is a
5701/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00005702///
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005703void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00005704#ifndef NDEBUG
5705 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5706 assert((!From->hasAnyUseOfValue(i) ||
5707 From->getValueType(i) == To->getValueType(i)) &&
5708 "Cannot use this version of ReplaceAllUsesWith!");
5709#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00005710
5711 // Handle the trivial case.
5712 if (From == To)
5713 return;
5714
Dan Gohmandbe664a2009-01-19 21:44:21 +00005715 // Iterate over just the existing users of From. See the comments in
5716 // the ReplaceAllUsesWith above.
5717 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005718 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005719 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005720 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005721
Chris Lattner8b52f212005-08-26 18:36:28 +00005722 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005723 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005724
Dan Gohman39946102009-01-25 16:29:12 +00005725 // A user can appear in a use list multiple times, and when this
5726 // happens the uses are usually next to each other in the list.
5727 // To help reduce the number of CSE recomputations, process all
5728 // the uses of this user that we can find this way.
5729 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005730 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005731 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005732 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00005733 } while (UI != UE && *UI == User);
5734
5735 // Now that we have modified User, add it back to the CSE maps. If it
5736 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005737 AddModifiedNodeToCSEMaps(User);
Chris Lattner8b8749f2005-08-17 19:00:20 +00005738 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005739
5740 // If we just RAUW'd the root, take note.
5741 if (From == getRoot().getNode())
5742 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattner8b8749f2005-08-17 19:00:20 +00005743}
5744
Chris Lattner8b52f212005-08-26 18:36:28 +00005745/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5746/// This can cause recursive merging of nodes in the DAG.
5747///
5748/// This version can replace From with any result values. To must match the
5749/// number and types of values returned by From.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005750void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner11d049c2008-02-03 03:35:22 +00005751 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005752 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005753
Dan Gohmandbe664a2009-01-19 21:44:21 +00005754 // Iterate over just the existing users of From. See the comments in
5755 // the ReplaceAllUsesWith above.
5756 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005757 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmandbe664a2009-01-19 21:44:21 +00005758 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00005759 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00005760
Chris Lattner7b2880c2005-08-24 22:44:39 +00005761 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00005762 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00005763
Dan Gohman39946102009-01-25 16:29:12 +00005764 // A user can appear in a use list multiple times, and when this
5765 // happens the uses are usually next to each other in the list.
5766 // To help reduce the number of CSE recomputations, process all
5767 // the uses of this user that we can find this way.
5768 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005769 SDUse &Use = UI.getUse();
5770 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00005771 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005772 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00005773 } while (UI != UE && *UI == User);
5774
5775 // Now that we have modified User, add it back to the CSE maps. If it
5776 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005777 AddModifiedNodeToCSEMaps(User);
Chris Lattner7b2880c2005-08-24 22:44:39 +00005778 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005779
5780 // If we just RAUW'd the root, take note.
5781 if (From == getRoot().getNode())
5782 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattner7b2880c2005-08-24 22:44:39 +00005783}
5784
Chris Lattner012f2412006-02-17 21:58:01 +00005785/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005786/// uses of other values produced by From.getNode() alone. The Deleted
5787/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005788void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005789 // Handle the really simple, really trivial case efficiently.
5790 if (From == To) return;
5791
Chris Lattner012f2412006-02-17 21:58:01 +00005792 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00005793 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005794 ReplaceAllUsesWith(From, To);
Chris Lattner012f2412006-02-17 21:58:01 +00005795 return;
5796 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00005797
Dan Gohman39946102009-01-25 16:29:12 +00005798 // Iterate over just the existing users of From. See the comments in
5799 // the ReplaceAllUsesWith above.
5800 SDNode::use_iterator UI = From.getNode()->use_begin(),
5801 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005802 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohman39946102009-01-25 16:29:12 +00005803 while (UI != UE) {
5804 SDNode *User = *UI;
5805 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00005806
Dan Gohman39946102009-01-25 16:29:12 +00005807 // A user can appear in a use list multiple times, and when this
5808 // happens the uses are usually next to each other in the list.
5809 // To help reduce the number of CSE recomputations, process all
5810 // the uses of this user that we can find this way.
5811 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00005812 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00005813
5814 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00005815 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00005816 ++UI;
5817 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00005818 }
Dan Gohman39946102009-01-25 16:29:12 +00005819
5820 // If this node hasn't been modified yet, it's still in the CSE maps,
5821 // so remove its old self from the CSE maps.
5822 if (!UserRemovedFromCSEMaps) {
5823 RemoveNodeFromCSEMaps(User);
5824 UserRemovedFromCSEMaps = true;
5825 }
5826
5827 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00005828 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00005829 } while (UI != UE && *UI == User);
5830
5831 // We are iterating over all uses of the From node, so if a use
5832 // doesn't use the specific value, no changes are made.
5833 if (!UserRemovedFromCSEMaps)
5834 continue;
5835
Chris Lattner01d029b2007-10-15 06:10:22 +00005836 // Now that we have modified User, add it back to the CSE maps. If it
5837 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005838 AddModifiedNodeToCSEMaps(User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005839 }
Dan Gohman65fd6562011-11-03 21:49:52 +00005840
5841 // If we just RAUW'd the root, take note.
5842 if (From == getRoot())
5843 setRoot(To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005844}
5845
Dan Gohman39946102009-01-25 16:29:12 +00005846namespace {
5847 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5848 /// to record information about a use.
5849 struct UseMemo {
5850 SDNode *User;
5851 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005852 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005853 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005854
5855 /// operator< - Sort Memos by User.
5856 bool operator<(const UseMemo &L, const UseMemo &R) {
5857 return (intptr_t)L.User < (intptr_t)R.User;
5858 }
Dan Gohman39946102009-01-25 16:29:12 +00005859}
5860
Dan Gohmane8be6c62008-07-17 19:10:17 +00005861/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005862/// uses of other values produced by From.getNode() alone. The same value
5863/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005864/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005865void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5866 const SDValue *To,
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005867 unsigned Num){
Dan Gohmane8be6c62008-07-17 19:10:17 +00005868 // Handle the simple, trivial case efficiently.
5869 if (Num == 1)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005870 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohmane8be6c62008-07-17 19:10:17 +00005871
Dan Gohman39946102009-01-25 16:29:12 +00005872 // Read up all the uses and make records of them. This helps
5873 // processing new uses that are introduced during the
5874 // replacement process.
5875 SmallVector<UseMemo, 4> Uses;
5876 for (unsigned i = 0; i != Num; ++i) {
5877 unsigned FromResNo = From[i].getResNo();
5878 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005879 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005880 E = FromNode->use_end(); UI != E; ++UI) {
5881 SDUse &Use = UI.getUse();
5882 if (Use.getResNo() == FromResNo) {
5883 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005884 Uses.push_back(Memo);
5885 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005886 }
Dan Gohman39946102009-01-25 16:29:12 +00005887 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005888
Dan Gohmane7852d02009-01-26 04:35:06 +00005889 // Sort the uses, so that all the uses from a given User are together.
5890 std::sort(Uses.begin(), Uses.end());
5891
Dan Gohman39946102009-01-25 16:29:12 +00005892 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5893 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005894 // We know that this user uses some value of From. If it is the right
5895 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005896 SDNode *User = Uses[UseIndex].User;
5897
5898 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005899 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005900
Dan Gohmane7852d02009-01-26 04:35:06 +00005901 // The Uses array is sorted, so all the uses for a given User
5902 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005903 // To help reduce the number of CSE recomputations, process all
5904 // the uses of this user that we can find this way.
5905 do {
5906 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005907 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005908 ++UseIndex;
5909
Dan Gohmane7852d02009-01-26 04:35:06 +00005910 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005911 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5912
Dan Gohmane8be6c62008-07-17 19:10:17 +00005913 // Now that we have modified User, add it back to the CSE maps. If it
5914 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005915 AddModifiedNodeToCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00005916 }
5917}
5918
Evan Chenge6f35d82006-08-01 08:20:41 +00005919/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005920/// based on their topological order. It returns the maximum id and a vector
5921/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005922unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005923
Dan Gohmanf06c8352008-09-30 18:30:35 +00005924 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005925
Dan Gohmanf06c8352008-09-30 18:30:35 +00005926 // SortedPos tracks the progress of the algorithm. Nodes before it are
5927 // sorted, nodes after it are unsorted. When the algorithm completes
5928 // it is at the end of the list.
5929 allnodes_iterator SortedPos = allnodes_begin();
5930
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005931 // Visit all the nodes. Move nodes with no operands to the front of
5932 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005933 // operand count. Before we do this, the Node Id fields of the nodes
5934 // may contain arbitrary values. After, the Node Id fields for nodes
5935 // before SortedPos will contain the topological sort index, and the
5936 // Node Id fields for nodes At SortedPos and after will contain the
5937 // count of outstanding operands.
5938 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5939 SDNode *N = I++;
David Greenecf495bc2010-01-20 20:13:31 +00005940 checkForCycles(N);
Dan Gohmanf06c8352008-09-30 18:30:35 +00005941 unsigned Degree = N->getNumOperands();
5942 if (Degree == 0) {
5943 // A node with no uses, add it to the result array immediately.
5944 N->setNodeId(DAGSize++);
5945 allnodes_iterator Q = N;
5946 if (Q != SortedPos)
5947 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene221925e2010-01-20 00:59:23 +00005948 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005949 ++SortedPos;
5950 } else {
5951 // Temporarily use the Node Id as scratch space for the degree count.
5952 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005953 }
5954 }
5955
Chad Rosierf496dea2012-05-21 17:13:41 +00005956 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohmanf06c8352008-09-30 18:30:35 +00005957 // such that by the time the end is reached all nodes will be sorted.
5958 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5959 SDNode *N = I;
David Greenecf495bc2010-01-20 20:13:31 +00005960 checkForCycles(N);
David Greene221925e2010-01-20 00:59:23 +00005961 // N is in sorted position, so all its uses have one less operand
5962 // that needs to be sorted.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005963 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5964 UI != UE; ++UI) {
5965 SDNode *P = *UI;
5966 unsigned Degree = P->getNodeId();
David Greene221925e2010-01-20 00:59:23 +00005967 assert(Degree != 0 && "Invalid node degree");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005968 --Degree;
5969 if (Degree == 0) {
5970 // All of P's operands are sorted, so P may sorted now.
5971 P->setNodeId(DAGSize++);
5972 if (P != SortedPos)
5973 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene221925e2010-01-20 00:59:23 +00005974 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005975 ++SortedPos;
5976 } else {
5977 // Update P's outstanding operand count.
5978 P->setNodeId(Degree);
5979 }
5980 }
David Greene221925e2010-01-20 00:59:23 +00005981 if (I == SortedPos) {
David Greene39143702010-02-09 23:03:05 +00005982#ifndef NDEBUG
5983 SDNode *S = ++I;
5984 dbgs() << "Overran sorted position:\n";
David Greene221925e2010-01-20 00:59:23 +00005985 S->dumprFull();
David Greene39143702010-02-09 23:03:05 +00005986#endif
5987 llvm_unreachable(0);
David Greene221925e2010-01-20 00:59:23 +00005988 }
Dan Gohmanf06c8352008-09-30 18:30:35 +00005989 }
5990
5991 assert(SortedPos == AllNodes.end() &&
5992 "Topological sort incomplete!");
5993 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5994 "First node in topological sort is not the entry token!");
5995 assert(AllNodes.front().getNodeId() == 0 &&
5996 "First node in topological sort has non-zero id!");
5997 assert(AllNodes.front().getNumOperands() == 0 &&
5998 "First node in topological sort has operands!");
5999 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
6000 "Last node in topologic sort has unexpected id!");
6001 assert(AllNodes.back().use_empty() &&
6002 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00006003 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00006004 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00006005}
6006
Evan Chengbfcb3052010-03-25 01:38:16 +00006007/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
6008/// value is produced by SD.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00006009void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
6010 DbgInfo->add(DB, SD, isParameter);
Evan Chengbfcb3052010-03-25 01:38:16 +00006011 if (SD)
6012 SD->setHasDebugValue(true);
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00006013}
Evan Cheng091cba12006-07-27 06:39:06 +00006014
Devang Patela2e868d2011-01-25 23:27:42 +00006015/// TransferDbgValues - Transfer SDDbgValues.
6016void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
6017 if (From == To || !From.getNode()->getHasDebugValue())
6018 return;
6019 SDNode *FromNode = From.getNode();
6020 SDNode *ToNode = To.getNode();
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006021 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patela778f5c2011-02-18 22:43:42 +00006022 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramer22a54c12011-06-18 13:13:44 +00006023 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patela2e868d2011-01-25 23:27:42 +00006024 I != E; ++I) {
Devang Patela778f5c2011-02-18 22:43:42 +00006025 SDDbgValue *Dbg = *I;
6026 if (Dbg->getKind() == SDDbgValue::SDNODE) {
6027 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
6028 Dbg->getOffset(), Dbg->getDebugLoc(),
6029 Dbg->getOrder());
6030 ClonedDVs.push_back(Clone);
Devang Patela2e868d2011-01-25 23:27:42 +00006031 }
6032 }
Craig Topperf22fd3f2013-07-03 05:11:49 +00006033 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patela778f5c2011-02-18 22:43:42 +00006034 E = ClonedDVs.end(); I != E; ++I)
6035 AddDbgValue(*I, ToNode, false);
Devang Patela2e868d2011-01-25 23:27:42 +00006036}
6037
Jim Laskey58b968b2005-08-17 20:08:02 +00006038//===----------------------------------------------------------------------===//
6039// SDNode Class
6040//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00006041
Chris Lattner48b85922007-02-04 02:41:42 +00006042HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00006043 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00006044}
6045
Andrew Trickac6d9be2013-05-25 02:42:55 +00006046GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
6047 DebugLoc DL, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006048 EVT VT, int64_t o, unsigned char TF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006049 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman383b5f62010-04-17 15:32:28 +00006050 TheGlobal = GA;
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00006051}
Chris Lattner48b85922007-02-04 02:41:42 +00006052
Matt Arsenault59d3ae62013-11-15 01:34:59 +00006053AddrSpaceCastSDNode::AddrSpaceCastSDNode(unsigned Order, DebugLoc dl, EVT VT,
6054 SDValue X, unsigned SrcAS,
6055 unsigned DestAS)
6056 : UnarySDNode(ISD::ADDRSPACECAST, Order, dl, getSDVTList(VT), X),
6057 SrcAddrSpace(SrcAS), DestAddrSpace(DestAS) {}
6058
Andrew Trickac6d9be2013-05-25 02:42:55 +00006059MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
6060 EVT memvt, MachineMemOperand *mmo)
6061 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006062 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006063 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006064 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greene1157f792010-02-17 20:21:42 +00006065 assert(isNonTemporal() == MMO->isNonTemporal() &&
6066 "Non-temporal encoding error!");
Dan Gohmanc76909a2009-09-25 20:36:54 +00006067 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00006068}
6069
Andrew Trickac6d9be2013-05-25 02:42:55 +00006070MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006071 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00006072 MachineMemOperand *mmo)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006073 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00006074 MemoryVT(memvt), MMO(mmo) {
David Greene1157f792010-02-17 20:21:42 +00006075 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00006076 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohmanc76909a2009-09-25 20:36:54 +00006077 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
6078 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00006079}
6080
Jim Laskey583bd472006-10-27 23:46:08 +00006081/// Profile - Gather unique data for the node.
6082///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00006083void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00006084 AddNodeIDNode(ID, this);
6085}
6086
Owen Andersond8110fb2009-08-25 22:27:22 +00006087namespace {
6088 struct EVTArray {
6089 std::vector<EVT> VTs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006090
Owen Andersond8110fb2009-08-25 22:27:22 +00006091 EVTArray() {
6092 VTs.reserve(MVT::LAST_VALUETYPE);
6093 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
6094 VTs.push_back(MVT((MVT::SimpleValueType)i));
6095 }
6096 };
6097}
6098
Owen Andersone50ed302009-08-10 22:56:29 +00006099static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00006100static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00006101static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00006102
Chris Lattnera3255112005-11-08 23:30:28 +00006103/// getValueTypeList - Return a pointer to the specified value type.
6104///
Owen Andersone50ed302009-08-10 22:56:29 +00006105const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006106 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00006107 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00006108 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00006109 } else {
Duncan Sandscdfad362010-11-03 12:17:33 +00006110 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandsad017dc2010-05-10 04:54:28 +00006111 "Value type out of range!");
Owen Andersond8110fb2009-08-25 22:27:22 +00006112 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00006113 }
Chris Lattnera3255112005-11-08 23:30:28 +00006114}
Duncan Sandsaf47b112007-10-16 09:56:48 +00006115
Chris Lattner5c884562005-01-12 18:37:47 +00006116/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
6117/// indicated value. This method ignores uses of other values defined by this
6118/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00006119bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00006120 assert(Value < getNumValues() && "Bad value!");
6121
Roman Levensteindc1adac2008-04-07 10:06:32 +00006122 // TODO: Only iterate over uses of a given value of the node
6123 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00006124 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00006125 if (NUses == 0)
6126 return false;
6127 --NUses;
6128 }
Chris Lattner5c884562005-01-12 18:37:47 +00006129 }
6130
6131 // Found exactly the right number of uses?
6132 return NUses == 0;
6133}
6134
6135
Evan Cheng33d55952007-08-02 05:29:38 +00006136/// hasAnyUseOfValue - Return true if there are any use of the indicated
6137/// value. This method ignores uses of other values defined by this operation.
6138bool SDNode::hasAnyUseOfValue(unsigned Value) const {
6139 assert(Value < getNumValues() && "Bad value!");
6140
Dan Gohman1373c1c2008-07-09 22:39:01 +00006141 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00006142 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00006143 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00006144
6145 return false;
6146}
6147
6148
Dan Gohman2a629952008-07-27 18:06:42 +00006149/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00006150///
Dan Gohman2a629952008-07-27 18:06:42 +00006151bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00006152 bool Seen = false;
6153 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006154 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00006155 if (User == this)
6156 Seen = true;
6157 else
6158 return false;
6159 }
6160
6161 return Seen;
6162}
6163
Evan Chenge6e97e62006-11-03 07:31:32 +00006164/// isOperand - Return true if this node is an operand of N.
6165///
Dan Gohman475871a2008-07-27 21:46:04 +00006166bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00006167 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6168 if (*this == N->getOperand(i))
6169 return true;
6170 return false;
6171}
6172
Evan Cheng917be682008-03-04 00:41:45 +00006173bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006174 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00006175 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00006176 return true;
6177 return false;
6178}
Evan Cheng4ee62112006-02-05 06:29:23 +00006179
Chris Lattner572dee72008-01-16 05:49:24 +00006180/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00006181/// be a chain) reaches the specified operand without crossing any
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006182/// side-effecting instructions on any chain path. In practice, this looks
6183/// through token factors and non-volatile loads. In order to remain efficient,
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006184/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00006185bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00006186 unsigned Depth) const {
6187 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00006188
Chris Lattner572dee72008-01-16 05:49:24 +00006189 // Don't search too deeply, we just want to be able to see through
6190 // TokenFactor's etc.
6191 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006192
Chris Lattner572dee72008-01-16 05:49:24 +00006193 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006194 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner572dee72008-01-16 05:49:24 +00006195 if (getOpcode() == ISD::TokenFactor) {
6196 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Anderson14ac1dd2010-09-18 04:45:14 +00006197 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6198 return false;
6199 return true;
Chris Lattner572dee72008-01-16 05:49:24 +00006200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006201
Chris Lattner572dee72008-01-16 05:49:24 +00006202 // Loads don't have side effects, look through them.
6203 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6204 if (!Ld->isVolatile())
6205 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6206 }
6207 return false;
6208}
6209
Lang Hames944520f2011-07-07 04:31:51 +00006210/// hasPredecessor - Return true if N is a predecessor of this node.
6211/// N is either an operand of this node, or can be reached by recursively
6212/// traversing up the operands.
6213/// NOTE: This is an expensive method. Use it carefully.
6214bool SDNode::hasPredecessor(const SDNode *N) const {
6215 SmallPtrSet<const SDNode *, 32> Visited;
6216 SmallVector<const SDNode *, 16> Worklist;
6217 return hasPredecessorHelper(N, Visited, Worklist);
6218}
Dan Gohman6688d612009-10-28 03:44:30 +00006219
Craig Toppera0ec3f92013-07-14 04:42:23 +00006220bool
6221SDNode::hasPredecessorHelper(const SDNode *N,
6222 SmallPtrSet<const SDNode *, 32> &Visited,
6223 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames944520f2011-07-07 04:31:51 +00006224 if (Visited.empty()) {
6225 Worklist.push_back(this);
6226 } else {
6227 // Take a look in the visited set. If we've already encountered this node
6228 // we needn't search further.
6229 if (Visited.count(N))
6230 return true;
6231 }
6232
6233 // Haven't visited N yet. Continue the search.
6234 while (!Worklist.empty()) {
6235 const SDNode *M = Worklist.pop_back_val();
6236 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6237 SDNode *Op = M->getOperand(i).getNode();
Dan Gohman6688d612009-10-28 03:44:30 +00006238 if (Visited.insert(Op))
6239 Worklist.push_back(Op);
Lang Hames944520f2011-07-07 04:31:51 +00006240 if (Op == N)
6241 return true;
Dan Gohman6688d612009-10-28 03:44:30 +00006242 }
Lang Hames944520f2011-07-07 04:31:51 +00006243 }
Dan Gohman6688d612009-10-28 03:44:30 +00006244
6245 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00006246}
6247
Evan Chengc5484282006-10-04 00:56:09 +00006248uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6249 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006250 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00006251}
6252
Mon P Wangcd6e7252009-11-30 02:42:02 +00006253SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6254 assert(N->getNumValues() == 1 &&
6255 "Can't unroll a vector with multiple results!");
6256
6257 EVT VT = N->getValueType(0);
6258 unsigned NE = VT.getVectorNumElements();
6259 EVT EltVT = VT.getVectorElementType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006260 SDLoc dl(N);
Mon P Wangcd6e7252009-11-30 02:42:02 +00006261
6262 SmallVector<SDValue, 8> Scalars;
6263 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6264
6265 // If ResNE is 0, fully unroll the vector op.
6266 if (ResNE == 0)
6267 ResNE = NE;
6268 else if (NE > ResNE)
6269 NE = ResNE;
6270
6271 unsigned i;
6272 for (i= 0; i != NE; ++i) {
Bill Wendlingd71bb562010-04-30 22:19:17 +00006273 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wangcd6e7252009-11-30 02:42:02 +00006274 SDValue Operand = N->getOperand(j);
6275 EVT OperandVT = Operand.getValueType();
6276 if (OperandVT.isVector()) {
6277 // A vector operand; extract a single element.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006278 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wangcd6e7252009-11-30 02:42:02 +00006279 EVT OperandEltVT = OperandVT.getVectorElementType();
6280 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6281 OperandEltVT,
6282 Operand,
Tom Stellard425b76c2013-08-05 22:22:01 +00006283 getConstant(i, TLI->getVectorIdxTy()));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006284 } else {
6285 // A scalar operand; just use it as is.
6286 Operands[j] = Operand;
6287 }
6288 }
6289
6290 switch (N->getOpcode()) {
6291 default:
6292 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6293 &Operands[0], Operands.size()));
6294 break;
Nadav Rotemaec58612011-09-13 19:17:42 +00006295 case ISD::VSELECT:
6296 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6297 &Operands[0], Operands.size()));
6298 break;
Mon P Wangcd6e7252009-11-30 02:42:02 +00006299 case ISD::SHL:
6300 case ISD::SRA:
6301 case ISD::SRL:
6302 case ISD::ROTL:
6303 case ISD::ROTR:
6304 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Jack Carter3af4d252013-09-09 22:02:08 +00006305 getShiftAmountOperand(Operands[0].getValueType(),
6306 Operands[1])));
Mon P Wangcd6e7252009-11-30 02:42:02 +00006307 break;
Dan Gohmand1996362010-01-09 02:13:55 +00006308 case ISD::SIGN_EXTEND_INREG:
6309 case ISD::FP_ROUND_INREG: {
6310 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6311 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6312 Operands[0],
6313 getValueType(ExtVT)));
6314 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00006315 }
6316 }
6317
6318 for (; i < ResNE; ++i)
6319 Scalars.push_back(getUNDEF(EltVT));
6320
6321 return getNode(ISD::BUILD_VECTOR, dl,
6322 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6323 &Scalars[0], Scalars.size());
6324}
6325
Evan Cheng64fa4a92009-12-09 01:36:00 +00006326
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006327/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6328/// location that is 'Dist' units away from the location that the 'Base' load
Evan Cheng64fa4a92009-12-09 01:36:00 +00006329/// is loading from.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006330bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Cheng64fa4a92009-12-09 01:36:00 +00006331 unsigned Bytes, int Dist) const {
6332 if (LD->getChain() != Base->getChain())
6333 return false;
6334 EVT VT = LD->getValueType(0);
6335 if (VT.getSizeInBits() / 8 != Bytes)
6336 return false;
6337
6338 SDValue Loc = LD->getOperand(1);
6339 SDValue BaseLoc = Base->getOperand(1);
6340 if (Loc.getOpcode() == ISD::FrameIndex) {
6341 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6342 return false;
6343 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6344 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6345 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6346 int FS = MFI->getObjectSize(FI);
6347 int BFS = MFI->getObjectSize(BFI);
6348 if (FS != BFS || FS != (int)Bytes) return false;
6349 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6350 }
Chris Lattner0a9481f2011-02-13 22:25:43 +00006351
6352 // Handle X+C
6353 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6354 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6355 return true;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006356
Dan Gohman46510a72010-04-15 01:51:59 +00006357 const GlobalValue *GV1 = NULL;
6358 const GlobalValue *GV2 = NULL;
Evan Cheng64fa4a92009-12-09 01:36:00 +00006359 int64_t Offset1 = 0;
6360 int64_t Offset2 = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006361 const TargetLowering *TLI = TM.getTargetLowering();
6362 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6363 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Cheng64fa4a92009-12-09 01:36:00 +00006364 if (isGA1 && isGA2 && GV1 == GV2)
6365 return Offset1 == (Offset2 + Dist*Bytes);
6366 return false;
6367}
6368
6369
Evan Chengf2dc5c72009-12-09 01:04:59 +00006370/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6371/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00006372unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Cheng7bd64782009-12-09 01:53:58 +00006373 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohman46510a72010-04-15 01:51:59 +00006374 const GlobalValue *GV;
Evan Cheng7bd64782009-12-09 01:53:58 +00006375 int64_t GVOffset = 0;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006376 const TargetLowering *TLI = TM.getTargetLowering();
6377 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
Matt Arsenaulte6e81122013-11-16 20:50:54 +00006378 unsigned PtrWidth = TLI->getPointerTypeSizeInBits(GV->getType());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006379 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00006380 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006381 TLI->getDataLayout());
Eli Friedmanc4c2a022011-11-28 22:48:22 +00006382 unsigned AlignBits = KnownZero.countTrailingOnes();
6383 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6384 if (Align)
6385 return MinAlign(Align, GVOffset);
Evan Cheng255f20f2010-04-01 06:04:33 +00006386 }
Evan Cheng7bd64782009-12-09 01:53:58 +00006387
Evan Chengf2dc5c72009-12-09 01:04:59 +00006388 // If this is a direct reference to a stack slot, use information about the
6389 // stack slot's alignment.
6390 int FrameIdx = 1 << 31;
6391 int64_t FrameOffset = 0;
6392 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6393 FrameIdx = FI->getIndex();
Chris Lattner0a9481f2011-02-13 22:25:43 +00006394 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Chengf2dc5c72009-12-09 01:04:59 +00006395 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner0a9481f2011-02-13 22:25:43 +00006396 // Handle FI+Cst
Evan Chengf2dc5c72009-12-09 01:04:59 +00006397 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6398 FrameOffset = Ptr.getConstantOperandVal(1);
6399 }
6400
6401 if (FrameIdx != (1 << 31)) {
Evan Chengf2dc5c72009-12-09 01:04:59 +00006402 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Chengde2ace12009-12-09 01:17:24 +00006403 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6404 FrameOffset);
Evan Chengde2ace12009-12-09 01:17:24 +00006405 return FIInfoAlign;
Evan Chengf2dc5c72009-12-09 01:04:59 +00006406 }
6407
6408 return 0;
6409}
6410
Bill Wendlingee287ca2013-11-22 05:17:44 +00006411/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
6412/// which is split (or expanded) into two not necessarily identical pieces.
6413std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
6414 // Currently all types are split in half.
6415 EVT LoVT, HiVT;
6416 if (!VT.isVector()) {
6417 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
6418 } else {
6419 unsigned NumElements = VT.getVectorNumElements();
6420 assert(!(NumElements & 1) && "Splitting vector, but not in half!");
6421 LoVT = HiVT = EVT::getVectorVT(*getContext(), VT.getVectorElementType(),
6422 NumElements/2);
6423 }
6424 return std::make_pair(LoVT, HiVT);
6425}
6426
6427/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
6428/// low/high part.
6429std::pair<SDValue, SDValue>
6430SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
6431 const EVT &HiVT) {
6432 assert(LoVT.getVectorNumElements() + HiVT.getVectorNumElements() <=
6433 N.getValueType().getVectorNumElements() &&
6434 "More vector elements requested than available!");
6435 SDValue Lo, Hi;
6436 Lo = getNode(ISD::EXTRACT_SUBVECTOR, DL, LoVT, N,
6437 getConstant(0, TLI->getVectorIdxTy()));
6438 Hi = getNode(ISD::EXTRACT_SUBVECTOR, DL, HiVT, N,
6439 getConstant(LoVT.getVectorNumElements(), TLI->getVectorIdxTy()));
6440 return std::make_pair(Lo, Hi);
6441}
6442
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00006443// getAddressSpace - Return the address space this GlobalAddress belongs to.
6444unsigned GlobalAddressSDNode::getAddressSpace() const {
6445 return getGlobal()->getType()->getAddressSpace();
6446}
6447
6448
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006449Type *ConstantPoolSDNode::getType() const {
Evan Chengd6594ae2006-09-12 21:00:35 +00006450 if (isMachineConstantPoolEntry())
6451 return Val.MachineCPVal->getType();
6452 return Val.ConstVal->getType();
6453}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006454
Bob Wilson24e338e2009-03-02 23:24:16 +00006455bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6456 APInt &SplatUndef,
6457 unsigned &SplatBitSize,
6458 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00006459 unsigned MinSplatBits,
6460 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00006461 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00006462 assert(VT.isVector() && "Expected a vector type");
6463 unsigned sz = VT.getSizeInBits();
6464 if (MinSplatBits > sz)
6465 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006466
Bob Wilson24e338e2009-03-02 23:24:16 +00006467 SplatValue = APInt(sz, 0);
6468 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006469
Bob Wilson24e338e2009-03-02 23:24:16 +00006470 // Get the bits. Bits with undefined values (when the corresponding element
6471 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6472 // in SplatValue. If any of the values are not constant, give up and return
6473 // false.
6474 unsigned int nOps = getNumOperands();
6475 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6476 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006477
6478 for (unsigned j = 0; j < nOps; ++j) {
6479 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006480 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006481 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006482
Bob Wilson24e338e2009-03-02 23:24:16 +00006483 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006484 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006485 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad40f8f622010-12-07 08:25:19 +00006486 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohman58c25872010-04-12 02:24:01 +00006487 zextOrTrunc(sz) << BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006488 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006489 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006490 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006491 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006492 }
6493
Bob Wilson24e338e2009-03-02 23:24:16 +00006494 // The build_vector is all constants or undefs. Find the smallest element
6495 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006496
Bob Wilson24e338e2009-03-02 23:24:16 +00006497 HasAnyUndefs = (SplatUndef != 0);
6498 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006499
Bob Wilson24e338e2009-03-02 23:24:16 +00006500 unsigned HalfSize = sz / 2;
Jay Foad40f8f622010-12-07 08:25:19 +00006501 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6502 APInt LowValue = SplatValue.trunc(HalfSize);
6503 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6504 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006505
Bob Wilson24e338e2009-03-02 23:24:16 +00006506 // If the two halves do not match (ignoring undef bits), stop here.
6507 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6508 MinSplatBits > HalfSize)
6509 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006510
Bob Wilson24e338e2009-03-02 23:24:16 +00006511 SplatValue = HighValue | LowValue;
6512 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006513
Bob Wilson24e338e2009-03-02 23:24:16 +00006514 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006515 }
6516
Bob Wilson24e338e2009-03-02 23:24:16 +00006517 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006518 return true;
6519}
Nate Begeman9008ca62009-04-27 18:41:29 +00006520
Owen Andersone50ed302009-08-10 22:56:29 +00006521bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006522 // Find the first non-undef value in the shuffle mask.
6523 unsigned i, e;
6524 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6525 /* search */;
6526
Nate Begemana6415752009-04-29 18:13:31 +00006527 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006528
Nate Begeman5a5ca152009-04-29 05:20:52 +00006529 // Make sure all remaining elements are either undef or the same as the first
6530 // non-undef value.
6531 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006532 if (Mask[i] >= 0 && Mask[i] != Idx)
6533 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006534 return true;
6535}
David Greenecf495bc2010-01-20 20:13:31 +00006536
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006537#ifdef XDEBUG
David Greenecf495bc2010-01-20 20:13:31 +00006538static void checkForCyclesHelper(const SDNode *N,
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006539 SmallPtrSet<const SDNode*, 32> &Visited,
6540 SmallPtrSet<const SDNode*, 32> &Checked) {
6541 // If this node has already been checked, don't check it again.
6542 if (Checked.count(N))
David Greenee3d97c72010-02-23 17:37:50 +00006543 return;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006544
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006545 // If a node has already been visited on this depth-first walk, reject it as
6546 // a cycle.
6547 if (!Visited.insert(N)) {
David Greenecf495bc2010-01-20 20:13:31 +00006548 dbgs() << "Offending node:\n";
6549 N->dumprFull();
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006550 errs() << "Detected cycle in SelectionDAG\n";
6551 abort();
David Greenecf495bc2010-01-20 20:13:31 +00006552 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006553
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006554 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6555 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006556
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006557 Checked.insert(N);
6558 Visited.erase(N);
David Greenecf495bc2010-01-20 20:13:31 +00006559}
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006560#endif
David Greenecf495bc2010-01-20 20:13:31 +00006561
6562void llvm::checkForCycles(const llvm::SDNode *N) {
6563#ifdef XDEBUG
Alp Toker18a988e2013-10-29 02:35:28 +00006564 assert(N && "Checking nonexistent SDNode");
Chris Lattner46ca5ef2010-02-24 21:34:04 +00006565 SmallPtrSet<const SDNode*, 32> visited;
6566 SmallPtrSet<const SDNode*, 32> checked;
David Greenee3d97c72010-02-23 17:37:50 +00006567 checkForCyclesHelper(N, visited, checked);
David Greenecf495bc2010-01-20 20:13:31 +00006568#endif
6569}
6570
6571void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6572 checkForCycles(DAG->getRoot().getNode());
6573}