blob: 851e6c040ea016e4185850f5e69d6a4884e836f9 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +000013#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000014#include "llvm/Constants.h"
Evan Cheng0ff39b32008-06-30 07:31:25 +000015#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000016#include "llvm/Function.h"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000017#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000018#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000019#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000020#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000022#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000028#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000029#include "llvm/Target/TargetData.h"
Evan Chengf2dc5c72009-12-09 01:04:59 +000030#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000031#include "llvm/Target/TargetLowering.h"
Dan Gohman760f86f2009-01-22 21:58:43 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000033#include "llvm/Target/TargetInstrInfo.h"
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +000034#include "llvm/Target/TargetIntrinsicInfo.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000035#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000036#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000038#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000039#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/raw_ostream.h"
Owen Andersonb4459082009-06-25 17:09:00 +000041#include "llvm/System/Mutex.h"
Chris Lattner012f2412006-02-17 21:58:01 +000042#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000043#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000044#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000045#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000046#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000047#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000048#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000049using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000050
Chris Lattner0b3e5252006-08-15 19:11:05 +000051/// makeVTList - Return an instance of the SDVTList struct initialized with the
52/// specified members.
Owen Andersone50ed302009-08-10 22:56:29 +000053static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000054 SDVTList Res = {VTs, NumVTs};
55 return Res;
56}
57
Owen Andersone50ed302009-08-10 22:56:29 +000058static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +000059 switch (VT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +000060 default: llvm_unreachable("Unknown FP format");
Owen Anderson825b72b2009-08-11 20:47:22 +000061 case MVT::f32: return &APFloat::IEEEsingle;
62 case MVT::f64: return &APFloat::IEEEdouble;
63 case MVT::f80: return &APFloat::x87DoubleExtended;
64 case MVT::f128: return &APFloat::IEEEquad;
65 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
Chris Lattnerec4a5672008-03-05 06:48:13 +000066 }
67}
68
Chris Lattnerf8dc0612008-02-03 06:49:24 +000069SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
70
Jim Laskey58b968b2005-08-17 20:08:02 +000071//===----------------------------------------------------------------------===//
72// ConstantFPSDNode Class
73//===----------------------------------------------------------------------===//
74
75/// isExactlyValue - We don't rely on operator== working on double values, as
76/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
77/// As such, this method can be used to do an exact bit-for-bit comparison of
78/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000079bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000080 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000081}
82
Owen Andersone50ed302009-08-10 22:56:29 +000083bool ConstantFPSDNode::isValueValidForType(EVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000084 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000085 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000086
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000087 // PPC long double cannot be converted to any other type.
Owen Anderson825b72b2009-08-11 20:47:22 +000088 if (VT == MVT::ppcf128 ||
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000089 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000090 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000091
Dale Johannesenf04afdb2007-08-30 00:23:21 +000092 // convert modifies in place, so make a copy.
93 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000094 bool losesInfo;
Owen Andersone50ed302009-08-10 22:56:29 +000095 (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
Dale Johannesen23a98552008-10-09 23:00:39 +000096 &losesInfo);
97 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000098}
99
Jim Laskey58b968b2005-08-17 20:08:02 +0000100//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +0000101// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +0000102//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +0000103
Evan Chenga8df1662006-03-27 06:58:47 +0000104/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +0000105/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000106bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000107 // Look through a bit convert.
108 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000109 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Evan Chenga8df1662006-03-27 06:58:47 +0000111 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattner61d43992006-03-25 22:57:01 +0000113 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000114
Chris Lattner61d43992006-03-25 22:57:01 +0000115 // Skip over all of the undef values.
116 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
117 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Chris Lattner61d43992006-03-25 22:57:01 +0000119 // Do not accept an all-undef vector.
120 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000121
Chris Lattner61d43992006-03-25 22:57:01 +0000122 // Do not accept build_vectors that aren't all constants or which have non-~0
123 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000124 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000125 if (isa<ConstantSDNode>(NotZero)) {
126 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
127 return false;
128 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000129 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen23a98552008-10-09 23:00:39 +0000130 bitcastToAPInt().isAllOnesValue())
Dan Gohman6c231502008-02-29 01:47:35 +0000131 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000132 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000133 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000134
Chris Lattner61d43992006-03-25 22:57:01 +0000135 // Okay, we have at least one ~0 value, check to see if the rest match or are
136 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000137 for (++i; i != e; ++i)
138 if (N->getOperand(i) != NotZero &&
139 N->getOperand(i).getOpcode() != ISD::UNDEF)
140 return false;
141 return true;
142}
143
144
Evan Cheng4a147842006-03-26 09:50:58 +0000145/// isBuildVectorAllZeros - Return true if the specified node is a
146/// BUILD_VECTOR where all of the elements are 0 or undef.
147bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000148 // Look through a bit convert.
149 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000150 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Evan Cheng4a147842006-03-26 09:50:58 +0000152 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000153
Evan Chenga8df1662006-03-27 06:58:47 +0000154 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Evan Chenga8df1662006-03-27 06:58:47 +0000156 // Skip over all of the undef values.
157 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
158 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000159
Evan Chenga8df1662006-03-27 06:58:47 +0000160 // Do not accept an all-undef vector.
161 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000162
Dan Gohman68f32cb2009-06-04 16:49:15 +0000163 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000164 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000166 if (isa<ConstantSDNode>(Zero)) {
167 if (!cast<ConstantSDNode>(Zero)->isNullValue())
168 return false;
169 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000170 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000171 return false;
172 } else
173 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000174
Dan Gohman68f32cb2009-06-04 16:49:15 +0000175 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000176 // undefs.
177 for (++i; i != e; ++i)
178 if (N->getOperand(i) != Zero &&
179 N->getOperand(i).getOpcode() != ISD::UNDEF)
180 return false;
181 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000182}
183
Evan Chengefec7512008-02-18 23:04:32 +0000184/// isScalarToVector - Return true if the specified node is a
185/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
186/// element is not an undef.
187bool ISD::isScalarToVector(const SDNode *N) {
188 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
189 return true;
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR)
192 return false;
193 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
194 return false;
195 unsigned NumElems = N->getNumOperands();
196 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000197 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000198 if (V.getOpcode() != ISD::UNDEF)
199 return false;
200 }
201 return true;
202}
203
Chris Lattnerc3aae252005-01-07 07:46:32 +0000204/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
205/// when given the operation for (X op Y).
206ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
207 // To perform this operation, we just need to swap the L and G bits of the
208 // operation.
209 unsigned OldL = (Operation >> 2) & 1;
210 unsigned OldG = (Operation >> 1) & 1;
211 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
212 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000213 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000214}
215
216/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
217/// 'op' is a valid SetCC operation.
218ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
219 unsigned Operation = Op;
220 if (isInteger)
221 Operation ^= 7; // Flip L, G, E bits, but not U.
222 else
223 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000224
Chris Lattnerc3aae252005-01-07 07:46:32 +0000225 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000226 Operation &= ~8; // Don't let N and U bits get set.
227
Chris Lattnerc3aae252005-01-07 07:46:32 +0000228 return ISD::CondCode(Operation);
229}
230
231
232/// isSignedOp - For an integer comparison, return 1 if the comparison is a
233/// signed operation and 2 if the result is an unsigned comparison. Return zero
234/// if the operation does not depend on the sign of the input (setne and seteq).
235static int isSignedOp(ISD::CondCode Opcode) {
236 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000237 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238 case ISD::SETEQ:
239 case ISD::SETNE: return 0;
240 case ISD::SETLT:
241 case ISD::SETLE:
242 case ISD::SETGT:
243 case ISD::SETGE: return 1;
244 case ISD::SETULT:
245 case ISD::SETULE:
246 case ISD::SETUGT:
247 case ISD::SETUGE: return 2;
248 }
249}
250
251/// getSetCCOrOperation - Return the result of a logical OR between different
252/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
253/// returns SETCC_INVALID if it is not possible to represent the resultant
254/// comparison.
255ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
256 bool isInteger) {
257 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
258 // Cannot fold a signed integer setcc with an unsigned integer setcc.
259 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000260
Chris Lattnerc3aae252005-01-07 07:46:32 +0000261 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattnerc3aae252005-01-07 07:46:32 +0000263 // If the N and U bits get set then the resultant comparison DOES suddenly
264 // care about orderedness, and is true when ordered.
265 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000266 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000267
Chris Lattnere41102b2006-05-12 17:03:46 +0000268 // Canonicalize illegal integer setcc's.
269 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
270 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000271
Chris Lattnerc3aae252005-01-07 07:46:32 +0000272 return ISD::CondCode(Op);
273}
274
275/// getSetCCAndOperation - Return the result of a logical AND between different
276/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
277/// function returns zero if it is not possible to represent the resultant
278/// comparison.
279ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
280 bool isInteger) {
281 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
282 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000283 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284
285 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000286 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000287
Chris Lattnera83385f2006-04-27 05:01:07 +0000288 // Canonicalize illegal integer setcc's.
289 if (isInteger) {
290 switch (Result) {
291 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000292 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000293 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000294 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
295 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
296 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000297 }
298 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000299
Chris Lattnera83385f2006-04-27 05:01:07 +0000300 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301}
302
Chris Lattnerb48da392005-01-23 04:39:44 +0000303const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000304 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000305}
306
Jim Laskey58b968b2005-08-17 20:08:02 +0000307//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000308// SDNode Profile Support
309//===----------------------------------------------------------------------===//
310
Jim Laskeydef69b92006-10-27 23:52:51 +0000311/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
312///
Jim Laskey583bd472006-10-27 23:46:08 +0000313static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
314 ID.AddInteger(OpC);
315}
316
317/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
318/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000319static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000320 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000321}
322
Jim Laskeydef69b92006-10-27 23:52:51 +0000323/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
324///
Jim Laskey583bd472006-10-27 23:46:08 +0000325static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000326 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000327 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000328 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000329 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000330 }
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
335static void AddNodeIDOperands(FoldingSetNodeID &ID,
336 const SDUse *Ops, unsigned NumOps) {
337 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000338 ID.AddPointer(Ops->getNode());
339 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000340 }
341}
342
Jim Laskey583bd472006-10-27 23:46:08 +0000343static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000344 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000345 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000346 AddNodeIDOpcode(ID, OpC);
347 AddNodeIDValueTypes(ID, VTList);
348 AddNodeIDOperands(ID, OpList, N);
349}
350
Duncan Sands0dc40452008-10-27 15:30:53 +0000351/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
352/// the NodeID data.
353static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000354 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000355 case ISD::TargetExternalSymbol:
356 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000357 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000358 default: break; // Normal nodes don't need extra info.
359 case ISD::TargetConstant:
360 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000361 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000362 break;
363 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000364 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000365 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000366 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000367 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000369 case ISD::GlobalAddress:
370 case ISD::TargetGlobalTLSAddress:
371 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000372 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000373 ID.AddPointer(GA->getGlobal());
374 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000375 ID.AddInteger(GA->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 break;
377 }
378 case ISD::BasicBlock:
379 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
380 break;
381 case ISD::Register:
382 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
383 break;
Devang Patelbef88882009-11-21 02:46:55 +0000384
Dan Gohman69de1932008-02-06 22:27:42 +0000385 case ISD::SRCVALUE:
386 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
387 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000388 case ISD::FrameIndex:
389 case ISD::TargetFrameIndex:
390 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
391 break;
392 case ISD::JumpTable:
393 case ISD::TargetJumpTable:
394 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000395 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000396 break;
397 case ISD::ConstantPool:
398 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000399 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000400 ID.AddInteger(CP->getAlignment());
401 ID.AddInteger(CP->getOffset());
402 if (CP->isMachineConstantPoolEntry())
403 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
404 else
405 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000406 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000407 break;
408 }
409 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000410 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000411 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000412 ID.AddInteger(LD->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000413 break;
414 }
415 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000416 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000417 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000418 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000419 break;
420 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000421 case ISD::ATOMIC_CMP_SWAP:
422 case ISD::ATOMIC_SWAP:
423 case ISD::ATOMIC_LOAD_ADD:
424 case ISD::ATOMIC_LOAD_SUB:
425 case ISD::ATOMIC_LOAD_AND:
426 case ISD::ATOMIC_LOAD_OR:
427 case ISD::ATOMIC_LOAD_XOR:
428 case ISD::ATOMIC_LOAD_NAND:
429 case ISD::ATOMIC_LOAD_MIN:
430 case ISD::ATOMIC_LOAD_MAX:
431 case ISD::ATOMIC_LOAD_UMIN:
432 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000433 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000434 ID.AddInteger(AT->getMemoryVT().getRawBits());
435 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang28873102008-06-25 08:15:39 +0000436 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000437 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000438 case ISD::VECTOR_SHUFFLE: {
439 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000440 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +0000441 i != e; ++i)
442 ID.AddInteger(SVN->getMaskElt(i));
443 break;
444 }
Dan Gohman8c2b5252009-10-30 01:27:03 +0000445 case ISD::TargetBlockAddress:
446 case ISD::BlockAddress: {
Dan Gohman29cbade2009-11-20 23:18:13 +0000447 ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
448 ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
Dan Gohman8c2b5252009-10-30 01:27:03 +0000449 break;
450 }
Mon P Wang28873102008-06-25 08:15:39 +0000451 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000452}
453
Duncan Sands0dc40452008-10-27 15:30:53 +0000454/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
455/// data.
456static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
457 AddNodeIDOpcode(ID, N->getOpcode());
458 // Add the return value info.
459 AddNodeIDValueTypes(ID, N->getVTList());
460 // Add the operand info.
461 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
462
463 // Handle SDNode leafs with special info.
464 AddNodeIDCustom(ID, N);
465}
466
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000467/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohmanc76909a2009-09-25 20:36:54 +0000468/// the CSE map that carries volatility, indexing mode, and
Dan Gohmana7ce7412009-02-03 00:08:45 +0000469/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000470///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000471static inline unsigned
Dan Gohmanc76909a2009-09-25 20:36:54 +0000472encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile) {
Dan Gohmana7ce7412009-02-03 00:08:45 +0000473 assert((ConvType & 3) == ConvType &&
474 "ConvType may not require more than 2 bits!");
475 assert((AM & 7) == AM &&
476 "AM may not require more than 3 bits!");
477 return ConvType |
478 (AM << 2) |
Dan Gohmanc76909a2009-09-25 20:36:54 +0000479 (isVolatile << 5);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000480}
481
Jim Laskey583bd472006-10-27 23:46:08 +0000482//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000483// SelectionDAG Class
484//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000485
Duncan Sands0dc40452008-10-27 15:30:53 +0000486/// doNotCSE - Return true if CSE should not be performed for this node.
487static bool doNotCSE(SDNode *N) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 if (N->getValueType(0) == MVT::Flag)
Duncan Sands0dc40452008-10-27 15:30:53 +0000489 return true; // Never CSE anything that produces a flag.
490
491 switch (N->getOpcode()) {
492 default: break;
493 case ISD::HANDLENODE:
Duncan Sands0dc40452008-10-27 15:30:53 +0000494 case ISD::EH_LABEL:
Duncan Sands0dc40452008-10-27 15:30:53 +0000495 return true; // Never CSE these nodes.
496 }
497
498 // Check that remaining values produced are not flags.
499 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +0000500 if (N->getValueType(i) == MVT::Flag)
Duncan Sands0dc40452008-10-27 15:30:53 +0000501 return true; // Never CSE anything that produces a flag.
502
503 return false;
504}
505
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000506/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000507/// SelectionDAG.
508void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000509 // Create a dummy node (which is not added to allnodes), that adds a reference
510 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000511 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000512
Chris Lattner190a4182006-08-04 17:45:20 +0000513 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000514
Chris Lattner190a4182006-08-04 17:45:20 +0000515 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000516 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000517 if (I->use_empty())
518 DeadNodes.push_back(I);
519
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000520 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000521
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000522 // If the root changed (e.g. it was a dead load, update the root).
523 setRoot(Dummy.getValue());
524}
525
526/// RemoveDeadNodes - This method deletes the unreachable nodes in the
527/// given list, and any nodes that become unreachable as a result.
528void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
529 DAGUpdateListener *UpdateListener) {
530
Chris Lattner190a4182006-08-04 17:45:20 +0000531 // Process the worklist, deleting the nodes and adding their uses to the
532 // worklist.
533 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000534 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000535
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000536 if (UpdateListener)
537 UpdateListener->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000538
Chris Lattner190a4182006-08-04 17:45:20 +0000539 // Take the node out of the appropriate CSE map.
540 RemoveNodeFromCSEMaps(N);
541
542 // Next, brutally remove the operand list. This is safe to do, as there are
543 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000544 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
545 SDUse &Use = *I++;
546 SDNode *Operand = Use.getNode();
547 Use.set(SDValue());
548
Chris Lattner190a4182006-08-04 17:45:20 +0000549 // Now that we removed this operand, see if there are no uses of it left.
550 if (Operand->use_empty())
551 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000552 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000553
Dan Gohmanc5336122009-01-19 22:39:36 +0000554 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000555 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000556}
557
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000558void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000559 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000560 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000561}
562
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000563void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000564 // First take this out of the appropriate CSE map.
565 RemoveNodeFromCSEMaps(N);
566
Scott Michelfdc40a02009-02-17 22:15:04 +0000567 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000568 // AllNodes list, and delete the node.
569 DeleteNodeNotInCSEMaps(N);
570}
571
572void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000573 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
574 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000575
Dan Gohmanf06c8352008-09-30 18:30:35 +0000576 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000577 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000578
Dan Gohmanc5336122009-01-19 22:39:36 +0000579 DeallocateNode(N);
580}
581
582void SelectionDAG::DeallocateNode(SDNode *N) {
583 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000584 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000585
Dan Gohmanc5336122009-01-19 22:39:36 +0000586 // Set the opcode to DELETED_NODE to help catch bugs when node
587 // memory is reallocated.
588 N->NodeType = ISD::DELETED_NODE;
589
Dan Gohman11467282008-08-26 01:44:34 +0000590 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000591}
592
Chris Lattner149c58c2005-08-16 18:17:10 +0000593/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
594/// correspond to it. This is useful when we're about to delete or repurpose
595/// the node. We don't want future request for structurally identical nodes
596/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000597bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000598 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000599 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000600 case ISD::EntryToken:
Torok Edwinc23197a2009-07-14 16:55:14 +0000601 llvm_unreachable("EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000602 return false;
603 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000604 case ISD::CONDCODE:
605 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
606 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000607 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000608 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
609 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000610 case ISD::ExternalSymbol:
611 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000612 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000613 case ISD::TargetExternalSymbol: {
614 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
615 Erased = TargetExternalSymbols.erase(
616 std::pair<std::string,unsigned char>(ESN->getSymbol(),
617 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000618 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000619 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000620 case ISD::VALUETYPE: {
Owen Andersone50ed302009-08-10 22:56:29 +0000621 EVT VT = cast<VTSDNode>(N)->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000622 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000623 Erased = ExtendedValueTypeNodes.erase(VT);
624 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
626 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000627 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000628 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000629 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000630 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000631 // Remove it from the CSE Map.
632 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000633 break;
634 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000635#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000636 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000637 // flag result (which cannot be CSE'd) or is one of the special cases that are
638 // not subject to CSE.
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000640 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000641 N->dump(this);
Chris Lattner45cfe542009-08-23 06:03:38 +0000642 errs() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000643 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000644 }
645#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000646 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000647}
648
Dan Gohman39946102009-01-25 16:29:12 +0000649/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
650/// maps and modified in place. Add it back to the CSE maps, unless an identical
651/// node already exists, in which case transfer all its users to the existing
652/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000653///
Dan Gohman39946102009-01-25 16:29:12 +0000654void
655SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
656 DAGUpdateListener *UpdateListener) {
657 // For node types that aren't CSE'd, just act as if no identical node
658 // already exists.
659 if (!doNotCSE(N)) {
660 SDNode *Existing = CSEMap.GetOrInsertNode(N);
661 if (Existing != N) {
662 // If there was already an existing matching node, use ReplaceAllUsesWith
663 // to replace the dead one with the existing one. This can cause
664 // recursive merging of other unrelated nodes down the line.
665 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000666
Dan Gohman39946102009-01-25 16:29:12 +0000667 // N is now dead. Inform the listener if it exists and delete it.
Scott Michelfdc40a02009-02-17 22:15:04 +0000668 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000669 UpdateListener->NodeDeleted(N, Existing);
670 DeleteNodeNotInCSEMaps(N);
671 return;
672 }
673 }
Evan Cheng71e86852008-07-08 20:06:39 +0000674
Dan Gohman39946102009-01-25 16:29:12 +0000675 // If the node doesn't already exist, we updated it. Inform a listener if
676 // it exists.
Scott Michelfdc40a02009-02-17 22:15:04 +0000677 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000678 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000679}
680
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000681/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000682/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000683/// return null, otherwise return a pointer to the slot it would take. If a
684/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000685SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000686 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000687 if (doNotCSE(N))
688 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000689
Dan Gohman475871a2008-07-27 21:46:04 +0000690 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000691 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000692 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000693 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000694 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000695}
696
697/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000698/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000699/// return null, otherwise return a pointer to the slot it would take. If a
700/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000701SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000702 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000703 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000704 if (doNotCSE(N))
705 return 0;
706
Dan Gohman475871a2008-07-27 21:46:04 +0000707 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000708 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000709 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000710 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000711 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
712}
713
714
715/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000716/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000717/// return null, otherwise return a pointer to the slot it would take. If a
718/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000719SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000720 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000721 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000722 if (doNotCSE(N))
723 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000724
Jim Laskey583bd472006-10-27 23:46:08 +0000725 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000726 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000727 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000728 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000729}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000730
Duncan Sandsd038e042008-07-21 10:20:31 +0000731/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
732void SelectionDAG::VerifyNode(SDNode *N) {
733 switch (N->getOpcode()) {
734 default:
735 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000736 case ISD::BUILD_PAIR: {
Owen Andersone50ed302009-08-10 22:56:29 +0000737 EVT VT = N->getValueType(0);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000738 assert(N->getNumValues() == 1 && "Too many results!");
739 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
740 "Wrong return type!");
741 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
742 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
743 "Mismatched operand types!");
744 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
745 "Wrong operand type!");
746 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
747 "Wrong return type size");
748 break;
749 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000750 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000751 assert(N->getNumValues() == 1 && "Too many results!");
752 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000753 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000754 "Wrong number of operands!");
Owen Andersone50ed302009-08-10 22:56:29 +0000755 EVT EltVT = N->getValueType(0).getVectorElementType();
Rafael Espindola15684b22009-04-24 12:40:33 +0000756 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
757 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000758 (EltVT.isInteger() && I->getValueType().isInteger() &&
759 EltVT.bitsLE(I->getValueType()))) &&
760 "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000761 break;
762 }
763 }
764}
765
Owen Andersone50ed302009-08-10 22:56:29 +0000766/// getEVTAlignment - Compute the default alignment value for the
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000767/// given type.
768///
Owen Andersone50ed302009-08-10 22:56:29 +0000769unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 const Type *Ty = VT == MVT::iPTR ?
Owen Anderson1d0be152009-08-13 21:58:54 +0000771 PointerType::get(Type::getInt8Ty(*getContext()), 0) :
Owen Anderson23b9b192009-08-12 00:36:31 +0000772 VT.getTypeForEVT(*getContext());
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000773
774 return TLI.getTargetData()->getABITypeAlignment(Ty);
775}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000776
Dale Johannesen92570c42009-02-07 02:15:05 +0000777// EntryNode could meaningfully have debug info if we can find it...
Dan Gohman7c3234c2008-08-27 23:52:12 +0000778SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Devang Patelb51d40c2009-02-03 18:46:32 +0000779 : TLI(tli), FLI(fli), DW(0),
Dale Johannesen92570c42009-02-07 02:15:05 +0000780 EntryNode(ISD::EntryToken, DebugLoc::getUnknownLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 getVTList(MVT::Other)), Root(getEntryNode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000782 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000783}
784
Devang Patel6e7a1612009-01-09 19:11:50 +0000785void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
Owen Anderson5dcaceb2009-07-09 18:44:09 +0000786 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000787 MF = &mf;
788 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000789 DW = dw;
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000790 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000791}
792
Chris Lattner78ec3112003-08-11 14:57:33 +0000793SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000794 allnodes_clear();
795}
796
797void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000798 assert(&*AllNodes.begin() == &EntryNode);
799 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000800 while (!AllNodes.empty())
801 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000802}
803
Dan Gohman7c3234c2008-08-27 23:52:12 +0000804void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000805 allnodes_clear();
806 OperandAllocator.Reset();
807 CSEMap.clear();
808
809 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000810 ExternalSymbols.clear();
811 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000812 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
813 static_cast<CondCodeSDNode*>(0));
814 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
815 static_cast<SDNode*>(0));
816
Dan Gohmane7852d02009-01-26 04:35:06 +0000817 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000818 AllNodes.push_back(&EntryNode);
819 Root = getEntryNode();
820}
821
Duncan Sands3a66a682009-10-13 21:04:12 +0000822SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
823 return VT.bitsGT(Op.getValueType()) ?
824 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
825 getNode(ISD::TRUNCATE, DL, VT, Op);
826}
827
828SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
829 return VT.bitsGT(Op.getValueType()) ?
830 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
831 getNode(ISD::TRUNCATE, DL, VT, Op);
832}
833
Owen Andersone50ed302009-08-10 22:56:29 +0000834SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
Bill Wendling6ce610f2009-01-30 22:23:15 +0000835 if (Op.getValueType() == VT) return Op;
836 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
837 VT.getSizeInBits());
838 return getNode(ISD::AND, DL, Op.getValueType(), Op,
839 getConstant(Imm, Op.getValueType()));
840}
841
Bob Wilson4c245462009-01-22 17:39:32 +0000842/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
843///
Owen Andersone50ed302009-08-10 22:56:29 +0000844SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
845 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000846 SDValue NegOne =
847 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000848 return getNode(ISD::XOR, DL, VT, Val, NegOne);
849}
850
Owen Andersone50ed302009-08-10 22:56:29 +0000851SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
852 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmance9bc122009-01-27 20:39:34 +0000853 assert((EltVT.getSizeInBits() >= 64 ||
854 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
855 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000856 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000857}
858
Owen Andersone50ed302009-08-10 22:56:29 +0000859SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000860 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000861}
862
Owen Andersone50ed302009-08-10 22:56:29 +0000863SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000865
Owen Andersone50ed302009-08-10 22:56:29 +0000866 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000868 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000869
870 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000871 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000872 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000873 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000874 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000875 SDNode *N = NULL;
876 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000878 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000879 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000880 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000881 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000882 CSEMap.InsertNode(N, IP);
883 AllNodes.push_back(N);
884 }
885
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000887 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000888 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000889 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +0000890 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
891 VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +0000892 }
893 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000894}
895
Dan Gohman475871a2008-07-27 21:46:04 +0000896SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000897 return getConstant(Val, TLI.getPointerTy(), isTarget);
898}
899
900
Owen Andersone50ed302009-08-10 22:56:29 +0000901SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000902 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000903}
904
Owen Andersone50ed302009-08-10 22:56:29 +0000905SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000906 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Owen Andersone50ed302009-08-10 22:56:29 +0000908 EVT EltVT =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000909 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910
Chris Lattnerd8658612005-02-17 20:17:32 +0000911 // Do the map lookup using the actual bit pattern for the floating point
912 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
913 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000914 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000915 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000916 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000917 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000918 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000919 SDNode *N = NULL;
920 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000921 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000922 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000923 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000924 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000925 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000926 CSEMap.InsertNode(N, IP);
927 AllNodes.push_back(N);
928 }
929
Dan Gohman475871a2008-07-27 21:46:04 +0000930 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000931 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000932 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000933 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +0000934 // FIXME DebugLoc info might be appropriate here
935 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
936 VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +0000937 }
938 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939}
940
Owen Andersone50ed302009-08-10 22:56:29 +0000941SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
942 EVT EltVT =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000943 VT.isVector() ? VT.getVectorElementType() : VT;
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 if (EltVT==MVT::f32)
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000945 return getConstantFP(APFloat((float)Val), VT, isTarget);
946 else
947 return getConstantFP(APFloat(Val), VT, isTarget);
948}
949
Dan Gohman475871a2008-07-27 21:46:04 +0000950SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Owen Andersone50ed302009-08-10 22:56:29 +0000951 EVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +0000952 bool isTargetGA,
953 unsigned char TargetFlags) {
954 assert((TargetFlags == 0 || isTargetGA) &&
955 "Cannot set target flags on target-independent globals");
Eric Christopher4d7c18c2009-08-22 00:40:45 +0000956
Dan Gohman6520e202008-10-18 02:06:02 +0000957 // Truncate (with sign-extension) the offset value to the pointer size.
Owen Andersone50ed302009-08-10 22:56:29 +0000958 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +0000959 unsigned BitWidth = PTy.getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000960 if (BitWidth < 64)
961 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
962
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000963 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
964 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000965 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000966 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000967 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000968 }
969
Chris Lattner2a4ed822009-06-25 21:21:14 +0000970 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000971 if (GVar && GVar->isThreadLocal())
972 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
973 else
974 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000975
Jim Laskey583bd472006-10-27 23:46:08 +0000976 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000977 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000978 ID.AddPointer(GV);
979 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +0000980 ID.AddInteger(TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +0000981 void *IP = 0;
982 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +0000983 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000984 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Chris Lattner505e9822009-06-26 21:14:05 +0000985 new (N) GlobalAddressSDNode(Opc, GV, VT, Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +0000986 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000988 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989}
990
Owen Andersone50ed302009-08-10 22:56:29 +0000991SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000992 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000993 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000994 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000995 ID.AddInteger(FI);
996 void *IP = 0;
997 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000998 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000999 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001000 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001001 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001002 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001003 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001004}
1005
Owen Andersone50ed302009-08-10 22:56:29 +00001006SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001007 unsigned char TargetFlags) {
1008 assert((TargetFlags == 0 || isTarget) &&
1009 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001010 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001011 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001012 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001013 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001014 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001015 void *IP = 0;
1016 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001017 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001018 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001019 new (N) JumpTableSDNode(JTI, VT, isTarget, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001020 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001021 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001022 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001023}
1024
Owen Andersone50ed302009-08-10 22:56:29 +00001025SDValue SelectionDAG::getConstantPool(Constant *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001026 unsigned Alignment, int Offset,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001027 bool isTarget,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001028 unsigned char TargetFlags) {
1029 assert((TargetFlags == 0 || isTarget) &&
1030 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001031 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001032 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001033 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001034 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001035 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001036 ID.AddInteger(Alignment);
1037 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001038 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001039 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001040 void *IP = 0;
1041 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001042 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001043 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001044 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001045 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001046 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001047 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001048}
1049
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050
Owen Andersone50ed302009-08-10 22:56:29 +00001051SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00001052 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001053 bool isTarget,
1054 unsigned char TargetFlags) {
1055 assert((TargetFlags == 0 || isTarget) &&
1056 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001057 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001058 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001059 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001060 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001061 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001062 ID.AddInteger(Alignment);
1063 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001064 C->AddSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001065 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001066 void *IP = 0;
1067 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001068 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001069 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001070 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001071 CSEMap.InsertNode(N, IP);
1072 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001073 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001074}
1075
Dan Gohman475871a2008-07-27 21:46:04 +00001076SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001077 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001078 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001079 ID.AddPointer(MBB);
1080 void *IP = 0;
1081 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001082 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001083 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001084 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001085 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001087 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088}
1089
Owen Andersone50ed302009-08-10 22:56:29 +00001090SDValue SelectionDAG::getValueType(EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1092 ValueTypeNodes.size())
1093 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001094
Duncan Sands83ec4b62008-06-06 12:08:01 +00001095 SDNode *&N = VT.isExtended() ?
Owen Anderson825b72b2009-08-11 20:47:22 +00001096 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
Duncan Sandsf411b832007-10-17 13:49:58 +00001097
Dan Gohman475871a2008-07-27 21:46:04 +00001098 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001099 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001100 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001101 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001102 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001103}
1104
Owen Andersone50ed302009-08-10 22:56:29 +00001105SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
Bill Wendling056292f2008-09-16 21:48:12 +00001106 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001107 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001108 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner1af22312009-06-25 18:45:50 +00001109 new (N) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001110 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001111 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001112}
1113
Owen Andersone50ed302009-08-10 22:56:29 +00001114SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
Chris Lattner1af22312009-06-25 18:45:50 +00001115 unsigned char TargetFlags) {
1116 SDNode *&N =
1117 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1118 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001119 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001120 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner1af22312009-06-25 18:45:50 +00001121 new (N) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001123 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124}
1125
Dan Gohman475871a2008-07-27 21:46:04 +00001126SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001127 if ((unsigned)Cond >= CondCodeNodes.size())
1128 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001129
Chris Lattner079a27a2005-08-09 20:40:02 +00001130 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001131 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001132 new (N) CondCodeSDNode(Cond);
1133 CondCodeNodes[Cond] = N;
1134 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001135 }
Dan Gohman475871a2008-07-27 21:46:04 +00001136 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001137}
1138
Nate Begeman5a5ca152009-04-29 05:20:52 +00001139// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1140// the shuffle mask M that point at N1 to point at N2, and indices that point
1141// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001142static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1143 std::swap(N1, N2);
1144 int NElts = M.size();
1145 for (int i = 0; i != NElts; ++i) {
1146 if (M[i] >= NElts)
1147 M[i] -= NElts;
1148 else if (M[i] >= 0)
1149 M[i] += NElts;
1150 }
1151}
1152
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001153SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
Nate Begeman9008ca62009-04-27 18:41:29 +00001154 SDValue N2, const int *Mask) {
1155 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001156 assert(VT.isVector() && N1.getValueType().isVector() &&
Nate Begeman9008ca62009-04-27 18:41:29 +00001157 "Vector Shuffle VTs must be a vectors");
1158 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1159 && "Vector Shuffle VTs must have same element type");
1160
1161 // Canonicalize shuffle undef, undef -> undef
1162 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001163 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001164
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001165 // Validate that all indices in Mask are within the range of the elements
Nate Begeman9008ca62009-04-27 18:41:29 +00001166 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001167 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001168 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001169 for (unsigned i = 0; i != NElts; ++i) {
1170 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001171 MaskVec.push_back(Mask[i]);
1172 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001173
Nate Begeman9008ca62009-04-27 18:41:29 +00001174 // Canonicalize shuffle v, v -> v, undef
1175 if (N1 == N2) {
1176 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001177 for (unsigned i = 0; i != NElts; ++i)
1178 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001179 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001180
Nate Begeman9008ca62009-04-27 18:41:29 +00001181 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1182 if (N1.getOpcode() == ISD::UNDEF)
1183 commuteShuffle(N1, N2, MaskVec);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001184
Nate Begeman9008ca62009-04-27 18:41:29 +00001185 // Canonicalize all index into lhs, -> shuffle lhs, undef
1186 // Canonicalize all index into rhs, -> shuffle rhs, undef
1187 bool AllLHS = true, AllRHS = true;
1188 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001189 for (unsigned i = 0; i != NElts; ++i) {
1190 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001191 if (N2Undef)
1192 MaskVec[i] = -1;
1193 else
1194 AllLHS = false;
1195 } else if (MaskVec[i] >= 0) {
1196 AllRHS = false;
1197 }
1198 }
1199 if (AllLHS && AllRHS)
1200 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001201 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001202 N2 = getUNDEF(VT);
1203 if (AllRHS) {
1204 N1 = getUNDEF(VT);
1205 commuteShuffle(N1, N2, MaskVec);
1206 }
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001207
Nate Begeman9008ca62009-04-27 18:41:29 +00001208 // If Identity shuffle, or all shuffle in to undef, return that node.
1209 bool AllUndef = true;
1210 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001211 for (unsigned i = 0; i != NElts; ++i) {
1212 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001213 if (MaskVec[i] >= 0) AllUndef = false;
1214 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001215 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001216 return N1;
1217 if (AllUndef)
1218 return getUNDEF(VT);
1219
1220 FoldingSetNodeID ID;
1221 SDValue Ops[2] = { N1, N2 };
1222 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001223 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001224 ID.AddInteger(MaskVec[i]);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001225
Nate Begeman9008ca62009-04-27 18:41:29 +00001226 void* IP = 0;
1227 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1228 return SDValue(E, 0);
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001229
Nate Begeman9008ca62009-04-27 18:41:29 +00001230 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1231 // SDNode doesn't have access to it. This memory will be "leaked" when
1232 // the node is deallocated, but recovered when the NodeAllocator is released.
1233 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1234 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001235
Nate Begeman9008ca62009-04-27 18:41:29 +00001236 ShuffleVectorSDNode *N = NodeAllocator.Allocate<ShuffleVectorSDNode>();
1237 new (N) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
1238 CSEMap.InsertNode(N, IP);
1239 AllNodes.push_back(N);
1240 return SDValue(N, 0);
1241}
1242
Owen Andersone50ed302009-08-10 22:56:29 +00001243SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
Dale Johannesena04b7572009-02-03 23:04:43 +00001244 SDValue Val, SDValue DTy,
1245 SDValue STy, SDValue Rnd, SDValue Sat,
1246 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001247 // If the src and dest types are the same and the conversion is between
1248 // integer types of the same sign or two floats, no conversion is necessary.
1249 if (DTy == STy &&
1250 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001251 return Val;
1252
1253 FoldingSetNodeID ID;
Mon P Wangbdea3482009-11-07 04:46:25 +00001254 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1255 AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
Dale Johannesena04b7572009-02-03 23:04:43 +00001256 void* IP = 0;
1257 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1258 return SDValue(E, 0);
1259 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
Dale Johannesena04b7572009-02-03 23:04:43 +00001260 new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code);
1261 CSEMap.InsertNode(N, IP);
1262 AllNodes.push_back(N);
1263 return SDValue(N, 0);
1264}
1265
Owen Andersone50ed302009-08-10 22:56:29 +00001266SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001267 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001268 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001269 ID.AddInteger(RegNo);
1270 void *IP = 0;
1271 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001272 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001273 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001274 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001275 CSEMap.InsertNode(N, IP);
1276 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001277 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001278}
1279
Dale Johannesene8c17332009-01-29 00:47:48 +00001280SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1281 SDValue Root,
1282 unsigned LabelID) {
1283 FoldingSetNodeID ID;
1284 SDValue Ops[] = { Root };
Owen Anderson825b72b2009-08-11 20:47:22 +00001285 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
Dale Johannesene8c17332009-01-29 00:47:48 +00001286 ID.AddInteger(LabelID);
1287 void *IP = 0;
1288 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1289 return SDValue(E, 0);
1290 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1291 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1292 CSEMap.InsertNode(N, IP);
1293 AllNodes.push_back(N);
1294 return SDValue(N, 0);
1295}
1296
Dan Gohman29cbade2009-11-20 23:18:13 +00001297SDValue SelectionDAG::getBlockAddress(BlockAddress *BA, EVT VT,
1298 bool isTarget,
1299 unsigned char TargetFlags) {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001300 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
1301
1302 FoldingSetNodeID ID;
Dan Gohman29cbade2009-11-20 23:18:13 +00001303 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001304 ID.AddPointer(BA);
Dan Gohman29cbade2009-11-20 23:18:13 +00001305 ID.AddInteger(TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001306 void *IP = 0;
1307 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1308 return SDValue(E, 0);
1309 SDNode *N = NodeAllocator.Allocate<BlockAddressSDNode>();
Dan Gohman29cbade2009-11-20 23:18:13 +00001310 new (N) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001311 CSEMap.InsertNode(N, IP);
1312 AllNodes.push_back(N);
1313 return SDValue(N, 0);
1314}
1315
Dan Gohman475871a2008-07-27 21:46:04 +00001316SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001317 assert((!V || isa<PointerType>(V->getType())) &&
1318 "SrcValue is not a pointer?");
1319
Jim Laskey583bd472006-10-27 23:46:08 +00001320 FoldingSetNodeID ID;
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001322 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001323
Chris Lattner61b09412006-08-11 21:01:22 +00001324 void *IP = 0;
1325 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001326 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001327
Dan Gohmanfed90b62008-07-28 21:51:04 +00001328 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001329 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001330 CSEMap.InsertNode(N, IP);
1331 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001332 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001333}
1334
Duncan Sands92abc622009-01-31 15:50:11 +00001335/// getShiftAmountOperand - Return the specified value casted to
1336/// the target's desired shift amount type.
1337SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
Owen Andersone50ed302009-08-10 22:56:29 +00001338 EVT OpTy = Op.getValueType();
Owen Anderson766b5ef2009-08-11 21:59:30 +00001339 MVT ShTy = TLI.getShiftAmountTy();
Duncan Sands92abc622009-01-31 15:50:11 +00001340 if (OpTy == ShTy || OpTy.isVector()) return Op;
1341
1342 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001343 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001344}
1345
Chris Lattner37ce9df2007-10-15 17:47:20 +00001346/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1347/// specified value type.
Owen Andersone50ed302009-08-10 22:56:29 +00001348SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001349 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Dan Gohman4e918b22009-09-23 21:07:02 +00001350 unsigned ByteSize = VT.getStoreSize();
Owen Anderson23b9b192009-08-12 00:36:31 +00001351 const Type *Ty = VT.getTypeForEVT(*getContext());
Mon P Wang364d73d2008-07-05 20:40:31 +00001352 unsigned StackAlign =
1353 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001354
David Greene3f2bf852009-11-12 20:49:22 +00001355 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Chris Lattner37ce9df2007-10-15 17:47:20 +00001356 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1357}
1358
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001359/// CreateStackTemporary - Create a stack temporary suitable for holding
1360/// either of the specified value types.
Owen Andersone50ed302009-08-10 22:56:29 +00001361SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001362 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1363 VT2.getStoreSizeInBits())/8;
Owen Anderson23b9b192009-08-12 00:36:31 +00001364 const Type *Ty1 = VT1.getTypeForEVT(*getContext());
1365 const Type *Ty2 = VT2.getTypeForEVT(*getContext());
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001366 const TargetData *TD = TLI.getTargetData();
1367 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1368 TD->getPrefTypeAlignment(Ty2));
1369
1370 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00001371 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001372 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1373}
1374
Owen Andersone50ed302009-08-10 22:56:29 +00001375SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001376 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 // These setcc operations always fold.
1378 switch (Cond) {
1379 default: break;
1380 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001381 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001382 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001383 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001384
Chris Lattnera83385f2006-04-27 05:01:07 +00001385 case ISD::SETOEQ:
1386 case ISD::SETOGT:
1387 case ISD::SETOGE:
1388 case ISD::SETOLT:
1389 case ISD::SETOLE:
1390 case ISD::SETONE:
1391 case ISD::SETO:
1392 case ISD::SETUO:
1393 case ISD::SETUEQ:
1394 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001395 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001396 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001397 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001398
Gabor Greifba36cb52008-08-28 21:40:38 +00001399 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001400 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001401 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001402 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001403
Chris Lattnerc3aae252005-01-07 07:46:32 +00001404 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001405 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001406 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1407 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001408 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1409 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1410 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1411 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1412 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1413 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1414 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1415 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001416 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417 }
Chris Lattner67255a12005-04-07 18:14:58 +00001418 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001419 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1420 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001421 // No compile time operations on this type yet.
Owen Anderson825b72b2009-08-11 20:47:22 +00001422 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001423 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001424
1425 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001426 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001427 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001428 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001429 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001430 // fall through
1431 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001432 case ISD::SETNE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001433 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001434 // fall through
1435 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001436 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001437 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001438 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001439 // fall through
1440 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001441 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001442 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001443 // fall through
1444 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001445 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001446 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001447 // fall through
1448 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001449 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001451 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001452 // fall through
1453 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001454 R==APFloat::cmpEqual, VT);
1455 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1456 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1457 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1458 R==APFloat::cmpEqual, VT);
1459 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1460 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1461 R==APFloat::cmpLessThan, VT);
1462 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1463 R==APFloat::cmpUnordered, VT);
1464 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1465 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001466 }
1467 } else {
1468 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001469 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001470 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001471 }
1472
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001473 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001474 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001475}
1476
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001477/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1478/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001479bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001480 // This predicate is not safe for vector operations.
1481 if (Op.getValueType().isVector())
1482 return false;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00001483
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001484 unsigned BitWidth = Op.getValueSizeInBits();
1485 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1486}
1487
Dan Gohmanea859be2007-06-22 14:59:07 +00001488/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1489/// this predicate to simplify operations downstream. Mask is known to be zero
1490/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001491bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001492 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001493 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001494 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001495 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001496 return (KnownZero & Mask) == Mask;
1497}
1498
1499/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1500/// known to be either zero or one and return them in the KnownZero/KnownOne
1501/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1502/// processing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001503void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001504 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001505 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001506 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001507 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001508 "Mask size mismatches value type size!");
1509
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001510 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001511 if (Depth == 6 || Mask == 0)
1512 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001513
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001514 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001515
1516 switch (Op.getOpcode()) {
1517 case ISD::Constant:
1518 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001519 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001520 KnownZero = ~KnownOne & Mask;
1521 return;
1522 case ISD::AND:
1523 // If either the LHS or the RHS are Zero, the result is zero.
1524 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001525 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1526 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001527 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1528 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001529
1530 // Output known-1 bits are only known if set in both the LHS & RHS.
1531 KnownOne &= KnownOne2;
1532 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1533 KnownZero |= KnownZero2;
1534 return;
1535 case ISD::OR:
1536 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001537 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1538 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001539 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1540 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1541
Dan Gohmanea859be2007-06-22 14:59:07 +00001542 // Output known-0 bits are only known if clear in both the LHS & RHS.
1543 KnownZero &= KnownZero2;
1544 // Output known-1 are known to be set if set in either the LHS | RHS.
1545 KnownOne |= KnownOne2;
1546 return;
1547 case ISD::XOR: {
1548 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1549 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001550 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1551 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1552
Dan Gohmanea859be2007-06-22 14:59:07 +00001553 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001554 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001555 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1556 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1557 KnownZero = KnownZeroOut;
1558 return;
1559 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001560 case ISD::MUL: {
1561 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1562 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1563 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1564 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1565 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1566
1567 // If low bits are zero in either operand, output low known-0 bits.
1568 // Also compute a conserative estimate for high known-0 bits.
1569 // More trickiness is possible, but this is sufficient for the
1570 // interesting case of alignment computation.
1571 KnownOne.clear();
1572 unsigned TrailZ = KnownZero.countTrailingOnes() +
1573 KnownZero2.countTrailingOnes();
1574 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001575 KnownZero2.countLeadingOnes(),
1576 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001577
1578 TrailZ = std::min(TrailZ, BitWidth);
1579 LeadZ = std::min(LeadZ, BitWidth);
1580 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1581 APInt::getHighBitsSet(BitWidth, LeadZ);
1582 KnownZero &= Mask;
1583 return;
1584 }
1585 case ISD::UDIV: {
1586 // For the purposes of computing leading zeros we can conservatively
1587 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001588 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001589 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1590 ComputeMaskedBits(Op.getOperand(0),
1591 AllOnes, KnownZero2, KnownOne2, Depth+1);
1592 unsigned LeadZ = KnownZero2.countLeadingOnes();
1593
1594 KnownOne2.clear();
1595 KnownZero2.clear();
1596 ComputeMaskedBits(Op.getOperand(1),
1597 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001598 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1599 if (RHSUnknownLeadingOnes != BitWidth)
1600 LeadZ = std::min(BitWidth,
1601 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001602
1603 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1604 return;
1605 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001606 case ISD::SELECT:
1607 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1608 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001609 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1610 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1611
Dan Gohmanea859be2007-06-22 14:59:07 +00001612 // Only known if known in both the LHS and RHS.
1613 KnownOne &= KnownOne2;
1614 KnownZero &= KnownZero2;
1615 return;
1616 case ISD::SELECT_CC:
1617 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1618 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001619 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1620 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1621
Dan Gohmanea859be2007-06-22 14:59:07 +00001622 // Only known if known in both the LHS and RHS.
1623 KnownOne &= KnownOne2;
1624 KnownZero &= KnownZero2;
1625 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001626 case ISD::SADDO:
1627 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001628 case ISD::SSUBO:
1629 case ISD::USUBO:
1630 case ISD::SMULO:
1631 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001632 if (Op.getResNo() != 1)
1633 return;
Duncan Sands03228082008-11-23 15:47:28 +00001634 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001635 case ISD::SETCC:
1636 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001637 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001638 BitWidth > 1)
1639 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001640 return;
1641 case ISD::SHL:
1642 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1643 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001644 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001645
1646 // If the shift count is an invalid immediate, don't do anything.
1647 if (ShAmt >= BitWidth)
1648 return;
1649
1650 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001651 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001652 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001653 KnownZero <<= ShAmt;
1654 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001655 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001656 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001657 }
1658 return;
1659 case ISD::SRL:
1660 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1661 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001662 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001663
Dan Gohmand4cf9922008-02-26 18:50:50 +00001664 // If the shift count is an invalid immediate, don't do anything.
1665 if (ShAmt >= BitWidth)
1666 return;
1667
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001668 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001669 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001670 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001671 KnownZero = KnownZero.lshr(ShAmt);
1672 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001673
Dan Gohman72d2fd52008-02-13 22:43:25 +00001674 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001675 KnownZero |= HighBits; // High bits known zero.
1676 }
1677 return;
1678 case ISD::SRA:
1679 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001680 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001681
Dan Gohmand4cf9922008-02-26 18:50:50 +00001682 // If the shift count is an invalid immediate, don't do anything.
1683 if (ShAmt >= BitWidth)
1684 return;
1685
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001686 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001687 // If any of the demanded bits are produced by the sign extension, we also
1688 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001689 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1690 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001691 InDemandedMask |= APInt::getSignBit(BitWidth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001692
Dan Gohmanea859be2007-06-22 14:59:07 +00001693 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1694 Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001695 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001696 KnownZero = KnownZero.lshr(ShAmt);
1697 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001698
Dan Gohmanea859be2007-06-22 14:59:07 +00001699 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001700 APInt SignBit = APInt::getSignBit(BitWidth);
1701 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001702
Dan Gohmanca93a432008-02-20 16:30:17 +00001703 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001704 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001705 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001706 KnownOne |= HighBits; // New bits are known one.
1707 }
1708 }
1709 return;
1710 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00001711 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001712 unsigned EBits = EVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001713
1714 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001715 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001716 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001717
Dan Gohman977a76f2008-02-13 22:28:48 +00001718 APInt InSignBit = APInt::getSignBit(EBits);
1719 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Dan Gohmanea859be2007-06-22 14:59:07 +00001721 // If the sign extended bits are demanded, we know that the sign
1722 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001723 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001724 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001725 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Dan Gohmanea859be2007-06-22 14:59:07 +00001727 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1728 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001729 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1730
Dan Gohmanea859be2007-06-22 14:59:07 +00001731 // If the sign bit of the input is known set or clear, then we know the
1732 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001733 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001734 KnownZero |= NewBits;
1735 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001736 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001737 KnownOne |= NewBits;
1738 KnownZero &= ~NewBits;
1739 } else { // Input sign bit unknown
1740 KnownZero &= ~NewBits;
1741 KnownOne &= ~NewBits;
1742 }
1743 return;
1744 }
1745 case ISD::CTTZ:
1746 case ISD::CTLZ:
1747 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001748 unsigned LowBits = Log2_32(BitWidth)+1;
1749 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001750 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001751 return;
1752 }
1753 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001754 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001755 LoadSDNode *LD = cast<LoadSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00001756 EVT VT = LD->getMemoryVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001757 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001758 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001759 }
1760 return;
1761 }
1762 case ISD::ZERO_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001763 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001764 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001765 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1766 APInt InMask = Mask;
1767 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001768 KnownZero.trunc(InBits);
1769 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001770 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001771 KnownZero.zext(BitWidth);
1772 KnownOne.zext(BitWidth);
1773 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001774 return;
1775 }
1776 case ISD::SIGN_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001777 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001778 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001779 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001780 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1781 APInt InMask = Mask;
1782 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001783
1784 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001785 // bit is demanded. Temporarily set this bit in the mask for our callee.
1786 if (NewBits.getBoolValue())
1787 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001788
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001789 KnownZero.trunc(InBits);
1790 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001791 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1792
1793 // Note if the sign bit is known to be zero or one.
1794 bool SignBitKnownZero = KnownZero.isNegative();
1795 bool SignBitKnownOne = KnownOne.isNegative();
1796 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1797 "Sign bit can't be known to be both zero and one!");
1798
1799 // If the sign bit wasn't actually demanded by our caller, we don't
1800 // want it set in the KnownZero and KnownOne result values. Reset the
1801 // mask and reapply it to the result values.
1802 InMask = Mask;
1803 InMask.trunc(InBits);
1804 KnownZero &= InMask;
1805 KnownOne &= InMask;
1806
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001807 KnownZero.zext(BitWidth);
1808 KnownOne.zext(BitWidth);
1809
Dan Gohman977a76f2008-02-13 22:28:48 +00001810 // If the sign bit is known zero or one, the top bits match.
1811 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001812 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001813 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001814 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001815 return;
1816 }
1817 case ISD::ANY_EXTEND: {
Owen Andersone50ed302009-08-10 22:56:29 +00001818 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001819 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001820 APInt InMask = Mask;
1821 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001822 KnownZero.trunc(InBits);
1823 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001824 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001825 KnownZero.zext(BitWidth);
1826 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001827 return;
1828 }
1829 case ISD::TRUNCATE: {
Owen Andersone50ed302009-08-10 22:56:29 +00001830 EVT InVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001831 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001832 APInt InMask = Mask;
1833 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001834 KnownZero.zext(InBits);
1835 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001836 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001837 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001838 KnownZero.trunc(BitWidth);
1839 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001840 break;
1841 }
1842 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00001843 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001844 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michelfdc40a02009-02-17 22:15:04 +00001845 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanea859be2007-06-22 14:59:07 +00001846 KnownOne, Depth+1);
1847 KnownZero |= (~InMask) & Mask;
1848 return;
1849 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001850 case ISD::FGETSIGN:
1851 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001852 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001853 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001854
Dan Gohman23e8b712008-04-28 17:02:21 +00001855 case ISD::SUB: {
1856 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1857 // We know that the top bits of C-X are clear if X contains less bits
1858 // than C (i.e. no wrap-around can happen). For example, 20-X is
1859 // positive if we can prove that X is >= 0 and < 16.
1860 if (CLHS->getAPIntValue().isNonNegative()) {
1861 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1862 // NLZ can't be BitWidth with no sign bit
1863 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1864 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1865 Depth+1);
1866
1867 // If all of the MaskV bits are known to be zero, then we know the
1868 // output top bits are zero, because we now know that the output is
1869 // from [0-C].
1870 if ((KnownZero2 & MaskV) == MaskV) {
1871 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1872 // Top bits known zero.
1873 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1874 }
1875 }
1876 }
1877 }
1878 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001879 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001880 // Output known-0 bits are known if clear or set in both the low clear bits
1881 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1882 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001883 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1884 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001885 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001886 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1887
1888 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001889 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001890 KnownZeroOut = std::min(KnownZeroOut,
1891 KnownZero2.countTrailingOnes());
1892
1893 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001894 return;
1895 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001896 case ISD::SREM:
1897 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001898 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001899 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001900 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001901 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1902 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001903
Dan Gohmana60832b2008-08-13 23:12:35 +00001904 // If the sign bit of the first operand is zero, the sign bit of
1905 // the result is zero. If the first operand has no one bits below
1906 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001907 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1908 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001909
Dan Gohman23e8b712008-04-28 17:02:21 +00001910 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001911
1912 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001913 }
1914 }
1915 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001916 case ISD::UREM: {
1917 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001918 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001919 if (RA.isPowerOf2()) {
1920 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001921 APInt Mask2 = LowBits & Mask;
1922 KnownZero |= ~LowBits & Mask;
1923 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1924 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1925 break;
1926 }
1927 }
1928
1929 // Since the result is less than or equal to either operand, any leading
1930 // zero bits in either operand must also exist in the result.
1931 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1932 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1933 Depth+1);
1934 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1935 Depth+1);
1936
1937 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1938 KnownZero2.countLeadingOnes());
1939 KnownOne.clear();
1940 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1941 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001942 }
1943 default:
1944 // Allow the target to implement this method for its nodes.
1945 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1946 case ISD::INTRINSIC_WO_CHAIN:
1947 case ISD::INTRINSIC_W_CHAIN:
1948 case ISD::INTRINSIC_VOID:
Dan Gohman206ad102009-08-04 00:24:42 +00001949 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
1950 Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001951 }
1952 return;
1953 }
1954}
1955
1956/// ComputeNumSignBits - Return the number of times the sign bit of the
1957/// register is replicated into the other bits. We know that at least 1 bit
1958/// is always equal to the sign bit (itself), but other cases can give us
1959/// information. For example, immediately after an "SRA X, 2", we know that
1960/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001961unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Owen Andersone50ed302009-08-10 22:56:29 +00001962 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001963 assert(VT.isInteger() && "Invalid VT!");
1964 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001965 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001966 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001967
Dan Gohmanea859be2007-06-22 14:59:07 +00001968 if (Depth == 6)
1969 return 1; // Limit search depth.
1970
1971 switch (Op.getOpcode()) {
1972 default: break;
1973 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001974 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001975 return VTBits-Tmp+1;
1976 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001977 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001978 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00001979
Dan Gohmanea859be2007-06-22 14:59:07 +00001980 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001981 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1982 // If negative, return # leading ones.
1983 if (Val.isNegative())
1984 return Val.countLeadingOnes();
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001986 // Return # leading zeros.
1987 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001988 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001989
Dan Gohmanea859be2007-06-22 14:59:07 +00001990 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001991 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001992 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00001993
Dan Gohmanea859be2007-06-22 14:59:07 +00001994 case ISD::SIGN_EXTEND_INREG:
1995 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001996 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001997 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001998
Dan Gohmanea859be2007-06-22 14:59:07 +00001999 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2000 return std::max(Tmp, Tmp2);
2001
2002 case ISD::SRA:
2003 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2004 // SRA X, C -> adds C sign bits.
2005 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002006 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002007 if (Tmp > VTBits) Tmp = VTBits;
2008 }
2009 return Tmp;
2010 case ISD::SHL:
2011 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2012 // shl destroys sign bits.
2013 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002014 if (C->getZExtValue() >= VTBits || // Bad shift.
2015 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2016 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 }
2018 break;
2019 case ISD::AND:
2020 case ISD::OR:
2021 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002022 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002023 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002024 if (Tmp != 1) {
2025 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2026 FirstAnswer = std::min(Tmp, Tmp2);
2027 // We computed what we know about the sign bits as our first
2028 // answer. Now proceed to the generic code that uses
2029 // ComputeMaskedBits, and pick whichever answer is better.
2030 }
2031 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002032
2033 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002034 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002035 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002036 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002037 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002038
2039 case ISD::SADDO:
2040 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002041 case ISD::SSUBO:
2042 case ISD::USUBO:
2043 case ISD::SMULO:
2044 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002045 if (Op.getResNo() != 1)
2046 break;
Duncan Sands03228082008-11-23 15:47:28 +00002047 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002048 case ISD::SETCC:
2049 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00002050 if (TLI.getBooleanContents() ==
2051 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002052 return VTBits;
2053 break;
2054 case ISD::ROTL:
2055 case ISD::ROTR:
2056 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002057 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002058
Dan Gohmanea859be2007-06-22 14:59:07 +00002059 // Handle rotate right by N like a rotate left by 32-N.
2060 if (Op.getOpcode() == ISD::ROTR)
2061 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2062
2063 // If we aren't rotating out all of the known-in sign bits, return the
2064 // number that are left. This handles rotl(sext(x), 1) for example.
2065 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2066 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2067 }
2068 break;
2069 case ISD::ADD:
2070 // Add can have at most one carry bit. Thus we know that the output
2071 // is, at worst, one more bit than the inputs.
2072 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2073 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002074
Dan Gohmanea859be2007-06-22 14:59:07 +00002075 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002076 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002077 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002078 APInt KnownZero, KnownOne;
2079 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002080 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002081
Dan Gohmanea859be2007-06-22 14:59:07 +00002082 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2083 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002084 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002085 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002086
Dan Gohmanea859be2007-06-22 14:59:07 +00002087 // If we are subtracting one from a positive number, there is no carry
2088 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002089 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002090 return Tmp;
2091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Dan Gohmanea859be2007-06-22 14:59:07 +00002093 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2094 if (Tmp2 == 1) return 1;
2095 return std::min(Tmp, Tmp2)-1;
2096 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Dan Gohmanea859be2007-06-22 14:59:07 +00002098 case ISD::SUB:
2099 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2100 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002101
Dan Gohmanea859be2007-06-22 14:59:07 +00002102 // Handle NEG.
2103 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002104 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002105 APInt KnownZero, KnownOne;
2106 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002107 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2108 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2109 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002110 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002111 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Dan Gohmanea859be2007-06-22 14:59:07 +00002113 // If the input is known to be positive (the sign bit is known clear),
2114 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002115 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002116 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002117
Dan Gohmanea859be2007-06-22 14:59:07 +00002118 // Otherwise, we treat this like a SUB.
2119 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002120
Dan Gohmanea859be2007-06-22 14:59:07 +00002121 // Sub can have at most one carry bit. Thus we know that the output
2122 // is, at worst, one more bit than the inputs.
2123 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2124 if (Tmp == 1) return 1; // Early out.
2125 return std::min(Tmp, Tmp2)-1;
2126 break;
2127 case ISD::TRUNCATE:
2128 // FIXME: it's tricky to do anything useful for this, but it is an important
2129 // case for targets like X86.
2130 break;
2131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002132
Dan Gohmanea859be2007-06-22 14:59:07 +00002133 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2134 if (Op.getOpcode() == ISD::LOAD) {
2135 LoadSDNode *LD = cast<LoadSDNode>(Op);
2136 unsigned ExtType = LD->getExtensionType();
2137 switch (ExtType) {
2138 default: break;
2139 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002141 return VTBits-Tmp+1;
2142 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002143 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002144 return VTBits-Tmp;
2145 }
2146 }
2147
2148 // Allow the target to implement this method for its nodes.
2149 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002150 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2152 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2153 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002154 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002156
Dan Gohmanea859be2007-06-22 14:59:07 +00002157 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2158 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002159 APInt KnownZero, KnownOne;
2160 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002161 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002162
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002163 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002164 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002165 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002166 Mask = KnownOne;
2167 } else {
2168 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002169 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002171
Dan Gohmanea859be2007-06-22 14:59:07 +00002172 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2173 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002174 Mask = ~Mask;
2175 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002176 // Return # leading zeros. We use 'min' here in case Val was zero before
2177 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002178 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002179}
2180
Dan Gohman8d44b282009-09-03 20:34:31 +00002181bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
2182 // If we're told that NaNs won't happen, assume they won't.
2183 if (FiniteOnlyFPMath())
2184 return true;
2185
2186 // If the value is a constant, we can obviously see if it is a NaN or not.
2187 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
2188 return !C->getValueAPF().isNaN();
2189
2190 // TODO: Recognize more cases here.
2191
2192 return false;
2193}
Chris Lattner51dabfb2006-10-14 00:41:01 +00002194
Dan Gohman475871a2008-07-27 21:46:04 +00002195bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002196 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2197 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002198 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002199 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2200 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002201 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002202 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002203}
2204
2205
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002206/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2207/// element of the result of the vector shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002208SDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N,
2209 unsigned i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002210 EVT VT = N->getValueType(0);
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002211 DebugLoc dl = N->getDebugLoc();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002212 if (N->getMaskElt(i) < 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002213 return getUNDEF(VT.getVectorElementType());
Nate Begeman5a5ca152009-04-29 05:20:52 +00002214 unsigned Index = N->getMaskElt(i);
2215 unsigned NumElems = VT.getVectorNumElements();
Dan Gohman475871a2008-07-27 21:46:04 +00002216 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002217 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002218
2219 if (V.getOpcode() == ISD::BIT_CONVERT) {
2220 V = V.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00002221 EVT VVT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00002222 if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002223 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002224 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002225 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002226 return (Index == 0) ? V.getOperand(0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002227 : getUNDEF(VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002228 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002229 return V.getOperand(Index);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002230 if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V))
2231 return getShuffleScalarElt(SVN, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002232 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002233}
2234
2235
Chris Lattnerc3aae252005-01-07 07:46:32 +00002236/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002237///
Owen Andersone50ed302009-08-10 22:56:29 +00002238SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002239 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002240 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002241 void *IP = 0;
2242 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002243 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002244 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohmanfc166572009-04-09 23:54:40 +00002245 new (N) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002246 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002247
Chris Lattner4a283e92006-08-11 18:38:11 +00002248 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002249#ifndef NDEBUG
2250 VerifyNode(N);
2251#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002252 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002253}
2254
Bill Wendling7ade28c2009-01-28 22:17:52 +00002255SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00002256 EVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002257 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002258 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002259 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002260 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002261 switch (Opcode) {
2262 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002263 case ISD::SIGN_EXTEND:
2264 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002265 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002266 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002267 case ISD::TRUNCATE:
2268 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002269 case ISD::UINT_TO_FP:
2270 case ISD::SINT_TO_FP: {
2271 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002272 // No compile time operations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +00002273 if (VT==MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +00002274 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002275 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
Scott Michelfdc40a02009-02-17 22:15:04 +00002276 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002277 Opcode==ISD::SINT_TO_FP,
2278 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002279 return getConstantFP(apf, VT);
2280 }
Chris Lattner94683772005-12-23 05:30:37 +00002281 case ISD::BIT_CONVERT:
Owen Anderson825b72b2009-08-11 20:47:22 +00002282 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002283 return getConstantFP(Val.bitsToFloat(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002284 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002285 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002286 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002287 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002288 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002289 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002290 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002291 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002292 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002293 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002294 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002295 }
2296 }
2297
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002298 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002299 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002300 APFloat V = C->getValueAPF(); // make copy
Owen Anderson825b72b2009-08-11 20:47:22 +00002301 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002302 switch (Opcode) {
2303 case ISD::FNEG:
2304 V.changeSign();
2305 return getConstantFP(V, VT);
2306 case ISD::FABS:
2307 V.clearSign();
2308 return getConstantFP(V, VT);
2309 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002310 case ISD::FP_EXTEND: {
2311 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002312 // This can return overflow, underflow, or inexact; we don't care.
2313 // FIXME need to be more flexible about rounding mode.
Owen Andersone50ed302009-08-10 22:56:29 +00002314 (void)V.convert(*EVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002315 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002316 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002317 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002318 case ISD::FP_TO_SINT:
2319 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002320 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002321 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002322 assert(integerPartWidth >= 64);
2323 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002324 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002325 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002326 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002327 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2328 break;
Dale Johannesen2f91f302009-04-24 21:34:13 +00002329 APInt api(VT.getSizeInBits(), 2, x);
2330 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002331 }
2332 case ISD::BIT_CONVERT:
Owen Anderson825b72b2009-08-11 20:47:22 +00002333 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002334 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002335 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002336 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002337 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002338 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002339 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002340 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002341
Gabor Greifba36cb52008-08-28 21:40:38 +00002342 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002343 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002344 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002345 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002346 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002347 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002348 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002349 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002350 assert(VT.isFloatingPoint() &&
2351 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002352 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002353 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002354 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002355 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002356 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002357 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002358 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002359 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002360 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002361 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002362 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002363 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002364 break;
2365 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002366 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002367 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002368 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002369 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002370 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002371 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002372 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002373 Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002374 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002375 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002376 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002377 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002378 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002379 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002380 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002381 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2382 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002383 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002384 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002385 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002386 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002387 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002388 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002389 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002390 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002391 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002392 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002393 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2394 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002395 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002396 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002397 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002398 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002399 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002400 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002401 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002402 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002403 break;
Chris Lattner35481892005-12-23 00:16:34 +00002404 case ISD::BIT_CONVERT:
2405 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002406 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002407 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002408 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002409 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002410 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002411 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002412 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002413 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002414 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002415 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002416 (VT.getVectorElementType() == Operand.getValueType() ||
2417 (VT.getVectorElementType().isInteger() &&
2418 Operand.getValueType().isInteger() &&
2419 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002420 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002421 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002422 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002423 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2424 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2425 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2426 Operand.getConstantOperandVal(1) == 0 &&
2427 Operand.getOperand(0).getValueType() == VT)
2428 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002429 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002430 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002431 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2432 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002433 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002434 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002435 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002436 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002437 break;
2438 case ISD::FABS:
2439 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002440 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002441 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002442 }
2443
Chris Lattner43247a12005-08-25 19:12:10 +00002444 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002445 SDVTList VTs = getVTList(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002446 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002447 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002448 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002449 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002450 void *IP = 0;
2451 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002452 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002453 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002454 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002455 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002456 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002457 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002458 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002459 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002460
Chris Lattnerc3aae252005-01-07 07:46:32 +00002461 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002462#ifndef NDEBUG
2463 VerifyNode(N);
2464#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002465 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002466}
2467
Bill Wendling688d1c42008-09-24 10:16:24 +00002468SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
Owen Andersone50ed302009-08-10 22:56:29 +00002469 EVT VT,
Bill Wendling688d1c42008-09-24 10:16:24 +00002470 ConstantSDNode *Cst1,
2471 ConstantSDNode *Cst2) {
2472 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2473
2474 switch (Opcode) {
2475 case ISD::ADD: return getConstant(C1 + C2, VT);
2476 case ISD::SUB: return getConstant(C1 - C2, VT);
2477 case ISD::MUL: return getConstant(C1 * C2, VT);
2478 case ISD::UDIV:
2479 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2480 break;
2481 case ISD::UREM:
2482 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2483 break;
2484 case ISD::SDIV:
2485 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2486 break;
2487 case ISD::SREM:
2488 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2489 break;
2490 case ISD::AND: return getConstant(C1 & C2, VT);
2491 case ISD::OR: return getConstant(C1 | C2, VT);
2492 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2493 case ISD::SHL: return getConstant(C1 << C2, VT);
2494 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2495 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2496 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2497 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2498 default: break;
2499 }
2500
2501 return SDValue();
2502}
2503
Owen Andersone50ed302009-08-10 22:56:29 +00002504SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002505 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002506 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2507 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002508 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002509 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002510 case ISD::TokenFactor:
Owen Anderson825b72b2009-08-11 20:47:22 +00002511 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2512 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002513 // Fold trivial token factors.
2514 if (N1.getOpcode() == ISD::EntryToken) return N2;
2515 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002516 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002517 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002518 case ISD::CONCAT_VECTORS:
2519 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2520 // one big BUILD_VECTOR.
2521 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2522 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002523 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2524 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002525 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002526 }
2527 break;
Chris Lattner76365122005-01-16 02:23:22 +00002528 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002530 N1.getValueType() == VT && "Binary operator types must match!");
2531 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2532 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002533 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002534 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002535 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2536 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002537 break;
Chris Lattner76365122005-01-16 02:23:22 +00002538 case ISD::OR:
2539 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002540 case ISD::ADD:
2541 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002543 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002544 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2545 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002546 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002547 return N1;
2548 break;
Chris Lattner76365122005-01-16 02:23:22 +00002549 case ISD::UDIV:
2550 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002551 case ISD::MULHU:
2552 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002553 case ISD::MUL:
2554 case ISD::SDIV:
2555 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002556 assert(VT.isInteger() && "This operator does not apply to FP types!");
2557 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002558 case ISD::FADD:
2559 case ISD::FSUB:
2560 case ISD::FMUL:
2561 case ISD::FDIV:
2562 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002563 if (UnsafeFPMath) {
2564 if (Opcode == ISD::FADD) {
2565 // 0+x --> x
2566 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2567 if (CFP->getValueAPF().isZero())
2568 return N2;
2569 // x+0 --> x
2570 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2571 if (CFP->getValueAPF().isZero())
2572 return N1;
2573 } else if (Opcode == ISD::FSUB) {
2574 // x-0 --> x
2575 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2576 if (CFP->getValueAPF().isZero())
2577 return N1;
2578 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002579 }
Chris Lattner76365122005-01-16 02:23:22 +00002580 assert(N1.getValueType() == N2.getValueType() &&
2581 N1.getValueType() == VT && "Binary operator types must match!");
2582 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002583 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2584 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002585 N1.getValueType().isFloatingPoint() &&
2586 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002587 "Invalid FCOPYSIGN!");
2588 break;
Chris Lattner76365122005-01-16 02:23:22 +00002589 case ISD::SHL:
2590 case ISD::SRA:
2591 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002592 case ISD::ROTL:
2593 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002594 assert(VT == N1.getValueType() &&
2595 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002597 "Shifts only work on integers");
2598
2599 // Always fold shifts of i1 values so the code generator doesn't need to
2600 // handle them. Since we know the size of the shift has to be less than the
2601 // size of the value, the shift/rotate count is guaranteed to be zero.
Owen Anderson825b72b2009-08-11 20:47:22 +00002602 if (VT == MVT::i1)
Chris Lattner349db172008-07-02 17:01:57 +00002603 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002604 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002605 case ISD::FP_ROUND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002606 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002607 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002608 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002609 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002610 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002611 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002612 break;
2613 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002614 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 assert(VT.isFloatingPoint() &&
2616 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002617 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002618 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002619 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002620 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002621 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002622 case ISD::AssertZext: {
Owen Andersone50ed302009-08-10 22:56:29 +00002623 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002624 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002625 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002626 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002627 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002628 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002629 break;
2630 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002631 case ISD::SIGN_EXTEND_INREG: {
Owen Andersone50ed302009-08-10 22:56:29 +00002632 EVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002633 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002634 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002635 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002636 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002637 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002638
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002639 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002640 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002641 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002642 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002643 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002644 return getConstant(Val, VT);
2645 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002646 break;
2647 }
2648 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002649 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2650 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002651 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002652
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002653 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2654 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002655 if (N2C &&
2656 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002657 N1.getNumOperands() > 0) {
2658 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002660 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002661 N1.getOperand(N2C->getZExtValue() / Factor),
2662 getConstant(N2C->getZExtValue() % Factor,
2663 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002664 }
2665
2666 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2667 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002668 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2669 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Owen Andersone50ed302009-08-10 22:56:29 +00002670 EVT VEltTy = N1.getValueType().getVectorElementType();
Eli Friedmanc680ac92009-07-09 22:01:03 +00002671 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002672 // If the vector element type is not legal, the BUILD_VECTOR operands
2673 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002674 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2675 }
2676 if (VT != VEltTy) {
2677 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2678 // result is implicitly extended.
2679 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002680 }
2681 return Elt;
2682 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002683
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002684 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2685 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002686 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohman197e88f2009-01-29 16:10:46 +00002687 // If the indices are the same, return the inserted element.
Dan Gohman6ab64222008-08-13 21:51:37 +00002688 if (N1.getOperand(2) == N2)
2689 return N1.getOperand(1);
Dan Gohman197e88f2009-01-29 16:10:46 +00002690 // If the indices are known different, extract the element from
2691 // the original vector.
2692 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2693 isa<ConstantSDNode>(N2))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002694 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman6ab64222008-08-13 21:51:37 +00002695 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002696 break;
2697 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002698 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002699 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2700 (N1.getValueType().isInteger() == VT.isInteger()) &&
2701 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002702
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002703 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2704 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002705 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002706 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002707 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002708
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002709 // EXTRACT_ELEMENT of a constant int is also very common.
2710 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002711 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002712 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002713 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2714 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002715 }
2716 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002717 case ISD::EXTRACT_SUBVECTOR:
2718 if (N1.getValueType() == VT) // Trivial extraction.
2719 return N1;
2720 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002721 }
2722
2723 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002724 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002725 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2726 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002727 } else { // Cannonicalize constant to RHS if commutative
2728 if (isCommutativeBinOp(Opcode)) {
2729 std::swap(N1C, N2C);
2730 std::swap(N1, N2);
2731 }
2732 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002733 }
2734
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002735 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002736 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2737 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002738 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002739 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2740 // Cannonicalize constant to RHS if commutative
2741 std::swap(N1CFP, N2CFP);
2742 std::swap(N1, N2);
Owen Anderson825b72b2009-08-11 20:47:22 +00002743 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002744 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2745 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002746 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002747 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002748 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002749 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002750 return getConstantFP(V1, VT);
2751 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002752 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002753 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2754 if (s!=APFloat::opInvalidOp)
2755 return getConstantFP(V1, VT);
2756 break;
2757 case ISD::FMUL:
2758 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2759 if (s!=APFloat::opInvalidOp)
2760 return getConstantFP(V1, VT);
2761 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002762 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002763 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2764 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2765 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002766 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002767 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002768 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2769 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2770 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002771 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002772 case ISD::FCOPYSIGN:
2773 V1.copySign(V2);
2774 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002775 default: break;
2776 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002777 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002778 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002779
Chris Lattner62b57722006-04-20 05:39:12 +00002780 // Canonicalize an UNDEF to the RHS, even over a constant.
2781 if (N1.getOpcode() == ISD::UNDEF) {
2782 if (isCommutativeBinOp(Opcode)) {
2783 std::swap(N1, N2);
2784 } else {
2785 switch (Opcode) {
2786 case ISD::FP_ROUND_INREG:
2787 case ISD::SIGN_EXTEND_INREG:
2788 case ISD::SUB:
2789 case ISD::FSUB:
2790 case ISD::FDIV:
2791 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002792 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002793 return N1; // fold op(undef, arg2) -> undef
2794 case ISD::UDIV:
2795 case ISD::SDIV:
2796 case ISD::UREM:
2797 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002798 case ISD::SRL:
2799 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002800 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002801 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2802 // For vectors, we can't easily build an all zero vector, just return
2803 // the LHS.
2804 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002805 }
2806 }
2807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002808
2809 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002810 if (N2.getOpcode() == ISD::UNDEF) {
2811 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002812 case ISD::XOR:
2813 if (N1.getOpcode() == ISD::UNDEF)
2814 // Handle undef ^ undef -> 0 special case. This is a common
2815 // idiom (misuse).
2816 return getConstant(0, VT);
2817 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002818 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002819 case ISD::ADDC:
2820 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002821 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00002822 case ISD::UDIV:
2823 case ISD::SDIV:
2824 case ISD::UREM:
2825 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002826 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00002827 case ISD::FADD:
2828 case ISD::FSUB:
2829 case ISD::FMUL:
2830 case ISD::FDIV:
2831 case ISD::FREM:
2832 if (UnsafeFPMath)
2833 return N2;
2834 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002835 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00002836 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002837 case ISD::SRL:
2838 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002839 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002840 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2841 // For vectors, we can't easily build an all zero vector, just return
2842 // the LHS.
2843 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002844 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002845 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002846 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002847 // For vectors, we can't easily build an all one vector, just return
2848 // the LHS.
2849 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002850 case ISD::SRA:
2851 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002852 }
2853 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002854
Chris Lattner27e9b412005-05-11 18:57:39 +00002855 // Memoize this node if possible.
2856 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002857 SDVTList VTs = getVTList(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002858 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002859 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002860 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002861 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002862 void *IP = 0;
2863 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002864 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002865 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002866 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002867 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002868 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002869 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002870 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002871 }
2872
Chris Lattnerc3aae252005-01-07 07:46:32 +00002873 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002874#ifndef NDEBUG
2875 VerifyNode(N);
2876#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002877 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002878}
2879
Owen Andersone50ed302009-08-10 22:56:29 +00002880SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002881 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002882 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002883 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2884 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002885 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002886 case ISD::CONCAT_VECTORS:
2887 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2888 // one big BUILD_VECTOR.
2889 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2890 N2.getOpcode() == ISD::BUILD_VECTOR &&
2891 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002892 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2893 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2894 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002895 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002896 }
2897 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002898 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002899 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002900 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002901 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002902 break;
2903 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002904 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002905 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002906 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002907 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002908 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002909 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002910 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002911
2912 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002913 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002914 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002915 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002916 if (N2C->getZExtValue()) // Unconditional branch
Owen Anderson825b72b2009-08-11 20:47:22 +00002917 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Chris Lattner5351e9b2005-01-07 22:49:57 +00002918 else
2919 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002920 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002921 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002922 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00002923 llvm_unreachable("should use getVectorShuffle constructor!");
Chris Lattnerfb194b92006-03-19 23:56:04 +00002924 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002925 case ISD::BIT_CONVERT:
2926 // Fold bit_convert nodes from a type to themselves.
2927 if (N1.getValueType() == VT)
2928 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002929 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002930 }
2931
Chris Lattner43247a12005-08-25 19:12:10 +00002932 // Memoize node if it doesn't produce a flag.
2933 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002934 SDVTList VTs = getVTList(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00002935 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002936 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002937 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002938 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002939 void *IP = 0;
2940 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002941 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002942 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002943 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002944 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002945 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002946 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002947 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002948 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002949 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002950#ifndef NDEBUG
2951 VerifyNode(N);
2952#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002953 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002954}
2955
Owen Andersone50ed302009-08-10 22:56:29 +00002956SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002957 SDValue N1, SDValue N2, SDValue N3,
2958 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00002959 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002960 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002961}
2962
Owen Andersone50ed302009-08-10 22:56:29 +00002963SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00002964 SDValue N1, SDValue N2, SDValue N3,
2965 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00002966 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002967 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002968}
2969
Dan Gohman98ca4f22009-08-05 01:29:28 +00002970/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
2971/// the incoming stack arguments to be loaded from the stack.
2972SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
2973 SmallVector<SDValue, 8> ArgChains;
2974
2975 // Include the original chain at the beginning of the list. When this is
2976 // used by target LowerCall hooks, this helps legalize find the
2977 // CALLSEQ_BEGIN node.
2978 ArgChains.push_back(Chain);
2979
2980 // Add a chain value for each stack argument.
2981 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
2982 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
2983 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
2984 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
2985 if (FI->getIndex() < 0)
2986 ArgChains.push_back(SDValue(L, 1));
2987
2988 // Build a tokenfactor for all the chains.
Owen Anderson825b72b2009-08-11 20:47:22 +00002989 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002990 &ArgChains[0], ArgChains.size());
2991}
2992
Dan Gohman707e0182008-04-12 04:36:06 +00002993/// getMemsetValue - Vectorized representation of the memset value
2994/// operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002995static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
Dale Johannesen0f502f62009-02-03 22:26:09 +00002996 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002997 unsigned NumBits = VT.isVector() ?
2998 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002999 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003000 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00003001 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003002 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00003003 Val = (Val << Shift) | Val;
3004 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00003005 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003006 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003007 return DAG.getConstant(Val, VT);
3008 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003009 }
Evan Chengf0df0312008-05-15 08:39:06 +00003010
Duncan Sands3d0f5af2008-10-30 20:26:50 +00003011 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen0f502f62009-02-03 22:26:09 +00003012 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Chengf0df0312008-05-15 08:39:06 +00003013 unsigned Shift = 8;
3014 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00003015 Value = DAG.getNode(ISD::OR, dl, VT,
3016 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00003017 DAG.getConstant(Shift,
3018 TLI.getShiftAmountTy())),
3019 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00003020 Shift <<= 1;
3021 }
3022
3023 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003024}
3025
Dan Gohman707e0182008-04-12 04:36:06 +00003026/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3027/// used when a memcpy is turned into a memset when the source is a constant
3028/// string ptr.
Owen Andersone50ed302009-08-10 22:56:29 +00003029static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
Chris Lattner7453f8a2009-09-20 17:32:21 +00003030 const TargetLowering &TLI,
3031 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003032 // Handle vector with all elements zero.
3033 if (Str.empty()) {
3034 if (VT.isInteger())
3035 return DAG.getConstant(0, VT);
3036 unsigned NumElts = VT.getVectorNumElements();
Owen Anderson766b5ef2009-08-11 21:59:30 +00003037 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003038 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson23b9b192009-08-12 00:36:31 +00003039 DAG.getConstant(0,
3040 EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts)));
Evan Cheng0ff39b32008-06-30 07:31:25 +00003041 }
3042
Duncan Sands83ec4b62008-06-06 12:08:01 +00003043 assert(!VT.isVector() && "Can't handle vector type here!");
3044 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00003045 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003046 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003047 if (TLI.isLittleEndian())
3048 Offset = Offset + MSB - 1;
3049 for (unsigned i = 0; i != MSB; ++i) {
3050 Val = (Val << 8) | (unsigned char)Str[Offset];
3051 Offset += TLI.isLittleEndian() ? -1 : 1;
3052 }
3053 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003054}
3055
Scott Michelfdc40a02009-02-17 22:15:04 +00003056/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003057///
Dan Gohman475871a2008-07-27 21:46:04 +00003058static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003059 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003060 EVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003061 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003062 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003063}
3064
Evan Chengf0df0312008-05-15 08:39:06 +00003065/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3066///
Dan Gohman475871a2008-07-27 21:46:04 +00003067static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003068 unsigned SrcDelta = 0;
3069 GlobalAddressSDNode *G = NULL;
3070 if (Src.getOpcode() == ISD::GlobalAddress)
3071 G = cast<GlobalAddressSDNode>(Src);
3072 else if (Src.getOpcode() == ISD::ADD &&
3073 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3074 Src.getOperand(1).getOpcode() == ISD::Constant) {
3075 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003076 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003077 }
3078 if (!G)
3079 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003080
Evan Chengf0df0312008-05-15 08:39:06 +00003081 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Bill Wendling0582ae92009-03-13 04:39:26 +00003082 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3083 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00003084
Evan Chengf0df0312008-05-15 08:39:06 +00003085 return false;
3086}
Dan Gohman707e0182008-04-12 04:36:06 +00003087
Evan Chengf0df0312008-05-15 08:39:06 +00003088/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3089/// to replace the memset / memcpy is below the threshold. It also returns the
3090/// types of the sequence of memory ops to perform memset / memcpy.
3091static
Owen Andersone50ed302009-08-10 22:56:29 +00003092bool MeetsMaxMemopRequirement(std::vector<EVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00003093 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00003094 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003095 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00003096 SelectionDAG &DAG,
3097 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003098 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00003099 bool isSrcConst = isa<ConstantSDNode>(Src);
Owen Andersone50ed302009-08-10 22:56:29 +00003100 EVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr, DAG);
Benjamin Kramerbc037cf2009-08-15 20:46:16 +00003101 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses(VT);
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 if (VT != MVT::iAny) {
Evan Cheng8a9d5f92009-08-15 23:41:42 +00003103 const Type *Ty = VT.getTypeForEVT(*DAG.getContext());
3104 unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
Evan Chengf0df0312008-05-15 08:39:06 +00003105 // If source is a string constant, this will require an unaligned load.
3106 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3107 if (Dst.getOpcode() != ISD::FrameIndex) {
3108 // Can't change destination alignment. It requires a unaligned store.
3109 if (AllowUnalign)
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 VT = MVT::iAny;
Evan Chengf0df0312008-05-15 08:39:06 +00003111 } else {
3112 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3113 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3114 if (MFI->isFixedObjectIndex(FI)) {
3115 // Can't change destination alignment. It requires a unaligned store.
3116 if (AllowUnalign)
Owen Anderson825b72b2009-08-11 20:47:22 +00003117 VT = MVT::iAny;
Evan Chengf0df0312008-05-15 08:39:06 +00003118 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00003119 // Give the stack frame object a larger alignment if needed.
3120 if (MFI->getObjectAlignment(FI) < NewAlign)
3121 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00003122 Align = NewAlign;
3123 }
3124 }
3125 }
3126 }
3127
Owen Anderson825b72b2009-08-11 20:47:22 +00003128 if (VT == MVT::iAny) {
Evan Cheng8a9d5f92009-08-15 23:41:42 +00003129 if (TLI.allowsUnalignedMemoryAccesses(MVT::i64)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003131 } else {
3132 switch (Align & 7) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 case 0: VT = MVT::i64; break;
3134 case 4: VT = MVT::i32; break;
3135 case 2: VT = MVT::i16; break;
3136 default: VT = MVT::i8; break;
Evan Chengf0df0312008-05-15 08:39:06 +00003137 }
3138 }
3139
Owen Anderson766b5ef2009-08-11 21:59:30 +00003140 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003141 while (!TLI.isTypeLegal(LVT))
Owen Anderson766b5ef2009-08-11 21:59:30 +00003142 LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003143 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003144
Duncan Sands8e4eb092008-06-08 20:54:56 +00003145 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003146 VT = LVT;
3147 }
Dan Gohman707e0182008-04-12 04:36:06 +00003148
3149 unsigned NumMemOps = 0;
3150 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003152 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003153 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003154 if (VT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 VT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003156 while (!TLI.isTypeLegal(VT))
Owen Anderson825b72b2009-08-11 20:47:22 +00003157 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003158 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003159 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003160 // This can result in a type that is not legal on the target, e.g.
3161 // 1 or 2 bytes on PPC.
Owen Anderson825b72b2009-08-11 20:47:22 +00003162 VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003163 VTSize >>= 1;
3164 }
Dan Gohman707e0182008-04-12 04:36:06 +00003165 }
Dan Gohman707e0182008-04-12 04:36:06 +00003166
3167 if (++NumMemOps > Limit)
3168 return false;
3169 MemOps.push_back(VT);
3170 Size -= VTSize;
3171 }
3172
3173 return true;
3174}
3175
Dale Johannesen0f502f62009-02-03 22:26:09 +00003176static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue Chain, SDValue Dst,
3178 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00003179 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003180 const Value *DstSV, uint64_t DstSVOff,
3181 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00003182 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3183
Dan Gohman21323f32008-05-29 19:42:22 +00003184 // Expand memcpy to a series of load and store ops if the size operand falls
3185 // below a certain threshold.
Owen Andersone50ed302009-08-10 22:56:29 +00003186 std::vector<EVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003187 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00003188 if (!AlwaysInline)
3189 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00003190 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003191 std::string Str;
3192 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003193 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003194 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003195 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003196
Dan Gohman707e0182008-04-12 04:36:06 +00003197
Evan Cheng0ff39b32008-06-30 07:31:25 +00003198 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003199 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003200 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003201 uint64_t SrcOff = 0, DstOff = 0;
Chris Lattner7453f8a2009-09-20 17:32:21 +00003202 for (unsigned i = 0; i != NumMemOps; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00003203 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003204 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003205 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003206
Evan Cheng0ff39b32008-06-30 07:31:25 +00003207 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003208 // It's unlikely a store of a vector immediate can be done in a single
3209 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003210 // We also handle store a vector with all zero's.
3211 // FIXME: Handle other cases where store of vector immediate is done in
3212 // a single instruction.
Dale Johannesen0f502f62009-02-03 22:26:09 +00003213 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3214 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003215 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003216 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003217 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003218 // The type might not be legal for the target. This should only happen
3219 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003220 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3221 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003222 // FIXME does the case above also need this?
Owen Anderson23b9b192009-08-12 00:36:31 +00003223 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003224 assert(NVT.bitsGE(VT));
3225 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
3226 getMemBasePlusOffset(Src, SrcOff, DAG),
3227 SrcSV, SrcSVOff + SrcOff, VT, false, Align);
3228 Store = DAG.getTruncStore(Chain, dl, Value,
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003229 getMemBasePlusOffset(Dst, DstOff, DAG),
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003230 DstSV, DstSVOff + DstOff, VT, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003231 }
3232 OutChains.push_back(Store);
3233 SrcOff += VTSize;
3234 DstOff += VTSize;
3235 }
3236
Owen Anderson825b72b2009-08-11 20:47:22 +00003237 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003238 &OutChains[0], OutChains.size());
3239}
3240
Dale Johannesen0f502f62009-02-03 22:26:09 +00003241static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue Chain, SDValue Dst,
3243 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003244 unsigned Align, bool AlwaysInline,
3245 const Value *DstSV, uint64_t DstSVOff,
3246 const Value *SrcSV, uint64_t SrcSVOff){
3247 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3248
3249 // Expand memmove to a series of load and store ops if the size operand falls
3250 // below a certain threshold.
Owen Andersone50ed302009-08-10 22:56:29 +00003251 std::vector<EVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003252 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003253 if (!AlwaysInline)
3254 Limit = TLI.getMaxStoresPerMemmove();
3255 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003256 std::string Str;
3257 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003258 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003259 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003260 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003261
Dan Gohman21323f32008-05-29 19:42:22 +00003262 uint64_t SrcOff = 0, DstOff = 0;
3263
Dan Gohman475871a2008-07-27 21:46:04 +00003264 SmallVector<SDValue, 8> LoadValues;
3265 SmallVector<SDValue, 8> LoadChains;
3266 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003267 unsigned NumMemOps = MemOps.size();
3268 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003269 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003270 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003271 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003272
Dale Johannesen0f502f62009-02-03 22:26:09 +00003273 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003274 getMemBasePlusOffset(Src, SrcOff, DAG),
3275 SrcSV, SrcSVOff + SrcOff, false, Align);
3276 LoadValues.push_back(Value);
3277 LoadChains.push_back(Value.getValue(1));
3278 SrcOff += VTSize;
3279 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003280 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003281 &LoadChains[0], LoadChains.size());
3282 OutChains.clear();
3283 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003284 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003285 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003286 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003287
Dale Johannesen0f502f62009-02-03 22:26:09 +00003288 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003289 getMemBasePlusOffset(Dst, DstOff, DAG),
3290 DstSV, DstSVOff + DstOff, false, DstAlign);
3291 OutChains.push_back(Store);
3292 DstOff += VTSize;
3293 }
3294
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003296 &OutChains[0], OutChains.size());
3297}
3298
Dale Johannesen0f502f62009-02-03 22:26:09 +00003299static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003300 SDValue Chain, SDValue Dst,
3301 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003302 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003303 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003304 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3305
3306 // Expand memset to a series of load/store ops if the size operand
3307 // falls below a certain threshold.
Owen Andersone50ed302009-08-10 22:56:29 +00003308 std::vector<EVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003309 std::string Str;
3310 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003311 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003312 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003313 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003314
Dan Gohman475871a2008-07-27 21:46:04 +00003315 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003316 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003317
3318 unsigned NumMemOps = MemOps.size();
3319 for (unsigned i = 0; i < NumMemOps; i++) {
Owen Andersone50ed302009-08-10 22:56:29 +00003320 EVT VT = MemOps[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003321 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003322 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3323 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003324 getMemBasePlusOffset(Dst, DstOff, DAG),
3325 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003326 OutChains.push_back(Store);
3327 DstOff += VTSize;
3328 }
3329
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003331 &OutChains[0], OutChains.size());
3332}
3333
Dale Johannesen0f502f62009-02-03 22:26:09 +00003334SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003335 SDValue Src, SDValue Size,
3336 unsigned Align, bool AlwaysInline,
3337 const Value *DstSV, uint64_t DstSVOff,
3338 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003339
3340 // Check to see if we should lower the memcpy to loads and stores first.
3341 // For cases within the target-specified limits, this is the best choice.
3342 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3343 if (ConstantSize) {
3344 // Memcpy with size zero? Just return the original chain.
3345 if (ConstantSize->isNullValue())
3346 return Chain;
3347
Dan Gohman475871a2008-07-27 21:46:04 +00003348 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003349 getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003350 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003351 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003352 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003353 return Result;
3354 }
3355
3356 // Then check to see if we should lower the memcpy with target-specific
3357 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003358 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003359 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman707e0182008-04-12 04:36:06 +00003360 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003361 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003362 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003363 return Result;
3364
3365 // If we really need inline code and the target declined to provide it,
3366 // use a (potentially long) sequence of loads and stores.
3367 if (AlwaysInline) {
3368 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003369 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003370 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003371 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003372 }
3373
3374 // Emit a library call.
3375 TargetLowering::ArgListTy Args;
3376 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003377 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003378 Entry.Node = Dst; Args.push_back(Entry);
3379 Entry.Node = Src; Args.push_back(Entry);
3380 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003381 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003382 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003383 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003384 false, false, false, false, 0,
3385 TLI.getLibcallCallingConv(RTLIB::MEMCPY), false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003386 /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003387 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003388 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003389 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003390 return CallResult.second;
3391}
3392
Dale Johannesen0f502f62009-02-03 22:26:09 +00003393SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003394 SDValue Src, SDValue Size,
3395 unsigned Align,
3396 const Value *DstSV, uint64_t DstSVOff,
3397 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003398
Dan Gohman21323f32008-05-29 19:42:22 +00003399 // Check to see if we should lower the memmove to loads and stores first.
3400 // For cases within the target-specified limits, this is the best choice.
3401 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3402 if (ConstantSize) {
3403 // Memmove with size zero? Just return the original chain.
3404 if (ConstantSize->isNullValue())
3405 return Chain;
3406
Dan Gohman475871a2008-07-27 21:46:04 +00003407 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003408 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003409 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003410 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003411 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003412 return Result;
3413 }
Dan Gohman707e0182008-04-12 04:36:06 +00003414
3415 // Then check to see if we should lower the memmove with target-specific
3416 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003417 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003418 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003419 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003420 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003421 return Result;
3422
3423 // Emit a library call.
3424 TargetLowering::ArgListTy Args;
3425 TargetLowering::ArgListEntry Entry;
Owen Anderson1d0be152009-08-13 21:58:54 +00003426 Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003427 Entry.Node = Dst; Args.push_back(Entry);
3428 Entry.Node = Src; Args.push_back(Entry);
3429 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003430 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003431 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003432 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003433 false, false, false, false, 0,
3434 TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003435 /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003436 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003437 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003438 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003439 return CallResult.second;
3440}
3441
Dale Johannesen0f502f62009-02-03 22:26:09 +00003442SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003443 SDValue Src, SDValue Size,
3444 unsigned Align,
3445 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003446
3447 // Check to see if we should lower the memset to stores first.
3448 // For cases within the target-specified limits, this is the best choice.
3449 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3450 if (ConstantSize) {
3451 // Memset with size zero? Just return the original chain.
3452 if (ConstantSize->isNullValue())
3453 return Chain;
3454
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003456 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003457 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003458 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003459 return Result;
3460 }
3461
3462 // Then check to see if we should lower the memset with target-specific
3463 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003464 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003465 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003466 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003467 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003468 return Result;
3469
3470 // Emit a library call.
Owen Anderson1d0be152009-08-13 21:58:54 +00003471 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
Dan Gohman707e0182008-04-12 04:36:06 +00003472 TargetLowering::ArgListTy Args;
3473 TargetLowering::ArgListEntry Entry;
3474 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3475 Args.push_back(Entry);
3476 // Extend or truncate the argument to be an i32 value for the call.
Owen Anderson825b72b2009-08-11 20:47:22 +00003477 if (Src.getValueType().bitsGT(MVT::i32))
3478 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003479 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003480 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Owen Anderson1d0be152009-08-13 21:58:54 +00003481 Entry.Node = Src;
3482 Entry.Ty = Type::getInt32Ty(*getContext());
3483 Entry.isSExt = true;
Dan Gohman707e0182008-04-12 04:36:06 +00003484 Args.push_back(Entry);
Owen Anderson1d0be152009-08-13 21:58:54 +00003485 Entry.Node = Size;
3486 Entry.Ty = IntPtrTy;
3487 Entry.isSExt = false;
Dan Gohman707e0182008-04-12 04:36:06 +00003488 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003489 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003490 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00003491 TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
Anton Korobeynikov72977a42009-08-14 20:10:52 +00003492 false, false, false, false, 0,
3493 TLI.getLibcallCallingConv(RTLIB::MEMSET), false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003494 /*isReturnValueUsed=*/false,
Eric Christopher4d7c18c2009-08-22 00:40:45 +00003495 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003496 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003497 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003498 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003499}
3500
Owen Andersone50ed302009-08-10 22:56:29 +00003501SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003502 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003503 SDValue Ptr, SDValue Cmp,
Dale Johannesene8c17332009-01-29 00:47:48 +00003504 SDValue Swp, const Value* PtrVal,
3505 unsigned Alignment) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003506 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3507 Alignment = getEVTAlignment(MemVT);
3508
3509 // Check if the memory reference references a frame index
3510 if (!PtrVal)
3511 if (const FrameIndexSDNode *FI =
Evan Chengff89dcb2009-10-18 18:16:27 +00003512 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3513 PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003514
Evan Chengff89dcb2009-10-18 18:16:27 +00003515 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003516 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3517
3518 // For now, atomics are considered to be volatile always.
3519 Flags |= MachineMemOperand::MOVolatile;
3520
3521 MachineMemOperand *MMO =
3522 MF.getMachineMemOperand(PtrVal, Flags, 0,
3523 MemVT.getStoreSize(), Alignment);
3524
3525 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO);
3526}
3527
3528SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3529 SDValue Chain,
3530 SDValue Ptr, SDValue Cmp,
3531 SDValue Swp, MachineMemOperand *MMO) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003532 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3533 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3534
Owen Andersone50ed302009-08-10 22:56:29 +00003535 EVT VT = Cmp.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003536
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003538 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003539 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003540 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3541 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3542 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003543 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3544 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003545 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003546 }
Dale Johannesene8c17332009-01-29 00:47:48 +00003547 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003548 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, Ptr, Cmp, Swp, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003549 CSEMap.InsertNode(N, IP);
3550 AllNodes.push_back(N);
3551 return SDValue(N, 0);
3552}
3553
Owen Andersone50ed302009-08-10 22:56:29 +00003554SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003555 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003556 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003557 const Value* PtrVal,
3558 unsigned Alignment) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003559 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3560 Alignment = getEVTAlignment(MemVT);
3561
3562 // Check if the memory reference references a frame index
3563 if (!PtrVal)
3564 if (const FrameIndexSDNode *FI =
Evan Chengff89dcb2009-10-18 18:16:27 +00003565 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3566 PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003567
Evan Chengff89dcb2009-10-18 18:16:27 +00003568 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003569 unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
3570
3571 // For now, atomics are considered to be volatile always.
3572 Flags |= MachineMemOperand::MOVolatile;
3573
3574 MachineMemOperand *MMO =
3575 MF.getMachineMemOperand(PtrVal, Flags, 0,
3576 MemVT.getStoreSize(), Alignment);
3577
3578 return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO);
3579}
3580
3581SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
3582 SDValue Chain,
3583 SDValue Ptr, SDValue Val,
3584 MachineMemOperand *MMO) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003585 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3586 Opcode == ISD::ATOMIC_LOAD_SUB ||
3587 Opcode == ISD::ATOMIC_LOAD_AND ||
3588 Opcode == ISD::ATOMIC_LOAD_OR ||
3589 Opcode == ISD::ATOMIC_LOAD_XOR ||
3590 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003591 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003592 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003593 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003594 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3595 Opcode == ISD::ATOMIC_SWAP) &&
3596 "Invalid Atomic Op");
3597
Owen Andersone50ed302009-08-10 22:56:29 +00003598 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003599
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDVTList VTs = getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003601 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003602 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003603 SDValue Ops[] = {Chain, Ptr, Val};
3604 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3605 void* IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003606 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3607 cast<AtomicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003608 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003609 }
Dale Johannesene8c17332009-01-29 00:47:48 +00003610 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003611 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain, Ptr, Val, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003612 CSEMap.InsertNode(N, IP);
3613 AllNodes.push_back(N);
3614 return SDValue(N, 0);
3615}
3616
Duncan Sands4bdcb612008-07-02 17:40:58 +00003617/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3618/// Allowed to return something different (and simpler) if Simplify is true.
Dale Johannesen54c94522009-02-02 20:47:48 +00003619SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3620 DebugLoc dl) {
3621 if (NumOps == 1)
3622 return Ops[0];
3623
Owen Andersone50ed302009-08-10 22:56:29 +00003624 SmallVector<EVT, 4> VTs;
Dale Johannesen54c94522009-02-02 20:47:48 +00003625 VTs.reserve(NumOps);
3626 for (unsigned i = 0; i < NumOps; ++i)
3627 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00003628 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00003629 Ops, NumOps);
3630}
3631
Dan Gohman475871a2008-07-27 21:46:04 +00003632SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003633SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003634 const EVT *VTs, unsigned NumVTs,
Dale Johannesene8c17332009-01-29 00:47:48 +00003635 const SDValue *Ops, unsigned NumOps,
Owen Andersone50ed302009-08-10 22:56:29 +00003636 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesene8c17332009-01-29 00:47:48 +00003637 unsigned Align, bool Vol,
3638 bool ReadMem, bool WriteMem) {
3639 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3640 MemVT, srcValue, SVOff, Align, Vol,
3641 ReadMem, WriteMem);
3642}
3643
3644SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003645SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3646 const SDValue *Ops, unsigned NumOps,
Owen Andersone50ed302009-08-10 22:56:29 +00003647 EVT MemVT, const Value *srcValue, int SVOff,
Dale Johannesene8c17332009-01-29 00:47:48 +00003648 unsigned Align, bool Vol,
3649 bool ReadMem, bool WriteMem) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003650 if (Align == 0) // Ensure that codegen never sees alignment 0
3651 Align = getEVTAlignment(MemVT);
3652
3653 MachineFunction &MF = getMachineFunction();
3654 unsigned Flags = 0;
3655 if (WriteMem)
3656 Flags |= MachineMemOperand::MOStore;
3657 if (ReadMem)
3658 Flags |= MachineMemOperand::MOLoad;
3659 if (Vol)
3660 Flags |= MachineMemOperand::MOVolatile;
3661 MachineMemOperand *MMO =
3662 MF.getMachineMemOperand(srcValue, Flags, SVOff,
3663 MemVT.getStoreSize(), Align);
3664
3665 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
3666}
3667
3668SDValue
3669SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3670 const SDValue *Ops, unsigned NumOps,
3671 EVT MemVT, MachineMemOperand *MMO) {
3672 assert((Opcode == ISD::INTRINSIC_VOID ||
3673 Opcode == ISD::INTRINSIC_W_CHAIN ||
3674 (Opcode <= INT_MAX &&
3675 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
3676 "Opcode is not a memory-accessing opcode!");
3677
Dale Johannesene8c17332009-01-29 00:47:48 +00003678 // Memoize the node unless it returns a flag.
3679 MemIntrinsicSDNode *N;
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003681 FoldingSetNodeID ID;
3682 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3683 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003684 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3685 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003686 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003687 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003688
Dale Johannesene8c17332009-01-29 00:47:48 +00003689 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003690 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003691 CSEMap.InsertNode(N, IP);
3692 } else {
3693 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003694 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003695 }
3696 AllNodes.push_back(N);
3697 return SDValue(N, 0);
3698}
3699
3700SDValue
Scott Michelfdc40a02009-02-17 22:15:04 +00003701SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
Owen Andersone50ed302009-08-10 22:56:29 +00003702 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
Dale Johannesene8c17332009-01-29 00:47:48 +00003703 SDValue Ptr, SDValue Offset,
Dan Gohman8a55ce42009-09-23 21:02:20 +00003704 const Value *SV, int SVOffset, EVT MemVT,
Nate Begeman101b25c2009-09-15 19:05:41 +00003705 bool isVolatile, unsigned Alignment) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003706 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Owen Andersone50ed302009-08-10 22:56:29 +00003707 Alignment = getEVTAlignment(VT);
Dale Johannesene8c17332009-01-29 00:47:48 +00003708
Dan Gohmanc76909a2009-09-25 20:36:54 +00003709 // Check if the memory reference references a frame index
3710 if (!SV)
3711 if (const FrameIndexSDNode *FI =
Evan Chengff89dcb2009-10-18 18:16:27 +00003712 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3713 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003714
Evan Chengff89dcb2009-10-18 18:16:27 +00003715 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003716 unsigned Flags = MachineMemOperand::MOLoad;
3717 if (isVolatile)
3718 Flags |= MachineMemOperand::MOVolatile;
3719 MachineMemOperand *MMO =
3720 MF.getMachineMemOperand(SV, Flags, SVOffset,
3721 MemVT.getStoreSize(), Alignment);
3722 return getLoad(AM, dl, ExtType, VT, Chain, Ptr, Offset, MemVT, MMO);
3723}
3724
3725SDValue
3726SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
3727 ISD::LoadExtType ExtType, EVT VT, SDValue Chain,
3728 SDValue Ptr, SDValue Offset, EVT MemVT,
3729 MachineMemOperand *MMO) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00003730 if (VT == MemVT) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003731 ExtType = ISD::NON_EXTLOAD;
3732 } else if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00003733 assert(VT == MemVT && "Non-extending load from different memory type!");
Dale Johannesene8c17332009-01-29 00:47:48 +00003734 } else {
3735 // Extending load.
3736 if (VT.isVector())
Dan Gohman8a55ce42009-09-23 21:02:20 +00003737 assert(MemVT.getVectorNumElements() == VT.getVectorNumElements() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003738 "Invalid vector extload!");
3739 else
Dan Gohman8a55ce42009-09-23 21:02:20 +00003740 assert(MemVT.bitsLT(VT) &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003741 "Should only be an extending load, not truncating!");
3742 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3743 "Cannot sign/zero extend a FP/Vector load!");
Dan Gohman8a55ce42009-09-23 21:02:20 +00003744 assert(VT.isInteger() == MemVT.isInteger() &&
Dale Johannesene8c17332009-01-29 00:47:48 +00003745 "Cannot convert from FP to Int or Int -> FP!");
3746 }
3747
3748 bool Indexed = AM != ISD::UNINDEXED;
3749 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3750 "Unindexed load with an offset!");
3751
3752 SDVTList VTs = Indexed ?
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003754 SDValue Ops[] = { Chain, Ptr, Offset };
3755 FoldingSetNodeID ID;
3756 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dan Gohman8a55ce42009-09-23 21:02:20 +00003757 ID.AddInteger(MemVT.getRawBits());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003758 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile()));
Dale Johannesene8c17332009-01-29 00:47:48 +00003759 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003760 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3761 cast<LoadSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003762 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003763 }
Dale Johannesene8c17332009-01-29 00:47:48 +00003764 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003765 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, MemVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003766 CSEMap.InsertNode(N, IP);
3767 AllNodes.push_back(N);
3768 return SDValue(N, 0);
3769}
3770
Owen Andersone50ed302009-08-10 22:56:29 +00003771SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00003772 SDValue Chain, SDValue Ptr,
3773 const Value *SV, int SVOffset,
Nate Begeman101b25c2009-09-15 19:05:41 +00003774 bool isVolatile, unsigned Alignment) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003775 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003776 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
Nate Begeman101b25c2009-09-15 19:05:41 +00003777 SV, SVOffset, VT, isVolatile, Alignment);
Dale Johannesene8c17332009-01-29 00:47:48 +00003778}
3779
Owen Andersone50ed302009-08-10 22:56:29 +00003780SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, EVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003781 SDValue Chain, SDValue Ptr,
3782 const Value *SV,
Dan Gohman8a55ce42009-09-23 21:02:20 +00003783 int SVOffset, EVT MemVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003784 bool isVolatile, unsigned Alignment) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003785 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003786 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
Dan Gohman8a55ce42009-09-23 21:02:20 +00003787 SV, SVOffset, MemVT, isVolatile, Alignment);
Dale Johannesene8c17332009-01-29 00:47:48 +00003788}
3789
Dan Gohman475871a2008-07-27 21:46:04 +00003790SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003791SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3792 SDValue Offset, ISD::MemIndexedMode AM) {
3793 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3794 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3795 "Load is already a indexed load!");
3796 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3797 LD->getChain(), Base, Offset, LD->getSrcValue(),
3798 LD->getSrcValueOffset(), LD->getMemoryVT(),
3799 LD->isVolatile(), LD->getAlignment());
3800}
3801
Dale Johannesene8c17332009-01-29 00:47:48 +00003802SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3803 SDValue Ptr, const Value *SV, int SVOffset,
Nate Begeman101b25c2009-09-15 19:05:41 +00003804 bool isVolatile, unsigned Alignment) {
Dale Johannesene8c17332009-01-29 00:47:48 +00003805 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohmanc76909a2009-09-25 20:36:54 +00003806 Alignment = getEVTAlignment(Val.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003807
Dan Gohmanc76909a2009-09-25 20:36:54 +00003808 // Check if the memory reference references a frame index
3809 if (!SV)
3810 if (const FrameIndexSDNode *FI =
Evan Chengff89dcb2009-10-18 18:16:27 +00003811 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3812 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003813
Evan Chengff89dcb2009-10-18 18:16:27 +00003814 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003815 unsigned Flags = MachineMemOperand::MOStore;
3816 if (isVolatile)
3817 Flags |= MachineMemOperand::MOVolatile;
3818 MachineMemOperand *MMO =
3819 MF.getMachineMemOperand(SV, Flags, SVOffset,
3820 Val.getValueType().getStoreSize(), Alignment);
3821
3822 return getStore(Chain, dl, Val, Ptr, MMO);
3823}
3824
3825SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3826 SDValue Ptr, MachineMemOperand *MMO) {
3827 EVT VT = Val.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00003828 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00003829 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003830 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3831 FoldingSetNodeID ID;
3832 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003833 ID.AddInteger(VT.getRawBits());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003834 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile()));
Dale Johannesene8c17332009-01-29 00:47:48 +00003835 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003836 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3837 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003838 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003839 }
Dale Johannesene8c17332009-01-29 00:47:48 +00003840 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003841 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false, VT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003842 CSEMap.InsertNode(N, IP);
3843 AllNodes.push_back(N);
3844 return SDValue(N, 0);
3845}
3846
Dale Johannesene8c17332009-01-29 00:47:48 +00003847SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3848 SDValue Ptr, const Value *SV,
Owen Andersone50ed302009-08-10 22:56:29 +00003849 int SVOffset, EVT SVT,
Dale Johannesene8c17332009-01-29 00:47:48 +00003850 bool isVolatile, unsigned Alignment) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00003851 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3852 Alignment = getEVTAlignment(SVT);
3853
3854 // Check if the memory reference references a frame index
3855 if (!SV)
3856 if (const FrameIndexSDNode *FI =
Evan Chengff89dcb2009-10-18 18:16:27 +00003857 dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
3858 SV = PseudoSourceValue::getFixedStack(FI->getIndex());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003859
Evan Chengff89dcb2009-10-18 18:16:27 +00003860 MachineFunction &MF = getMachineFunction();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003861 unsigned Flags = MachineMemOperand::MOStore;
3862 if (isVolatile)
3863 Flags |= MachineMemOperand::MOVolatile;
3864 MachineMemOperand *MMO =
3865 MF.getMachineMemOperand(SV, Flags, SVOffset, SVT.getStoreSize(), Alignment);
3866
3867 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
3868}
3869
3870SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3871 SDValue Ptr, EVT SVT,
3872 MachineMemOperand *MMO) {
Owen Andersone50ed302009-08-10 22:56:29 +00003873 EVT VT = Val.getValueType();
Dale Johannesene8c17332009-01-29 00:47:48 +00003874
3875 if (VT == SVT)
Dan Gohmanc76909a2009-09-25 20:36:54 +00003876 return getStore(Chain, dl, Val, Ptr, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003877
3878 assert(VT.bitsGT(SVT) && "Not a truncation?");
3879 assert(VT.isInteger() == SVT.isInteger() &&
3880 "Can't do FP-INT conversion!");
3881
Dale Johannesene8c17332009-01-29 00:47:48 +00003882
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00003884 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003885 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3886 FoldingSetNodeID ID;
3887 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003888 ID.AddInteger(SVT.getRawBits());
Dan Gohmanc76909a2009-09-25 20:36:54 +00003889 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile()));
Dale Johannesene8c17332009-01-29 00:47:48 +00003890 void *IP = 0;
Dan Gohmanc76909a2009-09-25 20:36:54 +00003891 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3892 cast<StoreSDNode>(E)->refineAlignment(MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003893 return SDValue(E, 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003894 }
Dale Johannesene8c17332009-01-29 00:47:48 +00003895 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohmanc76909a2009-09-25 20:36:54 +00003896 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true, SVT, MMO);
Dale Johannesene8c17332009-01-29 00:47:48 +00003897 CSEMap.InsertNode(N, IP);
3898 AllNodes.push_back(N);
3899 return SDValue(N, 0);
3900}
3901
Dan Gohman475871a2008-07-27 21:46:04 +00003902SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003903SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3904 SDValue Offset, ISD::MemIndexedMode AM) {
3905 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3906 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3907 "Store is already a indexed store!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dale Johannesene8c17332009-01-29 00:47:48 +00003909 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3910 FoldingSetNodeID ID;
3911 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003912 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003913 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00003914 void *IP = 0;
3915 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3916 return SDValue(E, 0);
3917 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3918 new (N) StoreSDNode(Ops, dl, VTs, AM,
3919 ST->isTruncatingStore(), ST->getMemoryVT(),
Dan Gohmanc76909a2009-09-25 20:36:54 +00003920 ST->getMemOperand());
Dale Johannesene8c17332009-01-29 00:47:48 +00003921 CSEMap.InsertNode(N, IP);
3922 AllNodes.push_back(N);
3923 return SDValue(N, 0);
3924}
3925
Owen Andersone50ed302009-08-10 22:56:29 +00003926SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00003927 SDValue Chain, SDValue Ptr,
3928 SDValue SV) {
3929 SDValue Ops[] = { Chain, Ptr, SV };
Owen Anderson825b72b2009-08-11 20:47:22 +00003930 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
Dale Johannesen0f502f62009-02-03 22:26:09 +00003931}
3932
Owen Andersone50ed302009-08-10 22:56:29 +00003933SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003934 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003935 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003936 case 0: return getNode(Opcode, DL, VT);
3937 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3938 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3939 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003940 default: break;
3941 }
3942
Dan Gohman475871a2008-07-27 21:46:04 +00003943 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003944 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003945 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003946 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003947}
3948
Owen Andersone50ed302009-08-10 22:56:29 +00003949SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
Bill Wendling7ade28c2009-01-28 22:17:52 +00003950 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003951 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003952 case 0: return getNode(Opcode, DL, VT);
3953 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3954 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3955 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003956 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003957 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003958
Chris Lattneref847df2005-04-09 03:27:28 +00003959 switch (Opcode) {
3960 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003961 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003962 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003963 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3964 "LHS and RHS of condition must have same type!");
3965 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3966 "True and False arms of SelectCC must have same type!");
3967 assert(Ops[2].getValueType() == VT &&
3968 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003969 break;
3970 }
3971 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003972 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003973 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3974 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003975 break;
3976 }
Chris Lattneref847df2005-04-09 03:27:28 +00003977 }
3978
Chris Lattner385328c2005-05-14 07:42:29 +00003979 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003980 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003981 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003982
Owen Anderson825b72b2009-08-11 20:47:22 +00003983 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003984 FoldingSetNodeID ID;
3985 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003986 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00003987
Chris Lattnera5682852006-08-07 23:03:03 +00003988 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003989 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003990
Dan Gohmanfed90b62008-07-28 21:51:04 +00003991 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003992 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003993 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003994 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003995 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003996 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003997 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00003998
Chris Lattneref847df2005-04-09 03:27:28 +00003999 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004000#ifndef NDEBUG
4001 VerifyNode(N);
4002#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004003 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00004004}
4005
Bill Wendling7ade28c2009-01-28 22:17:52 +00004006SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004007 const std::vector<EVT> &ResultTys,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004008 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004009 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004010 Ops, NumOps);
4011}
4012
Bill Wendling7ade28c2009-01-28 22:17:52 +00004013SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
Owen Andersone50ed302009-08-10 22:56:29 +00004014 const EVT *VTs, unsigned NumVTs,
Bill Wendling7ade28c2009-01-28 22:17:52 +00004015 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004016 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004017 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
4018 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00004019}
4020
Bill Wendling7ade28c2009-01-28 22:17:52 +00004021SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4022 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00004023 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004024 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00004025
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004026#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00004027 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00004028 // FIXME: figure out how to safely handle things like
4029 // int foo(int x) { return 1 << (x & 255); }
4030 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00004031 case ISD::SRA_PARTS:
4032 case ISD::SRL_PARTS:
4033 case ISD::SHL_PARTS:
4034 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004036 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004037 else if (N3.getOpcode() == ISD::AND)
4038 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
4039 // If the and is only masking out bits that cannot effect the shift,
4040 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004041 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00004042 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00004043 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00004044 }
4045 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00004046 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00004047#endif
Chris Lattner89c34632005-05-14 06:20:26 +00004048
Chris Lattner43247a12005-08-25 19:12:10 +00004049 // Memoize the node unless it returns a flag.
4050 SDNode *N;
Owen Anderson825b72b2009-08-11 20:47:22 +00004051 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00004052 FoldingSetNodeID ID;
4053 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00004054 void *IP = 0;
4055 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00004056 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004057 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004058 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004059 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004060 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004061 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004062 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004063 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004064 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004065 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004066 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004067 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004068 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004069 }
Chris Lattnera5682852006-08-07 23:03:03 +00004070 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00004071 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00004072 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004073 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004074 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004075 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004076 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004077 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004078 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004079 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004080 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004081 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00004082 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00004083 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00004084 }
Chris Lattner43247a12005-08-25 19:12:10 +00004085 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00004086 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00004087#ifndef NDEBUG
4088 VerifyNode(N);
4089#endif
Dan Gohman475871a2008-07-27 21:46:04 +00004090 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00004091}
4092
Bill Wendling7ade28c2009-01-28 22:17:52 +00004093SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
4094 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00004095}
4096
Bill Wendling7ade28c2009-01-28 22:17:52 +00004097SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4098 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004099 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004100 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00004101}
4102
Bill Wendling7ade28c2009-01-28 22:17:52 +00004103SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4104 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00004105 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004106 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00004107}
4108
Bill Wendling7ade28c2009-01-28 22:17:52 +00004109SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4110 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00004111 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004112 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00004113}
4114
Bill Wendling7ade28c2009-01-28 22:17:52 +00004115SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4116 SDValue N1, SDValue N2, SDValue N3,
4117 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00004118 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004119 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00004120}
4121
Bill Wendling7ade28c2009-01-28 22:17:52 +00004122SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
4123 SDValue N1, SDValue N2, SDValue N3,
4124 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00004125 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00004126 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00004127}
4128
Owen Andersone50ed302009-08-10 22:56:29 +00004129SDVTList SelectionDAG::getVTList(EVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00004130 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00004131}
4132
Owen Andersone50ed302009-08-10 22:56:29 +00004133SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004134 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4135 E = VTList.rend(); I != E; ++I)
4136 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
4137 return *I;
4138
Owen Andersone50ed302009-08-10 22:56:29 +00004139 EVT *Array = Allocator.Allocate<EVT>(2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004140 Array[0] = VT1;
4141 Array[1] = VT2;
4142 SDVTList Result = makeVTList(Array, 2);
4143 VTList.push_back(Result);
4144 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004145}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004146
Owen Andersone50ed302009-08-10 22:56:29 +00004147SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004148 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4149 E = VTList.rend(); I != E; ++I)
4150 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4151 I->VTs[2] == VT3)
4152 return *I;
4153
Owen Andersone50ed302009-08-10 22:56:29 +00004154 EVT *Array = Allocator.Allocate<EVT>(3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004155 Array[0] = VT1;
4156 Array[1] = VT2;
4157 Array[2] = VT3;
4158 SDVTList Result = makeVTList(Array, 3);
4159 VTList.push_back(Result);
4160 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004161}
4162
Owen Andersone50ed302009-08-10 22:56:29 +00004163SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
Bill Wendling13d6d442008-12-01 23:28:22 +00004164 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4165 E = VTList.rend(); I != E; ++I)
4166 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4167 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4168 return *I;
4169
Owen Andersone50ed302009-08-10 22:56:29 +00004170 EVT *Array = Allocator.Allocate<EVT>(3);
Bill Wendling13d6d442008-12-01 23:28:22 +00004171 Array[0] = VT1;
4172 Array[1] = VT2;
4173 Array[2] = VT3;
4174 Array[3] = VT4;
4175 SDVTList Result = makeVTList(Array, 4);
4176 VTList.push_back(Result);
4177 return Result;
4178}
4179
Owen Andersone50ed302009-08-10 22:56:29 +00004180SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004181 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004182 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004183 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004184 case 2: return getVTList(VTs[0], VTs[1]);
4185 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4186 default: break;
4187 }
4188
Dan Gohmane8be6c62008-07-17 19:10:17 +00004189 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4190 E = VTList.rend(); I != E; ++I) {
4191 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4192 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004193
Chris Lattner70046e92006-08-15 17:46:01 +00004194 bool NoMatch = false;
4195 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004196 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004197 NoMatch = true;
4198 break;
4199 }
4200 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004201 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004202 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004203
Owen Andersone50ed302009-08-10 22:56:29 +00004204 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004205 std::copy(VTs, VTs+NumVTs, Array);
4206 SDVTList Result = makeVTList(Array, NumVTs);
4207 VTList.push_back(Result);
4208 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004209}
4210
4211
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004212/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4213/// specified operands. If the resultant node already exists in the DAG,
4214/// this does not modify the specified node, instead it returns the node that
4215/// already exists. If the resultant node does not exist in the DAG, the
4216/// input node is returned. As a degenerate case, if you specify the same
4217/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00004218SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004219 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004220 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004221
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004222 // Check to see if there is no change.
4223 if (Op == N->getOperand(0)) return InN;
Scott Michelfdc40a02009-02-17 22:15:04 +00004224
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004225 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004226 void *InsertPos = 0;
4227 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004228 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004229
Dan Gohman79acd2b2008-07-21 22:38:59 +00004230 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004231 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004232 if (!RemoveNodeFromCSEMaps(N))
4233 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004234
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004235 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004236 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004237
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004238 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004239 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004240 return InN;
4241}
4242
Dan Gohman475871a2008-07-27 21:46:04 +00004243SDValue SelectionDAG::
4244UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004245 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004246 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004247
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004248 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004249 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4250 return InN; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004251
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004252 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004253 void *InsertPos = 0;
4254 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004255 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004256
Dan Gohman79acd2b2008-07-21 22:38:59 +00004257 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004258 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004259 if (!RemoveNodeFromCSEMaps(N))
4260 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004261
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004262 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004263 if (N->OperandList[0] != Op1)
4264 N->OperandList[0].set(Op1);
4265 if (N->OperandList[1] != Op2)
4266 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004267
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004268 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004269 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004270 return InN;
4271}
4272
Dan Gohman475871a2008-07-27 21:46:04 +00004273SDValue SelectionDAG::
4274UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4275 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004276 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004277}
4278
Dan Gohman475871a2008-07-27 21:46:04 +00004279SDValue SelectionDAG::
Scott Michelfdc40a02009-02-17 22:15:04 +00004280UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue Op3, SDValue Op4) {
4282 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004283 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004284}
4285
Dan Gohman475871a2008-07-27 21:46:04 +00004286SDValue SelectionDAG::
4287UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4288 SDValue Op3, SDValue Op4, SDValue Op5) {
4289 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004290 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004291}
4292
Dan Gohman475871a2008-07-27 21:46:04 +00004293SDValue SelectionDAG::
4294UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004295 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004296 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004297 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004298
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004299 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004300 bool AnyChange = false;
4301 for (unsigned i = 0; i != NumOps; ++i) {
4302 if (Ops[i] != N->getOperand(i)) {
4303 AnyChange = true;
4304 break;
4305 }
4306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004307
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004308 // No operands changed, just return the input node.
4309 if (!AnyChange) return InN;
Scott Michelfdc40a02009-02-17 22:15:04 +00004310
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004311 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004312 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004313 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004314 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004315
Dan Gohman7ceda162008-05-02 00:05:03 +00004316 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004317 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004318 if (!RemoveNodeFromCSEMaps(N))
4319 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004320
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004321 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004322 for (unsigned i = 0; i != NumOps; ++i)
4323 if (N->OperandList[i] != Ops[i])
4324 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004325
4326 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004327 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004328 return InN;
4329}
4330
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004331/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004332/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004333void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004334 // Unlike the code in MorphNodeTo that does this, we don't need to
4335 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004336 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4337 SDUse &Use = *I++;
4338 Use.set(SDValue());
4339 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004340}
Chris Lattner149c58c2005-08-16 18:17:10 +00004341
Dan Gohmane8be6c62008-07-17 19:10:17 +00004342/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4343/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004344///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004345SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004346 EVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004347 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004348 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004349}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004350
Dan Gohmane8be6c62008-07-17 19:10:17 +00004351SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004352 EVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004353 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004354 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004355 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004356}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004357
Dan Gohmane8be6c62008-07-17 19:10:17 +00004358SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004359 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004360 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004361 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004362 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004363 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004364}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004365
Dan Gohmane8be6c62008-07-17 19:10:17 +00004366SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004367 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004368 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004369 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004370 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004371 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004372}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004373
Dan Gohmane8be6c62008-07-17 19:10:17 +00004374SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004375 EVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004376 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004377 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004378 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004379}
4380
Dan Gohmane8be6c62008-07-17 19:10:17 +00004381SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004382 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004383 unsigned NumOps) {
4384 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004385 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004386}
4387
Dan Gohmane8be6c62008-07-17 19:10:17 +00004388SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004389 EVT VT1, EVT VT2) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004390 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004391 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004392}
4393
Dan Gohmane8be6c62008-07-17 19:10:17 +00004394SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004395 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004396 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004397 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004398 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004399}
4400
Bill Wendling13d6d442008-12-01 23:28:22 +00004401SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004402 EVT VT1, EVT VT2, EVT VT3, EVT VT4,
Bill Wendling13d6d442008-12-01 23:28:22 +00004403 const SDValue *Ops, unsigned NumOps) {
4404 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4405 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4406}
4407
Scott Michelfdc40a02009-02-17 22:15:04 +00004408SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004409 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004410 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004411 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004412 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004413 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004414}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004415
Scott Michelfdc40a02009-02-17 22:15:04 +00004416SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004417 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004418 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004419 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004420 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004421 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004422}
4423
Dan Gohmane8be6c62008-07-17 19:10:17 +00004424SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004425 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004426 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004427 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004428 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004429 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004430 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004431}
4432
Dan Gohmane8be6c62008-07-17 19:10:17 +00004433SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Owen Andersone50ed302009-08-10 22:56:29 +00004434 EVT VT1, EVT VT2, EVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004435 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004436 SDValue Op3) {
4437 SDVTList VTs = getVTList(VT1, VT2, VT3);
4438 SDValue Ops[] = { Op1, Op2, Op3 };
4439 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4440}
4441
4442SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004443 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004444 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004445 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4446}
4447
4448SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004449 EVT VT) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004450 SDVTList VTs = getVTList(VT);
4451 return MorphNodeTo(N, Opc, VTs, 0, 0);
4452}
4453
4454SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004455 EVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004456 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004457 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004458 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4459}
4460
4461SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004462 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004463 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004464 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004465 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004466 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4467}
4468
4469SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004470 EVT VT, SDValue Op1,
Dan Gohman475871a2008-07-27 21:46:04 +00004471 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004472 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004473 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004474 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4475}
4476
4477SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004478 EVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004479 unsigned NumOps) {
4480 SDVTList VTs = getVTList(VT);
4481 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4482}
4483
4484SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004485 EVT VT1, EVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004486 unsigned NumOps) {
4487 SDVTList VTs = getVTList(VT1, VT2);
4488 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4489}
4490
4491SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004492 EVT VT1, EVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004493 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004494 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004495}
4496
4497SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004498 EVT VT1, EVT VT2, EVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004499 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004500 SDVTList VTs = getVTList(VT1, VT2, VT3);
4501 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4502}
4503
Scott Michelfdc40a02009-02-17 22:15:04 +00004504SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004505 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004506 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004507 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004508 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004509 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4510}
4511
Scott Michelfdc40a02009-02-17 22:15:04 +00004512SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004513 EVT VT1, EVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004514 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004515 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004516 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004517 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4518}
4519
4520SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Owen Andersone50ed302009-08-10 22:56:29 +00004521 EVT VT1, EVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004522 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004523 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004524 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004525 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004526 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4527}
4528
4529/// MorphNodeTo - These *mutate* the specified node to have the specified
4530/// return type, opcode, and operands.
4531///
4532/// Note that MorphNodeTo returns the resultant node. If there is already a
4533/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004534/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004535///
4536/// Using MorphNodeTo is faster than creating a new node and swapping it in
4537/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004538/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004539/// the node's users.
4540///
4541SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004542 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004543 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004544 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004545 void *IP = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00004546 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004547 FoldingSetNodeID ID;
4548 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4549 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4550 return ON;
4551 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004552
Dan Gohman095cc292008-09-13 01:54:27 +00004553 if (!RemoveNodeFromCSEMaps(N))
4554 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004555
Dan Gohmane8be6c62008-07-17 19:10:17 +00004556 // Start the morphing.
4557 N->NodeType = Opc;
4558 N->ValueList = VTs.VTs;
4559 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004560
Dan Gohmane8be6c62008-07-17 19:10:17 +00004561 // Clear the operands list, updating used nodes to remove this from their
4562 // use list. Keep track of any operands that become dead as a result.
4563 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004564 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4565 SDUse &Use = *I++;
4566 SDNode *Used = Use.getNode();
4567 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004568 if (Used->use_empty())
4569 DeadNodeSet.insert(Used);
4570 }
4571
Dan Gohmanc76909a2009-09-25 20:36:54 +00004572 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
4573 // Initialize the memory references information.
4574 MN->setMemRefs(0, 0);
4575 // If NumOps is larger than the # of operands we can have in a
4576 // MachineSDNode, reallocate the operand list.
4577 if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
4578 if (MN->OperandsNeedDelete)
4579 delete[] MN->OperandList;
4580 if (NumOps > array_lengthof(MN->LocalOperands))
4581 // We're creating a final node that will live unmorphed for the
4582 // remainder of the current SelectionDAG iteration, so we can allocate
4583 // the operands directly out of a pool with no recycling metadata.
4584 MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
4585 Ops, NumOps);
4586 else
4587 MN->InitOperands(MN->LocalOperands, Ops, NumOps);
4588 MN->OperandsNeedDelete = false;
4589 } else
4590 MN->InitOperands(MN->OperandList, Ops, NumOps);
4591 } else {
4592 // If NumOps is larger than the # of operands we currently have, reallocate
4593 // the operand list.
4594 if (NumOps > N->NumOperands) {
4595 if (N->OperandsNeedDelete)
4596 delete[] N->OperandList;
4597 N->InitOperands(new SDUse[NumOps], Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004598 N->OperandsNeedDelete = true;
Dan Gohmanc76909a2009-09-25 20:36:54 +00004599 } else
Anton Korobeynikov443b2152009-10-22 00:15:17 +00004600 N->InitOperands(N->OperandList, Ops, NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004601 }
4602
4603 // Delete any nodes that are still dead after adding the uses for the
4604 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004605 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004606 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4607 E = DeadNodeSet.end(); I != E; ++I)
4608 if ((*I)->use_empty())
4609 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004610 RemoveDeadNodes(DeadNodes);
4611
Dan Gohmane8be6c62008-07-17 19:10:17 +00004612 if (IP)
4613 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004614 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004615}
4616
Chris Lattner0fb094f2005-11-19 01:44:53 +00004617
Dan Gohman602b0c82009-09-25 18:54:59 +00004618/// getMachineNode - These are used for target selectors to create a new node
4619/// with specified return type(s), MachineInstr opcode, and operands.
Evan Cheng6ae46c42006-02-09 07:15:23 +00004620///
Dan Gohman602b0c82009-09-25 18:54:59 +00004621/// Note that getMachineNode returns the resultant node. If there is already a
Evan Cheng6ae46c42006-02-09 07:15:23 +00004622/// node of the specified opcode and operands, it returns that node instead of
4623/// the current one.
Dan Gohmanc81b7832009-10-10 01:29:16 +00004624MachineSDNode *
4625SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004626 SDVTList VTs = getVTList(VT);
4627 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00004628}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004629
Dan Gohmanc81b7832009-10-10 01:29:16 +00004630MachineSDNode *
4631SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004632 SDVTList VTs = getVTList(VT);
4633 SDValue Ops[] = { Op1 };
4634 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00004635}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004636
Dan Gohmanc81b7832009-10-10 01:29:16 +00004637MachineSDNode *
4638SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4639 SDValue Op1, SDValue Op2) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004640 SDVTList VTs = getVTList(VT);
4641 SDValue Ops[] = { Op1, Op2 };
4642 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00004643}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004644
Dan Gohmanc81b7832009-10-10 01:29:16 +00004645MachineSDNode *
4646SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4647 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004648 SDVTList VTs = getVTList(VT);
4649 SDValue Ops[] = { Op1, Op2, Op3 };
4650 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00004651}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004652
Dan Gohmanc81b7832009-10-10 01:29:16 +00004653MachineSDNode *
4654SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
4655 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004656 SDVTList VTs = getVTList(VT);
4657 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00004658}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004659
Dan Gohmanc81b7832009-10-10 01:29:16 +00004660MachineSDNode *
4661SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004662 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004663 return getMachineNode(Opcode, dl, VTs, 0, 0);
Dale Johannesene8c17332009-01-29 00:47:48 +00004664}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004665
Dan Gohmanc81b7832009-10-10 01:29:16 +00004666MachineSDNode *
4667SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4668 EVT VT1, EVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004669 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004670 SDValue Ops[] = { Op1 };
4671 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00004672}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004673
Dan Gohmanc81b7832009-10-10 01:29:16 +00004674MachineSDNode *
4675SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4676 EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004677 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004678 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00004679 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00004680}
4681
Dan Gohmanc81b7832009-10-10 01:29:16 +00004682MachineSDNode *
4683SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4684 EVT VT1, EVT VT2, SDValue Op1,
4685 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004686 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004687 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00004688 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Bill Wendling56ab1a22009-01-29 09:01:55 +00004689}
4690
Dan Gohmanc81b7832009-10-10 01:29:16 +00004691MachineSDNode *
4692SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4693 EVT VT1, EVT VT2,
4694 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004695 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004696 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00004697}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004698
Dan Gohmanc81b7832009-10-10 01:29:16 +00004699MachineSDNode *
4700SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4701 EVT VT1, EVT VT2, EVT VT3,
4702 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004703 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00004704 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00004705 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Dale Johannesene8c17332009-01-29 00:47:48 +00004706}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004707
Dan Gohmanc81b7832009-10-10 01:29:16 +00004708MachineSDNode *
4709SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4710 EVT VT1, EVT VT2, EVT VT3,
4711 SDValue Op1, SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004712 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004713 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00004714 return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng6ae46c42006-02-09 07:15:23 +00004715}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004716
Dan Gohmanc81b7832009-10-10 01:29:16 +00004717MachineSDNode *
4718SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4719 EVT VT1, EVT VT2, EVT VT3,
4720 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004721 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004722 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Dale Johannesene8c17332009-01-29 00:47:48 +00004723}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004724
Dan Gohmanc81b7832009-10-10 01:29:16 +00004725MachineSDNode *
4726SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
4727 EVT VT2, EVT VT3, EVT VT4,
4728 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004729 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004730 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004731}
4732
Dan Gohmanc81b7832009-10-10 01:29:16 +00004733MachineSDNode *
4734SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
4735 const std::vector<EVT> &ResultTys,
4736 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004737 SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
4738 return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
4739}
4740
Dan Gohmanc81b7832009-10-10 01:29:16 +00004741MachineSDNode *
4742SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
4743 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00004744 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Flag;
4745 MachineSDNode *N;
4746 void *IP;
4747
4748 if (DoCSE) {
4749 FoldingSetNodeID ID;
4750 AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
4751 IP = 0;
4752 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohmanc81b7832009-10-10 01:29:16 +00004753 return cast<MachineSDNode>(E);
Dan Gohmanc76909a2009-09-25 20:36:54 +00004754 }
4755
4756 // Allocate a new MachineSDNode.
4757 N = NodeAllocator.Allocate<MachineSDNode>();
4758 new (N) MachineSDNode(~Opcode, DL, VTs);
4759
4760 // Initialize the operands list.
4761 if (NumOps > array_lengthof(N->LocalOperands))
4762 // We're creating a final node that will live unmorphed for the
4763 // remainder of the current SelectionDAG iteration, so we can allocate
4764 // the operands directly out of a pool with no recycling metadata.
4765 N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
4766 Ops, NumOps);
4767 else
4768 N->InitOperands(N->LocalOperands, Ops, NumOps);
4769 N->OperandsNeedDelete = false;
4770
4771 if (DoCSE)
4772 CSEMap.InsertNode(N, IP);
4773
4774 AllNodes.push_back(N);
4775#ifndef NDEBUG
4776 VerifyNode(N);
4777#endif
4778 return N;
Dale Johannesene8c17332009-01-29 00:47:48 +00004779}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004780
Dan Gohman6a402dc2009-08-19 18:16:17 +00004781/// getTargetExtractSubreg - A convenience function for creating
4782/// TargetInstrInfo::EXTRACT_SUBREG nodes.
4783SDValue
4784SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
4785 SDValue Operand) {
4786 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00004787 SDNode *Subreg = getMachineNode(TargetInstrInfo::EXTRACT_SUBREG, DL,
4788 VT, Operand, SRIdxVal);
Dan Gohman6a402dc2009-08-19 18:16:17 +00004789 return SDValue(Subreg, 0);
4790}
4791
Bob Wilson5fcbf0d2009-10-08 18:49:46 +00004792/// getTargetInsertSubreg - A convenience function for creating
4793/// TargetInstrInfo::INSERT_SUBREG nodes.
4794SDValue
4795SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
4796 SDValue Operand, SDValue Subreg) {
4797 SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
4798 SDNode *Result = getMachineNode(TargetInstrInfo::INSERT_SUBREG, DL,
4799 VT, Operand, Subreg, SRIdxVal);
4800 return SDValue(Result, 0);
4801}
4802
Evan Cheng08b11732008-03-22 01:55:50 +00004803/// getNodeIfExists - Get the specified node if it's already available, or
4804/// else return NULL.
4805SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004806 const SDValue *Ops, unsigned NumOps) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004807 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Evan Cheng08b11732008-03-22 01:55:50 +00004808 FoldingSetNodeID ID;
4809 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4810 void *IP = 0;
4811 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4812 return E;
4813 }
4814 return NULL;
4815}
4816
Evan Cheng99157a02006-08-07 22:13:29 +00004817/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004818/// This can cause recursive merging of nodes in the DAG.
4819///
Chris Lattner11d049c2008-02-03 03:35:22 +00004820/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004821///
Dan Gohman475871a2008-07-27 21:46:04 +00004822void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004823 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004824 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004825 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004826 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004827 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004828
Dan Gohman39946102009-01-25 16:29:12 +00004829 // Iterate over all the existing uses of From. New uses will be added
4830 // to the beginning of the use list, which we avoid visiting.
4831 // This specifically avoids visiting uses of From that arise while the
4832 // replacement is happening, because any such uses would be the result
4833 // of CSE: If an existing node looks like From after one of its operands
4834 // is replaced by To, we don't want to replace of all its users with To
4835 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00004836 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4837 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004838 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004839
Chris Lattner8b8749f2005-08-17 19:00:20 +00004840 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004841 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00004842
Dan Gohman39946102009-01-25 16:29:12 +00004843 // A user can appear in a use list multiple times, and when this
4844 // happens the uses are usually next to each other in the list.
4845 // To help reduce the number of CSE recomputations, process all
4846 // the uses of this user that we can find this way.
4847 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004848 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004849 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004850 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004851 } while (UI != UE && *UI == User);
4852
4853 // Now that we have modified User, add it back to the CSE maps. If it
4854 // already exists there, recursively merge the results together.
4855 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004856 }
4857}
4858
4859/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4860/// This can cause recursive merging of nodes in the DAG.
4861///
Dan Gohmanc23e4962009-04-15 20:06:30 +00004862/// This version assumes that for each value of From, there is a
4863/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00004864///
Chris Lattner1e111c72005-09-07 05:37:01 +00004865void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004866 DAGUpdateListener *UpdateListener) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00004867#ifndef NDEBUG
4868 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
4869 assert((!From->hasAnyUseOfValue(i) ||
4870 From->getValueType(i) == To->getValueType(i)) &&
4871 "Cannot use this version of ReplaceAllUsesWith!");
4872#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00004873
4874 // Handle the trivial case.
4875 if (From == To)
4876 return;
4877
Dan Gohmandbe664a2009-01-19 21:44:21 +00004878 // Iterate over just the existing users of From. See the comments in
4879 // the ReplaceAllUsesWith above.
4880 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4881 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004882 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004883
Chris Lattner8b52f212005-08-26 18:36:28 +00004884 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004885 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004886
Dan Gohman39946102009-01-25 16:29:12 +00004887 // A user can appear in a use list multiple times, and when this
4888 // happens the uses are usually next to each other in the list.
4889 // To help reduce the number of CSE recomputations, process all
4890 // the uses of this user that we can find this way.
4891 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004892 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004893 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004894 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00004895 } while (UI != UE && *UI == User);
4896
4897 // Now that we have modified User, add it back to the CSE maps. If it
4898 // already exists there, recursively merge the results together.
4899 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004900 }
4901}
4902
Chris Lattner8b52f212005-08-26 18:36:28 +00004903/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4904/// This can cause recursive merging of nodes in the DAG.
4905///
4906/// This version can replace From with any result values. To must match the
4907/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004908void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004909 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004910 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004911 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004912 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004913
Dan Gohmandbe664a2009-01-19 21:44:21 +00004914 // Iterate over just the existing users of From. See the comments in
4915 // the ReplaceAllUsesWith above.
4916 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4917 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004918 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004919
Chris Lattner7b2880c2005-08-24 22:44:39 +00004920 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004921 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004922
Dan Gohman39946102009-01-25 16:29:12 +00004923 // A user can appear in a use list multiple times, and when this
4924 // happens the uses are usually next to each other in the list.
4925 // To help reduce the number of CSE recomputations, process all
4926 // the uses of this user that we can find this way.
4927 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004928 SDUse &Use = UI.getUse();
4929 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00004930 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004931 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00004932 } while (UI != UE && *UI == User);
4933
4934 // Now that we have modified User, add it back to the CSE maps. If it
4935 // already exists there, recursively merge the results together.
4936 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004937 }
4938}
4939
Chris Lattner012f2412006-02-17 21:58:01 +00004940/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004941/// uses of other values produced by From.getNode() alone. The Deleted
4942/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004943void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004944 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004945 // Handle the really simple, really trivial case efficiently.
4946 if (From == To) return;
4947
Chris Lattner012f2412006-02-17 21:58:01 +00004948 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004949 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004950 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004951 return;
4952 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004953
Dan Gohman39946102009-01-25 16:29:12 +00004954 // Iterate over just the existing users of From. See the comments in
4955 // the ReplaceAllUsesWith above.
4956 SDNode::use_iterator UI = From.getNode()->use_begin(),
4957 UE = From.getNode()->use_end();
4958 while (UI != UE) {
4959 SDNode *User = *UI;
4960 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00004961
Dan Gohman39946102009-01-25 16:29:12 +00004962 // A user can appear in a use list multiple times, and when this
4963 // happens the uses are usually next to each other in the list.
4964 // To help reduce the number of CSE recomputations, process all
4965 // the uses of this user that we can find this way.
4966 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004967 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004968
4969 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00004970 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00004971 ++UI;
4972 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00004973 }
Dan Gohman39946102009-01-25 16:29:12 +00004974
4975 // If this node hasn't been modified yet, it's still in the CSE maps,
4976 // so remove its old self from the CSE maps.
4977 if (!UserRemovedFromCSEMaps) {
4978 RemoveNodeFromCSEMaps(User);
4979 UserRemovedFromCSEMaps = true;
4980 }
4981
4982 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004983 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004984 } while (UI != UE && *UI == User);
4985
4986 // We are iterating over all uses of the From node, so if a use
4987 // doesn't use the specific value, no changes are made.
4988 if (!UserRemovedFromCSEMaps)
4989 continue;
4990
Chris Lattner01d029b2007-10-15 06:10:22 +00004991 // Now that we have modified User, add it back to the CSE maps. If it
4992 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004993 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004994 }
4995}
4996
Dan Gohman39946102009-01-25 16:29:12 +00004997namespace {
4998 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
4999 /// to record information about a use.
5000 struct UseMemo {
5001 SDNode *User;
5002 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005003 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00005004 };
Dan Gohmane7852d02009-01-26 04:35:06 +00005005
5006 /// operator< - Sort Memos by User.
5007 bool operator<(const UseMemo &L, const UseMemo &R) {
5008 return (intptr_t)L.User < (intptr_t)R.User;
5009 }
Dan Gohman39946102009-01-25 16:29:12 +00005010}
5011
Dan Gohmane8be6c62008-07-17 19:10:17 +00005012/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00005013/// uses of other values produced by From.getNode() alone. The same value
5014/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00005015/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00005016void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
5017 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00005018 unsigned Num,
5019 DAGUpdateListener *UpdateListener){
5020 // Handle the simple, trivial case efficiently.
5021 if (Num == 1)
5022 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
5023
Dan Gohman39946102009-01-25 16:29:12 +00005024 // Read up all the uses and make records of them. This helps
5025 // processing new uses that are introduced during the
5026 // replacement process.
5027 SmallVector<UseMemo, 4> Uses;
5028 for (unsigned i = 0; i != Num; ++i) {
5029 unsigned FromResNo = From[i].getResNo();
5030 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005031 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00005032 E = FromNode->use_end(); UI != E; ++UI) {
5033 SDUse &Use = UI.getUse();
5034 if (Use.getResNo() == FromResNo) {
5035 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00005036 Uses.push_back(Memo);
5037 }
Dan Gohmane7852d02009-01-26 04:35:06 +00005038 }
Dan Gohman39946102009-01-25 16:29:12 +00005039 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005040
Dan Gohmane7852d02009-01-26 04:35:06 +00005041 // Sort the uses, so that all the uses from a given User are together.
5042 std::sort(Uses.begin(), Uses.end());
5043
Dan Gohman39946102009-01-25 16:29:12 +00005044 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
5045 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00005046 // We know that this user uses some value of From. If it is the right
5047 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00005048 SDNode *User = Uses[UseIndex].User;
5049
5050 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00005051 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00005052
Dan Gohmane7852d02009-01-26 04:35:06 +00005053 // The Uses array is sorted, so all the uses for a given User
5054 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00005055 // To help reduce the number of CSE recomputations, process all
5056 // the uses of this user that we can find this way.
5057 do {
5058 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00005059 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00005060 ++UseIndex;
5061
Dan Gohmane7852d02009-01-26 04:35:06 +00005062 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00005063 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
5064
Dan Gohmane8be6c62008-07-17 19:10:17 +00005065 // Now that we have modified User, add it back to the CSE maps. If it
5066 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00005067 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00005068 }
5069}
5070
Evan Chenge6f35d82006-08-01 08:20:41 +00005071/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00005072/// based on their topological order. It returns the maximum id and a vector
5073/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00005074unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00005075
Dan Gohmanf06c8352008-09-30 18:30:35 +00005076 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00005077
Dan Gohmanf06c8352008-09-30 18:30:35 +00005078 // SortedPos tracks the progress of the algorithm. Nodes before it are
5079 // sorted, nodes after it are unsorted. When the algorithm completes
5080 // it is at the end of the list.
5081 allnodes_iterator SortedPos = allnodes_begin();
5082
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005083 // Visit all the nodes. Move nodes with no operands to the front of
5084 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00005085 // operand count. Before we do this, the Node Id fields of the nodes
5086 // may contain arbitrary values. After, the Node Id fields for nodes
5087 // before SortedPos will contain the topological sort index, and the
5088 // Node Id fields for nodes At SortedPos and after will contain the
5089 // count of outstanding operands.
5090 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
5091 SDNode *N = I++;
5092 unsigned Degree = N->getNumOperands();
5093 if (Degree == 0) {
5094 // A node with no uses, add it to the result array immediately.
5095 N->setNodeId(DAGSize++);
5096 allnodes_iterator Q = N;
5097 if (Q != SortedPos)
5098 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
5099 ++SortedPos;
5100 } else {
5101 // Temporarily use the Node Id as scratch space for the degree count.
5102 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00005103 }
5104 }
5105
Dan Gohmanf06c8352008-09-30 18:30:35 +00005106 // Visit all the nodes. As we iterate, moves nodes into sorted order,
5107 // such that by the time the end is reached all nodes will be sorted.
5108 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
5109 SDNode *N = I;
5110 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
5111 UI != UE; ++UI) {
5112 SDNode *P = *UI;
5113 unsigned Degree = P->getNodeId();
5114 --Degree;
5115 if (Degree == 0) {
5116 // All of P's operands are sorted, so P may sorted now.
5117 P->setNodeId(DAGSize++);
5118 if (P != SortedPos)
5119 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
5120 ++SortedPos;
5121 } else {
5122 // Update P's outstanding operand count.
5123 P->setNodeId(Degree);
5124 }
5125 }
5126 }
5127
5128 assert(SortedPos == AllNodes.end() &&
5129 "Topological sort incomplete!");
5130 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
5131 "First node in topological sort is not the entry token!");
5132 assert(AllNodes.front().getNodeId() == 0 &&
5133 "First node in topological sort has non-zero id!");
5134 assert(AllNodes.front().getNumOperands() == 0 &&
5135 "First node in topological sort has operands!");
5136 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
5137 "Last node in topologic sort has unexpected id!");
5138 assert(AllNodes.back().use_empty() &&
5139 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00005140 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00005141 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00005142}
5143
5144
Evan Cheng091cba12006-07-27 06:39:06 +00005145
Jim Laskey58b968b2005-08-17 20:08:02 +00005146//===----------------------------------------------------------------------===//
5147// SDNode Class
5148//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00005149
Chris Lattner48b85922007-02-04 02:41:42 +00005150HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00005151 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00005152}
5153
Chris Lattner505e9822009-06-26 21:14:05 +00005154GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005155 EVT VT, int64_t o, unsigned char TF)
Chris Lattner505e9822009-06-26 21:14:05 +00005156 : SDNode(Opc, DebugLoc::getUnknownLoc(), getSDVTList(VT)),
Chris Lattner2a4ed822009-06-25 21:21:14 +00005157 Offset(o), TargetFlags(TF) {
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005158 TheGlobal = const_cast<GlobalValue*>(GA);
5159}
Chris Lattner48b85922007-02-04 02:41:42 +00005160
Owen Andersone50ed302009-08-10 22:56:29 +00005161MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005162 MachineMemOperand *mmo)
5163 : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
5164 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile());
5165 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5166 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005167}
5168
Scott Michelfdc40a02009-02-17 22:15:04 +00005169MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Nate Begemane6798372009-09-15 00:13:12 +00005170 const SDValue *Ops, unsigned NumOps, EVT memvt,
Dan Gohmanc76909a2009-09-25 20:36:54 +00005171 MachineMemOperand *mmo)
Dale Johannesen3edb43e2009-01-28 21:18:29 +00005172 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmanc76909a2009-09-25 20:36:54 +00005173 MemoryVT(memvt), MMO(mmo) {
5174 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile());
5175 assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
5176 assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
Mon P Wang28873102008-06-25 08:15:39 +00005177}
5178
Jim Laskey583bd472006-10-27 23:46:08 +00005179/// Profile - Gather unique data for the node.
5180///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00005181void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00005182 AddNodeIDNode(ID, this);
5183}
5184
Owen Andersond8110fb2009-08-25 22:27:22 +00005185namespace {
5186 struct EVTArray {
5187 std::vector<EVT> VTs;
5188
5189 EVTArray() {
5190 VTs.reserve(MVT::LAST_VALUETYPE);
5191 for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
5192 VTs.push_back(MVT((MVT::SimpleValueType)i));
5193 }
5194 };
5195}
5196
Owen Andersone50ed302009-08-10 22:56:29 +00005197static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
Owen Andersond8110fb2009-08-25 22:27:22 +00005198static ManagedStatic<EVTArray> SimpleVTArray;
Chris Lattner895a55e2009-08-22 04:07:34 +00005199static ManagedStatic<sys::SmartMutex<true> > VTMutex;
Owen Andersonb4459082009-06-25 17:09:00 +00005200
Chris Lattnera3255112005-11-08 23:30:28 +00005201/// getValueTypeList - Return a pointer to the specified value type.
5202///
Owen Andersone50ed302009-08-10 22:56:29 +00005203const EVT *SDNode::getValueTypeList(EVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005204 if (VT.isExtended()) {
Owen Anderson02b10342009-08-22 06:32:36 +00005205 sys::SmartScopedLock<true> Lock(*VTMutex);
Owen Andersonb4459082009-06-25 17:09:00 +00005206 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005207 } else {
Owen Andersond8110fb2009-08-25 22:27:22 +00005208 return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005209 }
Chris Lattnera3255112005-11-08 23:30:28 +00005210}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005211
Chris Lattner5c884562005-01-12 18:37:47 +00005212/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5213/// indicated value. This method ignores uses of other values defined by this
5214/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005215bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005216 assert(Value < getNumValues() && "Bad value!");
5217
Roman Levensteindc1adac2008-04-07 10:06:32 +00005218 // TODO: Only iterate over uses of a given value of the node
5219 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005220 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005221 if (NUses == 0)
5222 return false;
5223 --NUses;
5224 }
Chris Lattner5c884562005-01-12 18:37:47 +00005225 }
5226
5227 // Found exactly the right number of uses?
5228 return NUses == 0;
5229}
5230
5231
Evan Cheng33d55952007-08-02 05:29:38 +00005232/// hasAnyUseOfValue - Return true if there are any use of the indicated
5233/// value. This method ignores uses of other values defined by this operation.
5234bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5235 assert(Value < getNumValues() && "Bad value!");
5236
Dan Gohman1373c1c2008-07-09 22:39:01 +00005237 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005238 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005239 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005240
5241 return false;
5242}
5243
5244
Dan Gohman2a629952008-07-27 18:06:42 +00005245/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005246///
Dan Gohman2a629952008-07-27 18:06:42 +00005247bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005248 bool Seen = false;
5249 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005250 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005251 if (User == this)
5252 Seen = true;
5253 else
5254 return false;
5255 }
5256
5257 return Seen;
5258}
5259
Evan Chenge6e97e62006-11-03 07:31:32 +00005260/// isOperand - Return true if this node is an operand of N.
5261///
Dan Gohman475871a2008-07-27 21:46:04 +00005262bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005263 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5264 if (*this == N->getOperand(i))
5265 return true;
5266 return false;
5267}
5268
Evan Cheng917be682008-03-04 00:41:45 +00005269bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005270 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005271 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005272 return true;
5273 return false;
5274}
Evan Cheng4ee62112006-02-05 06:29:23 +00005275
Chris Lattner572dee72008-01-16 05:49:24 +00005276/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005277/// be a chain) reaches the specified operand without crossing any
Chris Lattner572dee72008-01-16 05:49:24 +00005278/// side-effecting instructions. In practice, this looks through token
5279/// factors and non-volatile loads. In order to remain efficient, this only
5280/// looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005281bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005282 unsigned Depth) const {
5283 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005284
Chris Lattner572dee72008-01-16 05:49:24 +00005285 // Don't search too deeply, we just want to be able to see through
5286 // TokenFactor's etc.
5287 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005288
Chris Lattner572dee72008-01-16 05:49:24 +00005289 // If this is a token factor, all inputs to the TF happen in parallel. If any
5290 // of the operands of the TF reach dest, then we can do the xform.
5291 if (getOpcode() == ISD::TokenFactor) {
5292 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5293 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5294 return true;
5295 return false;
5296 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005297
Chris Lattner572dee72008-01-16 05:49:24 +00005298 // Loads don't have side effects, look through them.
5299 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5300 if (!Ld->isVolatile())
5301 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5302 }
5303 return false;
5304}
5305
Evan Cheng917be682008-03-04 00:41:45 +00005306/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Dan Gohman6688d612009-10-28 03:44:30 +00005307/// is either an operand of N or it can be reached by traversing up the operands.
Evan Chenge6e97e62006-11-03 07:31:32 +00005308/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00005309bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005310 SmallPtrSet<SDNode *, 32> Visited;
Dan Gohman6688d612009-10-28 03:44:30 +00005311 SmallVector<SDNode *, 16> Worklist;
5312 Worklist.push_back(N);
5313
5314 do {
5315 N = Worklist.pop_back_val();
5316 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5317 SDNode *Op = N->getOperand(i).getNode();
5318 if (Op == this)
5319 return true;
5320 if (Visited.insert(Op))
5321 Worklist.push_back(Op);
5322 }
5323 } while (!Worklist.empty());
5324
5325 return false;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005326}
5327
Evan Chengc5484282006-10-04 00:56:09 +00005328uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5329 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005330 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005331}
5332
Reid Spencer577cc322007-04-01 07:32:19 +00005333std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005334 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005335 default:
5336 if (getOpcode() < ISD::BUILTIN_OP_END)
5337 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00005338 if (isMachineOpcode()) {
5339 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005340 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00005341 if (getMachineOpcode() < TII->getNumOpcodes())
5342 return TII->get(getMachineOpcode()).getName();
5343 return "<<Unknown Machine Node>>";
5344 }
5345 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00005346 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00005347 const char *Name = TLI.getTargetNodeName(getOpcode());
5348 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00005349 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005350 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005351 return "<<Unknown Node>>";
Scott Michelfdc40a02009-02-17 22:15:04 +00005352
Dan Gohmane8be6c62008-07-17 19:10:17 +00005353#ifndef NDEBUG
5354 case ISD::DELETED_NODE:
5355 return "<<Deleted Node!>>";
5356#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005357 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005358 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005359 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5360 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5361 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5362 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5363 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5364 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5365 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5366 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5367 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5368 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5369 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5370 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005371 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005372 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005373 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005374 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005375 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005376 case ISD::AssertSext: return "AssertSext";
5377 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005378
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005379 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005380 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005381 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005382
5383 case ISD::Constant: return "Constant";
5384 case ISD::ConstantFP: return "ConstantFP";
5385 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005386 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005387 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005388 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005389 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005390 case ISD::RETURNADDR: return "RETURNADDR";
5391 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005392 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005393 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005394 case ISD::LSDAADDR: return "LSDAADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005395 case ISD::EHSELECTION: return "EHSELECTION";
5396 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005397 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005398 case ISD::ExternalSymbol: return "ExternalSymbol";
Dan Gohman8c2b5252009-10-30 01:27:03 +00005399 case ISD::BlockAddress: return "BlockAddress";
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +00005400 case ISD::INTRINSIC_WO_CHAIN:
Chris Lattner48b61a72006-03-28 00:40:33 +00005401 case ISD::INTRINSIC_VOID:
5402 case ISD::INTRINSIC_W_CHAIN: {
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +00005403 unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
5404 unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
5405 if (IID < Intrinsic::num_intrinsics)
5406 return Intrinsic::getName((Intrinsic::ID)IID);
5407 else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
5408 return TII->getName(IID);
5409 llvm_unreachable("Invalid intrinsic ID");
Chris Lattner401ec7f2006-03-27 16:10:59 +00005410 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005411
Chris Lattnerb2827b02006-03-19 00:52:58 +00005412 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005413 case ISD::TargetConstant: return "TargetConstant";
5414 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005415 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005416 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005417 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005418 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005419 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005420 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Dan Gohman8c2b5252009-10-30 01:27:03 +00005421 case ISD::TargetBlockAddress: return "TargetBlockAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005422
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005423 case ISD::CopyToReg: return "CopyToReg";
5424 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005425 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005426 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005427 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005428 case ISD::EH_LABEL: return "eh_label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005429 case ISD::HANDLENODE: return "handlenode";
Scott Michelfdc40a02009-02-17 22:15:04 +00005430
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005431 // Unary operators
5432 case ISD::FABS: return "fabs";
5433 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005434 case ISD::FSQRT: return "fsqrt";
5435 case ISD::FSIN: return "fsin";
5436 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005437 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005438 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005439 case ISD::FTRUNC: return "ftrunc";
5440 case ISD::FFLOOR: return "ffloor";
5441 case ISD::FCEIL: return "fceil";
5442 case ISD::FRINT: return "frint";
5443 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005444
5445 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005446 case ISD::ADD: return "add";
5447 case ISD::SUB: return "sub";
5448 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005449 case ISD::MULHU: return "mulhu";
5450 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005451 case ISD::SDIV: return "sdiv";
5452 case ISD::UDIV: return "udiv";
5453 case ISD::SREM: return "srem";
5454 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005455 case ISD::SMUL_LOHI: return "smul_lohi";
5456 case ISD::UMUL_LOHI: return "umul_lohi";
5457 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005458 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005459 case ISD::AND: return "and";
5460 case ISD::OR: return "or";
5461 case ISD::XOR: return "xor";
5462 case ISD::SHL: return "shl";
5463 case ISD::SRA: return "sra";
5464 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005465 case ISD::ROTL: return "rotl";
5466 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005467 case ISD::FADD: return "fadd";
5468 case ISD::FSUB: return "fsub";
5469 case ISD::FMUL: return "fmul";
5470 case ISD::FDIV: return "fdiv";
5471 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005472 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005473 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005474
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005475 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005476 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005477 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005478 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005479 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005480 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005481 case ISD::CONCAT_VECTORS: return "concat_vectors";
5482 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005483 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005484 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Dale Johannesen874ae252009-06-02 03:12:52 +00005485 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005486 case ISD::ADDC: return "addc";
5487 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005488 case ISD::SADDO: return "saddo";
5489 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005490 case ISD::SSUBO: return "ssubo";
5491 case ISD::USUBO: return "usubo";
5492 case ISD::SMULO: return "smulo";
5493 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005494 case ISD::SUBC: return "subc";
5495 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005496 case ISD::SHL_PARTS: return "shl_parts";
5497 case ISD::SRA_PARTS: return "sra_parts";
5498 case ISD::SRL_PARTS: return "srl_parts";
Scott Michelfdc40a02009-02-17 22:15:04 +00005499
Chris Lattner7f644642005-04-28 21:44:03 +00005500 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005501 case ISD::SIGN_EXTEND: return "sign_extend";
5502 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005503 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005504 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005505 case ISD::TRUNCATE: return "truncate";
5506 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005507 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005508 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005509 case ISD::FP_EXTEND: return "fp_extend";
5510
5511 case ISD::SINT_TO_FP: return "sint_to_fp";
5512 case ISD::UINT_TO_FP: return "uint_to_fp";
5513 case ISD::FP_TO_SINT: return "fp_to_sint";
5514 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005515 case ISD::BIT_CONVERT: return "bit_convert";
Scott Michelfdc40a02009-02-17 22:15:04 +00005516
Mon P Wang77cdf302008-11-10 20:54:11 +00005517 case ISD::CONVERT_RNDSAT: {
5518 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005519 default: llvm_unreachable("Unknown cvt code!");
Mon P Wang77cdf302008-11-10 20:54:11 +00005520 case ISD::CVT_FF: return "cvt_ff";
5521 case ISD::CVT_FS: return "cvt_fs";
5522 case ISD::CVT_FU: return "cvt_fu";
5523 case ISD::CVT_SF: return "cvt_sf";
5524 case ISD::CVT_UF: return "cvt_uf";
5525 case ISD::CVT_SS: return "cvt_ss";
5526 case ISD::CVT_SU: return "cvt_su";
5527 case ISD::CVT_US: return "cvt_us";
5528 case ISD::CVT_UU: return "cvt_uu";
5529 }
5530 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005531
5532 // Control flow instructions
5533 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005534 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005535 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005536 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005537 case ISD::BR_CC: return "br_cc";
Chris Lattnera364fa12005-05-12 23:51:40 +00005538 case ISD::CALLSEQ_START: return "callseq_start";
5539 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005540
5541 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005542 case ISD::LOAD: return "load";
5543 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005544 case ISD::VAARG: return "vaarg";
5545 case ISD::VACOPY: return "vacopy";
5546 case ISD::VAEND: return "vaend";
5547 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005548 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005549 case ISD::EXTRACT_ELEMENT: return "extract_element";
5550 case ISD::BUILD_PAIR: return "build_pair";
5551 case ISD::STACKSAVE: return "stacksave";
5552 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005553 case ISD::TRAP: return "trap";
5554
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005555 // Bit manipulation
5556 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005557 case ISD::CTPOP: return "ctpop";
5558 case ISD::CTTZ: return "cttz";
5559 case ISD::CTLZ: return "ctlz";
5560
Duncan Sands36397f52007-07-27 12:58:54 +00005561 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005562 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005563
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005564 case ISD::CONDCODE:
5565 switch (cast<CondCodeSDNode>(this)->get()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005566 default: llvm_unreachable("Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005567 case ISD::SETOEQ: return "setoeq";
5568 case ISD::SETOGT: return "setogt";
5569 case ISD::SETOGE: return "setoge";
5570 case ISD::SETOLT: return "setolt";
5571 case ISD::SETOLE: return "setole";
5572 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005573
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005574 case ISD::SETO: return "seto";
5575 case ISD::SETUO: return "setuo";
5576 case ISD::SETUEQ: return "setue";
5577 case ISD::SETUGT: return "setugt";
5578 case ISD::SETUGE: return "setuge";
5579 case ISD::SETULT: return "setult";
5580 case ISD::SETULE: return "setule";
5581 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005582
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005583 case ISD::SETEQ: return "seteq";
5584 case ISD::SETGT: return "setgt";
5585 case ISD::SETGE: return "setge";
5586 case ISD::SETLT: return "setlt";
5587 case ISD::SETLE: return "setle";
5588 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005589 }
5590 }
5591}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005592
Evan Cheng144d8f02006-11-09 17:55:04 +00005593const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005594 switch (AM) {
5595 default:
5596 return "";
5597 case ISD::PRE_INC:
5598 return "<pre-inc>";
5599 case ISD::PRE_DEC:
5600 return "<pre-dec>";
5601 case ISD::POST_INC:
5602 return "<post-inc>";
5603 case ISD::POST_DEC:
5604 return "<post-dec>";
5605 }
5606}
5607
Duncan Sands276dcbd2008-03-21 09:14:45 +00005608std::string ISD::ArgFlagsTy::getArgFlagsString() {
5609 std::string S = "< ";
5610
5611 if (isZExt())
5612 S += "zext ";
5613 if (isSExt())
5614 S += "sext ";
5615 if (isInReg())
5616 S += "inreg ";
5617 if (isSRet())
5618 S += "sret ";
5619 if (isByVal())
5620 S += "byval ";
5621 if (isNest())
5622 S += "nest ";
5623 if (getByValAlign())
5624 S += "byval-align:" + utostr(getByValAlign()) + " ";
5625 if (getOrigAlign())
5626 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5627 if (getByValSize())
5628 S += "byval-size:" + utostr(getByValSize()) + " ";
5629 return S + ">";
5630}
5631
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005632void SDNode::dump() const { dump(0); }
5633void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005634 print(errs(), G);
5635}
5636
Stuart Hastings80d69772009-02-04 16:46:19 +00005637void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005638 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005639
5640 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005641 if (i) OS << ",";
Owen Anderson825b72b2009-08-11 20:47:22 +00005642 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005643 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005644 else
Owen Andersone50ed302009-08-10 22:56:29 +00005645 OS << getValueType(i).getEVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005646 }
Chris Lattner944fac72008-08-23 22:23:09 +00005647 OS << " = " << getOperationName(G);
Stuart Hastings80d69772009-02-04 16:46:19 +00005648}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005649
Stuart Hastings80d69772009-02-04 16:46:19 +00005650void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
Dan Gohmanc76909a2009-09-25 20:36:54 +00005651 if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
5652 if (!MN->memoperands_empty()) {
5653 OS << "<";
5654 OS << "Mem:";
5655 for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
5656 e = MN->memoperands_end(); i != e; ++i) {
5657 OS << **i;
5658 if (next(i) != e)
5659 OS << " ";
5660 }
5661 OS << ">";
5662 }
5663 } else if (const ShuffleVectorSDNode *SVN =
5664 dyn_cast<ShuffleVectorSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005665 OS << "<";
Nate Begeman9008ca62009-04-27 18:41:29 +00005666 for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
5667 int Idx = SVN->getMaskElt(i);
Chris Lattner944fac72008-08-23 22:23:09 +00005668 if (i) OS << ",";
Nate Begeman9008ca62009-04-27 18:41:29 +00005669 if (Idx < 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005670 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005671 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005672 OS << Idx;
Evan Chengce254432007-12-11 02:08:35 +00005673 }
Chris Lattner944fac72008-08-23 22:23:09 +00005674 OS << ">";
Dan Gohmanc76909a2009-09-25 20:36:54 +00005675 } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005676 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005677 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005678 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005679 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005680 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005681 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005682 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005683 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005684 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005685 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005686 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005687 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005688 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005689 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005690 OS << '<';
5691 WriteAsOperand(OS, GADN->getGlobal());
5692 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005693 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005694 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005695 else
Chris Lattner944fac72008-08-23 22:23:09 +00005696 OS << " " << offset;
Dan Gohman1944da12009-08-01 19:13:38 +00005697 if (unsigned int TF = GADN->getTargetFlags())
Chris Lattner2a4ed822009-06-25 21:21:14 +00005698 OS << " [TF=" << TF << ']';
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005699 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005700 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005701 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005702 OS << "<" << JTDN->getIndex() << ">";
Dan Gohman1944da12009-08-01 19:13:38 +00005703 if (unsigned int TF = JTDN->getTargetFlags())
Chris Lattnerf5a55462009-06-25 21:35:31 +00005704 OS << " [TF=" << TF << ']';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005705 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005706 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005707 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005708 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005709 else
Chris Lattner944fac72008-08-23 22:23:09 +00005710 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005711 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005712 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005713 else
Chris Lattner944fac72008-08-23 22:23:09 +00005714 OS << " " << offset;
Dan Gohman1944da12009-08-01 19:13:38 +00005715 if (unsigned int TF = CP->getTargetFlags())
Chris Lattnerf5a55462009-06-25 21:35:31 +00005716 OS << " [TF=" << TF << ']';
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005717 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005718 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005719 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5720 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005721 OS << LBB->getName() << " ";
5722 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005723 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005724 if (G && R->getReg() &&
5725 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Dan Gohman0ba90f32009-10-31 20:19:03 +00005726 OS << " %" << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005727 } else {
Dan Gohman0ba90f32009-10-31 20:19:03 +00005728 OS << " %reg" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005729 }
Bill Wendling056292f2008-09-16 21:48:12 +00005730 } else if (const ExternalSymbolSDNode *ES =
5731 dyn_cast<ExternalSymbolSDNode>(this)) {
5732 OS << "'" << ES->getSymbol() << "'";
Dan Gohman1944da12009-08-01 19:13:38 +00005733 if (unsigned int TF = ES->getTargetFlags())
Chris Lattner2a4ed822009-06-25 21:21:14 +00005734 OS << " [TF=" << TF << ']';
Chris Lattner2bf3c262005-05-09 04:08:27 +00005735 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5736 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005737 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005738 else
Chris Lattner944fac72008-08-23 22:23:09 +00005739 OS << "<null>";
Chris Lattnera23e8152005-08-18 03:31:02 +00005740 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005741 OS << ":" << N->getVT().getEVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005742 }
5743 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Dan Gohman6c1de182009-10-29 23:30:06 +00005744 OS << "<" << *LD->getMemOperand();
Evan Cheng81310132007-12-18 19:06:30 +00005745
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005746 bool doExt = true;
5747 switch (LD->getExtensionType()) {
5748 default: doExt = false; break;
Dan Gohmanc76909a2009-09-25 20:36:54 +00005749 case ISD::EXTLOAD: OS << ", anyext"; break;
5750 case ISD::SEXTLOAD: OS << ", sext"; break;
5751 case ISD::ZEXTLOAD: OS << ", zext"; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005752 }
5753 if (doExt)
Dan Gohmanc76909a2009-09-25 20:36:54 +00005754 OS << " from " << LD->getMemoryVT().getEVTString();
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005755
Evan Cheng144d8f02006-11-09 17:55:04 +00005756 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005757 if (*AM)
Dan Gohmanc76909a2009-09-25 20:36:54 +00005758 OS << ", " << AM;
5759
5760 OS << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00005761 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Dan Gohman6c1de182009-10-29 23:30:06 +00005762 OS << "<" << *ST->getMemOperand();
Evan Cheng81310132007-12-18 19:06:30 +00005763
5764 if (ST->isTruncatingStore())
Dan Gohmanc76909a2009-09-25 20:36:54 +00005765 OS << ", trunc to " << ST->getMemoryVT().getEVTString();
Evan Cheng81310132007-12-18 19:06:30 +00005766
5767 const char *AM = getIndexedModeName(ST->getAddressingMode());
5768 if (*AM)
Dan Gohmanc76909a2009-09-25 20:36:54 +00005769 OS << ", " << AM;
5770
5771 OS << ">";
5772 } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
Dan Gohman6c1de182009-10-29 23:30:06 +00005773 OS << "<" << *M->getMemOperand() << ">";
Dan Gohman8c2b5252009-10-30 01:27:03 +00005774 } else if (const BlockAddressSDNode *BA =
5775 dyn_cast<BlockAddressSDNode>(this)) {
5776 OS << "<";
5777 WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false);
5778 OS << ", ";
5779 WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false);
5780 OS << ">";
Dan Gohman29cbade2009-11-20 23:18:13 +00005781 if (unsigned int TF = BA->getTargetFlags())
5782 OS << " [TF=" << TF << ']';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005783 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005784}
5785
Stuart Hastings80d69772009-02-04 16:46:19 +00005786void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5787 print_types(OS, G);
Stuart Hastings80d69772009-02-04 16:46:19 +00005788 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohmanc5600c82009-11-05 18:49:11 +00005789 if (i) OS << ", "; else OS << " ";
Stuart Hastings80d69772009-02-04 16:46:19 +00005790 OS << (void*)getOperand(i).getNode();
5791 if (unsigned RN = getOperand(i).getResNo())
5792 OS << ":" << RN;
5793 }
5794 print_details(OS, G);
5795}
5796
Chris Lattnerde202b32005-11-09 23:47:37 +00005797static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005798 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005799 if (N->getOperand(i).getNode()->hasOneUse())
5800 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005801 else
Chris Lattner45cfe542009-08-23 06:03:38 +00005802 errs() << "\n" << std::string(indent+2, ' ')
5803 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005804
Chris Lattnerea946cd2005-01-09 20:38:33 +00005805
Chris Lattner45cfe542009-08-23 06:03:38 +00005806 errs() << "\n";
5807 errs().indent(indent);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005808 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005809}
5810
Mon P Wangcd6e7252009-11-30 02:42:02 +00005811SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
5812 assert(N->getNumValues() == 1 &&
5813 "Can't unroll a vector with multiple results!");
5814
5815 EVT VT = N->getValueType(0);
5816 unsigned NE = VT.getVectorNumElements();
5817 EVT EltVT = VT.getVectorElementType();
5818 DebugLoc dl = N->getDebugLoc();
5819
5820 SmallVector<SDValue, 8> Scalars;
5821 SmallVector<SDValue, 4> Operands(N->getNumOperands());
5822
5823 // If ResNE is 0, fully unroll the vector op.
5824 if (ResNE == 0)
5825 ResNE = NE;
5826 else if (NE > ResNE)
5827 NE = ResNE;
5828
5829 unsigned i;
5830 for (i= 0; i != NE; ++i) {
5831 for (unsigned j = 0; j != N->getNumOperands(); ++j) {
5832 SDValue Operand = N->getOperand(j);
5833 EVT OperandVT = Operand.getValueType();
5834 if (OperandVT.isVector()) {
5835 // A vector operand; extract a single element.
5836 EVT OperandEltVT = OperandVT.getVectorElementType();
5837 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5838 OperandEltVT,
5839 Operand,
5840 getConstant(i, MVT::i32));
5841 } else {
5842 // A scalar operand; just use it as is.
5843 Operands[j] = Operand;
5844 }
5845 }
5846
5847 switch (N->getOpcode()) {
5848 default:
5849 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
5850 &Operands[0], Operands.size()));
5851 break;
5852 case ISD::SHL:
5853 case ISD::SRA:
5854 case ISD::SRL:
5855 case ISD::ROTL:
5856 case ISD::ROTR:
5857 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
5858 getShiftAmountOperand(Operands[1])));
5859 break;
5860 }
5861 }
5862
5863 for (; i < ResNE; ++i)
5864 Scalars.push_back(getUNDEF(EltVT));
5865
5866 return getNode(ISD::BUILD_VECTOR, dl,
5867 EVT::getVectorVT(*getContext(), EltVT, ResNE),
5868 &Scalars[0], Scalars.size());
5869}
5870
Evan Chengf2dc5c72009-12-09 01:04:59 +00005871/// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
5872/// it cannot be inferred.
Evan Cheng7ced2e02009-12-09 01:10:37 +00005873unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
Evan Chengf2dc5c72009-12-09 01:04:59 +00005874 // If this is a direct reference to a stack slot, use information about the
5875 // stack slot's alignment.
5876 int FrameIdx = 1 << 31;
5877 int64_t FrameOffset = 0;
5878 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
5879 FrameIdx = FI->getIndex();
5880 } else if (Ptr.getOpcode() == ISD::ADD &&
5881 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
5882 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5883 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5884 FrameOffset = Ptr.getConstantOperandVal(1);
5885 }
5886
5887 if (FrameIdx != (1 << 31)) {
5888 // FIXME: Handle FI+CST.
5889 const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
5890 if (MFI.isFixedObjectIndex(FrameIdx)) {
5891 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
5892
5893 // The alignment of the frame index can be determined from its offset from
5894 // the incoming frame position. If the frame object is at offset 32 and
5895 // the stack is guaranteed to be 16-byte aligned, then we know that the
5896 // object is 16-byte aligned.
5897 unsigned StackAlign = getTarget().getFrameInfo()->getStackAlignment();
5898 unsigned Align = MinAlign(ObjectOffset, StackAlign);
5899
5900 // Finally, the frame object itself may have a known alignment. Factor
5901 // the alignment + offset into a new alignment. For example, if we know
5902 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
5903 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
5904 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
5905 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
5906 FrameOffset);
5907 return std::max(Align, FIInfoAlign);
5908 }
5909 }
5910
5911 return 0;
5912}
5913
Chris Lattnerc3aae252005-01-07 07:46:32 +00005914void SelectionDAG::dump() const {
Chris Lattner45cfe542009-08-23 06:03:38 +00005915 errs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
Scott Michelfdc40a02009-02-17 22:15:04 +00005916
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005917 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5918 I != E; ++I) {
5919 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005920 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005921 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005922 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005923
Gabor Greifba36cb52008-08-28 21:40:38 +00005924 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005925
Chris Lattner45cfe542009-08-23 06:03:38 +00005926 errs() << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005927}
5928
Stuart Hastings80d69772009-02-04 16:46:19 +00005929void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
5930 print_types(OS, G);
5931 print_details(OS, G);
5932}
5933
5934typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
Stuart Hastings4caa0422009-02-04 20:30:10 +00005935static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
Bill Wendling51b16f42009-05-30 01:09:53 +00005936 const SelectionDAG *G, VisitedSDNodeSet &once) {
5937 if (!once.insert(N)) // If we've been here before, return now.
Stuart Hastings80d69772009-02-04 16:46:19 +00005938 return;
5939 // Dump the current SDNode, but don't end the line yet.
5940 OS << std::string(indent, ' ');
5941 N->printr(OS, G);
5942 // Having printed this SDNode, walk the children:
5943 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5944 const SDNode *child = N->getOperand(i).getNode();
5945 if (i) OS << ",";
5946 OS << " ";
5947 if (child->getNumOperands() == 0) {
5948 // This child has no grandchildren; print it inline right here.
5949 child->printr(OS, G);
5950 once.insert(child);
Bill Wendling51b16f42009-05-30 01:09:53 +00005951 } else { // Just the address. FIXME: also print the child's opcode
Stuart Hastings80d69772009-02-04 16:46:19 +00005952 OS << (void*)child;
5953 if (unsigned RN = N->getOperand(i).getResNo())
Bill Wendling51b16f42009-05-30 01:09:53 +00005954 OS << ":" << RN;
Stuart Hastings80d69772009-02-04 16:46:19 +00005955 }
5956 }
5957 OS << "\n";
5958 // Dump children that have grandchildren on their own line(s).
5959 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5960 const SDNode *child = N->getOperand(i).getNode();
5961 DumpNodesr(OS, child, indent+2, G, once);
5962 }
5963}
5964
5965void SDNode::dumpr() const {
5966 VisitedSDNodeSet once;
5967 DumpNodesr(errs(), this, 0, 0, once);
Stuart Hastings80d69772009-02-04 16:46:19 +00005968}
5969
Dan Gohman8fc13cb2009-09-25 00:34:34 +00005970void SDNode::dumpr(const SelectionDAG *G) const {
5971 VisitedSDNodeSet once;
5972 DumpNodesr(errs(), this, 0, G, once);
5973}
5974
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00005975
5976// getAddressSpace - Return the address space this GlobalAddress belongs to.
5977unsigned GlobalAddressSDNode::getAddressSpace() const {
5978 return getGlobal()->getType()->getAddressSpace();
5979}
5980
5981
Evan Chengd6594ae2006-09-12 21:00:35 +00005982const Type *ConstantPoolSDNode::getType() const {
5983 if (isMachineConstantPoolEntry())
5984 return Val.MachineCPVal->getType();
5985 return Val.ConstVal->getType();
5986}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005987
Bob Wilson24e338e2009-03-02 23:24:16 +00005988bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
5989 APInt &SplatUndef,
5990 unsigned &SplatBitSize,
5991 bool &HasAnyUndefs,
Dale Johannesen1e608812009-11-13 01:45:18 +00005992 unsigned MinSplatBits,
5993 bool isBigEndian) {
Owen Andersone50ed302009-08-10 22:56:29 +00005994 EVT VT = getValueType(0);
Bob Wilson24e338e2009-03-02 23:24:16 +00005995 assert(VT.isVector() && "Expected a vector type");
5996 unsigned sz = VT.getSizeInBits();
5997 if (MinSplatBits > sz)
5998 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005999
Bob Wilson24e338e2009-03-02 23:24:16 +00006000 SplatValue = APInt(sz, 0);
6001 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006002
Bob Wilson24e338e2009-03-02 23:24:16 +00006003 // Get the bits. Bits with undefined values (when the corresponding element
6004 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
6005 // in SplatValue. If any of the values are not constant, give up and return
6006 // false.
6007 unsigned int nOps = getNumOperands();
6008 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
6009 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
Dale Johannesen1e608812009-11-13 01:45:18 +00006010
6011 for (unsigned j = 0; j < nOps; ++j) {
6012 unsigned i = isBigEndian ? nOps-1-j : j;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006013 SDValue OpVal = getOperand(i);
Dale Johannesen1e608812009-11-13 01:45:18 +00006014 unsigned BitPos = j * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006015
Bob Wilson24e338e2009-03-02 23:24:16 +00006016 if (OpVal.getOpcode() == ISD::UNDEF)
Dale Johannesen1e608812009-11-13 01:45:18 +00006017 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
Bob Wilson24e338e2009-03-02 23:24:16 +00006018 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Bob Wilsonb1303d02009-04-13 22:05:19 +00006019 SplatValue |= (APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
6020 zextOrTrunc(sz) << BitPos);
Bob Wilson24e338e2009-03-02 23:24:16 +00006021 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00006022 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00006023 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006024 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006025 }
6026
Bob Wilson24e338e2009-03-02 23:24:16 +00006027 // The build_vector is all constants or undefs. Find the smallest element
6028 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006029
Bob Wilson24e338e2009-03-02 23:24:16 +00006030 HasAnyUndefs = (SplatUndef != 0);
6031 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006032
Bob Wilson24e338e2009-03-02 23:24:16 +00006033 unsigned HalfSize = sz / 2;
6034 APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
6035 APInt LowValue = APInt(SplatValue).trunc(HalfSize);
6036 APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
6037 APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006038
Bob Wilson24e338e2009-03-02 23:24:16 +00006039 // If the two halves do not match (ignoring undef bits), stop here.
6040 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
6041 MinSplatBits > HalfSize)
6042 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006043
Bob Wilson24e338e2009-03-02 23:24:16 +00006044 SplatValue = HighValue | LowValue;
6045 SplatUndef = HighUndef & LowUndef;
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006046
Bob Wilson24e338e2009-03-02 23:24:16 +00006047 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006048 }
6049
Bob Wilson24e338e2009-03-02 23:24:16 +00006050 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00006051 return true;
6052}
Nate Begeman9008ca62009-04-27 18:41:29 +00006053
Owen Andersone50ed302009-08-10 22:56:29 +00006054bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00006055 // Find the first non-undef value in the shuffle mask.
6056 unsigned i, e;
6057 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
6058 /* search */;
6059
Nate Begemana6415752009-04-29 18:13:31 +00006060 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Eric Christopher4d7c18c2009-08-22 00:40:45 +00006061
Nate Begeman5a5ca152009-04-29 05:20:52 +00006062 // Make sure all remaining elements are either undef or the same as the first
6063 // non-undef value.
6064 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00006065 if (Mask[i] >= 0 && Mask[i] != Idx)
6066 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00006067 return true;
6068}
Mon P Wangcd6e7252009-11-30 02:42:02 +00006069