blob: 81d133a9ebe8a13b92ee49500189a4e0f45261dd [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Bill Wendling6de55a02009-12-21 19:34:59 +000013
Chris Lattner600d3082003-08-11 14:57:33 +000014#include "llvm/CodeGen/SelectionDAG.h"
Evan Cheng00fd0b62010-03-14 19:56:39 +000015#include "SDNodeDbgValue.h"
Chandler Carruthed0881b2012-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 Carruthd3e73552013-01-07 03:08:10 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Analysis/ValueTracking.h"
23#include "llvm/Assembly/Writer.h"
Chandler Carruthed0881b2012-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 Wendlinge38859d2012-06-28 00:05:13 +000028#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-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 Wendlingbd092622008-09-30 21:22:07 +000037#include "llvm/Support/CommandLine.h"
David Greened93137d2010-01-05 01:24:36 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Owen Anderson5defd562009-06-25 17:09:00 +000040#include "llvm/Support/ManagedStatic.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000041#include "llvm/Support/MathExtras.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000042#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-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 Cohen7d1670da2005-01-09 20:41:56 +000051#include <algorithm>
Jeff Cohencc08c832006-12-02 02:22:01 +000052#include <cmath>
Chris Lattner560b5e42004-06-02 04:28:06 +000053using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000054
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000055/// makeVTList - Return an instance of the SDVTList struct initialized with the
56/// specified members.
Owen Anderson53aa7a92009-08-10 22:56:29 +000057static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +000058 SDVTList Res = {VTs, NumVTs};
59 return Res;
60}
61
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +000062// Default null implementations of the callbacks.
63void SelectionDAG::DAGUpdateListener::NodeDeleted(SDNode*, SDNode*) {}
64void SelectionDAG::DAGUpdateListener::NodeUpdated(SDNode*) {}
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +000065
Jim Laskeyd66e6162005-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 Johannesenb6d2bec2007-08-26 01:18:27 +000074bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanec270fb2008-09-12 18:08:03 +000075 return getValueAPF().bitwiseIsEqual(V);
Jim Laskeyd66e6162005-08-17 20:08:02 +000076}
77
Owen Anderson53aa7a92009-08-10 22:56:29 +000078bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesend246b2c2007-08-30 00:23:21 +000079 const APFloat& Val) {
Duncan Sands13237ac2008-06-06 12:08:01 +000080 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelcf0da6c2009-02-17 22:15:04 +000081
Dale Johannesend246b2c2007-08-30 00:23:21 +000082 // convert modifies in place, so make a copy.
83 APFloat Val2 = APFloat(Val);
Dale Johannesen4f0bd682008-10-09 23:00:39 +000084 bool losesInfo;
Tim Northover29178a32013-01-22 09:46:31 +000085 (void) Val2.convert(SelectionDAG::EVTToAPFloatSemantics(VT),
86 APFloat::rmNearestTiesToEven,
Dale Johannesen4f0bd682008-10-09 23:00:39 +000087 &losesInfo);
88 return !losesInfo;
Dale Johannesend246b2c2007-08-30 00:23:21 +000089}
90
Jim Laskeyd66e6162005-08-17 20:08:02 +000091//===----------------------------------------------------------------------===//
Chris Lattnerc2d28112006-03-25 22:57:01 +000092// ISD Namespace
Jim Laskeyd66e6162005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000094
Evan Chengc70e33c2006-03-27 06:58:47 +000095/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattnerc2d28112006-03-25 22:57:01 +000096/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chengc70e33c2006-03-27 06:58:47 +000097bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner7e7ad592006-04-15 23:38:00 +000098 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +000099 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000100 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000101
Evan Chengc70e33c2006-03-27 06:58:47 +0000102 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000103
Chris Lattnerc2d28112006-03-25 22:57:01 +0000104 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000105
Chris Lattnerc2d28112006-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 Michelcf0da6c2009-02-17 22:15:04 +0000109
Chris Lattnerc2d28112006-03-25 22:57:01 +0000110 // Do not accept an all-undef vector.
111 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000112
Chris Lattnerc2d28112006-03-25 22:57:01 +0000113 // Do not accept build_vectors that aren't all constants or which have non-~0
Jim Grosbache13adc32012-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 Gohman2ce6f2a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Jim Grosbache13adc32012-03-21 17:48:04 +0000122 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
124 if (CN->getAPIntValue().countTrailingOnes() < EltSize)
Evan Chengc70e33c2006-03-27 06:58:47 +0000125 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000126 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
127 if (CFPN->getValueAPF().bitcastToAPInt().countTrailingOnes() < EltSize)
Dan Gohmanbd2fa562008-02-29 01:47:35 +0000128 return false;
Evan Chengc70e33c2006-03-27 06:58:47 +0000129 } else
Chris Lattnerc2d28112006-03-25 22:57:01 +0000130 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Chris Lattnerc2d28112006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
Jim Grosbache13adc32012-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 Lattnerc2d28112006-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 Chenga6789912006-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 Lattner7e7ad592006-04-15 23:38:00 +0000146 // Look through a bit convert.
Wesley Peck527da1b2010-11-23 03:31:01 +0000147 if (N->getOpcode() == ISD::BITCAST)
Gabor Greiff304a7a2008-08-28 21:40:38 +0000148 N = N->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000149
Evan Chenga6789912006-03-26 09:50:58 +0000150 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000151
Evan Chengc70e33c2006-03-27 06:58:47 +0000152 unsigned i = 0, e = N->getNumOperands();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000153
Evan Chengc70e33c2006-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 Michelcf0da6c2009-02-17 22:15:04 +0000157
Evan Chengc70e33c2006-03-27 06:58:47 +0000158 // Do not accept an all-undef vector.
159 if (i == e) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000161 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chengc70e33c2006-03-27 06:58:47 +0000162 // elements.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000163 SDValue Zero = N->getOperand(i);
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000164 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Zero)) {
165 if (!CN->isNullValue())
Evan Chengc70e33c2006-03-27 06:58:47 +0000166 return false;
Jakub Staszakec5a2f22012-09-30 21:24:57 +0000167 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Zero)) {
168 if (!CFPN->getValueAPF().isPosZero())
Evan Chengc70e33c2006-03-27 06:58:47 +0000169 return false;
170 } else
171 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000172
Dan Gohmanc2eed3b2009-06-04 16:49:15 +0000173 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chengc70e33c2006-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 Chenga6789912006-03-26 09:50:58 +0000180}
181
Evan Cheng6200c222008-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 Wang88ff56c2010-11-19 19:08:12 +0000194 if (NumElems == 1)
195 return false;
Evan Cheng6200c222008-02-18 23:04:32 +0000196 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000197 SDValue V = N->getOperand(i);
Evan Cheng6200c222008-02-18 23:04:32 +0000198 if (V.getOpcode() != ISD::UNDEF)
199 return false;
200 }
201 return true;
202}
203
Nadav Rotema62368c2012-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 Lattner061a1ea2005-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 Wendling8ec2a4a2008-10-19 20:51:12 +0000229 (OldG << 2)); // New L bit.
Chris Lattner061a1ea2005-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 Wendling8ec2a4a2008-10-19 20:51:12 +0000240
Chris Lattner061a1ea2005-01-07 07:46:32 +0000241 if (Operation > ISD::SETTRUE2)
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000242 Operation &= ~8; // Don't let N and U bits get set.
243
Chris Lattner061a1ea2005-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 Edwinfbcc6632009-07-14 16:55:14 +0000253 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattner061a1ea2005-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 Brukman835702a2005-04-21 22:36:52 +0000276
Chris Lattner061a1ea2005-01-07 07:46:32 +0000277 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000278
Chris Lattner061a1ea2005-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 Lattner8c02c3f2006-05-12 17:03:46 +0000282 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000283
Chris Lattner8c02c3f2006-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 Michelcf0da6c2009-02-17 22:15:04 +0000287
Chris Lattner061a1ea2005-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 Brukman835702a2005-04-21 22:36:52 +0000299 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000300
301 // Combine all of the condition bits.
Chris Lattner393d96a2006-04-27 05:01:07 +0000302 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000303
Chris Lattner393d96a2006-04-27 05:01:07 +0000304 // Canonicalize illegal integer setcc's.
305 if (isInteger) {
306 switch (Result) {
307 default: break;
Chris Lattner710b3d52006-06-28 18:29:47 +0000308 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohman3ab94df2008-05-14 18:17:09 +0000309 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner710b3d52006-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 Lattner393d96a2006-04-27 05:01:07 +0000313 }
314 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000315
Chris Lattner393d96a2006-04-27 05:01:07 +0000316 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000317}
318
Jim Laskeyd66e6162005-08-17 20:08:02 +0000319//===----------------------------------------------------------------------===//
Jim Laskeyf576b422006-10-27 23:46:08 +0000320// SDNode Profile Support
321//===----------------------------------------------------------------------===//
322
Jim Laskeybd0f0882006-10-27 23:52:51 +0000323/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
324///
Jim Laskeyf576b422006-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 Gohmand78c4002008-05-13 00:00:25 +0000331static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000332 ID.AddPointer(VTList.VTs);
Jim Laskeyf576b422006-10-27 23:46:08 +0000333}
334
Jim Laskeybd0f0882006-10-27 23:52:51 +0000335/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
336///
Jim Laskeyf576b422006-10-27 23:46:08 +0000337static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000338 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf17b4222007-02-04 07:28:00 +0000339 for (; NumOps; --NumOps, ++Ops) {
Gabor Greiff304a7a2008-08-28 21:40:38 +0000340 ID.AddPointer(Ops->getNode());
Gabor Greifabfdf922008-08-26 22:36:50 +0000341 ID.AddInteger(Ops->getResNo());
Chris Lattnerf17b4222007-02-04 07:28:00 +0000342 }
Jim Laskeyf576b422006-10-27 23:46:08 +0000343}
344
Dan Gohman768f2c92008-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 Gohman8e4ac9b2009-01-26 04:35:06 +0000350 ID.AddPointer(Ops->getNode());
351 ID.AddInteger(Ops->getResNo());
Dan Gohman768f2c92008-07-07 18:26:29 +0000352 }
353}
354
Jim Laskeyf576b422006-10-27 23:46:08 +0000355static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelcf0da6c2009-02-17 22:15:04 +0000356 unsigned short OpC, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000357 const SDValue *OpList, unsigned N) {
Jim Laskeyf576b422006-10-27 23:46:08 +0000358 AddNodeIDOpcode(ID, OpC);
359 AddNodeIDValueTypes(ID, VTList);
360 AddNodeIDOperands(ID, OpList, N);
361}
362
Duncan Sands835bdca2008-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 Johannesen4bbd2ee2007-03-30 21:38:07 +0000366 switch (N->getOpcode()) {
Chris Lattner8e34f982009-06-25 21:21:14 +0000367 case ISD::TargetExternalSymbol:
368 case ISD::ExternalSymbol:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000369 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000370 default: break; // Normal nodes don't need extra info.
371 case ISD::TargetConstant:
372 case ISD::Constant:
Dan Gohmanec270fb2008-09-12 18:08:03 +0000373 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000374 break;
375 case ISD::TargetConstantFP:
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000376 case ISD::ConstantFP: {
Dan Gohmanec270fb2008-09-12 18:08:03 +0000377 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000378 break;
Dale Johannesen3cf889f2007-08-31 04:03:46 +0000379 }
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000380 case ISD::TargetGlobalAddress:
Lauro Ramos Venancio25188892007-04-20 21:38:10 +0000381 case ISD::GlobalAddress:
382 case ISD::TargetGlobalTLSAddress:
383 case ISD::GlobalTLSAddress: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000384 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000385 ID.AddPointer(GA->getGlobal());
386 ID.AddInteger(GA->getOffset());
Chris Lattner8e34f982009-06-25 21:21:14 +0000387 ID.AddInteger(GA->getTargetFlags());
Pete Cooper91244262012-07-30 20:23:19 +0000388 ID.AddInteger(GA->getAddressSpace());
Dale Johannesen4bbd2ee2007-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 Olesen9349351d2012-01-18 23:52:12 +0000397 case ISD::RegisterMask:
398 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
399 break;
Dan Gohman2d489b52008-02-06 22:27:42 +0000400 case ISD::SRCVALUE:
401 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
402 break;
Dale Johannesen4bbd2ee2007-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 Lattnerb3586b62009-06-25 21:35:31 +0000410 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000411 break;
412 case ISD::ConstantPool:
413 case ISD::TargetConstantPool: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000414 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000415 ID.AddInteger(CP->getAlignment());
416 ID.AddInteger(CP->getOffset());
417 if (CP->isMachineConstantPoolEntry())
Jim Grosbachaf136f72011-09-27 20:59:33 +0000418 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000419 else
420 ID.AddPointer(CP->getConstVal());
Chris Lattnerb3586b62009-06-25 21:35:31 +0000421 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000422 break;
423 }
Jakob Stoklund Olesen505715d2012-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 Johannesen4bbd2ee2007-03-30 21:38:07 +0000431 case ISD::LOAD: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000432 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000434 ID.AddInteger(LD->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000435 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000436 break;
437 }
438 case ISD::STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000439 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sandsf1123e52008-06-06 12:49:32 +0000440 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +0000441 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +0000442 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +0000443 break;
444 }
Dan Gohman12f24902008-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 Friedman342e8df2011-08-24 20:50:09 +0000456 case ISD::ATOMIC_LOAD_UMAX:
457 case ISD::ATOMIC_LOAD:
458 case ISD::ATOMIC_STORE: {
Dan Gohman2da2bed2008-08-20 15:58:01 +0000459 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman76a07f52009-02-03 00:08:45 +0000460 ID.AddInteger(AT->getMemoryVT().getRawBits());
461 ID.AddInteger(AT->getRawSubclassData());
Pete Cooper91244262012-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 Wang6a490372008-06-25 08:15:39 +0000468 break;
Jim Laskeyf576b422006-10-27 23:46:08 +0000469 }
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000470 case ISD::VECTOR_SHUFFLE: {
471 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopherdfda92b2009-08-22 00:40:45 +0000472 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000473 i != e; ++i)
474 ID.AddInteger(SVN->getMaskElt(i));
475 break;
476 }
Dan Gohman6c938802009-10-30 01:27:03 +0000477 case ISD::TargetBlockAddress:
478 case ISD::BlockAddress: {
Michael Liaoabb87d42012-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 Gohman6c938802009-10-30 01:27:03 +0000483 break;
484 }
Mon P Wang6a490372008-06-25 08:15:39 +0000485 } // end switch (N->getOpcode())
Pete Cooper91244262012-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 Laskeyf576b422006-10-27 23:46:08 +0000490}
491
Duncan Sands835bdca2008-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 Gohman2da2bed2008-08-20 15:58:01 +0000505/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
David Greeneb7941b02010-02-17 20:21:42 +0000506/// the CSE map that carries volatility, temporalness, indexing mode, and
Dan Gohman76a07f52009-02-03 00:08:45 +0000507/// extension/truncation information.
Dan Gohman2da2bed2008-08-20 15:58:01 +0000508///
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000509static inline unsigned
David Greeneb7941b02010-02-17 20:21:42 +0000510encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000511 bool isNonTemporal, bool isInvariant) {
Dan Gohman76a07f52009-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 Greeneb7941b02010-02-17 20:21:42 +0000518 (isVolatile << 5) |
Pete Cooper82cd9e82011-11-08 18:42:53 +0000519 (isNonTemporal << 6) |
520 (isInvariant << 7);
Dan Gohman2da2bed2008-08-20 15:58:01 +0000521}
522
Jim Laskeyf576b422006-10-27 23:46:08 +0000523//===----------------------------------------------------------------------===//
Jim Laskeyd66e6162005-08-17 20:08:02 +0000524// SelectionDAG Class
525//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000526
Duncan Sands835bdca2008-10-27 15:30:53 +0000527/// doNotCSE - Return true if CSE should not be performed for this node.
528static bool doNotCSE(SDNode *N) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000529 if (N->getValueType(0) == MVT::Glue)
Duncan Sands835bdca2008-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 Sands835bdca2008-10-27 15:30:53 +0000535 case ISD::EH_LABEL:
Duncan Sands835bdca2008-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 Lattner3e5fbd72010-12-21 02:38:05 +0000541 if (N->getValueType(i) == MVT::Glue)
Duncan Sands835bdca2008-10-27 15:30:53 +0000542 return true; // Never CSE anything that produces a flag.
543
544 return false;
545}
546
Chris Lattner9c667932005-01-07 21:09:16 +0000547/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner8927c872006-08-04 17:45:20 +0000548/// SelectionDAG.
549void SelectionDAG::RemoveDeadNodes() {
Chris Lattner9c667932005-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 Lattner06f1d0f2005-10-05 06:35:28 +0000552 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000553
Chris Lattner8927c872006-08-04 17:45:20 +0000554 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000555
Chris Lattner8927c872006-08-04 17:45:20 +0000556 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000557 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner8927c872006-08-04 17:45:20 +0000558 if (I->use_empty())
559 DeadNodes.push_back(I);
560
Dan Gohman91697632008-07-07 20:57:48 +0000561 RemoveDeadNodes(DeadNodes);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000562
Dan Gohman91697632008-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 Olesenbeb94692012-04-20 22:08:46 +0000569void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes) {
Dan Gohman91697632008-07-07 20:57:48 +0000570
Chris Lattner8927c872006-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 Gohman8e4ac9b2009-01-26 04:35:06 +0000574 SDNode *N = DeadNodes.pop_back_val();
Scott Michelcf0da6c2009-02-17 22:15:04 +0000575
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000576 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
577 DUL->NodeDeleted(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000578
Chris Lattner8927c872006-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 Gohman8e4ac9b2009-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 Lattner8927c872006-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 Lattneraba48dd2005-11-08 18:52:27 +0000592 }
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000593
Dan Gohman534c8a22009-01-19 22:39:36 +0000594 DeallocateNode(N);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000595 }
Chris Lattner9c667932005-01-07 21:09:16 +0000596}
597
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000598void SelectionDAG::RemoveDeadNode(SDNode *N){
Dan Gohman17059682008-07-17 19:10:17 +0000599 SmallVector<SDNode*, 16> DeadNodes(1, N);
Eli Friedmanf2a9bd42011-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 Olesenbeb94692012-04-20 22:08:46 +0000606 RemoveDeadNodes(DeadNodes);
Evan Chenga731cb62006-10-12 20:34:05 +0000607}
608
Chris Lattnerc738d002005-08-29 21:59:31 +0000609void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc738d002005-08-29 21:59:31 +0000610 // First take this out of the appropriate CSE map.
611 RemoveNodeFromCSEMaps(N);
612
Scott Michelcf0da6c2009-02-17 22:15:04 +0000613 // Finally, remove uses due to operands of this node, remove from the
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000614 // AllNodes list, and delete the node.
615 DeleteNodeNotInCSEMaps(N);
616}
617
618void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane7b0dde2009-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 Gohman534c8a22009-01-19 22:39:36 +0000621
Dan Gohman86aa16a2008-09-30 18:30:35 +0000622 // Drop all of the operands and decrement used node's use counts.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000623 N->DropOperands();
Bill Wendling8ec2a4a2008-10-19 20:51:12 +0000624
Dan Gohman534c8a22009-01-19 22:39:36 +0000625 DeallocateNode(N);
626}
627
628void SelectionDAG::DeallocateNode(SDNode *N) {
629 if (N->OperandsNeedDelete)
Chris Lattnerf17b4222007-02-04 07:28:00 +0000630 delete[] N->OperandList;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000631
Dan Gohman534c8a22009-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 Gohman2e834902008-08-26 01:44:34 +0000636 NodeAllocator.Deallocate(AllNodes.remove(N));
Daniel Dunbarb827e522009-12-16 20:10:05 +0000637
Evan Cheng563fe3c2010-03-25 01:38:16 +0000638 // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
Benjamin Kramere1fc29b2011-06-18 13:13:44 +0000639 ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
Evan Cheng563fe3c2010-03-25 01:38:16 +0000640 for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
641 DbgVals[i]->setIsInvalidated();
Chris Lattnerc738d002005-08-29 21:59:31 +0000642}
643
Chris Lattner19732782005-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 Gohmand3fe1742008-09-13 01:54:27 +0000648bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000649 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000650 switch (N->getOpcode()) {
Dan Gohmand3fe1742008-09-13 01:54:27 +0000651 case ISD::HANDLENODE: return false; // noop.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000652 case ISD::CONDCODE:
653 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
654 "Cond code doesn't exist!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000655 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000656 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
657 break;
Bill Wendling24c79f22008-09-16 21:48:12 +0000658 case ISD::ExternalSymbol:
659 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000660 break;
Chris Lattneraf5dbfc2009-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 Lenharth4b3932a2005-10-23 03:40:17 +0000666 break;
Chris Lattneraf5dbfc2009-06-25 18:45:50 +0000667 }
Duncan Sandsd42c8122007-10-17 13:49:58 +0000668 case ISD::VALUETYPE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000669 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +0000670 if (VT.isExtended()) {
Duncan Sandsd42c8122007-10-17 13:49:58 +0000671 Erased = ExtendedValueTypeNodes.erase(VT);
672 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000673 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
674 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000675 }
Chris Lattner0b6ba902005-07-10 00:07:11 +0000676 break;
Duncan Sandsd42c8122007-10-17 13:49:58 +0000677 }
Chris Lattner9c667932005-01-07 21:09:16 +0000678 default:
Chris Lattnerfcb16472006-08-11 18:38:11 +0000679 // Remove it from the CSE Map.
Duncan Sandsd2e70b52010-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 Lattnerfcb16472006-08-11 18:38:11 +0000682 Erased = CSEMap.RemoveNode(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000683 break;
684 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000685#ifndef NDEBUG
Scott Michelcf0da6c2009-02-17 22:15:04 +0000686 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner1e89e362005-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 Lattner3e5fbd72010-12-21 02:38:05 +0000689 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
Duncan Sands835bdca2008-10-27 15:30:53 +0000690 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmana7644dd2007-06-19 14:13:56 +0000691 N->dump(this);
David Greened93137d2010-01-05 01:24:36 +0000692 dbgs() << "\n";
Torok Edwinfbcc6632009-07-14 16:55:14 +0000693 llvm_unreachable("Node is not in map!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000694 }
695#endif
Dan Gohmand3fe1742008-09-13 01:54:27 +0000696 return Erased;
Chris Lattner9c667932005-01-07 21:09:16 +0000697}
698
Dan Gohmanf1d38be2009-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 Lattnerab0de9d2005-08-17 19:00:20 +0000703///
Dan Gohmanf1d38be2009-01-25 16:29:12 +0000704void
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000705SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
Dan Gohmanf1d38be2009-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 Olesenbeb94692012-04-20 22:08:46 +0000714 ReplaceAllUsesWith(N, Existing);
Evan Cheng34ef1db2008-07-08 20:06:39 +0000715
Jakob Stoklund Olesenbeb94692012-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 Gohmanf1d38be2009-01-25 16:29:12 +0000719 DeleteNodeNotInCSEMaps(N);
720 return;
721 }
722 }
Evan Cheng34ef1db2008-07-08 20:06:39 +0000723
Jakob Stoklund Olesenbeb94692012-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 Lattnerab0de9d2005-08-17 19:00:20 +0000727}
728
Chris Lattnerf34156e2006-01-28 09:32:45 +0000729/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000730/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-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 Gohman2ce6f2a2008-07-27 21:46:04 +0000733SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000734 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000735 if (doNotCSE(N))
736 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000737
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000738 SDValue Ops[] = { Op };
Jim Laskeyf576b422006-10-27 23:46:08 +0000739 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000740 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands835bdca2008-10-27 15:30:53 +0000741 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000742 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000743 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000744}
745
746/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000747/// were replaced with those specified. If this node is never memoized,
Chris Lattnerf34156e2006-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 Michelcf0da6c2009-02-17 22:15:04 +0000750SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000751 SDValue Op1, SDValue Op2,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000752 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000753 if (doNotCSE(N))
754 return 0;
755
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000756 SDValue Ops[] = { Op1, Op2 };
Jim Laskeyf576b422006-10-27 23:46:08 +0000757 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +0000758 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands835bdca2008-10-27 15:30:53 +0000759 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000760 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000761 return Node;
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000762}
763
764
765/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelcf0da6c2009-02-17 22:15:04 +0000766/// were replaced with those specified. If this node is never memoized,
Chris Lattner1ee75ce2006-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 Michelcf0da6c2009-02-17 22:15:04 +0000769SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000770 const SDValue *Ops,unsigned NumOps,
Chris Lattner1ee75ce2006-08-07 23:03:03 +0000771 void *&InsertPos) {
Duncan Sands835bdca2008-10-27 15:30:53 +0000772 if (doNotCSE(N))
773 return 0;
Evan Cheng34ef1db2008-07-08 20:06:39 +0000774
Jim Laskeyf576b422006-10-27 23:46:08 +0000775 FoldingSetNodeID ID;
Dan Gohman92a7f3a2007-06-04 15:49:41 +0000776 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands835bdca2008-10-27 15:30:53 +0000777 AddNodeIDCustom(ID, N);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000778 SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Daniel Dunbarb827e522009-12-16 20:10:05 +0000779 return Node;
Chris Lattnerf34156e2006-01-28 09:32:45 +0000780}
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000781
Benjamin Kramerf6fb58a2010-11-20 15:53:24 +0000782#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000783/// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
784static void VerifyNodeCommon(SDNode *N) {
Duncan Sandsb0e39382008-07-21 10:20:31 +0000785 switch (N->getOpcode()) {
786 default:
787 break;
Duncan Sands17e678b2008-10-29 14:22:20 +0000788 case ISD::BUILD_PAIR: {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000789 EVT VT = N->getValueType(0);
Duncan Sands17e678b2008-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 Sandsb0e39382008-07-21 10:20:31 +0000802 case ISD::BUILD_VECTOR: {
Duncan Sands17e678b2008-10-29 14:22:20 +0000803 assert(N->getNumValues() == 1 && "Too many results!");
804 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsb0e39382008-07-21 10:20:31 +0000805 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands17e678b2008-10-29 14:22:20 +0000806 "Wrong number of operands!");
Owen Anderson53aa7a92009-08-10 22:56:29 +0000807 EVT EltVT = N->getValueType(0).getVectorElementType();
Eli Friedmanb7910b72011-09-09 21:04:06 +0000808 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Rafael Espindolab93db662009-04-24 12:40:33 +0000809 assert((I->getValueType() == EltVT ||
Nate Begeman8d6d4b92009-04-27 18:41:29 +0000810 (EltVT.isInteger() && I->getValueType().isInteger() &&
811 EltVT.bitsLE(I->getValueType()))) &&
812 "Wrong operand type!");
Eli Friedmanb7910b72011-09-09 21:04:06 +0000813 assert(I->getValueType() == N->getOperand(0).getValueType() &&
814 "Operands must all have the same type");
815 }
Duncan Sandsb0e39382008-07-21 10:20:31 +0000816 break;
817 }
818 }
819}
820
Duncan Sands7c601de2010-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 Kramerf6fb58a2010-11-20 15:53:24 +0000857#endif // NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +0000858
Owen Anderson53aa7a92009-08-10 22:56:29 +0000859/// getEVTAlignment - Compute the default alignment value for the
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000860/// given type.
861///
Owen Anderson53aa7a92009-08-10 22:56:29 +0000862unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Chris Lattner229907c2011-07-18 04:54:35 +0000863 Type *Ty = VT == MVT::iPTR ?
Owen Anderson55f1c092009-08-13 21:58:54 +0000864 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson117c9e82009-08-12 00:36:31 +0000865 VT.getTypeForEVT(*getContext());
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000866
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000867 return TM.getTargetLowering()->getDataLayout()->getABITypeAlignment(Ty);
Dan Gohmane8d8d2e2008-07-08 23:46:32 +0000868}
Chris Lattner9c667932005-01-07 21:09:16 +0000869
Dale Johannesen8ba713212009-02-07 02:15:05 +0000870// EntryNode could meaningfully have debug info if we can find it...
Devang Patel7bbc1e52011-12-15 18:21:18 +0000871SelectionDAG::SelectionDAG(const TargetMachine &tm, CodeGenOpt::Level OL)
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000872 : TM(tm), TSI(*tm.getSelectionDAGInfo()), TTI(0), OptLevel(OL),
873 EntryNode(ISD::EntryToken, 0, DebugLoc(), getVTList(MVT::Other)),
Andrew Tricke2431c62013-05-25 03:08:10 +0000874 Root(getEntryNode()), UpdateListeners(0) {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000875 AllNodes.push_back(&EntryNode);
Dale Johannesen49de0602010-03-10 22:13:47 +0000876 DbgInfo = new SDDbgInfo();
Dan Gohmanac37f9a2008-08-23 00:50:30 +0000877}
878
Chandler Carruth42e96112013-01-05 12:32:17 +0000879void SelectionDAG::init(MachineFunction &mf, const TargetTransformInfo *tti) {
Dan Gohmane1a9a782008-08-27 23:52:12 +0000880 MF = &mf;
Chandler Carruth42e96112013-01-05 12:32:17 +0000881 TTI = tti;
Eric Christopherdfda92b2009-08-22 00:40:45 +0000882 Context = &mf.getFunction()->getContext();
Dan Gohmane1a9a782008-08-27 23:52:12 +0000883}
884
Chris Lattner600d3082003-08-11 14:57:33 +0000885SelectionDAG::~SelectionDAG() {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000886 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000887 allnodes_clear();
Dale Johannesen49de0602010-03-10 22:13:47 +0000888 delete DbgInfo;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000889}
890
891void SelectionDAG::allnodes_clear() {
Dan Gohman2e834902008-08-26 01:44:34 +0000892 assert(&*AllNodes.begin() == &EntryNode);
893 AllNodes.remove(AllNodes.begin());
Dan Gohman534c8a22009-01-19 22:39:36 +0000894 while (!AllNodes.empty())
895 DeallocateNode(AllNodes.begin());
Chris Lattner600d3082003-08-11 14:57:33 +0000896}
897
Dan Gohmane1a9a782008-08-27 23:52:12 +0000898void SelectionDAG::clear() {
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000899 allnodes_clear();
900 OperandAllocator.Reset();
901 CSEMap.clear();
902
903 ExtendedValueTypeNodes.clear();
Bill Wendling24c79f22008-09-16 21:48:12 +0000904 ExternalSymbols.clear();
905 TargetExternalSymbols.clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000906 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
907 static_cast<CondCodeSDNode*>(0));
908 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
909 static_cast<SDNode*>(0));
910
Dan Gohman8e4ac9b2009-01-26 04:35:06 +0000911 EntryNode.UseList = 0;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000912 AllNodes.push_back(&EntryNode);
913 Root = getEntryNode();
Evan Cheng4d1aa2a2010-03-29 20:48:30 +0000914 DbgInfo->clear();
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000915}
916
Andrew Trickef9de2a2013-05-25 02:42:55 +0000917SDValue SelectionDAG::getAnyExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Nadav Rotem38b3b832011-09-27 11:16:47 +0000918 return VT.bitsGT(Op.getValueType()) ?
919 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
920 getNode(ISD::TRUNCATE, DL, VT, Op);
921}
922
Andrew Trickef9de2a2013-05-25 02:42:55 +0000923SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000924 return VT.bitsGT(Op.getValueType()) ?
925 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
926 getNode(ISD::TRUNCATE, DL, VT, Op);
927}
928
Andrew Trickef9de2a2013-05-25 02:42:55 +0000929SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, SDLoc DL, EVT VT) {
Duncan Sands18a956c2009-10-13 21:04:12 +0000930 return VT.bitsGT(Op.getValueType()) ?
931 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
932 getNode(ISD::TRUNCATE, DL, VT, Op);
933}
934
Andrew Trickef9de2a2013-05-25 02:42:55 +0000935SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, SDLoc DL, EVT VT) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000936 assert(!VT.isVector() &&
937 "getZeroExtendInReg should use the vector element type instead of "
938 "the vector type!");
Bill Wendlingc4093182009-01-30 22:23:15 +0000939 if (Op.getValueType() == VT) return Op;
Dan Gohman1d459e42009-12-11 21:31:27 +0000940 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
941 APInt Imm = APInt::getLowBitsSet(BitWidth,
Bill Wendlingc4093182009-01-30 22:23:15 +0000942 VT.getSizeInBits());
943 return getNode(ISD::AND, DL, Op.getValueType(), Op,
944 getConstant(Imm, Op.getValueType()));
945}
946
Bob Wilsonc5890052009-01-22 17:39:32 +0000947/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
948///
Andrew Trickef9de2a2013-05-25 02:42:55 +0000949SDValue SelectionDAG::getNOT(SDLoc DL, SDValue Val, EVT VT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000950 EVT EltVT = VT.getScalarType();
Dan Gohmane014b692009-04-20 22:51:43 +0000951 SDValue NegOne =
952 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendlingcab9a2e2009-01-30 22:11:22 +0000953 return getNode(ISD::XOR, DL, VT, Val, NegOne);
954}
955
Owen Anderson53aa7a92009-08-10 22:56:29 +0000956SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000957 EVT EltVT = VT.getScalarType();
Dan Gohmanfb58faf2009-01-27 20:39:34 +0000958 assert((EltVT.getSizeInBits() >= 64 ||
959 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
960 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands13237ac2008-06-06 12:08:01 +0000961 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman65f63eb2008-02-08 22:59:30 +0000962}
963
Owen Anderson53aa7a92009-08-10 22:56:29 +0000964SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000965 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohmanec270fb2008-09-12 18:08:03 +0000966}
967
Owen Anderson53aa7a92009-08-10 22:56:29 +0000968SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands13237ac2008-06-06 12:08:01 +0000969 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman7a7742c2007-12-12 22:21:26 +0000970
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +0000971 EVT EltVT = VT.getScalarType();
Duncan Sandsf2641e12011-09-06 19:07:46 +0000972 const ConstantInt *Elt = &Val;
Chris Lattner3f16b202006-08-11 21:01:22 +0000973
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000974 const TargetLowering *TLI = TM.getTargetLowering();
975
Duncan Sandsf2641e12011-09-06 19:07:46 +0000976 // In some cases the vector type is legal but the element type is illegal and
977 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
978 // inserted value (the type does not need to match the vector element type).
979 // Any extra bits introduced will be truncated away.
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000980 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +0000981 TargetLowering::TypePromoteInteger) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000982 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000983 APInt NewVal = Elt->getValue().zext(EltVT.getSizeInBits());
984 Elt = ConstantInt::get(*getContext(), NewVal);
985 }
986
987 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
988 "APInt size does not match type size!");
Chris Lattner3f16b202006-08-11 21:01:22 +0000989 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskeyf576b422006-10-27 23:46:08 +0000990 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +0000991 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000992 ID.AddPointer(Elt);
Chris Lattner3f16b202006-08-11 21:01:22 +0000993 void *IP = 0;
Dan Gohman7a7742c2007-12-12 22:21:26 +0000994 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +0000995 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +0000996 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000997 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +0000998
Dan Gohman7a7742c2007-12-12 22:21:26 +0000999 if (!N) {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001000 N = new (NodeAllocator) ConstantSDNode(isT, Elt, EltVT);
Dan Gohman7a7742c2007-12-12 22:21:26 +00001001 CSEMap.InsertNode(N, IP);
1002 AllNodes.push_back(N);
1003 }
1004
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001005 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001006 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001007 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001008 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001009 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohman7a7742c2007-12-12 22:21:26 +00001010 }
1011 return Result;
Chris Lattner600d3082003-08-11 14:57:33 +00001012}
1013
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001014SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001015 return getConstant(Val, TM.getTargetLowering()->getPointerTy(), isTarget);
Chris Lattner72733e52008-01-17 07:00:52 +00001016}
1017
1018
Owen Anderson53aa7a92009-08-10 22:56:29 +00001019SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001020 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001021}
1022
Owen Anderson53aa7a92009-08-10 22:56:29 +00001023SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands13237ac2008-06-06 12:08:01 +00001024 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00001025
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001026 EVT EltVT = VT.getScalarType();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001027
Chris Lattner381dddc2005-02-17 20:17:32 +00001028 // Do the map lookup using the actual bit pattern for the floating point
1029 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1030 // we don't have issues with SNANs.
Chris Lattner0c2e5412006-08-11 21:55:30 +00001031 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskeyf576b422006-10-27 23:46:08 +00001032 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001033 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanec270fb2008-09-12 18:08:03 +00001034 ID.AddPointer(&V);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001035 void *IP = 0;
Evan Cheng9458e6a2007-06-29 21:36:04 +00001036 SDNode *N = NULL;
Bill Wendling022d18f2009-12-18 23:32:53 +00001037 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands13237ac2008-06-06 12:08:01 +00001038 if (!VT.isVector())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001039 return SDValue(N, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001040
Evan Cheng9458e6a2007-06-29 21:36:04 +00001041 if (!N) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001042 N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Cheng9458e6a2007-06-29 21:36:04 +00001043 CSEMap.InsertNode(N, IP);
1044 AllNodes.push_back(N);
1045 }
1046
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001047 SDValue Result(N, 0);
Duncan Sands13237ac2008-06-06 12:08:01 +00001048 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001049 SmallVector<SDValue, 8> Ops;
Duncan Sands13237ac2008-06-06 12:08:01 +00001050 Ops.assign(VT.getVectorNumElements(), Result);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001051 // FIXME SDLoc info might be appropriate here
1052 Result = getNode(ISD::BUILD_VECTOR, SDLoc(), VT, &Ops[0], Ops.size());
Dan Gohmana8665142007-06-25 16:23:39 +00001053 }
1054 return Result;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001055}
1056
Owen Anderson53aa7a92009-08-10 22:56:29 +00001057SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00001058 EVT EltVT = VT.getScalarType();
Owen Anderson9f944592009-08-11 20:47:22 +00001059 if (EltVT==MVT::f32)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001060 return getConstantFP(APFloat((float)Val), VT, isTarget);
Dale Johannesen51c16952010-05-07 21:35:53 +00001061 else if (EltVT==MVT::f64)
Dale Johannesend246b2c2007-08-30 00:23:21 +00001062 return getConstantFP(APFloat(Val), VT, isTarget);
Hal Finkel6dbdd432012-12-30 19:03:32 +00001063 else if (EltVT==MVT::f80 || EltVT==MVT::f128 || EltVT==MVT::ppcf128 ||
1064 EltVT==MVT::f16) {
Dale Johannesen51c16952010-05-07 21:35:53 +00001065 bool ignored;
1066 APFloat apf = APFloat(Val);
Tim Northover29178a32013-01-22 09:46:31 +00001067 apf.convert(EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
Dale Johannesen51c16952010-05-07 21:35:53 +00001068 &ignored);
1069 return getConstantFP(apf, VT, isTarget);
Craig Topperee4dab52012-02-05 08:31:47 +00001070 } else
1071 llvm_unreachable("Unsupported type in getConstantFP");
Dale Johannesend246b2c2007-08-30 00:23:21 +00001072}
1073
Andrew Trickef9de2a2013-05-25 02:42:55 +00001074SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00001075 EVT VT, int64_t Offset,
Chris Lattner8e34f982009-06-25 21:21:14 +00001076 bool isTargetGA,
1077 unsigned char TargetFlags) {
1078 assert((TargetFlags == 0 || isTargetGA) &&
1079 "Cannot set target flags on target-independent globals");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001080
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001081 // Truncate (with sign-extension) the offset value to the pointer size.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001082 unsigned BitWidth = TM.getTargetLowering()->getPointerTy().getSizeInBits();
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001083 if (BitWidth < 64)
Richard Smith228e6d42012-08-24 23:29:28 +00001084 Offset = SignExtend64(Offset, BitWidth);
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001085
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001086 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1087 if (!GVar) {
Anton Korobeynikov2fa75182008-03-22 07:53:40 +00001088 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001089 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov1a114042008-09-09 20:05:04 +00001090 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001091 }
1092
Chris Lattner8e34f982009-06-25 21:21:14 +00001093 unsigned Opc;
Lauro Ramos Venancio25188892007-04-20 21:38:10 +00001094 if (GVar && GVar->isThreadLocal())
1095 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
1096 else
1097 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikove8fa50f2008-03-11 22:38:53 +00001098
Jim Laskeyf576b422006-10-27 23:46:08 +00001099 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001100 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001101 ID.AddPointer(GV);
1102 ID.AddInteger(Offset);
Chris Lattner8e34f982009-06-25 21:21:14 +00001103 ID.AddInteger(TargetFlags);
Pete Cooper91244262012-07-30 20:23:19 +00001104 ID.AddInteger(GV->getType()->getAddressSpace());
Chris Lattner3f16b202006-08-11 21:01:22 +00001105 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001106 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman3a113ec2009-01-25 16:21:38 +00001107 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001108
Andrew Trickef9de2a2013-05-25 02:42:55 +00001109 SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL.getIROrder(),
1110 DL.getDebugLoc(), GV, VT,
Dan Gohman01c65a22010-03-18 18:49:47 +00001111 Offset, TargetFlags);
Chris Lattner3f16b202006-08-11 21:01:22 +00001112 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001113 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001114 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001115}
1116
Owen Anderson53aa7a92009-08-10 22:56:29 +00001117SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattner0c2e5412006-08-11 21:55:30 +00001118 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskeyf576b422006-10-27 23:46:08 +00001119 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001120 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001121 ID.AddInteger(FI);
1122 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001123 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001124 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001125
Dan Gohman01c65a22010-03-18 18:49:47 +00001126 SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001127 CSEMap.InsertNode(N, IP);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001128 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001130}
1131
Owen Anderson53aa7a92009-08-10 22:56:29 +00001132SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001133 unsigned char TargetFlags) {
1134 assert((TargetFlags == 0 || isTarget) &&
1135 "Cannot set target flags on target-independent jump tables");
Chris Lattner0c2e5412006-08-11 21:55:30 +00001136 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskeyf576b422006-10-27 23:46:08 +00001137 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001138 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001139 ID.AddInteger(JTI);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001140 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001141 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001142 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001143 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001144
Dan Gohman01c65a22010-03-18 18:49:47 +00001145 SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
1146 TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001147 CSEMap.InsertNode(N, IP);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001148 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001149 return SDValue(N, 0);
Nate Begeman4ca2ea52006-04-22 18:53:45 +00001150}
1151
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001152SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001153 unsigned Alignment, int Offset,
Eric Christopherdfda92b2009-08-22 00:40:45 +00001154 bool isTarget,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001155 unsigned char TargetFlags) {
1156 assert((TargetFlags == 0 || isTarget) &&
1157 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001158 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001159 Alignment =
1160 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Chris Lattner0c2e5412006-08-11 21:55:30 +00001161 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001162 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001163 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001164 ID.AddInteger(Alignment);
1165 ID.AddInteger(Offset);
Chris Lattner8e372832006-08-14 20:12:44 +00001166 ID.AddPointer(C);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001167 ID.AddInteger(TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001168 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001169 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001170 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001171
Dan Gohman01c65a22010-03-18 18:49:47 +00001172 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1173 Alignment, TargetFlags);
Chris Lattner0c2e5412006-08-11 21:55:30 +00001174 CSEMap.InsertNode(N, IP);
Chris Lattner407c6412005-08-25 05:03:06 +00001175 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001176 return SDValue(N, 0);
Chris Lattner407c6412005-08-25 05:03:06 +00001177}
1178
Chris Lattner061a1ea2005-01-07 07:46:32 +00001179
Owen Anderson53aa7a92009-08-10 22:56:29 +00001180SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001181 unsigned Alignment, int Offset,
Chris Lattnerb3586b62009-06-25 21:35:31 +00001182 bool isTarget,
1183 unsigned char TargetFlags) {
1184 assert((TargetFlags == 0 || isTarget) &&
1185 "Cannot set target flags on target-independent globals");
Dan Gohman64d6c6f2008-09-16 22:05:41 +00001186 if (Alignment == 0)
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001187 Alignment =
1188 TM.getTargetLowering()->getDataLayout()->getPrefTypeAlignment(C->getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001189 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskeyf576b422006-10-27 23:46:08 +00001190 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001191 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001192 ID.AddInteger(Alignment);
1193 ID.AddInteger(Offset);
Jim Grosbachaf136f72011-09-27 20:59:33 +00001194 C->addSelectionDAGCSEId(ID);
Chris Lattnerb3586b62009-06-25 21:35:31 +00001195 ID.AddInteger(TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001196 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001197 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001198 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001199
Dan Gohman01c65a22010-03-18 18:49:47 +00001200 SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
1201 Alignment, TargetFlags);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001202 CSEMap.InsertNode(N, IP);
1203 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001204 return SDValue(N, 0);
Evan Cheng45fe3bc2006-09-12 21:00:35 +00001205}
1206
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +00001207SDValue SelectionDAG::getTargetIndex(int Index, EVT VT, int64_t Offset,
1208 unsigned char TargetFlags) {
1209 FoldingSetNodeID ID;
1210 AddNodeIDNode(ID, ISD::TargetIndex, getVTList(VT), 0, 0);
1211 ID.AddInteger(Index);
1212 ID.AddInteger(Offset);
1213 ID.AddInteger(TargetFlags);
1214 void *IP = 0;
1215 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1216 return SDValue(E, 0);
1217
1218 SDNode *N = new (NodeAllocator) TargetIndexSDNode(Index, VT, Offset,
1219 TargetFlags);
1220 CSEMap.InsertNode(N, IP);
1221 AllNodes.push_back(N);
1222 return SDValue(N, 0);
1223}
1224
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001225SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001226 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001227 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001228 ID.AddPointer(MBB);
1229 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001230 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001231 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001232
Dan Gohman01c65a22010-03-18 18:49:47 +00001233 SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
Chris Lattner3f16b202006-08-11 21:01:22 +00001234 CSEMap.InsertNode(N, IP);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001235 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001236 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001237}
1238
Owen Anderson53aa7a92009-08-10 22:56:29 +00001239SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson9f944592009-08-11 20:47:22 +00001240 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1241 ValueTypeNodes.size())
1242 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001243
Duncan Sands13237ac2008-06-06 12:08:01 +00001244 SDNode *&N = VT.isExtended() ?
Owen Anderson9f944592009-08-11 20:47:22 +00001245 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd42c8122007-10-17 13:49:58 +00001246
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001247 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001248 N = new (NodeAllocator) VTSDNode(VT);
Duncan Sandsd42c8122007-10-17 13:49:58 +00001249 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001250 return SDValue(N, 0);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001251}
1252
Owen Anderson53aa7a92009-08-10 22:56:29 +00001253SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling24c79f22008-09-16 21:48:12 +00001254 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001255 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001256 N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001257 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001258 return SDValue(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001259}
1260
Owen Anderson53aa7a92009-08-10 22:56:29 +00001261SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattneraf5dbfc2009-06-25 18:45:50 +00001262 unsigned char TargetFlags) {
1263 SDNode *&N =
1264 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1265 TargetFlags)];
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001266 if (N) return SDValue(N, 0);
Dan Gohman01c65a22010-03-18 18:49:47 +00001267 N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001268 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001269 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001270}
1271
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001272SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001273 if ((unsigned)Cond >= CondCodeNodes.size())
1274 CondCodeNodes.resize(Cond+1);
Duncan Sands13237ac2008-06-06 12:08:01 +00001275
Chris Lattner14e060f2005-08-09 20:40:02 +00001276 if (CondCodeNodes[Cond] == 0) {
Dan Gohman01c65a22010-03-18 18:49:47 +00001277 CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00001278 CondCodeNodes[Cond] = N;
1279 AllNodes.push_back(N);
Chris Lattner14e060f2005-08-09 20:40:02 +00001280 }
Bill Wendling022d18f2009-12-18 23:32:53 +00001281
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001282 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001283}
1284
Nate Begeman5f829d82009-04-29 05:20:52 +00001285// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1286// the shuffle mask M that point at N1 to point at N2, and indices that point
1287// N2 to point at N1.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001288static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1289 std::swap(N1, N2);
1290 int NElts = M.size();
1291 for (int i = 0; i != NElts; ++i) {
1292 if (M[i] >= NElts)
1293 M[i] -= NElts;
1294 else if (M[i] >= 0)
1295 M[i] += NElts;
1296 }
1297}
1298
Andrew Trickef9de2a2013-05-25 02:42:55 +00001299SDValue SelectionDAG::getVectorShuffle(EVT VT, SDLoc dl, SDValue N1,
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001300 SDValue N2, const int *Mask) {
1301 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopherdfda92b2009-08-22 00:40:45 +00001302 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001303 "Vector Shuffle VTs must be a vectors");
1304 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1305 && "Vector Shuffle VTs must have same element type");
1306
1307 // Canonicalize shuffle undef, undef -> undef
1308 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman6b041362009-07-09 00:46:33 +00001309 return getUNDEF(VT);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001310
Eric Christopherdfda92b2009-08-22 00:40:45 +00001311 // Validate that all indices in Mask are within the range of the elements
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001312 // input to the shuffle.
Nate Begeman5f829d82009-04-29 05:20:52 +00001313 unsigned NElts = VT.getVectorNumElements();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001314 SmallVector<int, 8> MaskVec;
Nate Begeman5f829d82009-04-29 05:20:52 +00001315 for (unsigned i = 0; i != NElts; ++i) {
1316 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001317 MaskVec.push_back(Mask[i]);
1318 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001319
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001320 // Canonicalize shuffle v, v -> v, undef
1321 if (N1 == N2) {
1322 N2 = getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001323 for (unsigned i = 0; i != NElts; ++i)
1324 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001325 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001326
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001327 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1328 if (N1.getOpcode() == ISD::UNDEF)
1329 commuteShuffle(N1, N2, MaskVec);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001330
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001331 // Canonicalize all index into lhs, -> shuffle lhs, undef
1332 // Canonicalize all index into rhs, -> shuffle rhs, undef
1333 bool AllLHS = true, AllRHS = true;
1334 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5f829d82009-04-29 05:20:52 +00001335 for (unsigned i = 0; i != NElts; ++i) {
1336 if (MaskVec[i] >= (int)NElts) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001337 if (N2Undef)
1338 MaskVec[i] = -1;
1339 else
1340 AllLHS = false;
1341 } else if (MaskVec[i] >= 0) {
1342 AllRHS = false;
1343 }
1344 }
1345 if (AllLHS && AllRHS)
1346 return getUNDEF(VT);
Nate Begeman5f829d82009-04-29 05:20:52 +00001347 if (AllLHS && !N2Undef)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001348 N2 = getUNDEF(VT);
1349 if (AllRHS) {
1350 N1 = getUNDEF(VT);
1351 commuteShuffle(N1, N2, MaskVec);
1352 }
Eric Christopherdfda92b2009-08-22 00:40:45 +00001353
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001354 // If Identity shuffle, or all shuffle in to undef, return that node.
1355 bool AllUndef = true;
1356 bool Identity = true;
Nate Begeman5f829d82009-04-29 05:20:52 +00001357 for (unsigned i = 0; i != NElts; ++i) {
1358 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001359 if (MaskVec[i] >= 0) AllUndef = false;
1360 }
Dan Gohman6b041362009-07-09 00:46:33 +00001361 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001362 return N1;
1363 if (AllUndef)
1364 return getUNDEF(VT);
1365
1366 FoldingSetNodeID ID;
1367 SDValue Ops[2] = { N1, N2 };
1368 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5f829d82009-04-29 05:20:52 +00001369 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001370 ID.AddInteger(MaskVec[i]);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001371
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001372 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001373 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001374 return SDValue(E, 0);
Eric Christopherdfda92b2009-08-22 00:40:45 +00001375
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001376 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1377 // SDNode doesn't have access to it. This memory will be "leaked" when
1378 // the node is deallocated, but recovered when the NodeAllocator is released.
1379 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1380 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopherdfda92b2009-08-22 00:40:45 +00001381
Dan Gohman01c65a22010-03-18 18:49:47 +00001382 ShuffleVectorSDNode *N =
Andrew Trickef9de2a2013-05-25 02:42:55 +00001383 new (NodeAllocator) ShuffleVectorSDNode(VT, dl.getIROrder(), dl.getDebugLoc(), N1, N2, MaskAlloc);
Nate Begeman8d6d4b92009-04-27 18:41:29 +00001384 CSEMap.InsertNode(N, IP);
1385 AllNodes.push_back(N);
1386 return SDValue(N, 0);
1387}
1388
Andrew Trickef9de2a2013-05-25 02:42:55 +00001389SDValue SelectionDAG::getConvertRndSat(EVT VT, SDLoc dl,
Dale Johannesen3a09f552009-02-03 23:04:43 +00001390 SDValue Val, SDValue DTy,
1391 SDValue STy, SDValue Rnd, SDValue Sat,
1392 ISD::CvtCode Code) {
Mon P Wang3f0e0a62009-02-05 04:47:42 +00001393 // If the src and dest types are the same and the conversion is between
1394 // integer types of the same sign or two floats, no conversion is necessary.
1395 if (DTy == STy &&
1396 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001397 return Val;
1398
1399 FoldingSetNodeID ID;
Mon P Wangfc032ce2009-11-07 04:46:25 +00001400 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1401 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001402 void* IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001403 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen3a09f552009-02-03 23:04:43 +00001404 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001405
Andrew Trickef9de2a2013-05-25 02:42:55 +00001406 CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl.getIROrder(), dl.getDebugLoc(), Ops, 5,
Dan Gohman01c65a22010-03-18 18:49:47 +00001407 Code);
Dale Johannesen3a09f552009-02-03 23:04:43 +00001408 CSEMap.InsertNode(N, IP);
1409 AllNodes.push_back(N);
1410 return SDValue(N, 0);
1411}
1412
Owen Anderson53aa7a92009-08-10 22:56:29 +00001413SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00001414 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00001415 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001416 ID.AddInteger(RegNo);
1417 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001418 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001419 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001420
Dan Gohman01c65a22010-03-18 18:49:47 +00001421 SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
Chris Lattner3f16b202006-08-11 21:01:22 +00001422 CSEMap.InsertNode(N, IP);
1423 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001424 return SDValue(N, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001425}
1426
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +00001427SDValue SelectionDAG::getRegisterMask(const uint32_t *RegMask) {
1428 FoldingSetNodeID ID;
1429 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), 0, 0);
1430 ID.AddPointer(RegMask);
1431 void *IP = 0;
1432 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1433 return SDValue(E, 0);
1434
1435 SDNode *N = new (NodeAllocator) RegisterMaskSDNode(RegMask);
1436 CSEMap.InsertNode(N, IP);
1437 AllNodes.push_back(N);
1438 return SDValue(N, 0);
1439}
1440
Andrew Trickef9de2a2013-05-25 02:42:55 +00001441SDValue SelectionDAG::getEHLabel(SDLoc dl, SDValue Root, MCSymbol *Label) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00001442 FoldingSetNodeID ID;
1443 SDValue Ops[] = { Root };
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001444 AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
1445 ID.AddPointer(Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001446 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00001448 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001449
Andrew Trickef9de2a2013-05-25 02:42:55 +00001450 SDNode *N = new (NodeAllocator) EHLabelSDNode(dl.getIROrder(), dl.getDebugLoc(), Root, Label);
Dale Johannesen839acbb2009-01-29 00:47:48 +00001451 CSEMap.InsertNode(N, IP);
1452 AllNodes.push_back(N);
1453 return SDValue(N, 0);
1454}
1455
Chris Lattneree2fbbc2010-03-14 02:33:54 +00001456
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001457SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
Michael Liaoabb87d42012-09-12 21:43:09 +00001458 int64_t Offset,
Dan Gohman7a6611792009-11-20 23:18:13 +00001459 bool isTarget,
1460 unsigned char TargetFlags) {
Dan Gohman6c938802009-10-30 01:27:03 +00001461 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1462
1463 FoldingSetNodeID ID;
Dan Gohman7a6611792009-11-20 23:18:13 +00001464 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman6c938802009-10-30 01:27:03 +00001465 ID.AddPointer(BA);
Michael Liaoabb87d42012-09-12 21:43:09 +00001466 ID.AddInteger(Offset);
Dan Gohman7a6611792009-11-20 23:18:13 +00001467 ID.AddInteger(TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001468 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001469 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman6c938802009-10-30 01:27:03 +00001470 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00001471
Michael Liaoabb87d42012-09-12 21:43:09 +00001472 SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, Offset,
1473 TargetFlags);
Dan Gohman6c938802009-10-30 01:27:03 +00001474 CSEMap.InsertNode(N, IP);
1475 AllNodes.push_back(N);
1476 return SDValue(N, 0);
1477}
1478
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001479SDValue SelectionDAG::getSrcValue(const Value *V) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001480 assert((!V || V->getType()->isPointerTy()) &&
Chris Lattner3f16b202006-08-11 21:01:22 +00001481 "SrcValue is not a pointer?");
1482
Jim Laskeyf576b422006-10-27 23:46:08 +00001483 FoldingSetNodeID ID;
Owen Anderson9f944592009-08-11 20:47:22 +00001484 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner3f16b202006-08-11 21:01:22 +00001485 ID.AddPointer(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001486
Chris Lattner3f16b202006-08-11 21:01:22 +00001487 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00001488 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001489 return SDValue(E, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001490
Dan Gohman01c65a22010-03-18 18:49:47 +00001491 SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
Dan Gohman2d489b52008-02-06 22:27:42 +00001492 CSEMap.InsertNode(N, IP);
1493 AllNodes.push_back(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001494 return SDValue(N, 0);
Dan Gohman2d489b52008-02-06 22:27:42 +00001495}
1496
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001497/// getMDNode - Return an MDNodeSDNode which holds an MDNode.
1498SDValue SelectionDAG::getMDNode(const MDNode *MD) {
1499 FoldingSetNodeID ID;
1500 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
1501 ID.AddPointer(MD);
Wesley Peck527da1b2010-11-23 03:31:01 +00001502
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001503 void *IP = 0;
1504 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1505 return SDValue(E, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001506
Chris Lattner3b9f02a2010-04-07 05:20:54 +00001507 SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
1508 CSEMap.InsertNode(N, IP);
1509 AllNodes.push_back(N);
1510 return SDValue(N, 0);
1511}
1512
1513
Duncan Sands41826032009-01-31 15:50:11 +00001514/// getShiftAmountOperand - Return the specified value casted to
1515/// the target's desired shift amount type.
Owen Andersoncd526fa2011-03-07 18:29:47 +00001516SDValue SelectionDAG::getShiftAmountOperand(EVT LHSTy, SDValue Op) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001517 EVT OpTy = Op.getValueType();
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001518 EVT ShTy = TM.getTargetLowering()->getShiftAmountTy(LHSTy);
Duncan Sands41826032009-01-31 15:50:11 +00001519 if (OpTy == ShTy || OpTy.isVector()) return Op;
1520
1521 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001522 return getNode(Opcode, SDLoc(Op), ShTy, Op);
Duncan Sands41826032009-01-31 15:50:11 +00001523}
1524
Chris Lattner9eb7a822007-10-15 17:47:20 +00001525/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1526/// specified value type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001527SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner9eb7a822007-10-15 17:47:20 +00001528 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman203d53e2009-09-23 21:07:02 +00001529 unsigned ByteSize = VT.getStoreSize();
Chris Lattner229907c2011-07-18 04:54:35 +00001530 Type *Ty = VT.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001531 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang5c755ff2008-07-05 20:40:31 +00001532 unsigned StackAlign =
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001533 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001534
David Greene1fbe0542009-11-12 20:49:22 +00001535 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001536 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Chris Lattner9eb7a822007-10-15 17:47:20 +00001537}
1538
Duncan Sands445071c2008-12-09 21:33:20 +00001539/// CreateStackTemporary - Create a stack temporary suitable for holding
1540/// either of the specified value types.
Owen Anderson53aa7a92009-08-10 22:56:29 +00001541SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands445071c2008-12-09 21:33:20 +00001542 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1543 VT2.getStoreSizeInBits())/8;
Chris Lattner229907c2011-07-18 04:54:35 +00001544 Type *Ty1 = VT1.getTypeForEVT(*getContext());
1545 Type *Ty2 = VT2.getTypeForEVT(*getContext());
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001546 const TargetLowering *TLI = TM.getTargetLowering();
1547 const DataLayout *TD = TLI->getDataLayout();
Duncan Sands445071c2008-12-09 21:33:20 +00001548 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1549 TD->getPrefTypeAlignment(Ty2));
1550
1551 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene1fbe0542009-11-12 20:49:22 +00001552 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001553 return getFrameIndex(FrameIdx, TLI->getPointerTy());
Duncan Sands445071c2008-12-09 21:33:20 +00001554}
1555
Owen Anderson53aa7a92009-08-10 22:56:29 +00001556SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001557 SDValue N2, ISD::CondCode Cond, SDLoc dl) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001558 // These setcc operations always fold.
1559 switch (Cond) {
1560 default: break;
1561 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001562 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001563 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001564 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001565
Chris Lattner393d96a2006-04-27 05:01:07 +00001566 case ISD::SETOEQ:
1567 case ISD::SETOGT:
1568 case ISD::SETOGE:
1569 case ISD::SETOLT:
1570 case ISD::SETOLE:
1571 case ISD::SETONE:
1572 case ISD::SETO:
1573 case ISD::SETUO:
1574 case ISD::SETUEQ:
1575 case ISD::SETUNE:
Duncan Sands13237ac2008-06-06 12:08:01 +00001576 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattner393d96a2006-04-27 05:01:07 +00001577 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001578 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001579
Gabor Greiff304a7a2008-08-28 21:40:38 +00001580 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001581 const APInt &C2 = N2C->getAPIntValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00001582 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001583 const APInt &C1 = N1C->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001584
Chris Lattner061a1ea2005-01-07 07:46:32 +00001585 switch (Cond) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001586 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001587 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1588 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohmanbd2fa562008-02-29 01:47:35 +00001589 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1590 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1591 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1592 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1593 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1594 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1595 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1596 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001597 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001598 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +00001599 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001600 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1601 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001602 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001603 switch (Cond) {
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001604 default: break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001605 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001606 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001607 // fall through
1608 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001609 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001610 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001611 // fall through
1612 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001613 R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001614 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001615 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001616 // fall through
1617 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001618 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001619 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001620 // fall through
1621 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001622 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001623 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001624 // fall through
1625 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001626 R==APFloat::cmpEqual, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001627 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen84935752009-02-06 23:05:02 +00001628 return getUNDEF(VT);
Dale Johannesenda7469f2007-08-31 17:03:33 +00001629 // fall through
1630 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesen3cf889f2007-08-31 04:03:46 +00001631 R==APFloat::cmpEqual, VT);
1632 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1633 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1634 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1635 R==APFloat::cmpEqual, VT);
1636 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1637 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1638 R==APFloat::cmpLessThan, VT);
1639 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1640 R==APFloat::cmpUnordered, VT);
1641 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1642 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001643 }
1644 } else {
1645 // Ensure that the constant occurs on the RHS.
Dale Johannesenf1163e92009-02-03 00:47:48 +00001646 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001647 }
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00001648 }
1649
Chris Lattnerd47675e2005-08-09 20:20:18 +00001650 // Could not fold it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001651 return SDValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001652}
1653
Dan Gohman1f372ed2008-02-25 21:11:39 +00001654/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1655/// use this predicate to simplify operations downstream.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001656bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerf3989ab2009-07-07 23:28:46 +00001657 // This predicate is not safe for vector operations.
1658 if (Op.getValueType().isVector())
1659 return false;
Eric Christopherdfda92b2009-08-22 00:40:45 +00001660
Dan Gohman1d459e42009-12-11 21:31:27 +00001661 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00001662 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1663}
1664
Dan Gohman309d3d52007-06-22 14:59:07 +00001665/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1666/// this predicate to simplify operations downstream. Mask is known to be zero
1667/// for bits that V cannot have.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001668bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohman309d3d52007-06-22 14:59:07 +00001669 unsigned Depth) const {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001670 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001671 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001672 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001673 return (KnownZero & Mask) == Mask;
1674}
1675
1676/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1677/// known to be either zero or one and return them in the KnownZero/KnownOne
1678/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1679/// processing.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001680void SelectionDAG::ComputeMaskedBits(SDValue Op, APInt &KnownZero,
1681 APInt &KnownOne, unsigned Depth) const {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001682 const TargetLowering *TLI = TM.getTargetLowering();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001683 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
Dan Gohman7e22a5d2008-02-13 23:13:32 +00001684
Dan Gohmanf990faf2008-02-13 00:35:47 +00001685 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001686 if (Depth == 6)
Dan Gohman309d3d52007-06-22 14:59:07 +00001687 return; // Limit search depth.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001688
Dan Gohmanf990faf2008-02-13 00:35:47 +00001689 APInt KnownZero2, KnownOne2;
Dan Gohman309d3d52007-06-22 14:59:07 +00001690
1691 switch (Op.getOpcode()) {
1692 case ISD::Constant:
1693 // We know all of the bits for a constant!
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001694 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
1695 KnownZero = ~KnownOne;
Dan Gohman309d3d52007-06-22 14:59:07 +00001696 return;
1697 case ISD::AND:
1698 // If either the LHS or the RHS are Zero, the result is zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001699 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1700 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001701 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1702 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00001703
1704 // Output known-1 bits are only known if set in both the LHS & RHS.
1705 KnownOne &= KnownOne2;
1706 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1707 KnownZero |= KnownZero2;
1708 return;
1709 case ISD::OR:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001710 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1711 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1713 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1714
Dan Gohman309d3d52007-06-22 14:59:07 +00001715 // Output known-0 bits are only known if clear in both the LHS & RHS.
1716 KnownZero &= KnownZero2;
1717 // Output known-1 are known to be set if set in either the LHS | RHS.
1718 KnownOne |= KnownOne2;
1719 return;
1720 case ISD::XOR: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001721 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1722 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001723 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1724 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1725
Dan Gohman309d3d52007-06-22 14:59:07 +00001726 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001727 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohman309d3d52007-06-22 14:59:07 +00001728 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1729 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1730 KnownZero = KnownZeroOut;
1731 return;
1732 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00001733 case ISD::MUL: {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001734 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1735 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001736 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1737 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1738
1739 // If low bits are zero in either operand, output low known-0 bits.
1740 // Also compute a conserative estimate for high known-0 bits.
1741 // More trickiness is possible, but this is sufficient for the
1742 // interesting case of alignment computation.
Jay Foad25a5e4c2010-12-01 08:53:58 +00001743 KnownOne.clearAllBits();
Dan Gohman72ec3f42008-04-28 17:02:21 +00001744 unsigned TrailZ = KnownZero.countTrailingOnes() +
1745 KnownZero2.countTrailingOnes();
1746 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman5a3eecd2008-05-07 00:35:55 +00001747 KnownZero2.countLeadingOnes(),
1748 BitWidth) - BitWidth;
Dan Gohman72ec3f42008-04-28 17:02:21 +00001749
1750 TrailZ = std::min(TrailZ, BitWidth);
1751 LeadZ = std::min(LeadZ, BitWidth);
1752 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1753 APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001754 return;
1755 }
1756 case ISD::UDIV: {
1757 // For the purposes of computing leading zeros we can conservatively
1758 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1962c2b2008-05-02 21:30:02 +00001759 // be less than the denominator.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001760 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001761 unsigned LeadZ = KnownZero2.countLeadingOnes();
1762
Jay Foad25a5e4c2010-12-01 08:53:58 +00001763 KnownOne2.clearAllBits();
1764 KnownZero2.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001765 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman1962c2b2008-05-02 21:30:02 +00001766 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1767 if (RHSUnknownLeadingOnes != BitWidth)
1768 LeadZ = std::min(BitWidth,
1769 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001770
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001771 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ);
Dan Gohman72ec3f42008-04-28 17:02:21 +00001772 return;
1773 }
Dan Gohman309d3d52007-06-22 14:59:07 +00001774 case ISD::SELECT:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001775 ComputeMaskedBits(Op.getOperand(2), KnownZero, KnownOne, Depth+1);
1776 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001777 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1778 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1779
Dan Gohman309d3d52007-06-22 14:59:07 +00001780 // Only known if known in both the LHS and RHS.
1781 KnownOne &= KnownOne2;
1782 KnownZero &= KnownZero2;
1783 return;
1784 case ISD::SELECT_CC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001785 ComputeMaskedBits(Op.getOperand(3), KnownZero, KnownOne, Depth+1);
1786 ComputeMaskedBits(Op.getOperand(2), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001787 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1788 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1789
Dan Gohman309d3d52007-06-22 14:59:07 +00001790 // Only known if known in both the LHS and RHS.
1791 KnownOne &= KnownOne2;
1792 KnownZero &= KnownZero2;
1793 return;
Bill Wendling5424e6d2008-11-22 07:24:01 +00001794 case ISD::SADDO:
1795 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00001796 case ISD::SSUBO:
1797 case ISD::USUBO:
1798 case ISD::SMULO:
1799 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00001800 if (Op.getResNo() != 1)
1801 return;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00001802 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00001803 case ISD::SETCC:
1804 // If we know the result of a setcc has the top bits zero, use this info.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00001805 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sandsf2641e12011-09-06 19:07:46 +00001806 TargetLowering::ZeroOrOneBooleanContent && BitWidth > 1)
Dan Gohmanf990faf2008-02-13 00:35:47 +00001807 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohman309d3d52007-06-22 14:59:07 +00001808 return;
1809 case ISD::SHL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001810 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001811 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001812 unsigned ShAmt = SA->getZExtValue();
Dan Gohman9db0aa82008-02-26 18:50:50 +00001813
1814 // If the shift count is an invalid immediate, don't do anything.
1815 if (ShAmt >= BitWidth)
1816 return;
1817
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001818 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001819 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman9db0aa82008-02-26 18:50:50 +00001820 KnownZero <<= ShAmt;
1821 KnownOne <<= ShAmt;
Dan Gohmanf990faf2008-02-13 00:35:47 +00001822 // low bits known zero.
Dan Gohman9db0aa82008-02-26 18:50:50 +00001823 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001824 }
1825 return;
1826 case ISD::SRL:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001827 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
Dan Gohman309d3d52007-06-22 14:59:07 +00001828 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001829 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001830
Dan Gohman9db0aa82008-02-26 18:50:50 +00001831 // If the shift count is an invalid immediate, don't do anything.
1832 if (ShAmt >= BitWidth)
1833 return;
1834
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001835 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001836 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001837 KnownZero = KnownZero.lshr(ShAmt);
1838 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001839
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001840 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Dan Gohman309d3d52007-06-22 14:59:07 +00001841 KnownZero |= HighBits; // High bits known zero.
1842 }
1843 return;
1844 case ISD::SRA:
1845 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001846 unsigned ShAmt = SA->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00001847
Dan Gohman9db0aa82008-02-26 18:50:50 +00001848 // If the shift count is an invalid immediate, don't do anything.
1849 if (ShAmt >= BitWidth)
1850 return;
1851
Dan Gohman309d3d52007-06-22 14:59:07 +00001852 // If any of the demanded bits are produced by the sign extension, we also
1853 // demand the input sign bit.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001854 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001855
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001856 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001857 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf990faf2008-02-13 00:35:47 +00001858 KnownZero = KnownZero.lshr(ShAmt);
1859 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001860
Dan Gohman309d3d52007-06-22 14:59:07 +00001861 // Handle the sign bits.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001862 APInt SignBit = APInt::getSignBit(BitWidth);
1863 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001864
Dan Gohmanb717fda2008-02-20 16:30:17 +00001865 if (KnownZero.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001866 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001867 } else if (KnownOne.intersects(SignBit)) {
Dan Gohman309d3d52007-06-22 14:59:07 +00001868 KnownOne |= HighBits; // New bits are known one.
1869 }
1870 }
1871 return;
1872 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001873 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001874 unsigned EBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001875
1876 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohman309d3d52007-06-22 14:59:07 +00001877 // present in the input.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001878 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00001879
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001880 APInt InSignBit = APInt::getSignBit(EBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001881 APInt InputDemandedBits = APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001882
Dan Gohman309d3d52007-06-22 14:59:07 +00001883 // If the sign extended bits are demanded, we know that the sign
1884 // bit is demanded.
Jay Foad583abbc2010-12-07 08:25:19 +00001885 InSignBit = InSignBit.zext(BitWidth);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001886 if (NewBits.getBoolValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00001887 InputDemandedBits |= InSignBit;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001888
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001889 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1890 KnownOne &= InputDemandedBits;
1891 KnownZero &= InputDemandedBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001892 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1893
Dan Gohman309d3d52007-06-22 14:59:07 +00001894 // If the sign bit of the input is known set or clear, then we know the
1895 // top bits of the result.
Dan Gohmanb717fda2008-02-20 16:30:17 +00001896 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohman309d3d52007-06-22 14:59:07 +00001897 KnownZero |= NewBits;
1898 KnownOne &= ~NewBits;
Dan Gohmanb717fda2008-02-20 16:30:17 +00001899 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohman309d3d52007-06-22 14:59:07 +00001900 KnownOne |= NewBits;
1901 KnownZero &= ~NewBits;
1902 } else { // Input sign bit unknown
1903 KnownZero &= ~NewBits;
1904 KnownOne &= ~NewBits;
1905 }
1906 return;
1907 }
1908 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001909 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00001910 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001911 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohman309d3d52007-06-22 14:59:07 +00001912 case ISD::CTPOP: {
Dan Gohmanf990faf2008-02-13 00:35:47 +00001913 unsigned LowBits = Log2_32(BitWidth)+1;
1914 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Jay Foad25a5e4c2010-12-01 08:53:58 +00001915 KnownOne.clearAllBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00001916 return;
1917 }
1918 case ISD::LOAD: {
Rafael Espindola80c540e2012-03-31 18:14:00 +00001919 LoadSDNode *LD = cast<LoadSDNode>(Op);
Nadav Rotem4536d582013-03-20 22:53:44 +00001920 // If this is a ZEXTLoad and we are looking at the loaded value.
1921 if (ISD::isZEXTLoad(Op.getNode()) && Op.getResNo() == 0) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001922 EVT VT = LD->getMemoryVT();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00001923 unsigned MemBits = VT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001924 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
Rafael Espindola80c540e2012-03-31 18:14:00 +00001925 } else if (const MDNode *Ranges = LD->getRanges()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001926 computeMaskedBitsLoad(*Ranges, KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00001927 }
1928 return;
1929 }
1930 case ISD::ZERO_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001931 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00001932 unsigned InBits = InVT.getScalarType().getSizeInBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001933 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Jay Foad583abbc2010-12-07 08:25:19 +00001934 KnownZero = KnownZero.trunc(InBits);
1935 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001936 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Jay Foad583abbc2010-12-07 08:25:19 +00001937 KnownZero = KnownZero.zext(BitWidth);
1938 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00001939 KnownZero |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00001940 return;
1941 }
1942 case ISD::SIGN_EXTEND: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001943 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00001944 unsigned InBits = InVT.getScalarType().getSizeInBits();
Dan Gohmanf990faf2008-02-13 00:35:47 +00001945 APInt InSignBit = APInt::getSignBit(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001946 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits);
Dan Gohmanf990faf2008-02-13 00:35:47 +00001947
Jay Foad583abbc2010-12-07 08:25:19 +00001948 KnownZero = KnownZero.trunc(InBits);
1949 KnownOne = KnownOne.trunc(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001950 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001951
1952 // Note if the sign bit is known to be zero or one.
1953 bool SignBitKnownZero = KnownZero.isNegative();
1954 bool SignBitKnownOne = KnownOne.isNegative();
1955 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1956 "Sign bit can't be known to be both zero and one!");
1957
Jay Foad583abbc2010-12-07 08:25:19 +00001958 KnownZero = KnownZero.zext(BitWidth);
1959 KnownOne = KnownOne.zext(BitWidth);
Dan Gohmanf990faf2008-02-13 00:35:47 +00001960
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001961 // If the sign bit is known zero or one, the top bits match.
1962 if (SignBitKnownZero)
Dan Gohman309d3d52007-06-22 14:59:07 +00001963 KnownZero |= NewBits;
Dan Gohmane1d9ee62008-02-13 22:28:48 +00001964 else if (SignBitKnownOne)
Dan Gohman309d3d52007-06-22 14:59:07 +00001965 KnownOne |= NewBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00001966 return;
1967 }
1968 case ISD::ANY_EXTEND: {
Evan Cheng9ec512d2012-12-06 19:13:27 +00001969 EVT InVT = Op.getOperand(0).getValueType();
1970 unsigned InBits = InVT.getScalarType().getSizeInBits();
1971 KnownZero = KnownZero.trunc(InBits);
1972 KnownOne = KnownOne.trunc(InBits);
1973 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1974 KnownZero = KnownZero.zext(BitWidth);
1975 KnownOne = KnownOne.zext(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001976 return;
1977 }
1978 case ISD::TRUNCATE: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001979 EVT InVT = Op.getOperand(0).getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00001980 unsigned InBits = InVT.getScalarType().getSizeInBits();
Jay Foad583abbc2010-12-07 08:25:19 +00001981 KnownZero = KnownZero.zext(InBits);
1982 KnownOne = KnownOne.zext(InBits);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001983 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001984 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Jay Foad583abbc2010-12-07 08:25:19 +00001985 KnownZero = KnownZero.trunc(BitWidth);
1986 KnownOne = KnownOne.trunc(BitWidth);
Dan Gohman309d3d52007-06-22 14:59:07 +00001987 break;
1988 }
1989 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001990 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands13237ac2008-06-06 12:08:01 +00001991 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001992 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
1993 KnownZero |= (~InMask);
Nadav Rotem839a06e2012-07-16 18:34:53 +00001994 KnownOne &= (~KnownZero);
Dan Gohman309d3d52007-06-22 14:59:07 +00001995 return;
1996 }
Chris Lattnerafc8f132007-12-22 21:26:52 +00001997 case ISD::FGETSIGN:
1998 // All bits are zero except the low bit.
Dan Gohmanf990faf2008-02-13 00:35:47 +00001999 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerafc8f132007-12-22 21:26:52 +00002000 return;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002001
Dan Gohman72ec3f42008-04-28 17:02:21 +00002002 case ISD::SUB: {
2003 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
2004 // We know that the top bits of C-X are clear if X contains less bits
2005 // than C (i.e. no wrap-around can happen). For example, 20-X is
2006 // positive if we can prove that X is >= 0 and < 16.
2007 if (CLHS->getAPIntValue().isNonNegative()) {
2008 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
2009 // NLZ can't be BitWidth with no sign bit
2010 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002011 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002012
2013 // If all of the MaskV bits are known to be zero, then we know the
2014 // output top bits are zero, because we now know that the output is
2015 // from [0-C].
2016 if ((KnownZero2 & MaskV) == MaskV) {
2017 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
2018 // Top bits known zero.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002019 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002020 }
2021 }
2022 }
2023 }
2024 // fall through
Chris Lattner440b2802010-12-19 20:38:28 +00002025 case ISD::ADD:
2026 case ISD::ADDE: {
Dan Gohman309d3d52007-06-22 14:59:07 +00002027 // Output known-0 bits are known if clear or set in both the low clear bits
2028 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
2029 // low 3 bits clear.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002030 ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002031 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002032 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
2033
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002034 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002035 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman72ec3f42008-04-28 17:02:21 +00002036 KnownZeroOut = std::min(KnownZeroOut,
2037 KnownZero2.countTrailingOnes());
2038
Chris Lattner440b2802010-12-19 20:38:28 +00002039 if (Op.getOpcode() == ISD::ADD) {
2040 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
2041 return;
2042 }
2043
2044 // With ADDE, a carry bit may be added in, so we can only use this
2045 // information if we know (at least) that the low two bits are clear. We
2046 // then return to the caller that the low bit is unknown but that other bits
2047 // are known zero.
2048 if (KnownZeroOut >= 2) // ADDE
2049 KnownZero |= APInt::getBitsSet(BitWidth, 1, KnownZeroOut);
Dan Gohman309d3d52007-06-22 14:59:07 +00002050 return;
2051 }
Dan Gohman72ec3f42008-04-28 17:02:21 +00002052 case ISD::SREM:
2053 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Duncan Sands33274982010-01-29 09:45:26 +00002054 const APInt &RA = Rem->getAPIntValue().abs();
2055 if (RA.isPowerOf2()) {
2056 APInt LowBits = RA - 1;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002057 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002058 ComputeMaskedBits(Op.getOperand(0), KnownZero2,KnownOne2,Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002059
Duncan Sands33274982010-01-29 09:45:26 +00002060 // The low bits of the first operand are unchanged by the srem.
2061 KnownZero = KnownZero2 & LowBits;
2062 KnownOne = KnownOne2 & LowBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002063
Duncan Sands33274982010-01-29 09:45:26 +00002064 // If the first operand is non-negative or has all low bits zero, then
2065 // the upper bits are all zero.
2066 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
2067 KnownZero |= ~LowBits;
2068
2069 // If the first operand is negative and not all low bits are zero, then
2070 // the upper bits are all one.
2071 if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
2072 KnownOne |= ~LowBits;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002073 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohman309d3d52007-06-22 14:59:07 +00002074 }
2075 }
2076 return;
Dan Gohman72ec3f42008-04-28 17:02:21 +00002077 case ISD::UREM: {
2078 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf3c4d7f2008-07-03 00:52:03 +00002079 const APInt &RA = Rem->getAPIntValue();
Dan Gohmancf0e3ac2008-05-06 00:51:48 +00002080 if (RA.isPowerOf2()) {
2081 APInt LowBits = (RA - 1);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002082 KnownZero |= ~LowBits;
2083 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne,Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002084 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
2085 break;
2086 }
2087 }
2088
2089 // Since the result is less than or equal to either operand, any leading
2090 // zero bits in either operand must also exist in the result.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002091 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
2092 ComputeMaskedBits(Op.getOperand(1), KnownZero2, KnownOne2, Depth+1);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002093
2094 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
2095 KnownZero2.countLeadingOnes());
Jay Foad25a5e4c2010-12-01 08:53:58 +00002096 KnownOne.clearAllBits();
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002097 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders);
Dan Gohman72ec3f42008-04-28 17:02:21 +00002098 return;
Dan Gohman309d3d52007-06-22 14:59:07 +00002099 }
Chris Lattner46c01a32011-02-13 22:25:43 +00002100 case ISD::FrameIndex:
2101 case ISD::TargetFrameIndex:
2102 if (unsigned Align = InferPtrAlignment(Op)) {
2103 // The low bits are known zero if the pointer is aligned.
2104 KnownZero = APInt::getLowBitsSet(BitWidth, Log2_32(Align));
2105 return;
2106 }
2107 break;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002108
Dan Gohman309d3d52007-06-22 14:59:07 +00002109 default:
Evan Cheng88f91372011-05-24 01:48:22 +00002110 if (Op.getOpcode() < ISD::BUILTIN_OP_END)
2111 break;
2112 // Fallthrough
Dan Gohman309d3d52007-06-22 14:59:07 +00002113 case ISD::INTRINSIC_WO_CHAIN:
2114 case ISD::INTRINSIC_W_CHAIN:
2115 case ISD::INTRINSIC_VOID:
Evan Cheng88f91372011-05-24 01:48:22 +00002116 // Allow the target to implement this method for its nodes.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002117 TLI->computeMaskedBitsForTargetNode(Op, KnownZero, KnownOne, *this, Depth);
Dan Gohman309d3d52007-06-22 14:59:07 +00002118 return;
2119 }
2120}
2121
2122/// ComputeNumSignBits - Return the number of times the sign bit of the
2123/// register is replicated into the other bits. We know that at least 1 bit
2124/// is always equal to the sign bit (itself), but other cases can give us
2125/// information. For example, immediately after an "SRA X, 2", we know that
2126/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002127unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002128 const TargetLowering *TLI = TM.getTargetLowering();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002129 EVT VT = Op.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00002130 assert(VT.isInteger() && "Invalid VT!");
Dan Gohman1d459e42009-12-11 21:31:27 +00002131 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002132 unsigned Tmp, Tmp2;
Dan Gohman6d5f1202008-05-23 02:28:01 +00002133 unsigned FirstAnswer = 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002134
Dan Gohman309d3d52007-06-22 14:59:07 +00002135 if (Depth == 6)
2136 return 1; // Limit search depth.
2137
2138 switch (Op.getOpcode()) {
2139 default: break;
2140 case ISD::AssertSext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002141 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002142 return VTBits-Tmp+1;
2143 case ISD::AssertZext:
Duncan Sands13237ac2008-06-06 12:08:01 +00002144 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002145 return VTBits-Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002146
Dan Gohman309d3d52007-06-22 14:59:07 +00002147 case ISD::Constant: {
Dan Gohman10f34072008-03-03 23:35:36 +00002148 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
Cameron Zwarich3cf92802011-02-24 10:00:20 +00002149 return Val.getNumSignBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002150 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002151
Dan Gohman309d3d52007-06-22 14:59:07 +00002152 case ISD::SIGN_EXTEND:
Dan Gohman1d459e42009-12-11 21:31:27 +00002153 Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002154 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002155
Dan Gohman309d3d52007-06-22 14:59:07 +00002156 case ISD::SIGN_EXTEND_INREG:
2157 // Max of the input and what this extends.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002158 Tmp =
2159 cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00002160 Tmp = VTBits-Tmp+1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002161
Dan Gohman309d3d52007-06-22 14:59:07 +00002162 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2163 return std::max(Tmp, Tmp2);
2164
2165 case ISD::SRA:
2166 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2167 // SRA X, C -> adds C sign bits.
2168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002169 Tmp += C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002170 if (Tmp > VTBits) Tmp = VTBits;
2171 }
2172 return Tmp;
2173 case ISD::SHL:
2174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2175 // shl destroys sign bits.
2176 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmaneffb8942008-09-12 16:56:44 +00002177 if (C->getZExtValue() >= VTBits || // Bad shift.
2178 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2179 return Tmp - C->getZExtValue();
Dan Gohman309d3d52007-06-22 14:59:07 +00002180 }
2181 break;
2182 case ISD::AND:
2183 case ISD::OR:
2184 case ISD::XOR: // NOT is handled here.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002185 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohman309d3d52007-06-22 14:59:07 +00002186 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002187 if (Tmp != 1) {
2188 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2189 FirstAnswer = std::min(Tmp, Tmp2);
2190 // We computed what we know about the sign bits as our first
2191 // answer. Now proceed to the generic code that uses
2192 // ComputeMaskedBits, and pick whichever answer is better.
2193 }
2194 break;
Dan Gohman309d3d52007-06-22 14:59:07 +00002195
2196 case ISD::SELECT:
Dan Gohmanfe136182008-05-20 20:59:51 +00002197 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002198 if (Tmp == 1) return 1; // Early out.
Dan Gohmanfe136182008-05-20 20:59:51 +00002199 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002200 return std::min(Tmp, Tmp2);
Bill Wendling5424e6d2008-11-22 07:24:01 +00002201
2202 case ISD::SADDO:
2203 case ISD::UADDO:
Bill Wendlingdb8ec2d2008-12-09 22:08:41 +00002204 case ISD::SSUBO:
2205 case ISD::USUBO:
2206 case ISD::SMULO:
2207 case ISD::UMULO:
Bill Wendling5424e6d2008-11-22 07:24:01 +00002208 if (Op.getResNo() != 1)
2209 break;
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002210 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohman309d3d52007-06-22 14:59:07 +00002211 case ISD::SETCC:
2212 // If setcc returns 0/-1, all bits are sign bits.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002213 if (TLI->getBooleanContents(Op.getValueType().isVector()) ==
Duncan Sands8d6e2e12008-11-23 15:47:28 +00002214 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohman309d3d52007-06-22 14:59:07 +00002215 return VTBits;
2216 break;
2217 case ISD::ROTL:
2218 case ISD::ROTR:
2219 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002220 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002221
Dan Gohman309d3d52007-06-22 14:59:07 +00002222 // Handle rotate right by N like a rotate left by 32-N.
2223 if (Op.getOpcode() == ISD::ROTR)
2224 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2225
2226 // If we aren't rotating out all of the known-in sign bits, return the
2227 // number that are left. This handles rotl(sext(x), 1) for example.
2228 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2229 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2230 }
2231 break;
2232 case ISD::ADD:
2233 // Add can have at most one carry bit. Thus we know that the output
2234 // is, at worst, one more bit than the inputs.
2235 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2236 if (Tmp == 1) return 1; // Early out.
Scott Michelcf0da6c2009-02-17 22:15:04 +00002237
Dan Gohman309d3d52007-06-22 14:59:07 +00002238 // Special case decrementing a value (ADD X, -1):
Dan Gohman4f356bb2009-02-24 02:00:40 +00002239 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman309d3d52007-06-22 14:59:07 +00002240 if (CRHS->isAllOnesValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002241 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002242 ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002243
Dan Gohman309d3d52007-06-22 14:59:07 +00002244 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2245 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002246 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002247 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002248
Dan Gohman309d3d52007-06-22 14:59:07 +00002249 // If we are subtracting one from a positive number, there is no carry
2250 // out of the result.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002251 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002252 return Tmp;
2253 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002254
Dan Gohman309d3d52007-06-22 14:59:07 +00002255 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2256 if (Tmp2 == 1) return 1;
David Blaikie46a9f012012-01-20 21:51:11 +00002257 return std::min(Tmp, Tmp2)-1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002258
Dan Gohman309d3d52007-06-22 14:59:07 +00002259 case ISD::SUB:
2260 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2261 if (Tmp2 == 1) return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002262
Dan Gohman309d3d52007-06-22 14:59:07 +00002263 // Handle NEG.
2264 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002265 if (CLHS->isNullValue()) {
Dan Gohmanf19609a2008-02-27 01:23:58 +00002266 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002267 ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
Dan Gohman309d3d52007-06-22 14:59:07 +00002268 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2269 // sign bits set.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002270 if ((KnownZero | APInt(VTBits, 1)).isAllOnesValue())
Dan Gohman309d3d52007-06-22 14:59:07 +00002271 return VTBits;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002272
Dan Gohman309d3d52007-06-22 14:59:07 +00002273 // If the input is known to be positive (the sign bit is known clear),
2274 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002275 if (KnownZero.isNegative())
Dan Gohman309d3d52007-06-22 14:59:07 +00002276 return Tmp2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002277
Dan Gohman309d3d52007-06-22 14:59:07 +00002278 // Otherwise, we treat this like a SUB.
2279 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002280
Dan Gohman309d3d52007-06-22 14:59:07 +00002281 // Sub can have at most one carry bit. Thus we know that the output
2282 // is, at worst, one more bit than the inputs.
2283 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2284 if (Tmp == 1) return 1; // Early out.
David Blaikie46a9f012012-01-20 21:51:11 +00002285 return std::min(Tmp, Tmp2)-1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002286 case ISD::TRUNCATE:
2287 // FIXME: it's tricky to do anything useful for this, but it is an important
2288 // case for targets like X86.
2289 break;
2290 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002291
Nadav Rotem4536d582013-03-20 22:53:44 +00002292 // If we are looking at the loaded value of the SDNode.
2293 if (Op.getResNo() == 0) {
2294 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2295 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
2296 unsigned ExtType = LD->getExtensionType();
2297 switch (ExtType) {
2298 default: break;
2299 case ISD::SEXTLOAD: // '17' bits known
2300 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2301 return VTBits-Tmp+1;
2302 case ISD::ZEXTLOAD: // '16' bits known
2303 Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
2304 return VTBits-Tmp;
2305 }
Dan Gohman309d3d52007-06-22 14:59:07 +00002306 }
2307 }
2308
2309 // Allow the target to implement this method for its nodes.
2310 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00002311 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohman309d3d52007-06-22 14:59:07 +00002312 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2313 Op.getOpcode() == ISD::INTRINSIC_VOID) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +00002314 unsigned NumBits = TLI->ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman6d5f1202008-05-23 02:28:01 +00002315 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohman309d3d52007-06-22 14:59:07 +00002316 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002317
Dan Gohman309d3d52007-06-22 14:59:07 +00002318 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2319 // use this information.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002320 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002321 ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002322
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00002323 APInt Mask;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002324 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohman309d3d52007-06-22 14:59:07 +00002325 Mask = KnownZero;
Dan Gohmanf19609a2008-02-27 01:23:58 +00002326 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohman309d3d52007-06-22 14:59:07 +00002327 Mask = KnownOne;
2328 } else {
2329 // Nothing known.
Dan Gohman6d5f1202008-05-23 02:28:01 +00002330 return FirstAnswer;
Dan Gohman309d3d52007-06-22 14:59:07 +00002331 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002332
Dan Gohman309d3d52007-06-22 14:59:07 +00002333 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2334 // the number of identical bits in the top of the input value.
Dan Gohmanf19609a2008-02-27 01:23:58 +00002335 Mask = ~Mask;
2336 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohman309d3d52007-06-22 14:59:07 +00002337 // Return # leading zeros. We use 'min' here in case Val was zero before
2338 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman6d5f1202008-05-23 02:28:01 +00002339 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohman309d3d52007-06-22 14:59:07 +00002340}
2341
Chris Lattner46c01a32011-02-13 22:25:43 +00002342/// isBaseWithConstantOffset - Return true if the specified operand is an
2343/// ISD::ADD with a ConstantSDNode on the right-hand side, or if it is an
2344/// ISD::OR with a ConstantSDNode that is guaranteed to have the same
2345/// semantics as an ADD. This handles the equivalence:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002346/// X|Cst == X+Cst iff X&Cst = 0.
Chris Lattner46c01a32011-02-13 22:25:43 +00002347bool SelectionDAG::isBaseWithConstantOffset(SDValue Op) const {
2348 if ((Op.getOpcode() != ISD::ADD && Op.getOpcode() != ISD::OR) ||
2349 !isa<ConstantSDNode>(Op.getOperand(1)))
2350 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002351
2352 if (Op.getOpcode() == ISD::OR &&
Chris Lattner46c01a32011-02-13 22:25:43 +00002353 !MaskedValueIsZero(Op.getOperand(0),
2354 cast<ConstantSDNode>(Op.getOperand(1))->getAPIntValue()))
2355 return false;
Owen Andersonb2c80da2011-02-25 21:41:48 +00002356
Chris Lattner46c01a32011-02-13 22:25:43 +00002357 return true;
2358}
2359
2360
Dan Gohmand0d5e682009-09-03 20:34:31 +00002361bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2362 // If we're told that NaNs won't happen, assume they won't.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002363 if (getTarget().Options.NoNaNsFPMath)
Dan Gohmand0d5e682009-09-03 20:34:31 +00002364 return true;
2365
2366 // If the value is a constant, we can obviously see if it is a NaN or not.
2367 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2368 return !C->getValueAPF().isNaN();
2369
2370 // TODO: Recognize more cases here.
2371
2372 return false;
2373}
Chris Lattnerbd9acad2006-10-14 00:41:01 +00002374
Dan Gohman38605212010-02-24 06:52:40 +00002375bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
2376 // If the value is a constant, we can obviously see if it is a zero or not.
2377 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2378 return !C->isZero();
2379
2380 // TODO: Recognize more cases here.
Evan Cheng88f91372011-05-24 01:48:22 +00002381 switch (Op.getOpcode()) {
2382 default: break;
2383 case ISD::OR:
2384 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
2385 return !C->isNullValue();
2386 break;
2387 }
Dan Gohman38605212010-02-24 06:52:40 +00002388
2389 return false;
2390}
2391
2392bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
2393 // Check the obvious case.
2394 if (A == B) return true;
2395
2396 // For for negative and positive zero.
2397 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
2398 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
2399 if (CA->isZero() && CB->isZero()) return true;
2400
2401 // Otherwise they may not be equal.
2402 return false;
2403}
2404
Chris Lattner061a1ea2005-01-07 07:46:32 +00002405/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +00002406///
Andrew Trickef9de2a2013-05-25 02:42:55 +00002407SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT) {
Jim Laskeyf576b422006-10-27 23:46:08 +00002408 FoldingSetNodeID ID;
Dan Gohman768f2c92008-07-07 18:26:29 +00002409 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattnerfcb16472006-08-11 18:38:11 +00002410 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002411 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002412 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002413
Andrew Trickef9de2a2013-05-25 02:42:55 +00002414 SDNode *N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), getVTList(VT));
Chris Lattnerfcb16472006-08-11 18:38:11 +00002415 CSEMap.InsertNode(N, IP);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002416
Chris Lattnerfcb16472006-08-11 18:38:11 +00002417 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002418#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002419 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002420#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002421 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002422}
2423
Andrew Trickef9de2a2013-05-25 02:42:55 +00002424SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00002425 EVT VT, SDValue Operand) {
Chris Lattnera1874602005-12-23 05:30:37 +00002426 // Constant fold unary operations with an integer constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002428 const APInt &Val = C->getAPIntValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002429 switch (Opcode) {
2430 default: break;
Evan Cheng34173f02008-03-06 17:42:34 +00002431 case ISD::SIGN_EXTEND:
Jay Foad583abbc2010-12-07 08:25:19 +00002432 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), VT);
Chris Lattner8c393c22005-09-02 00:17:32 +00002433 case ISD::ANY_EXTEND:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002434 case ISD::ZERO_EXTEND:
Evan Cheng34173f02008-03-06 17:42:34 +00002435 case ISD::TRUNCATE:
Jay Foad583abbc2010-12-07 08:25:19 +00002436 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), VT);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002437 case ISD::UINT_TO_FP:
2438 case ISD::SINT_TO_FP: {
Tim Northover29178a32013-01-22 09:46:31 +00002439 APFloat apf(EVTToAPFloatSemantics(VT),
2440 APInt::getNullValue(VT.getSizeInBits()));
Scott Michelcf0da6c2009-02-17 22:15:04 +00002441 (void)apf.convertFromAPInt(Val,
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002442 Opcode==ISD::SINT_TO_FP,
2443 APFloat::rmNearestTiesToEven);
Dale Johannesen7d67e542007-09-19 23:55:34 +00002444 return getConstantFP(apf, VT);
2445 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002446 case ISD::BITCAST:
Owen Anderson9f944592009-08-11 20:47:22 +00002447 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Tim Northover29178a32013-01-22 09:46:31 +00002448 return getConstantFP(APFloat(APFloat::IEEEsingle, Val), VT);
Owen Anderson9f944592009-08-11 20:47:22 +00002449 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Tim Northover29178a32013-01-22 09:46:31 +00002450 return getConstantFP(APFloat(APFloat::IEEEdouble, Val), VT);
Chris Lattnera1874602005-12-23 05:30:37 +00002451 break;
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002452 case ISD::BSWAP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002453 return getConstant(Val.byteSwap(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002454 case ISD::CTPOP:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002455 return getConstant(Val.countPopulation(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002456 case ISD::CTLZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002457 case ISD::CTLZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002458 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1e1eb5e2006-01-16 08:07:10 +00002459 case ISD::CTTZ:
Chandler Carruth637cc6a2011-12-13 01:56:10 +00002460 case ISD::CTTZ_ZERO_UNDEF:
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002461 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002462 }
2463 }
2464
Dale Johannesen446b9002007-08-31 23:34:27 +00002465 // Constant fold unary operations with a floating point constant operand.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002466 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen446b9002007-08-31 23:34:27 +00002467 APFloat V = C->getValueAPF(); // make copy
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002468 switch (Opcode) {
2469 case ISD::FNEG:
2470 V.changeSign();
2471 return getConstantFP(V, VT);
2472 case ISD::FABS:
2473 V.clearSign();
2474 return getConstantFP(V, VT);
2475 case ISD::FCEIL: {
2476 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
2477 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002478 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002479 break;
2480 }
2481 case ISD::FTRUNC: {
2482 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
2483 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002484 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002485 break;
2486 }
2487 case ISD::FFLOOR: {
2488 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
2489 if (fs == APFloat::opOK || fs == APFloat::opInexact)
Dale Johannesene5facd52007-10-16 23:38:29 +00002490 return getConstantFP(V, VT);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002491 break;
2492 }
2493 case ISD::FP_EXTEND: {
2494 bool ignored;
2495 // This can return overflow, underflow, or inexact; we don't care.
2496 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00002497 (void)V.convert(EVTToAPFloatSemantics(VT),
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002498 APFloat::rmNearestTiesToEven, &ignored);
2499 return getConstantFP(V, VT);
2500 }
2501 case ISD::FP_TO_SINT:
2502 case ISD::FP_TO_UINT: {
2503 integerPart x[2];
2504 bool ignored;
2505 assert(integerPartWidth >= 64);
2506 // FIXME need to be more flexible about rounding mode.
2507 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
2508 Opcode==ISD::FP_TO_SINT,
2509 APFloat::rmTowardZero, &ignored);
2510 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
Dale Johannesen446b9002007-08-31 23:34:27 +00002511 break;
Ulrich Weigand3abb3432012-10-29 18:35:49 +00002512 APInt api(VT.getSizeInBits(), x);
2513 return getConstant(api, VT);
2514 }
2515 case ISD::BITCAST:
2516 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
2517 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
2518 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
2519 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
2520 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002521 }
Dale Johannesen446b9002007-08-31 23:34:27 +00002522 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002523
Gabor Greiff304a7a2008-08-28 21:40:38 +00002524 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002525 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +00002526 case ISD::TokenFactor:
Duncan Sands3d960942008-12-01 11:41:29 +00002527 case ISD::MERGE_VALUES:
Dan Gohman550c9af2008-08-14 20:04:46 +00002528 case ISD::CONCAT_VECTORS:
Duncan Sands3d960942008-12-01 11:41:29 +00002529 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinfbcc6632009-07-14 16:55:14 +00002530 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattner18d67182007-04-09 05:23:13 +00002531 case ISD::FP_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002532 assert(VT.isFloatingPoint() &&
2533 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner52188502008-01-16 17:59:31 +00002534 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Dan Gohmancecad352009-12-14 23:40:38 +00002535 assert((!VT.isVector() ||
2536 VT.getVectorNumElements() ==
2537 Operand.getValueType().getVectorNumElements()) &&
2538 "Vector element count mismatch!");
Chris Lattner5c7bda42008-03-11 06:21:08 +00002539 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002540 return getUNDEF(VT);
Chris Lattner18d67182007-04-09 05:23:13 +00002541 break;
Chris Lattner5c7bda42008-03-11 06:21:08 +00002542 case ISD::SIGN_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002543 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002544 "Invalid SIGN_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002545 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002546 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2547 "Invalid sext node, dst < src!");
2548 assert((!VT.isVector() ||
2549 VT.getVectorNumElements() ==
2550 Operand.getValueType().getVectorNumElements()) &&
2551 "Vector element count mismatch!");
Nadav Rotem9450fcf2013-01-20 08:35:56 +00002552 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
2553 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
2554 else if (OpOpcode == ISD::UNDEF)
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002555 // sext(undef) = 0, because the top bits will all be the same.
2556 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002557 break;
2558 case ISD::ZERO_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002559 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002560 "Invalid ZERO_EXTEND!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002561 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002562 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2563 "Invalid zext node, dst < src!");
2564 assert((!VT.isVector() ||
2565 VT.getVectorNumElements() ==
2566 Operand.getValueType().getVectorNumElements()) &&
2567 "Vector element count mismatch!");
Chris Lattnerb32d9312005-04-07 19:43:53 +00002568 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00002569 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendb393622009-02-03 01:55:44 +00002570 Operand.getNode()->getOperand(0));
Evan Chengc5c2cfa2011-03-15 02:22:10 +00002571 else if (OpOpcode == ISD::UNDEF)
2572 // zext(undef) = 0, because the top bits will be zero.
2573 return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002574 break;
Chris Lattner8c393c22005-09-02 00:17:32 +00002575 case ISD::ANY_EXTEND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002576 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002577 "Invalid ANY_EXTEND!");
Chris Lattner8c393c22005-09-02 00:17:32 +00002578 if (Operand.getValueType() == VT) return Operand; // noop extension
Dan Gohmancecad352009-12-14 23:40:38 +00002579 assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
2580 "Invalid anyext node, dst < src!");
2581 assert((!VT.isVector() ||
2582 VT.getVectorNumElements() ==
2583 Operand.getValueType().getVectorNumElements()) &&
2584 "Vector element count mismatch!");
Dan Gohman600f62b2010-06-24 14:30:44 +00002585
Dan Gohman08837892010-06-18 00:08:30 +00002586 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2587 OpOpcode == ISD::ANY_EXTEND)
Chris Lattner8c393c22005-09-02 00:17:32 +00002588 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendb393622009-02-03 01:55:44 +00002589 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Evan Chengd2f3b012011-03-14 18:15:55 +00002590 else if (OpOpcode == ISD::UNDEF)
2591 return getUNDEF(VT);
Dan Gohman600f62b2010-06-24 14:30:44 +00002592
2593 // (ext (trunx x)) -> x
2594 if (OpOpcode == ISD::TRUNCATE) {
2595 SDValue OpOp = Operand.getNode()->getOperand(0);
2596 if (OpOp.getValueType() == VT)
2597 return OpOp;
2598 }
Chris Lattner8c393c22005-09-02 00:17:32 +00002599 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002600 case ISD::TRUNCATE:
Duncan Sands13237ac2008-06-06 12:08:01 +00002601 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattner18d67182007-04-09 05:23:13 +00002602 "Invalid TRUNCATE!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002603 if (Operand.getValueType() == VT) return Operand; // noop truncate
Dan Gohmancecad352009-12-14 23:40:38 +00002604 assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
2605 "Invalid truncate node, src < dst!");
2606 assert((!VT.isVector() ||
2607 VT.getVectorNumElements() ==
2608 Operand.getValueType().getVectorNumElements()) &&
2609 "Vector element count mismatch!");
Chris Lattner061a1ea2005-01-07 07:46:32 +00002610 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendb393622009-02-03 01:55:44 +00002611 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002612 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2613 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +00002614 // If the source is smaller than the dest, we still need an extend.
Dan Gohmancecad352009-12-14 23:40:38 +00002615 if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
2616 .bitsLT(VT.getScalarType()))
Dale Johannesendb393622009-02-03 01:55:44 +00002617 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002618 if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendb393622009-02-03 01:55:44 +00002619 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Craig Topper201c1a32012-01-15 01:05:11 +00002620 return Operand.getNode()->getOperand(0);
Chris Lattner4d5ba992005-01-07 21:56:24 +00002621 }
Craig Topper201c1a32012-01-15 01:05:11 +00002622 if (OpOpcode == ISD::UNDEF)
2623 return getUNDEF(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002624 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00002625 case ISD::BITCAST:
Chris Lattner36e663d2005-12-23 00:16:34 +00002626 // Basic sanity checking.
Duncan Sands13237ac2008-06-06 12:08:01 +00002627 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Wesley Peck527da1b2010-11-23 03:31:01 +00002628 && "Cannot BITCAST between types of different sizes!");
Chris Lattner36e663d2005-12-23 00:16:34 +00002629 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Wesley Peck527da1b2010-11-23 03:31:01 +00002630 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
2631 return getNode(ISD::BITCAST, DL, VT, Operand.getOperand(0));
Chris Lattnera9e77d12006-04-04 01:02:22 +00002632 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002633 return getUNDEF(VT);
Chris Lattner36e663d2005-12-23 00:16:34 +00002634 break;
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002635 case ISD::SCALAR_TO_VECTOR:
Duncan Sands13237ac2008-06-06 12:08:01 +00002636 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandse4ff21b2009-04-18 20:16:54 +00002637 (VT.getVectorElementType() == Operand.getValueType() ||
2638 (VT.getVectorElementType().isInteger() &&
2639 Operand.getValueType().isInteger() &&
2640 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002641 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnera1f25b02008-03-08 23:43:36 +00002642 if (OpOpcode == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00002643 return getUNDEF(VT);
Chris Lattnera1f25b02008-03-08 23:43:36 +00002644 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2645 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2646 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2647 Operand.getConstantOperandVal(1) == 0 &&
2648 Operand.getOperand(0).getValueType() == VT)
2649 return Operand.getOperand(0);
Chris Lattner9cdc5a02006-03-19 06:31:19 +00002650 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00002651 case ISD::FNEG:
Mon P Wangcf9ba822009-01-31 06:07:45 +00002652 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002653 if (getTarget().Options.UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendb393622009-02-03 01:55:44 +00002654 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greiff304a7a2008-08-28 21:40:38 +00002655 Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002656 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greiff304a7a2008-08-28 21:40:38 +00002657 return Operand.getNode()->getOperand(0);
Chris Lattner0ea81f92005-04-09 03:02:46 +00002658 break;
2659 case ISD::FABS:
2660 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendb393622009-02-03 01:55:44 +00002661 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner0ea81f92005-04-09 03:02:46 +00002662 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00002663 }
2664
Chris Lattnerf9c19152005-08-25 19:12:10 +00002665 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00002666 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002667 if (VT != MVT::Glue) { // Don't CSE flag producing nodes
Jim Laskeyf576b422006-10-27 23:46:08 +00002668 FoldingSetNodeID ID;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002669 SDValue Ops[1] = { Operand };
Chris Lattnerf17b4222007-02-04 07:28:00 +00002670 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002671 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00002672 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002673 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00002674
Andrew Trickef9de2a2013-05-25 02:42:55 +00002675 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, Operand);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00002676 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002677 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, Operand);
Chris Lattnerf9c19152005-08-25 19:12:10 +00002679 }
Duncan Sandsb0e39382008-07-21 10:20:31 +00002680
Chris Lattner061a1ea2005-01-07 07:46:32 +00002681 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002682#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00002683 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00002684#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002685 return SDValue(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +00002686}
2687
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002688SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, EVT VT,
2689 SDNode *Cst1, SDNode *Cst2) {
2690 SmallVector<std::pair<ConstantSDNode *, ConstantSDNode *>, 4> Inputs;
2691 SmallVector<SDValue, 4> Outputs;
2692 EVT SVT = VT.getScalarType();
Bill Wendling162c26d2008-09-24 10:16:24 +00002693
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002694 ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1);
2695 ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2);
2696 if (Scalar1 && Scalar2) {
2697 // Scalar instruction.
2698 Inputs.push_back(std::make_pair(Scalar1, Scalar2));
2699 } else {
2700 // For vectors extract each constant element into Inputs so we can constant
2701 // fold them individually.
2702 BuildVectorSDNode *BV1 = dyn_cast<BuildVectorSDNode>(Cst1);
2703 BuildVectorSDNode *BV2 = dyn_cast<BuildVectorSDNode>(Cst2);
2704 if (!BV1 || !BV2)
2705 return SDValue();
2706
2707 assert(BV1->getNumOperands() == BV2->getNumOperands() && "Out of sync!");
2708
2709 for (unsigned I = 0, E = BV1->getNumOperands(); I != E; ++I) {
2710 ConstantSDNode *V1 = dyn_cast<ConstantSDNode>(BV1->getOperand(I));
2711 ConstantSDNode *V2 = dyn_cast<ConstantSDNode>(BV2->getOperand(I));
2712 if (!V1 || !V2) // Not a constant, bail.
2713 return SDValue();
2714
2715 // Avoid BUILD_VECTOR nodes that perform implicit truncation.
2716 // FIXME: This is valid and could be handled by truncating the APInts.
2717 if (V1->getValueType(0) != SVT || V2->getValueType(0) != SVT)
2718 return SDValue();
2719
2720 Inputs.push_back(std::make_pair(V1, V2));
2721 }
Bill Wendling162c26d2008-09-24 10:16:24 +00002722 }
2723
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002724 // We have a number of constant values, constant fold them element by element.
2725 for (unsigned I = 0, E = Inputs.size(); I != E; ++I) {
2726 const APInt &C1 = Inputs[I].first->getAPIntValue();
2727 const APInt &C2 = Inputs[I].second->getAPIntValue();
2728
2729 switch (Opcode) {
2730 case ISD::ADD:
2731 Outputs.push_back(getConstant(C1 + C2, SVT));
2732 break;
2733 case ISD::SUB:
2734 Outputs.push_back(getConstant(C1 - C2, SVT));
2735 break;
2736 case ISD::MUL:
2737 Outputs.push_back(getConstant(C1 * C2, SVT));
2738 break;
2739 case ISD::UDIV:
2740 if (!C2.getBoolValue())
2741 return SDValue();
2742 Outputs.push_back(getConstant(C1.udiv(C2), SVT));
2743 break;
2744 case ISD::UREM:
2745 if (!C2.getBoolValue())
2746 return SDValue();
2747 Outputs.push_back(getConstant(C1.urem(C2), SVT));
2748 break;
2749 case ISD::SDIV:
2750 if (!C2.getBoolValue())
2751 return SDValue();
2752 Outputs.push_back(getConstant(C1.sdiv(C2), SVT));
2753 break;
2754 case ISD::SREM:
2755 if (!C2.getBoolValue())
2756 return SDValue();
2757 Outputs.push_back(getConstant(C1.srem(C2), SVT));
2758 break;
2759 case ISD::AND:
2760 Outputs.push_back(getConstant(C1 & C2, SVT));
2761 break;
2762 case ISD::OR:
2763 Outputs.push_back(getConstant(C1 | C2, SVT));
2764 break;
2765 case ISD::XOR:
2766 Outputs.push_back(getConstant(C1 ^ C2, SVT));
2767 break;
2768 case ISD::SHL:
2769 Outputs.push_back(getConstant(C1 << C2, SVT));
2770 break;
2771 case ISD::SRL:
2772 Outputs.push_back(getConstant(C1.lshr(C2), SVT));
2773 break;
2774 case ISD::SRA:
2775 Outputs.push_back(getConstant(C1.ashr(C2), SVT));
2776 break;
2777 case ISD::ROTL:
2778 Outputs.push_back(getConstant(C1.rotl(C2), SVT));
2779 break;
2780 case ISD::ROTR:
2781 Outputs.push_back(getConstant(C1.rotr(C2), SVT));
2782 break;
2783 default:
2784 return SDValue();
2785 }
2786 }
2787
2788 // Handle the scalar case first.
Silviu Baranga4ad2bc52013-04-25 09:32:33 +00002789 if (Scalar1 && Scalar2)
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002790 return Outputs.back();
2791
2792 // Otherwise build a big vector out of the scalar elements we generated.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002793 return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs.data(),
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002794 Outputs.size());
Bill Wendling162c26d2008-09-24 10:16:24 +00002795}
2796
Andrew Trickef9de2a2013-05-25 02:42:55 +00002797SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT, SDValue N1,
Benjamin Kramer548ffa22013-02-04 15:19:18 +00002798 SDValue N2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002799 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2800 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner4e550eb2005-01-16 02:23:22 +00002801 switch (Opcode) {
Chris Lattner16713612008-01-22 19:09:33 +00002802 default: break;
Chris Lattner9b75e142005-01-19 18:01:40 +00002803 case ISD::TokenFactor:
Owen Anderson9f944592009-08-11 20:47:22 +00002804 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2805 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner16713612008-01-22 19:09:33 +00002806 // Fold trivial token factors.
2807 if (N1.getOpcode() == ISD::EntryToken) return N2;
2808 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman94798d32008-10-01 15:11:19 +00002809 if (N1 == N2) return N1;
Chris Lattner9b75e142005-01-19 18:01:40 +00002810 break;
Dan Gohman550c9af2008-08-14 20:04:46 +00002811 case ISD::CONCAT_VECTORS:
Nadav Rotema62368c2012-07-15 08:38:23 +00002812 // Concat of UNDEFs is UNDEF.
2813 if (N1.getOpcode() == ISD::UNDEF &&
2814 N2.getOpcode() == ISD::UNDEF)
2815 return getUNDEF(VT);
2816
Dan Gohman550c9af2008-08-14 20:04:46 +00002817 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2818 // one big BUILD_VECTOR.
2819 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2820 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00002821 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
2822 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00002823 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00002824 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00002825 }
2826 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002827 case ISD::AND:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002828 assert(VT.isInteger() && "This operator does not apply to FP types!");
2829 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002830 N1.getValueType() == VT && "Binary operator types must match!");
2831 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2832 // worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002833 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002834 return N2;
Chris Lattner720d8992008-01-26 01:05:42 +00002835 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2836 return N1;
Chris Lattner16713612008-01-22 19:09:33 +00002837 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002838 case ISD::OR:
2839 case ISD::XOR:
Dan Gohman057240f2008-06-02 22:27:05 +00002840 case ISD::ADD:
2841 case ISD::SUB:
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002842 assert(VT.isInteger() && "This operator does not apply to FP types!");
2843 assert(N1.getValueType() == N2.getValueType() &&
Chris Lattner16713612008-01-22 19:09:33 +00002844 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman057240f2008-06-02 22:27:05 +00002845 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2846 // it's worth handling here.
Dan Gohmanb72127a2008-03-13 22:13:53 +00002847 if (N2C && N2C->isNullValue())
Chris Lattner16713612008-01-22 19:09:33 +00002848 return N1;
2849 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002850 case ISD::UDIV:
2851 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +00002852 case ISD::MULHU:
2853 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002854 case ISD::MUL:
2855 case ISD::SDIV:
2856 case ISD::SREM:
Dan Gohman1f3411d2009-01-22 21:58:43 +00002857 assert(VT.isInteger() && "This operator does not apply to FP types!");
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002858 assert(N1.getValueType() == N2.getValueType() &&
2859 N1.getValueType() == VT && "Binary operator types must match!");
2860 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002861 case ISD::FADD:
2862 case ISD::FSUB:
2863 case ISD::FMUL:
2864 case ISD::FDIV:
2865 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002866 if (getTarget().Options.UnsafeFPMath) {
Dan Gohman1275e282009-01-23 19:10:37 +00002867 if (Opcode == ISD::FADD) {
2868 // 0+x --> x
2869 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2870 if (CFP->getValueAPF().isZero())
2871 return N2;
2872 // x+0 --> x
2873 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2874 if (CFP->getValueAPF().isZero())
2875 return N1;
2876 } else if (Opcode == ISD::FSUB) {
2877 // x-0 --> x
2878 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2879 if (CFP->getValueAPF().isZero())
2880 return N1;
Michael Ilseman0666f052012-09-10 17:00:37 +00002881 } else if (Opcode == ISD::FMUL) {
2882 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1);
2883 SDValue V = N2;
2884
2885 // If the first operand isn't the constant, try the second
2886 if (!CFP) {
2887 CFP = dyn_cast<ConstantFPSDNode>(N2);
2888 V = N1;
2889 }
2890
2891 if (CFP) {
2892 // 0*x --> 0
2893 if (CFP->isZero())
2894 return SDValue(CFP,0);
2895 // 1*x --> x
2896 if (CFP->isExactlyValue(1.0))
2897 return V;
2898 }
Dan Gohman1275e282009-01-23 19:10:37 +00002899 }
Dan Gohman1f3411d2009-01-22 21:58:43 +00002900 }
Dale Johannesenbb4656c2010-05-15 18:38:02 +00002901 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
Chris Lattner4e550eb2005-01-16 02:23:22 +00002902 assert(N1.getValueType() == N2.getValueType() &&
2903 N1.getValueType() == VT && "Binary operator types must match!");
2904 break;
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002905 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2906 assert(N1.getValueType() == VT &&
Duncan Sands13237ac2008-06-06 12:08:01 +00002907 N1.getValueType().isFloatingPoint() &&
2908 N2.getValueType().isFloatingPoint() &&
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00002909 "Invalid FCOPYSIGN!");
2910 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002911 case ISD::SHL:
2912 case ISD::SRA:
2913 case ISD::SRL:
Nate Begeman1b8121b2006-01-11 21:21:00 +00002914 case ISD::ROTL:
2915 case ISD::ROTR:
Chris Lattner4e550eb2005-01-16 02:23:22 +00002916 assert(VT == N1.getValueType() &&
2917 "Shift operators return type must be the same as their first arg");
Duncan Sands13237ac2008-06-06 12:08:01 +00002918 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner6b2c4f62008-07-02 17:01:57 +00002919 "Shifts only work on integers");
Michael Liao6af16fc2013-03-01 18:40:30 +00002920 assert((!VT.isVector() || VT == N2.getValueType()) &&
2921 "Vector shift amounts must be in the same as their first arg");
Chris Lattnere95d1952011-02-13 19:09:16 +00002922 // Verify that the shift amount VT is bit enough to hold valid shift
2923 // amounts. This catches things like trying to shift an i1024 value by an
2924 // i8, which is easy to fall into in generic code that uses
2925 // TLI.getShiftAmount().
2926 assert(N2.getValueType().getSizeInBits() >=
Owen Andersonb2c80da2011-02-25 21:41:48 +00002927 Log2_32_Ceil(N1.getValueType().getSizeInBits()) &&
Chris Lattnere95d1952011-02-13 19:09:16 +00002928 "Invalid use of small shift amount with oversized value!");
Chris Lattner6b2c4f62008-07-02 17:01:57 +00002929
2930 // Always fold shifts of i1 values so the code generator doesn't need to
2931 // handle them. Since we know the size of the shift has to be less than the
2932 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson9f944592009-08-11 20:47:22 +00002933 if (VT == MVT::i1)
Chris Lattner6b2c4f62008-07-02 17:01:57 +00002934 return N1;
Evan Cheng166a4e62010-01-06 19:38:29 +00002935 if (N2C && N2C->isNullValue())
2936 return N1;
Chris Lattner4e550eb2005-01-16 02:23:22 +00002937 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00002938 case ISD::FP_ROUND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002939 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00002940 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002941 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00002942 "Cannot FP_ROUND_INREG integer types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002943 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002944 "FP_ROUND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002945 "type is vector!");
2946 assert((!EVT.isVector() ||
2947 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2948 "Vector element counts must match in FP_ROUND_INREG");
Duncan Sands11dd4242008-06-08 20:54:56 +00002949 assert(EVT.bitsLE(VT) && "Not rounding down!");
Duncan Sandsd278d352011-10-18 12:44:00 +00002950 (void)EVT;
Chris Lattner16713612008-01-22 19:09:33 +00002951 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner0b6ba902005-07-10 00:07:11 +00002952 break;
2953 }
Chris Lattner72733e52008-01-17 07:00:52 +00002954 case ISD::FP_ROUND:
Duncan Sands13237ac2008-06-06 12:08:01 +00002955 assert(VT.isFloatingPoint() &&
2956 N1.getValueType().isFloatingPoint() &&
Duncan Sands11dd4242008-06-08 20:54:56 +00002957 VT.bitsLE(N1.getValueType()) &&
Chris Lattner72733e52008-01-17 07:00:52 +00002958 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner16713612008-01-22 19:09:33 +00002959 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner72733e52008-01-17 07:00:52 +00002960 break;
Nate Begeman43144a22005-08-30 02:44:00 +00002961 case ISD::AssertSext:
Chris Lattner16713612008-01-22 19:09:33 +00002962 case ISD::AssertZext: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002963 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner16713612008-01-22 19:09:33 +00002964 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002965 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner16713612008-01-22 19:09:33 +00002966 "Cannot *_EXTEND_INREG FP types");
Dan Gohman1d459e42009-12-11 21:31:27 +00002967 assert(!EVT.isVector() &&
2968 "AssertSExt/AssertZExt type should be the vector element type "
2969 "rather than the vector type!");
Duncan Sands11dd4242008-06-08 20:54:56 +00002970 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands56689502008-02-10 10:08:52 +00002971 if (VT == EVT) return N1; // noop assertion.
Chris Lattner16713612008-01-22 19:09:33 +00002972 break;
2973 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00002974 case ISD::SIGN_EXTEND_INREG: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002975 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner0b6ba902005-07-10 00:07:11 +00002976 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands13237ac2008-06-06 12:08:01 +00002977 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00002978 "Cannot *_EXTEND_INREG FP types");
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002979 assert(EVT.isVector() == VT.isVector() &&
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002980 "SIGN_EXTEND_INREG type should be vector iff the operand "
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002981 "type is vector!");
2982 assert((!EVT.isVector() ||
2983 EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
2984 "Vector element counts must match in SIGN_EXTEND_INREG");
2985 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner16713612008-01-22 19:09:33 +00002986 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00002987
Chris Lattner16713612008-01-22 19:09:33 +00002988 if (N1C) {
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002989 APInt Val = N1C->getAPIntValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00002990 unsigned FromBits = EVT.getScalarType().getSizeInBits();
Dan Gohmanbd2fa562008-02-29 01:47:35 +00002991 Val <<= Val.getBitWidth()-FromBits;
Evan Chenga3cb0902008-03-06 08:20:51 +00002992 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattner751817c2006-05-06 23:05:41 +00002993 return getConstant(Val, VT);
2994 }
Chris Lattner16713612008-01-22 19:09:33 +00002995 break;
2996 }
2997 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnera1f25b02008-03-08 23:43:36 +00002998 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2999 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen84935752009-02-06 23:05:02 +00003000 return getUNDEF(VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003001
Chris Lattner16713612008-01-22 19:09:33 +00003002 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
3003 // expanding copies of large vectors from registers.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003004 if (N2C &&
3005 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner16713612008-01-22 19:09:33 +00003006 N1.getNumOperands() > 0) {
3007 unsigned Factor =
Duncan Sands13237ac2008-06-06 12:08:01 +00003008 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendb393622009-02-03 01:55:44 +00003009 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmaneffb8942008-09-12 16:56:44 +00003010 N1.getOperand(N2C->getZExtValue() / Factor),
3011 getConstant(N2C->getZExtValue() % Factor,
3012 N2.getValueType()));
Chris Lattner16713612008-01-22 19:09:33 +00003013 }
3014
3015 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
3016 // expanding large vector constants.
Bob Wilson59dbbb22009-04-13 22:05:19 +00003017 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
3018 SDValue Elt = N1.getOperand(N2C->getZExtValue());
James Molloy1e5c6112012-09-10 14:01:21 +00003019
3020 if (VT != Elt.getValueType())
Bob Wilson59dbbb22009-04-13 22:05:19 +00003021 // If the vector element type is not legal, the BUILD_VECTOR operands
James Molloy1e5c6112012-09-10 14:01:21 +00003022 // are promoted and implicitly truncated, and the result implicitly
3023 // extended. Make that explicit here.
3024 Elt = getAnyExtOrTrunc(Elt, DL, VT);
Michael Ilsemand5f91512012-09-10 16:56:31 +00003025
Bob Wilson59dbbb22009-04-13 22:05:19 +00003026 return Elt;
3027 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003028
Chris Lattner16713612008-01-22 19:09:33 +00003029 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
3030 // operations are lowered to scalars.
Dan Gohman7e3c3922008-08-13 21:51:37 +00003031 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Mon P Wangd74e0022010-02-01 22:15:09 +00003032 // If the indices are the same, return the inserted element else
3033 // if the indices are known different, extract the element from
Dan Gohmanef04ed52009-01-29 16:10:46 +00003034 // the original vector.
Bill Wendlingde4b2252010-04-30 22:19:17 +00003035 SDValue N1Op2 = N1.getOperand(2);
3036 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
3037
3038 if (N1Op2C && N2C) {
3039 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
3040 if (VT == N1.getOperand(1).getValueType())
3041 return N1.getOperand(1);
3042 else
3043 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
3044 }
3045
Dale Johannesendb393622009-02-03 01:55:44 +00003046 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Bill Wendlingde4b2252010-04-30 22:19:17 +00003047 }
Dan Gohman7e3c3922008-08-13 21:51:37 +00003048 }
Chris Lattner16713612008-01-22 19:09:33 +00003049 break;
3050 case ISD::EXTRACT_ELEMENT:
Dan Gohmaneffb8942008-09-12 16:56:44 +00003051 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands33ff5c82008-06-25 20:24:48 +00003052 assert(!N1.getValueType().isVector() && !VT.isVector() &&
3053 (N1.getValueType().isInteger() == VT.isInteger()) &&
Eli Friedman04c50252011-08-02 18:38:35 +00003054 N1.getValueType() != VT &&
Duncan Sands33ff5c82008-06-25 20:24:48 +00003055 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands87de65f2008-03-12 20:30:08 +00003056
Chris Lattner16713612008-01-22 19:09:33 +00003057 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
3058 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelcf0da6c2009-02-17 22:15:04 +00003059 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner16713612008-01-22 19:09:33 +00003060 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmaneffb8942008-09-12 16:56:44 +00003061 return N1.getOperand(N2C->getZExtValue());
Duncan Sands87de65f2008-03-12 20:30:08 +00003062
Chris Lattner16713612008-01-22 19:09:33 +00003063 // EXTRACT_ELEMENT of a constant int is also very common.
3064 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003065 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmaneffb8942008-09-12 16:56:44 +00003066 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohmand8ea0402008-03-24 16:38:05 +00003067 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
3068 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner16713612008-01-22 19:09:33 +00003069 }
3070 break;
David Greeneb6f16112011-01-26 15:38:49 +00003071 case ISD::EXTRACT_SUBVECTOR: {
3072 SDValue Index = N2;
3073 if (VT.isSimple() && N1.getValueType().isSimple()) {
3074 assert(VT.isVector() && N1.getValueType().isVector() &&
3075 "Extract subvector VTs must be a vectors!");
3076 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType() &&
3077 "Extract subvector VTs must have the same element type!");
3078 assert(VT.getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3079 "Extract subvector must be from larger vector to smaller vector!");
3080
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003081 if (isa<ConstantSDNode>(Index.getNode())) {
3082 assert((VT.getVectorNumElements() +
3083 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greeneb6f16112011-01-26 15:38:49 +00003084 <= N1.getValueType().getVectorNumElements())
3085 && "Extract subvector overflow!");
3086 }
3087
3088 // Trivial extraction.
3089 if (VT.getSimpleVT() == N1.getValueType().getSimpleVT())
3090 return N1;
3091 }
Duncan Sandse7b462b2008-02-20 17:38:09 +00003092 break;
Chris Lattner16713612008-01-22 19:09:33 +00003093 }
David Greeneb6f16112011-01-26 15:38:49 +00003094 }
Chris Lattner16713612008-01-22 19:09:33 +00003095
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003096 // Perform trivial constant folding.
3097 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1.getNode(), N2.getNode());
3098 if (SV.getNode()) return SV;
3099
3100 // Canonicalize constant to RHS if commutative.
3101 if (N1C && !N2C && isCommutativeBinOp(Opcode)) {
3102 std::swap(N1C, N2C);
3103 std::swap(N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003104 }
3105
Chris Lattner16713612008-01-22 19:09:33 +00003106 // Constant fold FP operations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003107 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
3108 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner0b6ba902005-07-10 00:07:11 +00003109 if (N1CFP) {
Chris Lattner16713612008-01-22 19:09:33 +00003110 if (!N2CFP && isCommutativeBinOp(Opcode)) {
Benjamin Kramer548ffa22013-02-04 15:19:18 +00003111 // Canonicalize constant to RHS if commutative.
Chris Lattner16713612008-01-22 19:09:33 +00003112 std::swap(N1CFP, N2CFP);
3113 std::swap(N1, N2);
Ulrich Weigand3abb3432012-10-29 18:35:49 +00003114 } else if (N2CFP) {
Dale Johannesen446b9002007-08-31 23:34:27 +00003115 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
3116 APFloat::opStatus s;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003117 switch (Opcode) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00003118 case ISD::FADD:
Dale Johannesen446b9002007-08-31 23:34:27 +00003119 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner16713612008-01-22 19:09:33 +00003120 if (s != APFloat::opInvalidOp)
Dale Johannesen446b9002007-08-31 23:34:27 +00003121 return getConstantFP(V1, VT);
3122 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003123 case ISD::FSUB:
Dale Johannesen446b9002007-08-31 23:34:27 +00003124 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
3125 if (s!=APFloat::opInvalidOp)
3126 return getConstantFP(V1, VT);
3127 break;
3128 case ISD::FMUL:
3129 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
3130 if (s!=APFloat::opInvalidOp)
3131 return getConstantFP(V1, VT);
3132 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003133 case ISD::FDIV:
Dale Johannesen446b9002007-08-31 23:34:27 +00003134 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
3135 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3136 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003137 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00003138 case ISD::FREM :
Dale Johannesen446b9002007-08-31 23:34:27 +00003139 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
3140 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
3141 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003142 break;
Dale Johannesen446b9002007-08-31 23:34:27 +00003143 case ISD::FCOPYSIGN:
3144 V1.copySign(V2);
3145 return getConstantFP(V1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003146 default: break;
3147 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003148 }
Owen Anderson6f1ee162012-04-10 22:46:53 +00003149
3150 if (Opcode == ISD::FP_ROUND) {
3151 APFloat V = N1CFP->getValueAPF(); // make copy
3152 bool ignored;
3153 // This can return overflow, underflow, or inexact; we don't care.
3154 // FIXME need to be more flexible about rounding mode.
Tim Northover29178a32013-01-22 09:46:31 +00003155 (void)V.convert(EVTToAPFloatSemantics(VT),
Owen Anderson6f1ee162012-04-10 22:46:53 +00003156 APFloat::rmNearestTiesToEven, &ignored);
3157 return getConstantFP(V, VT);
3158 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003159 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003160
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003161 // Canonicalize an UNDEF to the RHS, even over a constant.
3162 if (N1.getOpcode() == ISD::UNDEF) {
3163 if (isCommutativeBinOp(Opcode)) {
3164 std::swap(N1, N2);
3165 } else {
3166 switch (Opcode) {
3167 case ISD::FP_ROUND_INREG:
3168 case ISD::SIGN_EXTEND_INREG:
3169 case ISD::SUB:
3170 case ISD::FSUB:
3171 case ISD::FDIV:
3172 case ISD::FREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003173 case ISD::SRA:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003174 return N1; // fold op(undef, arg2) -> undef
3175 case ISD::UDIV:
3176 case ISD::SDIV:
3177 case ISD::UREM:
3178 case ISD::SREM:
Chris Lattner78da6792006-05-08 17:29:49 +00003179 case ISD::SRL:
3180 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003181 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003182 return getConstant(0, VT); // fold op(undef, arg2) -> 0
3183 // For vectors, we can't easily build an all zero vector, just return
3184 // the LHS.
3185 return N2;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003186 }
3187 }
3188 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003189
3190 // Fold a bunch of operators when the RHS is undef.
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003191 if (N2.getOpcode() == ISD::UNDEF) {
3192 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +00003193 case ISD::XOR:
3194 if (N1.getOpcode() == ISD::UNDEF)
3195 // Handle undef ^ undef -> 0 special case. This is a common
3196 // idiom (misuse).
3197 return getConstant(0, VT);
3198 // fallthrough
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003199 case ISD::ADD:
Chris Lattner362621c2007-03-04 20:01:46 +00003200 case ISD::ADDC:
3201 case ISD::ADDE:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003202 case ISD::SUB:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003203 case ISD::UDIV:
3204 case ISD::SDIV:
3205 case ISD::UREM:
3206 case ISD::SREM:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003207 return N2; // fold op(arg1, undef) -> undef
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003208 case ISD::FADD:
3209 case ISD::FSUB:
3210 case ISD::FMUL:
3211 case ISD::FDIV:
3212 case ISD::FREM:
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003213 if (getTarget().Options.UnsafeFPMath)
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00003214 return N2;
3215 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003216 case ISD::MUL:
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003217 case ISD::AND:
Chris Lattner78da6792006-05-08 17:29:49 +00003218 case ISD::SRL:
3219 case ISD::SHL:
Duncan Sands13237ac2008-06-06 12:08:01 +00003220 if (!VT.isVector())
Chris Lattner01a26c72007-04-25 00:00:45 +00003221 return getConstant(0, VT); // fold op(arg1, undef) -> 0
3222 // For vectors, we can't easily build an all zero vector, just return
3223 // the LHS.
3224 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003225 case ISD::OR:
Duncan Sands13237ac2008-06-06 12:08:01 +00003226 if (!VT.isVector())
Duncan Sands3ed76882009-02-01 18:06:53 +00003227 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner01a26c72007-04-25 00:00:45 +00003228 // For vectors, we can't easily build an all one vector, just return
3229 // the LHS.
3230 return N1;
Chris Lattner78da6792006-05-08 17:29:49 +00003231 case ISD::SRA:
3232 return N1;
Chris Lattnerbc1b2622006-04-20 05:39:12 +00003233 }
3234 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00003235
Chris Lattner724f7ee2005-05-11 18:57:39 +00003236 // Memoize this node if possible.
3237 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003238 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003239 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003240 SDValue Ops[] = { N1, N2 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003241 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003242 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003243 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003244 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003245 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003246
Andrew Trickef9de2a2013-05-25 02:42:55 +00003247 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003248 CSEMap.InsertNode(N, IP);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003249 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003250 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00003251 }
3252
Chris Lattner061a1ea2005-01-07 07:46:32 +00003253 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003254#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003255 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003256#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003257 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003258}
3259
Andrew Trickef9de2a2013-05-25 02:42:55 +00003260SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003261 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00003262 // Perform various simplifications.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003263 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Chris Lattner061a1ea2005-01-07 07:46:32 +00003264 switch (Opcode) {
Owen Anderson32baf992013-05-09 22:27:13 +00003265 case ISD::FMA: {
3266 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3267 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
3268 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
3269 if (N1CFP && N2CFP && N3CFP) {
3270 APFloat V1 = N1CFP->getValueAPF();
3271 const APFloat &V2 = N2CFP->getValueAPF();
3272 const APFloat &V3 = N3CFP->getValueAPF();
3273 APFloat::opStatus s =
3274 V1.fusedMultiplyAdd(V2, V3, APFloat::rmNearestTiesToEven);
3275 if (s != APFloat::opInvalidOp)
3276 return getConstantFP(V1, VT);
3277 }
3278 break;
3279 }
Dan Gohman550c9af2008-08-14 20:04:46 +00003280 case ISD::CONCAT_VECTORS:
3281 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
3282 // one big BUILD_VECTOR.
3283 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
3284 N2.getOpcode() == ISD::BUILD_VECTOR &&
3285 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif59f99702010-07-22 17:18:03 +00003286 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
3287 N1.getNode()->op_end());
Dan Gohmandd41bba2010-06-21 19:47:52 +00003288 Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
3289 Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga49de9d2009-02-25 22:49:59 +00003290 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman550c9af2008-08-14 20:04:46 +00003291 }
3292 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003293 case ISD::SETCC: {
Chris Lattnerbd9acad2006-10-14 00:41:01 +00003294 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenf1163e92009-02-03 00:47:48 +00003295 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003296 if (Simp.getNode()) return Simp;
Chris Lattnerd47675e2005-08-09 20:20:18 +00003297 break;
3298 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003299 case ISD::SELECT:
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003300 if (N1C) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003301 if (N1C->getZExtValue())
David Blaikie46a9f012012-01-20 21:51:11 +00003302 return N2; // select true, X, Y -> X
3303 return N3; // select false, X, Y -> Y
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00003304 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00003305
3306 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00003307 break;
Chris Lattner00f05892006-03-19 23:56:04 +00003308 case ISD::VECTOR_SHUFFLE:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003309 llvm_unreachable("should use getVectorShuffle constructor!");
David Greenebab5e6e2011-01-26 19:13:22 +00003310 case ISD::INSERT_SUBVECTOR: {
3311 SDValue Index = N3;
3312 if (VT.isSimple() && N1.getValueType().isSimple()
3313 && N2.getValueType().isSimple()) {
3314 assert(VT.isVector() && N1.getValueType().isVector() &&
3315 N2.getValueType().isVector() &&
3316 "Insert subvector VTs must be a vectors");
3317 assert(VT == N1.getValueType() &&
3318 "Dest and insert subvector source types must match!");
3319 assert(N2.getValueType().getSimpleVT() <= N1.getValueType().getSimpleVT() &&
3320 "Insert subvector must be from smaller vector to larger vector!");
Matt Beaumont-Gay29c8c8f2011-02-01 22:12:50 +00003321 if (isa<ConstantSDNode>(Index.getNode())) {
3322 assert((N2.getValueType().getVectorNumElements() +
3323 cast<ConstantSDNode>(Index.getNode())->getZExtValue()
David Greenebab5e6e2011-01-26 19:13:22 +00003324 <= VT.getVectorNumElements())
3325 && "Insert subvector overflow!");
3326 }
3327
3328 // Trivial insertion.
3329 if (VT.getSimpleVT() == N2.getValueType().getSimpleVT())
3330 return N2;
3331 }
3332 break;
3333 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003334 case ISD::BITCAST:
Dan Gohmana8665142007-06-25 16:23:39 +00003335 // Fold bit_convert nodes from a type to themselves.
3336 if (N1.getValueType() == VT)
3337 return N1;
Chris Lattnera77cb3c2007-04-12 05:58:43 +00003338 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00003339 }
3340
Chris Lattnerf9c19152005-08-25 19:12:10 +00003341 // Memoize node if it doesn't produce a flag.
3342 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00003343 SDVTList VTs = getVTList(VT);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003344 if (VT != MVT::Glue) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003345 SDValue Ops[] = { N1, N2, N3 };
Jim Laskeyf576b422006-10-27 23:46:08 +00003346 FoldingSetNodeID ID;
Chris Lattnerf17b4222007-02-04 07:28:00 +00003347 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003348 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00003349 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003350 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00003351
Andrew Trickef9de2a2013-05-25 02:42:55 +00003352 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00003353 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003354 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003355 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, N1, N2, N3);
Chris Lattnerf9c19152005-08-25 19:12:10 +00003356 }
Daniel Dunbarb827e522009-12-16 20:10:05 +00003357
Chris Lattner061a1ea2005-01-07 07:46:32 +00003358 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003359#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00003360 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00003361#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003362 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00003363}
3364
Andrew Trickef9de2a2013-05-25 02:42:55 +00003365SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003366 SDValue N1, SDValue N2, SDValue N3,
3367 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003368 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003369 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003370}
3371
Andrew Trickef9de2a2013-05-25 02:42:55 +00003372SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003373 SDValue N1, SDValue N2, SDValue N3,
3374 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003375 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00003376 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner36db1ed2005-07-10 00:29:18 +00003377}
3378
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003379/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
3380/// the incoming stack arguments to be loaded from the stack.
3381SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
3382 SmallVector<SDValue, 8> ArgChains;
3383
3384 // Include the original chain at the beginning of the list. When this is
3385 // used by target LowerCall hooks, this helps legalize find the
3386 // CALLSEQ_BEGIN node.
3387 ArgChains.push_back(Chain);
3388
3389 // Add a chain value for each stack argument.
3390 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
3391 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
3392 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
3393 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
3394 if (FI->getIndex() < 0)
3395 ArgChains.push_back(SDValue(L, 1));
3396
3397 // Build a tokenfactor for all the chains.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003398 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003399 &ArgChains[0], ArgChains.size());
3400}
3401
Dan Gohman544ab2c2008-04-12 04:36:06 +00003402/// getMemsetValue - Vectorized representation of the memset value
3403/// operand.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003404static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003405 SDLoc dl) {
Evan Cheng61399372010-04-02 19:36:14 +00003406 assert(Value.getOpcode() != ISD::UNDEF);
3407
Chris Lattnerd3aa3aa2010-02-24 22:44:06 +00003408 unsigned NumBits = VT.getScalarType().getSizeInBits();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003409 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003410 assert(C->getAPIntValue().getBitWidth() == 8);
3411 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
Duncan Sands13237ac2008-06-06 12:08:01 +00003412 if (VT.isInteger())
Evan Chengef377ad2008-05-15 08:39:06 +00003413 return DAG.getConstant(Val, VT);
Tim Northover29178a32013-01-22 09:46:31 +00003414 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003415 }
Evan Chengef377ad2008-05-15 08:39:06 +00003416
Dale Johannesenabf66b82009-02-03 22:26:09 +00003417 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003418 if (NumBits > 8) {
3419 // Use a multiplication with 0x010101... to extend the input to the
3420 // required length.
Benjamin Kramer5c3e21b2013-02-20 13:00:06 +00003421 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
Benjamin Kramer2fdea4c2011-01-02 19:44:58 +00003422 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
Evan Chengef377ad2008-05-15 08:39:06 +00003423 }
3424
3425 return Value;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003426}
3427
Dan Gohman544ab2c2008-04-12 04:36:06 +00003428/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3429/// used when a memcpy is turned into a memset when the source is a constant
3430/// string ptr.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003431static SDValue getMemsetStringVal(EVT VT, SDLoc dl, SelectionDAG &DAG,
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003432 const TargetLowering &TLI, StringRef Str) {
Evan Chengda3db112008-06-30 07:31:25 +00003433 // Handle vector with all elements zero.
3434 if (Str.empty()) {
3435 if (VT.isInteger())
3436 return DAG.getConstant(0, VT);
Duncan Sands14627772010-11-03 12:17:33 +00003437 else if (VT == MVT::f32 || VT == MVT::f64)
Evan Cheng43cd9e32010-04-01 06:04:33 +00003438 return DAG.getConstantFP(0.0, VT);
3439 else if (VT.isVector()) {
3440 unsigned NumElts = VT.getVectorNumElements();
3441 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Wesley Peck527da1b2010-11-23 03:31:01 +00003442 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003443 DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
3444 EltVT, NumElts)));
3445 } else
3446 llvm_unreachable("Expected type!");
Evan Chengda3db112008-06-30 07:31:25 +00003447 }
3448
Duncan Sands13237ac2008-06-06 12:08:01 +00003449 assert(!VT.isVector() && "Can't handle vector type here!");
Evan Chengc8444b12013-01-10 22:13:27 +00003450 unsigned NumVTBits = VT.getSizeInBits();
3451 unsigned NumVTBytes = NumVTBits / 8;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003452 unsigned NumBytes = std::min(NumVTBytes, unsigned(Str.size()));
3453
Evan Chengc8444b12013-01-10 22:13:27 +00003454 APInt Val(NumVTBits, 0);
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003455 if (TLI.isLittleEndian()) {
3456 for (unsigned i = 0; i != NumBytes; ++i)
3457 Val |= (uint64_t)(unsigned char)Str[i] << i*8;
3458 } else {
3459 for (unsigned i = 0; i != NumBytes; ++i)
3460 Val |= (uint64_t)(unsigned char)Str[i] << (NumVTBytes-i-1)*8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003461 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003462
Evan Chengeb542402012-12-11 23:26:14 +00003463 // If the "cost" of materializing the integer immediate is 1 or free, then
3464 // it is cost effective to turn the load into the immediate.
Chandler Carruth42e96112013-01-05 12:32:17 +00003465 const TargetTransformInfo *TTI = DAG.getTargetTransformInfo();
3466 if (TTI->getIntImmCost(Val, VT.getTypeForEVT(*DAG.getContext())) < 2)
Evan Cheng79e2ca92012-12-10 23:21:26 +00003467 return DAG.getConstant(Val, VT);
3468 return SDValue(0, 0);
Rafael Espindola846c19dd2007-10-19 10:41:11 +00003469}
3470
Scott Michelcf0da6c2009-02-17 22:15:04 +00003471/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengef377ad2008-05-15 08:39:06 +00003472///
Andrew Tricke2431c62013-05-25 03:08:10 +00003473static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset, SDLoc dl,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003474 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003475 EVT VT = Base.getValueType();
Andrew Tricke2431c62013-05-25 03:08:10 +00003476 return DAG.getNode(ISD::ADD, dl,
Dale Johannesendb393622009-02-03 01:55:44 +00003477 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman544ab2c2008-04-12 04:36:06 +00003478}
3479
Evan Chengef377ad2008-05-15 08:39:06 +00003480/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3481///
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003482static bool isMemSrcFromString(SDValue Src, StringRef &Str) {
Evan Chengef377ad2008-05-15 08:39:06 +00003483 unsigned SrcDelta = 0;
3484 GlobalAddressSDNode *G = NULL;
3485 if (Src.getOpcode() == ISD::GlobalAddress)
3486 G = cast<GlobalAddressSDNode>(Src);
3487 else if (Src.getOpcode() == ISD::ADD &&
3488 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3489 Src.getOperand(1).getOpcode() == ISD::Constant) {
3490 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmaneffb8942008-09-12 16:56:44 +00003491 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengef377ad2008-05-15 08:39:06 +00003492 }
3493 if (!G)
3494 return false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003495
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003496 return getConstantStringInfo(G->getGlobal(), Str, SrcDelta, false);
Evan Chengef377ad2008-05-15 08:39:06 +00003497}
Dan Gohman544ab2c2008-04-12 04:36:06 +00003498
Evan Cheng43cd9e32010-04-01 06:04:33 +00003499/// FindOptimalMemOpLowering - Determines the optimial series memory ops
3500/// to replace the memset / memcpy. Return true if the number of memory ops
3501/// is below the threshold. It returns the types of the sequence of
3502/// memory ops to perform memset / memcpy by reference.
3503static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003504 unsigned Limit, uint64_t Size,
3505 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003506 bool IsMemset,
3507 bool ZeroMemset,
Evan Chengebe47c82010-04-08 07:37:57 +00003508 bool MemcpyStrSrc,
Evan Cheng79e2ca92012-12-10 23:21:26 +00003509 bool AllowOverlap,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003510 SelectionDAG &DAG,
3511 const TargetLowering &TLI) {
3512 assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
3513 "Expecting memcpy / memset source to meet alignment requirement!");
Eric Christopherea336c72011-07-06 22:41:18 +00003514 // If 'SrcAlign' is zero, that means the memory operation does not need to
3515 // load the value, i.e. memset or memcpy from constant string. Otherwise,
3516 // it's the inferred alignment of the source. 'DstAlign', on the other hand,
3517 // is the specified alignment of the memory operation. If it is zero, that
3518 // means it's possible to change the alignment of the destination.
3519 // 'MemcpyStrSrc' indicates whether the memcpy source is constant so it does
3520 // not need to be loaded.
Evan Cheng61399372010-04-02 19:36:14 +00003521 EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00003522 IsMemset, ZeroMemset, MemcpyStrSrc,
Dan Gohman4d273f42010-04-16 20:22:43 +00003523 DAG.getMachineFunction());
Evan Chengef377ad2008-05-15 08:39:06 +00003524
Chris Lattner28dc6c12010-03-07 07:45:08 +00003525 if (VT == MVT::Other) {
Chandler Carruth5da3f052012-11-01 09:14:31 +00003526 if (DstAlign >= TLI.getDataLayout()->getPointerPrefAlignment() ||
Evan Cheng43cd9e32010-04-01 06:04:33 +00003527 TLI.allowsUnalignedMemoryAccesses(VT)) {
Evan Cheng272a2f82010-04-05 23:33:29 +00003528 VT = TLI.getPointerTy();
Evan Chengef377ad2008-05-15 08:39:06 +00003529 } else {
Evan Cheng43cd9e32010-04-01 06:04:33 +00003530 switch (DstAlign & 7) {
Owen Anderson9f944592009-08-11 20:47:22 +00003531 case 0: VT = MVT::i64; break;
3532 case 4: VT = MVT::i32; break;
3533 case 2: VT = MVT::i16; break;
3534 default: VT = MVT::i8; break;
Evan Chengef377ad2008-05-15 08:39:06 +00003535 }
3536 }
3537
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003538 MVT LVT = MVT::i64;
Evan Chengef377ad2008-05-15 08:39:06 +00003539 while (!TLI.isTypeLegal(LVT))
Owen Andersonc6daf8f2009-08-11 21:59:30 +00003540 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands13237ac2008-06-06 12:08:01 +00003541 assert(LVT.isInteger());
Evan Chengef377ad2008-05-15 08:39:06 +00003542
Duncan Sands11dd4242008-06-08 20:54:56 +00003543 if (VT.bitsGT(LVT))
Evan Chengef377ad2008-05-15 08:39:06 +00003544 VT = LVT;
3545 }
Wesley Peck527da1b2010-11-23 03:31:01 +00003546
Dan Gohman544ab2c2008-04-12 04:36:06 +00003547 unsigned NumMemOps = 0;
3548 while (Size != 0) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003549 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003550 while (VTSize > Size) {
Evan Chengef377ad2008-05-15 08:39:06 +00003551 // For now, only use non-vector load / store's for the left-over pieces.
Evan Cheng04e55182012-12-12 00:42:09 +00003552 EVT NewVT = VT;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003553 unsigned NewVTSize;
Evan Cheng04e55182012-12-12 00:42:09 +00003554
3555 bool Found = false;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003556 if (VT.isVector() || VT.isFloatingPoint()) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00003557 NewVT = (VT.getSizeInBits() > 64) ? MVT::i64 : MVT::i32;
Evan Cheng04e55182012-12-12 00:42:09 +00003558 if (TLI.isOperationLegalOrCustom(ISD::STORE, NewVT) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003559 TLI.isSafeMemOpType(NewVT.getSimpleVT()))
Evan Cheng04e55182012-12-12 00:42:09 +00003560 Found = true;
3561 else if (NewVT == MVT::i64 &&
3562 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::f64) &&
Evan Chengc3d1aca2012-12-12 01:32:07 +00003563 TLI.isSafeMemOpType(MVT::f64)) {
Evan Cheng04e55182012-12-12 00:42:09 +00003564 // i64 is usually not legal on 32-bit targets, but f64 may be.
3565 NewVT = MVT::f64;
3566 Found = true;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003567 }
Evan Cheng79e2ca92012-12-10 23:21:26 +00003568 }
3569
Evan Cheng04e55182012-12-12 00:42:09 +00003570 if (!Found) {
3571 do {
3572 NewVT = (MVT::SimpleValueType)(NewVT.getSimpleVT().SimpleTy - 1);
3573 if (NewVT == MVT::i8)
3574 break;
Evan Chengc3d1aca2012-12-12 01:32:07 +00003575 } while (!TLI.isSafeMemOpType(NewVT.getSimpleVT()));
Evan Cheng04e55182012-12-12 00:42:09 +00003576 }
3577 NewVTSize = NewVT.getSizeInBits() / 8;
3578
Evan Cheng79e2ca92012-12-10 23:21:26 +00003579 // If the new VT cannot cover all of the remaining bits, then consider
3580 // issuing a (or a pair of) unaligned and overlapping load / store.
3581 // FIXME: Only does this for 64-bit or more since we don't have proper
3582 // cost model for unaligned load / store.
3583 bool Fast;
Evan Chengb7d3d032012-12-12 20:43:23 +00003584 if (NumMemOps && AllowOverlap &&
3585 VTSize >= 8 && NewVTSize < Size &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00003586 TLI.allowsUnalignedMemoryAccesses(VT, &Fast) && Fast)
3587 VTSize = Size;
3588 else {
3589 VT = NewVT;
3590 VTSize = NewVTSize;
Evan Chengef377ad2008-05-15 08:39:06 +00003591 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003592 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003593
Evan Chengb7d3d032012-12-12 20:43:23 +00003594 if (++NumMemOps > Limit)
3595 return false;
3596
Dan Gohman544ab2c2008-04-12 04:36:06 +00003597 MemOps.push_back(VT);
3598 Size -= VTSize;
3599 }
3600
3601 return true;
3602}
3603
Andrew Trickef9de2a2013-05-25 02:42:55 +00003604static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng85eea4e2010-03-30 18:08:53 +00003605 SDValue Chain, SDValue Dst,
3606 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003607 unsigned Align, bool isVol,
3608 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003609 MachinePointerInfo DstPtrInfo,
3610 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003611 // Turn a memcpy of undef to nop.
3612 if (Src.getOpcode() == ISD::UNDEF)
3613 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003614
Dan Gohman714663a2008-05-29 19:42:22 +00003615 // Expand memcpy to a series of load and store ops if the size operand falls
3616 // below a certain threshold.
Duncan Sands6c25ca42010-11-05 15:20:29 +00003617 // TODO: In the AlwaysInline case, if the size is big then generate a loop
3618 // rather than maybe a humongous number of loads and stores.
Evan Cheng61399372010-04-02 19:36:14 +00003619 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003620 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003621 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003622 MachineFunction &MF = DAG.getMachineFunction();
3623 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingc9b22d72012-10-09 07:45:08 +00003624 bool OptSize =
Bill Wendling698e84f2012-12-30 10:32:01 +00003625 MF.getFunction()->getAttributes().
3626 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003627 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3628 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3629 DstAlignCanChange = true;
3630 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3631 if (Align > SrcAlign)
3632 SrcAlign = Align;
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003633 StringRef Str;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003634 bool CopyFromStr = isMemSrcFromString(Src, Str);
3635 bool isZeroStr = CopyFromStr && Str.empty();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003636 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
Wesley Peck527da1b2010-11-23 03:31:01 +00003637
Evan Cheng4c014c82010-04-01 18:19:11 +00003638 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003639 (DstAlignCanChange ? 0 : Align),
Evan Chengebe47c82010-04-08 07:37:57 +00003640 (isZeroStr ? 0 : SrcAlign),
Evan Cheng962711e2012-12-12 02:34:41 +00003641 false, false, CopyFromStr, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003642 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003643
Evan Cheng43cd9e32010-04-01 06:04:33 +00003644 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003645 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003646 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Lang Hamesdd478042013-01-31 20:23:43 +00003647
3648 // Don't promote to an alignment that would require dynamic stack
Stephen Lincfe7f352013-07-08 00:37:03 +00003649 // realignment.
Lang Hamesdd478042013-01-31 20:23:43 +00003650 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
3651 if (!TRI->needsStackRealignment(MF))
3652 while (NewAlign > Align &&
3653 TLI.getDataLayout()->exceedsNaturalStackAlignment(NewAlign))
3654 NewAlign /= 2;
3655
Evan Cheng43cd9e32010-04-01 06:04:33 +00003656 if (NewAlign > Align) {
3657 // Give the stack frame object a larger alignment if needed.
3658 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3659 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3660 Align = NewAlign;
3661 }
3662 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003663
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003664 SmallVector<SDValue, 8> OutChains;
Evan Chengef377ad2008-05-15 08:39:06 +00003665 unsigned NumMemOps = MemOps.size();
Evan Chengda3db112008-06-30 07:31:25 +00003666 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattnerbb1a1bd2009-09-20 17:32:21 +00003667 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003668 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003669 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003670 SDValue Value, Store;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003671
Evan Cheng79e2ca92012-12-10 23:21:26 +00003672 if (VTSize > Size) {
3673 // Issuing an unaligned load / store pair that overlaps with the previous
3674 // pair. Adjust the offset accordingly.
3675 assert(i == NumMemOps-1 && i != 0);
3676 SrcOff -= VTSize - Size;
3677 DstOff -= VTSize - Size;
3678 }
3679
Evan Cheng43cd9e32010-04-01 06:04:33 +00003680 if (CopyFromStr &&
3681 (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
Evan Chengef377ad2008-05-15 08:39:06 +00003682 // It's unlikely a store of a vector immediate can be done in a single
3683 // instruction. It would require a load from a constantpool first.
Evan Cheng43cd9e32010-04-01 06:04:33 +00003684 // We only handle zero vectors here.
Evan Chengda3db112008-06-30 07:31:25 +00003685 // FIXME: Handle other cases where store of vector immediate is done in
3686 // a single instruction.
Chris Lattnercf9e8f62012-02-05 02:29:43 +00003687 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str.substr(SrcOff));
Evan Cheng79e2ca92012-12-10 23:21:26 +00003688 if (Value.getNode())
3689 Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003690 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Evan Cheng79e2ca92012-12-10 23:21:26 +00003691 DstPtrInfo.getWithOffset(DstOff), isVol,
3692 false, Align);
3693 }
3694
3695 if (!Store.getNode()) {
Dale Johannesen315fb722009-06-22 20:59:07 +00003696 // The type might not be legal for the target. This should only happen
3697 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen92c11e92009-06-24 17:11:31 +00003698 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3699 // to Load/Store if NVT==VT.
Dale Johannesen315fb722009-06-22 20:59:07 +00003700 // FIXME does the case above also need this?
Owen Anderson117c9e82009-08-12 00:36:31 +00003701 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen92c11e92009-06-24 17:11:31 +00003702 assert(NVT.bitsGE(VT));
Stuart Hastings81c43062011-02-16 16:23:55 +00003703 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003704 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003705 SrcPtrInfo.getWithOffset(SrcOff), VT, isVol, false,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003706 MinAlign(SrcAlign, SrcOff));
Dale Johannesen92c11e92009-06-24 17:11:31 +00003707 Store = DAG.getTruncStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003708 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003709 DstPtrInfo.getWithOffset(DstOff), VT, isVol,
3710 false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003711 }
3712 OutChains.push_back(Store);
3713 SrcOff += VTSize;
3714 DstOff += VTSize;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003715 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003716 }
3717
Owen Anderson9f944592009-08-11 20:47:22 +00003718 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003719 &OutChains[0], OutChains.size());
3720}
3721
Andrew Trickef9de2a2013-05-25 02:42:55 +00003722static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003723 SDValue Chain, SDValue Dst,
3724 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003725 unsigned Align, bool isVol,
3726 bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003727 MachinePointerInfo DstPtrInfo,
3728 MachinePointerInfo SrcPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003729 // Turn a memmove of undef to nop.
3730 if (Src.getOpcode() == ISD::UNDEF)
3731 return Chain;
Dan Gohman714663a2008-05-29 19:42:22 +00003732
3733 // Expand memmove to a series of load and store ops if the size operand falls
3734 // below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003735 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003736 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003737 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003738 MachineFunction &MF = DAG.getMachineFunction();
3739 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003740 bool OptSize = MF.getFunction()->getAttributes().
3741 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003742 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3743 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3744 DstAlignCanChange = true;
3745 unsigned SrcAlign = DAG.InferPtrAlignment(Src);
3746 if (Align > SrcAlign)
3747 SrcAlign = Align;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003748 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003749
Evan Cheng4c014c82010-04-01 18:19:11 +00003750 if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
Evan Cheng962711e2012-12-12 02:34:41 +00003751 (DstAlignCanChange ? 0 : Align), SrcAlign,
3752 false, false, false, false, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003753 return SDValue();
Dan Gohman714663a2008-05-29 19:42:22 +00003754
Evan Cheng43cd9e32010-04-01 06:04:33 +00003755 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003756 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003757 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003758 if (NewAlign > Align) {
3759 // Give the stack frame object a larger alignment if needed.
3760 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3761 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3762 Align = NewAlign;
3763 }
3764 }
Dan Gohman714663a2008-05-29 19:42:22 +00003765
Evan Cheng43cd9e32010-04-01 06:04:33 +00003766 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003767 SmallVector<SDValue, 8> LoadValues;
3768 SmallVector<SDValue, 8> LoadChains;
3769 SmallVector<SDValue, 8> OutChains;
Dan Gohman714663a2008-05-29 19:42:22 +00003770 unsigned NumMemOps = MemOps.size();
3771 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003772 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003773 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003774 SDValue Value, Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003775
Dale Johannesenabf66b82009-02-03 22:26:09 +00003776 Value = DAG.getLoad(VT, dl, Chain,
Andrew Tricke2431c62013-05-25 03:08:10 +00003777 getMemBasePlusOffset(Src, SrcOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003778 SrcPtrInfo.getWithOffset(SrcOff), isVol,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003779 false, false, SrcAlign);
Dan Gohman714663a2008-05-29 19:42:22 +00003780 LoadValues.push_back(Value);
3781 LoadChains.push_back(Value.getValue(1));
3782 SrcOff += VTSize;
3783 }
Owen Anderson9f944592009-08-11 20:47:22 +00003784 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003785 &LoadChains[0], LoadChains.size());
3786 OutChains.clear();
3787 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003788 EVT VT = MemOps[i];
Duncan Sands13237ac2008-06-06 12:08:01 +00003789 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003790 SDValue Value, Store;
Dan Gohman714663a2008-05-29 19:42:22 +00003791
Dale Johannesenabf66b82009-02-03 22:26:09 +00003792 Store = DAG.getStore(Chain, dl, LoadValues[i],
Andrew Tricke2431c62013-05-25 03:08:10 +00003793 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003794 DstPtrInfo.getWithOffset(DstOff), isVol, false, Align);
Dan Gohman714663a2008-05-29 19:42:22 +00003795 OutChains.push_back(Store);
3796 DstOff += VTSize;
3797 }
3798
Owen Anderson9f944592009-08-11 20:47:22 +00003799 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman714663a2008-05-29 19:42:22 +00003800 &OutChains[0], OutChains.size());
3801}
3802
Andrew Trickef9de2a2013-05-25 02:42:55 +00003803static SDValue getMemsetStores(SelectionDAG &DAG, SDLoc dl,
Evan Cheng43cd9e32010-04-01 06:04:33 +00003804 SDValue Chain, SDValue Dst,
3805 SDValue Src, uint64_t Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003806 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003807 MachinePointerInfo DstPtrInfo) {
Evan Cheng61399372010-04-02 19:36:14 +00003808 // Turn a memset of undef to nop.
3809 if (Src.getOpcode() == ISD::UNDEF)
3810 return Chain;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003811
3812 // Expand memset to a series of load/store ops if the size operand
3813 // falls below a certain threshold.
Evan Cheng61399372010-04-02 19:36:14 +00003814 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Anderson53aa7a92009-08-10 22:56:29 +00003815 std::vector<EVT> MemOps;
Evan Cheng43cd9e32010-04-01 06:04:33 +00003816 bool DstAlignCanChange = false;
Evan Cheng3ae2b792011-01-06 06:52:41 +00003817 MachineFunction &MF = DAG.getMachineFunction();
3818 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendling698e84f2012-12-30 10:32:01 +00003819 bool OptSize = MF.getFunction()->getAttributes().
3820 hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003821 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
3822 if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
3823 DstAlignCanChange = true;
Lang Hames58dba012011-10-26 23:50:43 +00003824 bool IsZeroVal =
Evan Cheng61399372010-04-02 19:36:14 +00003825 isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
Evan Cheng3ae2b792011-01-06 06:52:41 +00003826 if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(OptSize),
Evan Cheng43cd9e32010-04-01 06:04:33 +00003827 Size, (DstAlignCanChange ? 0 : Align), 0,
Evan Cheng962711e2012-12-12 02:34:41 +00003828 true, IsZeroVal, false, true, DAG, TLI))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003829 return SDValue();
Dan Gohman544ab2c2008-04-12 04:36:06 +00003830
Evan Cheng43cd9e32010-04-01 06:04:33 +00003831 if (DstAlignCanChange) {
Chris Lattner229907c2011-07-18 04:54:35 +00003832 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00003833 unsigned NewAlign = (unsigned) TLI.getDataLayout()->getABITypeAlignment(Ty);
Evan Cheng43cd9e32010-04-01 06:04:33 +00003834 if (NewAlign > Align) {
3835 // Give the stack frame object a larger alignment if needed.
3836 if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
3837 MFI->setObjectAlignment(FI->getIndex(), NewAlign);
3838 Align = NewAlign;
3839 }
3840 }
3841
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003842 SmallVector<SDValue, 8> OutChains;
Dan Gohmanda440542008-04-28 17:15:20 +00003843 uint64_t DstOff = 0;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003844 unsigned NumMemOps = MemOps.size();
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003845
3846 // Find the largest store and generate the bit pattern for it.
3847 EVT LargestVT = MemOps[0];
3848 for (unsigned i = 1; i < NumMemOps; i++)
3849 if (MemOps[i].bitsGT(LargestVT))
3850 LargestVT = MemOps[i];
3851 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
3852
Dan Gohman544ab2c2008-04-12 04:36:06 +00003853 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003854 EVT VT = MemOps[i];
Evan Cheng79e2ca92012-12-10 23:21:26 +00003855 unsigned VTSize = VT.getSizeInBits() / 8;
3856 if (VTSize > Size) {
3857 // Issuing an unaligned load / store pair that overlaps with the previous
3858 // pair. Adjust the offset accordingly.
3859 assert(i == NumMemOps-1 && i != 0);
3860 DstOff -= VTSize - Size;
3861 }
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003862
3863 // If this store is smaller than the largest store see whether we can get
3864 // the smaller value for free with a truncate.
3865 SDValue Value = MemSetValue;
3866 if (VT.bitsLT(LargestVT)) {
3867 if (!LargestVT.isVector() && !VT.isVector() &&
3868 TLI.isTruncateFree(LargestVT, VT))
3869 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
3870 else
3871 Value = getMemsetValue(Src, VT, DAG, dl);
3872 }
3873 assert(Value.getValueType() == VT && "Value with wrong type.");
Dale Johannesenabf66b82009-02-03 22:26:09 +00003874 SDValue Store = DAG.getStore(Chain, dl, Value,
Andrew Tricke2431c62013-05-25 03:08:10 +00003875 getMemBasePlusOffset(Dst, DstOff, dl, DAG),
Chris Lattner2510de22010-09-21 05:40:29 +00003876 DstPtrInfo.getWithOffset(DstOff),
Dale Johannesened0d8402010-11-18 01:35:23 +00003877 isVol, false, Align);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003878 OutChains.push_back(Store);
Benjamin Kramer25e6e062011-01-02 19:57:05 +00003879 DstOff += VT.getSizeInBits() / 8;
Evan Cheng79e2ca92012-12-10 23:21:26 +00003880 Size -= VTSize;
Dan Gohman544ab2c2008-04-12 04:36:06 +00003881 }
3882
Owen Anderson9f944592009-08-11 20:47:22 +00003883 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman544ab2c2008-04-12 04:36:06 +00003884 &OutChains[0], OutChains.size());
3885}
3886
Andrew Trickef9de2a2013-05-25 02:42:55 +00003887SDValue SelectionDAG::getMemcpy(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003888 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003889 unsigned Align, bool isVol, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003890 MachinePointerInfo DstPtrInfo,
3891 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00003892 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00003893
3894 // Check to see if we should lower the memcpy to loads and stores first.
3895 // For cases within the target-specified limits, this is the best choice.
3896 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3897 if (ConstantSize) {
3898 // Memcpy with size zero? Just return the original chain.
3899 if (ConstantSize->isNullValue())
3900 return Chain;
3901
Evan Cheng43cd9e32010-04-01 06:04:33 +00003902 SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
3903 ConstantSize->getZExtValue(),Align,
Chris Lattner2510de22010-09-21 05:40:29 +00003904 isVol, false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003905 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00003906 return Result;
3907 }
3908
3909 // Then check to see if we should lower the memcpy with target-specific
3910 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003911 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00003912 TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Mon P Wangc576ee92010-04-04 03:10:48 +00003913 isVol, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +00003914 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003915 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00003916 return Result;
3917
3918 // If we really need inline code and the target declined to provide it,
3919 // use a (potentially long) sequence of loads and stores.
3920 if (AlwaysInline) {
3921 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesenabf66b82009-02-03 22:26:09 +00003922 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00003923 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003924 true, DstPtrInfo, SrcPtrInfo);
Dan Gohman544ab2c2008-04-12 04:36:06 +00003925 }
3926
Dan Gohmanf38547c2010-04-05 20:24:08 +00003927 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
3928 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
3929 // respect volatile, so they may do things like read or write memory
3930 // beyond the given memory regions. But fixing this isn't easy, and most
3931 // people don't care.
3932
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003933 const TargetLowering *TLI = TM.getTargetLowering();
3934
Dan Gohman544ab2c2008-04-12 04:36:06 +00003935 // Emit a library call.
3936 TargetLowering::ArgListTy Args;
3937 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003938 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00003939 Entry.Node = Dst; Args.push_back(Entry);
3940 Entry.Node = Src; Args.push_back(Entry);
3941 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003942 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00003943 TargetLowering::
3944 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00003945 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003946 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
Evan Cheng65f9d192012-02-28 18:51:51 +00003947 /*isTailCall=*/false,
3948 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003949 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
3950 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00003951 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003952 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00003953
Dan Gohman544ab2c2008-04-12 04:36:06 +00003954 return CallResult.second;
3955}
3956
Andrew Trickef9de2a2013-05-25 02:42:55 +00003957SDValue SelectionDAG::getMemmove(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003958 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00003959 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003960 MachinePointerInfo DstPtrInfo,
3961 MachinePointerInfo SrcPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00003962 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00003963
Dan Gohman714663a2008-05-29 19:42:22 +00003964 // Check to see if we should lower the memmove to loads and stores first.
3965 // For cases within the target-specified limits, this is the best choice.
3966 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3967 if (ConstantSize) {
3968 // Memmove with size zero? Just return the original chain.
3969 if (ConstantSize->isNullValue())
3970 return Chain;
3971
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003972 SDValue Result =
Dale Johannesenabf66b82009-02-03 22:26:09 +00003973 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Mon P Wangc576ee92010-04-04 03:10:48 +00003974 ConstantSize->getZExtValue(), Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003975 false, DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003976 if (Result.getNode())
Dan Gohman714663a2008-05-29 19:42:22 +00003977 return Result;
3978 }
Dan Gohman544ab2c2008-04-12 04:36:06 +00003979
3980 // Then check to see if we should lower the memmove with target-specific
3981 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003982 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00003983 TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00003984 DstPtrInfo, SrcPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003985 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00003986 return Result;
3987
Mon P Wangbf862242010-04-06 08:27:51 +00003988 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
3989 // not be safe. See memcpy above for more details.
3990
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003991 const TargetLowering *TLI = TM.getTargetLowering();
3992
Dan Gohman544ab2c2008-04-12 04:36:06 +00003993 // Emit a library call.
3994 TargetLowering::ArgListTy Args;
3995 TargetLowering::ArgListEntry Entry;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00003996 Entry.Ty = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00003997 Entry.Node = Dst; Args.push_back(Entry);
3998 Entry.Node = Src; Args.push_back(Entry);
3999 Entry.Node = Size; Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004000 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004001 TargetLowering::
4002 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004003 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004004 TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
Evan Cheng65f9d192012-02-28 18:51:51 +00004005 /*isTailCall=*/false,
4006 /*doesNotReturn=*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004007 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
4008 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004009 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004010 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004011
Dan Gohman544ab2c2008-04-12 04:36:06 +00004012 return CallResult.second;
4013}
4014
Andrew Trickef9de2a2013-05-25 02:42:55 +00004015SDValue SelectionDAG::getMemset(SDValue Chain, SDLoc dl, SDValue Dst,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004016 SDValue Src, SDValue Size,
Mon P Wangc576ee92010-04-04 03:10:48 +00004017 unsigned Align, bool isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004018 MachinePointerInfo DstPtrInfo) {
Chandler Carruth121dbf82013-02-25 14:29:38 +00004019 assert(Align && "The SDAG layer expects explicit alignment and reserves 0");
Dan Gohman544ab2c2008-04-12 04:36:06 +00004020
4021 // Check to see if we should lower the memset to stores first.
4022 // For cases within the target-specified limits, this is the best choice.
4023 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4024 if (ConstantSize) {
4025 // Memset with size zero? Just return the original chain.
4026 if (ConstantSize->isNullValue())
4027 return Chain;
4028
Mon P Wangc576ee92010-04-04 03:10:48 +00004029 SDValue Result =
4030 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Chris Lattner2510de22010-09-21 05:40:29 +00004031 Align, isVol, DstPtrInfo);
Mon P Wangc576ee92010-04-04 03:10:48 +00004032
Gabor Greiff304a7a2008-08-28 21:40:38 +00004033 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004034 return Result;
4035 }
4036
4037 // Then check to see if we should lower the memset with target-specific
4038 // code. If the target chooses to do this, this is the next best.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004039 SDValue Result =
Dan Gohmanbb919df2010-05-11 17:31:57 +00004040 TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
Chris Lattner2510de22010-09-21 05:40:29 +00004041 DstPtrInfo);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004042 if (Result.getNode())
Dan Gohman544ab2c2008-04-12 04:36:06 +00004043 return Result;
4044
Wesley Peck527da1b2010-11-23 03:31:01 +00004045 // Emit a library call.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004046 const TargetLowering *TLI = TM.getTargetLowering();
4047 Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(*getContext());
Dan Gohman544ab2c2008-04-12 04:36:06 +00004048 TargetLowering::ArgListTy Args;
4049 TargetLowering::ArgListEntry Entry;
4050 Entry.Node = Dst; Entry.Ty = IntPtrTy;
4051 Args.push_back(Entry);
4052 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson9f944592009-08-11 20:47:22 +00004053 if (Src.getValueType().bitsGT(MVT::i32))
4054 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman544ab2c2008-04-12 04:36:06 +00004055 else
Owen Anderson9f944592009-08-11 20:47:22 +00004056 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson55f1c092009-08-13 21:58:54 +00004057 Entry.Node = Src;
4058 Entry.Ty = Type::getInt32Ty(*getContext());
4059 Entry.isSExt = true;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004060 Args.push_back(Entry);
Owen Anderson55f1c092009-08-13 21:58:54 +00004061 Entry.Node = Size;
4062 Entry.Ty = IntPtrTy;
4063 Entry.isSExt = false;
Dan Gohman544ab2c2008-04-12 04:36:06 +00004064 Args.push_back(Entry);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004065 // FIXME: pass in SDLoc
Justin Holewinskiaa583972012-05-25 16:35:28 +00004066 TargetLowering::
4067 CallLoweringInfo CLI(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +00004068 false, false, false, false, 0,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004069 TLI->getLibcallCallingConv(RTLIB::MEMSET),
Evan Cheng65f9d192012-02-28 18:51:51 +00004070 /*isTailCall=*/false,
4071 /*doesNotReturn*/false, /*isReturnValueUsed=*/false,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004072 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
4073 TLI->getPointerTy()),
Bill Wendling78c5b7a2010-03-02 01:55:18 +00004074 Args, *this, dl);
Bill Wendlinga3cd3502013-06-19 21:36:55 +00004075 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
Justin Holewinskiaa583972012-05-25 16:35:28 +00004076
Dan Gohman544ab2c2008-04-12 04:36:06 +00004077 return CallResult.second;
Rafael Espindola846c19dd2007-10-19 10:41:11 +00004078}
4079
Andrew Trickef9de2a2013-05-25 02:42:55 +00004080SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Chris Lattner15d84c42010-09-21 04:53:42 +00004081 SDValue Chain, SDValue Ptr, SDValue Cmp,
4082 SDValue Swp, MachinePointerInfo PtrInfo,
Eli Friedmanadec5872011-07-29 03:05:32 +00004083 unsigned Alignment,
4084 AtomicOrdering Ordering,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004085 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004086 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4087 Alignment = getEVTAlignment(MemVT);
4088
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004089 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004090
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004091 // All atomics are load and store, except for ATMOIC_LOAD and ATOMIC_STORE.
Dan Gohman48b185d2009-09-25 20:36:54 +00004092 // For now, atomics are considered to be volatile always.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004093 // FIXME: Volatile isn't really correct; we should keep track of atomic
4094 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004095 unsigned Flags = MachineMemOperand::MOVolatile;
4096 if (Opcode != ISD::ATOMIC_STORE)
4097 Flags |= MachineMemOperand::MOLoad;
4098 if (Opcode != ISD::ATOMIC_LOAD)
4099 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004100
4101 MachineMemOperand *MMO =
Chris Lattner15d84c42010-09-21 04:53:42 +00004102 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment);
Dan Gohman48b185d2009-09-25 20:36:54 +00004103
Eli Friedmanadec5872011-07-29 03:05:32 +00004104 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO,
4105 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004106}
4107
Andrew Trickef9de2a2013-05-25 02:42:55 +00004108SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004109 SDValue Chain,
4110 SDValue Ptr, SDValue Cmp,
Eli Friedmanadec5872011-07-29 03:05:32 +00004111 SDValue Swp, MachineMemOperand *MMO,
4112 AtomicOrdering Ordering,
4113 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004114 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
4115 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
4116
Owen Anderson53aa7a92009-08-10 22:56:29 +00004117 EVT VT = Cmp.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004118
Owen Anderson9f944592009-08-11 20:47:22 +00004119 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004120 FoldingSetNodeID ID;
Dan Gohman76a07f52009-02-03 00:08:45 +00004121 ID.AddInteger(MemVT.getRawBits());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004122 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
4123 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Pete Cooper91244262012-07-30 20:23:19 +00004124 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004125 void* IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004126 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4127 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004128 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004129 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004130 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, MemVT, Chain,
Eli Friedmanadec5872011-07-29 03:05:32 +00004131 Ptr, Cmp, Swp, MMO, Ordering,
4132 SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004133 CSEMap.InsertNode(N, IP);
4134 AllNodes.push_back(N);
4135 return SDValue(N, 0);
4136}
4137
Andrew Trickef9de2a2013-05-25 02:42:55 +00004138SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004139 SDValue Chain,
Scott Michelcf0da6c2009-02-17 22:15:04 +00004140 SDValue Ptr, SDValue Val,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004141 const Value* PtrVal,
Eli Friedmanadec5872011-07-29 03:05:32 +00004142 unsigned Alignment,
4143 AtomicOrdering Ordering,
4144 SynchronizationScope SynchScope) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004145 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4146 Alignment = getEVTAlignment(MemVT);
4147
Evan Cheng0e9d9ca2009-10-18 18:16:27 +00004148 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004149 // An atomic store does not load. An atomic load does not store.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004150 // (An atomicrmw obviously both loads and stores.)
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004151 // For now, atomics are considered to be volatile always, and they are
4152 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004153 // FIXME: Volatile isn't really correct; we should keep track of atomic
4154 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004155 unsigned Flags = MachineMemOperand::MOVolatile;
4156 if (Opcode != ISD::ATOMIC_STORE)
4157 Flags |= MachineMemOperand::MOLoad;
4158 if (Opcode != ISD::ATOMIC_LOAD)
4159 Flags |= MachineMemOperand::MOStore;
Dan Gohman48b185d2009-09-25 20:36:54 +00004160
4161 MachineMemOperand *MMO =
Chris Lattnerb5f49202010-09-21 04:46:39 +00004162 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
Dan Gohman48b185d2009-09-25 20:36:54 +00004163 MemVT.getStoreSize(), Alignment);
4164
Eli Friedmanadec5872011-07-29 03:05:32 +00004165 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO,
4166 Ordering, SynchScope);
Dan Gohman48b185d2009-09-25 20:36:54 +00004167}
4168
Andrew Trickef9de2a2013-05-25 02:42:55 +00004169SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Dan Gohman48b185d2009-09-25 20:36:54 +00004170 SDValue Chain,
4171 SDValue Ptr, SDValue Val,
Eli Friedmanadec5872011-07-29 03:05:32 +00004172 MachineMemOperand *MMO,
4173 AtomicOrdering Ordering,
4174 SynchronizationScope SynchScope) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004175 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
4176 Opcode == ISD::ATOMIC_LOAD_SUB ||
4177 Opcode == ISD::ATOMIC_LOAD_AND ||
4178 Opcode == ISD::ATOMIC_LOAD_OR ||
4179 Opcode == ISD::ATOMIC_LOAD_XOR ||
4180 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004181 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004182 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelcf0da6c2009-02-17 22:15:04 +00004183 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesen839acbb2009-01-29 00:47:48 +00004184 Opcode == ISD::ATOMIC_LOAD_UMAX ||
Eli Friedman342e8df2011-08-24 20:50:09 +00004185 Opcode == ISD::ATOMIC_SWAP ||
4186 Opcode == ISD::ATOMIC_STORE) &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004187 "Invalid Atomic Op");
4188
Owen Anderson53aa7a92009-08-10 22:56:29 +00004189 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004190
Eli Friedman342e8df2011-08-24 20:50:09 +00004191 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
4192 getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004193 FoldingSetNodeID ID;
Dan Gohman76a07f52009-02-03 00:08:45 +00004194 ID.AddInteger(MemVT.getRawBits());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004195 SDValue Ops[] = {Chain, Ptr, Val};
4196 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Pete Cooper91244262012-07-30 20:23:19 +00004197 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004198 void* IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004199 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4200 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004201 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004202 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004203 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, MemVT, Chain,
Eli Friedmanadec5872011-07-29 03:05:32 +00004204 Ptr, Val, MMO,
4205 Ordering, SynchScope);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004206 CSEMap.InsertNode(N, IP);
4207 AllNodes.push_back(N);
4208 return SDValue(N, 0);
4209}
4210
Andrew Trickef9de2a2013-05-25 02:42:55 +00004211SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004212 EVT VT, SDValue Chain,
4213 SDValue Ptr,
4214 const Value* PtrVal,
4215 unsigned Alignment,
4216 AtomicOrdering Ordering,
4217 SynchronizationScope SynchScope) {
4218 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4219 Alignment = getEVTAlignment(MemVT);
4220
4221 MachineFunction &MF = getMachineFunction();
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004222 // An atomic store does not load. An atomic load does not store.
4223 // (An atomicrmw obviously both loads and stores.)
4224 // For now, atomics are considered to be volatile always, and they are
4225 // chained as such.
Eli Friedmane978d2f2011-09-07 02:23:42 +00004226 // FIXME: Volatile isn't really correct; we should keep track of atomic
4227 // orderings in the memoperand.
Jakob Stoklund Olesen87cb4712012-08-28 03:11:32 +00004228 unsigned Flags = MachineMemOperand::MOVolatile;
4229 if (Opcode != ISD::ATOMIC_STORE)
4230 Flags |= MachineMemOperand::MOLoad;
4231 if (Opcode != ISD::ATOMIC_LOAD)
4232 Flags |= MachineMemOperand::MOStore;
Eli Friedman342e8df2011-08-24 20:50:09 +00004233
4234 MachineMemOperand *MMO =
4235 MF.getMachineMemOperand(MachinePointerInfo(PtrVal), Flags,
4236 MemVT.getStoreSize(), Alignment);
4237
4238 return getAtomic(Opcode, dl, MemVT, VT, Chain, Ptr, MMO,
4239 Ordering, SynchScope);
4240}
4241
Andrew Trickef9de2a2013-05-25 02:42:55 +00004242SDValue SelectionDAG::getAtomic(unsigned Opcode, SDLoc dl, EVT MemVT,
Eli Friedman342e8df2011-08-24 20:50:09 +00004243 EVT VT, SDValue Chain,
4244 SDValue Ptr,
4245 MachineMemOperand *MMO,
4246 AtomicOrdering Ordering,
4247 SynchronizationScope SynchScope) {
4248 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
4249
4250 SDVTList VTs = getVTList(VT, MVT::Other);
4251 FoldingSetNodeID ID;
4252 ID.AddInteger(MemVT.getRawBits());
4253 SDValue Ops[] = {Chain, Ptr};
4254 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Pete Cooper91244262012-07-30 20:23:19 +00004255 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Eli Friedman342e8df2011-08-24 20:50:09 +00004256 void* IP = 0;
4257 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4258 cast<AtomicSDNode>(E)->refineAlignment(MMO);
4259 return SDValue(E, 0);
4260 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004261 SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, MemVT, Chain,
Eli Friedman342e8df2011-08-24 20:50:09 +00004262 Ptr, MMO, Ordering, SynchScope);
4263 CSEMap.InsertNode(N, IP);
4264 AllNodes.push_back(N);
4265 return SDValue(N, 0);
4266}
4267
Duncan Sands739a0542008-07-02 17:40:58 +00004268/// getMergeValues - Create a MERGE_VALUES node from the given operands.
Dale Johannesenae7992a2009-02-02 20:47:48 +00004269SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004270 SDLoc dl) {
Dale Johannesenae7992a2009-02-02 20:47:48 +00004271 if (NumOps == 1)
4272 return Ops[0];
4273
Owen Anderson53aa7a92009-08-10 22:56:29 +00004274 SmallVector<EVT, 4> VTs;
Dale Johannesenae7992a2009-02-02 20:47:48 +00004275 VTs.reserve(NumOps);
4276 for (unsigned i = 0; i < NumOps; ++i)
4277 VTs.push_back(Ops[i].getValueType());
Scott Michelcf0da6c2009-02-17 22:15:04 +00004278 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenae7992a2009-02-02 20:47:48 +00004279 Ops, NumOps);
4280}
4281
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004282SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004283SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004284 const EVT *VTs, unsigned NumVTs,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004285 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004286 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004287 unsigned Align, bool Vol,
4288 bool ReadMem, bool WriteMem) {
4289 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004290 MemVT, PtrInfo, Align, Vol,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004291 ReadMem, WriteMem);
4292}
4293
4294SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004295SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004296 const SDValue *Ops, unsigned NumOps,
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004297 EVT MemVT, MachinePointerInfo PtrInfo,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004298 unsigned Align, bool Vol,
4299 bool ReadMem, bool WriteMem) {
Dan Gohman48b185d2009-09-25 20:36:54 +00004300 if (Align == 0) // Ensure that codegen never sees alignment 0
4301 Align = getEVTAlignment(MemVT);
4302
4303 MachineFunction &MF = getMachineFunction();
4304 unsigned Flags = 0;
4305 if (WriteMem)
4306 Flags |= MachineMemOperand::MOStore;
4307 if (ReadMem)
4308 Flags |= MachineMemOperand::MOLoad;
4309 if (Vol)
4310 Flags |= MachineMemOperand::MOVolatile;
4311 MachineMemOperand *MMO =
Chris Lattnerd2d58ad2010-09-21 04:57:15 +00004312 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Align);
Dan Gohman48b185d2009-09-25 20:36:54 +00004313
4314 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
4315}
4316
4317SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004318SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDLoc dl, SDVTList VTList,
Dan Gohman48b185d2009-09-25 20:36:54 +00004319 const SDValue *Ops, unsigned NumOps,
4320 EVT MemVT, MachineMemOperand *MMO) {
4321 assert((Opcode == ISD::INTRINSIC_VOID ||
4322 Opcode == ISD::INTRINSIC_W_CHAIN ||
Dale Johannesene660f4d2010-10-26 23:11:10 +00004323 Opcode == ISD::PREFETCH ||
Nadav Rotem7c277da2012-09-06 09:17:37 +00004324 Opcode == ISD::LIFETIME_START ||
4325 Opcode == ISD::LIFETIME_END ||
Dan Gohman48b185d2009-09-25 20:36:54 +00004326 (Opcode <= INT_MAX &&
4327 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
4328 "Opcode is not a memory-accessing opcode!");
4329
Dale Johannesen839acbb2009-01-29 00:47:48 +00004330 // Memoize the node unless it returns a flag.
4331 MemIntrinsicSDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004332 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004333 FoldingSetNodeID ID;
4334 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Pete Cooper91244262012-07-30 20:23:19 +00004335 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004336 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004337 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4338 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004339 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004340 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004341
Andrew Trickef9de2a2013-05-25 02:42:55 +00004342 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTList, Ops, NumOps,
Dan Gohman01c65a22010-03-18 18:49:47 +00004343 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004344 CSEMap.InsertNode(N, IP);
4345 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004346 N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTList, Ops, NumOps,
Dan Gohman01c65a22010-03-18 18:49:47 +00004347 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004348 }
4349 AllNodes.push_back(N);
4350 return SDValue(N, 0);
4351}
4352
Chris Lattnerea952f02010-09-21 17:24:05 +00004353/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4354/// MachinePointerInfo record from it. This is particularly useful because the
4355/// code generator has many cases where it doesn't bother passing in a
4356/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4357static MachinePointerInfo InferPointerInfo(SDValue Ptr, int64_t Offset = 0) {
4358 // If this is FI+Offset, we can model it.
4359 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
4360 return MachinePointerInfo::getFixedStack(FI->getIndex(), Offset);
4361
4362 // If this is (FI+Offset1)+Offset2, we can model it.
4363 if (Ptr.getOpcode() != ISD::ADD ||
4364 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
4365 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
4366 return MachinePointerInfo();
Wesley Peck527da1b2010-11-23 03:31:01 +00004367
Chris Lattnerea952f02010-09-21 17:24:05 +00004368 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4369 return MachinePointerInfo::getFixedStack(FI, Offset+
4370 cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
4371}
4372
4373/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
4374/// MachinePointerInfo record from it. This is particularly useful because the
4375/// code generator has many cases where it doesn't bother passing in a
4376/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
4377static MachinePointerInfo InferPointerInfo(SDValue Ptr, SDValue OffsetOp) {
4378 // If the 'Offset' value isn't a constant, we can't handle this.
4379 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
4380 return InferPointerInfo(Ptr, OffsetNode->getSExtValue());
4381 if (OffsetOp.getOpcode() == ISD::UNDEF)
4382 return InferPointerInfo(Ptr);
4383 return MachinePointerInfo();
4384}
Wesley Peck527da1b2010-11-23 03:31:01 +00004385
Chris Lattnerea952f02010-09-21 17:24:05 +00004386
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004387SDValue
4388SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004389 EVT VT, SDLoc dl, SDValue Chain,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004390 SDValue Ptr, SDValue Offset,
4391 MachinePointerInfo PtrInfo, EVT MemVT,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004392 bool isVolatile, bool isNonTemporal, bool isInvariant,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004393 unsigned Alignment, const MDNode *TBAAInfo,
4394 const MDNode *Ranges) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004395 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004396 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004397 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4398 Alignment = getEVTAlignment(VT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004399
Dan Gohman48b185d2009-09-25 20:36:54 +00004400 unsigned Flags = MachineMemOperand::MOLoad;
4401 if (isVolatile)
4402 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004403 if (isNonTemporal)
4404 Flags |= MachineMemOperand::MONonTemporal;
Pete Cooper82cd9e82011-11-08 18:42:53 +00004405 if (isInvariant)
4406 Flags |= MachineMemOperand::MOInvariant;
Wesley Peck527da1b2010-11-23 03:31:01 +00004407
Chris Lattnerea952f02010-09-21 17:24:05 +00004408 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
4409 // clients.
4410 if (PtrInfo.V == 0)
4411 PtrInfo = InferPointerInfo(Ptr, Offset);
Wesley Peck527da1b2010-11-23 03:31:01 +00004412
Chris Lattnerea952f02010-09-21 17:24:05 +00004413 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004414 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004415 MF.getMachineMemOperand(PtrInfo, Flags, MemVT.getStoreSize(), Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004416 TBAAInfo, Ranges);
Evan Cheng1c349f12010-07-07 22:15:37 +00004417 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
Dan Gohman48b185d2009-09-25 20:36:54 +00004418}
4419
4420SDValue
Wesley Peck527da1b2010-11-23 03:31:01 +00004421SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004422 EVT VT, SDLoc dl, SDValue Chain,
Dan Gohman48b185d2009-09-25 20:36:54 +00004423 SDValue Ptr, SDValue Offset, EVT MemVT,
4424 MachineMemOperand *MMO) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004425 if (VT == MemVT) {
Dale Johannesen839acbb2009-01-29 00:47:48 +00004426 ExtType = ISD::NON_EXTLOAD;
4427 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman08c0a952009-09-23 21:02:20 +00004428 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004429 } else {
4430 // Extending load.
Dan Gohmancecad352009-12-14 23:40:38 +00004431 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
4432 "Should only be an extending load, not truncating!");
Dan Gohman08c0a952009-09-23 21:02:20 +00004433 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesen839acbb2009-01-29 00:47:48 +00004434 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohmancecad352009-12-14 23:40:38 +00004435 assert(VT.isVector() == MemVT.isVector() &&
4436 "Cannot use trunc store to convert to or from a vector!");
4437 assert((!VT.isVector() ||
4438 VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
4439 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004440 }
4441
4442 bool Indexed = AM != ISD::UNINDEXED;
4443 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
4444 "Unindexed load with an offset!");
4445
4446 SDVTList VTs = Indexed ?
Owen Anderson9f944592009-08-11 20:47:22 +00004447 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004448 SDValue Ops[] = { Chain, Ptr, Offset };
4449 FoldingSetNodeID ID;
4450 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman08c0a952009-09-23 21:02:20 +00004451 ID.AddInteger(MemVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004452 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004453 MMO->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004454 MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004455 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004456 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004457 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4458 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004459 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004460 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004461 SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ExtType,
Dan Gohman01c65a22010-03-18 18:49:47 +00004462 MemVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004463 CSEMap.InsertNode(N, IP);
4464 AllNodes.push_back(N);
4465 return SDValue(N, 0);
4466}
4467
Andrew Trickef9de2a2013-05-25 02:42:55 +00004468SDValue SelectionDAG::getLoad(EVT VT, SDLoc dl,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004469 SDValue Chain, SDValue Ptr,
Chris Lattner2510de22010-09-21 05:40:29 +00004470 MachinePointerInfo PtrInfo,
4471 bool isVolatile, bool isNonTemporal,
Michael Ilsemand5f91512012-09-10 16:56:31 +00004472 bool isInvariant, unsigned Alignment,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004473 const MDNode *TBAAInfo,
4474 const MDNode *Ranges) {
Chris Lattner2510de22010-09-21 05:40:29 +00004475 SDValue Undef = getUNDEF(Ptr.getValueType());
4476 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
Rafael Espindola80c540e2012-03-31 18:14:00 +00004477 PtrInfo, VT, isVolatile, isNonTemporal, isInvariant, Alignment,
4478 TBAAInfo, Ranges);
Chris Lattner2510de22010-09-21 05:40:29 +00004479}
Dale Johannesen839acbb2009-01-29 00:47:48 +00004480
Andrew Trickef9de2a2013-05-25 02:42:55 +00004481SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, SDLoc dl, EVT VT,
Chris Lattner2510de22010-09-21 05:40:29 +00004482 SDValue Chain, SDValue Ptr,
4483 MachinePointerInfo PtrInfo, EVT MemVT,
4484 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004485 unsigned Alignment, const MDNode *TBAAInfo) {
Chris Lattner2510de22010-09-21 05:40:29 +00004486 SDValue Undef = getUNDEF(Ptr.getValueType());
4487 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
Pete Cooper82cd9e82011-11-08 18:42:53 +00004488 PtrInfo, MemVT, isVolatile, isNonTemporal, false, Alignment,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004489 TBAAInfo);
Chris Lattner2510de22010-09-21 05:40:29 +00004490}
4491
4492
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004493SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004494SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004495 SDValue Offset, ISD::MemIndexedMode AM) {
4496 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
4497 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
4498 "Load is already a indexed load!");
Evan Cheng1c349f12010-07-07 22:15:37 +00004499 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
Chris Lattnerea952f02010-09-21 17:24:05 +00004500 LD->getChain(), Base, Offset, LD->getPointerInfo(),
Michael Ilsemand5f91512012-09-10 16:56:31 +00004501 LD->getMemoryVT(), LD->isVolatile(), LD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004502 false, LD->getAlignment());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004503}
4504
Andrew Trickef9de2a2013-05-25 02:42:55 +00004505SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004506 SDValue Ptr, MachinePointerInfo PtrInfo,
David Greene39c6d012010-02-15 17:00:31 +00004507 bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004508 unsigned Alignment, const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004509 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004510 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004511 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman48b185d2009-09-25 20:36:54 +00004512 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004513
Dan Gohman48b185d2009-09-25 20:36:54 +00004514 unsigned Flags = MachineMemOperand::MOStore;
4515 if (isVolatile)
4516 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004517 if (isNonTemporal)
4518 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004519
Chris Lattnerea952f02010-09-21 17:24:05 +00004520 if (PtrInfo.V == 0)
4521 PtrInfo = InferPointerInfo(Ptr);
4522
4523 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004524 MachineMemOperand *MMO =
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004525 MF.getMachineMemOperand(PtrInfo, Flags,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004526 Val.getValueType().getStoreSize(), Alignment,
4527 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004528
4529 return getStore(Chain, dl, Val, Ptr, MMO);
4530}
4531
Andrew Trickef9de2a2013-05-25 02:42:55 +00004532SDValue SelectionDAG::getStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004533 SDValue Ptr, MachineMemOperand *MMO) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004534 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004535 "Invalid chain type");
Dan Gohman48b185d2009-09-25 20:36:54 +00004536 EVT VT = Val.getValueType();
Owen Anderson9f944592009-08-11 20:47:22 +00004537 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004538 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004539 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4540 FoldingSetNodeID ID;
4541 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004542 ID.AddInteger(VT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004543 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004544 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004545 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004546 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004547 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4548 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004549 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004550 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004551 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), dl.getDebugLoc(), VTs, ISD::UNINDEXED,
Dan Gohman01c65a22010-03-18 18:49:47 +00004552 false, VT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004553 CSEMap.InsertNode(N, IP);
4554 AllNodes.push_back(N);
4555 return SDValue(N, 0);
4556}
4557
Andrew Trickef9de2a2013-05-25 02:42:55 +00004558SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004559 SDValue Ptr, MachinePointerInfo PtrInfo,
4560 EVT SVT,bool isVolatile, bool isNonTemporal,
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004561 unsigned Alignment,
4562 const MDNode *TBAAInfo) {
Michael Ilsemand5f91512012-09-10 16:56:31 +00004563 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004564 "Invalid chain type");
Chris Lattnerbc419ba2010-09-21 05:10:45 +00004565 if (Alignment == 0) // Ensure that codegen never sees alignment 0
4566 Alignment = getEVTAlignment(SVT);
Dan Gohman48b185d2009-09-25 20:36:54 +00004567
Dan Gohman48b185d2009-09-25 20:36:54 +00004568 unsigned Flags = MachineMemOperand::MOStore;
4569 if (isVolatile)
4570 Flags |= MachineMemOperand::MOVolatile;
David Greene39c6d012010-02-15 17:00:31 +00004571 if (isNonTemporal)
4572 Flags |= MachineMemOperand::MONonTemporal;
Wesley Peck527da1b2010-11-23 03:31:01 +00004573
Chris Lattnerea952f02010-09-21 17:24:05 +00004574 if (PtrInfo.V == 0)
4575 PtrInfo = InferPointerInfo(Ptr);
4576
4577 MachineFunction &MF = getMachineFunction();
Dan Gohman48b185d2009-09-25 20:36:54 +00004578 MachineMemOperand *MMO =
Dan Gohmana94cc6d2010-10-20 00:31:05 +00004579 MF.getMachineMemOperand(PtrInfo, Flags, SVT.getStoreSize(), Alignment,
4580 TBAAInfo);
Dan Gohman48b185d2009-09-25 20:36:54 +00004581
4582 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
4583}
4584
Andrew Trickef9de2a2013-05-25 02:42:55 +00004585SDValue SelectionDAG::getTruncStore(SDValue Chain, SDLoc dl, SDValue Val,
Dan Gohman48b185d2009-09-25 20:36:54 +00004586 SDValue Ptr, EVT SVT,
4587 MachineMemOperand *MMO) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004588 EVT VT = Val.getValueType();
Dale Johannesen839acbb2009-01-29 00:47:48 +00004589
Michael Ilsemand5f91512012-09-10 16:56:31 +00004590 assert(Chain.getValueType() == MVT::Other &&
Nadav Rotemdb213c02011-07-14 10:37:54 +00004591 "Invalid chain type");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004592 if (VT == SVT)
Dan Gohman48b185d2009-09-25 20:36:54 +00004593 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004594
Dan Gohmancecad352009-12-14 23:40:38 +00004595 assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
4596 "Should only be a truncating store, not extending!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004597 assert(VT.isInteger() == SVT.isInteger() &&
4598 "Can't do FP-INT conversion!");
Dan Gohmancecad352009-12-14 23:40:38 +00004599 assert(VT.isVector() == SVT.isVector() &&
4600 "Cannot use trunc store to convert to or from a vector!");
4601 assert((!VT.isVector() ||
4602 VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
4603 "Cannot use trunc store to change the number of vector elements!");
Dale Johannesen839acbb2009-01-29 00:47:48 +00004604
Owen Anderson9f944592009-08-11 20:47:22 +00004605 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen84935752009-02-06 23:05:02 +00004606 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004607 SDValue Ops[] = { Chain, Val, Ptr, Undef };
4608 FoldingSetNodeID ID;
4609 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004610 ID.AddInteger(SVT.getRawBits());
David Greeneb7941b02010-02-17 20:21:42 +00004611 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004612 MMO->isNonTemporal(), MMO->isInvariant()));
Pete Cooper91244262012-07-30 20:23:19 +00004613 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004614 void *IP = 0;
Dan Gohman48b185d2009-09-25 20:36:54 +00004615 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
4616 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004617 return SDValue(E, 0);
Dan Gohman48b185d2009-09-25 20:36:54 +00004618 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004619 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), dl.getDebugLoc(), VTs, ISD::UNINDEXED,
Dan Gohman01c65a22010-03-18 18:49:47 +00004620 true, SVT, MMO);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004621 CSEMap.InsertNode(N, IP);
4622 AllNodes.push_back(N);
4623 return SDValue(N, 0);
4624}
4625
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004626SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00004627SelectionDAG::getIndexedStore(SDValue OrigStore, SDLoc dl, SDValue Base,
Dale Johannesen839acbb2009-01-29 00:47:48 +00004628 SDValue Offset, ISD::MemIndexedMode AM) {
4629 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
4630 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
4631 "Store is already a indexed store!");
Owen Anderson9f944592009-08-11 20:47:22 +00004632 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004633 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
4634 FoldingSetNodeID ID;
4635 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesen839acbb2009-01-29 00:47:48 +00004636 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman76a07f52009-02-03 00:08:45 +00004637 ID.AddInteger(ST->getRawSubclassData());
Pete Cooper91244262012-07-30 20:23:19 +00004638 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004639 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004640 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dale Johannesen839acbb2009-01-29 00:47:48 +00004641 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004642
Andrew Trickef9de2a2013-05-25 02:42:55 +00004643 SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
Dan Gohman01c65a22010-03-18 18:49:47 +00004644 ST->isTruncatingStore(),
4645 ST->getMemoryVT(),
4646 ST->getMemOperand());
Dale Johannesen839acbb2009-01-29 00:47:48 +00004647 CSEMap.InsertNode(N, IP);
4648 AllNodes.push_back(N);
4649 return SDValue(N, 0);
4650}
4651
Andrew Trickef9de2a2013-05-25 02:42:55 +00004652SDValue SelectionDAG::getVAArg(EVT VT, SDLoc dl,
Dale Johannesenabf66b82009-02-03 22:26:09 +00004653 SDValue Chain, SDValue Ptr,
Rafael Espindola2041abd2010-06-26 18:22:20 +00004654 SDValue SV,
4655 unsigned Align) {
4656 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
4657 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
Dale Johannesenabf66b82009-02-03 22:26:09 +00004658}
4659
Andrew Trickef9de2a2013-05-25 02:42:55 +00004660SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004661 const SDUse *Ops, unsigned NumOps) {
Dan Gohman768f2c92008-07-07 18:26:29 +00004662 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004663 case 0: return getNode(Opcode, DL, VT);
4664 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4665 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4666 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman768f2c92008-07-07 18:26:29 +00004667 default: break;
4668 }
4669
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004670 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman768f2c92008-07-07 18:26:29 +00004671 // the regular getNode logic.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004672 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004673 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman768f2c92008-07-07 18:26:29 +00004674}
4675
Andrew Trickef9de2a2013-05-25 02:42:55 +00004676SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, EVT VT,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004677 const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00004678 switch (NumOps) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004679 case 0: return getNode(Opcode, DL, VT);
4680 case 1: return getNode(Opcode, DL, VT, Ops[0]);
4681 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
4682 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00004683 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00004684 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004685
Chris Lattnerb0713c72005-04-09 03:27:28 +00004686 switch (Opcode) {
4687 default: break;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004688 case ISD::SELECT_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004689 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004690 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
4691 "LHS and RHS of condition must have same type!");
4692 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4693 "True and False arms of SelectCC must have same type!");
4694 assert(Ops[2].getValueType() == VT &&
4695 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004696 break;
4697 }
4698 case ISD::BR_CC: {
Chris Lattner97af9d52006-08-08 01:09:31 +00004699 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004700 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
4701 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00004702 break;
4703 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00004704 }
4705
Chris Lattner566307f2005-05-14 07:42:29 +00004706 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00004707 SDNode *N;
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00004708 SDVTList VTs = getVTList(VT);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004709
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004710 if (VT != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004711 FoldingSetNodeID ID;
4712 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004713 void *IP = 0;
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004714
Bill Wendling022d18f2009-12-18 23:32:53 +00004715 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004716 return SDValue(E, 0);
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004717
Andrew Trickef9de2a2013-05-25 02:42:55 +00004718 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004719 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004720 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004721 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs, Ops, NumOps);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004722 }
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004723
Chris Lattnerb0713c72005-04-09 03:27:28 +00004724 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004725#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004726 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004727#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004728 return SDValue(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00004729}
4730
Andrew Trickef9de2a2013-05-25 02:42:55 +00004731SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00004732 ArrayRef<EVT> ResultTys,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004733 const SDValue *Ops, unsigned NumOps) {
Dan Gohmande912e22009-04-09 23:54:40 +00004734 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner3bf4be42006-08-14 23:31:51 +00004735 Ops, NumOps);
4736}
4737
Andrew Trickef9de2a2013-05-25 02:42:55 +00004738SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL,
Owen Anderson53aa7a92009-08-10 22:56:29 +00004739 const EVT *VTs, unsigned NumVTs,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004740 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3bf4be42006-08-14 23:31:51 +00004741 if (NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004742 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4743 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004744}
4745
Andrew Trickef9de2a2013-05-25 02:42:55 +00004746SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004747 const SDValue *Ops, unsigned NumOps) {
Chris Lattner65879ca2006-08-16 22:57:46 +00004748 if (VTList.NumVTs == 1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004749 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattnerd5531332005-05-14 06:20:26 +00004750
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004751#if 0
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004752 switch (Opcode) {
Chris Lattner669e8c22005-05-14 07:25:05 +00004753 // FIXME: figure out how to safely handle things like
4754 // int foo(int x) { return 1 << (x & 255); }
4755 // int bar() { return foo(256); }
Chris Lattner669e8c22005-05-14 07:25:05 +00004756 case ISD::SRA_PARTS:
4757 case ISD::SRL_PARTS:
4758 case ISD::SHL_PARTS:
4759 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson9f944592009-08-11 20:47:22 +00004760 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004761 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004762 else if (N3.getOpcode() == ISD::AND)
4763 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4764 // If the and is only masking out bits that cannot effect the shift,
4765 // eliminate the and.
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004766 unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
Chris Lattner669e8c22005-05-14 07:25:05 +00004767 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004768 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattner669e8c22005-05-14 07:25:05 +00004769 }
4770 break;
Chris Lattnerde0a4b12005-07-10 01:55:33 +00004771 }
Daniel Dunbarac0ca922009-07-19 01:38:38 +00004772#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00004773
Chris Lattnerf9c19152005-08-25 19:12:10 +00004774 // Memoize the node unless it returns a flag.
4775 SDNode *N;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004776 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Jim Laskeyf576b422006-10-27 23:46:08 +00004777 FoldingSetNodeID ID;
4778 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004779 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00004780 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004781 return SDValue(E, 0);
Bill Wendling022d18f2009-12-18 23:32:53 +00004782
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004783 if (NumOps == 1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004784 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004785 } else if (NumOps == 2) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004786 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops[0], Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004787 } else if (NumOps == 3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004788 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops[0], Ops[1],
Dan Gohman01c65a22010-03-18 18:49:47 +00004789 Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004790 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004791 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004792 }
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004793 CSEMap.InsertNode(N, IP);
Chris Lattnerf9c19152005-08-25 19:12:10 +00004794 } else {
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004795 if (NumOps == 1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004796 N = new (NodeAllocator) UnarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops[0]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004797 } else if (NumOps == 2) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004798 N = new (NodeAllocator) BinarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops[0], Ops[1]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004799 } else if (NumOps == 3) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004800 N = new (NodeAllocator) TernarySDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops[0], Ops[1],
Dan Gohman01c65a22010-03-18 18:49:47 +00004801 Ops[2]);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004802 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004803 N = new (NodeAllocator) SDNode(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList, Ops, NumOps);
Dan Gohman7f8b6d52008-07-07 23:02:41 +00004804 }
Chris Lattnerf9c19152005-08-25 19:12:10 +00004805 }
Chris Lattner006f56b2005-05-14 06:42:57 +00004806 AllNodes.push_back(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004807#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00004808 VerifySDNode(N);
Duncan Sandsb0e39382008-07-21 10:20:31 +00004809#endif
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004810 return SDValue(N, 0);
Chris Lattnerd5531332005-05-14 06:20:26 +00004811}
4812
Andrew Trickef9de2a2013-05-25 02:42:55 +00004813SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList) {
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004814 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004815}
4816
Andrew Trickef9de2a2013-05-25 02:42:55 +00004817SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004818 SDValue N1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004819 SDValue Ops[] = { N1 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004820 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004821}
4822
Andrew Trickef9de2a2013-05-25 02:42:55 +00004823SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004824 SDValue N1, SDValue N2) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004825 SDValue Ops[] = { N1, N2 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004826 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004827}
4828
Andrew Trickef9de2a2013-05-25 02:42:55 +00004829SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004830 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004831 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004832 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004833}
4834
Andrew Trickef9de2a2013-05-25 02:42:55 +00004835SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004836 SDValue N1, SDValue N2, SDValue N3,
4837 SDValue N4) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004838 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004839 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004840}
4841
Andrew Trickef9de2a2013-05-25 02:42:55 +00004842SDValue SelectionDAG::getNode(unsigned Opcode, SDLoc DL, SDVTList VTList,
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004843 SDValue N1, SDValue N2, SDValue N3,
4844 SDValue N4, SDValue N5) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004845 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling1b6a3bc2009-01-28 22:17:52 +00004846 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohmanb08c8bf2007-10-08 15:49:58 +00004847}
4848
Owen Anderson53aa7a92009-08-10 22:56:29 +00004849SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00004850 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattner88fa11c2005-11-08 23:30:28 +00004851}
4852
Owen Anderson53aa7a92009-08-10 22:56:29 +00004853SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohman17059682008-07-17 19:10:17 +00004854 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4855 E = VTList.rend(); I != E; ++I)
4856 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4857 return *I;
4858
Owen Anderson53aa7a92009-08-10 22:56:29 +00004859 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohman17059682008-07-17 19:10:17 +00004860 Array[0] = VT1;
4861 Array[1] = VT2;
4862 SDVTList Result = makeVTList(Array, 2);
4863 VTList.push_back(Result);
4864 return Result;
Chris Lattner88fa11c2005-11-08 23:30:28 +00004865}
Dan Gohman17059682008-07-17 19:10:17 +00004866
Owen Anderson53aa7a92009-08-10 22:56:29 +00004867SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohman17059682008-07-17 19:10:17 +00004868 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4869 E = VTList.rend(); I != E; ++I)
4870 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4871 I->VTs[2] == VT3)
4872 return *I;
4873
Owen Anderson53aa7a92009-08-10 22:56:29 +00004874 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohman17059682008-07-17 19:10:17 +00004875 Array[0] = VT1;
4876 Array[1] = VT2;
4877 Array[2] = VT3;
4878 SDVTList Result = makeVTList(Array, 3);
4879 VTList.push_back(Result);
4880 return Result;
Chris Lattnerf98411a2006-08-15 17:46:01 +00004881}
4882
Owen Anderson53aa7a92009-08-10 22:56:29 +00004883SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling2d598632008-12-01 23:28:22 +00004884 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4885 E = VTList.rend(); I != E; ++I)
4886 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4887 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4888 return *I;
4889
Anton Korobeynikov94b63102009-12-13 01:00:59 +00004890 EVT *Array = Allocator.Allocate<EVT>(4);
Bill Wendling2d598632008-12-01 23:28:22 +00004891 Array[0] = VT1;
4892 Array[1] = VT2;
4893 Array[2] = VT3;
4894 Array[3] = VT4;
4895 SDVTList Result = makeVTList(Array, 4);
4896 VTList.push_back(Result);
4897 return Result;
4898}
4899
Owen Anderson53aa7a92009-08-10 22:56:29 +00004900SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattnerf98411a2006-08-15 17:46:01 +00004901 switch (NumVTs) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004902 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohmana8665142007-06-25 16:23:39 +00004903 case 1: return getVTList(VTs[0]);
Chris Lattnerf98411a2006-08-15 17:46:01 +00004904 case 2: return getVTList(VTs[0], VTs[1]);
4905 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
Anton Korobeynikov10590172009-12-19 02:04:00 +00004906 case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
Chris Lattnerf98411a2006-08-15 17:46:01 +00004907 default: break;
4908 }
4909
Dan Gohman17059682008-07-17 19:10:17 +00004910 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4911 E = VTList.rend(); I != E; ++I) {
4912 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4913 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004914
Benjamin Kramerf364a632012-07-19 10:46:05 +00004915 if (std::equal(&VTs[2], &VTs[NumVTs], &I->VTs[2]))
Dan Gohman17059682008-07-17 19:10:17 +00004916 return *I;
Chris Lattnerf98411a2006-08-15 17:46:01 +00004917 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004918
Owen Anderson53aa7a92009-08-10 22:56:29 +00004919 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohman17059682008-07-17 19:10:17 +00004920 std::copy(VTs, VTs+NumVTs, Array);
4921 SDVTList Result = makeVTList(Array, NumVTs);
4922 VTList.push_back(Result);
4923 return Result;
Chris Lattner3bf4be42006-08-14 23:31:51 +00004924}
4925
4926
Chris Lattnerf34156e2006-01-28 09:32:45 +00004927/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4928/// specified operands. If the resultant node already exists in the DAG,
4929/// this does not modify the specified node, instead it returns the node that
4930/// already exists. If the resultant node does not exist in the DAG, the
4931/// input node is returned. As a degenerate case, if you specify the same
4932/// input operands as the node already has, the input node is returned.
Dan Gohman92c11ac2010-06-18 15:30:29 +00004933SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00004934 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00004935
Chris Lattnerf34156e2006-01-28 09:32:45 +00004936 // Check to see if there is no change.
Dan Gohman92c11ac2010-06-18 15:30:29 +00004937 if (Op == N->getOperand(0)) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004938
Chris Lattnerf34156e2006-01-28 09:32:45 +00004939 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004940 void *InsertPos = 0;
4941 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00004942 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004943
Dan Gohmanebeccb42008-07-21 22:38:59 +00004944 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004945 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00004946 if (!RemoveNodeFromCSEMaps(N))
4947 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004948
Chris Lattnerf34156e2006-01-28 09:32:45 +00004949 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00004950 N->OperandList[0].set(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004951
Chris Lattnerf34156e2006-01-28 09:32:45 +00004952 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004953 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00004954 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00004955}
4956
Dan Gohman92c11ac2010-06-18 15:30:29 +00004957SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
Chris Lattnerf34156e2006-01-28 09:32:45 +00004958 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00004959
Chris Lattnerf34156e2006-01-28 09:32:45 +00004960 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00004961 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
Dan Gohman92c11ac2010-06-18 15:30:29 +00004962 return N; // No operands changed, just return the input node.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004963
Chris Lattnerf34156e2006-01-28 09:32:45 +00004964 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004965 void *InsertPos = 0;
4966 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00004967 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004968
Dan Gohmanebeccb42008-07-21 22:38:59 +00004969 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004970 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00004971 if (!RemoveNodeFromCSEMaps(N))
4972 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004973
Chris Lattnerf34156e2006-01-28 09:32:45 +00004974 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00004975 if (N->OperandList[0] != Op1)
4976 N->OperandList[0].set(Op1);
4977 if (N->OperandList[1] != Op2)
4978 N->OperandList[1].set(Op2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004979
Chris Lattnerf34156e2006-01-28 09:32:45 +00004980 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00004981 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00004982 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00004983}
4984
Dan Gohman92c11ac2010-06-18 15:30:29 +00004985SDNode *SelectionDAG::
4986UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004987 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattner97af9d52006-08-08 01:09:31 +00004988 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerf34156e2006-01-28 09:32:45 +00004989}
4990
Dan Gohman92c11ac2010-06-18 15:30:29 +00004991SDNode *SelectionDAG::
4992UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004993 SDValue Op3, SDValue Op4) {
4994 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattner97af9d52006-08-08 01:09:31 +00004995 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerf34156e2006-01-28 09:32:45 +00004996}
4997
Dan Gohman92c11ac2010-06-18 15:30:29 +00004998SDNode *SelectionDAG::
4999UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005000 SDValue Op3, SDValue Op4, SDValue Op5) {
5001 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattner97af9d52006-08-08 01:09:31 +00005002 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner580b12a2006-01-28 10:09:25 +00005003}
5004
Dan Gohman92c11ac2010-06-18 15:30:29 +00005005SDNode *SelectionDAG::
5006UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
Chris Lattner97af9d52006-08-08 01:09:31 +00005007 assert(N->getNumOperands() == NumOps &&
Chris Lattnerf34156e2006-01-28 09:32:45 +00005008 "Update with wrong number of operands");
Scott Michelcf0da6c2009-02-17 22:15:04 +00005009
Chris Lattnerf34156e2006-01-28 09:32:45 +00005010 // Check to see if there is no change.
Chris Lattnerf34156e2006-01-28 09:32:45 +00005011 bool AnyChange = false;
5012 for (unsigned i = 0; i != NumOps; ++i) {
5013 if (Ops[i] != N->getOperand(i)) {
5014 AnyChange = true;
5015 break;
5016 }
5017 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005018
Chris Lattnerf34156e2006-01-28 09:32:45 +00005019 // No operands changed, just return the input node.
Dan Gohman92c11ac2010-06-18 15:30:29 +00005020 if (!AnyChange) return N;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005021
Chris Lattnerf34156e2006-01-28 09:32:45 +00005022 // See if the modified node already exists.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005023 void *InsertPos = 0;
Chris Lattner97af9d52006-08-08 01:09:31 +00005024 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Dan Gohman92c11ac2010-06-18 15:30:29 +00005025 return Existing;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005026
Dan Gohman2f83b472008-05-02 00:05:03 +00005027 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005028 if (InsertPos)
Dan Gohmand3fe1742008-09-13 01:54:27 +00005029 if (!RemoveNodeFromCSEMaps(N))
5030 InsertPos = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005031
Chris Lattnerf34156e2006-01-28 09:32:45 +00005032 // Now we update the operands.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005033 for (unsigned i = 0; i != NumOps; ++i)
5034 if (N->OperandList[i] != Ops[i])
5035 N->OperandList[i].set(Ops[i]);
Chris Lattnerf34156e2006-01-28 09:32:45 +00005036
5037 // If this gets put into a CSE map, add it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005038 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Dan Gohman92c11ac2010-06-18 15:30:29 +00005039 return N;
Chris Lattnerf34156e2006-01-28 09:32:45 +00005040}
5041
Dan Gohman91697632008-07-07 20:57:48 +00005042/// DropOperands - Release the operands and set this node to have
Dan Gohman17059682008-07-17 19:10:17 +00005043/// zero operands.
Dan Gohman91697632008-07-07 20:57:48 +00005044void SDNode::DropOperands() {
Dan Gohman91697632008-07-07 20:57:48 +00005045 // Unlike the code in MorphNodeTo that does this, we don't need to
5046 // watch for dead nodes here.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005047 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
5048 SDUse &Use = *I++;
5049 Use.set(SDValue());
5050 }
Chris Lattneredfc7e52007-02-04 02:49:29 +00005051}
Chris Lattner19732782005-08-16 18:17:10 +00005052
Dan Gohman17059682008-07-17 19:10:17 +00005053/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
5054/// machine opcode.
Chris Lattner9d0d7152005-12-01 18:00:57 +00005055///
Dan Gohman17059682008-07-17 19:10:17 +00005056SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005057 EVT VT) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005058 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005059 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner45e1ce42005-08-24 23:00:29 +00005060}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005061
Dan Gohman17059682008-07-17 19:10:17 +00005062SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005063 EVT VT, SDValue Op1) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005064 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005065 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005066 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner19732782005-08-16 18:17:10 +00005067}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005068
Dan Gohman17059682008-07-17 19:10:17 +00005069SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005070 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005071 SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005072 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005073 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005074 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner19732782005-08-16 18:17:10 +00005075}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005076
Dan Gohman17059682008-07-17 19:10:17 +00005077SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005078 EVT VT, SDValue Op1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005079 SDValue Op2, SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005080 SDVTList VTs = getVTList(VT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005081 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005082 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner19732782005-08-16 18:17:10 +00005083}
Chris Lattner466fece2005-08-21 22:30:30 +00005084
Dan Gohman17059682008-07-17 19:10:17 +00005085SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005086 EVT VT, const SDValue *Ops,
Evan Cheng849f4bf2006-08-27 08:08:54 +00005087 unsigned NumOps) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005088 SDVTList VTs = getVTList(VT);
Dan Gohman17059682008-07-17 19:10:17 +00005089 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005090}
5091
Dan Gohman17059682008-07-17 19:10:17 +00005092SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005093 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005094 unsigned NumOps) {
5095 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman17059682008-07-17 19:10:17 +00005096 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005097}
5098
Dan Gohman17059682008-07-17 19:10:17 +00005099SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005100 EVT VT1, EVT VT2) {
Dan Gohman22e97072008-07-02 23:23:19 +00005101 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005102 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman22e97072008-07-02 23:23:19 +00005103}
5104
Dan Gohman17059682008-07-17 19:10:17 +00005105SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005106 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005107 const SDValue *Ops, unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005108 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman17059682008-07-17 19:10:17 +00005109 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman22e97072008-07-02 23:23:19 +00005110}
5111
Bill Wendling2d598632008-12-01 23:28:22 +00005112SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005113 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling2d598632008-12-01 23:28:22 +00005114 const SDValue *Ops, unsigned NumOps) {
5115 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
5116 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
5117}
5118
Scott Michelcf0da6c2009-02-17 22:15:04 +00005119SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005120 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005121 SDValue Op1) {
Dan Gohman22e97072008-07-02 23:23:19 +00005122 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005123 SDValue Ops[] = { Op1 };
Dan Gohman17059682008-07-17 19:10:17 +00005124 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth683352382006-01-23 21:51:14 +00005125}
Andrew Lenharthc2856382006-01-23 20:59:12 +00005126
Scott Michelcf0da6c2009-02-17 22:15:04 +00005127SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005128 EVT VT1, EVT VT2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005129 SDValue Op1, SDValue Op2) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005130 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005131 SDValue Ops[] = { Op1, Op2 };
Dan Gohman17059682008-07-17 19:10:17 +00005132 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005133}
5134
Dan Gohman17059682008-07-17 19:10:17 +00005135SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005136 EVT VT1, EVT VT2,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005137 SDValue Op1, SDValue Op2,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005138 SDValue Op3) {
Chris Lattnera5a3eaf2006-08-15 19:11:05 +00005139 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005140 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman17059682008-07-17 19:10:17 +00005141 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman22e97072008-07-02 23:23:19 +00005142}
5143
Dan Gohman17059682008-07-17 19:10:17 +00005144SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005145 EVT VT1, EVT VT2, EVT VT3,
Scott Michelcf0da6c2009-02-17 22:15:04 +00005146 SDValue Op1, SDValue Op2,
Bill Wendling2d598632008-12-01 23:28:22 +00005147 SDValue Op3) {
5148 SDVTList VTs = getVTList(VT1, VT2, VT3);
5149 SDValue Ops[] = { Op1, Op2, Op3 };
5150 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
5151}
5152
5153SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005154 SDVTList VTs, const SDValue *Ops,
Dan Gohman22e97072008-07-02 23:23:19 +00005155 unsigned NumOps) {
Chris Lattner625916d2010-02-23 23:01:35 +00005156 N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
5157 // Reset the NodeID to -1.
5158 N->setNodeId(-1);
5159 return N;
Dan Gohman17059682008-07-17 19:10:17 +00005160}
5161
Andrew Trickef9de2a2013-05-25 02:42:55 +00005162/// UpdadeSDLocOnMergedSDNode - If the opt level is -O0 then it throws away
Devang Patel7bbc1e52011-12-15 18:21:18 +00005163/// the line number information on the merged node since it is not possible to
5164/// preserve the information that operation is associated with multiple lines.
5165/// This will make the debugger working better at -O0, were there is a higher
5166/// probability having other instructions associated with that line.
5167///
Andrew Trickef9de2a2013-05-25 02:42:55 +00005168/// For IROrder, we keep the smaller of the two
5169SDNode *SelectionDAG::UpdadeSDLocOnMergedSDNode(SDNode *N, SDLoc OLoc) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005170 DebugLoc NLoc = N->getDebugLoc();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005171 if (!(NLoc.isUnknown()) && (OptLevel == CodeGenOpt::None) &&
5172 (OLoc.getDebugLoc() != NLoc)) {
Devang Patel7bbc1e52011-12-15 18:21:18 +00005173 N->setDebugLoc(DebugLoc());
5174 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005175 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
5176 N->setIROrder(Order);
Devang Patel7bbc1e52011-12-15 18:21:18 +00005177 return N;
5178}
5179
Chris Lattneraf197502010-02-28 21:36:14 +00005180/// MorphNodeTo - This *mutates* the specified node to have the specified
Dan Gohman17059682008-07-17 19:10:17 +00005181/// return type, opcode, and operands.
5182///
5183/// Note that MorphNodeTo returns the resultant node. If there is already a
5184/// node of the specified opcode and operands, it returns that node instead of
Andrew Trickef9de2a2013-05-25 02:42:55 +00005185/// the current one. Note that the SDLoc need not be the same.
Dan Gohman17059682008-07-17 19:10:17 +00005186///
5187/// Using MorphNodeTo is faster than creating a new node and swapping it in
5188/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif66ccf602008-08-30 22:16:05 +00005189/// node, and because it doesn't require CSE recalculation for any of
Dan Gohman17059682008-07-17 19:10:17 +00005190/// the node's users.
5191///
5192SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005193 SDVTList VTs, const SDValue *Ops,
Dan Gohman17059682008-07-17 19:10:17 +00005194 unsigned NumOps) {
Dan Gohman22e97072008-07-02 23:23:19 +00005195 // If an identical node already exists, use it.
Chris Lattner1ee75ce2006-08-07 23:03:03 +00005196 void *IP = 0;
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005197 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
Dan Gohman17059682008-07-17 19:10:17 +00005198 FoldingSetNodeID ID;
5199 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
Bill Wendling022d18f2009-12-18 23:32:53 +00005200 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005201 return UpdadeSDLocOnMergedSDNode(ON, SDLoc(N));
Dan Gohman17059682008-07-17 19:10:17 +00005202 }
Chris Lattner9d0d7152005-12-01 18:00:57 +00005203
Dan Gohmand3fe1742008-09-13 01:54:27 +00005204 if (!RemoveNodeFromCSEMaps(N))
5205 IP = 0;
Chris Lattner486edfb2007-02-04 02:32:44 +00005206
Dan Gohman17059682008-07-17 19:10:17 +00005207 // Start the morphing.
5208 N->NodeType = Opc;
5209 N->ValueList = VTs.VTs;
5210 N->NumValues = VTs.NumVTs;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005211
Dan Gohman17059682008-07-17 19:10:17 +00005212 // Clear the operands list, updating used nodes to remove this from their
5213 // use list. Keep track of any operands that become dead as a result.
5214 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005215 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
5216 SDUse &Use = *I++;
5217 SDNode *Used = Use.getNode();
5218 Use.set(SDValue());
Dan Gohman17059682008-07-17 19:10:17 +00005219 if (Used->use_empty())
5220 DeadNodeSet.insert(Used);
5221 }
5222
Dan Gohman48b185d2009-09-25 20:36:54 +00005223 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
5224 // Initialize the memory references information.
5225 MN->setMemRefs(0, 0);
5226 // If NumOps is larger than the # of operands we can have in a
5227 // MachineSDNode, reallocate the operand list.
5228 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
5229 if (MN->OperandsNeedDelete)
5230 delete[] MN->OperandList;
5231 if (NumOps > array_lengthof(MN->LocalOperands))
5232 // We're creating a final node that will live unmorphed for the
5233 // remainder of the current SelectionDAG iteration, so we can allocate
5234 // the operands directly out of a pool with no recycling metadata.
5235 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
Dan Gohman01c65a22010-03-18 18:49:47 +00005236 Ops, NumOps);
Dan Gohman48b185d2009-09-25 20:36:54 +00005237 else
5238 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
5239 MN->OperandsNeedDelete = false;
5240 } else
5241 MN->InitOperands(MN->OperandList, Ops, NumOps);
5242 } else {
5243 // If NumOps is larger than the # of operands we currently have, reallocate
5244 // the operand list.
5245 if (NumOps > N->NumOperands) {
5246 if (N->OperandsNeedDelete)
5247 delete[] N->OperandList;
5248 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005249 N->OperandsNeedDelete = true;
Dan Gohman48b185d2009-09-25 20:36:54 +00005250 } else
Anton Korobeynikov86263672009-10-22 00:15:17 +00005251 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohman17059682008-07-17 19:10:17 +00005252 }
5253
5254 // Delete any nodes that are still dead after adding the uses for the
5255 // new operands.
Chris Lattnere89ca7c2010-03-01 07:43:08 +00005256 if (!DeadNodeSet.empty()) {
5257 SmallVector<SDNode *, 16> DeadNodes;
5258 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
5259 E = DeadNodeSet.end(); I != E; ++I)
5260 if ((*I)->use_empty())
5261 DeadNodes.push_back(*I);
5262 RemoveDeadNodes(DeadNodes);
5263 }
Dan Gohman91697632008-07-07 20:57:48 +00005264
Dan Gohman17059682008-07-17 19:10:17 +00005265 if (IP)
5266 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng34b70ee2006-08-26 08:00:10 +00005267 return N;
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005268}
5269
Chris Lattnerf090f7e2005-11-19 01:44:53 +00005270
Dan Gohman32f71d72009-09-25 18:54:59 +00005271/// getMachineNode - These are used for target selectors to create a new node
5272/// with specified return type(s), MachineInstr opcode, and operands.
Evan Chengd3f1db92006-02-09 07:15:23 +00005273///
Dan Gohman32f71d72009-09-25 18:54:59 +00005274/// Note that getMachineNode returns the resultant node. If there is already a
Evan Chengd3f1db92006-02-09 07:15:23 +00005275/// node of the specified opcode and operands, it returns that node instead of
5276/// the current one.
Dan Gohmana22f2d82009-10-10 01:29:16 +00005277MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005278SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005279 SDVTList VTs = getVTList(VT);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005280 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005281}
Bill Wendlinga434d932009-01-29 09:01:55 +00005282
Dan Gohmana22f2d82009-10-10 01:29:16 +00005283MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005284SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT, SDValue Op1) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005285 SDVTList VTs = getVTList(VT);
5286 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005287 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005288}
Bill Wendlinga434d932009-01-29 09:01:55 +00005289
Dan Gohmana22f2d82009-10-10 01:29:16 +00005290MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005291SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005292 SDValue Op1, SDValue Op2) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005293 SDVTList VTs = getVTList(VT);
5294 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005295 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005296}
Bill Wendlinga434d932009-01-29 09:01:55 +00005297
Dan Gohmana22f2d82009-10-10 01:29:16 +00005298MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005299SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005300 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005301 SDVTList VTs = getVTList(VT);
5302 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005303 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005304}
Bill Wendlinga434d932009-01-29 09:01:55 +00005305
Dan Gohmana22f2d82009-10-10 01:29:16 +00005306MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005307SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT,
Michael Liaob53d8962013-04-19 22:22:57 +00005308 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005309 SDVTList VTs = getVTList(VT);
Michael Liaob53d8962013-04-19 22:22:57 +00005310 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005311}
Bill Wendlinga434d932009-01-29 09:01:55 +00005312
Dan Gohmana22f2d82009-10-10 01:29:16 +00005313MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005314SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1, EVT VT2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005315 SDVTList VTs = getVTList(VT1, VT2);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00005316 return getMachineNode(Opcode, dl, VTs, None);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005317}
Bill Wendlinga434d932009-01-29 09:01:55 +00005318
Dan Gohmana22f2d82009-10-10 01:29:16 +00005319MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005320SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005321 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmande912e22009-04-09 23:54:40 +00005322 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman48b185d2009-09-25 20:36:54 +00005323 SDValue Ops[] = { Op1 };
Michael Liaob53d8962013-04-19 22:22:57 +00005324 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005325}
Bill Wendlinga434d932009-01-29 09:01:55 +00005326
Dan Gohmana22f2d82009-10-10 01:29:16 +00005327MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005328SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005329 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005330 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005331 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005332 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005333}
5334
Dan Gohmana22f2d82009-10-10 01:29:16 +00005335MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005336SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005337 EVT VT1, EVT VT2, SDValue Op1,
5338 SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005339 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendlinga434d932009-01-29 09:01:55 +00005340 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005341 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005342}
5343
Dan Gohmana22f2d82009-10-10 01:29:16 +00005344MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005345SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005346 EVT VT1, EVT VT2,
Michael Liaob53d8962013-04-19 22:22:57 +00005347 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005348 SDVTList VTs = getVTList(VT1, VT2);
Michael Liaob53d8962013-04-19 22:22:57 +00005349 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005350}
Bill Wendlinga434d932009-01-29 09:01:55 +00005351
Dan Gohmana22f2d82009-10-10 01:29:16 +00005352MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005353SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005354 EVT VT1, EVT VT2, EVT VT3,
5355 SDValue Op1, SDValue Op2) {
Dan Gohmande912e22009-04-09 23:54:40 +00005356 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005357 SDValue Ops[] = { Op1, Op2 };
Michael Liaob53d8962013-04-19 22:22:57 +00005358 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005359}
Bill Wendlinga434d932009-01-29 09:01:55 +00005360
Dan Gohmana22f2d82009-10-10 01:29:16 +00005361MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005362SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005363 EVT VT1, EVT VT2, EVT VT3,
5364 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmande912e22009-04-09 23:54:40 +00005365 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendlinga434d932009-01-29 09:01:55 +00005366 SDValue Ops[] = { Op1, Op2, Op3 };
Michael Liaob53d8962013-04-19 22:22:57 +00005367 return getMachineNode(Opcode, dl, VTs, Ops);
Evan Chengd3f1db92006-02-09 07:15:23 +00005368}
Bill Wendlinga434d932009-01-29 09:01:55 +00005369
Dan Gohmana22f2d82009-10-10 01:29:16 +00005370MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005371SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005372 EVT VT1, EVT VT2, EVT VT3,
Michael Liaob53d8962013-04-19 22:22:57 +00005373 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005374 SDVTList VTs = getVTList(VT1, VT2, VT3);
Michael Liaob53d8962013-04-19 22:22:57 +00005375 return getMachineNode(Opcode, dl, VTs, Ops);
Dale Johannesen839acbb2009-01-29 00:47:48 +00005376}
Bill Wendlinga434d932009-01-29 09:01:55 +00005377
Dan Gohmana22f2d82009-10-10 01:29:16 +00005378MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005379SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl, EVT VT1,
Dan Gohmana22f2d82009-10-10 01:29:16 +00005380 EVT VT2, EVT VT3, EVT VT4,
Michael Liaob53d8962013-04-19 22:22:57 +00005381 ArrayRef<SDValue> Ops) {
Dan Gohmande912e22009-04-09 23:54:40 +00005382 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Michael Liaob53d8962013-04-19 22:22:57 +00005383 return getMachineNode(Opcode, dl, VTs, Ops);
Bill Wendlinga434d932009-01-29 09:01:55 +00005384}
5385
Dan Gohmana22f2d82009-10-10 01:29:16 +00005386MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005387SelectionDAG::getMachineNode(unsigned Opcode, SDLoc dl,
Benjamin Kramerfdf362b2013-03-07 20:33:29 +00005388 ArrayRef<EVT> ResultTys,
Michael Liaob53d8962013-04-19 22:22:57 +00005389 ArrayRef<SDValue> Ops) {
Dan Gohman48b185d2009-09-25 20:36:54 +00005390 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
Michael Liaob53d8962013-04-19 22:22:57 +00005391 return getMachineNode(Opcode, dl, VTs, Ops);
Dan Gohman48b185d2009-09-25 20:36:54 +00005392}
5393
Dan Gohmana22f2d82009-10-10 01:29:16 +00005394MachineSDNode *
Andrew Trickef9de2a2013-05-25 02:42:55 +00005395SelectionDAG::getMachineNode(unsigned Opcode, SDLoc DL, SDVTList VTs,
Michael Liaob53d8962013-04-19 22:22:57 +00005396 ArrayRef<SDValue> OpsArray) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005397 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
Dan Gohman48b185d2009-09-25 20:36:54 +00005398 MachineSDNode *N;
Ted Kremenek3c4408c2011-01-23 17:05:06 +00005399 void *IP = 0;
Michael Liaob53d8962013-04-19 22:22:57 +00005400 const SDValue *Ops = OpsArray.data();
5401 unsigned NumOps = OpsArray.size();
Dan Gohman48b185d2009-09-25 20:36:54 +00005402
5403 if (DoCSE) {
5404 FoldingSetNodeID ID;
5405 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
5406 IP = 0;
Devang Patel7bbc1e52011-12-15 18:21:18 +00005407 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005408 return cast<MachineSDNode>(UpdadeSDLocOnMergedSDNode(E, DL));
Devang Patel7bbc1e52011-12-15 18:21:18 +00005409 }
Dan Gohman48b185d2009-09-25 20:36:54 +00005410 }
5411
5412 // Allocate a new MachineSDNode.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005413 N = new (NodeAllocator) MachineSDNode(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
Dan Gohman48b185d2009-09-25 20:36:54 +00005414
5415 // Initialize the operands list.
5416 if (NumOps > array_lengthof(N->LocalOperands))
5417 // We're creating a final node that will live unmorphed for the
5418 // remainder of the current SelectionDAG iteration, so we can allocate
5419 // the operands directly out of a pool with no recycling metadata.
5420 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
5421 Ops, NumOps);
5422 else
5423 N->InitOperands(N->LocalOperands, Ops, NumOps);
5424 N->OperandsNeedDelete = false;
5425
5426 if (DoCSE)
5427 CSEMap.InsertNode(N, IP);
5428
5429 AllNodes.push_back(N);
5430#ifndef NDEBUG
Duncan Sands7c601de2010-11-20 11:25:00 +00005431 VerifyMachineNode(N);
Dan Gohman48b185d2009-09-25 20:36:54 +00005432#endif
5433 return N;
Dale Johannesen839acbb2009-01-29 00:47:48 +00005434}
Evan Chengd3f1db92006-02-09 07:15:23 +00005435
Dan Gohmanac33a902009-08-19 18:16:17 +00005436/// getTargetExtractSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005437/// TargetOpcode::EXTRACT_SUBREG nodes.
Dan Gohmanac33a902009-08-19 18:16:17 +00005438SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005439SelectionDAG::getTargetExtractSubreg(int SRIdx, SDLoc DL, EVT VT,
Dan Gohmanac33a902009-08-19 18:16:17 +00005440 SDValue Operand) {
5441 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005442 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
Dan Gohman32f71d72009-09-25 18:54:59 +00005443 VT, Operand, SRIdxVal);
Dan Gohmanac33a902009-08-19 18:16:17 +00005444 return SDValue(Subreg, 0);
5445}
5446
Bob Wilson2a45a652009-10-08 18:49:46 +00005447/// getTargetInsertSubreg - A convenience function for creating
Chris Lattnerb06015a2010-02-09 19:54:29 +00005448/// TargetOpcode::INSERT_SUBREG nodes.
Bob Wilson2a45a652009-10-08 18:49:46 +00005449SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005450SelectionDAG::getTargetInsertSubreg(int SRIdx, SDLoc DL, EVT VT,
Bob Wilson2a45a652009-10-08 18:49:46 +00005451 SDValue Operand, SDValue Subreg) {
5452 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Chris Lattnerb06015a2010-02-09 19:54:29 +00005453 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
Bob Wilson2a45a652009-10-08 18:49:46 +00005454 VT, Operand, Subreg, SRIdxVal);
5455 return SDValue(Result, 0);
5456}
5457
Evan Cheng31604a62008-03-22 01:55:50 +00005458/// getNodeIfExists - Get the specified node if it's already available, or
5459/// else return NULL.
5460SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005461 const SDValue *Ops, unsigned NumOps) {
Chris Lattner3e5fbd72010-12-21 02:38:05 +00005462 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
Evan Cheng31604a62008-03-22 01:55:50 +00005463 FoldingSetNodeID ID;
5464 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
5465 void *IP = 0;
Bill Wendling022d18f2009-12-18 23:32:53 +00005466 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng31604a62008-03-22 01:55:50 +00005467 return E;
5468 }
5469 return NULL;
5470}
5471
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005472/// getDbgValue - Creates a SDDbgValue node.
5473///
5474SDDbgValue *
5475SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
5476 DebugLoc DL, unsigned O) {
5477 return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
5478}
5479
5480SDDbgValue *
Dan Gohmanbcaf6812010-04-15 01:51:59 +00005481SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
Evan Cheng4d1aa2a2010-03-29 20:48:30 +00005482 DebugLoc DL, unsigned O) {
5483 return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
5484}
5485
5486SDDbgValue *
5487SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
5488 DebugLoc DL, unsigned O) {
5489 return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
5490}
5491
Dan Gohman7d099f72010-03-03 21:33:37 +00005492namespace {
5493
Dan Gohman9cc886b2010-03-04 19:11:28 +00005494/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
Dan Gohman7d099f72010-03-03 21:33:37 +00005495/// pointed to by a use iterator is deleted, increment the use iterator
5496/// so that it doesn't dangle.
5497///
Dan Gohman7d099f72010-03-03 21:33:37 +00005498class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
Dan Gohman7d099f72010-03-03 21:33:37 +00005499 SDNode::use_iterator &UI;
5500 SDNode::use_iterator &UE;
5501
5502 virtual void NodeDeleted(SDNode *N, SDNode *E) {
5503 // Increment the iterator as needed.
5504 while (UI != UE && N == *UI)
5505 ++UI;
Dan Gohman7d099f72010-03-03 21:33:37 +00005506 }
5507
5508public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005509 RAUWUpdateListener(SelectionDAG &d,
Dan Gohman7d099f72010-03-03 21:33:37 +00005510 SDNode::use_iterator &ui,
5511 SDNode::use_iterator &ue)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005512 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
Dan Gohman7d099f72010-03-03 21:33:37 +00005513};
5514
5515}
5516
Evan Cheng445b91a2006-08-07 22:13:29 +00005517/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005518/// This can cause recursive merging of nodes in the DAG.
5519///
Chris Lattner76858912008-02-03 03:35:22 +00005520/// This version assumes From has a single result value.
Chris Lattner373f0482005-08-26 18:36:28 +00005521///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005522void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005523 SDNode *From = FromN.getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005524 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner373f0482005-08-26 18:36:28 +00005525 "Cannot replace with this method!");
Gabor Greiff304a7a2008-08-28 21:40:38 +00005526 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein51f532f2008-04-07 10:06:32 +00005527
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005528 // Iterate over all the existing uses of From. New uses will be added
5529 // to the beginning of the use list, which we avoid visiting.
5530 // This specifically avoids visiting uses of From that arise while the
5531 // replacement is happening, because any such uses would be the result
5532 // of CSE: If an existing node looks like From after one of its operands
5533 // is replaced by To, we don't want to replace of all its users with To
5534 // too. See PR3018 for more info.
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005535 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005536 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005537 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005538 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005539
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005540 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005541 RemoveNodeFromCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005542
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005543 // A user can appear in a use list multiple times, and when this
5544 // happens the uses are usually next to each other in the list.
5545 // To help reduce the number of CSE recomputations, process all
5546 // the uses of this user that we can find this way.
5547 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005548 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005549 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005550 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005551 } while (UI != UE && *UI == User);
5552
5553 // Now that we have modified User, add it back to the CSE maps. If it
5554 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005555 AddModifiedNodeToCSEMaps(User);
Chris Lattner373f0482005-08-26 18:36:28 +00005556 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005557
5558 // If we just RAUW'd the root, take note.
5559 if (FromN == getRoot())
5560 setRoot(To);
Chris Lattner373f0482005-08-26 18:36:28 +00005561}
5562
5563/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5564/// This can cause recursive merging of nodes in the DAG.
5565///
Dan Gohman8aa28b92009-04-15 20:06:30 +00005566/// This version assumes that for each value of From, there is a
5567/// corresponding value in To in the same position with the same type.
Chris Lattner373f0482005-08-26 18:36:28 +00005568///
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005569void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
Dan Gohman8aa28b92009-04-15 20:06:30 +00005570#ifndef NDEBUG
5571 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
5572 assert((!From->hasAnyUseOfValue(i) ||
5573 From->getValueType(i) == To->getValueType(i)) &&
5574 "Cannot use this version of ReplaceAllUsesWith!");
5575#endif
Dan Gohman17059682008-07-17 19:10:17 +00005576
5577 // Handle the trivial case.
5578 if (From == To)
5579 return;
5580
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005581 // Iterate over just the existing users of From. See the comments in
5582 // the ReplaceAllUsesWith above.
5583 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005584 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005585 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005586 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005587
Chris Lattner373f0482005-08-26 18:36:28 +00005588 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005589 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005590
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005591 // A user can appear in a use list multiple times, and when this
5592 // happens the uses are usually next to each other in the list.
5593 // To help reduce the number of CSE recomputations, process all
5594 // the uses of this user that we can find this way.
5595 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005596 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005597 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005598 Use.setNode(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005599 } while (UI != UE && *UI == User);
5600
5601 // Now that we have modified User, add it back to the CSE maps. If it
5602 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005603 AddModifiedNodeToCSEMaps(User);
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005604 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005605
5606 // If we just RAUW'd the root, take note.
5607 if (From == getRoot().getNode())
5608 setRoot(SDValue(To, getRoot().getResNo()));
Chris Lattnerab0de9d2005-08-17 19:00:20 +00005609}
5610
Chris Lattner373f0482005-08-26 18:36:28 +00005611/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
5612/// This can cause recursive merging of nodes in the DAG.
5613///
5614/// This version can replace From with any result values. To must match the
5615/// number and types of values returned by From.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005616void SelectionDAG::ReplaceAllUsesWith(SDNode *From, const SDValue *To) {
Chris Lattner76858912008-02-03 03:35:22 +00005617 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005618 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005619
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005620 // Iterate over just the existing users of From. See the comments in
5621 // the ReplaceAllUsesWith above.
5622 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005623 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00005624 while (UI != UE) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005625 SDNode *User = *UI;
Roman Levenstein51f532f2008-04-07 10:06:32 +00005626
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005627 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005628 RemoveNodeFromCSEMaps(User);
Roman Levenstein51f532f2008-04-07 10:06:32 +00005629
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005630 // A user can appear in a use list multiple times, and when this
5631 // happens the uses are usually next to each other in the list.
5632 // To help reduce the number of CSE recomputations, process all
5633 // the uses of this user that we can find this way.
5634 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005635 SDUse &Use = UI.getUse();
5636 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005637 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005638 Use.set(ToOp);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005639 } while (UI != UE && *UI == User);
5640
5641 // Now that we have modified User, add it back to the CSE maps. If it
5642 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005643 AddModifiedNodeToCSEMaps(User);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005644 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005645
5646 // If we just RAUW'd the root, take note.
5647 if (From == getRoot().getNode())
5648 setRoot(SDValue(To[getRoot().getResNo()]));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00005649}
5650
Chris Lattner375e1a72006-02-17 21:58:01 +00005651/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005652/// uses of other values produced by From.getNode() alone. The Deleted
5653/// vector is handled the same way as for ReplaceAllUsesWith.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005654void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To){
Dan Gohman17059682008-07-17 19:10:17 +00005655 // Handle the really simple, really trivial case efficiently.
5656 if (From == To) return;
5657
Chris Lattner375e1a72006-02-17 21:58:01 +00005658 // Handle the simple, trivial, case efficiently.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005659 if (From.getNode()->getNumValues() == 1) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005660 ReplaceAllUsesWith(From, To);
Chris Lattner375e1a72006-02-17 21:58:01 +00005661 return;
5662 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00005663
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005664 // Iterate over just the existing users of From. See the comments in
5665 // the ReplaceAllUsesWith above.
5666 SDNode::use_iterator UI = From.getNode()->use_begin(),
5667 UE = From.getNode()->use_end();
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005668 RAUWUpdateListener Listener(*this, UI, UE);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005669 while (UI != UE) {
5670 SDNode *User = *UI;
5671 bool UserRemovedFromCSEMaps = false;
Chris Lattner375e1a72006-02-17 21:58:01 +00005672
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005673 // A user can appear in a use list multiple times, and when this
5674 // happens the uses are usually next to each other in the list.
5675 // To help reduce the number of CSE recomputations, process all
5676 // the uses of this user that we can find this way.
5677 do {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005678 SDUse &Use = UI.getUse();
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005679
5680 // Skip uses of different values from the same node.
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005681 if (Use.getResNo() != From.getResNo()) {
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005682 ++UI;
5683 continue;
Chris Lattner375e1a72006-02-17 21:58:01 +00005684 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005685
5686 // If this node hasn't been modified yet, it's still in the CSE maps,
5687 // so remove its old self from the CSE maps.
5688 if (!UserRemovedFromCSEMaps) {
5689 RemoveNodeFromCSEMaps(User);
5690 UserRemovedFromCSEMaps = true;
5691 }
5692
5693 ++UI;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005694 Use.set(To);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005695 } while (UI != UE && *UI == User);
5696
5697 // We are iterating over all uses of the From node, so if a use
5698 // doesn't use the specific value, no changes are made.
5699 if (!UserRemovedFromCSEMaps)
5700 continue;
5701
Chris Lattner3cfb56d2007-10-15 06:10:22 +00005702 // Now that we have modified User, add it back to the CSE maps. If it
5703 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005704 AddModifiedNodeToCSEMaps(User);
Dan Gohman17059682008-07-17 19:10:17 +00005705 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005706
5707 // If we just RAUW'd the root, take note.
5708 if (From == getRoot())
5709 setRoot(To);
Dan Gohman17059682008-07-17 19:10:17 +00005710}
5711
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005712namespace {
5713 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
5714 /// to record information about a use.
5715 struct UseMemo {
5716 SDNode *User;
5717 unsigned Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005718 SDUse *Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005719 };
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005720
5721 /// operator< - Sort Memos by User.
5722 bool operator<(const UseMemo &L, const UseMemo &R) {
5723 return (intptr_t)L.User < (intptr_t)R.User;
5724 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005725}
5726
Dan Gohman17059682008-07-17 19:10:17 +00005727/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005728/// uses of other values produced by From.getNode() alone. The same value
5729/// may appear in both the From and To list. The Deleted vector is
Dan Gohman17059682008-07-17 19:10:17 +00005730/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005731void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5732 const SDValue *To,
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005733 unsigned Num){
Dan Gohman17059682008-07-17 19:10:17 +00005734 // Handle the simple, trivial case efficiently.
5735 if (Num == 1)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005736 return ReplaceAllUsesOfValueWith(*From, *To);
Dan Gohman17059682008-07-17 19:10:17 +00005737
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005738 // Read up all the uses and make records of them. This helps
5739 // processing new uses that are introduced during the
5740 // replacement process.
5741 SmallVector<UseMemo, 4> Uses;
5742 for (unsigned i = 0; i != Num; ++i) {
5743 unsigned FromResNo = From[i].getResNo();
5744 SDNode *FromNode = From[i].getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005745 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005746 E = FromNode->use_end(); UI != E; ++UI) {
5747 SDUse &Use = UI.getUse();
5748 if (Use.getResNo() == FromResNo) {
5749 UseMemo Memo = { *UI, i, &Use };
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005750 Uses.push_back(Memo);
5751 }
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005752 }
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005753 }
Dan Gohman17059682008-07-17 19:10:17 +00005754
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005755 // Sort the uses, so that all the uses from a given User are together.
5756 std::sort(Uses.begin(), Uses.end());
5757
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005758 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5759 UseIndex != UseIndexEnd; ) {
Dan Gohman17059682008-07-17 19:10:17 +00005760 // We know that this user uses some value of From. If it is the right
5761 // value, update it.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005762 SDNode *User = Uses[UseIndex].User;
5763
5764 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman17059682008-07-17 19:10:17 +00005765 RemoveNodeFromCSEMaps(User);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005766
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005767 // The Uses array is sorted, so all the uses for a given User
5768 // are next to each other in the list.
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005769 // To help reduce the number of CSE recomputations, process all
5770 // the uses of this user that we can find this way.
5771 do {
5772 unsigned i = Uses[UseIndex].Index;
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005773 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005774 ++UseIndex;
5775
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005776 Use.set(To[i]);
Dan Gohmanf1d38be2009-01-25 16:29:12 +00005777 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5778
Dan Gohman17059682008-07-17 19:10:17 +00005779 // Now that we have modified User, add it back to the CSE maps. If it
5780 // already exists there, recursively merge the results together.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005781 AddModifiedNodeToCSEMaps(User);
Chris Lattner375e1a72006-02-17 21:58:01 +00005782 }
5783}
5784
Evan Cheng9631a602006-08-01 08:20:41 +00005785/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengbba1ebd2006-08-02 22:00:34 +00005786/// based on their topological order. It returns the maximum id and a vector
5787/// of the SDNodes* in assigned order by reference.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005788unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengbba1ebd2006-08-02 22:00:34 +00005789
Dan Gohman86aa16a2008-09-30 18:30:35 +00005790 unsigned DAGSize = 0;
Evan Cheng9631a602006-08-01 08:20:41 +00005791
Dan Gohman86aa16a2008-09-30 18:30:35 +00005792 // SortedPos tracks the progress of the algorithm. Nodes before it are
5793 // sorted, nodes after it are unsorted. When the algorithm completes
5794 // it is at the end of the list.
5795 allnodes_iterator SortedPos = allnodes_begin();
5796
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005797 // Visit all the nodes. Move nodes with no operands to the front of
5798 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman86aa16a2008-09-30 18:30:35 +00005799 // operand count. Before we do this, the Node Id fields of the nodes
5800 // may contain arbitrary values. After, the Node Id fields for nodes
5801 // before SortedPos will contain the topological sort index, and the
5802 // Node Id fields for nodes At SortedPos and after will contain the
5803 // count of outstanding operands.
5804 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5805 SDNode *N = I++;
David Greene09851602010-01-20 20:13:31 +00005806 checkForCycles(N);
Dan Gohman86aa16a2008-09-30 18:30:35 +00005807 unsigned Degree = N->getNumOperands();
5808 if (Degree == 0) {
5809 // A node with no uses, add it to the result array immediately.
5810 N->setNodeId(DAGSize++);
5811 allnodes_iterator Q = N;
5812 if (Q != SortedPos)
5813 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
David Greene3b2a68c2010-01-20 00:59:23 +00005814 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005815 ++SortedPos;
5816 } else {
5817 // Temporarily use the Node Id as scratch space for the degree count.
5818 N->setNodeId(Degree);
Evan Cheng9631a602006-08-01 08:20:41 +00005819 }
5820 }
5821
Chad Rosier5d1f5d22012-05-21 17:13:41 +00005822 // Visit all the nodes. As we iterate, move nodes into sorted order,
Dan Gohman86aa16a2008-09-30 18:30:35 +00005823 // such that by the time the end is reached all nodes will be sorted.
5824 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5825 SDNode *N = I;
David Greene09851602010-01-20 20:13:31 +00005826 checkForCycles(N);
David Greene3b2a68c2010-01-20 00:59:23 +00005827 // N is in sorted position, so all its uses have one less operand
5828 // that needs to be sorted.
Dan Gohman86aa16a2008-09-30 18:30:35 +00005829 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5830 UI != UE; ++UI) {
5831 SDNode *P = *UI;
5832 unsigned Degree = P->getNodeId();
David Greene3b2a68c2010-01-20 00:59:23 +00005833 assert(Degree != 0 && "Invalid node degree");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005834 --Degree;
5835 if (Degree == 0) {
5836 // All of P's operands are sorted, so P may sorted now.
5837 P->setNodeId(DAGSize++);
5838 if (P != SortedPos)
5839 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
David Greene3b2a68c2010-01-20 00:59:23 +00005840 assert(SortedPos != AllNodes.end() && "Overran node list");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005841 ++SortedPos;
5842 } else {
5843 // Update P's outstanding operand count.
5844 P->setNodeId(Degree);
5845 }
5846 }
David Greene3b2a68c2010-01-20 00:59:23 +00005847 if (I == SortedPos) {
David Greene893047d2010-02-09 23:03:05 +00005848#ifndef NDEBUG
5849 SDNode *S = ++I;
5850 dbgs() << "Overran sorted position:\n";
David Greene3b2a68c2010-01-20 00:59:23 +00005851 S->dumprFull();
David Greene893047d2010-02-09 23:03:05 +00005852#endif
5853 llvm_unreachable(0);
David Greene3b2a68c2010-01-20 00:59:23 +00005854 }
Dan Gohman86aa16a2008-09-30 18:30:35 +00005855 }
5856
5857 assert(SortedPos == AllNodes.end() &&
5858 "Topological sort incomplete!");
5859 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5860 "First node in topological sort is not the entry token!");
5861 assert(AllNodes.front().getNodeId() == 0 &&
5862 "First node in topological sort has non-zero id!");
5863 assert(AllNodes.front().getNumOperands() == 0 &&
5864 "First node in topological sort has operands!");
5865 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5866 "Last node in topologic sort has unexpected id!");
5867 assert(AllNodes.back().use_empty() &&
5868 "Last node in topologic sort has users!");
Dan Gohman8dfa51c2008-11-21 19:10:41 +00005869 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman86aa16a2008-09-30 18:30:35 +00005870 return DAGSize;
Evan Cheng9631a602006-08-01 08:20:41 +00005871}
5872
Evan Cheng563fe3c2010-03-25 01:38:16 +00005873/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
5874/// value is produced by SD.
Dale Johannesene0983522010-04-26 20:06:49 +00005875void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
5876 DbgInfo->add(DB, SD, isParameter);
Evan Cheng563fe3c2010-03-25 01:38:16 +00005877 if (SD)
5878 SD->setHasDebugValue(true);
Dale Johannesen49de0602010-03-10 22:13:47 +00005879}
Evan Cheng29eefc12006-07-27 06:39:06 +00005880
Devang Patelefc6b162011-01-25 23:27:42 +00005881/// TransferDbgValues - Transfer SDDbgValues.
5882void SelectionDAG::TransferDbgValues(SDValue From, SDValue To) {
5883 if (From == To || !From.getNode()->getHasDebugValue())
5884 return;
5885 SDNode *FromNode = From.getNode();
5886 SDNode *ToNode = To.getNode();
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00005887 ArrayRef<SDDbgValue *> DVs = GetDbgValues(FromNode);
Devang Patelb7ae3cc2011-02-18 22:43:42 +00005888 SmallVector<SDDbgValue *, 2> ClonedDVs;
Benjamin Kramere1fc29b2011-06-18 13:13:44 +00005889 for (ArrayRef<SDDbgValue *>::iterator I = DVs.begin(), E = DVs.end();
Devang Patelefc6b162011-01-25 23:27:42 +00005890 I != E; ++I) {
Devang Patelb7ae3cc2011-02-18 22:43:42 +00005891 SDDbgValue *Dbg = *I;
5892 if (Dbg->getKind() == SDDbgValue::SDNODE) {
5893 SDDbgValue *Clone = getDbgValue(Dbg->getMDPtr(), ToNode, To.getResNo(),
5894 Dbg->getOffset(), Dbg->getDebugLoc(),
5895 Dbg->getOrder());
5896 ClonedDVs.push_back(Clone);
Devang Patelefc6b162011-01-25 23:27:42 +00005897 }
5898 }
Craig Toppere1c1d362013-07-03 05:11:49 +00005899 for (SmallVectorImpl<SDDbgValue *>::iterator I = ClonedDVs.begin(),
Devang Patelb7ae3cc2011-02-18 22:43:42 +00005900 E = ClonedDVs.end(); I != E; ++I)
5901 AddDbgValue(*I, ToNode, false);
Devang Patelefc6b162011-01-25 23:27:42 +00005902}
5903
Jim Laskeyd66e6162005-08-17 20:08:02 +00005904//===----------------------------------------------------------------------===//
5905// SDNode Class
5906//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00005907
Chris Lattner3bf17b62007-02-04 02:41:42 +00005908HandleSDNode::~HandleSDNode() {
Dan Gohman91697632008-07-07 20:57:48 +00005909 DropOperands();
Chris Lattner3bf17b62007-02-04 02:41:42 +00005910}
5911
Andrew Trickef9de2a2013-05-25 02:42:55 +00005912GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, unsigned Order,
5913 DebugLoc DL, const GlobalValue *GA,
Owen Anderson53aa7a92009-08-10 22:56:29 +00005914 EVT VT, int64_t o, unsigned char TF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005915 : SDNode(Opc, Order, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
Dan Gohman8422e572010-04-17 15:32:28 +00005916 TheGlobal = GA;
Lauro Ramos Venancio4e919082007-04-21 20:56:26 +00005917}
Chris Lattner3bf17b62007-02-04 02:41:42 +00005918
Andrew Trickef9de2a2013-05-25 02:42:55 +00005919MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
5920 EVT memvt, MachineMemOperand *mmo)
5921 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00005922 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005923 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00005924 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
David Greeneb7941b02010-02-17 20:21:42 +00005925 assert(isNonTemporal() == MMO->isNonTemporal() &&
5926 "Non-temporal encoding error!");
Dan Gohman48b185d2009-09-25 20:36:54 +00005927 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen666bf202009-01-28 21:18:29 +00005928}
5929
Andrew Trickef9de2a2013-05-25 02:42:55 +00005930MemSDNode::MemSDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs,
Wesley Peck527da1b2010-11-23 03:31:01 +00005931 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman48b185d2009-09-25 20:36:54 +00005932 MachineMemOperand *mmo)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005933 : SDNode(Opc, Order, dl, VTs, Ops, NumOps),
Dan Gohman48b185d2009-09-25 20:36:54 +00005934 MemoryVT(memvt), MMO(mmo) {
David Greeneb7941b02010-02-17 20:21:42 +00005935 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005936 MMO->isNonTemporal(), MMO->isInvariant());
Dan Gohman48b185d2009-09-25 20:36:54 +00005937 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5938 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6a490372008-06-25 08:15:39 +00005939}
5940
Jim Laskeyf576b422006-10-27 23:46:08 +00005941/// Profile - Gather unique data for the node.
5942///
Dan Gohman2da2bed2008-08-20 15:58:01 +00005943void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskeyf576b422006-10-27 23:46:08 +00005944 AddNodeIDNode(ID, this);
5945}
5946
Owen Anderson3b1665e2009-08-25 22:27:22 +00005947namespace {
5948 struct EVTArray {
5949 std::vector<EVT> VTs;
Wesley Peck527da1b2010-11-23 03:31:01 +00005950
Owen Anderson3b1665e2009-08-25 22:27:22 +00005951 EVTArray() {
5952 VTs.reserve(MVT::LAST_VALUETYPE);
5953 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5954 VTs.push_back(MVT((MVT::SimpleValueType)i));
5955 }
5956 };
5957}
5958
Owen Anderson53aa7a92009-08-10 22:56:29 +00005959static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson3b1665e2009-08-25 22:27:22 +00005960static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner56d60ea2009-08-22 04:07:34 +00005961static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Anderson5defd562009-06-25 17:09:00 +00005962
Chris Lattner88fa11c2005-11-08 23:30:28 +00005963/// getValueTypeList - Return a pointer to the specified value type.
5964///
Owen Anderson53aa7a92009-08-10 22:56:29 +00005965const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands13237ac2008-06-06 12:08:01 +00005966 if (VT.isExtended()) {
Owen Anderson63010bb2009-08-22 06:32:36 +00005967 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Anderson5defd562009-06-25 17:09:00 +00005968 return &(*EVTs->insert(VT).first);
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005969 } else {
Duncan Sands14627772010-11-03 12:17:33 +00005970 assert(VT.getSimpleVT() < MVT::LAST_VALUETYPE &&
Duncan Sandse4d66702010-05-10 04:54:28 +00005971 "Value type out of range!");
Owen Anderson3b1665e2009-08-25 22:27:22 +00005972 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005973 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00005974}
Duncan Sandsbbbfbe92007-10-16 09:56:48 +00005975
Chris Lattner40e79822005-01-12 18:37:47 +00005976/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5977/// indicated value. This method ignores uses of other values defined by this
5978/// operation.
Evan Chengd37645c2006-02-05 06:29:23 +00005979bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner40e79822005-01-12 18:37:47 +00005980 assert(Value < getNumValues() && "Bad value!");
5981
Roman Levenstein51f532f2008-04-07 10:06:32 +00005982 // TODO: Only iterate over uses of a given value of the node
5983 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00005984 if (UI.getUse().getResNo() == Value) {
Roman Levenstein51f532f2008-04-07 10:06:32 +00005985 if (NUses == 0)
5986 return false;
5987 --NUses;
5988 }
Chris Lattner40e79822005-01-12 18:37:47 +00005989 }
5990
5991 // Found exactly the right number of uses?
5992 return NUses == 0;
5993}
5994
5995
Evan Cheng358c3d12007-08-02 05:29:38 +00005996/// hasAnyUseOfValue - Return true if there are any use of the indicated
5997/// value. This method ignores uses of other values defined by this operation.
5998bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5999 assert(Value < getNumValues() && "Bad value!");
6000
Dan Gohman7a510c22008-07-09 22:39:01 +00006001 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006002 if (UI.getUse().getResNo() == Value)
Dan Gohman7a510c22008-07-09 22:39:01 +00006003 return true;
Evan Cheng358c3d12007-08-02 05:29:38 +00006004
6005 return false;
6006}
6007
6008
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006009/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Cheng9456dd82006-11-03 07:31:32 +00006010///
Dan Gohmanbb5f43e2008-07-27 18:06:42 +00006011bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Chengd37645c2006-02-05 06:29:23 +00006012 bool Seen = false;
6013 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00006014 SDNode *User = *I;
Evan Chengd37645c2006-02-05 06:29:23 +00006015 if (User == this)
6016 Seen = true;
6017 else
6018 return false;
6019 }
6020
6021 return Seen;
6022}
6023
Evan Cheng9456dd82006-11-03 07:31:32 +00006024/// isOperand - Return true if this node is an operand of N.
6025///
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006026bool SDValue::isOperandOf(SDNode *N) const {
Evan Cheng23e75f52006-03-03 06:42:32 +00006027 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6028 if (*this == N->getOperand(i))
6029 return true;
6030 return false;
6031}
6032
Evan Cheng567d2e52008-03-04 00:41:45 +00006033bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng6b08ae82006-03-03 06:24:54 +00006034 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006035 if (this == N->OperandList[i].getNode())
Evan Cheng6b08ae82006-03-03 06:24:54 +00006036 return true;
6037 return false;
6038}
Evan Chengd37645c2006-02-05 06:29:23 +00006039
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006040/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelcf0da6c2009-02-17 22:15:04 +00006041/// be a chain) reaches the specified operand without crossing any
Wesley Peck527da1b2010-11-23 03:31:01 +00006042/// side-effecting instructions on any chain path. In practice, this looks
6043/// through token factors and non-volatile loads. In order to remain efficient,
Owen Andersonb92b13d2010-09-18 04:45:14 +00006044/// this only looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006045bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006046 unsigned Depth) const {
6047 if (*this == Dest) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006048
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006049 // Don't search too deeply, we just want to be able to see through
6050 // TokenFactor's etc.
6051 if (Depth == 0) return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006052
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006053 // If this is a token factor, all inputs to the TF happen in parallel. If any
Owen Andersonb92b13d2010-09-18 04:45:14 +00006054 // of the operands of the TF does not reach dest, then we cannot do the xform.
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006055 if (getOpcode() == ISD::TokenFactor) {
6056 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
Owen Andersonb92b13d2010-09-18 04:45:14 +00006057 if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
6058 return false;
6059 return true;
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006060 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006061
Chris Lattner2e50a6f2008-01-16 05:49:24 +00006062 // Loads don't have side effects, look through them.
6063 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
6064 if (!Ld->isVolatile())
6065 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
6066 }
6067 return false;
6068}
6069
Lang Hames5a004992011-07-07 04:31:51 +00006070/// hasPredecessor - Return true if N is a predecessor of this node.
6071/// N is either an operand of this node, or can be reached by recursively
6072/// traversing up the operands.
6073/// NOTE: This is an expensive method. Use it carefully.
6074bool SDNode::hasPredecessor(const SDNode *N) const {
6075 SmallPtrSet<const SDNode *, 32> Visited;
6076 SmallVector<const SDNode *, 16> Worklist;
6077 return hasPredecessorHelper(N, Visited, Worklist);
6078}
Dan Gohmancd139c02009-10-28 03:44:30 +00006079
Craig Topperb94011f2013-07-14 04:42:23 +00006080bool
6081SDNode::hasPredecessorHelper(const SDNode *N,
6082 SmallPtrSet<const SDNode *, 32> &Visited,
6083 SmallVectorImpl<const SDNode *> &Worklist) const {
Lang Hames5a004992011-07-07 04:31:51 +00006084 if (Visited.empty()) {
6085 Worklist.push_back(this);
6086 } else {
6087 // Take a look in the visited set. If we've already encountered this node
6088 // we needn't search further.
6089 if (Visited.count(N))
6090 return true;
6091 }
6092
6093 // Haven't visited N yet. Continue the search.
6094 while (!Worklist.empty()) {
6095 const SDNode *M = Worklist.pop_back_val();
6096 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
6097 SDNode *Op = M->getOperand(i).getNode();
Dan Gohmancd139c02009-10-28 03:44:30 +00006098 if (Visited.insert(Op))
6099 Worklist.push_back(Op);
Lang Hames5a004992011-07-07 04:31:51 +00006100 if (Op == N)
6101 return true;
Dan Gohmancd139c02009-10-28 03:44:30 +00006102 }
Lang Hames5a004992011-07-07 04:31:51 +00006103 }
Dan Gohmancd139c02009-10-28 03:44:30 +00006104
6105 return false;
Evan Chengc176f032006-11-03 03:05:24 +00006106}
6107
Evan Cheng5d9fd972006-10-04 00:56:09 +00006108uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
6109 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmaneffb8942008-09-12 16:56:44 +00006110 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Cheng5d9fd972006-10-04 00:56:09 +00006111}
6112
Mon P Wang32f8bb92009-11-30 02:42:02 +00006113SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
6114 assert(N->getNumValues() == 1 &&
6115 "Can't unroll a vector with multiple results!");
6116
6117 EVT VT = N->getValueType(0);
6118 unsigned NE = VT.getVectorNumElements();
6119 EVT EltVT = VT.getVectorElementType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00006120 SDLoc dl(N);
Mon P Wang32f8bb92009-11-30 02:42:02 +00006121
6122 SmallVector<SDValue, 8> Scalars;
6123 SmallVector<SDValue, 4> Operands(N->getNumOperands());
6124
6125 // If ResNE is 0, fully unroll the vector op.
6126 if (ResNE == 0)
6127 ResNE = NE;
6128 else if (NE > ResNE)
6129 NE = ResNE;
6130
6131 unsigned i;
6132 for (i= 0; i != NE; ++i) {
Bill Wendlingde4b2252010-04-30 22:19:17 +00006133 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
Mon P Wang32f8bb92009-11-30 02:42:02 +00006134 SDValue Operand = N->getOperand(j);
6135 EVT OperandVT = Operand.getValueType();
6136 if (OperandVT.isVector()) {
6137 // A vector operand; extract a single element.
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006138 const TargetLowering *TLI = TM.getTargetLowering();
Mon P Wang32f8bb92009-11-30 02:42:02 +00006139 EVT OperandEltVT = OperandVT.getVectorElementType();
6140 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
6141 OperandEltVT,
6142 Operand,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006143 getConstant(i, TLI->getPointerTy()));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006144 } else {
6145 // A scalar operand; just use it as is.
6146 Operands[j] = Operand;
6147 }
6148 }
6149
6150 switch (N->getOpcode()) {
6151 default:
6152 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6153 &Operands[0], Operands.size()));
6154 break;
Nadav Rotem52202fb2011-09-13 19:17:42 +00006155 case ISD::VSELECT:
6156 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT,
6157 &Operands[0], Operands.size()));
6158 break;
Mon P Wang32f8bb92009-11-30 02:42:02 +00006159 case ISD::SHL:
6160 case ISD::SRA:
6161 case ISD::SRL:
6162 case ISD::ROTL:
6163 case ISD::ROTR:
6164 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
Owen Andersoncd526fa2011-03-07 18:29:47 +00006165 getShiftAmountOperand(Operands[0].getValueType(),
6166 Operands[1])));
Mon P Wang32f8bb92009-11-30 02:42:02 +00006167 break;
Dan Gohman6bd3ef82010-01-09 02:13:55 +00006168 case ISD::SIGN_EXTEND_INREG:
6169 case ISD::FP_ROUND_INREG: {
6170 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
6171 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
6172 Operands[0],
6173 getValueType(ExtVT)));
6174 }
Mon P Wang32f8bb92009-11-30 02:42:02 +00006175 }
6176 }
6177
6178 for (; i < ResNE; ++i)
6179 Scalars.push_back(getUNDEF(EltVT));
6180
6181 return getNode(ISD::BUILD_VECTOR, dl,
6182 EVT::getVectorVT(*getContext(), EltVT, ResNE),
6183 &Scalars[0], Scalars.size());
6184}
6185
Evan Chengf5938d52009-12-09 01:36:00 +00006186
Wesley Peck527da1b2010-11-23 03:31:01 +00006187/// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
6188/// location that is 'Dist' units away from the location that the 'Base' load
Evan Chengf5938d52009-12-09 01:36:00 +00006189/// is loading from.
Wesley Peck527da1b2010-11-23 03:31:01 +00006190bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
Evan Chengf5938d52009-12-09 01:36:00 +00006191 unsigned Bytes, int Dist) const {
6192 if (LD->getChain() != Base->getChain())
6193 return false;
6194 EVT VT = LD->getValueType(0);
6195 if (VT.getSizeInBits() / 8 != Bytes)
6196 return false;
6197
6198 SDValue Loc = LD->getOperand(1);
6199 SDValue BaseLoc = Base->getOperand(1);
6200 if (Loc.getOpcode() == ISD::FrameIndex) {
6201 if (BaseLoc.getOpcode() != ISD::FrameIndex)
6202 return false;
6203 const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
6204 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
6205 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
6206 int FS = MFI->getObjectSize(FI);
6207 int BFS = MFI->getObjectSize(BFI);
6208 if (FS != BFS || FS != (int)Bytes) return false;
6209 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
6210 }
Chris Lattner46c01a32011-02-13 22:25:43 +00006211
6212 // Handle X+C
6213 if (isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc &&
6214 cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes)
6215 return true;
Evan Chengf5938d52009-12-09 01:36:00 +00006216
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006217 const GlobalValue *GV1 = NULL;
6218 const GlobalValue *GV2 = NULL;
Evan Chengf5938d52009-12-09 01:36:00 +00006219 int64_t Offset1 = 0;
6220 int64_t Offset2 = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006221 const TargetLowering *TLI = TM.getTargetLowering();
6222 bool isGA1 = TLI->isGAPlusOffset(Loc.getNode(), GV1, Offset1);
6223 bool isGA2 = TLI->isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
Evan Chengf5938d52009-12-09 01:36:00 +00006224 if (isGA1 && isGA2 && GV1 == GV2)
6225 return Offset1 == (Offset2 + Dist*Bytes);
6226 return false;
6227}
6228
6229
Evan Cheng34a23ea2009-12-09 01:04:59 +00006230/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
6231/// it cannot be inferred.
Evan Cheng17500092009-12-09 01:10:37 +00006232unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengd938faf2009-12-09 01:53:58 +00006233 // If this is a GlobalAddress + cst, return the alignment.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00006234 const GlobalValue *GV;
Evan Chengd938faf2009-12-09 01:53:58 +00006235 int64_t GVOffset = 0;
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006236 const TargetLowering *TLI = TM.getTargetLowering();
6237 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
6238 unsigned PtrWidth = TLI->getPointerTy().getSizeInBits();
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006239 APInt KnownZero(PtrWidth, 0), KnownOne(PtrWidth, 0);
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00006240 llvm::ComputeMaskedBits(const_cast<GlobalValue*>(GV), KnownZero, KnownOne,
Bill Wendlinga3cd3502013-06-19 21:36:55 +00006241 TLI->getDataLayout());
Eli Friedmane7ab1a22011-11-28 22:48:22 +00006242 unsigned AlignBits = KnownZero.countTrailingOnes();
6243 unsigned Align = AlignBits ? 1 << std::min(31U, AlignBits) : 0;
6244 if (Align)
6245 return MinAlign(Align, GVOffset);
Evan Cheng43cd9e32010-04-01 06:04:33 +00006246 }
Evan Chengd938faf2009-12-09 01:53:58 +00006247
Evan Cheng34a23ea2009-12-09 01:04:59 +00006248 // If this is a direct reference to a stack slot, use information about the
6249 // stack slot's alignment.
6250 int FrameIdx = 1 << 31;
6251 int64_t FrameOffset = 0;
6252 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
6253 FrameIdx = FI->getIndex();
Chris Lattner46c01a32011-02-13 22:25:43 +00006254 } else if (isBaseWithConstantOffset(Ptr) &&
Evan Cheng34a23ea2009-12-09 01:04:59 +00006255 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
Chris Lattner46c01a32011-02-13 22:25:43 +00006256 // Handle FI+Cst
Evan Cheng34a23ea2009-12-09 01:04:59 +00006257 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
6258 FrameOffset = Ptr.getConstantOperandVal(1);
6259 }
6260
6261 if (FrameIdx != (1 << 31)) {
Evan Cheng34a23ea2009-12-09 01:04:59 +00006262 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
Evan Cheng2d412f02009-12-09 01:17:24 +00006263 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
6264 FrameOffset);
Evan Cheng2d412f02009-12-09 01:17:24 +00006265 return FIInfoAlign;
Evan Cheng34a23ea2009-12-09 01:04:59 +00006266 }
6267
6268 return 0;
6269}
6270
Sanjiv Guptaccd30942009-04-29 04:43:24 +00006271// getAddressSpace - Return the address space this GlobalAddress belongs to.
6272unsigned GlobalAddressSDNode::getAddressSpace() const {
6273 return getGlobal()->getType()->getAddressSpace();
6274}
6275
6276
Chris Lattner229907c2011-07-18 04:54:35 +00006277Type *ConstantPoolSDNode::getType() const {
Evan Cheng45fe3bc2006-09-12 21:00:35 +00006278 if (isMachineConstantPoolEntry())
6279 return Val.MachineCPVal->getType();
6280 return Val.ConstVal->getType();
6281}
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006282
Bob Wilson85cefe82009-03-02 23:24:16 +00006283bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
6284 APInt &SplatUndef,
6285 unsigned &SplatBitSize,
6286 bool &HasAnyUndefs,
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006287 unsigned MinSplatBits,
6288 bool isBigEndian) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006289 EVT VT = getValueType(0);
Bob Wilson85cefe82009-03-02 23:24:16 +00006290 assert(VT.isVector() && "Expected a vector type");
6291 unsigned sz = VT.getSizeInBits();
6292 if (MinSplatBits > sz)
6293 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006294
Bob Wilson85cefe82009-03-02 23:24:16 +00006295 SplatValue = APInt(sz, 0);
6296 SplatUndef = APInt(sz, 0);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006297
Bob Wilson85cefe82009-03-02 23:24:16 +00006298 // Get the bits. Bits with undefined values (when the corresponding element
6299 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6300 // in SplatValue. If any of the values are not constant, give up and return
6301 // false.
6302 unsigned int nOps = getNumOperands();
6303 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6304 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006305
6306 for (unsigned j = 0; j < nOps; ++j) {
6307 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006308 SDValue OpVal = getOperand(i);
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006309 unsigned BitPos = j * EltBitSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006310
Bob Wilson85cefe82009-03-02 23:24:16 +00006311 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen5f4eecf2009-11-13 01:45:18 +00006312 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson85cefe82009-03-02 23:24:16 +00006313 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Jay Foad583abbc2010-12-07 08:25:19 +00006314 SplatValue |= CN->getAPIntValue().zextOrTrunc(EltBitSize).
Dan Gohmanecd40a32010-04-12 02:24:01 +00006315 zextOrTrunc(sz) << BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006316 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson5b15d012009-03-04 17:47:01 +00006317 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson85cefe82009-03-02 23:24:16 +00006318 else
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006319 return false;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006320 }
6321
Bob Wilson85cefe82009-03-02 23:24:16 +00006322 // The build_vector is all constants or undefs. Find the smallest element
6323 // size that splats the vector.
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006324
Bob Wilson85cefe82009-03-02 23:24:16 +00006325 HasAnyUndefs = (SplatUndef != 0);
6326 while (sz > 8) {
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006327
Bob Wilson85cefe82009-03-02 23:24:16 +00006328 unsigned HalfSize = sz / 2;
Jay Foad583abbc2010-12-07 08:25:19 +00006329 APInt HighValue = SplatValue.lshr(HalfSize).trunc(HalfSize);
6330 APInt LowValue = SplatValue.trunc(HalfSize);
6331 APInt HighUndef = SplatUndef.lshr(HalfSize).trunc(HalfSize);
6332 APInt LowUndef = SplatUndef.trunc(HalfSize);
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006333
Bob Wilson85cefe82009-03-02 23:24:16 +00006334 // If the two halves do not match (ignoring undef bits), stop here.
6335 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6336 MinSplatBits > HalfSize)
6337 break;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006338
Bob Wilson85cefe82009-03-02 23:24:16 +00006339 SplatValue = HighValue | LowValue;
6340 SplatUndef = HighUndef & LowUndef;
Eric Christopherdfda92b2009-08-22 00:40:45 +00006341
Bob Wilson85cefe82009-03-02 23:24:16 +00006342 sz = HalfSize;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006343 }
6344
Bob Wilson85cefe82009-03-02 23:24:16 +00006345 SplatBitSize = sz;
Bob Wilsond8ea0e12009-03-01 01:13:55 +00006346 return true;
6347}
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006348
Owen Anderson53aa7a92009-08-10 22:56:29 +00006349bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5f829d82009-04-29 05:20:52 +00006350 // Find the first non-undef value in the shuffle mask.
6351 unsigned i, e;
6352 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6353 /* search */;
6354
Nate Begeman39b59db2009-04-29 18:13:31 +00006355 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopherdfda92b2009-08-22 00:40:45 +00006356
Nate Begeman5f829d82009-04-29 05:20:52 +00006357 // Make sure all remaining elements are either undef or the same as the first
6358 // non-undef value.
6359 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006360 if (Mask[i] >= 0 && Mask[i] != Idx)
6361 return false;
Nate Begeman8d6d4b92009-04-27 18:41:29 +00006362 return true;
6363}
David Greene09851602010-01-20 20:13:31 +00006364
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006365#ifdef XDEBUG
David Greene09851602010-01-20 20:13:31 +00006366static void checkForCyclesHelper(const SDNode *N,
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006367 SmallPtrSet<const SDNode*, 32> &Visited,
6368 SmallPtrSet<const SDNode*, 32> &Checked) {
6369 // If this node has already been checked, don't check it again.
6370 if (Checked.count(N))
David Greened8ecd5e2010-02-23 17:37:50 +00006371 return;
Wesley Peck527da1b2010-11-23 03:31:01 +00006372
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006373 // If a node has already been visited on this depth-first walk, reject it as
6374 // a cycle.
6375 if (!Visited.insert(N)) {
David Greene09851602010-01-20 20:13:31 +00006376 dbgs() << "Offending node:\n";
6377 N->dumprFull();
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006378 errs() << "Detected cycle in SelectionDAG\n";
6379 abort();
David Greene09851602010-01-20 20:13:31 +00006380 }
Wesley Peck527da1b2010-11-23 03:31:01 +00006381
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006382 for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
6383 checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
Wesley Peck527da1b2010-11-23 03:31:01 +00006384
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006385 Checked.insert(N);
6386 Visited.erase(N);
David Greene09851602010-01-20 20:13:31 +00006387}
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006388#endif
David Greene09851602010-01-20 20:13:31 +00006389
6390void llvm::checkForCycles(const llvm::SDNode *N) {
6391#ifdef XDEBUG
6392 assert(N && "Checking nonexistant SDNode");
Chris Lattner9b7cfd32010-02-24 21:34:04 +00006393 SmallPtrSet<const SDNode*, 32> visited;
6394 SmallPtrSet<const SDNode*, 32> checked;
David Greened8ecd5e2010-02-23 17:37:50 +00006395 checkForCyclesHelper(N, visited, checked);
David Greene09851602010-01-20 20:13:31 +00006396#endif
6397}
6398
6399void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
6400 checkForCycles(DAG->getRoot().getNode());
6401}