blob: c38c79b14597a745a38779c567ea82977d5b1876 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAG class.
11//
12//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013#include "llvm/CodeGen/SelectionDAG.h"
14#include "llvm/Constants.h"
Evan Cheng833501d2008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Dan Gohman45f13d42009-08-11 16:02:12 +000016#include "llvm/Function.h"
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +000017#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/GlobalVariable.h"
19#include "llvm/Intrinsics.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Assembly/Writer.h"
Dan Gohmane8b391e2008-04-12 04:36:06 +000022#include "llvm/CallingConv.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner53f5aee2007-10-15 17:47:20 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetLowering.h"
Dan Gohman3d015562009-01-22 21:58:43 +000031#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesen75dbdd12009-10-15 18:50:03 +000033#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Target/TargetMachine.h"
Bill Wendling5db7ffb2008-09-30 21:22:07 +000035#include "llvm/Support/CommandLine.h"
Edwin Török675d5622009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Owen Andersonef31ae32009-06-25 17:09:00 +000037#include "llvm/Support/ManagedStatic.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000038#include "llvm/Support/MathExtras.h"
39#include "llvm/Support/raw_ostream.h"
Owen Andersonef31ae32009-06-25 17:09:00 +000040#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/SetVector.h"
42#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsa9810f32007-10-16 09:56:48 +000043#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/ADT/SmallVector.h"
45#include "llvm/ADT/StringExtras.h"
46#include <algorithm>
47#include <cmath>
48using namespace llvm;
49
50/// makeVTList - Return an instance of the SDVTList struct initialized with the
51/// specified members.
Owen Andersonac9de032009-08-10 22:56:29 +000052static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 SDVTList Res = {VTs, NumVTs};
54 return Res;
55}
56
Owen Andersonac9de032009-08-10 22:56:29 +000057static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000058 switch (VT.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +000059 default: llvm_unreachable("Unknown FP format");
Owen Anderson36e3a6e2009-08-11 20:47:22 +000060 case MVT::f32: return &APFloat::IEEEsingle;
61 case MVT::f64: return &APFloat::IEEEdouble;
62 case MVT::f80: return &APFloat::x87DoubleExtended;
63 case MVT::f128: return &APFloat::IEEEquad;
64 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerd037d482008-03-05 06:48:13 +000065 }
66}
67
Chris Lattner7bcb18f2008-02-03 06:49:24 +000068SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
69
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070//===----------------------------------------------------------------------===//
71// ConstantFPSDNode Class
72//===----------------------------------------------------------------------===//
73
74/// isExactlyValue - We don't rely on operator== working on double values, as
75/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
76/// As such, this method can be used to do an exact bit-for-bit comparison of
77/// two floating point values.
Dale Johannesenc53301c2007-08-26 01:18:27 +000078bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohmanc1f3a072008-09-12 18:08:03 +000079 return getValueAPF().bitwiseIsEqual(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080}
81
Owen Andersonac9de032009-08-10 22:56:29 +000082bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenbbe2b702007-08-30 00:23:21 +000083 const APFloat& Val) {
Duncan Sands92c43912008-06-06 12:08:01 +000084 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michel91099d62009-02-17 22:15:04 +000085
Dale Johannesen90c25d12008-04-20 18:23:46 +000086 // PPC long double cannot be converted to any other type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000087 if (VT == MVT::ppcf128 ||
Dale Johannesen90c25d12008-04-20 18:23:46 +000088 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerd037d482008-03-05 06:48:13 +000089 return false;
Scott Michel91099d62009-02-17 22:15:04 +000090
Dale Johannesenbbe2b702007-08-30 00:23:21 +000091 // convert modifies in place, so make a copy.
92 APFloat Val2 = APFloat(Val);
Dale Johannesen6e547b42008-10-09 23:00:39 +000093 bool losesInfo;
Owen Andersonac9de032009-08-10 22:56:29 +000094 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen6e547b42008-10-09 23:00:39 +000095 &losesInfo);
96 return !losesInfo;
Dale Johannesenbbe2b702007-08-30 00:23:21 +000097}
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099//===----------------------------------------------------------------------===//
100// ISD Namespace
101//===----------------------------------------------------------------------===//
102
103/// isBuildVectorAllOnes - Return true if the specified node is a
104/// BUILD_VECTOR where all of the elements are ~0 or undef.
105bool ISD::isBuildVectorAllOnes(const SDNode *N) {
106 // Look through a bit convert.
107 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greif1c80d112008-08-28 21:40:38 +0000108 N = N->getOperand(0).getNode();
Scott Michel91099d62009-02-17 22:15:04 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 unsigned i = 0, e = N->getNumOperands();
Scott Michel91099d62009-02-17 22:15:04 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 // Skip over all of the undef values.
115 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
116 ++i;
Scott Michel91099d62009-02-17 22:15:04 +0000117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // Do not accept an all-undef vector.
119 if (i == e) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // Do not accept build_vectors that aren't all constants or which have non-~0
122 // elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000123 SDValue NotZero = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 if (isa<ConstantSDNode>(NotZero)) {
125 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
126 return false;
127 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman161652c2008-02-29 01:47:35 +0000128 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen6e547b42008-10-09 23:00:39 +0000129 bitcastToAPInt().isAllOnesValue())
Dan Gohman161652c2008-02-29 01:47:35 +0000130 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 } else
132 return false;
Scott Michel91099d62009-02-17 22:15:04 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 // Okay, we have at least one ~0 value, check to see if the rest match or are
135 // undefs.
136 for (++i; i != e; ++i)
137 if (N->getOperand(i) != NotZero &&
138 N->getOperand(i).getOpcode() != ISD::UNDEF)
139 return false;
140 return true;
141}
142
143
144/// isBuildVectorAllZeros - Return true if the specified node is a
145/// BUILD_VECTOR where all of the elements are 0 or undef.
146bool ISD::isBuildVectorAllZeros(const SDNode *N) {
147 // Look through a bit convert.
148 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greif1c80d112008-08-28 21:40:38 +0000149 N = N->getOperand(0).getNode();
Scott Michel91099d62009-02-17 22:15:04 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000152
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 unsigned i = 0, e = N->getNumOperands();
Scott Michel91099d62009-02-17 22:15:04 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Skip over all of the undef values.
156 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
157 ++i;
Scott Michel91099d62009-02-17 22:15:04 +0000158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 // Do not accept an all-undef vector.
160 if (i == e) return false;
Scott Michel91099d62009-02-17 22:15:04 +0000161
Dan Gohman2ac69f62009-06-04 16:49:15 +0000162 // Do not accept build_vectors that aren't all constants or which have non-0
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 // elements.
Dan Gohman8181bd12008-07-27 21:46:04 +0000164 SDValue Zero = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 if (isa<ConstantSDNode>(Zero)) {
166 if (!cast<ConstantSDNode>(Zero)->isNullValue())
167 return false;
168 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johannesendf8a8312007-08-31 04:03:46 +0000169 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 return false;
171 } else
172 return false;
Scott Michel91099d62009-02-17 22:15:04 +0000173
Dan Gohman2ac69f62009-06-04 16:49:15 +0000174 // Okay, we have at least one 0 value, check to see if the rest match or are
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 // undefs.
176 for (++i; i != e; ++i)
177 if (N->getOperand(i) != Zero &&
178 N->getOperand(i).getOpcode() != ISD::UNDEF)
179 return false;
180 return true;
181}
182
Evan Chengd1045a62008-02-18 23:04:32 +0000183/// isScalarToVector - Return true if the specified node is a
184/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
185/// element is not an undef.
186bool ISD::isScalarToVector(const SDNode *N) {
187 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
188 return true;
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR)
191 return false;
192 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
193 return false;
194 unsigned NumElems = N->getNumOperands();
195 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000196 SDValue V = N->getOperand(i);
Evan Chengd1045a62008-02-18 23:04:32 +0000197 if (V.getOpcode() != ISD::UNDEF)
198 return false;
199 }
200 return true;
201}
202
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
204/// when given the operation for (X op Y).
205ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
206 // To perform this operation, we just need to swap the L and G bits of the
207 // operation.
208 unsigned OldL = (Operation >> 2) & 1;
209 unsigned OldG = (Operation >> 1) & 1;
210 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
211 (OldL << 1) | // New G bit
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000212 (OldG << 2)); // New L bit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213}
214
215/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
216/// 'op' is a valid SetCC operation.
217ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
218 unsigned Operation = Op;
219 if (isInteger)
220 Operation ^= 7; // Flip L, G, E bits, but not U.
221 else
222 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000223
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 if (Operation > ISD::SETTRUE2)
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000225 Operation &= ~8; // Don't let N and U bits get set.
226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 return ISD::CondCode(Operation);
228}
229
230
231/// isSignedOp - For an integer comparison, return 1 if the comparison is a
232/// signed operation and 2 if the result is an unsigned comparison. Return zero
233/// if the operation does not depend on the sign of the input (setne and seteq).
234static int isSignedOp(ISD::CondCode Opcode) {
235 switch (Opcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000236 default: llvm_unreachable("Illegal integer setcc operation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 case ISD::SETEQ:
238 case ISD::SETNE: return 0;
239 case ISD::SETLT:
240 case ISD::SETLE:
241 case ISD::SETGT:
242 case ISD::SETGE: return 1;
243 case ISD::SETULT:
244 case ISD::SETULE:
245 case ISD::SETUGT:
246 case ISD::SETUGE: return 2;
247 }
248}
249
250/// getSetCCOrOperation - Return the result of a logical OR between different
251/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
252/// returns SETCC_INVALID if it is not possible to represent the resultant
253/// comparison.
254ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
255 bool isInteger) {
256 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
257 // Cannot fold a signed integer setcc with an unsigned integer setcc.
258 return ISD::SETCC_INVALID;
259
260 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
261
262 // If the N and U bits get set then the resultant comparison DOES suddenly
263 // care about orderedness, and is true when ordered.
264 if (Op > ISD::SETTRUE2)
265 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michel91099d62009-02-17 22:15:04 +0000266
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 // Canonicalize illegal integer setcc's.
268 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
269 Op = ISD::SETNE;
Scott Michel91099d62009-02-17 22:15:04 +0000270
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 return ISD::CondCode(Op);
272}
273
274/// getSetCCAndOperation - Return the result of a logical AND between different
275/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
276/// function returns zero if it is not possible to represent the resultant
277/// comparison.
278ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
279 bool isInteger) {
280 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
281 // Cannot fold a signed setcc with an unsigned setcc.
282 return ISD::SETCC_INVALID;
283
284 // Combine all of the condition bits.
285 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michel91099d62009-02-17 22:15:04 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 // Canonicalize illegal integer setcc's.
288 if (isInteger) {
289 switch (Result) {
290 default: break;
291 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmanf1e8e552008-05-14 18:17:09 +0000292 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
294 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
295 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
296 }
297 }
Scott Michel91099d62009-02-17 22:15:04 +0000298
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 return Result;
300}
301
302const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman404e8542008-09-04 15:39:15 +0000303 return MF->getTarget();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304}
305
306//===----------------------------------------------------------------------===//
307// SDNode Profile Support
308//===----------------------------------------------------------------------===//
309
310/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
311///
312static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
313 ID.AddInteger(OpC);
314}
315
316/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
317/// solely with their pointer.
Dan Gohman089efff2008-05-13 00:00:25 +0000318static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michel91099d62009-02-17 22:15:04 +0000319 ID.AddPointer(VTList.VTs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320}
321
322/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
323///
324static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman8181bd12008-07-27 21:46:04 +0000325 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 for (; NumOps; --NumOps, ++Ops) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000327 ID.AddPointer(Ops->getNode());
Gabor Greif46bf5472008-08-26 22:36:50 +0000328 ID.AddInteger(Ops->getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 }
330}
331
Dan Gohman703ce5d2008-07-07 18:26:29 +0000332/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
333///
334static void AddNodeIDOperands(FoldingSetNodeID &ID,
335 const SDUse *Ops, unsigned NumOps) {
336 for (; NumOps; --NumOps, ++Ops) {
djgc2517d32009-01-26 04:35:06 +0000337 ID.AddPointer(Ops->getNode());
338 ID.AddInteger(Ops->getResNo());
Dan Gohman703ce5d2008-07-07 18:26:29 +0000339 }
340}
341
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michel91099d62009-02-17 22:15:04 +0000343 unsigned short OpC, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +0000344 const SDValue *OpList, unsigned N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 AddNodeIDOpcode(ID, OpC);
346 AddNodeIDValueTypes(ID, VTList);
347 AddNodeIDOperands(ID, OpList, N);
348}
349
Duncan Sands1e450d42008-10-27 15:30:53 +0000350/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
351/// the NodeID data.
352static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 switch (N->getOpcode()) {
Chris Lattner7123ef72009-06-25 21:21:14 +0000354 case ISD::TargetExternalSymbol:
355 case ISD::ExternalSymbol:
Edwin Törökbd448e32009-07-14 16:55:14 +0000356 llvm_unreachable("Should only be used on nodes with operands");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 default: break; // Normal nodes don't need extra info.
358 case ISD::TargetConstant:
359 case ISD::Constant:
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000360 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 break;
362 case ISD::TargetConstantFP:
Dale Johannesendf8a8312007-08-31 04:03:46 +0000363 case ISD::ConstantFP: {
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000364 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 break;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 case ISD::TargetGlobalAddress:
368 case ISD::GlobalAddress:
369 case ISD::TargetGlobalTLSAddress:
370 case ISD::GlobalTLSAddress: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000371 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 ID.AddPointer(GA->getGlobal());
373 ID.AddInteger(GA->getOffset());
Chris Lattner7123ef72009-06-25 21:21:14 +0000374 ID.AddInteger(GA->getTargetFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 break;
376 }
377 case ISD::BasicBlock:
378 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
379 break;
380 case ISD::Register:
381 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
382 break;
Devang Patel1432b622009-11-21 02:46:55 +0000383
Dan Gohman12a9c082008-02-06 22:27:42 +0000384 case ISD::SRCVALUE:
385 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
386 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 case ISD::FrameIndex:
388 case ISD::TargetFrameIndex:
389 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
390 break;
391 case ISD::JumpTable:
392 case ISD::TargetJumpTable:
393 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattner583c7962009-06-25 21:35:31 +0000394 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 break;
396 case ISD::ConstantPool:
397 case ISD::TargetConstantPool: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000398 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 ID.AddInteger(CP->getAlignment());
400 ID.AddInteger(CP->getOffset());
401 if (CP->isMachineConstantPoolEntry())
402 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
403 else
404 ID.AddPointer(CP->getConstVal());
Chris Lattner583c7962009-06-25 21:35:31 +0000405 ID.AddInteger(CP->getTargetFlags());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 break;
407 }
408 case ISD::LOAD: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000409 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3f5d2432008-06-06 12:49:32 +0000410 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +0000411 ID.AddInteger(LD->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 break;
413 }
414 case ISD::STORE: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000415 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3f5d2432008-06-06 12:49:32 +0000416 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +0000417 ID.AddInteger(ST->getRawSubclassData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 break;
419 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000420 case ISD::ATOMIC_CMP_SWAP:
421 case ISD::ATOMIC_SWAP:
422 case ISD::ATOMIC_LOAD_ADD:
423 case ISD::ATOMIC_LOAD_SUB:
424 case ISD::ATOMIC_LOAD_AND:
425 case ISD::ATOMIC_LOAD_OR:
426 case ISD::ATOMIC_LOAD_XOR:
427 case ISD::ATOMIC_LOAD_NAND:
428 case ISD::ATOMIC_LOAD_MIN:
429 case ISD::ATOMIC_LOAD_MAX:
430 case ISD::ATOMIC_LOAD_UMIN:
431 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohman98beebe2008-08-20 15:58:01 +0000432 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohman9d0f5022009-02-03 00:08:45 +0000433 ID.AddInteger(AT->getMemoryVT().getRawBits());
434 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000435 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 }
Nate Begeman543d2142009-04-27 18:41:29 +0000437 case ISD::VECTOR_SHUFFLE: {
438 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher440d9eb2009-08-22 00:40:45 +0000439 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +0000440 i != e; ++i)
441 ID.AddInteger(SVN->getMaskElt(i));
442 break;
443 }
Dan Gohman91057512009-10-30 01:27:03 +0000444 case ISD::TargetBlockAddress:
445 case ISD::BlockAddress: {
Dan Gohman885793b2009-11-20 23:18:13 +0000446 ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
447 ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
Dan Gohman91057512009-10-30 01:27:03 +0000448 break;
449 }
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000450 } // end switch (N->getOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451}
452
Duncan Sands1e450d42008-10-27 15:30:53 +0000453/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
454/// data.
455static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
456 AddNodeIDOpcode(ID, N->getOpcode());
457 // Add the return value info.
458 AddNodeIDValueTypes(ID, N->getVTList());
459 // Add the operand info.
460 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
461
462 // Handle SDNode leafs with special info.
463 AddNodeIDCustom(ID, N);
464}
465
Dan Gohman98beebe2008-08-20 15:58:01 +0000466/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000467/// the CSE map that carries volatility, indexing mode, and
Dan Gohman9d0f5022009-02-03 00:08:45 +0000468/// extension/truncation information.
Dan Gohman98beebe2008-08-20 15:58:01 +0000469///
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000470static inline unsigned
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000471encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile) {
Dan Gohman9d0f5022009-02-03 00:08:45 +0000472 assert((ConvType & 3) == ConvType &&
473 "ConvType may not require more than 2 bits!");
474 assert((AM & 7) == AM &&
475 "AM may not require more than 3 bits!");
476 return ConvType |
477 (AM << 2) |
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000478 (isVolatile << 5);
Dan Gohman98beebe2008-08-20 15:58:01 +0000479}
480
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481//===----------------------------------------------------------------------===//
482// SelectionDAG Class
483//===----------------------------------------------------------------------===//
484
Duncan Sands1e450d42008-10-27 15:30:53 +0000485/// doNotCSE - Return true if CSE should not be performed for this node.
486static bool doNotCSE(SDNode *N) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000487 if (N->getValueType(0) == MVT::Flag)
Duncan Sands1e450d42008-10-27 15:30:53 +0000488 return true; // Never CSE anything that produces a flag.
489
490 switch (N->getOpcode()) {
491 default: break;
492 case ISD::HANDLENODE:
Duncan Sands1e450d42008-10-27 15:30:53 +0000493 case ISD::EH_LABEL:
Duncan Sands1e450d42008-10-27 15:30:53 +0000494 return true; // Never CSE these nodes.
495 }
496
497 // Check that remaining values produced are not flags.
498 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000499 if (N->getValueType(i) == MVT::Flag)
Duncan Sands1e450d42008-10-27 15:30:53 +0000500 return true; // Never CSE anything that produces a flag.
501
502 return false;
503}
504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505/// RemoveDeadNodes - This method deletes all unreachable nodes in the
506/// SelectionDAG.
507void SelectionDAG::RemoveDeadNodes() {
508 // Create a dummy node (which is not added to allnodes), that adds a reference
509 // to the root node, preventing it from being deleted.
510 HandleSDNode Dummy(getRoot());
511
512 SmallVector<SDNode*, 128> DeadNodes;
Scott Michel91099d62009-02-17 22:15:04 +0000513
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 // Add all obviously-dead nodes to the DeadNodes worklist.
515 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
516 if (I->use_empty())
517 DeadNodes.push_back(I);
518
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000519 RemoveDeadNodes(DeadNodes);
Scott Michel91099d62009-02-17 22:15:04 +0000520
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000521 // If the root changed (e.g. it was a dead load, update the root).
522 setRoot(Dummy.getValue());
523}
524
525/// RemoveDeadNodes - This method deletes the unreachable nodes in the
526/// given list, and any nodes that become unreachable as a result.
527void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
528 DAGUpdateListener *UpdateListener) {
529
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 // Process the worklist, deleting the nodes and adding their uses to the
531 // worklist.
532 while (!DeadNodes.empty()) {
djgc2517d32009-01-26 04:35:06 +0000533 SDNode *N = DeadNodes.pop_back_val();
Scott Michel91099d62009-02-17 22:15:04 +0000534
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000535 if (UpdateListener)
536 UpdateListener->NodeDeleted(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +0000537
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 // Take the node out of the appropriate CSE map.
539 RemoveNodeFromCSEMaps(N);
540
541 // Next, brutally remove the operand list. This is safe to do, as there are
542 // no cycles in the graph.
djgc2517d32009-01-26 04:35:06 +0000543 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
544 SDUse &Use = *I++;
545 SDNode *Operand = Use.getNode();
546 Use.set(SDValue());
547
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 // Now that we removed this operand, see if there are no uses of it left.
549 if (Operand->use_empty())
550 DeadNodes.push_back(Operand);
551 }
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000552
Dan Gohman98a355c2009-01-19 22:39:36 +0000553 DeallocateNode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555}
556
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000557void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +0000558 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohmanb997a4e2008-07-07 20:57:48 +0000559 RemoveDeadNodes(DeadNodes, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560}
561
562void SelectionDAG::DeleteNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 // First take this out of the appropriate CSE map.
564 RemoveNodeFromCSEMaps(N);
565
Scott Michel91099d62009-02-17 22:15:04 +0000566 // Finally, remove uses due to operands of this node, remove from the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 // AllNodes list, and delete the node.
568 DeleteNodeNotInCSEMaps(N);
569}
570
571void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
djgf03abfe2009-01-25 16:20:37 +0000572 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
573 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohman98a355c2009-01-19 22:39:36 +0000574
Dan Gohman2d2a7a32008-09-30 18:30:35 +0000575 // Drop all of the operands and decrement used node's use counts.
djgc2517d32009-01-26 04:35:06 +0000576 N->DropOperands();
Bill Wendlingb75ddd22008-10-19 20:51:12 +0000577
Dan Gohman98a355c2009-01-19 22:39:36 +0000578 DeallocateNode(N);
579}
580
581void SelectionDAG::DeallocateNode(SDNode *N) {
582 if (N->OperandsNeedDelete)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 delete[] N->OperandList;
Scott Michel91099d62009-02-17 22:15:04 +0000584
Dan Gohman98a355c2009-01-19 22:39:36 +0000585 // Set the opcode to DELETED_NODE to help catch bugs when node
586 // memory is reallocated.
587 N->NodeType = ISD::DELETED_NODE;
588
Dan Gohman0b08b152008-08-26 01:44:34 +0000589 NodeAllocator.Deallocate(AllNodes.remove(N));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590}
591
592/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
593/// correspond to it. This is useful when we're about to delete or repurpose
594/// the node. We don't want future request for structurally identical nodes
595/// to return N anymore.
Dan Gohman705e3f72008-09-13 01:54:27 +0000596bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 bool Erased = false;
598 switch (N->getOpcode()) {
Dan Gohman14a66442008-08-23 02:25:05 +0000599 case ISD::EntryToken:
Edwin Törökbd448e32009-07-14 16:55:14 +0000600 llvm_unreachable("EntryToken should not be in CSEMaps!");
Dan Gohman705e3f72008-09-13 01:54:27 +0000601 return false;
602 case ISD::HANDLENODE: return false; // noop.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 case ISD::CONDCODE:
604 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
605 "Cond code doesn't exist!");
606 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
607 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
608 break;
Bill Wendlingfef06052008-09-16 21:48:12 +0000609 case ISD::ExternalSymbol:
610 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 break;
Chris Lattner8430a292009-06-25 18:45:50 +0000612 case ISD::TargetExternalSymbol: {
613 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
614 Erased = TargetExternalSymbols.erase(
615 std::pair<std::string,unsigned char>(ESN->getSymbol(),
616 ESN->getTargetFlags()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 break;
Chris Lattner8430a292009-06-25 18:45:50 +0000618 }
Duncan Sandsd7307a92007-10-17 13:49:58 +0000619 case ISD::VALUETYPE: {
Owen Andersonac9de032009-08-10 22:56:29 +0000620 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands92c43912008-06-06 12:08:01 +0000621 if (VT.isExtended()) {
Duncan Sandsd7307a92007-10-17 13:49:58 +0000622 Erased = ExtendedValueTypeNodes.erase(VT);
623 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000624 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
625 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000626 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 break;
Duncan Sandsd7307a92007-10-17 13:49:58 +0000628 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 default:
630 // Remove it from the CSE Map.
631 Erased = CSEMap.RemoveNode(N);
632 break;
633 }
634#ifndef NDEBUG
Scott Michel91099d62009-02-17 22:15:04 +0000635 // Verify that the node was actually in one of the CSE maps, unless it has a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 // flag result (which cannot be CSE'd) or is one of the special cases that are
637 // not subject to CSE.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000638 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands1e450d42008-10-27 15:30:53 +0000639 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 N->dump(this);
Chris Lattner397f4562009-08-23 06:03:38 +0000641 errs() << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +0000642 llvm_unreachable("Node is not in map!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 }
644#endif
Dan Gohman705e3f72008-09-13 01:54:27 +0000645 return Erased;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646}
647
djg943376a2009-01-25 16:29:12 +0000648/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
649/// maps and modified in place. Add it back to the CSE maps, unless an identical
650/// node already exists, in which case transfer all its users to the existing
651/// node. This transfer can potentially trigger recursive merging.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652///
djg943376a2009-01-25 16:29:12 +0000653void
654SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
655 DAGUpdateListener *UpdateListener) {
656 // For node types that aren't CSE'd, just act as if no identical node
657 // already exists.
658 if (!doNotCSE(N)) {
659 SDNode *Existing = CSEMap.GetOrInsertNode(N);
660 if (Existing != N) {
661 // If there was already an existing matching node, use ReplaceAllUsesWith
662 // to replace the dead one with the existing one. This can cause
663 // recursive merging of other unrelated nodes down the line.
664 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Chengd6f57682008-07-08 20:06:39 +0000665
djg943376a2009-01-25 16:29:12 +0000666 // N is now dead. Inform the listener if it exists and delete it.
Scott Michel91099d62009-02-17 22:15:04 +0000667 if (UpdateListener)
djg943376a2009-01-25 16:29:12 +0000668 UpdateListener->NodeDeleted(N, Existing);
669 DeleteNodeNotInCSEMaps(N);
670 return;
671 }
672 }
Evan Chengd6f57682008-07-08 20:06:39 +0000673
djg943376a2009-01-25 16:29:12 +0000674 // If the node doesn't already exist, we updated it. Inform a listener if
675 // it exists.
Scott Michel91099d62009-02-17 22:15:04 +0000676 if (UpdateListener)
djg943376a2009-01-25 16:29:12 +0000677 UpdateListener->NodeUpdated(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678}
679
680/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michel91099d62009-02-17 22:15:04 +0000681/// were replaced with those specified. If this node is never memoized,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682/// return null, otherwise return a pointer to the slot it would take. If a
683/// node already exists with these operands, the slot will be non-null.
Dan Gohman8181bd12008-07-27 21:46:04 +0000684SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000686 if (doNotCSE(N))
687 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000688
Dan Gohman8181bd12008-07-27 21:46:04 +0000689 SDValue Ops[] = { Op };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 FoldingSetNodeID ID;
691 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands1e450d42008-10-27 15:30:53 +0000692 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
694}
695
696/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michel91099d62009-02-17 22:15:04 +0000697/// were replaced with those specified. If this node is never memoized,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698/// return null, otherwise return a pointer to the slot it would take. If a
699/// node already exists with these operands, the slot will be non-null.
Scott Michel91099d62009-02-17 22:15:04 +0000700SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000701 SDValue Op1, SDValue Op2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000703 if (doNotCSE(N))
704 return 0;
705
Dan Gohman8181bd12008-07-27 21:46:04 +0000706 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 FoldingSetNodeID ID;
708 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands1e450d42008-10-27 15:30:53 +0000709 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
711}
712
713
714/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michel91099d62009-02-17 22:15:04 +0000715/// were replaced with those specified. If this node is never memoized,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716/// return null, otherwise return a pointer to the slot it would take. If a
717/// node already exists with these operands, the slot will be non-null.
Scott Michel91099d62009-02-17 22:15:04 +0000718SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000719 const SDValue *Ops,unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 void *&InsertPos) {
Duncan Sands1e450d42008-10-27 15:30:53 +0000721 if (doNotCSE(N))
722 return 0;
Evan Chengd6f57682008-07-08 20:06:39 +0000723
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 FoldingSetNodeID ID;
725 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands1e450d42008-10-27 15:30:53 +0000726 AddNodeIDCustom(ID, N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
728}
729
Duncan Sandsd3ace282008-07-21 10:20:31 +0000730/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
731void SelectionDAG::VerifyNode(SDNode *N) {
732 switch (N->getOpcode()) {
733 default:
734 break;
Duncan Sands70269a72008-10-29 14:22:20 +0000735 case ISD::BUILD_PAIR: {
Owen Andersonac9de032009-08-10 22:56:29 +0000736 EVT VT = N->getValueType(0);
Duncan Sands70269a72008-10-29 14:22:20 +0000737 assert(N->getNumValues() == 1 && "Too many results!");
738 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
739 "Wrong return type!");
740 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
741 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
742 "Mismatched operand types!");
743 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
744 "Wrong operand type!");
745 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
746 "Wrong return type size");
747 break;
748 }
Duncan Sandsd3ace282008-07-21 10:20:31 +0000749 case ISD::BUILD_VECTOR: {
Duncan Sands70269a72008-10-29 14:22:20 +0000750 assert(N->getNumValues() == 1 && "Too many results!");
751 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000752 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sands70269a72008-10-29 14:22:20 +0000753 "Wrong number of operands!");
Owen Andersonac9de032009-08-10 22:56:29 +0000754 EVT EltVT = N->getValueType(0).getVectorElementType();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +0000755 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
756 assert((I->getValueType() == EltVT ||
Nate Begeman543d2142009-04-27 18:41:29 +0000757 (EltVT.isInteger() && I->getValueType().isInteger() &&
758 EltVT.bitsLE(I->getValueType()))) &&
759 "Wrong operand type!");
Duncan Sandsd3ace282008-07-21 10:20:31 +0000760 break;
761 }
762 }
763}
764
Owen Andersonac9de032009-08-10 22:56:29 +0000765/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9e3a3422008-07-08 23:46:32 +0000766/// given type.
767///
Owen Andersonac9de032009-08-10 22:56:29 +0000768unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000769 const Type *Ty = VT == MVT::iPTR ?
Owen Anderson35b47072009-08-13 21:58:54 +0000770 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson77f4eb52009-08-12 00:36:31 +0000771 VT.getTypeForEVT(*getContext());
Dan Gohman9e3a3422008-07-08 23:46:32 +0000772
773 return TLI.getTargetData()->getABITypeAlignment(Ty);
774}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775
Dale Johannesenf89c5ca2009-02-07 02:15:05 +0000776// EntryNode could meaningfully have debug info if we can find it...
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000777SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Devang Pateleb4e0bd2009-02-03 18:46:32 +0000778 : TLI(tli), FLI(fli), DW(0),
Dale Johannesenf89c5ca2009-02-07 02:15:05 +0000779 EntryNode(ISD::EntryToken, DebugLoc::getUnknownLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000780 getVTList(MVT::Other)), Root(getEntryNode()) {
Dan Gohman14a66442008-08-23 02:25:05 +0000781 AllNodes.push_back(&EntryNode);
Dan Gohman03405562008-08-23 00:50:30 +0000782}
783
Devang Patel10e63332009-01-09 19:11:50 +0000784void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
Owen Andersonf3a4d6b2009-07-09 18:44:09 +0000785 DwarfWriter *dw) {
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000786 MF = &mf;
787 MMI = mmi;
Devang Patel10e63332009-01-09 19:11:50 +0000788 DW = dw;
Eric Christopher440d9eb2009-08-22 00:40:45 +0000789 Context = &mf.getFunction()->getContext();
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000790}
791
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792SelectionDAG::~SelectionDAG() {
Dan Gohman14a66442008-08-23 02:25:05 +0000793 allnodes_clear();
794}
795
796void SelectionDAG::allnodes_clear() {
Dan Gohman0b08b152008-08-26 01:44:34 +0000797 assert(&*AllNodes.begin() == &EntryNode);
798 AllNodes.remove(AllNodes.begin());
Dan Gohman98a355c2009-01-19 22:39:36 +0000799 while (!AllNodes.empty())
800 DeallocateNode(AllNodes.begin());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801}
802
Dan Gohman0f2d71d2008-08-27 23:52:12 +0000803void SelectionDAG::clear() {
Dan Gohman14a66442008-08-23 02:25:05 +0000804 allnodes_clear();
805 OperandAllocator.Reset();
806 CSEMap.clear();
807
808 ExtendedValueTypeNodes.clear();
Bill Wendlingfef06052008-09-16 21:48:12 +0000809 ExternalSymbols.clear();
810 TargetExternalSymbols.clear();
Dan Gohman14a66442008-08-23 02:25:05 +0000811 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
812 static_cast<CondCodeSDNode*>(0));
813 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
814 static_cast<SDNode*>(0));
815
djgc2517d32009-01-26 04:35:06 +0000816 EntryNode.UseList = 0;
Dan Gohman14a66442008-08-23 02:25:05 +0000817 AllNodes.push_back(&EntryNode);
818 Root = getEntryNode();
819}
820
Duncan Sandsdd7bd932009-10-13 21:04:12 +0000821SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
822 return VT.bitsGT(Op.getValueType()) ?
823 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
824 getNode(ISD::TRUNCATE, DL, VT, Op);
825}
826
827SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
828 return VT.bitsGT(Op.getValueType()) ?
829 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
830 getNode(ISD::TRUNCATE, DL, VT, Op);
831}
832
Owen Andersonac9de032009-08-10 22:56:29 +0000833SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Bill Wendling085b2072009-01-30 22:23:15 +0000834 if (Op.getValueType() == VT) return Op;
835 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
836 VT.getSizeInBits());
837 return getNode(ISD::AND, DL, Op.getValueType(), Op,
838 getConstant(Imm, Op.getValueType()));
839}
840
Bob Wilson81a42cf2009-01-22 17:39:32 +0000841/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
842///
Owen Andersonac9de032009-08-10 22:56:29 +0000843SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
844 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohman0b8972d2009-04-20 22:51:43 +0000845 SDValue NegOne =
846 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling4b8dd442009-01-30 22:11:22 +0000847 return getNode(ISD::XOR, DL, VT, Val, NegOne);
848}
849
Owen Andersonac9de032009-08-10 22:56:29 +0000850SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
851 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
djg51bef2e2009-01-27 20:39:34 +0000852 assert((EltVT.getSizeInBits() >= 64 ||
853 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
854 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands92c43912008-06-06 12:08:01 +0000855 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohmandc458cf2008-02-08 22:59:30 +0000856}
857
Owen Andersonac9de032009-08-10 22:56:29 +0000858SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000859 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000860}
861
Owen Andersonac9de032009-08-10 22:56:29 +0000862SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000863 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman5b9d6412007-12-12 22:21:26 +0000864
Owen Andersonac9de032009-08-10 22:56:29 +0000865 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands92c43912008-06-06 12:08:01 +0000866 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohmandc458cf2008-02-08 22:59:30 +0000867 "APInt size does not match type size!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868
869 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
870 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000871 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000872 ID.AddPointer(&Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 void *IP = 0;
Dan Gohman5b9d6412007-12-12 22:21:26 +0000874 SDNode *N = NULL;
875 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000876 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000877 return SDValue(N, 0);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000878 if (!N) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000879 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000880 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000881 CSEMap.InsertNode(N, IP);
882 AllNodes.push_back(N);
883 }
884
Dan Gohman8181bd12008-07-27 21:46:04 +0000885 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000886 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000887 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000888 Ops.assign(VT.getVectorNumElements(), Result);
Evan Cheng907a2d22009-02-25 22:49:59 +0000889 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
890 VT, &Ops[0], Ops.size());
Dan Gohman5b9d6412007-12-12 22:21:26 +0000891 }
892 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893}
894
Dan Gohman8181bd12008-07-27 21:46:04 +0000895SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner5872a362008-01-17 07:00:52 +0000896 return getConstant(Val, TLI.getPointerTy(), isTarget);
897}
898
899
Owen Andersonac9de032009-08-10 22:56:29 +0000900SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Andersond363a0e2009-07-27 20:59:43 +0000901 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000902}
903
Owen Andersonac9de032009-08-10 22:56:29 +0000904SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands92c43912008-06-06 12:08:01 +0000905 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michel91099d62009-02-17 22:15:04 +0000906
Owen Andersonac9de032009-08-10 22:56:29 +0000907 EVT EltVT =
Duncan Sands92c43912008-06-06 12:08:01 +0000908 VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909
910 // Do the map lookup using the actual bit pattern for the floating point
911 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
912 // we don't have issues with SNANs.
913 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
914 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000915 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000916 ID.AddPointer(&V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 void *IP = 0;
918 SDNode *N = NULL;
919 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands92c43912008-06-06 12:08:01 +0000920 if (!VT.isVector())
Dan Gohman8181bd12008-07-27 21:46:04 +0000921 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 if (!N) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000923 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohmanc1f3a072008-09-12 18:08:03 +0000924 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 CSEMap.InsertNode(N, IP);
926 AllNodes.push_back(N);
927 }
928
Dan Gohman8181bd12008-07-27 21:46:04 +0000929 SDValue Result(N, 0);
Duncan Sands92c43912008-06-06 12:08:01 +0000930 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000931 SmallVector<SDValue, 8> Ops;
Duncan Sands92c43912008-06-06 12:08:01 +0000932 Ops.assign(VT.getVectorNumElements(), Result);
Evan Cheng907a2d22009-02-25 22:49:59 +0000933 // FIXME DebugLoc info might be appropriate here
934 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
935 VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 }
937 return Result;
938}
939
Owen Andersonac9de032009-08-10 22:56:29 +0000940SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
941 EVT EltVT =
Duncan Sands92c43912008-06-06 12:08:01 +0000942 VT.isVector() ? VT.getVectorElementType() : VT;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000943 if (EltVT==MVT::f32)
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000944 return getConstantFP(APFloat((float)Val), VT, isTarget);
945 else
946 return getConstantFP(APFloat(Val), VT, isTarget);
947}
948
Dan Gohman8181bd12008-07-27 21:46:04 +0000949SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Owen Andersonac9de032009-08-10 22:56:29 +0000950 EVT VT, int64_t Offset,
Chris Lattner7123ef72009-06-25 21:21:14 +0000951 bool isTargetGA,
952 unsigned char TargetFlags) {
953 assert((TargetFlags == 0 || isTargetGA) &&
954 "Cannot set target flags on target-independent globals");
Eric Christopher440d9eb2009-08-22 00:40:45 +0000955
Dan Gohman36322c72008-10-18 02:06:02 +0000956 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersonac9de032009-08-10 22:56:29 +0000957 EVT PTy = TLI.getPointerTy();
Owen Anderson27bfb672009-08-10 18:56:59 +0000958 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman36322c72008-10-18 02:06:02 +0000959 if (BitWidth < 64)
960 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
961
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000962 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
963 if (!GVar) {
Anton Korobeynikov85149302008-03-22 07:53:40 +0000964 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000965 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikovc7b90912008-09-09 20:05:04 +0000966 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000967 }
968
Chris Lattner7123ef72009-06-25 21:21:14 +0000969 unsigned Opc;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 if (GVar && GVar->isThreadLocal())
971 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
972 else
973 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikovdd9dc5d2008-03-11 22:38:53 +0000974
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000976 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 ID.AddPointer(GV);
978 ID.AddInteger(Offset);
Chris Lattner7123ef72009-06-25 21:21:14 +0000979 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 void *IP = 0;
981 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
djg08f48f52009-01-25 16:21:38 +0000982 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000983 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Chris Lattnerb31fb962009-06-26 21:14:05 +0000984 new (N) GlobalAddressSDNode(Opc, GV, VT, Offset, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 CSEMap.InsertNode(N, IP);
986 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000987 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988}
989
Owen Andersonac9de032009-08-10 22:56:29 +0000990SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
992 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +0000993 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 ID.AddInteger(FI);
995 void *IP = 0;
996 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +0000997 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000998 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +0000999 new (N) FrameIndexSDNode(FI, VT, isTarget);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 CSEMap.InsertNode(N, IP);
1001 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001002 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003}
1004
Owen Andersonac9de032009-08-10 22:56:29 +00001005SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattner583c7962009-06-25 21:35:31 +00001006 unsigned char TargetFlags) {
1007 assert((TargetFlags == 0 || isTarget) &&
1008 "Cannot set target flags on target-independent jump tables");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1010 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001011 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 ID.AddInteger(JTI);
Chris Lattner583c7962009-06-25 21:35:31 +00001013 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 void *IP = 0;
1015 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001016 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001017 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Chris Lattner583c7962009-06-25 21:35:31 +00001018 new (N) JumpTableSDNode(JTI, VT, isTarget, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 CSEMap.InsertNode(N, IP);
1020 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001021 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022}
1023
Owen Andersonac9de032009-08-10 22:56:29 +00001024SDValue SelectionDAG::getConstantPool(Constant *C, EVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00001025 unsigned Alignment, int Offset,
Eric Christopher440d9eb2009-08-22 00:40:45 +00001026 bool isTarget,
Chris Lattner583c7962009-06-25 21:35:31 +00001027 unsigned char TargetFlags) {
1028 assert((TargetFlags == 0 || isTarget) &&
1029 "Cannot set target flags on target-independent globals");
Dan Gohman04637d12008-09-16 22:05:41 +00001030 if (Alignment == 0)
Evan Cheng68c18682009-03-13 07:51:59 +00001031 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1033 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001034 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 ID.AddInteger(Alignment);
1036 ID.AddInteger(Offset);
1037 ID.AddPointer(C);
Chris Lattner583c7962009-06-25 21:35:31 +00001038 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 void *IP = 0;
1040 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001041 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001042 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattner583c7962009-06-25 21:35:31 +00001043 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 CSEMap.InsertNode(N, IP);
1045 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001046 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047}
1048
1049
Owen Andersonac9de032009-08-10 22:56:29 +00001050SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00001051 unsigned Alignment, int Offset,
Chris Lattner583c7962009-06-25 21:35:31 +00001052 bool isTarget,
1053 unsigned char TargetFlags) {
1054 assert((TargetFlags == 0 || isTarget) &&
1055 "Cannot set target flags on target-independent globals");
Dan Gohman04637d12008-09-16 22:05:41 +00001056 if (Alignment == 0)
Evan Cheng68c18682009-03-13 07:51:59 +00001057 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1059 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001060 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 ID.AddInteger(Alignment);
1062 ID.AddInteger(Offset);
1063 C->AddSelectionDAGCSEId(ID);
Chris Lattner583c7962009-06-25 21:35:31 +00001064 ID.AddInteger(TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 void *IP = 0;
1066 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001067 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001068 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattner583c7962009-06-25 21:35:31 +00001069 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001070 CSEMap.InsertNode(N, IP);
1071 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001072 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073}
1074
Dan Gohman8181bd12008-07-27 21:46:04 +00001075SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 FoldingSetNodeID ID;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001077 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 ID.AddPointer(MBB);
1079 void *IP = 0;
1080 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001081 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001082 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001083 new (N) BasicBlockSDNode(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084 CSEMap.InsertNode(N, IP);
1085 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001086 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087}
1088
Owen Andersonac9de032009-08-10 22:56:29 +00001089SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001090 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1091 ValueTypeNodes.size())
1092 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093
Duncan Sands92c43912008-06-06 12:08:01 +00001094 SDNode *&N = VT.isExtended() ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001095 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsd7307a92007-10-17 13:49:58 +00001096
Dan Gohman8181bd12008-07-27 21:46:04 +00001097 if (N) return SDValue(N, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001098 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001099 new (N) VTSDNode(VT);
Duncan Sandsd7307a92007-10-17 13:49:58 +00001100 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001101 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102}
1103
Owen Andersonac9de032009-08-10 22:56:29 +00001104SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendlingfef06052008-09-16 21:48:12 +00001105 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman8181bd12008-07-27 21:46:04 +00001106 if (N) return SDValue(N, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001107 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner8430a292009-06-25 18:45:50 +00001108 new (N) ExternalSymbolSDNode(false, Sym, 0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001110 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111}
1112
Owen Andersonac9de032009-08-10 22:56:29 +00001113SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner8430a292009-06-25 18:45:50 +00001114 unsigned char TargetFlags) {
1115 SDNode *&N =
1116 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1117 TargetFlags)];
Dan Gohman8181bd12008-07-27 21:46:04 +00001118 if (N) return SDValue(N, 0);
Bill Wendlingfef06052008-09-16 21:48:12 +00001119 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner8430a292009-06-25 18:45:50 +00001120 new (N) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001122 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123}
1124
Dan Gohman8181bd12008-07-27 21:46:04 +00001125SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 if ((unsigned)Cond >= CondCodeNodes.size())
1127 CondCodeNodes.resize(Cond+1);
Duncan Sands92c43912008-06-06 12:08:01 +00001128
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 if (CondCodeNodes[Cond] == 0) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001130 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001131 new (N) CondCodeSDNode(Cond);
1132 CondCodeNodes[Cond] = N;
1133 AllNodes.push_back(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001135 return SDValue(CondCodeNodes[Cond], 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136}
1137
Nate Begemane8f61cb2009-04-29 05:20:52 +00001138// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1139// the shuffle mask M that point at N1 to point at N2, and indices that point
1140// N2 to point at N1.
Nate Begeman543d2142009-04-27 18:41:29 +00001141static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1142 std::swap(N1, N2);
1143 int NElts = M.size();
1144 for (int i = 0; i != NElts; ++i) {
1145 if (M[i] >= NElts)
1146 M[i] -= NElts;
1147 else if (M[i] >= 0)
1148 M[i] += NElts;
1149 }
1150}
1151
Eric Christopher440d9eb2009-08-22 00:40:45 +00001152SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman543d2142009-04-27 18:41:29 +00001153 SDValue N2, const int *Mask) {
1154 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher440d9eb2009-08-22 00:40:45 +00001155 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman543d2142009-04-27 18:41:29 +00001156 "Vector Shuffle VTs must be a vectors");
1157 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1158 && "Vector Shuffle VTs must have same element type");
1159
1160 // Canonicalize shuffle undef, undef -> undef
1161 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman76a3e1c2009-07-09 00:46:33 +00001162 return getUNDEF(VT);
Nate Begeman543d2142009-04-27 18:41:29 +00001163
Eric Christopher440d9eb2009-08-22 00:40:45 +00001164 // Validate that all indices in Mask are within the range of the elements
Nate Begeman543d2142009-04-27 18:41:29 +00001165 // input to the shuffle.
Nate Begemane8f61cb2009-04-29 05:20:52 +00001166 unsigned NElts = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00001167 SmallVector<int, 8> MaskVec;
Nate Begemane8f61cb2009-04-29 05:20:52 +00001168 for (unsigned i = 0; i != NElts; ++i) {
1169 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman543d2142009-04-27 18:41:29 +00001170 MaskVec.push_back(Mask[i]);
1171 }
Eric Christopher440d9eb2009-08-22 00:40:45 +00001172
Nate Begeman543d2142009-04-27 18:41:29 +00001173 // Canonicalize shuffle v, v -> v, undef
1174 if (N1 == N2) {
1175 N2 = getUNDEF(VT);
Nate Begemane8f61cb2009-04-29 05:20:52 +00001176 for (unsigned i = 0; i != NElts; ++i)
1177 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman543d2142009-04-27 18:41:29 +00001178 }
Eric Christopher440d9eb2009-08-22 00:40:45 +00001179
Nate Begeman543d2142009-04-27 18:41:29 +00001180 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1181 if (N1.getOpcode() == ISD::UNDEF)
1182 commuteShuffle(N1, N2, MaskVec);
Eric Christopher440d9eb2009-08-22 00:40:45 +00001183
Nate Begeman543d2142009-04-27 18:41:29 +00001184 // Canonicalize all index into lhs, -> shuffle lhs, undef
1185 // Canonicalize all index into rhs, -> shuffle rhs, undef
1186 bool AllLHS = true, AllRHS = true;
1187 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begemane8f61cb2009-04-29 05:20:52 +00001188 for (unsigned i = 0; i != NElts; ++i) {
1189 if (MaskVec[i] >= (int)NElts) {
Nate Begeman543d2142009-04-27 18:41:29 +00001190 if (N2Undef)
1191 MaskVec[i] = -1;
1192 else
1193 AllLHS = false;
1194 } else if (MaskVec[i] >= 0) {
1195 AllRHS = false;
1196 }
1197 }
1198 if (AllLHS && AllRHS)
1199 return getUNDEF(VT);
Nate Begemane8f61cb2009-04-29 05:20:52 +00001200 if (AllLHS && !N2Undef)
Nate Begeman543d2142009-04-27 18:41:29 +00001201 N2 = getUNDEF(VT);
1202 if (AllRHS) {
1203 N1 = getUNDEF(VT);
1204 commuteShuffle(N1, N2, MaskVec);
1205 }
Eric Christopher440d9eb2009-08-22 00:40:45 +00001206
Nate Begeman543d2142009-04-27 18:41:29 +00001207 // If Identity shuffle, or all shuffle in to undef, return that node.
1208 bool AllUndef = true;
1209 bool Identity = true;
Nate Begemane8f61cb2009-04-29 05:20:52 +00001210 for (unsigned i = 0; i != NElts; ++i) {
1211 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman543d2142009-04-27 18:41:29 +00001212 if (MaskVec[i] >= 0) AllUndef = false;
1213 }
Dan Gohman76a3e1c2009-07-09 00:46:33 +00001214 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman543d2142009-04-27 18:41:29 +00001215 return N1;
1216 if (AllUndef)
1217 return getUNDEF(VT);
1218
1219 FoldingSetNodeID ID;
1220 SDValue Ops[2] = { N1, N2 };
1221 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begemane8f61cb2009-04-29 05:20:52 +00001222 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00001223 ID.AddInteger(MaskVec[i]);
Eric Christopher440d9eb2009-08-22 00:40:45 +00001224
Nate Begeman543d2142009-04-27 18:41:29 +00001225 void* IP = 0;
1226 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1227 return SDValue(E, 0);
Eric Christopher440d9eb2009-08-22 00:40:45 +00001228
Nate Begeman543d2142009-04-27 18:41:29 +00001229 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1230 // SDNode doesn't have access to it. This memory will be "leaked" when
1231 // the node is deallocated, but recovered when the NodeAllocator is released.
1232 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1233 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher440d9eb2009-08-22 00:40:45 +00001234
Nate Begeman543d2142009-04-27 18:41:29 +00001235 ShuffleVectorSDNode *N = NodeAllocator.Allocate<ShuffleVectorSDNode>();
1236 new (N) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
1237 CSEMap.InsertNode(N, IP);
1238 AllNodes.push_back(N);
1239 return SDValue(N, 0);
1240}
1241
Owen Andersonac9de032009-08-10 22:56:29 +00001242SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001243 SDValue Val, SDValue DTy,
1244 SDValue STy, SDValue Rnd, SDValue Sat,
1245 ISD::CvtCode Code) {
Mon P Wang8a443d72009-02-05 04:47:42 +00001246 // If the src and dest types are the same and the conversion is between
1247 // integer types of the same sign or two floats, no conversion is necessary.
1248 if (DTy == STy &&
1249 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001250 return Val;
1251
1252 FoldingSetNodeID ID;
Mon P Wange6f60f12009-11-07 04:46:25 +00001253 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1254 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001255 void* IP = 0;
1256 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1257 return SDValue(E, 0);
1258 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
Dale Johannesen7140aaa2009-02-03 23:04:43 +00001259 new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code);
1260 CSEMap.InsertNode(N, IP);
1261 AllNodes.push_back(N);
1262 return SDValue(N, 0);
1263}
1264
Owen Andersonac9de032009-08-10 22:56:29 +00001265SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00001267 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 ID.AddInteger(RegNo);
1269 void *IP = 0;
1270 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001271 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001272 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001273 new (N) RegisterSDNode(RegNo, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 CSEMap.InsertNode(N, IP);
1275 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001276 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277}
1278
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001279SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1280 SDValue Root,
1281 unsigned LabelID) {
1282 FoldingSetNodeID ID;
1283 SDValue Ops[] = { Root };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001284 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00001285 ID.AddInteger(LabelID);
1286 void *IP = 0;
1287 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1288 return SDValue(E, 0);
1289 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1290 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1291 CSEMap.InsertNode(N, IP);
1292 AllNodes.push_back(N);
1293 return SDValue(N, 0);
1294}
1295
Dan Gohman885793b2009-11-20 23:18:13 +00001296SDValue SelectionDAG::getBlockAddress(BlockAddress *BA, EVT VT,
1297 bool isTarget,
1298 unsigned char TargetFlags) {
Dan Gohman91057512009-10-30 01:27:03 +00001299 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1300
1301 FoldingSetNodeID ID;
Dan Gohman885793b2009-11-20 23:18:13 +00001302 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman91057512009-10-30 01:27:03 +00001303 ID.AddPointer(BA);
Dan Gohman885793b2009-11-20 23:18:13 +00001304 ID.AddInteger(TargetFlags);
Dan Gohman91057512009-10-30 01:27:03 +00001305 void *IP = 0;
1306 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1307 return SDValue(E, 0);
1308 SDNode *N = NodeAllocator.Allocate<BlockAddressSDNode>();
Dan Gohman885793b2009-11-20 23:18:13 +00001309 new (N) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
Dan Gohman91057512009-10-30 01:27:03 +00001310 CSEMap.InsertNode(N, IP);
1311 AllNodes.push_back(N);
1312 return SDValue(N, 0);
1313}
1314
Dan Gohman8181bd12008-07-27 21:46:04 +00001315SDValue SelectionDAG::getSrcValue(const Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 assert((!V || isa<PointerType>(V->getType())) &&
1317 "SrcValue is not a pointer?");
1318
1319 FoldingSetNodeID ID;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001320 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321 ID.AddPointer(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001322
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001323 void *IP = 0;
1324 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00001325 return SDValue(E, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001326
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00001327 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohmaned825d12008-07-07 23:02:41 +00001328 new (N) SrcValueSDNode(V);
Dan Gohman12a9c082008-02-06 22:27:42 +00001329 CSEMap.InsertNode(N, IP);
1330 AllNodes.push_back(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00001331 return SDValue(N, 0);
Dan Gohman12a9c082008-02-06 22:27:42 +00001332}
1333
Duncan Sands7d9e3612009-01-31 15:50:11 +00001334/// getShiftAmountOperand - Return the specified value casted to
1335/// the target's desired shift amount type.
1336SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
Owen Andersonac9de032009-08-10 22:56:29 +00001337 EVT OpTy = Op.getValueType();
Owen Anderson2dd68a22009-08-11 21:59:30 +00001338 MVT ShTy = TLI.getShiftAmountTy();
Duncan Sands7d9e3612009-01-31 15:50:11 +00001339 if (OpTy == ShTy || OpTy.isVector()) return Op;
1340
1341 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001342 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands7d9e3612009-01-31 15:50:11 +00001343}
1344
Chris Lattner53f5aee2007-10-15 17:47:20 +00001345/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1346/// specified value type.
Owen Andersonac9de032009-08-10 22:56:29 +00001347SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner53f5aee2007-10-15 17:47:20 +00001348 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohmanad488032009-09-23 21:07:02 +00001349 unsigned ByteSize = VT.getStoreSize();
Owen Anderson77f4eb52009-08-12 00:36:31 +00001350 const Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang55854cc2008-07-05 20:40:31 +00001351 unsigned StackAlign =
1352 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michel91099d62009-02-17 22:15:04 +00001353
David Greene6424ab92009-11-12 20:49:22 +00001354 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner53f5aee2007-10-15 17:47:20 +00001355 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1356}
1357
Duncan Sands871e55f2008-12-09 21:33:20 +00001358/// CreateStackTemporary - Create a stack temporary suitable for holding
1359/// either of the specified value types.
Owen Andersonac9de032009-08-10 22:56:29 +00001360SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands871e55f2008-12-09 21:33:20 +00001361 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1362 VT2.getStoreSizeInBits())/8;
Owen Anderson77f4eb52009-08-12 00:36:31 +00001363 const Type *Ty1 = VT1.getTypeForEVT(*getContext());
1364 const Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands871e55f2008-12-09 21:33:20 +00001365 const TargetData *TD = TLI.getTargetData();
1366 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1367 TD->getPrefTypeAlignment(Ty2));
1368
1369 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene6424ab92009-11-12 20:49:22 +00001370 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands871e55f2008-12-09 21:33:20 +00001371 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1372}
1373
Owen Andersonac9de032009-08-10 22:56:29 +00001374SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesen38496eb2009-02-03 00:47:48 +00001375 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 // These setcc operations always fold.
1377 switch (Cond) {
1378 default: break;
1379 case ISD::SETFALSE:
1380 case ISD::SETFALSE2: return getConstant(0, VT);
1381 case ISD::SETTRUE:
1382 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001383
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 case ISD::SETOEQ:
1385 case ISD::SETOGT:
1386 case ISD::SETOGE:
1387 case ISD::SETOLT:
1388 case ISD::SETOLE:
1389 case ISD::SETONE:
1390 case ISD::SETO:
1391 case ISD::SETUO:
1392 case ISD::SETUEQ:
1393 case ISD::SETUNE:
Duncan Sands92c43912008-06-06 12:08:01 +00001394 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 break;
1396 }
Scott Michel91099d62009-02-17 22:15:04 +00001397
Gabor Greif1c80d112008-08-28 21:40:38 +00001398 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001399 const APInt &C2 = N2C->getAPIntValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00001400 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00001401 const APInt &C1 = N1C->getAPIntValue();
Scott Michel91099d62009-02-17 22:15:04 +00001402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403 switch (Cond) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001404 default: llvm_unreachable("Unknown integer setcc!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1406 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman161652c2008-02-29 01:47:35 +00001407 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1408 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1409 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1410 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1411 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1412 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1413 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1414 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 }
1416 }
1417 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001418 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1419 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen80ca14c2007-10-14 01:56:47 +00001420 // No compile time operations on this type yet.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001421 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman8181bd12008-07-27 21:46:04 +00001422 return SDValue();
Dale Johannesendf8a8312007-08-31 04:03:46 +00001423
1424 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 switch (Cond) {
Dale Johannesendf8a8312007-08-31 04:03:46 +00001426 default: break;
Scott Michel91099d62009-02-17 22:15:04 +00001427 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001428 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001429 // fall through
1430 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001431 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001432 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001433 // fall through
1434 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001435 R==APFloat::cmpLessThan, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001436 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001437 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001438 // fall through
1439 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001440 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001441 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001442 // fall through
1443 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001444 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001445 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001446 // fall through
1447 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001448 R==APFloat::cmpEqual, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001449 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00001450 return getUNDEF(VT);
Dale Johannesen76844472007-08-31 17:03:33 +00001451 // fall through
1452 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johannesendf8a8312007-08-31 04:03:46 +00001453 R==APFloat::cmpEqual, VT);
1454 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1455 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1456 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1457 R==APFloat::cmpEqual, VT);
1458 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1459 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1460 R==APFloat::cmpLessThan, VT);
1461 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1462 R==APFloat::cmpUnordered, VT);
1463 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1464 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 }
1466 } else {
1467 // Ensure that the constant occurs on the RHS.
Dale Johannesen38496eb2009-02-03 00:47:48 +00001468 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 }
Anton Korobeynikov53422f62008-02-20 11:10:28 +00001470 }
1471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 // Could not fold it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001473 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474}
1475
Dan Gohman07961cd2008-02-25 21:11:39 +00001476/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1477/// use this predicate to simplify operations downstream.
Dan Gohman8181bd12008-07-27 21:46:04 +00001478bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnerb9e009a2009-07-07 23:28:46 +00001479 // This predicate is not safe for vector operations.
1480 if (Op.getValueType().isVector())
1481 return false;
Eric Christopher440d9eb2009-08-22 00:40:45 +00001482
Dan Gohman07961cd2008-02-25 21:11:39 +00001483 unsigned BitWidth = Op.getValueSizeInBits();
1484 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1485}
1486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1488/// this predicate to simplify operations downstream. Mask is known to be zero
1489/// for bits that V cannot have.
Scott Michel91099d62009-02-17 22:15:04 +00001490bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 unsigned Depth) const {
Dan Gohman07961cd2008-02-25 21:11:39 +00001492 APInt KnownZero, KnownOne;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michel91099d62009-02-17 22:15:04 +00001494 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 return (KnownZero & Mask) == Mask;
1496}
1497
1498/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1499/// known to be either zero or one and return them in the KnownZero/KnownOne
1500/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1501/// processing.
Scott Michel91099d62009-02-17 22:15:04 +00001502void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00001503 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 unsigned Depth) const {
Dan Gohman229fa052008-02-13 00:35:47 +00001505 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands92c43912008-06-06 12:08:01 +00001506 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohman56eaab32008-02-13 23:13:32 +00001507 "Mask size mismatches value type size!");
1508
Dan Gohman229fa052008-02-13 00:35:47 +00001509 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 if (Depth == 6 || Mask == 0)
1511 return; // Limit search depth.
Scott Michel91099d62009-02-17 22:15:04 +00001512
Dan Gohman229fa052008-02-13 00:35:47 +00001513 APInt KnownZero2, KnownOne2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514
1515 switch (Op.getOpcode()) {
1516 case ISD::Constant:
1517 // We know all of the bits for a constant!
Dan Gohman229fa052008-02-13 00:35:47 +00001518 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 KnownZero = ~KnownOne & Mask;
1520 return;
1521 case ISD::AND:
1522 // If either the LHS or the RHS are Zero, the result is zero.
1523 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001524 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1525 KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001526 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1527 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528
1529 // Output known-1 bits are only known if set in both the LHS & RHS.
1530 KnownOne &= KnownOne2;
1531 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1532 KnownZero |= KnownZero2;
1533 return;
1534 case ISD::OR:
1535 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001536 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1537 KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001538 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1539 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541 // Output known-0 bits are only known if clear in both the LHS & RHS.
1542 KnownZero &= KnownZero2;
1543 // Output known-1 are known to be set if set in either the LHS | RHS.
1544 KnownOne |= KnownOne2;
1545 return;
1546 case ISD::XOR: {
1547 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1548 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001549 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1550 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohman229fa052008-02-13 00:35:47 +00001553 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1555 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1556 KnownZero = KnownZeroOut;
1557 return;
1558 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001559 case ISD::MUL: {
1560 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1561 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1562 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1563 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1564 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1565
1566 // If low bits are zero in either operand, output low known-0 bits.
1567 // Also compute a conserative estimate for high known-0 bits.
1568 // More trickiness is possible, but this is sufficient for the
1569 // interesting case of alignment computation.
1570 KnownOne.clear();
1571 unsigned TrailZ = KnownZero.countTrailingOnes() +
1572 KnownZero2.countTrailingOnes();
1573 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman4c451852008-05-07 00:35:55 +00001574 KnownZero2.countLeadingOnes(),
1575 BitWidth) - BitWidth;
Dan Gohmanbec16052008-04-28 17:02:21 +00001576
1577 TrailZ = std::min(TrailZ, BitWidth);
1578 LeadZ = std::min(LeadZ, BitWidth);
1579 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1580 APInt::getHighBitsSet(BitWidth, LeadZ);
1581 KnownZero &= Mask;
1582 return;
1583 }
1584 case ISD::UDIV: {
1585 // For the purposes of computing leading zeros we can conservatively
1586 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001587 // be less than the denominator.
Dan Gohmanbec16052008-04-28 17:02:21 +00001588 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1589 ComputeMaskedBits(Op.getOperand(0),
1590 AllOnes, KnownZero2, KnownOne2, Depth+1);
1591 unsigned LeadZ = KnownZero2.countLeadingOnes();
1592
1593 KnownOne2.clear();
1594 KnownZero2.clear();
1595 ComputeMaskedBits(Op.getOperand(1),
1596 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1b9fb1f2008-05-02 21:30:02 +00001597 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1598 if (RHSUnknownLeadingOnes != BitWidth)
1599 LeadZ = std::min(BitWidth,
1600 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001601
1602 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1603 return;
1604 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 case ISD::SELECT:
1606 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1607 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001608 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1609 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1610
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 // Only known if known in both the LHS and RHS.
1612 KnownOne &= KnownOne2;
1613 KnownZero &= KnownZero2;
1614 return;
1615 case ISD::SELECT_CC:
1616 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1617 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001618 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1619 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001621 // Only known if known in both the LHS and RHS.
1622 KnownOne &= KnownOne2;
1623 KnownZero &= KnownZero2;
1624 return;
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001625 case ISD::SADDO:
1626 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00001627 case ISD::SSUBO:
1628 case ISD::USUBO:
1629 case ISD::SMULO:
1630 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00001631 if (Op.getResNo() != 1)
1632 return;
Duncan Sands8cf4a822008-11-23 15:47:28 +00001633 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001634 case ISD::SETCC:
1635 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands8cf4a822008-11-23 15:47:28 +00001636 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohman229fa052008-02-13 00:35:47 +00001637 BitWidth > 1)
1638 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 return;
1640 case ISD::SHL:
1641 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1642 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001643 unsigned ShAmt = SA->getZExtValue();
Dan Gohman4b8d0002008-02-26 18:50:50 +00001644
1645 // If the shift count is an invalid immediate, don't do anything.
1646 if (ShAmt >= BitWidth)
1647 return;
1648
1649 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001650 KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001651 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman4b8d0002008-02-26 18:50:50 +00001652 KnownZero <<= ShAmt;
1653 KnownOne <<= ShAmt;
Dan Gohman229fa052008-02-13 00:35:47 +00001654 // low bits known zero.
Dan Gohman4b8d0002008-02-26 18:50:50 +00001655 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001656 }
1657 return;
1658 case ISD::SRL:
1659 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1660 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001661 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662
Dan Gohman4b8d0002008-02-26 18:50:50 +00001663 // If the shift count is an invalid immediate, don't do anything.
1664 if (ShAmt >= BitWidth)
1665 return;
1666
Dan Gohman229fa052008-02-13 00:35:47 +00001667 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001668 KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001669 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001670 KnownZero = KnownZero.lshr(ShAmt);
1671 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672
Dan Gohman4d81a742008-02-13 22:43:25 +00001673 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 KnownZero |= HighBits; // High bits known zero.
1675 }
1676 return;
1677 case ISD::SRA:
1678 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001679 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680
Dan Gohman4b8d0002008-02-26 18:50:50 +00001681 // If the shift count is an invalid immediate, don't do anything.
1682 if (ShAmt >= BitWidth)
1683 return;
1684
Dan Gohman229fa052008-02-13 00:35:47 +00001685 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 // If any of the demanded bits are produced by the sign extension, we also
1687 // demand the input sign bit.
Dan Gohman4d81a742008-02-13 22:43:25 +00001688 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1689 if (HighBits.getBoolValue())
Dan Gohman229fa052008-02-13 00:35:47 +00001690 InDemandedMask |= APInt::getSignBit(BitWidth);
Scott Michel91099d62009-02-17 22:15:04 +00001691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1693 Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001694 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001695 KnownZero = KnownZero.lshr(ShAmt);
1696 KnownOne = KnownOne.lshr(ShAmt);
Scott Michel91099d62009-02-17 22:15:04 +00001697
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 // Handle the sign bits.
Dan Gohman229fa052008-02-13 00:35:47 +00001699 APInt SignBit = APInt::getSignBit(BitWidth);
1700 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michel91099d62009-02-17 22:15:04 +00001701
Dan Gohman91507292008-02-20 16:30:17 +00001702 if (KnownZero.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 KnownZero |= HighBits; // New bits are known zero.
Dan Gohman91507292008-02-20 16:30:17 +00001704 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 KnownOne |= HighBits; // New bits are known one.
1706 }
1707 }
1708 return;
1709 case ISD::SIGN_EXTEND_INREG: {
Owen Andersonac9de032009-08-10 22:56:29 +00001710 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands92c43912008-06-06 12:08:01 +00001711 unsigned EBits = EVT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00001712
1713 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 // present in the input.
Dan Gohmand0dfc772008-02-13 22:28:48 +00001715 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001716
Dan Gohmand0dfc772008-02-13 22:28:48 +00001717 APInt InSignBit = APInt::getSignBit(EBits);
1718 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Scott Michel91099d62009-02-17 22:15:04 +00001719
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 // If the sign extended bits are demanded, we know that the sign
1721 // bit is demanded.
Dan Gohman229fa052008-02-13 00:35:47 +00001722 InSignBit.zext(BitWidth);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001723 if (NewBits.getBoolValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 InputDemandedBits |= InSignBit;
Scott Michel91099d62009-02-17 22:15:04 +00001725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001726 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1727 KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001728 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1729
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 // If the sign bit of the input is known set or clear, then we know the
1731 // top bits of the result.
Dan Gohman91507292008-02-20 16:30:17 +00001732 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001733 KnownZero |= NewBits;
1734 KnownOne &= ~NewBits;
Dan Gohman91507292008-02-20 16:30:17 +00001735 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001736 KnownOne |= NewBits;
1737 KnownZero &= ~NewBits;
1738 } else { // Input sign bit unknown
1739 KnownZero &= ~NewBits;
1740 KnownOne &= ~NewBits;
1741 }
1742 return;
1743 }
1744 case ISD::CTTZ:
1745 case ISD::CTLZ:
1746 case ISD::CTPOP: {
Dan Gohman229fa052008-02-13 00:35:47 +00001747 unsigned LowBits = Log2_32(BitWidth)+1;
1748 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman75e24fb2008-06-21 22:02:15 +00001749 KnownOne.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001750 return;
1751 }
1752 case ISD::LOAD: {
Gabor Greif1c80d112008-08-28 21:40:38 +00001753 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 LoadSDNode *LD = cast<LoadSDNode>(Op);
Owen Andersonac9de032009-08-10 22:56:29 +00001755 EVT VT = LD->getMemoryVT();
Duncan Sands92c43912008-06-06 12:08:01 +00001756 unsigned MemBits = VT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001757 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 }
1759 return;
1760 }
1761 case ISD::ZERO_EXTEND: {
Owen Andersonac9de032009-08-10 22:56:29 +00001762 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001763 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001764 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1765 APInt InMask = Mask;
1766 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001767 KnownZero.trunc(InBits);
1768 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001769 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001770 KnownZero.zext(BitWidth);
1771 KnownOne.zext(BitWidth);
1772 KnownZero |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 return;
1774 }
1775 case ISD::SIGN_EXTEND: {
Owen Andersonac9de032009-08-10 22:56:29 +00001776 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001777 unsigned InBits = InVT.getSizeInBits();
Dan Gohman229fa052008-02-13 00:35:47 +00001778 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001779 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1780 APInt InMask = Mask;
1781 InMask.trunc(InBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782
1783 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohmand0dfc772008-02-13 22:28:48 +00001784 // bit is demanded. Temporarily set this bit in the mask for our callee.
1785 if (NewBits.getBoolValue())
1786 InMask |= InSignBit;
Dan Gohman229fa052008-02-13 00:35:47 +00001787
Dan Gohman229fa052008-02-13 00:35:47 +00001788 KnownZero.trunc(InBits);
1789 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001790 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1791
1792 // Note if the sign bit is known to be zero or one.
1793 bool SignBitKnownZero = KnownZero.isNegative();
1794 bool SignBitKnownOne = KnownOne.isNegative();
1795 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1796 "Sign bit can't be known to be both zero and one!");
1797
1798 // If the sign bit wasn't actually demanded by our caller, we don't
1799 // want it set in the KnownZero and KnownOne result values. Reset the
1800 // mask and reapply it to the result values.
1801 InMask = Mask;
1802 InMask.trunc(InBits);
1803 KnownZero &= InMask;
1804 KnownOne &= InMask;
1805
Dan Gohman229fa052008-02-13 00:35:47 +00001806 KnownZero.zext(BitWidth);
1807 KnownOne.zext(BitWidth);
1808
Dan Gohmand0dfc772008-02-13 22:28:48 +00001809 // If the sign bit is known zero or one, the top bits match.
1810 if (SignBitKnownZero)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 KnownZero |= NewBits;
Dan Gohmand0dfc772008-02-13 22:28:48 +00001812 else if (SignBitKnownOne)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001813 KnownOne |= NewBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001814 return;
1815 }
1816 case ISD::ANY_EXTEND: {
Owen Andersonac9de032009-08-10 22:56:29 +00001817 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001818 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001819 APInt InMask = Mask;
1820 InMask.trunc(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001821 KnownZero.trunc(InBits);
1822 KnownOne.trunc(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001823 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohman229fa052008-02-13 00:35:47 +00001824 KnownZero.zext(BitWidth);
1825 KnownOne.zext(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 return;
1827 }
1828 case ISD::TRUNCATE: {
Owen Andersonac9de032009-08-10 22:56:29 +00001829 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001830 unsigned InBits = InVT.getSizeInBits();
Dan Gohmand0dfc772008-02-13 22:28:48 +00001831 APInt InMask = Mask;
1832 InMask.zext(InBits);
Dan Gohman229fa052008-02-13 00:35:47 +00001833 KnownZero.zext(InBits);
1834 KnownOne.zext(InBits);
Dan Gohmand0dfc772008-02-13 22:28:48 +00001835 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001836 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohman229fa052008-02-13 00:35:47 +00001837 KnownZero.trunc(BitWidth);
1838 KnownOne.trunc(BitWidth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001839 break;
1840 }
1841 case ISD::AssertZext: {
Owen Andersonac9de032009-08-10 22:56:29 +00001842 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands92c43912008-06-06 12:08:01 +00001843 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michel91099d62009-02-17 22:15:04 +00001844 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001845 KnownOne, Depth+1);
1846 KnownZero |= (~InMask) & Mask;
1847 return;
1848 }
Chris Lattner13f06832007-12-22 21:26:52 +00001849 case ISD::FGETSIGN:
1850 // All bits are zero except the low bit.
Dan Gohman229fa052008-02-13 00:35:47 +00001851 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattner13f06832007-12-22 21:26:52 +00001852 return;
Scott Michel91099d62009-02-17 22:15:04 +00001853
Dan Gohmanbec16052008-04-28 17:02:21 +00001854 case ISD::SUB: {
1855 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1856 // We know that the top bits of C-X are clear if X contains less bits
1857 // than C (i.e. no wrap-around can happen). For example, 20-X is
1858 // positive if we can prove that X is >= 0 and < 16.
1859 if (CLHS->getAPIntValue().isNonNegative()) {
1860 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1861 // NLZ can't be BitWidth with no sign bit
1862 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1863 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1864 Depth+1);
1865
1866 // If all of the MaskV bits are known to be zero, then we know the
1867 // output top bits are zero, because we now know that the output is
1868 // from [0-C].
1869 if ((KnownZero2 & MaskV) == MaskV) {
1870 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1871 // Top bits known zero.
1872 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1873 }
1874 }
1875 }
1876 }
1877 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001878 case ISD::ADD: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879 // Output known-0 bits are known if clear or set in both the low clear bits
1880 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1881 // low 3 bits clear.
Dan Gohmanbec16052008-04-28 17:02:21 +00001882 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1883 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001884 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanbec16052008-04-28 17:02:21 +00001885 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1886
1887 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00001888 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanbec16052008-04-28 17:02:21 +00001889 KnownZeroOut = std::min(KnownZeroOut,
1890 KnownZero2.countTrailingOnes());
1891
1892 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001893 return;
1894 }
Dan Gohmanbec16052008-04-28 17:02:21 +00001895 case ISD::SREM:
1896 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001897 const APInt &RA = Rem->getAPIntValue();
Dan Gohmanbec16052008-04-28 17:02:21 +00001898 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman5a154a12008-05-06 00:51:48 +00001899 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohmanbec16052008-04-28 17:02:21 +00001900 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1901 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902
Dan Gohman2275be02008-08-13 23:12:35 +00001903 // If the sign bit of the first operand is zero, the sign bit of
1904 // the result is zero. If the first operand has no one bits below
1905 // the second operand's single 1 bit, its sign will be zero.
Dan Gohmanbec16052008-04-28 17:02:21 +00001906 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1907 KnownZero2 |= ~LowBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908
Dan Gohmanbec16052008-04-28 17:02:21 +00001909 KnownZero |= KnownZero2 & Mask;
Dan Gohmanbec16052008-04-28 17:02:21 +00001910
1911 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 }
1913 }
1914 return;
Dan Gohmanbec16052008-04-28 17:02:21 +00001915 case ISD::UREM: {
1916 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman1b1a3c62008-07-03 00:52:03 +00001917 const APInt &RA = Rem->getAPIntValue();
Dan Gohman5a154a12008-05-06 00:51:48 +00001918 if (RA.isPowerOf2()) {
1919 APInt LowBits = (RA - 1);
Dan Gohmanbec16052008-04-28 17:02:21 +00001920 APInt Mask2 = LowBits & Mask;
1921 KnownZero |= ~LowBits & Mask;
1922 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1923 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1924 break;
1925 }
1926 }
1927
1928 // Since the result is less than or equal to either operand, any leading
1929 // zero bits in either operand must also exist in the result.
1930 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1931 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1932 Depth+1);
1933 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1934 Depth+1);
1935
1936 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1937 KnownZero2.countLeadingOnes());
1938 KnownOne.clear();
1939 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1940 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001941 }
1942 default:
1943 // Allow the target to implement this method for its nodes.
1944 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1945 case ISD::INTRINSIC_WO_CHAIN:
1946 case ISD::INTRINSIC_W_CHAIN:
1947 case ISD::INTRINSIC_VOID:
Dan Gohmandd5cdf62009-08-04 00:24:42 +00001948 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
1949 Depth);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 }
1951 return;
1952 }
1953}
1954
1955/// ComputeNumSignBits - Return the number of times the sign bit of the
1956/// register is replicated into the other bits. We know that at least 1 bit
1957/// is always equal to the sign bit (itself), but other cases can give us
1958/// information. For example, immediately after an "SRA X, 2", we know that
1959/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman8181bd12008-07-27 21:46:04 +00001960unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersonac9de032009-08-10 22:56:29 +00001961 EVT VT = Op.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001962 assert(VT.isInteger() && "Invalid VT!");
1963 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964 unsigned Tmp, Tmp2;
Dan Gohman4afc4252008-05-23 02:28:01 +00001965 unsigned FirstAnswer = 1;
Scott Michel91099d62009-02-17 22:15:04 +00001966
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001967 if (Depth == 6)
1968 return 1; // Limit search depth.
1969
1970 switch (Op.getOpcode()) {
1971 default: break;
1972 case ISD::AssertSext:
Duncan Sands92c43912008-06-06 12:08:01 +00001973 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974 return VTBits-Tmp+1;
1975 case ISD::AssertZext:
Duncan Sands92c43912008-06-06 12:08:01 +00001976 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977 return VTBits-Tmp;
Scott Michel91099d62009-02-17 22:15:04 +00001978
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001979 case ISD::Constant: {
Dan Gohman463db8c2008-03-03 23:35:36 +00001980 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1981 // If negative, return # leading ones.
1982 if (Val.isNegative())
1983 return Val.countLeadingOnes();
Scott Michel91099d62009-02-17 22:15:04 +00001984
Dan Gohman463db8c2008-03-03 23:35:36 +00001985 // Return # leading zeros.
1986 return Val.countLeadingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 }
Scott Michel91099d62009-02-17 22:15:04 +00001988
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00001990 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michel91099d62009-02-17 22:15:04 +00001992
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993 case ISD::SIGN_EXTEND_INREG:
1994 // Max of the input and what this extends.
Duncan Sands92c43912008-06-06 12:08:01 +00001995 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001996 Tmp = VTBits-Tmp+1;
Scott Michel91099d62009-02-17 22:15:04 +00001997
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1999 return std::max(Tmp, Tmp2);
2000
2001 case ISD::SRA:
2002 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2003 // SRA X, C -> adds C sign bits.
2004 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002005 Tmp += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 if (Tmp > VTBits) Tmp = VTBits;
2007 }
2008 return Tmp;
2009 case ISD::SHL:
2010 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2011 // shl destroys sign bits.
2012 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002013 if (C->getZExtValue() >= VTBits || // Bad shift.
2014 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2015 return Tmp - C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 }
2017 break;
2018 case ISD::AND:
2019 case ISD::OR:
2020 case ISD::XOR: // NOT is handled here.
Dan Gohman4afc4252008-05-23 02:28:01 +00002021 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohman4afc4252008-05-23 02:28:01 +00002023 if (Tmp != 1) {
2024 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2025 FirstAnswer = std::min(Tmp, Tmp2);
2026 // We computed what we know about the sign bits as our first
2027 // answer. Now proceed to the generic code that uses
2028 // ComputeMaskedBits, and pick whichever answer is better.
2029 }
2030 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031
2032 case ISD::SELECT:
Dan Gohman6ffcf262008-05-20 20:59:51 +00002033 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002034 if (Tmp == 1) return 1; // Early out.
Dan Gohman6ffcf262008-05-20 20:59:51 +00002035 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 return std::min(Tmp, Tmp2);
Bill Wendlingf8bb4402008-11-22 07:24:01 +00002037
2038 case ISD::SADDO:
2039 case ISD::UADDO:
Bill Wendling7e04be62008-12-09 22:08:41 +00002040 case ISD::SSUBO:
2041 case ISD::USUBO:
2042 case ISD::SMULO:
2043 case ISD::UMULO:
Bill Wendlingf8bb4402008-11-22 07:24:01 +00002044 if (Op.getResNo() != 1)
2045 break;
Duncan Sands8cf4a822008-11-23 15:47:28 +00002046 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002047 case ISD::SETCC:
2048 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands8cf4a822008-11-23 15:47:28 +00002049 if (TLI.getBooleanContents() ==
2050 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 return VTBits;
2052 break;
2053 case ISD::ROTL:
2054 case ISD::ROTR:
2055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002056 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michel91099d62009-02-17 22:15:04 +00002057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 // Handle rotate right by N like a rotate left by 32-N.
2059 if (Op.getOpcode() == ISD::ROTR)
2060 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2061
2062 // If we aren't rotating out all of the known-in sign bits, return the
2063 // number that are left. This handles rotl(sext(x), 1) for example.
2064 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2065 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2066 }
2067 break;
2068 case ISD::ADD:
2069 // Add can have at most one carry bit. Thus we know that the output
2070 // is, at worst, one more bit than the inputs.
2071 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2072 if (Tmp == 1) return 1; // Early out.
Scott Michel91099d62009-02-17 22:15:04 +00002073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 // Special case decrementing a value (ADD X, -1):
Dan Gohman843649e2009-02-24 02:00:40 +00002075 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 if (CRHS->isAllOnesValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00002077 APInt KnownZero, KnownOne;
2078 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002079 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +00002080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002081 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2082 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00002083 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 return VTBits;
Scott Michel91099d62009-02-17 22:15:04 +00002085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 // If we are subtracting one from a positive number, there is no carry
2087 // out of the result.
Dan Gohman63f4e462008-02-27 01:23:58 +00002088 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 return Tmp;
2090 }
Scott Michel91099d62009-02-17 22:15:04 +00002091
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2093 if (Tmp2 == 1) return 1;
2094 return std::min(Tmp, Tmp2)-1;
2095 break;
Scott Michel91099d62009-02-17 22:15:04 +00002096
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 case ISD::SUB:
2098 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2099 if (Tmp2 == 1) return 1;
Scott Michel91099d62009-02-17 22:15:04 +00002100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 // Handle NEG.
2102 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002103 if (CLHS->isNullValue()) {
Dan Gohman63f4e462008-02-27 01:23:58 +00002104 APInt KnownZero, KnownOne;
2105 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2107 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2108 // sign bits set.
Dan Gohman63f4e462008-02-27 01:23:58 +00002109 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 return VTBits;
Scott Michel91099d62009-02-17 22:15:04 +00002111
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112 // If the input is known to be positive (the sign bit is known clear),
2113 // the output of the NEG has the same number of sign bits as the input.
Dan Gohman63f4e462008-02-27 01:23:58 +00002114 if (KnownZero.isNegative())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 return Tmp2;
Scott Michel91099d62009-02-17 22:15:04 +00002116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 // Otherwise, we treat this like a SUB.
2118 }
Scott Michel91099d62009-02-17 22:15:04 +00002119
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002120 // Sub can have at most one carry bit. Thus we know that the output
2121 // is, at worst, one more bit than the inputs.
2122 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2123 if (Tmp == 1) return 1; // Early out.
2124 return std::min(Tmp, Tmp2)-1;
2125 break;
2126 case ISD::TRUNCATE:
2127 // FIXME: it's tricky to do anything useful for this, but it is an important
2128 // case for targets like X86.
2129 break;
2130 }
Scott Michel91099d62009-02-17 22:15:04 +00002131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2133 if (Op.getOpcode() == ISD::LOAD) {
2134 LoadSDNode *LD = cast<LoadSDNode>(Op);
2135 unsigned ExtType = LD->getExtensionType();
2136 switch (ExtType) {
2137 default: break;
2138 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00002139 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002140 return VTBits-Tmp+1;
2141 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands92c43912008-06-06 12:08:01 +00002142 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002143 return VTBits-Tmp;
2144 }
2145 }
2146
2147 // Allow the target to implement this method for its nodes.
2148 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michel91099d62009-02-17 22:15:04 +00002149 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2151 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2152 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohman4afc4252008-05-23 02:28:01 +00002153 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 }
Scott Michel91099d62009-02-17 22:15:04 +00002155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2157 // use this information.
Dan Gohman63f4e462008-02-27 01:23:58 +00002158 APInt KnownZero, KnownOne;
2159 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michel91099d62009-02-17 22:15:04 +00002161
Dan Gohman63f4e462008-02-27 01:23:58 +00002162 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 Mask = KnownZero;
Dan Gohman63f4e462008-02-27 01:23:58 +00002164 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002165 Mask = KnownOne;
2166 } else {
2167 // Nothing known.
Dan Gohman4afc4252008-05-23 02:28:01 +00002168 return FirstAnswer;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002169 }
Scott Michel91099d62009-02-17 22:15:04 +00002170
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2172 // the number of identical bits in the top of the input value.
Dan Gohman63f4e462008-02-27 01:23:58 +00002173 Mask = ~Mask;
2174 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 // Return # leading zeros. We use 'min' here in case Val was zero before
2176 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohman4afc4252008-05-23 02:28:01 +00002177 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002178}
2179
Dan Gohman41b3f4a2009-09-03 20:34:31 +00002180bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2181 // If we're told that NaNs won't happen, assume they won't.
2182 if (FiniteOnlyFPMath())
2183 return true;
2184
2185 // If the value is a constant, we can obviously see if it is a NaN or not.
2186 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2187 return !C->getValueAPF().isNaN();
2188
2189 // TODO: Recognize more cases here.
2190
2191 return false;
2192}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002193
Dan Gohman8181bd12008-07-27 21:46:04 +00002194bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Cheng2e28d622008-02-02 04:07:54 +00002195 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2196 if (!GA) return false;
Dan Gohman36322c72008-10-18 02:06:02 +00002197 if (GA->getOffset() != 0) return false;
Evan Cheng2e28d622008-02-02 04:07:54 +00002198 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2199 if (!GV) return false;
Devang Patelfb3a4882009-01-13 22:54:57 +00002200 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patel42f6bed2009-01-13 23:54:55 +00002201 return MMI && MMI->hasDebugInfo();
Evan Cheng2e28d622008-02-02 04:07:54 +00002202}
2203
2204
Evan Cheng411fc172008-05-13 08:35:03 +00002205/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2206/// element of the result of the vector shuffle.
Nate Begemane8f61cb2009-04-29 05:20:52 +00002207SDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N,
2208 unsigned i) {
Owen Andersonac9de032009-08-10 22:56:29 +00002209 EVT VT = N->getValueType(0);
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002210 DebugLoc dl = N->getDebugLoc();
Nate Begemane8f61cb2009-04-29 05:20:52 +00002211 if (N->getMaskElt(i) < 0)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002212 return getUNDEF(VT.getVectorElementType());
Nate Begemane8f61cb2009-04-29 05:20:52 +00002213 unsigned Index = N->getMaskElt(i);
2214 unsigned NumElems = VT.getVectorNumElements();
Dan Gohman8181bd12008-07-27 21:46:04 +00002215 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Cheng57db53b2008-06-25 20:52:59 +00002216 Index %= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00002217
2218 if (V.getOpcode() == ISD::BIT_CONVERT) {
2219 V = V.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002220 EVT VVT = V.getValueType();
Nate Begeman543d2142009-04-27 18:41:29 +00002221 if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems)
Dan Gohman8181bd12008-07-27 21:46:04 +00002222 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002223 }
Evan Chengdea99362008-05-29 08:22:04 +00002224 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002225 return (Index == 0) ? V.getOperand(0)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002226 : getUNDEF(VT.getVectorElementType());
Evan Chengdea99362008-05-29 08:22:04 +00002227 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Cheng57db53b2008-06-25 20:52:59 +00002228 return V.getOperand(Index);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002229 if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V))
2230 return getShuffleScalarElt(SVN, Index);
Dan Gohman8181bd12008-07-27 21:46:04 +00002231 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00002232}
2233
2234
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002235/// getNode - Gets or creates the specified node.
2236///
Owen Andersonac9de032009-08-10 22:56:29 +00002237SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002238 FoldingSetNodeID ID;
Dan Gohman703ce5d2008-07-07 18:26:29 +00002239 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002240 void *IP = 0;
2241 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002242 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002243 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohmanee036282009-04-09 23:54:40 +00002244 new (N) SDNode(Opcode, DL, getVTList(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002245 CSEMap.InsertNode(N, IP);
Scott Michel91099d62009-02-17 22:15:04 +00002246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002248#ifndef NDEBUG
2249 VerifyNode(N);
2250#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002251 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002252}
2253
Bill Wendlingdf4de112009-01-28 22:17:52 +00002254SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersonac9de032009-08-10 22:56:29 +00002255 EVT VT, SDValue Operand) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 // Constant fold unary operations with an integer constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002257 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman161652c2008-02-29 01:47:35 +00002258 const APInt &Val = C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002259 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 switch (Opcode) {
2261 default: break;
Evan Chengeadaf442008-03-06 17:42:34 +00002262 case ISD::SIGN_EXTEND:
2263 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 case ISD::ANY_EXTEND:
Dan Gohman161652c2008-02-29 01:47:35 +00002265 case ISD::ZERO_EXTEND:
Evan Chengeadaf442008-03-06 17:42:34 +00002266 case ISD::TRUNCATE:
2267 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002268 case ISD::UINT_TO_FP:
2269 case ISD::SINT_TO_FP: {
2270 const uint64_t zero[] = {0, 0};
Dale Johannesenb89072e2007-10-16 23:38:29 +00002271 // No compile time operations on this type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002272 if (VT==MVT::ppcf128)
Dale Johannesenb89072e2007-10-16 23:38:29 +00002273 break;
Dan Gohman161652c2008-02-29 01:47:35 +00002274 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
Scott Michel91099d62009-02-17 22:15:04 +00002275 (void)apf.convertFromAPInt(Val,
Dan Gohman161652c2008-02-29 01:47:35 +00002276 Opcode==ISD::SINT_TO_FP,
2277 APFloat::rmNearestTiesToEven);
Dale Johannesen958b08b2007-09-19 23:55:34 +00002278 return getConstantFP(apf, VT);
2279 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 case ISD::BIT_CONVERT:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002281 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman161652c2008-02-29 01:47:35 +00002282 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002283 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman161652c2008-02-29 01:47:35 +00002284 return getConstantFP(Val.bitsToDouble(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002285 break;
2286 case ISD::BSWAP:
Dan Gohman161652c2008-02-29 01:47:35 +00002287 return getConstant(Val.byteSwap(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 case ISD::CTPOP:
Dan Gohman161652c2008-02-29 01:47:35 +00002289 return getConstant(Val.countPopulation(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 case ISD::CTLZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002291 return getConstant(Val.countLeadingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 case ISD::CTTZ:
Dan Gohman161652c2008-02-29 01:47:35 +00002293 return getConstant(Val.countTrailingZeros(), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002294 }
2295 }
2296
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002297 // Constant fold unary operations with a floating point constant operand.
Gabor Greif1c80d112008-08-28 21:40:38 +00002298 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002299 APFloat V = C->getValueAPF(); // make copy
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002300 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesenb89072e2007-10-16 23:38:29 +00002301 switch (Opcode) {
2302 case ISD::FNEG:
2303 V.changeSign();
2304 return getConstantFP(V, VT);
2305 case ISD::FABS:
2306 V.clearSign();
2307 return getConstantFP(V, VT);
2308 case ISD::FP_ROUND:
Dale Johannesen6e547b42008-10-09 23:00:39 +00002309 case ISD::FP_EXTEND: {
2310 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002311 // This can return overflow, underflow, or inexact; we don't care.
2312 // FIXME need to be more flexible about rounding mode.
Owen Andersonac9de032009-08-10 22:56:29 +00002313 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen6e547b42008-10-09 23:00:39 +00002314 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002315 return getConstantFP(V, VT);
Dale Johannesen6e547b42008-10-09 23:00:39 +00002316 }
Dale Johannesenb89072e2007-10-16 23:38:29 +00002317 case ISD::FP_TO_SINT:
2318 case ISD::FP_TO_UINT: {
Dale Johannesen63d45032009-04-24 21:34:13 +00002319 integerPart x[2];
Dale Johannesen6e547b42008-10-09 23:00:39 +00002320 bool ignored;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002321 assert(integerPartWidth >= 64);
2322 // FIXME need to be more flexible about rounding mode.
Dale Johannesen63d45032009-04-24 21:34:13 +00002323 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesenb89072e2007-10-16 23:38:29 +00002324 Opcode==ISD::FP_TO_SINT,
Dale Johannesen6e547b42008-10-09 23:00:39 +00002325 APFloat::rmTowardZero, &ignored);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002326 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2327 break;
Dale Johannesen63d45032009-04-24 21:34:13 +00002328 APInt api(VT.getSizeInBits(), 2, x);
2329 return getConstant(api, VT);
Dale Johannesenb89072e2007-10-16 23:38:29 +00002330 }
2331 case ISD::BIT_CONVERT:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002332 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002333 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002334 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen6e547b42008-10-09 23:00:39 +00002335 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002336 break;
Dale Johannesenb89072e2007-10-16 23:38:29 +00002337 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 }
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002339 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340
Gabor Greif1c80d112008-08-28 21:40:38 +00002341 unsigned OpOpcode = Operand.getNode()->getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342 switch (Opcode) {
2343 case ISD::TokenFactor:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002344 case ISD::MERGE_VALUES:
Dan Gohman29c3cef2008-08-14 20:04:46 +00002345 case ISD::CONCAT_VECTORS:
Duncan Sands42d7bb82008-12-01 11:41:29 +00002346 return Operand; // Factor, merge or concat of one node? No need.
Edwin Törökbd448e32009-07-14 16:55:14 +00002347 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348 case ISD::FP_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002349 assert(VT.isFloatingPoint() &&
2350 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattnerd3f56172008-01-16 17:59:31 +00002351 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattnere6465dd2008-03-11 06:21:08 +00002352 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002353 return getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002354 break;
Chris Lattnere6465dd2008-03-11 06:21:08 +00002355 case ISD::SIGN_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002356 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 "Invalid SIGN_EXTEND!");
2358 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002359 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002360 && "Invalid sext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002361 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002362 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 break;
2364 case ISD::ZERO_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002365 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002366 "Invalid ZERO_EXTEND!");
2367 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002368 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002369 && "Invalid zext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michel91099d62009-02-17 22:15:04 +00002371 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002372 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002373 break;
2374 case ISD::ANY_EXTEND:
Duncan Sands92c43912008-06-06 12:08:01 +00002375 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 "Invalid ANY_EXTEND!");
2377 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sandsec142ee2008-06-08 20:54:56 +00002378 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002379 && "Invalid anyext node, dst < src!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2381 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002382 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 break;
2384 case ISD::TRUNCATE:
Duncan Sands92c43912008-06-06 12:08:01 +00002385 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002386 "Invalid TRUNCATE!");
2387 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sandsec142ee2008-06-08 20:54:56 +00002388 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsa9810f32007-10-16 09:56:48 +00002389 && "Invalid truncate node, src < dst!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002390 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002391 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2393 OpOpcode == ISD::ANY_EXTEND) {
2394 // If the source is smaller than the dest, we still need an extend.
Gabor Greif1c80d112008-08-28 21:40:38 +00002395 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002396 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00002397 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002398 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 else
Gabor Greif1c80d112008-08-28 21:40:38 +00002400 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002401 }
2402 break;
2403 case ISD::BIT_CONVERT:
2404 // Basic sanity checking.
Duncan Sands92c43912008-06-06 12:08:01 +00002405 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 && "Cannot BIT_CONVERT between types of different sizes!");
2407 if (VT == Operand.getValueType()) return Operand; // noop conversion.
2408 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002409 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 if (OpOpcode == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002411 return getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 break;
2413 case ISD::SCALAR_TO_VECTOR:
Duncan Sands92c43912008-06-06 12:08:01 +00002414 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sands6a3a01e2009-04-18 20:16:54 +00002415 (VT.getVectorElementType() == Operand.getValueType() ||
2416 (VT.getVectorElementType().isInteger() &&
2417 Operand.getValueType().isInteger() &&
2418 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattner9f0705c2008-03-08 23:43:36 +00002420 if (OpOpcode == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002421 return getUNDEF(VT);
Chris Lattner9f0705c2008-03-08 23:43:36 +00002422 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2423 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2424 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2425 Operand.getConstantOperandVal(1) == 0 &&
2426 Operand.getOperand(0).getValueType() == VT)
2427 return Operand.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 break;
2429 case ISD::FNEG:
Mon P Wang4340d5d2009-01-31 06:07:45 +00002430 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2431 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002432 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greif1c80d112008-08-28 21:40:38 +00002433 Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greif1c80d112008-08-28 21:40:38 +00002435 return Operand.getNode()->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002436 break;
2437 case ISD::FABS:
2438 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002439 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002440 break;
2441 }
2442
2443 SDNode *N;
2444 SDVTList VTs = getVTList(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002445 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 FoldingSetNodeID ID;
Dan Gohman8181bd12008-07-27 21:46:04 +00002447 SDValue Ops[1] = { Operand };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
2449 void *IP = 0;
2450 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002451 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002452 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002453 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 CSEMap.InsertNode(N, IP);
2455 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002456 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002457 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002458 }
Duncan Sandsd3ace282008-07-21 10:20:31 +00002459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002461#ifndef NDEBUG
2462 VerifyNode(N);
2463#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002464 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465}
2466
Bill Wendling16bc18c2008-09-24 10:16:24 +00002467SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersonac9de032009-08-10 22:56:29 +00002468 EVT VT,
Bill Wendling16bc18c2008-09-24 10:16:24 +00002469 ConstantSDNode *Cst1,
2470 ConstantSDNode *Cst2) {
2471 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2472
2473 switch (Opcode) {
2474 case ISD::ADD: return getConstant(C1 + C2, VT);
2475 case ISD::SUB: return getConstant(C1 - C2, VT);
2476 case ISD::MUL: return getConstant(C1 * C2, VT);
2477 case ISD::UDIV:
2478 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2479 break;
2480 case ISD::UREM:
2481 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2482 break;
2483 case ISD::SDIV:
2484 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2485 break;
2486 case ISD::SREM:
2487 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2488 break;
2489 case ISD::AND: return getConstant(C1 & C2, VT);
2490 case ISD::OR: return getConstant(C1 | C2, VT);
2491 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2492 case ISD::SHL: return getConstant(C1 << C2, VT);
2493 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2494 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2495 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2496 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2497 default: break;
2498 }
2499
2500 return SDValue();
2501}
2502
Owen Andersonac9de032009-08-10 22:56:29 +00002503SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00002504 SDValue N1, SDValue N2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002505 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2506 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 switch (Opcode) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002508 default: break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 case ISD::TokenFactor:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002510 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2511 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002512 // Fold trivial token factors.
2513 if (N1.getOpcode() == ISD::EntryToken) return N2;
2514 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohmanc0e18d62008-10-01 15:11:19 +00002515 if (N1 == N2) return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 break;
Dan Gohman29c3cef2008-08-14 20:04:46 +00002517 case ISD::CONCAT_VECTORS:
2518 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2519 // one big BUILD_VECTOR.
2520 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2521 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002522 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2523 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Cheng907a2d22009-02-25 22:49:59 +00002524 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002525 }
2526 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527 case ISD::AND:
Duncan Sands92c43912008-06-06 12:08:01 +00002528 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002529 N1.getValueType() == VT && "Binary operator types must match!");
2530 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2531 // worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002532 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002533 return N2;
Chris Lattner8aa8a5e2008-01-26 01:05:42 +00002534 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2535 return N1;
Chris Lattnercc126e32008-01-22 19:09:33 +00002536 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 case ISD::OR:
2538 case ISD::XOR:
Dan Gohman3f76aed2008-06-02 22:27:05 +00002539 case ISD::ADD:
2540 case ISD::SUB:
Duncan Sands92c43912008-06-06 12:08:01 +00002541 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002542 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman3f76aed2008-06-02 22:27:05 +00002543 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2544 // it's worth handling here.
Dan Gohman9d24dc72008-03-13 22:13:53 +00002545 if (N2C && N2C->isNullValue())
Chris Lattnercc126e32008-01-22 19:09:33 +00002546 return N1;
2547 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 case ISD::UDIV:
2549 case ISD::UREM:
2550 case ISD::MULHU:
2551 case ISD::MULHS:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002552 case ISD::MUL:
2553 case ISD::SDIV:
2554 case ISD::SREM:
Dan Gohman3d015562009-01-22 21:58:43 +00002555 assert(VT.isInteger() && "This operator does not apply to FP types!");
2556 // fall through
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557 case ISD::FADD:
2558 case ISD::FSUB:
2559 case ISD::FMUL:
2560 case ISD::FDIV:
2561 case ISD::FREM:
Dan Gohman46ef3eb2009-01-23 19:10:37 +00002562 if (UnsafeFPMath) {
2563 if (Opcode == ISD::FADD) {
2564 // 0+x --> x
2565 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2566 if (CFP->getValueAPF().isZero())
2567 return N2;
2568 // x+0 --> x
2569 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2570 if (CFP->getValueAPF().isZero())
2571 return N1;
2572 } else if (Opcode == ISD::FSUB) {
2573 // x-0 --> x
2574 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2575 if (CFP->getValueAPF().isZero())
2576 return N1;
2577 }
Dan Gohman3d015562009-01-22 21:58:43 +00002578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579 assert(N1.getValueType() == N2.getValueType() &&
2580 N1.getValueType() == VT && "Binary operator types must match!");
2581 break;
2582 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2583 assert(N1.getValueType() == VT &&
Duncan Sands92c43912008-06-06 12:08:01 +00002584 N1.getValueType().isFloatingPoint() &&
2585 N2.getValueType().isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586 "Invalid FCOPYSIGN!");
2587 break;
2588 case ISD::SHL:
2589 case ISD::SRA:
2590 case ISD::SRL:
2591 case ISD::ROTL:
2592 case ISD::ROTR:
2593 assert(VT == N1.getValueType() &&
2594 "Shift operators return type must be the same as their first arg");
Duncan Sands92c43912008-06-06 12:08:01 +00002595 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattnera113d3e2008-07-02 17:01:57 +00002596 "Shifts only work on integers");
2597
2598 // Always fold shifts of i1 values so the code generator doesn't need to
2599 // handle them. Since we know the size of the shift has to be less than the
2600 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002601 if (VT == MVT::i1)
Chris Lattnera113d3e2008-07-02 17:01:57 +00002602 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 break;
2604 case ISD::FP_ROUND_INREG: {
Owen Andersonac9de032009-08-10 22:56:29 +00002605 EVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands92c43912008-06-06 12:08:01 +00002607 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 "Cannot FP_ROUND_INREG integer types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002609 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002610 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611 break;
2612 }
Chris Lattner5872a362008-01-17 07:00:52 +00002613 case ISD::FP_ROUND:
Duncan Sands92c43912008-06-06 12:08:01 +00002614 assert(VT.isFloatingPoint() &&
2615 N1.getValueType().isFloatingPoint() &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00002616 VT.bitsLE(N1.getValueType()) &&
Chris Lattner5872a362008-01-17 07:00:52 +00002617 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002618 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner5872a362008-01-17 07:00:52 +00002619 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 case ISD::AssertSext:
Chris Lattnercc126e32008-01-22 19:09:33 +00002621 case ISD::AssertZext: {
Owen Andersonac9de032009-08-10 22:56:29 +00002622 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattnercc126e32008-01-22 19:09:33 +00002623 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002624 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002625 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002626 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sands539510b2008-02-10 10:08:52 +00002627 if (VT == EVT) return N1; // noop assertion.
Chris Lattnercc126e32008-01-22 19:09:33 +00002628 break;
2629 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 case ISD::SIGN_EXTEND_INREG: {
Owen Andersonac9de032009-08-10 22:56:29 +00002631 EVT EVT = cast<VTSDNode>(N2)->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands92c43912008-06-06 12:08:01 +00002633 assert(VT.isInteger() && EVT.isInteger() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634 "Cannot *_EXTEND_INREG FP types");
Duncan Sandsec142ee2008-06-08 20:54:56 +00002635 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattnercc126e32008-01-22 19:09:33 +00002636 if (EVT == VT) return N1; // Not actually extending
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637
Chris Lattnercc126e32008-01-22 19:09:33 +00002638 if (N1C) {
Dan Gohman161652c2008-02-29 01:47:35 +00002639 APInt Val = N1C->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002640 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman161652c2008-02-29 01:47:35 +00002641 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng7faa1d72008-03-06 08:20:51 +00002642 Val = Val.ashr(Val.getBitWidth()-FromBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 return getConstant(Val, VT);
2644 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002645 break;
2646 }
2647 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattner9f0705c2008-03-08 23:43:36 +00002648 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2649 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002650 return getUNDEF(VT);
Scott Michel91099d62009-02-17 22:15:04 +00002651
Chris Lattnercc126e32008-01-22 19:09:33 +00002652 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2653 // expanding copies of large vectors from registers.
Dan Gohman101124c2008-08-13 21:51:37 +00002654 if (N2C &&
2655 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattnercc126e32008-01-22 19:09:33 +00002656 N1.getNumOperands() > 0) {
2657 unsigned Factor =
Duncan Sands92c43912008-06-06 12:08:01 +00002658 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002659 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002660 N1.getOperand(N2C->getZExtValue() / Factor),
2661 getConstant(N2C->getZExtValue() % Factor,
2662 N2.getValueType()));
Chris Lattnercc126e32008-01-22 19:09:33 +00002663 }
2664
2665 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2666 // expanding large vector constants.
Bob Wilson2b570bb2009-04-13 22:05:19 +00002667 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2668 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersonac9de032009-08-10 22:56:29 +00002669 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanf1896f22009-07-09 22:01:03 +00002670 if (Elt.getValueType() != VEltTy) {
Bob Wilson2b570bb2009-04-13 22:05:19 +00002671 // If the vector element type is not legal, the BUILD_VECTOR operands
2672 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanf1896f22009-07-09 22:01:03 +00002673 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2674 }
2675 if (VT != VEltTy) {
2676 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2677 // result is implicitly extended.
2678 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilson2b570bb2009-04-13 22:05:19 +00002679 }
2680 return Elt;
2681 }
Scott Michel91099d62009-02-17 22:15:04 +00002682
Chris Lattnercc126e32008-01-22 19:09:33 +00002683 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2684 // operations are lowered to scalars.
Dan Gohman101124c2008-08-13 21:51:37 +00002685 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohmanb38eabf2009-01-29 16:10:46 +00002686 // If the indices are the same, return the inserted element.
Dan Gohman101124c2008-08-13 21:51:37 +00002687 if (N1.getOperand(2) == N2)
2688 return N1.getOperand(1);
Dan Gohmanb38eabf2009-01-29 16:10:46 +00002689 // If the indices are known different, extract the element from
2690 // the original vector.
2691 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2692 isa<ConstantSDNode>(N2))
Dale Johannesenf2103ee2009-02-03 01:55:44 +00002693 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman101124c2008-08-13 21:51:37 +00002694 }
Chris Lattnercc126e32008-01-22 19:09:33 +00002695 break;
2696 case ISD::EXTRACT_ELEMENT:
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002697 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands1568af82008-06-25 20:24:48 +00002698 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2699 (N1.getValueType().isInteger() == VT.isInteger()) &&
2700 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sandsc4d85172008-03-12 20:30:08 +00002701
Chris Lattnercc126e32008-01-22 19:09:33 +00002702 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2703 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michel91099d62009-02-17 22:15:04 +00002704 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattnercc126e32008-01-22 19:09:33 +00002705 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002706 return N1.getOperand(N2C->getZExtValue());
Duncan Sandsc4d85172008-03-12 20:30:08 +00002707
Chris Lattnercc126e32008-01-22 19:09:33 +00002708 // EXTRACT_ELEMENT of a constant int is also very common.
2709 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002710 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002711 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman452b4ce2008-03-24 16:38:05 +00002712 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2713 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattnercc126e32008-01-22 19:09:33 +00002714 }
2715 break;
Duncan Sandsbd13a812008-02-20 17:38:09 +00002716 case ISD::EXTRACT_SUBVECTOR:
2717 if (N1.getValueType() == VT) // Trivial extraction.
2718 return N1;
2719 break;
Chris Lattnercc126e32008-01-22 19:09:33 +00002720 }
2721
2722 if (N1C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 if (N2C) {
Bill Wendling16bc18c2008-09-24 10:16:24 +00002724 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2725 if (SV.getNode()) return SV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 } else { // Cannonicalize constant to RHS if commutative
2727 if (isCommutativeBinOp(Opcode)) {
2728 std::swap(N1C, N2C);
2729 std::swap(N1, N2);
2730 }
2731 }
2732 }
2733
Chris Lattnercc126e32008-01-22 19:09:33 +00002734 // Constant fold FP operations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002735 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2736 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737 if (N1CFP) {
Chris Lattnercc126e32008-01-22 19:09:33 +00002738 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2739 // Cannonicalize constant to RHS if commutative
2740 std::swap(N1CFP, N2CFP);
2741 std::swap(N1, N2);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002742 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002743 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2744 APFloat::opStatus s;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745 switch (Opcode) {
Scott Michel91099d62009-02-17 22:15:04 +00002746 case ISD::FADD:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002747 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattnercc126e32008-01-22 19:09:33 +00002748 if (s != APFloat::opInvalidOp)
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002749 return getConstantFP(V1, VT);
2750 break;
Scott Michel91099d62009-02-17 22:15:04 +00002751 case ISD::FSUB:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002752 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2753 if (s!=APFloat::opInvalidOp)
2754 return getConstantFP(V1, VT);
2755 break;
2756 case ISD::FMUL:
2757 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2758 if (s!=APFloat::opInvalidOp)
2759 return getConstantFP(V1, VT);
2760 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 case ISD::FDIV:
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002762 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2763 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2764 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 break;
2766 case ISD::FREM :
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002767 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2768 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2769 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770 break;
Dale Johannesen7604c1b2007-08-31 23:34:27 +00002771 case ISD::FCOPYSIGN:
2772 V1.copySign(V2);
2773 return getConstantFP(V1, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774 default: break;
2775 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 }
2777 }
Scott Michel91099d62009-02-17 22:15:04 +00002778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 // Canonicalize an UNDEF to the RHS, even over a constant.
2780 if (N1.getOpcode() == ISD::UNDEF) {
2781 if (isCommutativeBinOp(Opcode)) {
2782 std::swap(N1, N2);
2783 } else {
2784 switch (Opcode) {
2785 case ISD::FP_ROUND_INREG:
2786 case ISD::SIGN_EXTEND_INREG:
2787 case ISD::SUB:
2788 case ISD::FSUB:
2789 case ISD::FDIV:
2790 case ISD::FREM:
2791 case ISD::SRA:
2792 return N1; // fold op(undef, arg2) -> undef
2793 case ISD::UDIV:
2794 case ISD::SDIV:
2795 case ISD::UREM:
2796 case ISD::SREM:
2797 case ISD::SRL:
2798 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002799 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2801 // For vectors, we can't easily build an all zero vector, just return
2802 // the LHS.
2803 return N2;
2804 }
2805 }
2806 }
Scott Michel91099d62009-02-17 22:15:04 +00002807
2808 // Fold a bunch of operators when the RHS is undef.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809 if (N2.getOpcode() == ISD::UNDEF) {
2810 switch (Opcode) {
Evan Cheng5d00cb42008-03-25 20:08:07 +00002811 case ISD::XOR:
2812 if (N1.getOpcode() == ISD::UNDEF)
2813 // Handle undef ^ undef -> 0 special case. This is a common
2814 // idiom (misuse).
2815 return getConstant(0, VT);
2816 // fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817 case ISD::ADD:
2818 case ISD::ADDC:
2819 case ISD::ADDE:
2820 case ISD::SUB:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 case ISD::UDIV:
2822 case ISD::SDIV:
2823 case ISD::UREM:
2824 case ISD::SREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002825 return N2; // fold op(arg1, undef) -> undef
Dan Gohmana22a8402009-06-04 17:12:12 +00002826 case ISD::FADD:
2827 case ISD::FSUB:
2828 case ISD::FMUL:
2829 case ISD::FDIV:
2830 case ISD::FREM:
2831 if (UnsafeFPMath)
2832 return N2;
2833 break;
Scott Michel91099d62009-02-17 22:15:04 +00002834 case ISD::MUL:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 case ISD::AND:
2836 case ISD::SRL:
2837 case ISD::SHL:
Duncan Sands92c43912008-06-06 12:08:01 +00002838 if (!VT.isVector())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2840 // For vectors, we can't easily build an all zero vector, just return
2841 // the LHS.
2842 return N1;
2843 case ISD::OR:
Duncan Sands92c43912008-06-06 12:08:01 +00002844 if (!VT.isVector())
Duncan Sands505ba942009-02-01 18:06:53 +00002845 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002846 // For vectors, we can't easily build an all one vector, just return
2847 // the LHS.
2848 return N1;
2849 case ISD::SRA:
2850 return N1;
2851 }
2852 }
2853
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 // Memoize this node if possible.
2855 SDNode *N;
2856 SDVTList VTs = getVTList(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002857 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002858 SDValue Ops[] = { N1, N2 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 FoldingSetNodeID ID;
2860 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
2861 void *IP = 0;
2862 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002863 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002864 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002865 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 CSEMap.InsertNode(N, IP);
2867 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002868 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002869 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 }
2871
2872 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002873#ifndef NDEBUG
2874 VerifyNode(N);
2875#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002876 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002877}
2878
Owen Andersonac9de032009-08-10 22:56:29 +00002879SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00002880 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 // Perform various simplifications.
Gabor Greif1c80d112008-08-28 21:40:38 +00002882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2883 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884 switch (Opcode) {
Dan Gohman29c3cef2008-08-14 20:04:46 +00002885 case ISD::CONCAT_VECTORS:
2886 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2887 // one big BUILD_VECTOR.
2888 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2889 N2.getOpcode() == ISD::BUILD_VECTOR &&
2890 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002891 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2892 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2893 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Cheng907a2d22009-02-25 22:49:59 +00002894 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman29c3cef2008-08-14 20:04:46 +00002895 }
2896 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 case ISD::SETCC: {
2898 // Use FoldSetCC to simplify SETCC's.
Dale Johannesen38496eb2009-02-03 00:47:48 +00002899 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002900 if (Simp.getNode()) return Simp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 break;
2902 }
2903 case ISD::SELECT:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002904 if (N1C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002905 if (N1C->getZExtValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906 return N2; // select true, X, Y -> X
2907 else
2908 return N3; // select false, X, Y -> Y
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002909 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002910
2911 if (N2 == N3) return N2; // select C, X, X -> X
2912 break;
2913 case ISD::BRCOND:
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002914 if (N2C) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002915 if (N2C->getZExtValue()) // Unconditional branch
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002916 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 else
2918 return N1; // Never-taken branch
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002919 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 break;
2921 case ISD::VECTOR_SHUFFLE:
Edwin Törökbd448e32009-07-14 16:55:14 +00002922 llvm_unreachable("should use getVectorShuffle constructor!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002923 break;
2924 case ISD::BIT_CONVERT:
2925 // Fold bit_convert nodes from a type to themselves.
2926 if (N1.getValueType() == VT)
2927 return N1;
2928 break;
2929 }
2930
2931 // Memoize node if it doesn't produce a flag.
2932 SDNode *N;
2933 SDVTList VTs = getVTList(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002934 if (VT != MVT::Flag) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002935 SDValue Ops[] = { N1, N2, N3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936 FoldingSetNodeID ID;
2937 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
2938 void *IP = 0;
2939 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00002940 return SDValue(E, 0);
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002941 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002942 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 CSEMap.InsertNode(N, IP);
2944 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00002945 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00002946 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 }
2948 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00002949#ifndef NDEBUG
2950 VerifyNode(N);
2951#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00002952 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953}
2954
Owen Andersonac9de032009-08-10 22:56:29 +00002955SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00002956 SDValue N1, SDValue N2, SDValue N3,
2957 SDValue N4) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002958 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00002959 return getNode(Opcode, DL, VT, Ops, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960}
2961
Owen Andersonac9de032009-08-10 22:56:29 +00002962SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00002963 SDValue N1, SDValue N2, SDValue N3,
2964 SDValue N4, SDValue N5) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002965 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00002966 return getNode(Opcode, DL, VT, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967}
2968
Dan Gohman9178de12009-08-05 01:29:28 +00002969/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
2970/// the incoming stack arguments to be loaded from the stack.
2971SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
2972 SmallVector<SDValue, 8> ArgChains;
2973
2974 // Include the original chain at the beginning of the list. When this is
2975 // used by target LowerCall hooks, this helps legalize find the
2976 // CALLSEQ_BEGIN node.
2977 ArgChains.push_back(Chain);
2978
2979 // Add a chain value for each stack argument.
2980 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
2981 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
2982 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
2983 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
2984 if (FI->getIndex() < 0)
2985 ArgChains.push_back(SDValue(L, 1));
2986
2987 // Build a tokenfactor for all the chains.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002988 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman9178de12009-08-05 01:29:28 +00002989 &ArgChains[0], ArgChains.size());
2990}
2991
Dan Gohmane8b391e2008-04-12 04:36:06 +00002992/// getMemsetValue - Vectorized representation of the memset value
2993/// operand.
Owen Andersonac9de032009-08-10 22:56:29 +00002994static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00002995 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00002996 unsigned NumBits = VT.isVector() ?
2997 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohmane8b391e2008-04-12 04:36:06 +00002998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002999 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003000 unsigned Shift = 8;
Evan Cheng8c590372008-05-15 08:39:06 +00003001 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003002 Val = (Val << Shift) | Val;
3003 Shift <<= 1;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003004 }
Duncan Sands92c43912008-06-06 12:08:01 +00003005 if (VT.isInteger())
Evan Cheng8c590372008-05-15 08:39:06 +00003006 return DAG.getConstant(Val, VT);
3007 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003008 }
Evan Cheng8c590372008-05-15 08:39:06 +00003009
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00003010 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003011 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Cheng8c590372008-05-15 08:39:06 +00003012 unsigned Shift = 8;
3013 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003014 Value = DAG.getNode(ISD::OR, dl, VT,
3015 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands5bb3f8d2008-10-30 20:26:50 +00003016 DAG.getConstant(Shift,
3017 TLI.getShiftAmountTy())),
3018 Value);
Evan Cheng8c590372008-05-15 08:39:06 +00003019 Shift <<= 1;
3020 }
3021
3022 return Value;
Rafael Espindola80825902007-10-19 10:41:11 +00003023}
3024
Dan Gohmane8b391e2008-04-12 04:36:06 +00003025/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3026/// used when a memcpy is turned into a memset when the source is a constant
3027/// string ptr.
Owen Andersonac9de032009-08-10 22:56:29 +00003028static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner9c220f62009-09-20 17:32:21 +00003029 const TargetLowering &TLI,
3030 std::string &Str, unsigned Offset) {
Evan Cheng833501d2008-06-30 07:31:25 +00003031 // Handle vector with all elements zero.
3032 if (Str.empty()) {
3033 if (VT.isInteger())
3034 return DAG.getConstant(0, VT);
3035 unsigned NumElts = VT.getVectorNumElements();
Owen Anderson2dd68a22009-08-11 21:59:30 +00003036 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003037 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson77f4eb52009-08-12 00:36:31 +00003038 DAG.getConstant(0,
3039 EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts)));
Evan Cheng833501d2008-06-30 07:31:25 +00003040 }
3041
Duncan Sands92c43912008-06-06 12:08:01 +00003042 assert(!VT.isVector() && "Can't handle vector type here!");
3043 unsigned NumBits = VT.getSizeInBits();
Evan Cheng8c590372008-05-15 08:39:06 +00003044 unsigned MSB = NumBits / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003045 uint64_t Val = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003046 if (TLI.isLittleEndian())
3047 Offset = Offset + MSB - 1;
3048 for (unsigned i = 0; i != MSB; ++i) {
3049 Val = (Val << 8) | (unsigned char)Str[Offset];
3050 Offset += TLI.isLittleEndian() ? -1 : 1;
3051 }
3052 return DAG.getConstant(Val, VT);
Rafael Espindola80825902007-10-19 10:41:11 +00003053}
3054
Scott Michel91099d62009-02-17 22:15:04 +00003055/// getMemBasePlusOffset - Returns base and offset node for the
Evan Cheng8c590372008-05-15 08:39:06 +00003056///
Dan Gohman8181bd12008-07-27 21:46:04 +00003057static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003058 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003059 EVT VT = Base.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003060 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesenf2103ee2009-02-03 01:55:44 +00003061 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohmane8b391e2008-04-12 04:36:06 +00003062}
3063
Evan Cheng8c590372008-05-15 08:39:06 +00003064/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3065///
Dan Gohman8181bd12008-07-27 21:46:04 +00003066static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Cheng8c590372008-05-15 08:39:06 +00003067 unsigned SrcDelta = 0;
3068 GlobalAddressSDNode *G = NULL;
3069 if (Src.getOpcode() == ISD::GlobalAddress)
3070 G = cast<GlobalAddressSDNode>(Src);
3071 else if (Src.getOpcode() == ISD::ADD &&
3072 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3073 Src.getOperand(1).getOpcode() == ISD::Constant) {
3074 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003075 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Cheng8c590372008-05-15 08:39:06 +00003076 }
3077 if (!G)
3078 return false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003079
Evan Cheng8c590372008-05-15 08:39:06 +00003080 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Bill Wendlingf3f16e82009-03-13 04:39:26 +00003081 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3082 return true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003083
Evan Cheng8c590372008-05-15 08:39:06 +00003084 return false;
3085}
Dan Gohmane8b391e2008-04-12 04:36:06 +00003086
Evan Cheng8c590372008-05-15 08:39:06 +00003087/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3088/// to replace the memset / memcpy is below the threshold. It also returns the
3089/// types of the sequence of memory ops to perform memset / memcpy.
3090static
Owen Andersonac9de032009-08-10 22:56:29 +00003091bool MeetsMaxMemopRequirement(std::vector<EVT> &MemOps,
Dan Gohman8181bd12008-07-27 21:46:04 +00003092 SDValue Dst, SDValue Src,
Evan Cheng8c590372008-05-15 08:39:06 +00003093 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng833501d2008-06-30 07:31:25 +00003094 std::string &Str, bool &isSrcStr,
Evan Cheng8c590372008-05-15 08:39:06 +00003095 SelectionDAG &DAG,
3096 const TargetLowering &TLI) {
Evan Cheng833501d2008-06-30 07:31:25 +00003097 isSrcStr = isMemSrcFromString(Src, Str);
Evan Cheng8c590372008-05-15 08:39:06 +00003098 bool isSrcConst = isa<ConstantSDNode>(Src);
Owen Andersonac9de032009-08-10 22:56:29 +00003099 EVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr, DAG);
Benjamin Kramer8e8abb32009-08-15 20:46:16 +00003100 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses(VT);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003101 if (VT != MVT::iAny) {
Evan Cheng4e3d3972009-08-15 23:41:42 +00003102 const Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3103 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
Evan Cheng8c590372008-05-15 08:39:06 +00003104 // If source is a string constant, this will require an unaligned load.
3105 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3106 if (Dst.getOpcode() != ISD::FrameIndex) {
3107 // Can't change destination alignment. It requires a unaligned store.
3108 if (AllowUnalign)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003109 VT = MVT::iAny;
Evan Cheng8c590372008-05-15 08:39:06 +00003110 } else {
3111 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3112 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3113 if (MFI->isFixedObjectIndex(FI)) {
3114 // Can't change destination alignment. It requires a unaligned store.
3115 if (AllowUnalign)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003116 VT = MVT::iAny;
Evan Cheng8c590372008-05-15 08:39:06 +00003117 } else {
Evan Cheng36cd8862008-06-04 23:37:54 +00003118 // Give the stack frame object a larger alignment if needed.
3119 if (MFI->getObjectAlignment(FI) < NewAlign)
3120 MFI->setObjectAlignment(FI, NewAlign);
Evan Cheng8c590372008-05-15 08:39:06 +00003121 Align = NewAlign;
3122 }
3123 }
3124 }
3125 }
3126
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003127 if (VT == MVT::iAny) {
Evan Cheng4e3d3972009-08-15 23:41:42 +00003128 if (TLI.allowsUnalignedMemoryAccesses(MVT::i64)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003129 VT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003130 } else {
3131 switch (Align & 7) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003132 case 0: VT = MVT::i64; break;
3133 case 4: VT = MVT::i32; break;
3134 case 2: VT = MVT::i16; break;
3135 default: VT = MVT::i8; break;
Evan Cheng8c590372008-05-15 08:39:06 +00003136 }
3137 }
3138
Owen Anderson2dd68a22009-08-11 21:59:30 +00003139 MVT LVT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003140 while (!TLI.isTypeLegal(LVT))
Owen Anderson2dd68a22009-08-11 21:59:30 +00003141 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands92c43912008-06-06 12:08:01 +00003142 assert(LVT.isInteger());
Evan Cheng8c590372008-05-15 08:39:06 +00003143
Duncan Sandsec142ee2008-06-08 20:54:56 +00003144 if (VT.bitsGT(LVT))
Evan Cheng8c590372008-05-15 08:39:06 +00003145 VT = LVT;
3146 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003147
3148 unsigned NumMemOps = 0;
3149 while (Size != 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00003150 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003151 while (VTSize > Size) {
Evan Cheng8c590372008-05-15 08:39:06 +00003152 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands92c43912008-06-06 12:08:01 +00003153 if (VT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003154 VT = MVT::i64;
Evan Cheng8c590372008-05-15 08:39:06 +00003155 while (!TLI.isTypeLegal(VT))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003156 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands92c43912008-06-06 12:08:01 +00003157 VTSize = VT.getSizeInBits() / 8;
Evan Cheng8c590372008-05-15 08:39:06 +00003158 } else {
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003159 // This can result in a type that is not legal on the target, e.g.
3160 // 1 or 2 bytes on PPC.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003161 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Cheng8c590372008-05-15 08:39:06 +00003162 VTSize >>= 1;
3163 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003164 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003165
3166 if (++NumMemOps > Limit)
3167 return false;
3168 MemOps.push_back(VT);
3169 Size -= VTSize;
3170 }
3171
3172 return true;
3173}
3174
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003175static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00003176 SDValue Chain, SDValue Dst,
3177 SDValue Src, uint64_t Size,
Evan Cheng8c590372008-05-15 08:39:06 +00003178 unsigned Align, bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00003179 const Value *DstSV, uint64_t DstSVOff,
3180 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00003181 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3182
Dan Gohman42d311c2008-05-29 19:42:22 +00003183 // Expand memcpy to a series of load and store ops if the size operand falls
3184 // below a certain threshold.
Owen Andersonac9de032009-08-10 22:56:29 +00003185 std::vector<EVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003186 uint64_t Limit = -1ULL;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003187 if (!AlwaysInline)
3188 Limit = TLI.getMaxStoresPerMemcpy();
Evan Cheng8c590372008-05-15 08:39:06 +00003189 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00003190 std::string Str;
3191 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00003192 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00003193 Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003194 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003195
Dan Gohmane8b391e2008-04-12 04:36:06 +00003196
Evan Cheng833501d2008-06-30 07:31:25 +00003197 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman8181bd12008-07-27 21:46:04 +00003198 SmallVector<SDValue, 8> OutChains;
Evan Cheng8c590372008-05-15 08:39:06 +00003199 unsigned NumMemOps = MemOps.size();
Evan Cheng833501d2008-06-30 07:31:25 +00003200 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner9c220f62009-09-20 17:32:21 +00003201 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersonac9de032009-08-10 22:56:29 +00003202 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003203 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003204 SDValue Value, Store;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003205
Evan Cheng833501d2008-06-30 07:31:25 +00003206 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Cheng8c590372008-05-15 08:39:06 +00003207 // It's unlikely a store of a vector immediate can be done in a single
3208 // instruction. It would require a load from a constantpool first.
Evan Cheng833501d2008-06-30 07:31:25 +00003209 // We also handle store a vector with all zero's.
3210 // FIXME: Handle other cases where store of vector immediate is done in
3211 // a single instruction.
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003212 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3213 Store = DAG.getStore(Chain, dl, Value,
Evan Cheng8c590372008-05-15 08:39:06 +00003214 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng757ec312008-07-09 06:38:06 +00003215 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003216 } else {
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003217 // The type might not be legal for the target. This should only happen
3218 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen6933df92009-06-24 17:11:31 +00003219 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3220 // to Load/Store if NVT==VT.
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003221 // FIXME does the case above also need this?
Owen Anderson77f4eb52009-08-12 00:36:31 +00003222 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen6933df92009-06-24 17:11:31 +00003223 assert(NVT.bitsGE(VT));
3224 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
3225 getMemBasePlusOffset(Src, SrcOff, DAG),
3226 SrcSV, SrcSVOff + SrcOff, VT, false, Align);
3227 Store = DAG.getTruncStore(Chain, dl, Value,
Dale Johannesen5ebb9302009-06-22 20:59:07 +00003228 getMemBasePlusOffset(Dst, DstOff, DAG),
Dale Johannesen6933df92009-06-24 17:11:31 +00003229 DstSV, DstSVOff + DstOff, VT, false, DstAlign);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003230 }
3231 OutChains.push_back(Store);
3232 SrcOff += VTSize;
3233 DstOff += VTSize;
3234 }
3235
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003236 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003237 &OutChains[0], OutChains.size());
3238}
3239
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003240static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00003241 SDValue Chain, SDValue Dst,
3242 SDValue Src, uint64_t Size,
Dan Gohman42d311c2008-05-29 19:42:22 +00003243 unsigned Align, bool AlwaysInline,
3244 const Value *DstSV, uint64_t DstSVOff,
3245 const Value *SrcSV, uint64_t SrcSVOff){
3246 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3247
3248 // Expand memmove to a series of load and store ops if the size operand falls
3249 // below a certain threshold.
Owen Andersonac9de032009-08-10 22:56:29 +00003250 std::vector<EVT> MemOps;
Dan Gohman88566ae2008-10-03 17:56:45 +00003251 uint64_t Limit = -1ULL;
Dan Gohman42d311c2008-05-29 19:42:22 +00003252 if (!AlwaysInline)
3253 Limit = TLI.getMaxStoresPerMemmove();
3254 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng833501d2008-06-30 07:31:25 +00003255 std::string Str;
3256 bool CopyFromStr;
Dan Gohman42d311c2008-05-29 19:42:22 +00003257 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng833501d2008-06-30 07:31:25 +00003258 Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 return SDValue();
Dan Gohman42d311c2008-05-29 19:42:22 +00003260
Dan Gohman42d311c2008-05-29 19:42:22 +00003261 uint64_t SrcOff = 0, DstOff = 0;
3262
Dan Gohman8181bd12008-07-27 21:46:04 +00003263 SmallVector<SDValue, 8> LoadValues;
3264 SmallVector<SDValue, 8> LoadChains;
3265 SmallVector<SDValue, 8> OutChains;
Dan Gohman42d311c2008-05-29 19:42:22 +00003266 unsigned NumMemOps = MemOps.size();
3267 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersonac9de032009-08-10 22:56:29 +00003268 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003269 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003270 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003271
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003272 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman42d311c2008-05-29 19:42:22 +00003273 getMemBasePlusOffset(Src, SrcOff, DAG),
3274 SrcSV, SrcSVOff + SrcOff, false, Align);
3275 LoadValues.push_back(Value);
3276 LoadChains.push_back(Value.getValue(1));
3277 SrcOff += VTSize;
3278 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003279 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman42d311c2008-05-29 19:42:22 +00003280 &LoadChains[0], LoadChains.size());
3281 OutChains.clear();
3282 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersonac9de032009-08-10 22:56:29 +00003283 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003284 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman8181bd12008-07-27 21:46:04 +00003285 SDValue Value, Store;
Dan Gohman42d311c2008-05-29 19:42:22 +00003286
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003287 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman42d311c2008-05-29 19:42:22 +00003288 getMemBasePlusOffset(Dst, DstOff, DAG),
3289 DstSV, DstSVOff + DstOff, false, DstAlign);
3290 OutChains.push_back(Store);
3291 DstOff += VTSize;
3292 }
3293
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003294 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman42d311c2008-05-29 19:42:22 +00003295 &OutChains[0], OutChains.size());
3296}
3297
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003298static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue Chain, SDValue Dst,
3300 SDValue Src, uint64_t Size,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003301 unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00003302 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003303 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3304
3305 // Expand memset to a series of load/store ops if the size operand
3306 // falls below a certain threshold.
Owen Andersonac9de032009-08-10 22:56:29 +00003307 std::vector<EVT> MemOps;
Evan Cheng833501d2008-06-30 07:31:25 +00003308 std::string Str;
3309 bool CopyFromStr;
Evan Cheng8c590372008-05-15 08:39:06 +00003310 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng833501d2008-06-30 07:31:25 +00003311 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00003312 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00003313
Dan Gohman8181bd12008-07-27 21:46:04 +00003314 SmallVector<SDValue, 8> OutChains;
Dan Gohman65118f42008-04-28 17:15:20 +00003315 uint64_t DstOff = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003316
3317 unsigned NumMemOps = MemOps.size();
3318 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersonac9de032009-08-10 22:56:29 +00003319 EVT VT = MemOps[i];
Duncan Sands92c43912008-06-06 12:08:01 +00003320 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003321 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3322 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner23d3bdf2008-10-28 07:10:51 +00003323 getMemBasePlusOffset(Dst, DstOff, DAG),
3324 DstSV, DstSVOff + DstOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003325 OutChains.push_back(Store);
3326 DstOff += VTSize;
3327 }
3328
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003329 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003330 &OutChains[0], OutChains.size());
3331}
3332
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003333SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003334 SDValue Src, SDValue Size,
3335 unsigned Align, bool AlwaysInline,
3336 const Value *DstSV, uint64_t DstSVOff,
3337 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003338
3339 // Check to see if we should lower the memcpy to loads and stores first.
3340 // For cases within the target-specified limits, this is the best choice.
3341 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3342 if (ConstantSize) {
3343 // Memcpy with size zero? Just return the original chain.
3344 if (ConstantSize->isNullValue())
3345 return Chain;
3346
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003348 getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003349 ConstantSize->getZExtValue(),
Dan Gohman65118f42008-04-28 17:15:20 +00003350 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003351 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003352 return Result;
3353 }
3354
3355 // Then check to see if we should lower the memcpy with target-specific
3356 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003357 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003358 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00003359 AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00003360 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003361 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003362 return Result;
3363
3364 // If we really need inline code and the target declined to provide it,
3365 // use a (potentially long) sequence of loads and stores.
3366 if (AlwaysInline) {
3367 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003368 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003369 ConstantSize->getZExtValue(), Align, true,
Dan Gohman65118f42008-04-28 17:15:20 +00003370 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003371 }
3372
3373 // Emit a library call.
3374 TargetLowering::ArgListTy Args;
3375 TargetLowering::ArgListEntry Entry;
Owen Anderson35b47072009-08-13 21:58:54 +00003376 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohmane8b391e2008-04-12 04:36:06 +00003377 Entry.Node = Dst; Args.push_back(Entry);
3378 Entry.Node = Src; Args.push_back(Entry);
3379 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003380 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003381 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00003382 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +00003383 false, false, false, false, 0,
3384 TLI.getLibcallCallingConv(RTLIB::MEMCPY), false,
Dan Gohman9178de12009-08-05 01:29:28 +00003385 /*isReturnValueUsed=*/false,
Eric Christopher440d9eb2009-08-22 00:40:45 +00003386 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaf4e64d62009-07-30 09:12:56 +00003387 TLI.getPointerTy()),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003388 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003389 return CallResult.second;
3390}
3391
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003392SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SDValue Src, SDValue Size,
3394 unsigned Align,
3395 const Value *DstSV, uint64_t DstSVOff,
3396 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003397
Dan Gohman42d311c2008-05-29 19:42:22 +00003398 // Check to see if we should lower the memmove to loads and stores first.
3399 // For cases within the target-specified limits, this is the best choice.
3400 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3401 if (ConstantSize) {
3402 // Memmove with size zero? Just return the original chain.
3403 if (ConstantSize->isNullValue())
3404 return Chain;
3405
Dan Gohman8181bd12008-07-27 21:46:04 +00003406 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003407 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003408 ConstantSize->getZExtValue(),
Dan Gohman42d311c2008-05-29 19:42:22 +00003409 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003410 if (Result.getNode())
Dan Gohman42d311c2008-05-29 19:42:22 +00003411 return Result;
3412 }
Dan Gohmane8b391e2008-04-12 04:36:06 +00003413
3414 // Then check to see if we should lower the memmove with target-specific
3415 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003416 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003417 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman65118f42008-04-28 17:15:20 +00003418 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003419 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003420 return Result;
3421
3422 // Emit a library call.
3423 TargetLowering::ArgListTy Args;
3424 TargetLowering::ArgListEntry Entry;
Owen Anderson35b47072009-08-13 21:58:54 +00003425 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohmane8b391e2008-04-12 04:36:06 +00003426 Entry.Node = Dst; Args.push_back(Entry);
3427 Entry.Node = Src; Args.push_back(Entry);
3428 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003429 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003430 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00003431 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +00003432 false, false, false, false, 0,
3433 TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false,
Dan Gohman9178de12009-08-05 01:29:28 +00003434 /*isReturnValueUsed=*/false,
Eric Christopher440d9eb2009-08-22 00:40:45 +00003435 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaf4e64d62009-07-30 09:12:56 +00003436 TLI.getPointerTy()),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003437 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003438 return CallResult.second;
3439}
3440
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003441SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman8181bd12008-07-27 21:46:04 +00003442 SDValue Src, SDValue Size,
3443 unsigned Align,
3444 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00003445
3446 // Check to see if we should lower the memset to stores first.
3447 // For cases within the target-specified limits, this is the best choice.
3448 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3449 if (ConstantSize) {
3450 // Memset with size zero? Just return the original chain.
3451 if (ConstantSize->isNullValue())
3452 return Chain;
3453
Dan Gohman8181bd12008-07-27 21:46:04 +00003454 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003455 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003456 Align, DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003457 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003458 return Result;
3459 }
3460
3461 // Then check to see if we should lower the memset with target-specific
3462 // code. If the target chooses to do this, this is the next best.
Dan Gohman8181bd12008-07-27 21:46:04 +00003463 SDValue Result =
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003464 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align,
Bill Wendling4b2e3782008-10-01 00:59:58 +00003465 DstSV, DstSVOff);
Gabor Greif1c80d112008-08-28 21:40:38 +00003466 if (Result.getNode())
Dan Gohmane8b391e2008-04-12 04:36:06 +00003467 return Result;
3468
3469 // Emit a library call.
Owen Anderson35b47072009-08-13 21:58:54 +00003470 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohmane8b391e2008-04-12 04:36:06 +00003471 TargetLowering::ArgListTy Args;
3472 TargetLowering::ArgListEntry Entry;
3473 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3474 Args.push_back(Entry);
3475 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003476 if (Src.getValueType().bitsGT(MVT::i32))
3477 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003478 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003479 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson35b47072009-08-13 21:58:54 +00003480 Entry.Node = Src;
3481 Entry.Ty = Type::getInt32Ty(*getContext());
3482 Entry.isSExt = true;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003483 Args.push_back(Entry);
Owen Anderson35b47072009-08-13 21:58:54 +00003484 Entry.Node = Size;
3485 Entry.Ty = IntPtrTy;
3486 Entry.isSExt = false;
Dan Gohmane8b391e2008-04-12 04:36:06 +00003487 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00003488 // FIXME: pass in DebugLoc
Dan Gohman8181bd12008-07-27 21:46:04 +00003489 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00003490 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikovcdab71f2009-08-14 20:10:52 +00003491 false, false, false, false, 0,
3492 TLI.getLibcallCallingConv(RTLIB::MEMSET), false,
Dan Gohman9178de12009-08-05 01:29:28 +00003493 /*isReturnValueUsed=*/false,
Eric Christopher440d9eb2009-08-22 00:40:45 +00003494 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaf4e64d62009-07-30 09:12:56 +00003495 TLI.getPointerTy()),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003496 Args, *this, dl);
Dan Gohmane8b391e2008-04-12 04:36:06 +00003497 return CallResult.second;
Rafael Espindola80825902007-10-19 10:41:11 +00003498}
3499
Owen Andersonac9de032009-08-10 22:56:29 +00003500SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003501 SDValue Chain,
Scott Michel91099d62009-02-17 22:15:04 +00003502 SDValue Ptr, SDValue Cmp,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003503 SDValue Swp, const Value* PtrVal,
3504 unsigned Alignment) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003505 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3506 Alignment = getEVTAlignment(MemVT);
3507
3508 // Check if the memory reference references a frame index
3509 if (!PtrVal)
3510 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003511 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3512 PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003513
Evan Cheng174e2cf2009-10-18 18:16:27 +00003514 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003515 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3516
3517 // For now, atomics are considered to be volatile always.
3518 Flags |= MachineMemOperand::MOVolatile;
3519
3520 MachineMemOperand *MMO =
3521 MF.getMachineMemOperand(PtrVal, Flags, 0,
3522 MemVT.getStoreSize(), Alignment);
3523
3524 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO);
3525}
3526
3527SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3528 SDValue Chain,
3529 SDValue Ptr, SDValue Cmp,
3530 SDValue Swp, MachineMemOperand *MMO) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003531 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3532 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3533
Owen Andersonac9de032009-08-10 22:56:29 +00003534 EVT VT = Cmp.getValueType();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003535
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003536 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003537 FoldingSetNodeID ID;
Dan Gohman9d0f5022009-02-03 00:08:45 +00003538 ID.AddInteger(MemVT.getRawBits());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003539 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3540 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3541 void* IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003542 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3543 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003544 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003545 }
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003546 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003547 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, Ptr, Cmp, Swp, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003548 CSEMap.InsertNode(N, IP);
3549 AllNodes.push_back(N);
3550 return SDValue(N, 0);
3551}
3552
Owen Andersonac9de032009-08-10 22:56:29 +00003553SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003554 SDValue Chain,
Scott Michel91099d62009-02-17 22:15:04 +00003555 SDValue Ptr, SDValue Val,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003556 const Value* PtrVal,
3557 unsigned Alignment) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003558 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3559 Alignment = getEVTAlignment(MemVT);
3560
3561 // Check if the memory reference references a frame index
3562 if (!PtrVal)
3563 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003564 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3565 PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003566
Evan Cheng174e2cf2009-10-18 18:16:27 +00003567 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003568 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3569
3570 // For now, atomics are considered to be volatile always.
3571 Flags |= MachineMemOperand::MOVolatile;
3572
3573 MachineMemOperand *MMO =
3574 MF.getMachineMemOperand(PtrVal, Flags, 0,
3575 MemVT.getStoreSize(), Alignment);
3576
3577 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO);
3578}
3579
3580SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3581 SDValue Chain,
3582 SDValue Ptr, SDValue Val,
3583 MachineMemOperand *MMO) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003584 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3585 Opcode == ISD::ATOMIC_LOAD_SUB ||
3586 Opcode == ISD::ATOMIC_LOAD_AND ||
3587 Opcode == ISD::ATOMIC_LOAD_OR ||
3588 Opcode == ISD::ATOMIC_LOAD_XOR ||
3589 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michel91099d62009-02-17 22:15:04 +00003590 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003591 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michel91099d62009-02-17 22:15:04 +00003592 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003593 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3594 Opcode == ISD::ATOMIC_SWAP) &&
3595 "Invalid Atomic Op");
3596
Owen Andersonac9de032009-08-10 22:56:29 +00003597 EVT VT = Val.getValueType();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003598
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003599 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003600 FoldingSetNodeID ID;
Dan Gohman9d0f5022009-02-03 00:08:45 +00003601 ID.AddInteger(MemVT.getRawBits());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003602 SDValue Ops[] = {Chain, Ptr, Val};
3603 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3604 void* IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003605 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3606 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003607 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003608 }
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003609 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003610 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, Ptr, Val, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003611 CSEMap.InsertNode(N, IP);
3612 AllNodes.push_back(N);
3613 return SDValue(N, 0);
3614}
3615
Duncan Sands698842f2008-07-02 17:40:58 +00003616/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3617/// Allowed to return something different (and simpler) if Simplify is true.
Dale Johannesenf3b91132009-02-02 20:47:48 +00003618SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3619 DebugLoc dl) {
3620 if (NumOps == 1)
3621 return Ops[0];
3622
Owen Andersonac9de032009-08-10 22:56:29 +00003623 SmallVector<EVT, 4> VTs;
Dale Johannesenf3b91132009-02-02 20:47:48 +00003624 VTs.reserve(NumOps);
3625 for (unsigned i = 0; i < NumOps; ++i)
3626 VTs.push_back(Ops[i].getValueType());
Scott Michel91099d62009-02-17 22:15:04 +00003627 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesenf3b91132009-02-02 20:47:48 +00003628 Ops, NumOps);
3629}
3630
Dan Gohman8181bd12008-07-27 21:46:04 +00003631SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003632SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersonac9de032009-08-10 22:56:29 +00003633 const EVT *VTs, unsigned NumVTs,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003634 const SDValue *Ops, unsigned NumOps,
Owen Andersonac9de032009-08-10 22:56:29 +00003635 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003636 unsigned Align, bool Vol,
3637 bool ReadMem, bool WriteMem) {
3638 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3639 MemVT, srcValue, SVOff, Align, Vol,
3640 ReadMem, WriteMem);
3641}
3642
3643SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003644SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3645 const SDValue *Ops, unsigned NumOps,
Owen Andersonac9de032009-08-10 22:56:29 +00003646 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003647 unsigned Align, bool Vol,
3648 bool ReadMem, bool WriteMem) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003649 if (Align == 0) // Ensure that codegen never sees alignment 0
3650 Align = getEVTAlignment(MemVT);
3651
3652 MachineFunction &MF = getMachineFunction();
3653 unsigned Flags = 0;
3654 if (WriteMem)
3655 Flags |= MachineMemOperand::MOStore;
3656 if (ReadMem)
3657 Flags |= MachineMemOperand::MOLoad;
3658 if (Vol)
3659 Flags |= MachineMemOperand::MOVolatile;
3660 MachineMemOperand *MMO =
3661 MF.getMachineMemOperand(srcValue, Flags, SVOff,
3662 MemVT.getStoreSize(), Align);
3663
3664 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
3665}
3666
3667SDValue
3668SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3669 const SDValue *Ops, unsigned NumOps,
3670 EVT MemVT, MachineMemOperand *MMO) {
3671 assert((Opcode == ISD::INTRINSIC_VOID ||
3672 Opcode == ISD::INTRINSIC_W_CHAIN ||
3673 (Opcode <= INT_MAX &&
3674 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
3675 "Opcode is not a memory-accessing opcode!");
3676
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003677 // Memoize the node unless it returns a flag.
3678 MemIntrinsicSDNode *N;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003679 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003680 FoldingSetNodeID ID;
3681 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3682 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003683 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3684 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003685 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003686 }
Scott Michel91099d62009-02-17 22:15:04 +00003687
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003688 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003689 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003690 CSEMap.InsertNode(N, IP);
3691 } else {
3692 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003693 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003694 }
3695 AllNodes.push_back(N);
3696 return SDValue(N, 0);
3697}
3698
3699SDValue
Scott Michel91099d62009-02-17 22:15:04 +00003700SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
Owen Andersonac9de032009-08-10 22:56:29 +00003701 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003702 SDValue Ptr, SDValue Offset,
Dan Gohman3bab1f72009-09-23 21:02:20 +00003703 const Value *SV, int SVOffset, EVT MemVT,
Nate Begeman24947772009-09-15 19:05:41 +00003704 bool isVolatile, unsigned Alignment) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003705 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersonac9de032009-08-10 22:56:29 +00003706 Alignment = getEVTAlignment(VT);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003707
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003708 // Check if the memory reference references a frame index
3709 if (!SV)
3710 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003711 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3712 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003713
Evan Cheng174e2cf2009-10-18 18:16:27 +00003714 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003715 unsigned Flags = MachineMemOperand::MOLoad;
3716 if (isVolatile)
3717 Flags |= MachineMemOperand::MOVolatile;
3718 MachineMemOperand *MMO =
3719 MF.getMachineMemOperand(SV, Flags, SVOffset,
3720 MemVT.getStoreSize(), Alignment);
3721 return getLoad(AM, dl, ExtType, VT, Chain, Ptr, Offset, MemVT, MMO);
3722}
3723
3724SDValue
3725SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3726 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
3727 SDValue Ptr, SDValue Offset, EVT MemVT,
3728 MachineMemOperand *MMO) {
Dan Gohman3bab1f72009-09-23 21:02:20 +00003729 if (VT == MemVT) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003730 ExtType = ISD::NON_EXTLOAD;
3731 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman3bab1f72009-09-23 21:02:20 +00003732 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003733 } else {
3734 // Extending load.
3735 if (VT.isVector())
Dan Gohman3bab1f72009-09-23 21:02:20 +00003736 assert(MemVT.getVectorNumElements() == VT.getVectorNumElements() &&
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003737 "Invalid vector extload!");
3738 else
Dan Gohman3bab1f72009-09-23 21:02:20 +00003739 assert(MemVT.bitsLT(VT) &&
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003740 "Should only be an extending load, not truncating!");
3741 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3742 "Cannot sign/zero extend a FP/Vector load!");
Dan Gohman3bab1f72009-09-23 21:02:20 +00003743 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003744 "Cannot convert from FP to Int or Int -> FP!");
3745 }
3746
3747 bool Indexed = AM != ISD::UNINDEXED;
3748 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3749 "Unindexed load with an offset!");
3750
3751 SDVTList VTs = Indexed ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003752 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003753 SDValue Ops[] = { Chain, Ptr, Offset };
3754 FoldingSetNodeID ID;
3755 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003756 ID.AddInteger(MemVT.getRawBits());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003757 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile()));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003758 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003759 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3760 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003761 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003762 }
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003763 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003764 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, MemVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003765 CSEMap.InsertNode(N, IP);
3766 AllNodes.push_back(N);
3767 return SDValue(N, 0);
3768}
3769
Owen Andersonac9de032009-08-10 22:56:29 +00003770SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003771 SDValue Chain, SDValue Ptr,
3772 const Value *SV, int SVOffset,
Nate Begeman24947772009-09-15 19:05:41 +00003773 bool isVolatile, unsigned Alignment) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003774 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003775 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
Nate Begeman24947772009-09-15 19:05:41 +00003776 SV, SVOffset, VT, isVolatile, Alignment);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003777}
3778
Owen Andersonac9de032009-08-10 22:56:29 +00003779SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003780 SDValue Chain, SDValue Ptr,
3781 const Value *SV,
Dan Gohman3bab1f72009-09-23 21:02:20 +00003782 int SVOffset, EVT MemVT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003783 bool isVolatile, unsigned Alignment) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003784 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003785 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
Dan Gohman3bab1f72009-09-23 21:02:20 +00003786 SV, SVOffset, MemVT, isVolatile, Alignment);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003787}
3788
Dan Gohman8181bd12008-07-27 21:46:04 +00003789SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003790SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3791 SDValue Offset, ISD::MemIndexedMode AM) {
3792 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3793 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3794 "Load is already a indexed load!");
3795 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3796 LD->getChain(), Base, Offset, LD->getSrcValue(),
3797 LD->getSrcValueOffset(), LD->getMemoryVT(),
3798 LD->isVolatile(), LD->getAlignment());
3799}
3800
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003801SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3802 SDValue Ptr, const Value *SV, int SVOffset,
Nate Begeman24947772009-09-15 19:05:41 +00003803 bool isVolatile, unsigned Alignment) {
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003804 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003805 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003806
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003807 // Check if the memory reference references a frame index
3808 if (!SV)
3809 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003810 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3811 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003812
Evan Cheng174e2cf2009-10-18 18:16:27 +00003813 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003814 unsigned Flags = MachineMemOperand::MOStore;
3815 if (isVolatile)
3816 Flags |= MachineMemOperand::MOVolatile;
3817 MachineMemOperand *MMO =
3818 MF.getMachineMemOperand(SV, Flags, SVOffset,
3819 Val.getValueType().getStoreSize(), Alignment);
3820
3821 return getStore(Chain, dl, Val, Ptr, MMO);
3822}
3823
3824SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3825 SDValue Ptr, MachineMemOperand *MMO) {
3826 EVT VT = Val.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003827 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003828 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003829 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3830 FoldingSetNodeID ID;
3831 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003832 ID.AddInteger(VT.getRawBits());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003833 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile()));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003834 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003835 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3836 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003837 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003838 }
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003839 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003840 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false, VT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003841 CSEMap.InsertNode(N, IP);
3842 AllNodes.push_back(N);
3843 return SDValue(N, 0);
3844}
3845
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003846SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3847 SDValue Ptr, const Value *SV,
Owen Andersonac9de032009-08-10 22:56:29 +00003848 int SVOffset, EVT SVT,
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003849 bool isVolatile, unsigned Alignment) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003850 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3851 Alignment = getEVTAlignment(SVT);
3852
3853 // Check if the memory reference references a frame index
3854 if (!SV)
3855 if (const FrameIndexSDNode *FI =
Evan Cheng174e2cf2009-10-18 18:16:27 +00003856 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3857 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003858
Evan Cheng174e2cf2009-10-18 18:16:27 +00003859 MachineFunction &MF = getMachineFunction();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003860 unsigned Flags = MachineMemOperand::MOStore;
3861 if (isVolatile)
3862 Flags |= MachineMemOperand::MOVolatile;
3863 MachineMemOperand *MMO =
3864 MF.getMachineMemOperand(SV, Flags, SVOffset, SVT.getStoreSize(), Alignment);
3865
3866 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
3867}
3868
3869SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3870 SDValue Ptr, EVT SVT,
3871 MachineMemOperand *MMO) {
Owen Andersonac9de032009-08-10 22:56:29 +00003872 EVT VT = Val.getValueType();
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003873
3874 if (VT == SVT)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003875 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003876
3877 assert(VT.bitsGT(SVT) && "Not a truncation?");
3878 assert(VT.isInteger() == SVT.isInteger() &&
3879 "Can't do FP-INT conversion!");
3880
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003881
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003882 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003883 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003884 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3885 FoldingSetNodeID ID;
3886 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003887 ID.AddInteger(SVT.getRawBits());
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003888 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile()));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003889 void *IP = 0;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003890 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3891 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003892 return SDValue(E, 0);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003893 }
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003894 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003895 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003896 CSEMap.InsertNode(N, IP);
3897 AllNodes.push_back(N);
3898 return SDValue(N, 0);
3899}
3900
Dan Gohman8181bd12008-07-27 21:46:04 +00003901SDValue
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003902SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3903 SDValue Offset, ISD::MemIndexedMode AM) {
3904 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3905 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3906 "Store is already a indexed store!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003907 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003908 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3909 FoldingSetNodeID ID;
3910 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003911 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohman9d0f5022009-02-03 00:08:45 +00003912 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003913 void *IP = 0;
3914 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3915 return SDValue(E, 0);
3916 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3917 new (N) StoreSDNode(Ops, dl, VTs, AM,
3918 ST->isTruncatingStore(), ST->getMemoryVT(),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00003919 ST->getMemOperand());
Dale Johannesenba9d87f2009-01-29 00:47:48 +00003920 CSEMap.InsertNode(N, IP);
3921 AllNodes.push_back(N);
3922 return SDValue(N, 0);
3923}
3924
Owen Andersonac9de032009-08-10 22:56:29 +00003925SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003926 SDValue Chain, SDValue Ptr,
3927 SDValue SV) {
3928 SDValue Ops[] = { Chain, Ptr, SV };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003929 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00003930}
3931
Owen Andersonac9de032009-08-10 22:56:29 +00003932SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00003933 const SDUse *Ops, unsigned NumOps) {
Dan Gohman703ce5d2008-07-07 18:26:29 +00003934 switch (NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003935 case 0: return getNode(Opcode, DL, VT);
3936 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3937 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3938 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman703ce5d2008-07-07 18:26:29 +00003939 default: break;
3940 }
3941
Dan Gohman8181bd12008-07-27 21:46:04 +00003942 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman703ce5d2008-07-07 18:26:29 +00003943 // the regular getNode logic.
Dan Gohman8181bd12008-07-27 21:46:04 +00003944 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendlingdf4de112009-01-28 22:17:52 +00003945 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman703ce5d2008-07-07 18:26:29 +00003946}
3947
Owen Andersonac9de032009-08-10 22:56:29 +00003948SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendlingdf4de112009-01-28 22:17:52 +00003949 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950 switch (NumOps) {
Bill Wendlingdf4de112009-01-28 22:17:52 +00003951 case 0: return getNode(Opcode, DL, VT);
3952 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3953 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3954 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003955 default: break;
3956 }
Scott Michel91099d62009-02-17 22:15:04 +00003957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 switch (Opcode) {
3959 default: break;
3960 case ISD::SELECT_CC: {
3961 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
3962 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3963 "LHS and RHS of condition must have same type!");
3964 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3965 "True and False arms of SelectCC must have same type!");
3966 assert(Ops[2].getValueType() == VT &&
3967 "select_cc node must be of same type as true and false value!");
3968 break;
3969 }
3970 case ISD::BR_CC: {
3971 assert(NumOps == 5 && "BR_CC takes 5 operands!");
3972 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3973 "LHS/RHS of comparison should match types!");
3974 break;
3975 }
3976 }
3977
3978 // Memoize nodes.
3979 SDNode *N;
3980 SDVTList VTs = getVTList(VT);
Bill Wendlingdf4de112009-01-28 22:17:52 +00003981
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003982 if (VT != MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 FoldingSetNodeID ID;
3984 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
3985 void *IP = 0;
Bill Wendlingdf4de112009-01-28 22:17:52 +00003986
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00003988 return SDValue(E, 0);
Bill Wendlingdf4de112009-01-28 22:17:52 +00003989
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003990 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00003991 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992 CSEMap.InsertNode(N, IP);
3993 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00003994 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00003995 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 }
Bill Wendlingdf4de112009-01-28 22:17:52 +00003997
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003998 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003999#ifndef NDEBUG
4000 VerifyNode(N);
4001#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00004002 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003}
4004
Bill Wendlingdf4de112009-01-28 22:17:52 +00004005SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersonac9de032009-08-10 22:56:29 +00004006 const std::vector<EVT> &ResultTys,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004007 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004008 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004009 Ops, NumOps);
4010}
4011
Bill Wendlingdf4de112009-01-28 22:17:52 +00004012SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersonac9de032009-08-10 22:56:29 +00004013 const EVT *VTs, unsigned NumVTs,
Bill Wendlingdf4de112009-01-28 22:17:52 +00004014 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 if (NumVTs == 1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004016 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4017 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michel91099d62009-02-17 22:15:04 +00004018}
4019
Bill Wendlingdf4de112009-01-28 22:17:52 +00004020SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4021 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 if (VTList.NumVTs == 1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004023 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024
Daniel Dunbar43e3a622009-07-19 01:38:38 +00004025#if 0
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 switch (Opcode) {
4027 // FIXME: figure out how to safely handle things like
4028 // int foo(int x) { return 1 << (x & 255); }
4029 // int bar() { return foo(256); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 case ISD::SRA_PARTS:
4031 case ISD::SRL_PARTS:
4032 case ISD::SHL_PARTS:
4033 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004034 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004035 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 else if (N3.getOpcode() == ISD::AND)
4037 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4038 // If the and is only masking out bits that cannot effect the shift,
4039 // eliminate the and.
Duncan Sands92c43912008-06-06 12:08:01 +00004040 unsigned NumBits = VT.getSizeInBits()*2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendlingdf4de112009-01-28 22:17:52 +00004042 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 }
4044 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004045 }
Daniel Dunbar43e3a622009-07-19 01:38:38 +00004046#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004047
4048 // Memoize the node unless it returns a flag.
4049 SDNode *N;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004050 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004051 FoldingSetNodeID ID;
4052 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4053 void *IP = 0;
4054 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman8181bd12008-07-27 21:46:04 +00004055 return SDValue(E, 0);
Dan Gohmaned825d12008-07-07 23:02:41 +00004056 if (NumOps == 1) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004057 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004058 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004059 } else if (NumOps == 2) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004060 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004061 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004062 } else if (NumOps == 3) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004063 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004064 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004065 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004066 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004067 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohmaned825d12008-07-07 23:02:41 +00004068 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 CSEMap.InsertNode(N, IP);
4070 } else {
Dan Gohmaned825d12008-07-07 23:02:41 +00004071 if (NumOps == 1) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004072 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004073 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004074 } else if (NumOps == 2) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004075 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004076 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004077 } else if (NumOps == 3) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004078 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004079 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohmaned825d12008-07-07 23:02:41 +00004080 } else {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +00004081 N = NodeAllocator.Allocate<SDNode>();
Bill Wendlingdf4de112009-01-28 22:17:52 +00004082 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohmaned825d12008-07-07 23:02:41 +00004083 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084 }
4085 AllNodes.push_back(N);
Duncan Sandsd3ace282008-07-21 10:20:31 +00004086#ifndef NDEBUG
4087 VerifyNode(N);
4088#endif
Dan Gohman8181bd12008-07-27 21:46:04 +00004089 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090}
4091
Bill Wendlingdf4de112009-01-28 22:17:52 +00004092SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4093 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman798d1272007-10-08 15:49:58 +00004094}
4095
Bill Wendlingdf4de112009-01-28 22:17:52 +00004096SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4097 SDValue N1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004098 SDValue Ops[] = { N1 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004099 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman798d1272007-10-08 15:49:58 +00004100}
4101
Bill Wendlingdf4de112009-01-28 22:17:52 +00004102SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4103 SDValue N1, SDValue N2) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004104 SDValue Ops[] = { N1, N2 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004105 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman798d1272007-10-08 15:49:58 +00004106}
4107
Bill Wendlingdf4de112009-01-28 22:17:52 +00004108SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4109 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004110 SDValue Ops[] = { N1, N2, N3 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004111 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman798d1272007-10-08 15:49:58 +00004112}
4113
Bill Wendlingdf4de112009-01-28 22:17:52 +00004114SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4115 SDValue N1, SDValue N2, SDValue N3,
4116 SDValue N4) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004117 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004118 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman798d1272007-10-08 15:49:58 +00004119}
4120
Bill Wendlingdf4de112009-01-28 22:17:52 +00004121SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4122 SDValue N1, SDValue N2, SDValue N3,
4123 SDValue N4, SDValue N5) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004124 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendlingdf4de112009-01-28 22:17:52 +00004125 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman798d1272007-10-08 15:49:58 +00004126}
4127
Owen Andersonac9de032009-08-10 22:56:29 +00004128SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsa9810f32007-10-16 09:56:48 +00004129 return makeVTList(SDNode::getValueTypeList(VT), 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130}
4131
Owen Andersonac9de032009-08-10 22:56:29 +00004132SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004133 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4134 E = VTList.rend(); I != E; ++I)
4135 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4136 return *I;
4137
Owen Andersonac9de032009-08-10 22:56:29 +00004138 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004139 Array[0] = VT1;
4140 Array[1] = VT2;
4141 SDVTList Result = makeVTList(Array, 2);
4142 VTList.push_back(Result);
4143 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004144}
Dan Gohmanbd68c792008-07-17 19:10:17 +00004145
Owen Andersonac9de032009-08-10 22:56:29 +00004146SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004147 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4148 E = VTList.rend(); I != E; ++I)
4149 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4150 I->VTs[2] == VT3)
4151 return *I;
4152
Owen Andersonac9de032009-08-10 22:56:29 +00004153 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004154 Array[0] = VT1;
4155 Array[1] = VT2;
4156 Array[2] = VT3;
4157 SDVTList Result = makeVTList(Array, 3);
4158 VTList.push_back(Result);
4159 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160}
4161
Owen Andersonac9de032009-08-10 22:56:29 +00004162SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling7a4076f2008-12-01 23:28:22 +00004163 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4164 E = VTList.rend(); I != E; ++I)
4165 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4166 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4167 return *I;
4168
Owen Andersonac9de032009-08-10 22:56:29 +00004169 EVT *Array = Allocator.Allocate<EVT>(3);
Bill Wendling7a4076f2008-12-01 23:28:22 +00004170 Array[0] = VT1;
4171 Array[1] = VT2;
4172 Array[2] = VT3;
4173 Array[3] = VT4;
4174 SDVTList Result = makeVTList(Array, 4);
4175 VTList.push_back(Result);
4176 return Result;
4177}
4178
Owen Andersonac9de032009-08-10 22:56:29 +00004179SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 switch (NumVTs) {
Edwin Törökbd448e32009-07-14 16:55:14 +00004181 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182 case 1: return getVTList(VTs[0]);
4183 case 2: return getVTList(VTs[0], VTs[1]);
4184 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4185 default: break;
4186 }
4187
Dan Gohmanbd68c792008-07-17 19:10:17 +00004188 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4189 E = VTList.rend(); I != E; ++I) {
4190 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4191 continue;
Scott Michel91099d62009-02-17 22:15:04 +00004192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193 bool NoMatch = false;
4194 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmanbd68c792008-07-17 19:10:17 +00004195 if (VTs[i] != I->VTs[i]) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 NoMatch = true;
4197 break;
4198 }
4199 if (!NoMatch)
Dan Gohmanbd68c792008-07-17 19:10:17 +00004200 return *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201 }
Scott Michel91099d62009-02-17 22:15:04 +00004202
Owen Andersonac9de032009-08-10 22:56:29 +00004203 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004204 std::copy(VTs, VTs+NumVTs, Array);
4205 SDVTList Result = makeVTList(Array, NumVTs);
4206 VTList.push_back(Result);
4207 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208}
4209
4210
4211/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4212/// specified operands. If the resultant node already exists in the DAG,
4213/// this does not modify the specified node, instead it returns the node that
4214/// already exists. If the resultant node does not exist in the DAG, the
4215/// input node is returned. As a degenerate case, if you specify the same
4216/// input operands as the node already has, the input node is returned.
Dan Gohman8181bd12008-07-27 21:46:04 +00004217SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004218 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michel91099d62009-02-17 22:15:04 +00004220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004221 // Check to see if there is no change.
4222 if (Op == N->getOperand(0)) return InN;
Scott Michel91099d62009-02-17 22:15:04 +00004223
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224 // See if the modified node already exists.
4225 void *InsertPos = 0;
4226 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004227 return SDValue(Existing, InN.getResNo());
Scott Michel91099d62009-02-17 22:15:04 +00004228
Dan Gohman7d919ea2008-07-21 22:38:59 +00004229 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004231 if (!RemoveNodeFromCSEMaps(N))
4232 InsertPos = 0;
Scott Michel91099d62009-02-17 22:15:04 +00004233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004235 N->OperandList[0].set(Op);
Scott Michel91099d62009-02-17 22:15:04 +00004236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 // If this gets put into a CSE map, add it.
4238 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4239 return InN;
4240}
4241
Dan Gohman8181bd12008-07-27 21:46:04 +00004242SDValue SelectionDAG::
4243UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004244 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michel91099d62009-02-17 22:15:04 +00004246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 // Check to see if there is no change.
4248 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4249 return InN; // No operands changed, just return the input node.
Scott Michel91099d62009-02-17 22:15:04 +00004250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251 // See if the modified node already exists.
4252 void *InsertPos = 0;
4253 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004254 return SDValue(Existing, InN.getResNo());
Scott Michel91099d62009-02-17 22:15:04 +00004255
Dan Gohman7d919ea2008-07-21 22:38:59 +00004256 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004258 if (!RemoveNodeFromCSEMaps(N))
4259 InsertPos = 0;
Scott Michel91099d62009-02-17 22:15:04 +00004260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004262 if (N->OperandList[0] != Op1)
4263 N->OperandList[0].set(Op1);
4264 if (N->OperandList[1] != Op2)
4265 N->OperandList[1].set(Op2);
Scott Michel91099d62009-02-17 22:15:04 +00004266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004267 // If this gets put into a CSE map, add it.
4268 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4269 return InN;
4270}
4271
Dan Gohman8181bd12008-07-27 21:46:04 +00004272SDValue SelectionDAG::
4273UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4274 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275 return UpdateNodeOperands(N, Ops, 3);
4276}
4277
Dan Gohman8181bd12008-07-27 21:46:04 +00004278SDValue SelectionDAG::
Scott Michel91099d62009-02-17 22:15:04 +00004279UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004280 SDValue Op3, SDValue Op4) {
4281 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282 return UpdateNodeOperands(N, Ops, 4);
4283}
4284
Dan Gohman8181bd12008-07-27 21:46:04 +00004285SDValue SelectionDAG::
4286UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4287 SDValue Op3, SDValue Op4, SDValue Op5) {
4288 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 return UpdateNodeOperands(N, Ops, 5);
4290}
4291
Dan Gohman8181bd12008-07-27 21:46:04 +00004292SDValue SelectionDAG::
4293UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004294 SDNode *N = InN.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295 assert(N->getNumOperands() == NumOps &&
4296 "Update with wrong number of operands");
Scott Michel91099d62009-02-17 22:15:04 +00004297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298 // Check to see if there is no change.
4299 bool AnyChange = false;
4300 for (unsigned i = 0; i != NumOps; ++i) {
4301 if (Ops[i] != N->getOperand(i)) {
4302 AnyChange = true;
4303 break;
4304 }
4305 }
Scott Michel91099d62009-02-17 22:15:04 +00004306
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307 // No operands changed, just return the input node.
4308 if (!AnyChange) return InN;
Scott Michel91099d62009-02-17 22:15:04 +00004309
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310 // See if the modified node already exists.
4311 void *InsertPos = 0;
4312 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif46bf5472008-08-26 22:36:50 +00004313 return SDValue(Existing, InN.getResNo());
Scott Michel91099d62009-02-17 22:15:04 +00004314
Dan Gohman14a027d2008-05-02 00:05:03 +00004315 // Nope it doesn't. Remove the node from its current place in the maps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 if (InsertPos)
Dan Gohman705e3f72008-09-13 01:54:27 +00004317 if (!RemoveNodeFromCSEMaps(N))
4318 InsertPos = 0;
Scott Michel91099d62009-02-17 22:15:04 +00004319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 // Now we update the operands.
djgc2517d32009-01-26 04:35:06 +00004321 for (unsigned i = 0; i != NumOps; ++i)
4322 if (N->OperandList[i] != Ops[i])
4323 N->OperandList[i].set(Ops[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004324
4325 // If this gets put into a CSE map, add it.
4326 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
4327 return InN;
4328}
4329
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004330/// DropOperands - Release the operands and set this node to have
Dan Gohmanbd68c792008-07-17 19:10:17 +00004331/// zero operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004332void SDNode::DropOperands() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004333 // Unlike the code in MorphNodeTo that does this, we don't need to
4334 // watch for dead nodes here.
djgc2517d32009-01-26 04:35:06 +00004335 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4336 SDUse &Use = *I++;
4337 Use.set(SDValue());
4338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339}
4340
Dan Gohmanbd68c792008-07-17 19:10:17 +00004341/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4342/// machine opcode.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343///
Dan Gohmanbd68c792008-07-17 19:10:17 +00004344SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004345 EVT VT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004347 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348}
4349
Dan Gohmanbd68c792008-07-17 19:10:17 +00004350SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004351 EVT VT, SDValue Op1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004353 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004354 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355}
4356
Dan Gohmanbd68c792008-07-17 19:10:17 +00004357SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004358 EVT VT, SDValue Op1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004359 SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004361 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004362 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363}
4364
Dan Gohmanbd68c792008-07-17 19:10:17 +00004365SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004366 EVT VT, SDValue Op1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004367 SDValue Op2, SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004368 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004369 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004370 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371}
4372
Dan Gohmanbd68c792008-07-17 19:10:17 +00004373SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004374 EVT VT, const SDValue *Ops,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375 unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 SDVTList VTs = getVTList(VT);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004377 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004378}
4379
Dan Gohmanbd68c792008-07-17 19:10:17 +00004380SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004381 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004382 unsigned NumOps) {
4383 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004384 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004385}
4386
Dan Gohmanbd68c792008-07-17 19:10:17 +00004387SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004388 EVT VT1, EVT VT2) {
Dan Gohman7eced112008-07-02 23:23:19 +00004389 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004390 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohman7eced112008-07-02 23:23:19 +00004391}
4392
Dan Gohmanbd68c792008-07-17 19:10:17 +00004393SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004394 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004395 const SDValue *Ops, unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004396 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004397 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohman7eced112008-07-02 23:23:19 +00004398}
4399
Bill Wendling7a4076f2008-12-01 23:28:22 +00004400SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004401 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling7a4076f2008-12-01 23:28:22 +00004402 const SDValue *Ops, unsigned NumOps) {
4403 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4404 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4405}
4406
Scott Michel91099d62009-02-17 22:15:04 +00004407SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004408 EVT VT1, EVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004409 SDValue Op1) {
Dan Gohman7eced112008-07-02 23:23:19 +00004410 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004411 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004412 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413}
4414
Scott Michel91099d62009-02-17 22:15:04 +00004415SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004416 EVT VT1, EVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004417 SDValue Op1, SDValue Op2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004419 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004420 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421}
4422
Dan Gohmanbd68c792008-07-17 19:10:17 +00004423SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004424 EVT VT1, EVT VT2,
Scott Michel91099d62009-02-17 22:15:04 +00004425 SDValue Op1, SDValue Op2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004426 SDValue Op3) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004428 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004429 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohman7eced112008-07-02 23:23:19 +00004430}
4431
Dan Gohmanbd68c792008-07-17 19:10:17 +00004432SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersonac9de032009-08-10 22:56:29 +00004433 EVT VT1, EVT VT2, EVT VT3,
Scott Michel91099d62009-02-17 22:15:04 +00004434 SDValue Op1, SDValue Op2,
Bill Wendling7a4076f2008-12-01 23:28:22 +00004435 SDValue Op3) {
4436 SDVTList VTs = getVTList(VT1, VT2, VT3);
4437 SDValue Ops[] = { Op1, Op2, Op3 };
4438 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4439}
4440
4441SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004442 SDVTList VTs, const SDValue *Ops,
Dan Gohman7eced112008-07-02 23:23:19 +00004443 unsigned NumOps) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004444 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4445}
4446
4447SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004448 EVT VT) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004449 SDVTList VTs = getVTList(VT);
4450 return MorphNodeTo(N, Opc, VTs, 0, 0);
4451}
4452
4453SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004454 EVT VT, SDValue Op1) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004455 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004456 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004457 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4458}
4459
4460SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004461 EVT VT, SDValue Op1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 SDValue Op2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004463 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004464 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004465 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4466}
4467
4468SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004469 EVT VT, SDValue Op1,
Dan Gohman8181bd12008-07-27 21:46:04 +00004470 SDValue Op2, SDValue Op3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004471 SDVTList VTs = getVTList(VT);
Dan Gohman8181bd12008-07-27 21:46:04 +00004472 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004473 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4474}
4475
4476SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004477 EVT VT, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004478 unsigned NumOps) {
4479 SDVTList VTs = getVTList(VT);
4480 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4481}
4482
4483SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004484 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004485 unsigned NumOps) {
4486 SDVTList VTs = getVTList(VT1, VT2);
4487 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4488}
4489
4490SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004491 EVT VT1, EVT VT2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004492 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004494}
4495
4496SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004497 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman8181bd12008-07-27 21:46:04 +00004498 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004499 SDVTList VTs = getVTList(VT1, VT2, VT3);
4500 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4501}
4502
Scott Michel91099d62009-02-17 22:15:04 +00004503SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004504 EVT VT1, EVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004505 SDValue Op1) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004506 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004507 SDValue Ops[] = { Op1 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004508 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4509}
4510
Scott Michel91099d62009-02-17 22:15:04 +00004511SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004512 EVT VT1, EVT VT2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004513 SDValue Op1, SDValue Op2) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004514 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004515 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004516 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4517}
4518
4519SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersonac9de032009-08-10 22:56:29 +00004520 EVT VT1, EVT VT2,
Scott Michel91099d62009-02-17 22:15:04 +00004521 SDValue Op1, SDValue Op2,
Dan Gohman8181bd12008-07-27 21:46:04 +00004522 SDValue Op3) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004523 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004524 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanbd68c792008-07-17 19:10:17 +00004525 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4526}
4527
4528/// MorphNodeTo - These *mutate* the specified node to have the specified
4529/// return type, opcode, and operands.
4530///
4531/// Note that MorphNodeTo returns the resultant node. If there is already a
4532/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesenb03cc3f2009-02-04 23:02:30 +00004533/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmanbd68c792008-07-17 19:10:17 +00004534///
4535/// Using MorphNodeTo is faster than creating a new node and swapping it in
4536/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greif8b2235c2008-08-30 22:16:05 +00004537/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmanbd68c792008-07-17 19:10:17 +00004538/// the node's users.
4539///
4540SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman8181bd12008-07-27 21:46:04 +00004541 SDVTList VTs, const SDValue *Ops,
Dan Gohmanbd68c792008-07-17 19:10:17 +00004542 unsigned NumOps) {
Dan Gohman7eced112008-07-02 23:23:19 +00004543 // If an identical node already exists, use it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 void *IP = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004545 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00004546 FoldingSetNodeID ID;
4547 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4548 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4549 return ON;
4550 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551
Dan Gohman705e3f72008-09-13 01:54:27 +00004552 if (!RemoveNodeFromCSEMaps(N))
4553 IP = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554
Dan Gohmanbd68c792008-07-17 19:10:17 +00004555 // Start the morphing.
4556 N->NodeType = Opc;
4557 N->ValueList = VTs.VTs;
4558 N->NumValues = VTs.NumVTs;
Scott Michel91099d62009-02-17 22:15:04 +00004559
Dan Gohmanbd68c792008-07-17 19:10:17 +00004560 // Clear the operands list, updating used nodes to remove this from their
4561 // use list. Keep track of any operands that become dead as a result.
4562 SmallPtrSet<SDNode*, 16> DeadNodeSet;
djgc2517d32009-01-26 04:35:06 +00004563 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4564 SDUse &Use = *I++;
4565 SDNode *Used = Use.getNode();
4566 Use.set(SDValue());
Dan Gohmanbd68c792008-07-17 19:10:17 +00004567 if (Used->use_empty())
4568 DeadNodeSet.insert(Used);
4569 }
4570
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004571 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
4572 // Initialize the memory references information.
4573 MN->setMemRefs(0, 0);
4574 // If NumOps is larger than the # of operands we can have in a
4575 // MachineSDNode, reallocate the operand list.
4576 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
4577 if (MN->OperandsNeedDelete)
4578 delete[] MN->OperandList;
4579 if (NumOps > array_lengthof(MN->LocalOperands))
4580 // We're creating a final node that will live unmorphed for the
4581 // remainder of the current SelectionDAG iteration, so we can allocate
4582 // the operands directly out of a pool with no recycling metadata.
4583 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
4584 Ops, NumOps);
4585 else
4586 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
4587 MN->OperandsNeedDelete = false;
4588 } else
4589 MN->InitOperands(MN->OperandList, Ops, NumOps);
4590 } else {
4591 // If NumOps is larger than the # of operands we currently have, reallocate
4592 // the operand list.
4593 if (NumOps > N->NumOperands) {
4594 if (N->OperandsNeedDelete)
4595 delete[] N->OperandList;
4596 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004597 N->OperandsNeedDelete = true;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004598 } else
Anton Korobeynikovfcfa3112009-10-22 00:15:17 +00004599 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004600 }
4601
4602 // Delete any nodes that are still dead after adding the uses for the
4603 // new operands.
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004604 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmanbd68c792008-07-17 19:10:17 +00004605 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4606 E = DeadNodeSet.end(); I != E; ++I)
4607 if ((*I)->use_empty())
4608 DeadNodes.push_back(*I);
Dan Gohmanb997a4e2008-07-07 20:57:48 +00004609 RemoveDeadNodes(DeadNodes);
4610
Dan Gohmanbd68c792008-07-17 19:10:17 +00004611 if (IP)
4612 CSEMap.InsertNode(N, IP); // Memoize the new node.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613 return N;
4614}
4615
4616
Dan Gohman61fda0d2009-09-25 18:54:59 +00004617/// getMachineNode - These are used for target selectors to create a new node
4618/// with specified return type(s), MachineInstr opcode, and operands.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619///
Dan Gohman61fda0d2009-09-25 18:54:59 +00004620/// Note that getMachineNode returns the resultant node. If there is already a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621/// node of the specified opcode and operands, it returns that node instead of
4622/// the current one.
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004623MachineSDNode *
4624SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004625 SDVTList VTs = getVTList(VT);
4626 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004627}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004628
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004629MachineSDNode *
4630SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004631 SDVTList VTs = getVTList(VT);
4632 SDValue Ops[] = { Op1 };
4633 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004634}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004635
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004636MachineSDNode *
4637SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4638 SDValue Op1, SDValue Op2) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004639 SDVTList VTs = getVTList(VT);
4640 SDValue Ops[] = { Op1, Op2 };
4641 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004642}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004643
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004644MachineSDNode *
4645SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4646 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004647 SDVTList VTs = getVTList(VT);
4648 SDValue Ops[] = { Op1, Op2, Op3 };
4649 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004650}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004651
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004652MachineSDNode *
4653SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4654 const SDValue *Ops, unsigned NumOps) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004655 SDVTList VTs = getVTList(VT);
4656 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004657}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004658
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004659MachineSDNode *
4660SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanee036282009-04-09 23:54:40 +00004661 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004662 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004663}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004664
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004665MachineSDNode *
4666SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4667 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanee036282009-04-09 23:54:40 +00004668 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004669 SDValue Ops[] = { Op1 };
4670 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004671}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004672
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004673MachineSDNode *
4674SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4675 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanee036282009-04-09 23:54:40 +00004676 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004677 SDValue Ops[] = { Op1, Op2 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004678 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling6ed1f082009-01-29 09:01:55 +00004679}
4680
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004681MachineSDNode *
4682SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4683 EVT VT1, EVT VT2, SDValue Op1,
4684 SDValue Op2, SDValue Op3) {
Dan Gohmanee036282009-04-09 23:54:40 +00004685 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004686 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004687 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling6ed1f082009-01-29 09:01:55 +00004688}
4689
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004690MachineSDNode *
4691SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4692 EVT VT1, EVT VT2,
4693 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004694 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004695 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004696}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004697
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004698MachineSDNode *
4699SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4700 EVT VT1, EVT VT2, EVT VT3,
4701 SDValue Op1, SDValue Op2) {
Dan Gohmanee036282009-04-09 23:54:40 +00004702 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004703 SDValue Ops[] = { Op1, Op2 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004704 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004705}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004706
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004707MachineSDNode *
4708SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4709 EVT VT1, EVT VT2, EVT VT3,
4710 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanee036282009-04-09 23:54:40 +00004711 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004712 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004713 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004714}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004715
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004716MachineSDNode *
4717SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4718 EVT VT1, EVT VT2, EVT VT3,
4719 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004720 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004721 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004722}
Bill Wendling6ed1f082009-01-29 09:01:55 +00004723
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004724MachineSDNode *
4725SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4726 EVT VT2, EVT VT3, EVT VT4,
4727 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanee036282009-04-09 23:54:40 +00004728 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004729 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling6ed1f082009-01-29 09:01:55 +00004730}
4731
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004732MachineSDNode *
4733SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4734 const std::vector<EVT> &ResultTys,
4735 const SDValue *Ops, unsigned NumOps) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004736 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
4737 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
4738}
4739
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004740MachineSDNode *
4741SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
4742 const SDValue *Ops, unsigned NumOps) {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004743 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Flag;
4744 MachineSDNode *N;
4745 void *IP;
4746
4747 if (DoCSE) {
4748 FoldingSetNodeID ID;
4749 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
4750 IP = 0;
4751 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohmane1c0b3b2009-10-10 01:29:16 +00004752 return cast<MachineSDNode>(E);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00004753 }
4754
4755 // Allocate a new MachineSDNode.
4756 N = NodeAllocator.Allocate<MachineSDNode>();
4757 new (N) MachineSDNode(~Opcode, DL, VTs);
4758
4759 // Initialize the operands list.
4760 if (NumOps > array_lengthof(N->LocalOperands))
4761 // We're creating a final node that will live unmorphed for the
4762 // remainder of the current SelectionDAG iteration, so we can allocate
4763 // the operands directly out of a pool with no recycling metadata.
4764 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
4765 Ops, NumOps);
4766 else
4767 N->InitOperands(N->LocalOperands, Ops, NumOps);
4768 N->OperandsNeedDelete = false;
4769
4770 if (DoCSE)
4771 CSEMap.InsertNode(N, IP);
4772
4773 AllNodes.push_back(N);
4774#ifndef NDEBUG
4775 VerifyNode(N);
4776#endif
4777 return N;
Dale Johannesenba9d87f2009-01-29 00:47:48 +00004778}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779
Dan Gohman7ffdd432009-08-19 18:16:17 +00004780/// getTargetExtractSubreg - A convenience function for creating
4781/// TargetInstrInfo::EXTRACT_SUBREG nodes.
4782SDValue
4783SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
4784 SDValue Operand) {
4785 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Dan Gohman61fda0d2009-09-25 18:54:59 +00004786 SDNode *Subreg = getMachineNode(TargetInstrInfo::EXTRACT_SUBREG, DL,
4787 VT, Operand, SRIdxVal);
Dan Gohman7ffdd432009-08-19 18:16:17 +00004788 return SDValue(Subreg, 0);
4789}
4790
Bob Wilsonfcaa4cf2009-10-08 18:49:46 +00004791/// getTargetInsertSubreg - A convenience function for creating
4792/// TargetInstrInfo::INSERT_SUBREG nodes.
4793SDValue
4794SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
4795 SDValue Operand, SDValue Subreg) {
4796 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
4797 SDNode *Result = getMachineNode(TargetInstrInfo::INSERT_SUBREG, DL,
4798 VT, Operand, Subreg, SRIdxVal);
4799 return SDValue(Result, 0);
4800}
4801
Evan Chengd1113582008-03-22 01:55:50 +00004802/// getNodeIfExists - Get the specified node if it's already available, or
4803/// else return NULL.
4804SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman8181bd12008-07-27 21:46:04 +00004805 const SDValue *Ops, unsigned NumOps) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004806 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Evan Chengd1113582008-03-22 01:55:50 +00004807 FoldingSetNodeID ID;
4808 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4809 void *IP = 0;
4810 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4811 return E;
4812 }
4813 return NULL;
4814}
4815
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4817/// This can cause recursive merging of nodes in the DAG.
4818///
Chris Lattnerdca329f2008-02-03 03:35:22 +00004819/// This version assumes From has a single result value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004820///
Dan Gohman8181bd12008-07-27 21:46:04 +00004821void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004822 DAGUpdateListener *UpdateListener) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004823 SDNode *From = FromN.getNode();
Scott Michel91099d62009-02-17 22:15:04 +00004824 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 "Cannot replace with this method!");
Gabor Greif1c80d112008-08-28 21:40:38 +00004826 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levenstein05650fd2008-04-07 10:06:32 +00004827
djg943376a2009-01-25 16:29:12 +00004828 // Iterate over all the existing uses of From. New uses will be added
4829 // to the beginning of the use list, which we avoid visiting.
4830 // This specifically avoids visiting uses of From that arise while the
4831 // replacement is happening, because any such uses would be the result
4832 // of CSE: If an existing node looks like From after one of its operands
4833 // is replaced by To, we don't want to replace of all its users with To
4834 // too. See PR3018 for more info.
Dan Gohman86bf1392009-01-19 21:44:21 +00004835 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4836 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004837 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004838
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004839 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004840 RemoveNodeFromCSEMaps(User);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841
djg943376a2009-01-25 16:29:12 +00004842 // A user can appear in a use list multiple times, and when this
4843 // happens the uses are usually next to each other in the list.
4844 // To help reduce the number of CSE recomputations, process all
4845 // the uses of this user that we can find this way.
4846 do {
djgc2517d32009-01-26 04:35:06 +00004847 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004848 ++UI;
djgc2517d32009-01-26 04:35:06 +00004849 Use.set(To);
djg943376a2009-01-25 16:29:12 +00004850 } while (UI != UE && *UI == User);
4851
4852 // Now that we have modified User, add it back to the CSE maps. If it
4853 // already exists there, recursively merge the results together.
4854 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855 }
4856}
4857
4858/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4859/// This can cause recursive merging of nodes in the DAG.
4860///
Dan Gohman3209ac32009-04-15 20:06:30 +00004861/// This version assumes that for each value of From, there is a
4862/// corresponding value in To in the same position with the same type.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004863///
4864void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004865 DAGUpdateListener *UpdateListener) {
Dan Gohman3209ac32009-04-15 20:06:30 +00004866#ifndef NDEBUG
4867 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
4868 assert((!From->hasAnyUseOfValue(i) ||
4869 From->getValueType(i) == To->getValueType(i)) &&
4870 "Cannot use this version of ReplaceAllUsesWith!");
4871#endif
Dan Gohmanbd68c792008-07-17 19:10:17 +00004872
4873 // Handle the trivial case.
4874 if (From == To)
4875 return;
4876
Dan Gohman86bf1392009-01-19 21:44:21 +00004877 // Iterate over just the existing users of From. See the comments in
4878 // the ReplaceAllUsesWith above.
4879 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4880 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004881 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004882
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004884 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00004885
djg943376a2009-01-25 16:29:12 +00004886 // A user can appear in a use list multiple times, and when this
4887 // happens the uses are usually next to each other in the list.
4888 // To help reduce the number of CSE recomputations, process all
4889 // the uses of this user that we can find this way.
4890 do {
djgc2517d32009-01-26 04:35:06 +00004891 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004892 ++UI;
djgc2517d32009-01-26 04:35:06 +00004893 Use.setNode(To);
djg943376a2009-01-25 16:29:12 +00004894 } while (UI != UE && *UI == User);
4895
4896 // Now that we have modified User, add it back to the CSE maps. If it
4897 // already exists there, recursively merge the results together.
4898 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004899 }
4900}
4901
4902/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4903/// This can cause recursive merging of nodes in the DAG.
4904///
4905/// This version can replace From with any result values. To must match the
4906/// number and types of values returned by From.
4907void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman8181bd12008-07-27 21:46:04 +00004908 const SDValue *To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004909 DAGUpdateListener *UpdateListener) {
Chris Lattnerdca329f2008-02-03 03:35:22 +00004910 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman8181bd12008-07-27 21:46:04 +00004911 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912
Dan Gohman86bf1392009-01-19 21:44:21 +00004913 // Iterate over just the existing users of From. See the comments in
4914 // the ReplaceAllUsesWith above.
4915 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4916 while (UI != UE) {
djg943376a2009-01-25 16:29:12 +00004917 SDNode *User = *UI;
Roman Levenstein05650fd2008-04-07 10:06:32 +00004918
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004919 // This node is about to morph, remove its old self from the CSE maps.
djg943376a2009-01-25 16:29:12 +00004920 RemoveNodeFromCSEMaps(User);
Roman Levenstein05650fd2008-04-07 10:06:32 +00004921
djg943376a2009-01-25 16:29:12 +00004922 // A user can appear in a use list multiple times, and when this
4923 // happens the uses are usually next to each other in the list.
4924 // To help reduce the number of CSE recomputations, process all
4925 // the uses of this user that we can find this way.
4926 do {
djgc2517d32009-01-26 04:35:06 +00004927 SDUse &Use = UI.getUse();
4928 const SDValue &ToOp = To[Use.getResNo()];
djg943376a2009-01-25 16:29:12 +00004929 ++UI;
djgc2517d32009-01-26 04:35:06 +00004930 Use.set(ToOp);
djg943376a2009-01-25 16:29:12 +00004931 } while (UI != UE && *UI == User);
4932
4933 // Now that we have modified User, add it back to the CSE maps. If it
4934 // already exists there, recursively merge the results together.
4935 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004936 }
4937}
4938
4939/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
djgc2517d32009-01-26 04:35:06 +00004940/// uses of other values produced by From.getNode() alone. The Deleted
4941/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00004942void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004943 DAGUpdateListener *UpdateListener){
Dan Gohmanbd68c792008-07-17 19:10:17 +00004944 // Handle the really simple, really trivial case efficiently.
4945 if (From == To) return;
4946
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947 // Handle the simple, trivial, case efficiently.
Gabor Greif1c80d112008-08-28 21:40:38 +00004948 if (From.getNode()->getNumValues() == 1) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004949 ReplaceAllUsesWith(From, To, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 return;
4951 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004952
djg943376a2009-01-25 16:29:12 +00004953 // Iterate over just the existing users of From. See the comments in
4954 // the ReplaceAllUsesWith above.
4955 SDNode::use_iterator UI = From.getNode()->use_begin(),
4956 UE = From.getNode()->use_end();
4957 while (UI != UE) {
4958 SDNode *User = *UI;
4959 bool UserRemovedFromCSEMaps = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960
djg943376a2009-01-25 16:29:12 +00004961 // A user can appear in a use list multiple times, and when this
4962 // happens the uses are usually next to each other in the list.
4963 // To help reduce the number of CSE recomputations, process all
4964 // the uses of this user that we can find this way.
4965 do {
djgc2517d32009-01-26 04:35:06 +00004966 SDUse &Use = UI.getUse();
djg943376a2009-01-25 16:29:12 +00004967
4968 // Skip uses of different values from the same node.
djgc2517d32009-01-26 04:35:06 +00004969 if (Use.getResNo() != From.getResNo()) {
djg943376a2009-01-25 16:29:12 +00004970 ++UI;
4971 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004972 }
djg943376a2009-01-25 16:29:12 +00004973
4974 // If this node hasn't been modified yet, it's still in the CSE maps,
4975 // so remove its old self from the CSE maps.
4976 if (!UserRemovedFromCSEMaps) {
4977 RemoveNodeFromCSEMaps(User);
4978 UserRemovedFromCSEMaps = true;
4979 }
4980
4981 ++UI;
djgc2517d32009-01-26 04:35:06 +00004982 Use.set(To);
djg943376a2009-01-25 16:29:12 +00004983 } while (UI != UE && *UI == User);
4984
4985 // We are iterating over all uses of the From node, so if a use
4986 // doesn't use the specific value, no changes are made.
4987 if (!UserRemovedFromCSEMaps)
4988 continue;
4989
Chris Lattner8a258202007-10-15 06:10:22 +00004990 // Now that we have modified User, add it back to the CSE maps. If it
4991 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00004992 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanbd68c792008-07-17 19:10:17 +00004993 }
4994}
4995
djg943376a2009-01-25 16:29:12 +00004996namespace {
4997 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
4998 /// to record information about a use.
4999 struct UseMemo {
5000 SDNode *User;
5001 unsigned Index;
djgc2517d32009-01-26 04:35:06 +00005002 SDUse *Use;
djg943376a2009-01-25 16:29:12 +00005003 };
djgc2517d32009-01-26 04:35:06 +00005004
5005 /// operator< - Sort Memos by User.
5006 bool operator<(const UseMemo &L, const UseMemo &R) {
5007 return (intptr_t)L.User < (intptr_t)R.User;
5008 }
djg943376a2009-01-25 16:29:12 +00005009}
5010
Dan Gohmanbd68c792008-07-17 19:10:17 +00005011/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
djgc2517d32009-01-26 04:35:06 +00005012/// uses of other values produced by From.getNode() alone. The same value
5013/// may appear in both the From and To list. The Deleted vector is
Dan Gohmanbd68c792008-07-17 19:10:17 +00005014/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman8181bd12008-07-27 21:46:04 +00005015void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5016 const SDValue *To,
Dan Gohmanbd68c792008-07-17 19:10:17 +00005017 unsigned Num,
5018 DAGUpdateListener *UpdateListener){
5019 // Handle the simple, trivial case efficiently.
5020 if (Num == 1)
5021 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5022
djg943376a2009-01-25 16:29:12 +00005023 // Read up all the uses and make records of them. This helps
5024 // processing new uses that are introduced during the
5025 // replacement process.
5026 SmallVector<UseMemo, 4> Uses;
5027 for (unsigned i = 0; i != Num; ++i) {
5028 unsigned FromResNo = From[i].getResNo();
5029 SDNode *FromNode = From[i].getNode();
Scott Michel91099d62009-02-17 22:15:04 +00005030 for (SDNode::use_iterator UI = FromNode->use_begin(),
djgc2517d32009-01-26 04:35:06 +00005031 E = FromNode->use_end(); UI != E; ++UI) {
5032 SDUse &Use = UI.getUse();
5033 if (Use.getResNo() == FromResNo) {
5034 UseMemo Memo = { *UI, i, &Use };
djg943376a2009-01-25 16:29:12 +00005035 Uses.push_back(Memo);
5036 }
djgc2517d32009-01-26 04:35:06 +00005037 }
djg943376a2009-01-25 16:29:12 +00005038 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00005039
djgc2517d32009-01-26 04:35:06 +00005040 // Sort the uses, so that all the uses from a given User are together.
5041 std::sort(Uses.begin(), Uses.end());
5042
djg943376a2009-01-25 16:29:12 +00005043 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5044 UseIndex != UseIndexEnd; ) {
Dan Gohmanbd68c792008-07-17 19:10:17 +00005045 // We know that this user uses some value of From. If it is the right
5046 // value, update it.
djg943376a2009-01-25 16:29:12 +00005047 SDNode *User = Uses[UseIndex].User;
5048
5049 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmanbd68c792008-07-17 19:10:17 +00005050 RemoveNodeFromCSEMaps(User);
djg943376a2009-01-25 16:29:12 +00005051
djgc2517d32009-01-26 04:35:06 +00005052 // The Uses array is sorted, so all the uses for a given User
5053 // are next to each other in the list.
djg943376a2009-01-25 16:29:12 +00005054 // To help reduce the number of CSE recomputations, process all
5055 // the uses of this user that we can find this way.
5056 do {
5057 unsigned i = Uses[UseIndex].Index;
djgc2517d32009-01-26 04:35:06 +00005058 SDUse &Use = *Uses[UseIndex].Use;
djg943376a2009-01-25 16:29:12 +00005059 ++UseIndex;
5060
djgc2517d32009-01-26 04:35:06 +00005061 Use.set(To[i]);
djg943376a2009-01-25 16:29:12 +00005062 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5063
Dan Gohmanbd68c792008-07-17 19:10:17 +00005064 // Now that we have modified User, add it back to the CSE maps. If it
5065 // already exists there, recursively merge the results together.
djg943376a2009-01-25 16:29:12 +00005066 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005067 }
5068}
5069
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005070/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
5071/// based on their topological order. It returns the maximum id and a vector
5072/// of the SDNodes* in assigned order by reference.
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005073unsigned SelectionDAG::AssignTopologicalOrder() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005074
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005075 unsigned DAGSize = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005077 // SortedPos tracks the progress of the algorithm. Nodes before it are
5078 // sorted, nodes after it are unsorted. When the algorithm completes
5079 // it is at the end of the list.
5080 allnodes_iterator SortedPos = allnodes_begin();
5081
Dan Gohman6e780312008-11-21 19:10:41 +00005082 // Visit all the nodes. Move nodes with no operands to the front of
5083 // the list immediately. Annotate nodes that do have operands with their
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005084 // operand count. Before we do this, the Node Id fields of the nodes
5085 // may contain arbitrary values. After, the Node Id fields for nodes
5086 // before SortedPos will contain the topological sort index, and the
5087 // Node Id fields for nodes At SortedPos and after will contain the
5088 // count of outstanding operands.
5089 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5090 SDNode *N = I++;
5091 unsigned Degree = N->getNumOperands();
5092 if (Degree == 0) {
5093 // A node with no uses, add it to the result array immediately.
5094 N->setNodeId(DAGSize++);
5095 allnodes_iterator Q = N;
5096 if (Q != SortedPos)
5097 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
5098 ++SortedPos;
5099 } else {
5100 // Temporarily use the Node Id as scratch space for the degree count.
5101 N->setNodeId(Degree);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005102 }
5103 }
5104
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005105 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5106 // such that by the time the end is reached all nodes will be sorted.
5107 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5108 SDNode *N = I;
5109 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5110 UI != UE; ++UI) {
5111 SDNode *P = *UI;
5112 unsigned Degree = P->getNodeId();
5113 --Degree;
5114 if (Degree == 0) {
5115 // All of P's operands are sorted, so P may sorted now.
5116 P->setNodeId(DAGSize++);
5117 if (P != SortedPos)
5118 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
5119 ++SortedPos;
5120 } else {
5121 // Update P's outstanding operand count.
5122 P->setNodeId(Degree);
5123 }
5124 }
5125 }
5126
5127 assert(SortedPos == AllNodes.end() &&
5128 "Topological sort incomplete!");
5129 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5130 "First node in topological sort is not the entry token!");
5131 assert(AllNodes.front().getNodeId() == 0 &&
5132 "First node in topological sort has non-zero id!");
5133 assert(AllNodes.front().getNumOperands() == 0 &&
5134 "First node in topological sort has operands!");
5135 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5136 "Last node in topologic sort has unexpected id!");
5137 assert(AllNodes.back().use_empty() &&
5138 "Last node in topologic sort has users!");
Dan Gohman6e780312008-11-21 19:10:41 +00005139 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohman2d2a7a32008-09-30 18:30:35 +00005140 return DAGSize;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005141}
5142
5143
5144
5145//===----------------------------------------------------------------------===//
5146// SDNode Class
5147//===----------------------------------------------------------------------===//
5148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149HandleSDNode::~HandleSDNode() {
Dan Gohmanb997a4e2008-07-07 20:57:48 +00005150 DropOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151}
5152
Chris Lattnerb31fb962009-06-26 21:14:05 +00005153GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA,
Owen Andersonac9de032009-08-10 22:56:29 +00005154 EVT VT, int64_t o, unsigned char TF)
Chris Lattnerb31fb962009-06-26 21:14:05 +00005155 : SDNode(Opc, DebugLoc::getUnknownLoc(), getSDVTList(VT)),
Chris Lattner7123ef72009-06-25 21:21:14 +00005156 Offset(o), TargetFlags(TF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005157 TheGlobal = const_cast<GlobalValue*>(GA);
5158}
5159
Owen Andersonac9de032009-08-10 22:56:29 +00005160MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005161 MachineMemOperand *mmo)
5162 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
5163 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile());
5164 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5165 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen08f344e2009-01-28 21:18:29 +00005166}
5167
Scott Michel91099d62009-02-17 22:15:04 +00005168MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Nate Begemane22356d2009-09-15 00:13:12 +00005169 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005170 MachineMemOperand *mmo)
Dale Johannesen08f344e2009-01-28 21:18:29 +00005171 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005172 MemoryVT(memvt), MMO(mmo) {
5173 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile());
5174 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5175 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005176}
5177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005178/// Profile - Gather unique data for the node.
5179///
Dan Gohman98beebe2008-08-20 15:58:01 +00005180void SDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 AddNodeIDNode(ID, this);
5182}
5183
Owen Anderson97be9d32009-08-25 22:27:22 +00005184namespace {
5185 struct EVTArray {
5186 std::vector<EVT> VTs;
5187
5188 EVTArray() {
5189 VTs.reserve(MVT::LAST_VALUETYPE);
5190 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5191 VTs.push_back(MVT((MVT::SimpleValueType)i));
5192 }
5193 };
5194}
5195
Owen Andersonac9de032009-08-10 22:56:29 +00005196static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Anderson97be9d32009-08-25 22:27:22 +00005197static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner28ab49b2009-08-22 04:07:34 +00005198static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonef31ae32009-06-25 17:09:00 +00005199
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005200/// getValueTypeList - Return a pointer to the specified value type.
5201///
Owen Andersonac9de032009-08-10 22:56:29 +00005202const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands92c43912008-06-06 12:08:01 +00005203 if (VT.isExtended()) {
Owen Anderson1a285c22009-08-22 06:32:36 +00005204 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonef31ae32009-06-25 17:09:00 +00005205 return &(*EVTs->insert(VT).first);
Duncan Sandsa9810f32007-10-16 09:56:48 +00005206 } else {
Owen Anderson97be9d32009-08-25 22:27:22 +00005207 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsa9810f32007-10-16 09:56:48 +00005208 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005209}
Duncan Sandsa9810f32007-10-16 09:56:48 +00005210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005211/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5212/// indicated value. This method ignores uses of other values defined by this
5213/// operation.
5214bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
5215 assert(Value < getNumValues() && "Bad value!");
5216
Roman Levenstein05650fd2008-04-07 10:06:32 +00005217 // TODO: Only iterate over uses of a given value of the node
5218 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
djgc2517d32009-01-26 04:35:06 +00005219 if (UI.getUse().getResNo() == Value) {
Roman Levenstein05650fd2008-04-07 10:06:32 +00005220 if (NUses == 0)
5221 return false;
5222 --NUses;
5223 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224 }
5225
5226 // Found exactly the right number of uses?
5227 return NUses == 0;
5228}
5229
5230
Evan Cheng0af04f72007-08-02 05:29:38 +00005231/// hasAnyUseOfValue - Return true if there are any use of the indicated
5232/// value. This method ignores uses of other values defined by this operation.
5233bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5234 assert(Value < getNumValues() && "Bad value!");
5235
Dan Gohmana9651732008-07-09 22:39:01 +00005236 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
djgc2517d32009-01-26 04:35:06 +00005237 if (UI.getUse().getResNo() == Value)
Dan Gohmana9651732008-07-09 22:39:01 +00005238 return true;
Evan Cheng0af04f72007-08-02 05:29:38 +00005239
5240 return false;
5241}
5242
5243
Dan Gohman07fadb02008-07-27 18:06:42 +00005244/// isOnlyUserOf - Return true if this node is the only use of N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245///
Dan Gohman07fadb02008-07-27 18:06:42 +00005246bool SDNode::isOnlyUserOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005247 bool Seen = false;
5248 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00005249 SDNode *User = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005250 if (User == this)
5251 Seen = true;
5252 else
5253 return false;
5254 }
5255
5256 return Seen;
5257}
5258
5259/// isOperand - Return true if this node is an operand of N.
5260///
Dan Gohman8181bd12008-07-27 21:46:04 +00005261bool SDValue::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005262 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5263 if (*this == N->getOperand(i))
5264 return true;
5265 return false;
5266}
5267
Evan Chengd9387682008-03-04 00:41:45 +00005268bool SDNode::isOperandOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005269 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
djgc2517d32009-01-26 04:35:06 +00005270 if (this == N->OperandList[i].getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005271 return true;
5272 return false;
5273}
5274
Chris Lattner10d94f92008-01-16 05:49:24 +00005275/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michel91099d62009-02-17 22:15:04 +00005276/// be a chain) reaches the specified operand without crossing any
Chris Lattner10d94f92008-01-16 05:49:24 +00005277/// side-effecting instructions. In practice, this looks through token
5278/// factors and non-volatile loads. In order to remain efficient, this only
5279/// looks a couple of nodes in, it does not do an exhaustive search.
Scott Michel91099d62009-02-17 22:15:04 +00005280bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner10d94f92008-01-16 05:49:24 +00005281 unsigned Depth) const {
5282 if (*this == Dest) return true;
Scott Michel91099d62009-02-17 22:15:04 +00005283
Chris Lattner10d94f92008-01-16 05:49:24 +00005284 // Don't search too deeply, we just want to be able to see through
5285 // TokenFactor's etc.
5286 if (Depth == 0) return false;
Scott Michel91099d62009-02-17 22:15:04 +00005287
Chris Lattner10d94f92008-01-16 05:49:24 +00005288 // If this is a token factor, all inputs to the TF happen in parallel. If any
5289 // of the operands of the TF reach dest, then we can do the xform.
5290 if (getOpcode() == ISD::TokenFactor) {
5291 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5292 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5293 return true;
5294 return false;
5295 }
Scott Michel91099d62009-02-17 22:15:04 +00005296
Chris Lattner10d94f92008-01-16 05:49:24 +00005297 // Loads don't have side effects, look through them.
5298 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5299 if (!Ld->isVolatile())
5300 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5301 }
5302 return false;
5303}
5304
Evan Chengd9387682008-03-04 00:41:45 +00005305/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohmaned7cc322009-10-28 03:44:30 +00005306/// is either an operand of N or it can be reached by traversing up the operands.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307/// NOTE: this is an expensive method. Use it carefully.
Evan Chengd9387682008-03-04 00:41:45 +00005308bool SDNode::isPredecessorOf(SDNode *N) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005309 SmallPtrSet<SDNode *, 32> Visited;
Dan Gohmaned7cc322009-10-28 03:44:30 +00005310 SmallVector<SDNode *, 16> Worklist;
5311 Worklist.push_back(N);
5312
5313 do {
5314 N = Worklist.pop_back_val();
5315 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5316 SDNode *Op = N->getOperand(i).getNode();
5317 if (Op == this)
5318 return true;
5319 if (Visited.insert(Op))
5320 Worklist.push_back(Op);
5321 }
5322 } while (!Worklist.empty());
5323
5324 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325}
5326
5327uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5328 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005329 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330}
5331
5332std::string SDNode::getOperationName(const SelectionDAG *G) const {
5333 switch (getOpcode()) {
5334 default:
5335 if (getOpcode() < ISD::BUILTIN_OP_END)
5336 return "<<Unknown DAG Node>>";
Dan Gohmanbd68c792008-07-17 19:10:17 +00005337 if (isMachineOpcode()) {
5338 if (G)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005339 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmanbd68c792008-07-17 19:10:17 +00005340 if (getMachineOpcode() < TII->getNumOpcodes())
5341 return TII->get(getMachineOpcode()).getName();
5342 return "<<Unknown Machine Node>>";
5343 }
5344 if (G) {
Dan Gohmana0c429e2009-01-15 16:58:17 +00005345 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmanbd68c792008-07-17 19:10:17 +00005346 const char *Name = TLI.getTargetNodeName(getOpcode());
5347 if (Name) return Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005348 return "<<Unknown Target Node>>";
5349 }
Dan Gohmanbd68c792008-07-17 19:10:17 +00005350 return "<<Unknown Node>>";
Scott Michel91099d62009-02-17 22:15:04 +00005351
Dan Gohmanbd68c792008-07-17 19:10:17 +00005352#ifndef NDEBUG
5353 case ISD::DELETED_NODE:
5354 return "<<Deleted Node!>>";
5355#endif
Evan Chengd1d68072008-03-08 00:58:38 +00005356 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth785610d2008-02-16 01:24:58 +00005357 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohmanbebba8d2008-12-23 21:37:04 +00005358 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5359 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5360 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5361 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5362 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5363 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5364 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5365 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5366 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5367 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5368 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5369 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005370 case ISD::PCMARKER: return "PCMarker";
5371 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
5372 case ISD::SRCVALUE: return "SrcValue";
5373 case ISD::EntryToken: return "EntryToken";
5374 case ISD::TokenFactor: return "TokenFactor";
5375 case ISD::AssertSext: return "AssertSext";
5376 case ISD::AssertZext: return "AssertZext";
5377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378 case ISD::BasicBlock: return "BasicBlock";
5379 case ISD::VALUETYPE: return "ValueType";
5380 case ISD::Register: return "Register";
5381
5382 case ISD::Constant: return "Constant";
5383 case ISD::ConstantFP: return "ConstantFP";
5384 case ISD::GlobalAddress: return "GlobalAddress";
5385 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
5386 case ISD::FrameIndex: return "FrameIndex";
5387 case ISD::JumpTable: return "JumpTable";
5388 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendlingfef06052008-09-16 21:48:12 +00005389 case ISD::RETURNADDR: return "RETURNADDR";
5390 case ISD::FRAMEADDR: return "FRAMEADDR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
5392 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Jim Grosbach29feb6a2009-08-11 00:09:57 +00005393 case ISD::LSDAADDR: return "LSDAADDR";
Bill Wendlingfef06052008-09-16 21:48:12 +00005394 case ISD::EHSELECTION: return "EHSELECTION";
5395 case ISD::EH_RETURN: return "EH_RETURN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005396 case ISD::ConstantPool: return "ConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005397 case ISD::ExternalSymbol: return "ExternalSymbol";
Dan Gohman91057512009-10-30 01:27:03 +00005398 case ISD::BlockAddress: return "BlockAddress";
Jakob Stoklund Olesen75dbdd12009-10-15 18:50:03 +00005399 case ISD::INTRINSIC_WO_CHAIN:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400 case ISD::INTRINSIC_VOID:
5401 case ISD::INTRINSIC_W_CHAIN: {
Jakob Stoklund Olesen75dbdd12009-10-15 18:50:03 +00005402 unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
5403 unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
5404 if (IID < Intrinsic::num_intrinsics)
5405 return Intrinsic::getName((Intrinsic::ID)IID);
5406 else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
5407 return TII->getName(IID);
5408 llvm_unreachable("Invalid intrinsic ID");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 }
5410
5411 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
5412 case ISD::TargetConstant: return "TargetConstant";
5413 case ISD::TargetConstantFP:return "TargetConstantFP";
5414 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
5415 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
5416 case ISD::TargetFrameIndex: return "TargetFrameIndex";
5417 case ISD::TargetJumpTable: return "TargetJumpTable";
5418 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendlingfef06052008-09-16 21:48:12 +00005419 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Dan Gohman91057512009-10-30 01:27:03 +00005420 case ISD::TargetBlockAddress: return "TargetBlockAddress";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421
5422 case ISD::CopyToReg: return "CopyToReg";
5423 case ISD::CopyFromReg: return "CopyFromReg";
5424 case ISD::UNDEF: return "undef";
5425 case ISD::MERGE_VALUES: return "merge_values";
5426 case ISD::INLINEASM: return "inlineasm";
Dan Gohmanfa607c92008-07-01 00:05:16 +00005427 case ISD::EH_LABEL: return "eh_label";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428 case ISD::HANDLENODE: return "handlenode";
Scott Michel91099d62009-02-17 22:15:04 +00005429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005430 // Unary operators
5431 case ISD::FABS: return "fabs";
5432 case ISD::FNEG: return "fneg";
5433 case ISD::FSQRT: return "fsqrt";
5434 case ISD::FSIN: return "fsin";
5435 case ISD::FCOS: return "fcos";
5436 case ISD::FPOWI: return "fpowi";
Dan Gohman1d744bb2007-10-11 23:06:37 +00005437 case ISD::FPOW: return "fpow";
Dan Gohmanc8b20e22008-08-21 17:55:02 +00005438 case ISD::FTRUNC: return "ftrunc";
5439 case ISD::FFLOOR: return "ffloor";
5440 case ISD::FCEIL: return "fceil";
5441 case ISD::FRINT: return "frint";
5442 case ISD::FNEARBYINT: return "fnearbyint";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443
5444 // Binary operators
5445 case ISD::ADD: return "add";
5446 case ISD::SUB: return "sub";
5447 case ISD::MUL: return "mul";
5448 case ISD::MULHU: return "mulhu";
5449 case ISD::MULHS: return "mulhs";
5450 case ISD::SDIV: return "sdiv";
5451 case ISD::UDIV: return "udiv";
5452 case ISD::SREM: return "srem";
5453 case ISD::UREM: return "urem";
Dan Gohmanb945cee2007-10-05 14:11:04 +00005454 case ISD::SMUL_LOHI: return "smul_lohi";
5455 case ISD::UMUL_LOHI: return "umul_lohi";
5456 case ISD::SDIVREM: return "sdivrem";
Dan Gohman2e66b052008-09-08 16:30:29 +00005457 case ISD::UDIVREM: return "udivrem";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458 case ISD::AND: return "and";
5459 case ISD::OR: return "or";
5460 case ISD::XOR: return "xor";
5461 case ISD::SHL: return "shl";
5462 case ISD::SRA: return "sra";
5463 case ISD::SRL: return "srl";
5464 case ISD::ROTL: return "rotl";
5465 case ISD::ROTR: return "rotr";
5466 case ISD::FADD: return "fadd";
5467 case ISD::FSUB: return "fsub";
5468 case ISD::FMUL: return "fmul";
5469 case ISD::FDIV: return "fdiv";
5470 case ISD::FREM: return "frem";
5471 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner13f06832007-12-22 21:26:52 +00005472 case ISD::FGETSIGN: return "fgetsign";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005473
5474 case ISD::SETCC: return "setcc";
Nate Begeman9a1ce152008-05-12 19:40:03 +00005475 case ISD::VSETCC: return "vsetcc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 case ISD::SELECT: return "select";
5477 case ISD::SELECT_CC: return "select_cc";
5478 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
5479 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
5480 case ISD::CONCAT_VECTORS: return "concat_vectors";
5481 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
5482 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
5483 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Dale Johannesen747fe522009-06-02 03:12:52 +00005484 case ISD::CARRY_FALSE: return "carry_false";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 case ISD::ADDC: return "addc";
5486 case ISD::ADDE: return "adde";
Bill Wendlingea8b7c92008-11-21 02:12:42 +00005487 case ISD::SADDO: return "saddo";
5488 case ISD::UADDO: return "uaddo";
Bill Wendling7e04be62008-12-09 22:08:41 +00005489 case ISD::SSUBO: return "ssubo";
5490 case ISD::USUBO: return "usubo";
5491 case ISD::SMULO: return "smulo";
5492 case ISD::UMULO: return "umulo";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493 case ISD::SUBC: return "subc";
5494 case ISD::SUBE: return "sube";
5495 case ISD::SHL_PARTS: return "shl_parts";
5496 case ISD::SRA_PARTS: return "sra_parts";
5497 case ISD::SRL_PARTS: return "srl_parts";
Scott Michel91099d62009-02-17 22:15:04 +00005498
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005499 // Conversion operators.
5500 case ISD::SIGN_EXTEND: return "sign_extend";
5501 case ISD::ZERO_EXTEND: return "zero_extend";
5502 case ISD::ANY_EXTEND: return "any_extend";
5503 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
5504 case ISD::TRUNCATE: return "truncate";
5505 case ISD::FP_ROUND: return "fp_round";
Dan Gohman819574c2008-01-31 00:41:03 +00005506 case ISD::FLT_ROUNDS_: return "flt_rounds";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
5508 case ISD::FP_EXTEND: return "fp_extend";
5509
5510 case ISD::SINT_TO_FP: return "sint_to_fp";
5511 case ISD::UINT_TO_FP: return "uint_to_fp";
5512 case ISD::FP_TO_SINT: return "fp_to_sint";
5513 case ISD::FP_TO_UINT: return "fp_to_uint";
5514 case ISD::BIT_CONVERT: return "bit_convert";
Scott Michel91099d62009-02-17 22:15:04 +00005515
Mon P Wang73d31542008-11-10 20:54:11 +00005516 case ISD::CONVERT_RNDSAT: {
5517 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005518 default: llvm_unreachable("Unknown cvt code!");
Mon P Wang73d31542008-11-10 20:54:11 +00005519 case ISD::CVT_FF: return "cvt_ff";
5520 case ISD::CVT_FS: return "cvt_fs";
5521 case ISD::CVT_FU: return "cvt_fu";
5522 case ISD::CVT_SF: return "cvt_sf";
5523 case ISD::CVT_UF: return "cvt_uf";
5524 case ISD::CVT_SS: return "cvt_ss";
5525 case ISD::CVT_SU: return "cvt_su";
5526 case ISD::CVT_US: return "cvt_us";
5527 case ISD::CVT_UU: return "cvt_uu";
5528 }
5529 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530
5531 // Control flow instructions
5532 case ISD::BR: return "br";
5533 case ISD::BRIND: return "brind";
5534 case ISD::BR_JT: return "br_jt";
5535 case ISD::BRCOND: return "brcond";
5536 case ISD::BR_CC: return "br_cc";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005537 case ISD::CALLSEQ_START: return "callseq_start";
5538 case ISD::CALLSEQ_END: return "callseq_end";
5539
5540 // Other operators
5541 case ISD::LOAD: return "load";
5542 case ISD::STORE: return "store";
5543 case ISD::VAARG: return "vaarg";
5544 case ISD::VACOPY: return "vacopy";
5545 case ISD::VAEND: return "vaend";
5546 case ISD::VASTART: return "vastart";
5547 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
5548 case ISD::EXTRACT_ELEMENT: return "extract_element";
5549 case ISD::BUILD_PAIR: return "build_pair";
5550 case ISD::STACKSAVE: return "stacksave";
5551 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +00005552 case ISD::TRAP: return "trap";
5553
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 // Bit manipulation
5555 case ISD::BSWAP: return "bswap";
5556 case ISD::CTPOP: return "ctpop";
5557 case ISD::CTTZ: return "cttz";
5558 case ISD::CTLZ: return "ctlz";
5559
Duncan Sands38947cd2007-07-27 12:58:54 +00005560 // Trampolines
Duncan Sands7407a9f2007-09-11 14:10:23 +00005561 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands38947cd2007-07-27 12:58:54 +00005562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563 case ISD::CONDCODE:
5564 switch (cast<CondCodeSDNode>(this)->get()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005565 default: llvm_unreachable("Unknown setcc condition!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 case ISD::SETOEQ: return "setoeq";
5567 case ISD::SETOGT: return "setogt";
5568 case ISD::SETOGE: return "setoge";
5569 case ISD::SETOLT: return "setolt";
5570 case ISD::SETOLE: return "setole";
5571 case ISD::SETONE: return "setone";
5572
5573 case ISD::SETO: return "seto";
5574 case ISD::SETUO: return "setuo";
5575 case ISD::SETUEQ: return "setue";
5576 case ISD::SETUGT: return "setugt";
5577 case ISD::SETUGE: return "setuge";
5578 case ISD::SETULT: return "setult";
5579 case ISD::SETULE: return "setule";
5580 case ISD::SETUNE: return "setune";
5581
5582 case ISD::SETEQ: return "seteq";
5583 case ISD::SETGT: return "setgt";
5584 case ISD::SETGE: return "setge";
5585 case ISD::SETLT: return "setlt";
5586 case ISD::SETLE: return "setle";
5587 case ISD::SETNE: return "setne";
5588 }
5589 }
5590}
5591
5592const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
5593 switch (AM) {
5594 default:
5595 return "";
5596 case ISD::PRE_INC:
5597 return "<pre-inc>";
5598 case ISD::PRE_DEC:
5599 return "<pre-dec>";
5600 case ISD::POST_INC:
5601 return "<post-inc>";
5602 case ISD::POST_DEC:
5603 return "<post-dec>";
5604 }
5605}
5606
Duncan Sandsc93fae32008-03-21 09:14:45 +00005607std::string ISD::ArgFlagsTy::getArgFlagsString() {
5608 std::string S = "< ";
5609
5610 if (isZExt())
5611 S += "zext ";
5612 if (isSExt())
5613 S += "sext ";
5614 if (isInReg())
5615 S += "inreg ";
5616 if (isSRet())
5617 S += "sret ";
5618 if (isByVal())
5619 S += "byval ";
5620 if (isNest())
5621 S += "nest ";
5622 if (getByValAlign())
5623 S += "byval-align:" + utostr(getByValAlign()) + " ";
5624 if (getOrigAlign())
5625 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5626 if (getByValSize())
5627 S += "byval-size:" + utostr(getByValSize()) + " ";
5628 return S + ">";
5629}
5630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005631void SDNode::dump() const { dump(0); }
5632void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005633 print(errs(), G);
5634}
5635
Stuart Hastingsc6250282009-02-04 16:46:19 +00005636void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005637 OS << (void*)this << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005638
5639 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005640 if (i) OS << ",";
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005641 if (getValueType(i) == MVT::Other)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005642 OS << "ch";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005643 else
Owen Andersonac9de032009-08-10 22:56:29 +00005644 OS << getValueType(i).getEVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005645 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005646 OS << " = " << getOperationName(G);
Stuart Hastingsc6250282009-02-04 16:46:19 +00005647}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005648
Stuart Hastingsc6250282009-02-04 16:46:19 +00005649void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005650 if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
5651 if (!MN->memoperands_empty()) {
5652 OS << "<";
5653 OS << "Mem:";
5654 for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
5655 e = MN->memoperands_end(); i != e; ++i) {
5656 OS << **i;
5657 if (next(i) != e)
5658 OS << " ";
5659 }
5660 OS << ">";
5661 }
5662 } else if (const ShuffleVectorSDNode *SVN =
5663 dyn_cast<ShuffleVectorSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005664 OS << "<";
Nate Begeman543d2142009-04-27 18:41:29 +00005665 for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
5666 int Idx = SVN->getMaskElt(i);
Chris Lattner1fefaac2008-08-23 22:23:09 +00005667 if (i) OS << ",";
Nate Begeman543d2142009-04-27 18:41:29 +00005668 if (Idx < 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005669 OS << "u";
Evan Chengaad43a02007-12-11 02:08:35 +00005670 else
Nate Begeman543d2142009-04-27 18:41:29 +00005671 OS << Idx;
Evan Chengaad43a02007-12-11 02:08:35 +00005672 }
Chris Lattner1fefaac2008-08-23 22:23:09 +00005673 OS << ">";
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005674 } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005675 OS << '<' << CSDN->getAPIntValue() << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005676 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen2fc20782007-09-14 22:26:36 +00005677 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005678 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005679 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005680 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen2fc20782007-09-14 22:26:36 +00005681 else {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005682 OS << "<APFloat(";
Dale Johannesen6e547b42008-10-09 23:00:39 +00005683 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005684 OS << ")>";
Dale Johannesen2fc20782007-09-14 22:26:36 +00005685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005686 } else if (const GlobalAddressSDNode *GADN =
5687 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohmancfe43232008-10-18 18:22:42 +00005688 int64_t offset = GADN->getOffset();
Chris Lattner1fefaac2008-08-23 22:23:09 +00005689 OS << '<';
5690 WriteAsOperand(OS, GADN->getGlobal());
5691 OS << '>';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005692 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005693 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005694 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005695 OS << " " << offset;
Dan Gohman36251262009-08-01 19:13:38 +00005696 if (unsigned int TF = GADN->getTargetFlags())
Chris Lattner7123ef72009-06-25 21:21:14 +00005697 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005699 OS << "<" << FIDN->getIndex() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005700 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005701 OS << "<" << JTDN->getIndex() << ">";
Dan Gohman36251262009-08-01 19:13:38 +00005702 if (unsigned int TF = JTDN->getTargetFlags())
Chris Lattner583c7962009-06-25 21:35:31 +00005703 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005704 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
5705 int offset = CP->getOffset();
5706 if (CP->isMachineConstantPoolEntry())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005707 OS << "<" << *CP->getMachineCPVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005708 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005709 OS << "<" << *CP->getConstVal() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710 if (offset > 0)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005711 OS << " + " << offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005712 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005713 OS << " " << offset;
Dan Gohman36251262009-08-01 19:13:38 +00005714 if (unsigned int TF = CP->getTargetFlags())
Chris Lattner583c7962009-06-25 21:35:31 +00005715 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005716 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner1fefaac2008-08-23 22:23:09 +00005717 OS << "<";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5719 if (LBB)
Chris Lattner1fefaac2008-08-23 22:23:09 +00005720 OS << LBB->getName() << " ";
5721 OS << (const void*)BBDN->getBasicBlock() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman1e57df32008-02-10 18:45:23 +00005723 if (G && R->getReg() &&
5724 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Dan Gohman57b31652009-10-31 20:19:03 +00005725 OS << " %" << G->getTarget().getRegisterInfo()->getName(R->getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005726 } else {
Dan Gohman57b31652009-10-31 20:19:03 +00005727 OS << " %reg" << R->getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005728 }
Bill Wendlingfef06052008-09-16 21:48:12 +00005729 } else if (const ExternalSymbolSDNode *ES =
5730 dyn_cast<ExternalSymbolSDNode>(this)) {
5731 OS << "'" << ES->getSymbol() << "'";
Dan Gohman36251262009-08-01 19:13:38 +00005732 if (unsigned int TF = ES->getTargetFlags())
Chris Lattner7123ef72009-06-25 21:21:14 +00005733 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005734 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5735 if (M->getValue())
Chris Lattner1fefaac2008-08-23 22:23:09 +00005736 OS << "<" << M->getValue() << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005737 else
Chris Lattner1fefaac2008-08-23 22:23:09 +00005738 OS << "<null>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005739 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Owen Andersonac9de032009-08-10 22:56:29 +00005740 OS << ":" << N->getVT().getEVTString();
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005741 }
5742 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Dan Gohman438d4102009-10-29 23:30:06 +00005743 OS << "<" << *LD->getMemOperand();
Evan Cheng034c4f82007-12-18 19:06:30 +00005744
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005745 bool doExt = true;
5746 switch (LD->getExtensionType()) {
5747 default: doExt = false; break;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005748 case ISD::EXTLOAD: OS << ", anyext"; break;
5749 case ISD::SEXTLOAD: OS << ", sext"; break;
5750 case ISD::ZEXTLOAD: OS << ", zext"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751 }
5752 if (doExt)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005753 OS << " from " << LD->getMemoryVT().getEVTString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754
5755 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sandsf9a44972007-07-19 07:31:58 +00005756 if (*AM)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005757 OS << ", " << AM;
5758
5759 OS << ">";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Dan Gohman438d4102009-10-29 23:30:06 +00005761 OS << "<" << *ST->getMemOperand();
Evan Cheng034c4f82007-12-18 19:06:30 +00005762
5763 if (ST->isTruncatingStore())
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005764 OS << ", trunc to " << ST->getMemoryVT().getEVTString();
Evan Cheng034c4f82007-12-18 19:06:30 +00005765
5766 const char *AM = getIndexedModeName(ST->getAddressingMode());
5767 if (*AM)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00005768 OS << ", " << AM;
5769
5770 OS << ">";
5771 } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
Dan Gohman438d4102009-10-29 23:30:06 +00005772 OS << "<" << *M->getMemOperand() << ">";
Dan Gohman91057512009-10-30 01:27:03 +00005773 } else if (const BlockAddressSDNode *BA =
5774 dyn_cast<BlockAddressSDNode>(this)) {
5775 OS << "<";
5776 WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false);
5777 OS << ", ";
5778 WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false);
5779 OS << ">";
Dan Gohman885793b2009-11-20 23:18:13 +00005780 if (unsigned int TF = BA->getTargetFlags())
5781 OS << " [TF=" << TF << ']';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782 }
5783}
5784
Stuart Hastingsc6250282009-02-04 16:46:19 +00005785void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5786 print_types(OS, G);
Stuart Hastingsc6250282009-02-04 16:46:19 +00005787 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman63e2b7e2009-11-05 18:49:11 +00005788 if (i) OS << ", "; else OS << " ";
Stuart Hastingsc6250282009-02-04 16:46:19 +00005789 OS << (void*)getOperand(i).getNode();
5790 if (unsigned RN = getOperand(i).getResNo())
5791 OS << ":" << RN;
5792 }
5793 print_details(OS, G);
5794}
5795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005796static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
5797 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greif1c80d112008-08-28 21:40:38 +00005798 if (N->getOperand(i).getNode()->hasOneUse())
5799 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 else
Chris Lattner397f4562009-08-23 06:03:38 +00005801 errs() << "\n" << std::string(indent+2, ' ')
5802 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005803
5804
Chris Lattner397f4562009-08-23 06:03:38 +00005805 errs() << "\n";
5806 errs().indent(indent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005807 N->dump(G);
5808}
5809
Mon P Wangc707f3f2009-11-30 02:42:02 +00005810SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5811 assert(N->getNumValues() == 1 &&
5812 "Can't unroll a vector with multiple results!");
5813
5814 EVT VT = N->getValueType(0);
5815 unsigned NE = VT.getVectorNumElements();
5816 EVT EltVT = VT.getVectorElementType();
5817 DebugLoc dl = N->getDebugLoc();
5818
5819 SmallVector<SDValue, 8> Scalars;
5820 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5821
5822 // If ResNE is 0, fully unroll the vector op.
5823 if (ResNE == 0)
5824 ResNE = NE;
5825 else if (NE > ResNE)
5826 NE = ResNE;
5827
5828 unsigned i;
5829 for (i= 0; i != NE; ++i) {
5830 for (unsigned j = 0; j != N->getNumOperands(); ++j) {
5831 SDValue Operand = N->getOperand(j);
5832 EVT OperandVT = Operand.getValueType();
5833 if (OperandVT.isVector()) {
5834 // A vector operand; extract a single element.
5835 EVT OperandEltVT = OperandVT.getVectorElementType();
5836 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5837 OperandEltVT,
5838 Operand,
5839 getConstant(i, MVT::i32));
5840 } else {
5841 // A scalar operand; just use it as is.
5842 Operands[j] = Operand;
5843 }
5844 }
5845
5846 switch (N->getOpcode()) {
5847 default:
5848 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5849 &Operands[0], Operands.size()));
5850 break;
5851 case ISD::SHL:
5852 case ISD::SRA:
5853 case ISD::SRL:
5854 case ISD::ROTL:
5855 case ISD::ROTR:
5856 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
5857 getShiftAmountOperand(Operands[1])));
5858 break;
5859 }
5860 }
5861
5862 for (; i < ResNE; ++i)
5863 Scalars.push_back(getUNDEF(EltVT));
5864
5865 return getNode(ISD::BUILD_VECTOR, dl,
5866 EVT::getVectorVT(*getContext(), EltVT, ResNE),
5867 &Scalars[0], Scalars.size());
5868}
5869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005870void SelectionDAG::dump() const {
Chris Lattner397f4562009-08-23 06:03:38 +00005871 errs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
Scott Michel91099d62009-02-17 22:15:04 +00005872
Dan Gohman5fafd422008-07-15 18:18:54 +00005873 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5874 I != E; ++I) {
5875 const SDNode *N = I;
Gabor Greif1c80d112008-08-28 21:40:38 +00005876 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman5fafd422008-07-15 18:18:54 +00005877 DumpNodes(N, 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 }
5879
Gabor Greif1c80d112008-08-28 21:40:38 +00005880 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005881
Chris Lattner397f4562009-08-23 06:03:38 +00005882 errs() << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005883}
5884
Stuart Hastingsc6250282009-02-04 16:46:19 +00005885void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
5886 print_types(OS, G);
5887 print_details(OS, G);
5888}
5889
5890typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
Stuart Hastings620a4d12009-02-04 20:30:10 +00005891static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
Bill Wendlingc1946742009-05-30 01:09:53 +00005892 const SelectionDAG *G, VisitedSDNodeSet &once) {
5893 if (!once.insert(N)) // If we've been here before, return now.
Stuart Hastingsc6250282009-02-04 16:46:19 +00005894 return;
5895 // Dump the current SDNode, but don't end the line yet.
5896 OS << std::string(indent, ' ');
5897 N->printr(OS, G);
5898 // Having printed this SDNode, walk the children:
5899 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5900 const SDNode *child = N->getOperand(i).getNode();
5901 if (i) OS << ",";
5902 OS << " ";
5903 if (child->getNumOperands() == 0) {
5904 // This child has no grandchildren; print it inline right here.
5905 child->printr(OS, G);
5906 once.insert(child);
Bill Wendlingc1946742009-05-30 01:09:53 +00005907 } else { // Just the address. FIXME: also print the child's opcode
Stuart Hastingsc6250282009-02-04 16:46:19 +00005908 OS << (void*)child;
5909 if (unsigned RN = N->getOperand(i).getResNo())
Bill Wendlingc1946742009-05-30 01:09:53 +00005910 OS << ":" << RN;
Stuart Hastingsc6250282009-02-04 16:46:19 +00005911 }
5912 }
5913 OS << "\n";
5914 // Dump children that have grandchildren on their own line(s).
5915 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5916 const SDNode *child = N->getOperand(i).getNode();
5917 DumpNodesr(OS, child, indent+2, G, once);
5918 }
5919}
5920
5921void SDNode::dumpr() const {
5922 VisitedSDNodeSet once;
5923 DumpNodesr(errs(), this, 0, 0, once);
Stuart Hastingsc6250282009-02-04 16:46:19 +00005924}
5925
Dan Gohman594bb862009-09-25 00:34:34 +00005926void SDNode::dumpr(const SelectionDAG *G) const {
5927 VisitedSDNodeSet once;
5928 DumpNodesr(errs(), this, 0, G, once);
5929}
5930
Sanjiv Guptababc5c42009-04-29 04:43:24 +00005931
5932// getAddressSpace - Return the address space this GlobalAddress belongs to.
5933unsigned GlobalAddressSDNode::getAddressSpace() const {
5934 return getGlobal()->getType()->getAddressSpace();
5935}
5936
5937
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005938const Type *ConstantPoolSDNode::getType() const {
5939 if (isMachineConstantPoolEntry())
5940 return Val.MachineCPVal->getType();
5941 return Val.ConstVal->getType();
5942}
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005943
Bob Wilsone6539682009-03-02 23:24:16 +00005944bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
5945 APInt &SplatUndef,
5946 unsigned &SplatBitSize,
5947 bool &HasAnyUndefs,
Dale Johannesen48fd1e42009-11-13 01:45:18 +00005948 unsigned MinSplatBits,
5949 bool isBigEndian) {
Owen Andersonac9de032009-08-10 22:56:29 +00005950 EVT VT = getValueType(0);
Bob Wilsone6539682009-03-02 23:24:16 +00005951 assert(VT.isVector() && "Expected a vector type");
5952 unsigned sz = VT.getSizeInBits();
5953 if (MinSplatBits > sz)
5954 return false;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005955
Bob Wilsone6539682009-03-02 23:24:16 +00005956 SplatValue = APInt(sz, 0);
5957 SplatUndef = APInt(sz, 0);
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005958
Bob Wilsone6539682009-03-02 23:24:16 +00005959 // Get the bits. Bits with undefined values (when the corresponding element
5960 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
5961 // in SplatValue. If any of the values are not constant, give up and return
5962 // false.
5963 unsigned int nOps = getNumOperands();
5964 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
5965 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen48fd1e42009-11-13 01:45:18 +00005966
5967 for (unsigned j = 0; j < nOps; ++j) {
5968 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005969 SDValue OpVal = getOperand(i);
Dale Johannesen48fd1e42009-11-13 01:45:18 +00005970 unsigned BitPos = j * EltBitSize;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005971
Bob Wilsone6539682009-03-02 23:24:16 +00005972 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen48fd1e42009-11-13 01:45:18 +00005973 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilsone6539682009-03-02 23:24:16 +00005974 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Bob Wilson2b570bb2009-04-13 22:05:19 +00005975 SplatValue |= (APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
5976 zextOrTrunc(sz) << BitPos);
Bob Wilsone6539682009-03-02 23:24:16 +00005977 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilson0e94e912009-03-04 17:47:01 +00005978 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilsone6539682009-03-02 23:24:16 +00005979 else
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005980 return false;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005981 }
5982
Bob Wilsone6539682009-03-02 23:24:16 +00005983 // The build_vector is all constants or undefs. Find the smallest element
5984 // size that splats the vector.
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005985
Bob Wilsone6539682009-03-02 23:24:16 +00005986 HasAnyUndefs = (SplatUndef != 0);
5987 while (sz > 8) {
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005988
Bob Wilsone6539682009-03-02 23:24:16 +00005989 unsigned HalfSize = sz / 2;
5990 APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
5991 APInt LowValue = APInt(SplatValue).trunc(HalfSize);
5992 APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
5993 APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005994
Bob Wilsone6539682009-03-02 23:24:16 +00005995 // If the two halves do not match (ignoring undef bits), stop here.
5996 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
5997 MinSplatBits > HalfSize)
5998 break;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00005999
Bob Wilsone6539682009-03-02 23:24:16 +00006000 SplatValue = HighValue | LowValue;
6001 SplatUndef = HighUndef & LowUndef;
Eric Christopher440d9eb2009-08-22 00:40:45 +00006002
Bob Wilsone6539682009-03-02 23:24:16 +00006003 sz = HalfSize;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006004 }
6005
Bob Wilsone6539682009-03-02 23:24:16 +00006006 SplatBitSize = sz;
Bob Wilsonb6fc1fb2009-03-01 01:13:55 +00006007 return true;
6008}
Nate Begeman543d2142009-04-27 18:41:29 +00006009
Owen Andersonac9de032009-08-10 22:56:29 +00006010bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00006011 // Find the first non-undef value in the shuffle mask.
6012 unsigned i, e;
6013 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6014 /* search */;
6015
Nate Begeman4da76202009-04-29 18:13:31 +00006016 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher440d9eb2009-08-22 00:40:45 +00006017
Nate Begemane8f61cb2009-04-29 05:20:52 +00006018 // Make sure all remaining elements are either undef or the same as the first
6019 // non-undef value.
6020 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00006021 if (Mask[i] >= 0 && Mask[i] != Idx)
6022 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00006023 return true;
6024}
Mon P Wangc707f3f2009-11-30 02:42:02 +00006025