blob: 87a0b1f3a0b2034b6f4e57f26716aa3534e40cb8 [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"
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +000016#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000017#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000018#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000020#include "llvm/Assembly/Writer.h"
Dan Gohman707e0182008-04-12 04:36:06 +000021#include "llvm/CallingConv.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000029#include "llvm/Target/TargetLowering.h"
Dan Gohman760f86f2009-01-22 21:58:43 +000030#include "llvm/Target/TargetOptions.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000033#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Owen Andersonb4459082009-06-25 17:09:00 +000035#include "llvm/Support/ManagedStatic.h"
Chris Lattner944fac72008-08-23 22:23:09 +000036#include "llvm/Support/MathExtras.h"
37#include "llvm/Support/raw_ostream.h"
Owen Andersonb4459082009-06-25 17:09:00 +000038#include "llvm/System/Mutex.h"
Chris Lattner012f2412006-02-17 21:58:01 +000039#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000040#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000041#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000042#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000043#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000044#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000045#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000046using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000047
Chris Lattner0b3e5252006-08-15 19:11:05 +000048/// makeVTList - Return an instance of the SDVTList struct initialized with the
49/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000050static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000051 SDVTList Res = {VTs, NumVTs};
52 return Res;
53}
54
Duncan Sands83ec4b62008-06-06 12:08:01 +000055static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
56 switch (VT.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000057 default: llvm_unreachable("Unknown FP format");
Chris Lattnerec4a5672008-03-05 06:48:13 +000058 case MVT::f32: return &APFloat::IEEEsingle;
59 case MVT::f64: return &APFloat::IEEEdouble;
60 case MVT::f80: return &APFloat::x87DoubleExtended;
61 case MVT::f128: return &APFloat::IEEEquad;
62 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
63 }
64}
65
Chris Lattnerf8dc0612008-02-03 06:49:24 +000066SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
67
Jim Laskey58b968b2005-08-17 20:08:02 +000068//===----------------------------------------------------------------------===//
69// ConstantFPSDNode Class
70//===----------------------------------------------------------------------===//
71
72/// isExactlyValue - We don't rely on operator== working on double values, as
73/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
74/// As such, this method can be used to do an exact bit-for-bit comparison of
75/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000076bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000077 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000078}
79
Duncan Sands83ec4b62008-06-06 12:08:01 +000080bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000081 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000082 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Scott Michelfdc40a02009-02-17 22:15:04 +000083
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000084 // PPC long double cannot be converted to any other type.
85 if (VT == MVT::ppcf128 ||
86 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000087 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +000088
Dale Johannesenf04afdb2007-08-30 00:23:21 +000089 // convert modifies in place, so make a copy.
90 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000091 bool losesInfo;
92 (void) Val2.convert(*MVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
93 &losesInfo);
94 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000095}
96
Jim Laskey58b968b2005-08-17 20:08:02 +000097//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000098// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000099//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +0000100
Evan Chenga8df1662006-03-27 06:58:47 +0000101/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +0000102/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +0000103bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000104 // Look through a bit convert.
105 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000106 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000107
Evan Chenga8df1662006-03-27 06:58:47 +0000108 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000109
Chris Lattner61d43992006-03-25 22:57:01 +0000110 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000111
Chris Lattner61d43992006-03-25 22:57:01 +0000112 // Skip over all of the undef values.
113 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
114 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
Chris Lattner61d43992006-03-25 22:57:01 +0000116 // Do not accept an all-undef vector.
117 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Chris Lattner61d43992006-03-25 22:57:01 +0000119 // Do not accept build_vectors that aren't all constants or which have non-~0
120 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000122 if (isa<ConstantSDNode>(NotZero)) {
123 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
124 return false;
125 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000126 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen23a98552008-10-09 23:00:39 +0000127 bitcastToAPInt().isAllOnesValue())
Dan Gohman6c231502008-02-29 01:47:35 +0000128 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000130 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Chris Lattner61d43992006-03-25 22:57:01 +0000132 // Okay, we have at least one ~0 value, check to see if the rest match or are
133 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000134 for (++i; i != e; ++i)
135 if (N->getOperand(i) != NotZero &&
136 N->getOperand(i).getOpcode() != ISD::UNDEF)
137 return false;
138 return true;
139}
140
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142/// isBuildVectorAllZeros - Return true if the specified node is a
143/// BUILD_VECTOR where all of the elements are 0 or undef.
144bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000145 // Look through a bit convert.
146 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000147 N = N->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +0000148
Evan Cheng4a147842006-03-26 09:50:58 +0000149 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000150
Evan Chenga8df1662006-03-27 06:58:47 +0000151 unsigned i = 0, e = N->getNumOperands();
Scott Michelfdc40a02009-02-17 22:15:04 +0000152
Evan Chenga8df1662006-03-27 06:58:47 +0000153 // Skip over all of the undef values.
154 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
155 ++i;
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Evan Chenga8df1662006-03-27 06:58:47 +0000157 // Do not accept an all-undef vector.
158 if (i == e) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000159
Dan Gohman68f32cb2009-06-04 16:49:15 +0000160 // Do not accept build_vectors that aren't all constants or which have non-0
Evan Chenga8df1662006-03-27 06:58:47 +0000161 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000162 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000163 if (isa<ConstantSDNode>(Zero)) {
164 if (!cast<ConstantSDNode>(Zero)->isNullValue())
165 return false;
166 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000167 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000168 return false;
169 } else
170 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000171
Dan Gohman68f32cb2009-06-04 16:49:15 +0000172 // Okay, we have at least one 0 value, check to see if the rest match or are
Evan Chenga8df1662006-03-27 06:58:47 +0000173 // undefs.
174 for (++i; i != e; ++i)
175 if (N->getOperand(i) != Zero &&
176 N->getOperand(i).getOpcode() != ISD::UNDEF)
177 return false;
178 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000179}
180
Evan Chengefec7512008-02-18 23:04:32 +0000181/// isScalarToVector - Return true if the specified node is a
182/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
183/// element is not an undef.
184bool ISD::isScalarToVector(const SDNode *N) {
185 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
186 return true;
187
188 if (N->getOpcode() != ISD::BUILD_VECTOR)
189 return false;
190 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
191 return false;
192 unsigned NumElems = N->getNumOperands();
193 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000194 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000195 if (V.getOpcode() != ISD::UNDEF)
196 return false;
197 }
198 return true;
199}
200
201
Evan Chengbb81d972008-01-31 09:59:15 +0000202/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000203/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000204bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000206 if (N->getOpcode() == ISD::DBG_LABEL)
207 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000208 if (N->isMachineOpcode() &&
209 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000210 return true;
211 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000212}
213
Chris Lattnerc3aae252005-01-07 07:46:32 +0000214/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
215/// when given the operation for (X op Y).
216ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
217 // To perform this operation, we just need to swap the L and G bits of the
218 // operation.
219 unsigned OldL = (Operation >> 2) & 1;
220 unsigned OldG = (Operation >> 1) & 1;
221 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
222 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000223 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000224}
225
226/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
227/// 'op' is a valid SetCC operation.
228ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
229 unsigned Operation = Op;
230 if (isInteger)
231 Operation ^= 7; // Flip L, G, E bits, but not U.
232 else
233 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000234
Chris Lattnerc3aae252005-01-07 07:46:32 +0000235 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000236 Operation &= ~8; // Don't let N and U bits get set.
237
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238 return ISD::CondCode(Operation);
239}
240
241
242/// isSignedOp - For an integer comparison, return 1 if the comparison is a
243/// signed operation and 2 if the result is an unsigned comparison. Return zero
244/// if the operation does not depend on the sign of the input (setne and seteq).
245static int isSignedOp(ISD::CondCode Opcode) {
246 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000247 default: llvm_unreachable("Illegal integer setcc operation!");
Chris Lattnerc3aae252005-01-07 07:46:32 +0000248 case ISD::SETEQ:
249 case ISD::SETNE: return 0;
250 case ISD::SETLT:
251 case ISD::SETLE:
252 case ISD::SETGT:
253 case ISD::SETGE: return 1;
254 case ISD::SETULT:
255 case ISD::SETULE:
256 case ISD::SETUGT:
257 case ISD::SETUGE: return 2;
258 }
259}
260
261/// getSetCCOrOperation - Return the result of a logical OR between different
262/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
263/// returns SETCC_INVALID if it is not possible to represent the resultant
264/// comparison.
265ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
266 bool isInteger) {
267 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
268 // Cannot fold a signed integer setcc with an unsigned integer setcc.
269 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000270
Chris Lattnerc3aae252005-01-07 07:46:32 +0000271 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000272
Chris Lattnerc3aae252005-01-07 07:46:32 +0000273 // If the N and U bits get set then the resultant comparison DOES suddenly
274 // care about orderedness, and is true when ordered.
275 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000276 Op &= ~16; // Clear the U bit if the N bit is set.
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
Chris Lattnere41102b2006-05-12 17:03:46 +0000278 // Canonicalize illegal integer setcc's.
279 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
280 Op = ISD::SETNE;
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Chris Lattnerc3aae252005-01-07 07:46:32 +0000282 return ISD::CondCode(Op);
283}
284
285/// getSetCCAndOperation - Return the result of a logical AND between different
286/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
287/// function returns zero if it is not possible to represent the resultant
288/// comparison.
289ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
290 bool isInteger) {
291 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
292 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000293 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000294
295 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000296 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000297
Chris Lattnera83385f2006-04-27 05:01:07 +0000298 // Canonicalize illegal integer setcc's.
299 if (isInteger) {
300 switch (Result) {
301 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000302 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000303 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000304 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
305 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
306 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000307 }
308 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000309
Chris Lattnera83385f2006-04-27 05:01:07 +0000310 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000311}
312
Chris Lattnerb48da392005-01-23 04:39:44 +0000313const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000314 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000315}
316
Jim Laskey58b968b2005-08-17 20:08:02 +0000317//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000318// SDNode Profile Support
319//===----------------------------------------------------------------------===//
320
Jim Laskeydef69b92006-10-27 23:52:51 +0000321/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
322///
Jim Laskey583bd472006-10-27 23:46:08 +0000323static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
324 ID.AddInteger(OpC);
325}
326
327/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
328/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000329static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000330 ID.AddPointer(VTList.VTs);
Jim Laskey583bd472006-10-27 23:46:08 +0000331}
332
Jim Laskeydef69b92006-10-27 23:52:51 +0000333/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
334///
Jim Laskey583bd472006-10-27 23:46:08 +0000335static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000336 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000337 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000338 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000339 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000340 }
Jim Laskey583bd472006-10-27 23:46:08 +0000341}
342
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000343/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
344///
345static void AddNodeIDOperands(FoldingSetNodeID &ID,
346 const SDUse *Ops, unsigned NumOps) {
347 for (; NumOps; --NumOps, ++Ops) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000348 ID.AddPointer(Ops->getNode());
349 ID.AddInteger(Ops->getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000350 }
351}
352
Jim Laskey583bd472006-10-27 23:46:08 +0000353static void AddNodeIDNode(FoldingSetNodeID &ID,
Scott Michelfdc40a02009-02-17 22:15:04 +0000354 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000355 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000356 AddNodeIDOpcode(ID, OpC);
357 AddNodeIDValueTypes(ID, VTList);
358 AddNodeIDOperands(ID, OpList, N);
359}
360
Duncan Sands0dc40452008-10-27 15:30:53 +0000361/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
362/// the NodeID data.
363static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000364 switch (N->getOpcode()) {
Chris Lattner2a4ed822009-06-25 21:21:14 +0000365 case ISD::TargetExternalSymbol:
366 case ISD::ExternalSymbol:
Torok Edwinc23197a2009-07-14 16:55:14 +0000367 llvm_unreachable("Should only be used on nodes with operands");
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 default: break; // Normal nodes don't need extra info.
369 case ISD::TargetConstant:
370 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000371 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000372 break;
373 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000374 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000375 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000376 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000377 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000378 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000379 case ISD::GlobalAddress:
380 case ISD::TargetGlobalTLSAddress:
381 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000382 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000383 ID.AddPointer(GA->getGlobal());
384 ID.AddInteger(GA->getOffset());
Chris Lattner2a4ed822009-06-25 21:21:14 +0000385 ID.AddInteger(GA->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000386 break;
387 }
388 case ISD::BasicBlock:
389 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
390 break;
391 case ISD::Register:
392 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
393 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000394 case ISD::DBG_STOPPOINT: {
395 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
396 ID.AddInteger(DSP->getLine());
397 ID.AddInteger(DSP->getColumn());
398 ID.AddPointer(DSP->getCompileUnit());
399 break;
400 }
Dan Gohman69de1932008-02-06 22:27:42 +0000401 case ISD::SRCVALUE:
402 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
403 break;
404 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000405 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000406 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000407 break;
408 }
409 case ISD::FrameIndex:
410 case ISD::TargetFrameIndex:
411 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
412 break;
413 case ISD::JumpTable:
414 case ISD::TargetJumpTable:
415 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000416 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000417 break;
418 case ISD::ConstantPool:
419 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000420 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000421 ID.AddInteger(CP->getAlignment());
422 ID.AddInteger(CP->getOffset());
423 if (CP->isMachineConstantPoolEntry())
424 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
425 else
426 ID.AddPointer(CP->getConstVal());
Chris Lattnerf5a55462009-06-25 21:35:31 +0000427 ID.AddInteger(CP->getTargetFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000428 break;
429 }
430 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000431 const LoadSDNode *LD = cast<LoadSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000432 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000433 ID.AddInteger(LD->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000434 break;
435 }
436 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000437 const StoreSDNode *ST = cast<StoreSDNode>(N);
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000438 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +0000439 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000440 break;
441 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000442 case ISD::ATOMIC_CMP_SWAP:
443 case ISD::ATOMIC_SWAP:
444 case ISD::ATOMIC_LOAD_ADD:
445 case ISD::ATOMIC_LOAD_SUB:
446 case ISD::ATOMIC_LOAD_AND:
447 case ISD::ATOMIC_LOAD_OR:
448 case ISD::ATOMIC_LOAD_XOR:
449 case ISD::ATOMIC_LOAD_NAND:
450 case ISD::ATOMIC_LOAD_MIN:
451 case ISD::ATOMIC_LOAD_MAX:
452 case ISD::ATOMIC_LOAD_UMIN:
453 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000454 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
Dan Gohmana7ce7412009-02-03 00:08:45 +0000455 ID.AddInteger(AT->getMemoryVT().getRawBits());
456 ID.AddInteger(AT->getRawSubclassData());
Mon P Wang28873102008-06-25 08:15:39 +0000457 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000458 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000459 case ISD::VECTOR_SHUFFLE: {
460 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
461 for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
462 i != e; ++i)
463 ID.AddInteger(SVN->getMaskElt(i));
464 break;
465 }
Mon P Wang28873102008-06-25 08:15:39 +0000466 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000467}
468
Duncan Sands0dc40452008-10-27 15:30:53 +0000469/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
470/// data.
471static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
472 AddNodeIDOpcode(ID, N->getOpcode());
473 // Add the return value info.
474 AddNodeIDValueTypes(ID, N->getVTList());
475 // Add the operand info.
476 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
477
478 // Handle SDNode leafs with special info.
479 AddNodeIDCustom(ID, N);
480}
481
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000482/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
Dan Gohmana7ce7412009-02-03 00:08:45 +0000483/// the CSE map that carries alignment, volatility, indexing mode, and
484/// extension/truncation information.
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000485///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000486static inline unsigned
Dan Gohmana7ce7412009-02-03 00:08:45 +0000487encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM,
488 bool isVolatile, unsigned Alignment) {
489 assert((ConvType & 3) == ConvType &&
490 "ConvType may not require more than 2 bits!");
491 assert((AM & 7) == AM &&
492 "AM may not require more than 3 bits!");
493 return ConvType |
494 (AM << 2) |
495 (isVolatile << 5) |
496 ((Log2_32(Alignment) + 1) << 6);
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000497}
498
Jim Laskey583bd472006-10-27 23:46:08 +0000499//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000500// SelectionDAG Class
501//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000502
Duncan Sands0dc40452008-10-27 15:30:53 +0000503/// doNotCSE - Return true if CSE should not be performed for this node.
504static bool doNotCSE(SDNode *N) {
505 if (N->getValueType(0) == MVT::Flag)
506 return true; // Never CSE anything that produces a flag.
507
508 switch (N->getOpcode()) {
509 default: break;
510 case ISD::HANDLENODE:
511 case ISD::DBG_LABEL:
512 case ISD::DBG_STOPPOINT:
513 case ISD::EH_LABEL:
514 case ISD::DECLARE:
515 return true; // Never CSE these nodes.
516 }
517
518 // Check that remaining values produced are not flags.
519 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
520 if (N->getValueType(i) == MVT::Flag)
521 return true; // Never CSE anything that produces a flag.
522
523 return false;
524}
525
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000526/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000527/// SelectionDAG.
528void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000529 // Create a dummy node (which is not added to allnodes), that adds a reference
530 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000531 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000532
Chris Lattner190a4182006-08-04 17:45:20 +0000533 SmallVector<SDNode*, 128> DeadNodes;
Scott Michelfdc40a02009-02-17 22:15:04 +0000534
Chris Lattner190a4182006-08-04 17:45:20 +0000535 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000536 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000537 if (I->use_empty())
538 DeadNodes.push_back(I);
539
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000540 RemoveDeadNodes(DeadNodes);
Scott Michelfdc40a02009-02-17 22:15:04 +0000541
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000542 // If the root changed (e.g. it was a dead load, update the root).
543 setRoot(Dummy.getValue());
544}
545
546/// RemoveDeadNodes - This method deletes the unreachable nodes in the
547/// given list, and any nodes that become unreachable as a result.
548void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
549 DAGUpdateListener *UpdateListener) {
550
Chris Lattner190a4182006-08-04 17:45:20 +0000551 // Process the worklist, deleting the nodes and adding their uses to the
552 // worklist.
553 while (!DeadNodes.empty()) {
Dan Gohmane7852d02009-01-26 04:35:06 +0000554 SDNode *N = DeadNodes.pop_back_val();
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000556 if (UpdateListener)
557 UpdateListener->NodeDeleted(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000558
Chris Lattner190a4182006-08-04 17:45:20 +0000559 // Take the node out of the appropriate CSE map.
560 RemoveNodeFromCSEMaps(N);
561
562 // Next, brutally remove the operand list. This is safe to do, as there are
563 // no cycles in the graph.
Dan Gohmane7852d02009-01-26 04:35:06 +0000564 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
565 SDUse &Use = *I++;
566 SDNode *Operand = Use.getNode();
567 Use.set(SDValue());
568
Chris Lattner190a4182006-08-04 17:45:20 +0000569 // Now that we removed this operand, see if there are no uses of it left.
570 if (Operand->use_empty())
571 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000572 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000573
Dan Gohmanc5336122009-01-19 22:39:36 +0000574 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000575 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000576}
577
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000578void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000579 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000580 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000581}
582
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000583void SelectionDAG::DeleteNode(SDNode *N) {
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000584 // First take this out of the appropriate CSE map.
585 RemoveNodeFromCSEMaps(N);
586
Scott Michelfdc40a02009-02-17 22:15:04 +0000587 // Finally, remove uses due to operands of this node, remove from the
Chris Lattner1e111c72005-09-07 05:37:01 +0000588 // AllNodes list, and delete the node.
589 DeleteNodeNotInCSEMaps(N);
590}
591
592void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmane77f89d2009-01-25 16:20:37 +0000593 assert(N != AllNodes.begin() && "Cannot delete the entry node!");
594 assert(N->use_empty() && "Cannot delete a node that is not dead!");
Dan Gohmanc5336122009-01-19 22:39:36 +0000595
Dan Gohmanf06c8352008-09-30 18:30:35 +0000596 // Drop all of the operands and decrement used node's use counts.
Dan Gohmane7852d02009-01-26 04:35:06 +0000597 N->DropOperands();
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000598
Dan Gohmanc5336122009-01-19 22:39:36 +0000599 DeallocateNode(N);
600}
601
602void SelectionDAG::DeallocateNode(SDNode *N) {
603 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000604 delete[] N->OperandList;
Scott Michelfdc40a02009-02-17 22:15:04 +0000605
Dan Gohmanc5336122009-01-19 22:39:36 +0000606 // Set the opcode to DELETED_NODE to help catch bugs when node
607 // memory is reallocated.
608 N->NodeType = ISD::DELETED_NODE;
609
Dan Gohman11467282008-08-26 01:44:34 +0000610 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000611}
612
Chris Lattner149c58c2005-08-16 18:17:10 +0000613/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
614/// correspond to it. This is useful when we're about to delete or repurpose
615/// the node. We don't want future request for structurally identical nodes
616/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000617bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000618 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000619 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000620 case ISD::EntryToken:
Torok Edwinc23197a2009-07-14 16:55:14 +0000621 llvm_unreachable("EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000622 return false;
623 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000624 case ISD::CONDCODE:
625 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
626 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000627 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000628 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
629 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000630 case ISD::ExternalSymbol:
631 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000632 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000633 case ISD::TargetExternalSymbol: {
634 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
635 Erased = TargetExternalSymbols.erase(
636 std::pair<std::string,unsigned char>(ESN->getSymbol(),
637 ESN->getTargetFlags()));
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000638 break;
Chris Lattner1af22312009-06-25 18:45:50 +0000639 }
Duncan Sandsf411b832007-10-17 13:49:58 +0000640 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000641 MVT VT = cast<VTSDNode>(N)->getVT();
642 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000643 Erased = ExtendedValueTypeNodes.erase(VT);
644 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000645 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
646 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000647 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000648 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000649 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000650 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000651 // Remove it from the CSE Map.
652 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000653 break;
654 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000655#ifndef NDEBUG
Scott Michelfdc40a02009-02-17 22:15:04 +0000656 // Verify that the node was actually in one of the CSE maps, unless it has a
Chris Lattner6621e3b2005-09-02 19:15:44 +0000657 // flag result (which cannot be CSE'd) or is one of the special cases that are
658 // not subject to CSE.
659 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000660 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000661 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000662 cerr << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000663 llvm_unreachable("Node is not in map!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000664 }
665#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000666 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000667}
668
Dan Gohman39946102009-01-25 16:29:12 +0000669/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
670/// maps and modified in place. Add it back to the CSE maps, unless an identical
671/// node already exists, in which case transfer all its users to the existing
672/// node. This transfer can potentially trigger recursive merging.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000673///
Dan Gohman39946102009-01-25 16:29:12 +0000674void
675SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
676 DAGUpdateListener *UpdateListener) {
677 // For node types that aren't CSE'd, just act as if no identical node
678 // already exists.
679 if (!doNotCSE(N)) {
680 SDNode *Existing = CSEMap.GetOrInsertNode(N);
681 if (Existing != N) {
682 // If there was already an existing matching node, use ReplaceAllUsesWith
683 // to replace the dead one with the existing one. This can cause
684 // recursive merging of other unrelated nodes down the line.
685 ReplaceAllUsesWith(N, Existing, UpdateListener);
Evan Cheng71e86852008-07-08 20:06:39 +0000686
Dan Gohman39946102009-01-25 16:29:12 +0000687 // N is now dead. Inform the listener if it exists and delete it.
Scott Michelfdc40a02009-02-17 22:15:04 +0000688 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000689 UpdateListener->NodeDeleted(N, Existing);
690 DeleteNodeNotInCSEMaps(N);
691 return;
692 }
693 }
Evan Cheng71e86852008-07-08 20:06:39 +0000694
Dan Gohman39946102009-01-25 16:29:12 +0000695 // If the node doesn't already exist, we updated it. Inform a listener if
696 // it exists.
Scott Michelfdc40a02009-02-17 22:15:04 +0000697 if (UpdateListener)
Dan Gohman39946102009-01-25 16:29:12 +0000698 UpdateListener->NodeUpdated(N);
Chris Lattner8b8749f2005-08-17 19:00:20 +0000699}
700
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000701/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000702/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000703/// return null, otherwise return a pointer to the slot it would take. If a
704/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000705SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000706 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000707 if (doNotCSE(N))
708 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000709
Dan Gohman475871a2008-07-27 21:46:04 +0000710 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000711 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000712 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000713 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000714 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000715}
716
717/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000718/// were replaced with those specified. If this node is never memoized,
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000719/// return null, otherwise return a pointer to the slot it would take. If a
720/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000721SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000722 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000723 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000724 if (doNotCSE(N))
725 return 0;
726
Dan Gohman475871a2008-07-27 21:46:04 +0000727 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000728 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000729 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000730 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000731 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
732}
733
734
735/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
Scott Michelfdc40a02009-02-17 22:15:04 +0000736/// were replaced with those specified. If this node is never memoized,
Chris Lattnera5682852006-08-07 23:03:03 +0000737/// return null, otherwise return a pointer to the slot it would take. If a
738/// node already exists with these operands, the slot will be non-null.
Scott Michelfdc40a02009-02-17 22:15:04 +0000739SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000740 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000741 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000742 if (doNotCSE(N))
743 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000744
Jim Laskey583bd472006-10-27 23:46:08 +0000745 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000746 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000747 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000748 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000749}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000750
Duncan Sandsd038e042008-07-21 10:20:31 +0000751/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
752void SelectionDAG::VerifyNode(SDNode *N) {
753 switch (N->getOpcode()) {
754 default:
755 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000756 case ISD::BUILD_PAIR: {
757 MVT VT = N->getValueType(0);
758 assert(N->getNumValues() == 1 && "Too many results!");
759 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
760 "Wrong return type!");
761 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
762 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
763 "Mismatched operand types!");
764 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
765 "Wrong operand type!");
766 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
767 "Wrong return type size");
768 break;
769 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000770 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000771 assert(N->getNumValues() == 1 && "Too many results!");
772 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000773 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000774 "Wrong number of operands!");
Rafael Espindola15684b22009-04-24 12:40:33 +0000775 MVT EltVT = N->getValueType(0).getVectorElementType();
776 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
777 assert((I->getValueType() == EltVT ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000778 (EltVT.isInteger() && I->getValueType().isInteger() &&
779 EltVT.bitsLE(I->getValueType()))) &&
780 "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000781 break;
782 }
783 }
784}
785
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000786/// getMVTAlignment - Compute the default alignment value for the
787/// given type.
788///
789unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
790 const Type *Ty = VT == MVT::iPTR ?
791 PointerType::get(Type::Int8Ty, 0) :
Owen Andersondebcb012009-07-29 22:17:13 +0000792 VT.getTypeForMVT();
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000793
794 return TLI.getTargetData()->getABITypeAlignment(Ty);
795}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000796
Dale Johannesen92570c42009-02-07 02:15:05 +0000797// EntryNode could meaningfully have debug info if we can find it...
Dan Gohman7c3234c2008-08-27 23:52:12 +0000798SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
Devang Patelb51d40c2009-02-03 18:46:32 +0000799 : TLI(tli), FLI(fli), DW(0),
Dale Johannesen92570c42009-02-07 02:15:05 +0000800 EntryNode(ISD::EntryToken, DebugLoc::getUnknownLoc(),
801 getVTList(MVT::Other)), Root(getEntryNode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000802 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000803}
804
Devang Patel6e7a1612009-01-09 19:11:50 +0000805void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
Owen Anderson5dcaceb2009-07-09 18:44:09 +0000806 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000807 MF = &mf;
808 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000809 DW = dw;
Owen Andersone922c022009-07-22 00:24:57 +0000810 Context = &mf.getFunction()->getContext();
Dan Gohman7c3234c2008-08-27 23:52:12 +0000811}
812
Chris Lattner78ec3112003-08-11 14:57:33 +0000813SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000814 allnodes_clear();
815}
816
817void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000818 assert(&*AllNodes.begin() == &EntryNode);
819 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000820 while (!AllNodes.empty())
821 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000822}
823
Dan Gohman7c3234c2008-08-27 23:52:12 +0000824void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000825 allnodes_clear();
826 OperandAllocator.Reset();
827 CSEMap.clear();
828
829 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000830 ExternalSymbols.clear();
831 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000832 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
833 static_cast<CondCodeSDNode*>(0));
834 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
835 static_cast<SDNode*>(0));
836
Dan Gohmane7852d02009-01-26 04:35:06 +0000837 EntryNode.UseList = 0;
Dan Gohmanf350b272008-08-23 02:25:05 +0000838 AllNodes.push_back(&EntryNode);
839 Root = getEntryNode();
840}
841
Bill Wendling6ce610f2009-01-30 22:23:15 +0000842SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, MVT VT) {
843 if (Op.getValueType() == VT) return Op;
844 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
845 VT.getSizeInBits());
846 return getNode(ISD::AND, DL, Op.getValueType(), Op,
847 getConstant(Imm, Op.getValueType()));
848}
849
Bob Wilson4c245462009-01-22 17:39:32 +0000850/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
851///
Bill Wendling41b9d272009-01-30 22:11:22 +0000852SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, MVT VT) {
Dan Gohmanbd209ab2009-04-20 22:51:43 +0000853 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
854 SDValue NegOne =
855 getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
Bill Wendling41b9d272009-01-30 22:11:22 +0000856 return getNode(ISD::XOR, DL, VT, Val, NegOne);
857}
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000860 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Dan Gohmance9bc122009-01-27 20:39:34 +0000861 assert((EltVT.getSizeInBits() >= 64 ||
862 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
863 "getConstant with a uint64_t value that doesn't fit in the type!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000864 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000865}
866
Dan Gohman475871a2008-07-27 21:46:04 +0000867SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000868 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000869}
870
871SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000873
Evan Cheng0ff39b32008-06-30 07:31:25 +0000874 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000875 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000876 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000877
878 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000879 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000880 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000881 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000882 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000883 SDNode *N = NULL;
884 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000886 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000887 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000888 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000889 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000890 CSEMap.InsertNode(N, IP);
891 AllNodes.push_back(N);
892 }
893
Dan Gohman475871a2008-07-27 21:46:04 +0000894 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000897 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +0000898 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
899 VT, &Ops[0], Ops.size());
Dan Gohman89081322007-12-12 22:21:26 +0000900 }
901 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000902}
903
Dan Gohman475871a2008-07-27 21:46:04 +0000904SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000905 return getConstant(Val, TLI.getPointerTy(), isTarget);
906}
907
908
Dan Gohman475871a2008-07-27 21:46:04 +0000909SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000910 return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000911}
912
913SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000914 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000915
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916 MVT EltVT =
917 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000918
Chris Lattnerd8658612005-02-17 20:17:32 +0000919 // Do the map lookup using the actual bit pattern for the floating point
920 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
921 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000922 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000923 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000924 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000925 ID.AddPointer(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000926 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000927 SDNode *N = NULL;
928 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000929 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000930 return SDValue(N, 0);
Evan Chengc908dcd2007-06-29 21:36:04 +0000931 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000932 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000933 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000934 CSEMap.InsertNode(N, IP);
935 AllNodes.push_back(N);
936 }
937
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000939 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 Ops.assign(VT.getVectorNumElements(), Result);
Evan Chenga87008d2009-02-25 22:49:59 +0000942 // FIXME DebugLoc info might be appropriate here
943 Result = getNode(ISD::BUILD_VECTOR, DebugLoc::getUnknownLoc(),
944 VT, &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +0000945 }
946 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947}
948
Dan Gohman475871a2008-07-27 21:46:04 +0000949SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000950 MVT EltVT =
951 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000952 if (EltVT==MVT::f32)
953 return getConstantFP(APFloat((float)Val), VT, isTarget);
954 else
955 return getConstantFP(APFloat(Val), VT, isTarget);
956}
957
Dan Gohman475871a2008-07-27 21:46:04 +0000958SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +0000959 MVT VT, int64_t Offset,
Chris Lattner2a4ed822009-06-25 21:21:14 +0000960 bool isTargetGA,
961 unsigned char TargetFlags) {
962 assert((TargetFlags == 0 || isTargetGA) &&
963 "Cannot set target flags on target-independent globals");
964
Dan Gohman6520e202008-10-18 02:06:02 +0000965 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman44013612008-10-21 03:38:42 +0000966 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000967 if (BitWidth < 64)
968 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
969
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000970 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
971 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000972 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000973 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000974 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000975 }
976
Chris Lattner2a4ed822009-06-25 21:21:14 +0000977 unsigned Opc;
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000978 if (GVar && GVar->isThreadLocal())
979 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
980 else
981 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000982
Jim Laskey583bd472006-10-27 23:46:08 +0000983 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000984 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000985 ID.AddPointer(GV);
986 ID.AddInteger(Offset);
Chris Lattner2a4ed822009-06-25 21:21:14 +0000987 ID.AddInteger(TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +0000988 void *IP = 0;
989 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman74692c02009-01-25 16:21:38 +0000990 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000991 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Chris Lattner505e9822009-06-26 21:14:05 +0000992 new (N) GlobalAddressSDNode(Opc, GV, VT, Offset, TargetFlags);
Chris Lattner61b09412006-08-11 21:01:22 +0000993 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000994 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000995 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996}
997
Dan Gohman475871a2008-07-27 21:46:04 +0000998SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000999 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +00001000 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001001 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001002 ID.AddInteger(FI);
1003 void *IP = 0;
1004 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001005 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001006 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001007 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001008 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001009 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001010 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001011}
1012
Chris Lattnerf5a55462009-06-25 21:35:31 +00001013SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget,
1014 unsigned char TargetFlags) {
1015 assert((TargetFlags == 0 || isTarget) &&
1016 "Cannot set target flags on target-independent jump tables");
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001017 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +00001018 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001019 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001020 ID.AddInteger(JTI);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001021 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001022 void *IP = 0;
1023 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001024 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001025 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001026 new (N) JumpTableSDNode(JTI, VT, isTarget, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001027 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +00001028 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001029 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +00001030}
1031
Dan Gohman475871a2008-07-27 21:46:04 +00001032SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
1033 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001034 bool isTarget,
1035 unsigned char TargetFlags) {
1036 assert((TargetFlags == 0 || isTarget) &&
1037 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001038 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001039 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001040 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001041 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001042 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001043 ID.AddInteger(Alignment);
1044 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +00001045 ID.AddPointer(C);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001046 ID.AddInteger(TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001047 void *IP = 0;
1048 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001049 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001050 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001051 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Chris Lattnerc9f8f412006-08-11 21:55:30 +00001052 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001053 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001054 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +00001055}
1056
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057
Dan Gohman475871a2008-07-27 21:46:04 +00001058SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1059 unsigned Alignment, int Offset,
Chris Lattnerf5a55462009-06-25 21:35:31 +00001060 bool isTarget,
1061 unsigned char TargetFlags) {
1062 assert((TargetFlags == 0 || isTarget) &&
1063 "Cannot set target flags on target-independent globals");
Dan Gohman50284d82008-09-16 22:05:41 +00001064 if (Alignment == 0)
Evan Cheng1606e8e2009-03-13 07:51:59 +00001065 Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001066 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001067 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001068 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001069 ID.AddInteger(Alignment);
1070 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001071 C->AddSelectionDAGCSEId(ID);
Chris Lattnerf5a55462009-06-25 21:35:31 +00001072 ID.AddInteger(TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001073 void *IP = 0;
1074 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001075 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001076 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Chris Lattnerf5a55462009-06-25 21:35:31 +00001077 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment, TargetFlags);
Evan Chengd6594ae2006-09-12 21:00:35 +00001078 CSEMap.InsertNode(N, IP);
1079 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001080 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001081}
1082
Dan Gohman475871a2008-07-27 21:46:04 +00001083SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001084 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001085 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001086 ID.AddPointer(MBB);
1087 void *IP = 0;
1088 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001089 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001090 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001091 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001092 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001094 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095}
1096
Dan Gohman475871a2008-07-27 21:46:04 +00001097SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001098 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1099 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001100
Duncan Sands83ec4b62008-06-06 12:08:01 +00001101 SDNode *&N = VT.isExtended() ?
1102 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001103
Dan Gohman475871a2008-07-27 21:46:04 +00001104 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001105 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001106 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001107 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001108 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001109}
1110
Bill Wendling056292f2008-09-16 21:48:12 +00001111SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1112 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001113 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001114 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner1af22312009-06-25 18:45:50 +00001115 new (N) ExternalSymbolSDNode(false, Sym, 0, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001116 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001117 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001118}
1119
Chris Lattner1af22312009-06-25 18:45:50 +00001120SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT,
1121 unsigned char TargetFlags) {
1122 SDNode *&N =
1123 TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
1124 TargetFlags)];
Dan Gohman475871a2008-07-27 21:46:04 +00001125 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001126 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
Chris Lattner1af22312009-06-25 18:45:50 +00001127 new (N) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001129 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130}
1131
Dan Gohman475871a2008-07-27 21:46:04 +00001132SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001133 if ((unsigned)Cond >= CondCodeNodes.size())
1134 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001135
Chris Lattner079a27a2005-08-09 20:40:02 +00001136 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001137 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001138 new (N) CondCodeSDNode(Cond);
1139 CondCodeNodes[Cond] = N;
1140 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001141 }
Dan Gohman475871a2008-07-27 21:46:04 +00001142 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001143}
1144
Nate Begeman5a5ca152009-04-29 05:20:52 +00001145// commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
1146// the shuffle mask M that point at N1 to point at N2, and indices that point
1147// N2 to point at N1.
Nate Begeman9008ca62009-04-27 18:41:29 +00001148static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
1149 std::swap(N1, N2);
1150 int NElts = M.size();
1151 for (int i = 0; i != NElts; ++i) {
1152 if (M[i] >= NElts)
1153 M[i] -= NElts;
1154 else if (M[i] >= 0)
1155 M[i] += NElts;
1156 }
1157}
1158
1159SDValue SelectionDAG::getVectorShuffle(MVT VT, DebugLoc dl, SDValue N1,
1160 SDValue N2, const int *Mask) {
1161 assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
1162 assert(VT.isVector() && N1.getValueType().isVector() &&
1163 "Vector Shuffle VTs must be a vectors");
1164 assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
1165 && "Vector Shuffle VTs must have same element type");
1166
1167 // Canonicalize shuffle undef, undef -> undef
1168 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
Dan Gohman2e4284d2009-07-09 00:46:33 +00001169 return getUNDEF(VT);
Nate Begeman9008ca62009-04-27 18:41:29 +00001170
1171 // Validate that all indices in Mask are within the range of the elements
1172 // input to the shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00001173 unsigned NElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00001174 SmallVector<int, 8> MaskVec;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001175 for (unsigned i = 0; i != NElts; ++i) {
1176 assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
Nate Begeman9008ca62009-04-27 18:41:29 +00001177 MaskVec.push_back(Mask[i]);
1178 }
1179
1180 // Canonicalize shuffle v, v -> v, undef
1181 if (N1 == N2) {
1182 N2 = getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001183 for (unsigned i = 0; i != NElts; ++i)
1184 if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00001185 }
1186
1187 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
1188 if (N1.getOpcode() == ISD::UNDEF)
1189 commuteShuffle(N1, N2, MaskVec);
1190
1191 // Canonicalize all index into lhs, -> shuffle lhs, undef
1192 // Canonicalize all index into rhs, -> shuffle rhs, undef
1193 bool AllLHS = true, AllRHS = true;
1194 bool N2Undef = N2.getOpcode() == ISD::UNDEF;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001195 for (unsigned i = 0; i != NElts; ++i) {
1196 if (MaskVec[i] >= (int)NElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00001197 if (N2Undef)
1198 MaskVec[i] = -1;
1199 else
1200 AllLHS = false;
1201 } else if (MaskVec[i] >= 0) {
1202 AllRHS = false;
1203 }
1204 }
1205 if (AllLHS && AllRHS)
1206 return getUNDEF(VT);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001207 if (AllLHS && !N2Undef)
Nate Begeman9008ca62009-04-27 18:41:29 +00001208 N2 = getUNDEF(VT);
1209 if (AllRHS) {
1210 N1 = getUNDEF(VT);
1211 commuteShuffle(N1, N2, MaskVec);
1212 }
1213
1214 // If Identity shuffle, or all shuffle in to undef, return that node.
1215 bool AllUndef = true;
1216 bool Identity = true;
Nate Begeman5a5ca152009-04-29 05:20:52 +00001217 for (unsigned i = 0; i != NElts; ++i) {
1218 if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00001219 if (MaskVec[i] >= 0) AllUndef = false;
1220 }
Dan Gohman2e4284d2009-07-09 00:46:33 +00001221 if (Identity && NElts == N1.getValueType().getVectorNumElements())
Nate Begeman9008ca62009-04-27 18:41:29 +00001222 return N1;
1223 if (AllUndef)
1224 return getUNDEF(VT);
1225
1226 FoldingSetNodeID ID;
1227 SDValue Ops[2] = { N1, N2 };
1228 AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
Nate Begeman5a5ca152009-04-29 05:20:52 +00001229 for (unsigned i = 0; i != NElts; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00001230 ID.AddInteger(MaskVec[i]);
1231
1232 void* IP = 0;
1233 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1234 return SDValue(E, 0);
1235
1236 // Allocate the mask array for the node out of the BumpPtrAllocator, since
1237 // SDNode doesn't have access to it. This memory will be "leaked" when
1238 // the node is deallocated, but recovered when the NodeAllocator is released.
1239 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
1240 memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
1241
1242 ShuffleVectorSDNode *N = NodeAllocator.Allocate<ShuffleVectorSDNode>();
1243 new (N) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
1244 CSEMap.InsertNode(N, IP);
1245 AllNodes.push_back(N);
1246 return SDValue(N, 0);
1247}
1248
Dale Johannesena04b7572009-02-03 23:04:43 +00001249SDValue SelectionDAG::getConvertRndSat(MVT VT, DebugLoc dl,
1250 SDValue Val, SDValue DTy,
1251 SDValue STy, SDValue Rnd, SDValue Sat,
1252 ISD::CvtCode Code) {
Mon P Wangb0e341b2009-02-05 04:47:42 +00001253 // If the src and dest types are the same and the conversion is between
1254 // integer types of the same sign or two floats, no conversion is necessary.
1255 if (DTy == STy &&
1256 (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
Dale Johannesena04b7572009-02-03 23:04:43 +00001257 return Val;
1258
1259 FoldingSetNodeID ID;
1260 void* IP = 0;
1261 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1262 return SDValue(E, 0);
1263 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1264 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1265 new (N) CvtRndSatSDNode(VT, dl, Ops, 5, Code);
1266 CSEMap.InsertNode(N, IP);
1267 AllNodes.push_back(N);
1268 return SDValue(N, 0);
1269}
1270
Dan Gohman475871a2008-07-27 21:46:04 +00001271SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001272 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001273 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001274 ID.AddInteger(RegNo);
1275 void *IP = 0;
1276 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001277 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001278 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001279 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001280 CSEMap.InsertNode(N, IP);
1281 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001282 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001283}
1284
Chris Lattneraf29a522009-05-04 22:10:05 +00001285SDValue SelectionDAG::getDbgStopPoint(DebugLoc DL, SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001286 unsigned Line, unsigned Col,
1287 Value *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001288 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001289 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Chris Lattneraf29a522009-05-04 22:10:05 +00001290 N->setDebugLoc(DL);
Dan Gohman7f460202008-06-30 20:59:49 +00001291 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001292 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001293}
1294
Dale Johannesene8c17332009-01-29 00:47:48 +00001295SDValue SelectionDAG::getLabel(unsigned Opcode, DebugLoc dl,
1296 SDValue Root,
1297 unsigned LabelID) {
1298 FoldingSetNodeID ID;
1299 SDValue Ops[] = { Root };
1300 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1301 ID.AddInteger(LabelID);
1302 void *IP = 0;
1303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1304 return SDValue(E, 0);
1305 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
1306 new (N) LabelSDNode(Opcode, dl, Root, LabelID);
1307 CSEMap.InsertNode(N, IP);
1308 AllNodes.push_back(N);
1309 return SDValue(N, 0);
1310}
1311
Dan Gohman475871a2008-07-27 21:46:04 +00001312SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001313 assert((!V || isa<PointerType>(V->getType())) &&
1314 "SrcValue is not a pointer?");
1315
Jim Laskey583bd472006-10-27 23:46:08 +00001316 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001317 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001318 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001319
Chris Lattner61b09412006-08-11 21:01:22 +00001320 void *IP = 0;
1321 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001322 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001323
Dan Gohmanfed90b62008-07-28 21:51:04 +00001324 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001325 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001326 CSEMap.InsertNode(N, IP);
1327 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001328 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001329}
1330
Dan Gohman475871a2008-07-27 21:46:04 +00001331SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001332#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001333 const Value *v = MO.getValue();
1334 assert((!v || isa<PointerType>(v->getType())) &&
1335 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001336#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001337
1338 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001339 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001340 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001341
1342 void *IP = 0;
1343 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001344 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001345
Dan Gohmanfed90b62008-07-28 21:51:04 +00001346 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001347 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001348 CSEMap.InsertNode(N, IP);
1349 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001350 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001351}
1352
Duncan Sands92abc622009-01-31 15:50:11 +00001353/// getShiftAmountOperand - Return the specified value casted to
1354/// the target's desired shift amount type.
1355SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
1356 MVT OpTy = Op.getValueType();
1357 MVT ShTy = TLI.getShiftAmountTy();
1358 if (OpTy == ShTy || OpTy.isVector()) return Op;
1359
1360 ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001361 return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
Duncan Sands92abc622009-01-31 15:50:11 +00001362}
1363
Chris Lattner37ce9df2007-10-15 17:47:20 +00001364/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1365/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001366SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001367 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001368 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Owen Andersondebcb012009-07-29 22:17:13 +00001369 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001370 unsigned StackAlign =
1371 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
Scott Michelfdc40a02009-02-17 22:15:04 +00001372
Chris Lattner37ce9df2007-10-15 17:47:20 +00001373 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1374 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1375}
1376
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001377/// CreateStackTemporary - Create a stack temporary suitable for holding
1378/// either of the specified value types.
1379SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1380 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1381 VT2.getStoreSizeInBits())/8;
Owen Andersondebcb012009-07-29 22:17:13 +00001382 const Type *Ty1 = VT1.getTypeForMVT();
1383 const Type *Ty2 = VT2.getTypeForMVT();
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001384 const TargetData *TD = TLI.getTargetData();
1385 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1386 TD->getPrefTypeAlignment(Ty2));
1387
1388 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1389 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1390 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1391}
1392
Dan Gohman475871a2008-07-27 21:46:04 +00001393SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001394 SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001395 // These setcc operations always fold.
1396 switch (Cond) {
1397 default: break;
1398 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001399 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001400 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001401 case ISD::SETTRUE2: return getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001402
Chris Lattnera83385f2006-04-27 05:01:07 +00001403 case ISD::SETOEQ:
1404 case ISD::SETOGT:
1405 case ISD::SETOGE:
1406 case ISD::SETOLT:
1407 case ISD::SETOLE:
1408 case ISD::SETONE:
1409 case ISD::SETO:
1410 case ISD::SETUO:
1411 case ISD::SETUEQ:
1412 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001413 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001414 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001415 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001416
Gabor Greifba36cb52008-08-28 21:40:38 +00001417 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001418 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001419 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001420 const APInt &C1 = N1C->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001421
Chris Lattnerc3aae252005-01-07 07:46:32 +00001422 switch (Cond) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001423 default: llvm_unreachable("Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001424 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1425 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001426 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1427 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1428 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1429 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1430 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1431 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1432 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1433 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001434 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001435 }
Chris Lattner67255a12005-04-07 18:14:58 +00001436 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001437 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1438 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001439 // No compile time operations on this type yet.
1440 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001441 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001442
1443 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001444 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001445 default: break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001446 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001447 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001448 // fall through
1449 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001450 case ISD::SETNE: 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::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001454 R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001455 case ISD::SETLT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001456 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001457 // fall through
1458 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001459 case ISD::SETGT: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001460 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001461 // fall through
1462 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001463 case ISD::SETLE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001464 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001465 // fall through
1466 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001467 R==APFloat::cmpEqual, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001468 case ISD::SETGE: if (R==APFloat::cmpUnordered)
Dale Johannesene8d72302009-02-06 23:05:02 +00001469 return getUNDEF(VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001470 // fall through
1471 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001472 R==APFloat::cmpEqual, VT);
1473 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1474 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1475 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1476 R==APFloat::cmpEqual, VT);
1477 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1478 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1479 R==APFloat::cmpLessThan, VT);
1480 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1481 R==APFloat::cmpUnordered, VT);
1482 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1483 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001484 }
1485 } else {
1486 // Ensure that the constant occurs on the RHS.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00001487 return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001488 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001489 }
1490
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001491 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001492 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001493}
1494
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001495/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1496/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001497bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Chris Lattnera4f73182009-07-07 23:28:46 +00001498 // This predicate is not safe for vector operations.
1499 if (Op.getValueType().isVector())
1500 return false;
1501
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001502 unsigned BitWidth = Op.getValueSizeInBits();
1503 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1504}
1505
Dan Gohmanea859be2007-06-22 14:59:07 +00001506/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1507/// this predicate to simplify operations downstream. Mask is known to be zero
1508/// for bits that V cannot have.
Scott Michelfdc40a02009-02-17 22:15:04 +00001509bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001510 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001511 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001512 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001513 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001514 return (KnownZero & Mask) == Mask;
1515}
1516
1517/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1518/// known to be either zero or one and return them in the KnownZero/KnownOne
1519/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1520/// processing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001521void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001522 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001523 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001524 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001525 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001526 "Mask size mismatches value type size!");
1527
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001528 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001529 if (Depth == 6 || Mask == 0)
1530 return; // Limit search depth.
Scott Michelfdc40a02009-02-17 22:15:04 +00001531
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001532 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001533
1534 switch (Op.getOpcode()) {
1535 case ISD::Constant:
1536 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001537 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001538 KnownZero = ~KnownOne & Mask;
1539 return;
1540 case ISD::AND:
1541 // If either the LHS or the RHS are Zero, the result is zero.
1542 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001543 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1544 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001545 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1546 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001547
1548 // Output known-1 bits are only known if set in both the LHS & RHS.
1549 KnownOne &= KnownOne2;
1550 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1551 KnownZero |= KnownZero2;
1552 return;
1553 case ISD::OR:
1554 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001555 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1556 KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001557 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1558 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1559
Dan Gohmanea859be2007-06-22 14:59:07 +00001560 // Output known-0 bits are only known if clear in both the LHS & RHS.
1561 KnownZero &= KnownZero2;
1562 // Output known-1 are known to be set if set in either the LHS | RHS.
1563 KnownOne |= KnownOne2;
1564 return;
1565 case ISD::XOR: {
1566 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1567 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001568 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1569 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1570
Dan Gohmanea859be2007-06-22 14:59:07 +00001571 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001572 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001573 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1574 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1575 KnownZero = KnownZeroOut;
1576 return;
1577 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001578 case ISD::MUL: {
1579 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1580 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1581 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1582 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1583 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1584
1585 // If low bits are zero in either operand, output low known-0 bits.
1586 // Also compute a conserative estimate for high known-0 bits.
1587 // More trickiness is possible, but this is sufficient for the
1588 // interesting case of alignment computation.
1589 KnownOne.clear();
1590 unsigned TrailZ = KnownZero.countTrailingOnes() +
1591 KnownZero2.countTrailingOnes();
1592 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001593 KnownZero2.countLeadingOnes(),
1594 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001595
1596 TrailZ = std::min(TrailZ, BitWidth);
1597 LeadZ = std::min(LeadZ, BitWidth);
1598 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1599 APInt::getHighBitsSet(BitWidth, LeadZ);
1600 KnownZero &= Mask;
1601 return;
1602 }
1603 case ISD::UDIV: {
1604 // For the purposes of computing leading zeros we can conservatively
1605 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001606 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001607 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1608 ComputeMaskedBits(Op.getOperand(0),
1609 AllOnes, KnownZero2, KnownOne2, Depth+1);
1610 unsigned LeadZ = KnownZero2.countLeadingOnes();
1611
1612 KnownOne2.clear();
1613 KnownZero2.clear();
1614 ComputeMaskedBits(Op.getOperand(1),
1615 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001616 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1617 if (RHSUnknownLeadingOnes != BitWidth)
1618 LeadZ = std::min(BitWidth,
1619 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001620
1621 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1622 return;
1623 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001624 case ISD::SELECT:
1625 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1626 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001627 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1628 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1629
Dan Gohmanea859be2007-06-22 14:59:07 +00001630 // Only known if known in both the LHS and RHS.
1631 KnownOne &= KnownOne2;
1632 KnownZero &= KnownZero2;
1633 return;
1634 case ISD::SELECT_CC:
1635 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1636 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001637 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1638 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1639
Dan Gohmanea859be2007-06-22 14:59:07 +00001640 // Only known if known in both the LHS and RHS.
1641 KnownOne &= KnownOne2;
1642 KnownZero &= KnownZero2;
1643 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001644 case ISD::SADDO:
1645 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001646 case ISD::SSUBO:
1647 case ISD::USUBO:
1648 case ISD::SMULO:
1649 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001650 if (Op.getResNo() != 1)
1651 return;
Duncan Sands03228082008-11-23 15:47:28 +00001652 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001653 case ISD::SETCC:
1654 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001655 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001656 BitWidth > 1)
1657 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001658 return;
1659 case ISD::SHL:
1660 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1661 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001662 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001663
1664 // If the shift count is an invalid immediate, don't do anything.
1665 if (ShAmt >= BitWidth)
1666 return;
1667
1668 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(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 Gohmand4cf9922008-02-26 18:50:50 +00001671 KnownZero <<= ShAmt;
1672 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001673 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001674 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001675 }
1676 return;
1677 case ISD::SRL:
1678 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
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 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001687 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001688 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001689 KnownZero = KnownZero.lshr(ShAmt);
1690 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001691
Dan Gohman72d2fd52008-02-13 22:43:25 +00001692 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001693 KnownZero |= HighBits; // High bits known zero.
1694 }
1695 return;
1696 case ISD::SRA:
1697 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001698 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001699
Dan Gohmand4cf9922008-02-26 18:50:50 +00001700 // If the shift count is an invalid immediate, don't do anything.
1701 if (ShAmt >= BitWidth)
1702 return;
1703
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001704 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001705 // If any of the demanded bits are produced by the sign extension, we also
1706 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001707 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1708 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001709 InDemandedMask |= APInt::getSignBit(BitWidth);
Scott Michelfdc40a02009-02-17 22:15:04 +00001710
Dan Gohmanea859be2007-06-22 14:59:07 +00001711 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1712 Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001713 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001714 KnownZero = KnownZero.lshr(ShAmt);
1715 KnownOne = KnownOne.lshr(ShAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00001716
Dan Gohmanea859be2007-06-22 14:59:07 +00001717 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001718 APInt SignBit = APInt::getSignBit(BitWidth);
1719 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Scott Michelfdc40a02009-02-17 22:15:04 +00001720
Dan Gohmanca93a432008-02-20 16:30:17 +00001721 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001722 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001723 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001724 KnownOne |= HighBits; // New bits are known one.
1725 }
1726 }
1727 return;
1728 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1730 unsigned EBits = EVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00001731
1732 // Sign extension. Compute the demanded bits in the result that are not
Dan Gohmanea859be2007-06-22 14:59:07 +00001733 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001734 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001735
Dan Gohman977a76f2008-02-13 22:28:48 +00001736 APInt InSignBit = APInt::getSignBit(EBits);
1737 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Dan Gohmanea859be2007-06-22 14:59:07 +00001739 // If the sign extended bits are demanded, we know that the sign
1740 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001741 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001742 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001743 InputDemandedBits |= InSignBit;
Scott Michelfdc40a02009-02-17 22:15:04 +00001744
Dan Gohmanea859be2007-06-22 14:59:07 +00001745 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1746 KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001747 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1748
Dan Gohmanea859be2007-06-22 14:59:07 +00001749 // If the sign bit of the input is known set or clear, then we know the
1750 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001751 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001752 KnownZero |= NewBits;
1753 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001754 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001755 KnownOne |= NewBits;
1756 KnownZero &= ~NewBits;
1757 } else { // Input sign bit unknown
1758 KnownZero &= ~NewBits;
1759 KnownOne &= ~NewBits;
1760 }
1761 return;
1762 }
1763 case ISD::CTTZ:
1764 case ISD::CTLZ:
1765 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001766 unsigned LowBits = Log2_32(BitWidth)+1;
1767 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001768 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001769 return;
1770 }
1771 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001772 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001773 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001774 MVT VT = LD->getMemoryVT();
1775 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001776 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001777 }
1778 return;
1779 }
1780 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001781 MVT InVT = Op.getOperand(0).getValueType();
1782 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001783 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1784 APInt InMask = Mask;
1785 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001786 KnownZero.trunc(InBits);
1787 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001788 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001789 KnownZero.zext(BitWidth);
1790 KnownOne.zext(BitWidth);
1791 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001792 return;
1793 }
1794 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001795 MVT InVT = Op.getOperand(0).getValueType();
1796 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001797 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001798 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1799 APInt InMask = Mask;
1800 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001801
1802 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001803 // bit is demanded. Temporarily set this bit in the mask for our callee.
1804 if (NewBits.getBoolValue())
1805 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001806
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001807 KnownZero.trunc(InBits);
1808 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001809 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1810
1811 // Note if the sign bit is known to be zero or one.
1812 bool SignBitKnownZero = KnownZero.isNegative();
1813 bool SignBitKnownOne = KnownOne.isNegative();
1814 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1815 "Sign bit can't be known to be both zero and one!");
1816
1817 // If the sign bit wasn't actually demanded by our caller, we don't
1818 // want it set in the KnownZero and KnownOne result values. Reset the
1819 // mask and reapply it to the result values.
1820 InMask = Mask;
1821 InMask.trunc(InBits);
1822 KnownZero &= InMask;
1823 KnownOne &= InMask;
1824
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001825 KnownZero.zext(BitWidth);
1826 KnownOne.zext(BitWidth);
1827
Dan Gohman977a76f2008-02-13 22:28:48 +00001828 // If the sign bit is known zero or one, the top bits match.
1829 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001830 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001831 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001832 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001833 return;
1834 }
1835 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001836 MVT InVT = Op.getOperand(0).getValueType();
1837 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001838 APInt InMask = Mask;
1839 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001840 KnownZero.trunc(InBits);
1841 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001842 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001843 KnownZero.zext(BitWidth);
1844 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001845 return;
1846 }
1847 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001848 MVT InVT = Op.getOperand(0).getValueType();
1849 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001850 APInt InMask = Mask;
1851 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001852 KnownZero.zext(InBits);
1853 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001854 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001855 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001856 KnownZero.trunc(BitWidth);
1857 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001858 break;
1859 }
1860 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001861 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1862 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Scott Michelfdc40a02009-02-17 22:15:04 +00001863 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
Dan Gohmanea859be2007-06-22 14:59:07 +00001864 KnownOne, Depth+1);
1865 KnownZero |= (~InMask) & Mask;
1866 return;
1867 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001868 case ISD::FGETSIGN:
1869 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001870 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001871 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Dan Gohman23e8b712008-04-28 17:02:21 +00001873 case ISD::SUB: {
1874 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1875 // We know that the top bits of C-X are clear if X contains less bits
1876 // than C (i.e. no wrap-around can happen). For example, 20-X is
1877 // positive if we can prove that X is >= 0 and < 16.
1878 if (CLHS->getAPIntValue().isNonNegative()) {
1879 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1880 // NLZ can't be BitWidth with no sign bit
1881 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1882 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1883 Depth+1);
1884
1885 // If all of the MaskV bits are known to be zero, then we know the
1886 // output top bits are zero, because we now know that the output is
1887 // from [0-C].
1888 if ((KnownZero2 & MaskV) == MaskV) {
1889 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1890 // Top bits known zero.
1891 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1892 }
1893 }
1894 }
1895 }
1896 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001898 // Output known-0 bits are known if clear or set in both the low clear bits
1899 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1900 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001901 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1902 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001903 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001904 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1905
1906 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001907 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
Dan Gohman23e8b712008-04-28 17:02:21 +00001908 KnownZeroOut = std::min(KnownZeroOut,
1909 KnownZero2.countTrailingOnes());
1910
1911 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001912 return;
1913 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001914 case ISD::SREM:
1915 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001916 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001917 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001918 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001919 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1920 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001921
Dan Gohmana60832b2008-08-13 23:12:35 +00001922 // If the sign bit of the first operand is zero, the sign bit of
1923 // the result is zero. If the first operand has no one bits below
1924 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001925 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1926 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001927
Dan Gohman23e8b712008-04-28 17:02:21 +00001928 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001929
1930 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001931 }
1932 }
1933 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001934 case ISD::UREM: {
1935 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001936 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001937 if (RA.isPowerOf2()) {
1938 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001939 APInt Mask2 = LowBits & Mask;
1940 KnownZero |= ~LowBits & Mask;
1941 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1942 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1943 break;
1944 }
1945 }
1946
1947 // Since the result is less than or equal to either operand, any leading
1948 // zero bits in either operand must also exist in the result.
1949 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1950 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1951 Depth+1);
1952 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1953 Depth+1);
1954
1955 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1956 KnownZero2.countLeadingOnes());
1957 KnownOne.clear();
1958 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1959 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001960 }
1961 default:
1962 // Allow the target to implement this method for its nodes.
1963 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1964 case ISD::INTRINSIC_WO_CHAIN:
1965 case ISD::INTRINSIC_W_CHAIN:
1966 case ISD::INTRINSIC_VOID:
Dan Gohman206ad102009-08-04 00:24:42 +00001967 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
1968 Depth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001969 }
1970 return;
1971 }
1972}
1973
1974/// ComputeNumSignBits - Return the number of times the sign bit of the
1975/// register is replicated into the other bits. We know that at least 1 bit
1976/// is always equal to the sign bit (itself), but other cases can give us
1977/// information. For example, immediately after an "SRA X, 2", we know that
1978/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001979unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001980 MVT VT = Op.getValueType();
1981 assert(VT.isInteger() && "Invalid VT!");
1982 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001983 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001984 unsigned FirstAnswer = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Dan Gohmanea859be2007-06-22 14:59:07 +00001986 if (Depth == 6)
1987 return 1; // Limit search depth.
1988
1989 switch (Op.getOpcode()) {
1990 default: break;
1991 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001992 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001993 return VTBits-Tmp+1;
1994 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001995 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001996 return VTBits-Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00001997
Dan Gohmanea859be2007-06-22 14:59:07 +00001998 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001999 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
2000 // If negative, return # leading ones.
2001 if (Val.isNegative())
2002 return Val.countLeadingOnes();
Scott Michelfdc40a02009-02-17 22:15:04 +00002003
Dan Gohmanbb271ff2008-03-03 23:35:36 +00002004 // Return # leading zeros.
2005 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00002006 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002007
Dan Gohmanea859be2007-06-22 14:59:07 +00002008 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002009 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002010 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
Scott Michelfdc40a02009-02-17 22:15:04 +00002011
Dan Gohmanea859be2007-06-22 14:59:07 +00002012 case ISD::SIGN_EXTEND_INREG:
2013 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002014 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002015 Tmp = VTBits-Tmp+1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002016
Dan Gohmanea859be2007-06-22 14:59:07 +00002017 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2018 return std::max(Tmp, Tmp2);
2019
2020 case ISD::SRA:
2021 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2022 // SRA X, C -> adds C sign bits.
2023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002024 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002025 if (Tmp > VTBits) Tmp = VTBits;
2026 }
2027 return Tmp;
2028 case ISD::SHL:
2029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
2030 // shl destroys sign bits.
2031 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002032 if (C->getZExtValue() >= VTBits || // Bad shift.
2033 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
2034 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00002035 }
2036 break;
2037 case ISD::AND:
2038 case ISD::OR:
2039 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00002040 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00002041 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00002042 if (Tmp != 1) {
2043 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2044 FirstAnswer = std::min(Tmp, Tmp2);
2045 // We computed what we know about the sign bits as our first
2046 // answer. Now proceed to the generic code that uses
2047 // ComputeMaskedBits, and pick whichever answer is better.
2048 }
2049 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00002050
2051 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00002052 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002053 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00002054 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00002055 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00002056
2057 case ISD::SADDO:
2058 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00002059 case ISD::SSUBO:
2060 case ISD::USUBO:
2061 case ISD::SMULO:
2062 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00002063 if (Op.getResNo() != 1)
2064 break;
Duncan Sands03228082008-11-23 15:47:28 +00002065 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00002066 case ISD::SETCC:
2067 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00002068 if (TLI.getBooleanContents() ==
2069 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00002070 return VTBits;
2071 break;
2072 case ISD::ROTL:
2073 case ISD::ROTR:
2074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002075 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002076
Dan Gohmanea859be2007-06-22 14:59:07 +00002077 // Handle rotate right by N like a rotate left by 32-N.
2078 if (Op.getOpcode() == ISD::ROTR)
2079 RotAmt = (VTBits-RotAmt) & (VTBits-1);
2080
2081 // If we aren't rotating out all of the known-in sign bits, return the
2082 // number that are left. This handles rotl(sext(x), 1) for example.
2083 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2084 if (Tmp > RotAmt+1) return Tmp-RotAmt;
2085 }
2086 break;
2087 case ISD::ADD:
2088 // Add can have at most one carry bit. Thus we know that the output
2089 // is, at worst, one more bit than the inputs.
2090 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2091 if (Tmp == 1) return 1; // Early out.
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Dan Gohmanea859be2007-06-22 14:59:07 +00002093 // Special case decrementing a value (ADD X, -1):
Dan Gohman0001e562009-02-24 02:00:40 +00002094 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Dan Gohmanea859be2007-06-22 14:59:07 +00002095 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002096 APInt KnownZero, KnownOne;
2097 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002098 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00002099
Dan Gohmanea859be2007-06-22 14:59:07 +00002100 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2101 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002102 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002103 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002104
Dan Gohmanea859be2007-06-22 14:59:07 +00002105 // If we are subtracting one from a positive number, there is no carry
2106 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002107 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002108 return Tmp;
2109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002110
Dan Gohmanea859be2007-06-22 14:59:07 +00002111 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2112 if (Tmp2 == 1) return 1;
2113 return std::min(Tmp, Tmp2)-1;
2114 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002115
Dan Gohmanea859be2007-06-22 14:59:07 +00002116 case ISD::SUB:
2117 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
2118 if (Tmp2 == 1) return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00002119
Dan Gohmanea859be2007-06-22 14:59:07 +00002120 // Handle NEG.
2121 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002122 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002123 APInt KnownZero, KnownOne;
2124 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002125 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
2126 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2127 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002128 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00002129 return VTBits;
Scott Michelfdc40a02009-02-17 22:15:04 +00002130
Dan Gohmanea859be2007-06-22 14:59:07 +00002131 // If the input is known to be positive (the sign bit is known clear),
2132 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002133 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00002134 return Tmp2;
Scott Michelfdc40a02009-02-17 22:15:04 +00002135
Dan Gohmanea859be2007-06-22 14:59:07 +00002136 // Otherwise, we treat this like a SUB.
2137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002138
Dan Gohmanea859be2007-06-22 14:59:07 +00002139 // Sub can have at most one carry bit. Thus we know that the output
2140 // is, at worst, one more bit than the inputs.
2141 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
2142 if (Tmp == 1) return 1; // Early out.
2143 return std::min(Tmp, Tmp2)-1;
2144 break;
2145 case ISD::TRUNCATE:
2146 // FIXME: it's tricky to do anything useful for this, but it is an important
2147 // case for targets like X86.
2148 break;
2149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002150
Dan Gohmanea859be2007-06-22 14:59:07 +00002151 // Handle LOADX separately here. EXTLOAD case will fallthrough.
2152 if (Op.getOpcode() == ISD::LOAD) {
2153 LoadSDNode *LD = cast<LoadSDNode>(Op);
2154 unsigned ExtType = LD->getExtensionType();
2155 switch (ExtType) {
2156 default: break;
2157 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002158 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002159 return VTBits-Tmp+1;
2160 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00002161 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002162 return VTBits-Tmp;
2163 }
2164 }
2165
2166 // Allow the target to implement this method for its nodes.
2167 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
Scott Michelfdc40a02009-02-17 22:15:04 +00002168 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
Dan Gohmanea859be2007-06-22 14:59:07 +00002169 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
2170 Op.getOpcode() == ISD::INTRINSIC_VOID) {
2171 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002172 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002173 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002174
Dan Gohmanea859be2007-06-22 14:59:07 +00002175 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2176 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002177 APInt KnownZero, KnownOne;
2178 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002179 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
Scott Michelfdc40a02009-02-17 22:15:04 +00002180
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002181 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002182 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002183 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002184 Mask = KnownOne;
2185 } else {
2186 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002187 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002188 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002189
Dan Gohmanea859be2007-06-22 14:59:07 +00002190 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2191 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002192 Mask = ~Mask;
2193 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002194 // Return # leading zeros. We use 'min' here in case Val was zero before
2195 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002196 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002197}
2198
Chris Lattner51dabfb2006-10-14 00:41:01 +00002199
Dan Gohman475871a2008-07-27 21:46:04 +00002200bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002201 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2202 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002203 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002204 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2205 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002206 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002207 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002208}
2209
2210
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002211/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2212/// element of the result of the vector shuffle.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002213SDValue SelectionDAG::getShuffleScalarElt(const ShuffleVectorSDNode *N,
2214 unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002215 MVT VT = N->getValueType(0);
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002216 DebugLoc dl = N->getDebugLoc();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002217 if (N->getMaskElt(i) < 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002218 return getUNDEF(VT.getVectorElementType());
Nate Begeman5a5ca152009-04-29 05:20:52 +00002219 unsigned Index = N->getMaskElt(i);
2220 unsigned NumElems = VT.getVectorNumElements();
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002222 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002223
2224 if (V.getOpcode() == ISD::BIT_CONVERT) {
2225 V = V.getOperand(0);
Mon P Wange9f10152008-12-09 05:46:39 +00002226 MVT VVT = V.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00002227 if (!VVT.isVector() || VVT.getVectorNumElements() != (unsigned)NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002228 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002229 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002230 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002231 return (Index == 0) ? V.getOperand(0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002232 : getUNDEF(VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002233 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002234 return V.getOperand(Index);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002235 if (const ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(V))
2236 return getShuffleScalarElt(SVN, Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002237 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002238}
2239
2240
Chris Lattnerc3aae252005-01-07 07:46:32 +00002241/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002242///
Bill Wendling7ade28c2009-01-28 22:17:52 +00002243SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002244 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002245 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002246 void *IP = 0;
2247 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002248 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002249 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohmanfc166572009-04-09 23:54:40 +00002250 new (N) SDNode(Opcode, DL, getVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002251 CSEMap.InsertNode(N, IP);
Scott Michelfdc40a02009-02-17 22:15:04 +00002252
Chris Lattner4a283e92006-08-11 18:38:11 +00002253 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002254#ifndef NDEBUG
2255 VerifyNode(N);
2256#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002257 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002258}
2259
Bill Wendling7ade28c2009-01-28 22:17:52 +00002260SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
2261 MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002262 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002263 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002264 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002265 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002266 switch (Opcode) {
2267 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002268 case ISD::SIGN_EXTEND:
2269 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002270 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002271 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002272 case ISD::TRUNCATE:
2273 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002274 case ISD::UINT_TO_FP:
2275 case ISD::SINT_TO_FP: {
2276 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002277 // No compile time operations on this type.
2278 if (VT==MVT::ppcf128)
2279 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002280 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
Scott Michelfdc40a02009-02-17 22:15:04 +00002281 (void)apf.convertFromAPInt(Val,
Dan Gohman6c231502008-02-29 01:47:35 +00002282 Opcode==ISD::SINT_TO_FP,
2283 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002284 return getConstantFP(apf, VT);
2285 }
Chris Lattner94683772005-12-23 05:30:37 +00002286 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002287 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002288 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002289 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002290 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002291 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002292 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002293 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002294 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002295 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002296 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002297 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002298 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002299 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002300 }
2301 }
2302
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002303 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002304 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002305 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002306 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002307 switch (Opcode) {
2308 case ISD::FNEG:
2309 V.changeSign();
2310 return getConstantFP(V, VT);
2311 case ISD::FABS:
2312 V.clearSign();
2313 return getConstantFP(V, VT);
2314 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002315 case ISD::FP_EXTEND: {
2316 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002317 // This can return overflow, underflow, or inexact; we don't care.
2318 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002319 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002320 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002321 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002322 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002323 case ISD::FP_TO_SINT:
2324 case ISD::FP_TO_UINT: {
Dale Johannesen2f91f302009-04-24 21:34:13 +00002325 integerPart x[2];
Dale Johannesen23a98552008-10-09 23:00:39 +00002326 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002327 assert(integerPartWidth >= 64);
2328 // FIXME need to be more flexible about rounding mode.
Dale Johannesen2f91f302009-04-24 21:34:13 +00002329 APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
Dale Johannesendb44bf82007-10-16 23:38:29 +00002330 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002331 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002332 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2333 break;
Dale Johannesen2f91f302009-04-24 21:34:13 +00002334 APInt api(VT.getSizeInBits(), 2, x);
2335 return getConstant(api, VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002336 }
2337 case ISD::BIT_CONVERT:
2338 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002339 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002340 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002341 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002342 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002343 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002345 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002346
Gabor Greifba36cb52008-08-28 21:40:38 +00002347 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002348 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002349 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002350 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002351 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002352 return Operand; // Factor, merge or concat of one node? No need.
Torok Edwinc23197a2009-07-14 16:55:14 +00002353 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002354 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002355 assert(VT.isFloatingPoint() &&
2356 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002357 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002358 if (Operand.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002359 return getUNDEF(VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002360 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002361 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002362 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002363 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002364 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002365 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002366 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002367 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002368 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002369 break;
2370 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002372 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002373 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002374 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002375 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002376 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Scott Michelfdc40a02009-02-17 22:15:04 +00002377 return getNode(ISD::ZERO_EXTEND, DL, VT,
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002378 Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002379 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002380 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002381 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002382 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002383 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002384 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002385 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002386 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2387 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002388 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002389 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002390 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002391 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002392 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002393 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002394 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002395 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002396 if (OpOpcode == ISD::TRUNCATE)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002397 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002398 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2399 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002400 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002401 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002402 return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002403 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002404 return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002405 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002406 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002407 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002408 break;
Chris Lattner35481892005-12-23 00:16:34 +00002409 case ISD::BIT_CONVERT:
2410 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002411 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002412 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002413 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002414 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002415 return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002416 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002417 return getUNDEF(VT);
Chris Lattner35481892005-12-23 00:16:34 +00002418 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002419 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002420 assert(VT.isVector() && !Operand.getValueType().isVector() &&
Duncan Sandsb10b5ac2009-04-18 20:16:54 +00002421 (VT.getVectorElementType() == Operand.getValueType() ||
2422 (VT.getVectorElementType().isInteger() &&
2423 Operand.getValueType().isInteger() &&
2424 VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
Chris Lattnerce872152006-03-19 06:31:19 +00002425 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002426 if (OpOpcode == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002427 return getUNDEF(VT);
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002428 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2429 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2430 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2431 Operand.getConstantOperandVal(1) == 0 &&
2432 Operand.getOperand(0).getValueType() == VT)
2433 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002434 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002435 case ISD::FNEG:
Mon P Wanga7b6cff2009-01-31 06:07:45 +00002436 // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
2437 if (UnsafeFPMath && OpOpcode == ISD::FSUB)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002438 return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
Gabor Greifba36cb52008-08-28 21:40:38 +00002439 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002440 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002441 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002442 break;
2443 case ISD::FABS:
2444 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002445 return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002446 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002447 }
2448
Chris Lattner43247a12005-08-25 19:12:10 +00002449 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002450 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002451 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002452 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002453 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002454 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002455 void *IP = 0;
2456 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002457 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002458 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002459 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002460 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002461 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002462 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002463 new (N) UnarySDNode(Opcode, DL, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002464 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002465
Chris Lattnerc3aae252005-01-07 07:46:32 +00002466 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002467#ifndef NDEBUG
2468 VerifyNode(N);
2469#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002470 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002471}
2472
Bill Wendling688d1c42008-09-24 10:16:24 +00002473SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2474 MVT VT,
2475 ConstantSDNode *Cst1,
2476 ConstantSDNode *Cst2) {
2477 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2478
2479 switch (Opcode) {
2480 case ISD::ADD: return getConstant(C1 + C2, VT);
2481 case ISD::SUB: return getConstant(C1 - C2, VT);
2482 case ISD::MUL: return getConstant(C1 * C2, VT);
2483 case ISD::UDIV:
2484 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2485 break;
2486 case ISD::UREM:
2487 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2488 break;
2489 case ISD::SDIV:
2490 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2491 break;
2492 case ISD::SREM:
2493 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2494 break;
2495 case ISD::AND: return getConstant(C1 & C2, VT);
2496 case ISD::OR: return getConstant(C1 | C2, VT);
2497 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2498 case ISD::SHL: return getConstant(C1 << C2, VT);
2499 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2500 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2501 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2502 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2503 default: break;
2504 }
2505
2506 return SDValue();
2507}
2508
Bill Wendling7ade28c2009-01-28 22:17:52 +00002509SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2510 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002511 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2512 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002513 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002514 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002515 case ISD::TokenFactor:
2516 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2517 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002518 // Fold trivial token factors.
2519 if (N1.getOpcode() == ISD::EntryToken) return N2;
2520 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002521 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002522 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002523 case ISD::CONCAT_VECTORS:
2524 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2525 // one big BUILD_VECTOR.
2526 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2527 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002528 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2529 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002530 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002531 }
2532 break;
Chris Lattner76365122005-01-16 02:23:22 +00002533 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002534 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002535 N1.getValueType() == VT && "Binary operator types must match!");
2536 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2537 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002538 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002539 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002540 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2541 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002542 break;
Chris Lattner76365122005-01-16 02:23:22 +00002543 case ISD::OR:
2544 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002545 case ISD::ADD:
2546 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002547 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002548 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002549 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2550 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002551 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002552 return N1;
2553 break;
Chris Lattner76365122005-01-16 02:23:22 +00002554 case ISD::UDIV:
2555 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002556 case ISD::MULHU:
2557 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00002558 case ISD::MUL:
2559 case ISD::SDIV:
2560 case ISD::SREM:
Dan Gohman760f86f2009-01-22 21:58:43 +00002561 assert(VT.isInteger() && "This operator does not apply to FP types!");
2562 // fall through
Chris Lattner01b3d732005-09-28 22:28:18 +00002563 case ISD::FADD:
2564 case ISD::FSUB:
2565 case ISD::FMUL:
2566 case ISD::FDIV:
2567 case ISD::FREM:
Dan Gohmana90c8e62009-01-23 19:10:37 +00002568 if (UnsafeFPMath) {
2569 if (Opcode == ISD::FADD) {
2570 // 0+x --> x
2571 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
2572 if (CFP->getValueAPF().isZero())
2573 return N2;
2574 // x+0 --> x
2575 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2576 if (CFP->getValueAPF().isZero())
2577 return N1;
2578 } else if (Opcode == ISD::FSUB) {
2579 // x-0 --> x
2580 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
2581 if (CFP->getValueAPF().isZero())
2582 return N1;
2583 }
Dan Gohman760f86f2009-01-22 21:58:43 +00002584 }
Chris Lattner76365122005-01-16 02:23:22 +00002585 assert(N1.getValueType() == N2.getValueType() &&
2586 N1.getValueType() == VT && "Binary operator types must match!");
2587 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002588 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2589 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002590 N1.getValueType().isFloatingPoint() &&
2591 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002592 "Invalid FCOPYSIGN!");
2593 break;
Chris Lattner76365122005-01-16 02:23:22 +00002594 case ISD::SHL:
2595 case ISD::SRA:
2596 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002597 case ISD::ROTL:
2598 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002599 assert(VT == N1.getValueType() &&
2600 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002601 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002602 "Shifts only work on integers");
2603
2604 // Always fold shifts of i1 values so the code generator doesn't need to
2605 // handle them. Since we know the size of the shift has to be less than the
2606 // size of the value, the shift/rotate count is guaranteed to be zero.
2607 if (VT == MVT::i1)
2608 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002609 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002610 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002611 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002612 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002613 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002614 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002615 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002616 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002617 break;
2618 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002619 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002620 assert(VT.isFloatingPoint() &&
2621 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002622 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002623 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002624 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002625 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002626 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002627 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002628 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002629 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002630 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002631 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002632 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002633 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002634 break;
2635 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002636 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002637 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002638 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002639 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002640 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002641 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002642 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002643
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002644 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002645 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002646 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002647 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002648 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002649 return getConstant(Val, VT);
2650 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002651 break;
2652 }
2653 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002654 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2655 if (N1.getOpcode() == ISD::UNDEF)
Dale Johannesene8d72302009-02-06 23:05:02 +00002656 return getUNDEF(VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002657
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002658 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2659 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002660 if (N2C &&
2661 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002662 N1.getNumOperands() > 0) {
2663 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002664 N1.getOperand(0).getValueType().getVectorNumElements();
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002665 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002666 N1.getOperand(N2C->getZExtValue() / Factor),
2667 getConstant(N2C->getZExtValue() % Factor,
2668 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002669 }
2670
2671 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2672 // expanding large vector constants.
Bob Wilsonb1303d02009-04-13 22:05:19 +00002673 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
2674 SDValue Elt = N1.getOperand(N2C->getZExtValue());
Eli Friedmanc680ac92009-07-09 22:01:03 +00002675 MVT VEltTy = N1.getValueType().getVectorElementType();
2676 if (Elt.getValueType() != VEltTy) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00002677 // If the vector element type is not legal, the BUILD_VECTOR operands
2678 // are promoted and implicitly truncated. Make that explicit here.
Eli Friedmanc680ac92009-07-09 22:01:03 +00002679 Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
2680 }
2681 if (VT != VEltTy) {
2682 // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
2683 // result is implicitly extended.
2684 Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
Bob Wilsonb1303d02009-04-13 22:05:19 +00002685 }
2686 return Elt;
2687 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002688
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002689 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2690 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002691 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
Dan Gohman197e88f2009-01-29 16:10:46 +00002692 // If the indices are the same, return the inserted element.
Dan Gohman6ab64222008-08-13 21:51:37 +00002693 if (N1.getOperand(2) == N2)
2694 return N1.getOperand(1);
Dan Gohman197e88f2009-01-29 16:10:46 +00002695 // If the indices are known different, extract the element from
2696 // the original vector.
2697 else if (isa<ConstantSDNode>(N1.getOperand(2)) &&
2698 isa<ConstantSDNode>(N2))
Dale Johannesendbfd8db2009-02-03 01:55:44 +00002699 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
Dan Gohman6ab64222008-08-13 21:51:37 +00002700 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002701 break;
2702 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002703 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002704 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2705 (N1.getValueType().isInteger() == VT.isInteger()) &&
2706 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002707
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002708 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2709 // 64-bit integers into 32-bit parts. Instead of building the extract of
Scott Michelfdc40a02009-02-17 22:15:04 +00002710 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002711 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002712 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002713
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002714 // EXTRACT_ELEMENT of a constant int is also very common.
2715 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002716 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002717 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002718 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2719 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002720 }
2721 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002722 case ISD::EXTRACT_SUBVECTOR:
2723 if (N1.getValueType() == VT) // Trivial extraction.
2724 return N1;
2725 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002726 }
2727
2728 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002729 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002730 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2731 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002732 } else { // Cannonicalize constant to RHS if commutative
2733 if (isCommutativeBinOp(Opcode)) {
2734 std::swap(N1C, N2C);
2735 std::swap(N1, N2);
2736 }
2737 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002738 }
2739
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002740 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002741 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2742 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002743 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002744 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2745 // Cannonicalize constant to RHS if commutative
2746 std::swap(N1CFP, N2CFP);
2747 std::swap(N1, N2);
2748 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002749 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2750 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002751 switch (Opcode) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002752 case ISD::FADD:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002753 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002754 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002755 return getConstantFP(V1, VT);
2756 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002757 case ISD::FSUB:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002758 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2759 if (s!=APFloat::opInvalidOp)
2760 return getConstantFP(V1, VT);
2761 break;
2762 case ISD::FMUL:
2763 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2764 if (s!=APFloat::opInvalidOp)
2765 return getConstantFP(V1, VT);
2766 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002767 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002768 s = V1.divide(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;
Chris Lattner01b3d732005-09-28 22:28:18 +00002772 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002773 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2774 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2775 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002776 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002777 case ISD::FCOPYSIGN:
2778 V1.copySign(V2);
2779 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002780 default: break;
2781 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002782 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002783 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002784
Chris Lattner62b57722006-04-20 05:39:12 +00002785 // Canonicalize an UNDEF to the RHS, even over a constant.
2786 if (N1.getOpcode() == ISD::UNDEF) {
2787 if (isCommutativeBinOp(Opcode)) {
2788 std::swap(N1, N2);
2789 } else {
2790 switch (Opcode) {
2791 case ISD::FP_ROUND_INREG:
2792 case ISD::SIGN_EXTEND_INREG:
2793 case ISD::SUB:
2794 case ISD::FSUB:
2795 case ISD::FDIV:
2796 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002797 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002798 return N1; // fold op(undef, arg2) -> undef
2799 case ISD::UDIV:
2800 case ISD::SDIV:
2801 case ISD::UREM:
2802 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002803 case ISD::SRL:
2804 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002805 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002806 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2807 // For vectors, we can't easily build an all zero vector, just return
2808 // the LHS.
2809 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002810 }
2811 }
2812 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002813
2814 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002815 if (N2.getOpcode() == ISD::UNDEF) {
2816 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002817 case ISD::XOR:
2818 if (N1.getOpcode() == ISD::UNDEF)
2819 // Handle undef ^ undef -> 0 special case. This is a common
2820 // idiom (misuse).
2821 return getConstant(0, VT);
2822 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002823 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002824 case ISD::ADDC:
2825 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002826 case ISD::SUB:
Chris Lattner62b57722006-04-20 05:39:12 +00002827 case ISD::UDIV:
2828 case ISD::SDIV:
2829 case ISD::UREM:
2830 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002831 return N2; // fold op(arg1, undef) -> undef
Dan Gohman77b81fe2009-06-04 17:12:12 +00002832 case ISD::FADD:
2833 case ISD::FSUB:
2834 case ISD::FMUL:
2835 case ISD::FDIV:
2836 case ISD::FREM:
2837 if (UnsafeFPMath)
2838 return N2;
2839 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002840 case ISD::MUL:
Chris Lattner62b57722006-04-20 05:39:12 +00002841 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002842 case ISD::SRL:
2843 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002844 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002845 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2846 // For vectors, we can't easily build an all zero vector, just return
2847 // the LHS.
2848 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002849 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 if (!VT.isVector())
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00002851 return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002852 // For vectors, we can't easily build an all one vector, just return
2853 // the LHS.
2854 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002855 case ISD::SRA:
2856 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002857 }
2858 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002859
Chris Lattner27e9b412005-05-11 18:57:39 +00002860 // Memoize this node if possible.
2861 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002862 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002863 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002864 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002865 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002866 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002867 void *IP = 0;
2868 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002869 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002870 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002871 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002872 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002873 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002874 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002875 new (N) BinarySDNode(Opcode, DL, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002876 }
2877
Chris Lattnerc3aae252005-01-07 07:46:32 +00002878 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002879#ifndef NDEBUG
2880 VerifyNode(N);
2881#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002882 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002883}
2884
Bill Wendling7ade28c2009-01-28 22:17:52 +00002885SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2886 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002887 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002888 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2889 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002890 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002891 case ISD::CONCAT_VECTORS:
2892 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2893 // one big BUILD_VECTOR.
2894 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2895 N2.getOpcode() == ISD::BUILD_VECTOR &&
2896 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002897 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2898 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2899 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Evan Chenga87008d2009-02-25 22:49:59 +00002900 return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002901 }
2902 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002903 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002904 // Use FoldSetCC to simplify SETCC's.
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002905 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002906 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002907 break;
2908 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002909 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002910 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002911 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002912 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002913 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002914 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002915 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002916
2917 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002918 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002919 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002920 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002921 if (N2C->getZExtValue()) // Unconditional branch
Dale Johannesenff97d4f2009-02-03 00:47:48 +00002922 return getNode(ISD::BR, DL, MVT::Other, N1, N3);
Chris Lattner5351e9b2005-01-07 22:49:57 +00002923 else
2924 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002925 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002926 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002927 case ISD::VECTOR_SHUFFLE:
Torok Edwinc23197a2009-07-14 16:55:14 +00002928 llvm_unreachable("should use getVectorShuffle constructor!");
Chris Lattnerfb194b92006-03-19 23:56:04 +00002929 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002930 case ISD::BIT_CONVERT:
2931 // Fold bit_convert nodes from a type to themselves.
2932 if (N1.getValueType() == VT)
2933 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002934 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002935 }
2936
Chris Lattner43247a12005-08-25 19:12:10 +00002937 // Memoize node if it doesn't produce a flag.
2938 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002939 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002940 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002941 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002942 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002943 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002944 void *IP = 0;
2945 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002946 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002947 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002948 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002949 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002950 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002951 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00002952 new (N) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002953 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002954 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002955#ifndef NDEBUG
2956 VerifyNode(N);
2957#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002958 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002959}
2960
Bill Wendling7ade28c2009-01-28 22:17:52 +00002961SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2962 SDValue N1, SDValue N2, SDValue N3,
2963 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00002964 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002965 return getNode(Opcode, DL, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002966}
2967
Bill Wendling7ade28c2009-01-28 22:17:52 +00002968SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
2969 SDValue N1, SDValue N2, SDValue N3,
2970 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00002971 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00002972 return getNode(Opcode, DL, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002973}
2974
Dan Gohman98ca4f22009-08-05 01:29:28 +00002975/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
2976/// the incoming stack arguments to be loaded from the stack.
2977SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
2978 SmallVector<SDValue, 8> ArgChains;
2979
2980 // Include the original chain at the beginning of the list. When this is
2981 // used by target LowerCall hooks, this helps legalize find the
2982 // CALLSEQ_BEGIN node.
2983 ArgChains.push_back(Chain);
2984
2985 // Add a chain value for each stack argument.
2986 for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
2987 UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
2988 if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
2989 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
2990 if (FI->getIndex() < 0)
2991 ArgChains.push_back(SDValue(L, 1));
2992
2993 // Build a tokenfactor for all the chains.
2994 return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
2995 &ArgChains[0], ArgChains.size());
2996}
2997
Dan Gohman707e0182008-04-12 04:36:06 +00002998/// getMemsetValue - Vectorized representation of the memset value
2999/// operand.
Dale Johannesen0f502f62009-02-03 22:26:09 +00003000static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG,
3001 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003002 unsigned NumBits = VT.isVector() ?
3003 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00003004 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003005 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00003006 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003007 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00003008 Val = (Val << Shift) | Val;
3009 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00003010 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003011 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00003012 return DAG.getConstant(Val, VT);
3013 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00003014 }
Evan Chengf0df0312008-05-15 08:39:06 +00003015
Duncan Sands3d0f5af2008-10-30 20:26:50 +00003016 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dale Johannesen0f502f62009-02-03 22:26:09 +00003017 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
Evan Chengf0df0312008-05-15 08:39:06 +00003018 unsigned Shift = 8;
3019 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dale Johannesen0f502f62009-02-03 22:26:09 +00003020 Value = DAG.getNode(ISD::OR, dl, VT,
3021 DAG.getNode(ISD::SHL, dl, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00003022 DAG.getConstant(Shift,
3023 TLI.getShiftAmountTy())),
3024 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00003025 Shift <<= 1;
3026 }
3027
3028 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003029}
3030
Dan Gohman707e0182008-04-12 04:36:06 +00003031/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
3032/// used when a memcpy is turned into a memset when the source is a constant
3033/// string ptr.
Dale Johannesen0f502f62009-02-03 22:26:09 +00003034static SDValue getMemsetStringVal(MVT VT, DebugLoc dl, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00003035 const TargetLowering &TLI,
3036 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003037 // Handle vector with all elements zero.
3038 if (Str.empty()) {
3039 if (VT.isInteger())
3040 return DAG.getConstant(0, VT);
3041 unsigned NumElts = VT.getVectorNumElements();
3042 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003043 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003044 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
3045 }
3046
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 assert(!VT.isVector() && "Can't handle vector type here!");
3048 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00003049 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003050 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003051 if (TLI.isLittleEndian())
3052 Offset = Offset + MSB - 1;
3053 for (unsigned i = 0; i != MSB; ++i) {
3054 Val = (Val << 8) | (unsigned char)Str[Offset];
3055 Offset += TLI.isLittleEndian() ? -1 : 1;
3056 }
3057 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003058}
3059
Scott Michelfdc40a02009-02-17 22:15:04 +00003060/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00003061///
Dan Gohman475871a2008-07-27 21:46:04 +00003062static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00003063 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003064 MVT VT = Base.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003065 return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
Dale Johannesendbfd8db2009-02-03 01:55:44 +00003066 VT, Base, DAG.getConstant(Offset, VT));
Dan Gohman707e0182008-04-12 04:36:06 +00003067}
3068
Evan Chengf0df0312008-05-15 08:39:06 +00003069/// isMemSrcFromString - Returns true if memcpy source is a string constant.
3070///
Dan Gohman475871a2008-07-27 21:46:04 +00003071static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00003072 unsigned SrcDelta = 0;
3073 GlobalAddressSDNode *G = NULL;
3074 if (Src.getOpcode() == ISD::GlobalAddress)
3075 G = cast<GlobalAddressSDNode>(Src);
3076 else if (Src.getOpcode() == ISD::ADD &&
3077 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
3078 Src.getOperand(1).getOpcode() == ISD::Constant) {
3079 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003080 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00003081 }
3082 if (!G)
3083 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00003084
Evan Chengf0df0312008-05-15 08:39:06 +00003085 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Bill Wendling0582ae92009-03-13 04:39:26 +00003086 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
3087 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00003088
Evan Chengf0df0312008-05-15 08:39:06 +00003089 return false;
3090}
Dan Gohman707e0182008-04-12 04:36:06 +00003091
Evan Chengf0df0312008-05-15 08:39:06 +00003092/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
3093/// to replace the memset / memcpy is below the threshold. It also returns the
3094/// types of the sequence of memory ops to perform memset / memcpy.
3095static
Duncan Sands83ec4b62008-06-06 12:08:01 +00003096bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00003098 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003099 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00003100 SelectionDAG &DAG,
3101 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00003102 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00003103 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00003104 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Devang Patel578efa92009-06-05 21:57:13 +00003105 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr, DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003106 if (VT != MVT::iAny) {
3107 unsigned NewAlign = (unsigned)
Owen Andersondebcb012009-07-29 22:17:13 +00003108 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00003109 // If source is a string constant, this will require an unaligned load.
3110 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
3111 if (Dst.getOpcode() != ISD::FrameIndex) {
3112 // Can't change destination alignment. It requires a unaligned store.
3113 if (AllowUnalign)
3114 VT = MVT::iAny;
3115 } else {
3116 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
3117 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3118 if (MFI->isFixedObjectIndex(FI)) {
3119 // Can't change destination alignment. It requires a unaligned store.
3120 if (AllowUnalign)
3121 VT = MVT::iAny;
3122 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00003123 // Give the stack frame object a larger alignment if needed.
3124 if (MFI->getObjectAlignment(FI) < NewAlign)
3125 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00003126 Align = NewAlign;
3127 }
3128 }
3129 }
3130 }
3131
3132 if (VT == MVT::iAny) {
3133 if (AllowUnalign) {
3134 VT = MVT::i64;
3135 } else {
3136 switch (Align & 7) {
3137 case 0: VT = MVT::i64; break;
3138 case 4: VT = MVT::i32; break;
3139 case 2: VT = MVT::i16; break;
3140 default: VT = MVT::i8; break;
3141 }
3142 }
3143
Duncan Sands83ec4b62008-06-06 12:08:01 +00003144 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00003145 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003146 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
3147 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00003148
Duncan Sands8e4eb092008-06-08 20:54:56 +00003149 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00003150 VT = LVT;
3151 }
Dan Gohman707e0182008-04-12 04:36:06 +00003152
3153 unsigned NumMemOps = 0;
3154 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003155 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00003156 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00003157 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003158 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00003159 VT = MVT::i64;
3160 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003161 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
3162 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00003163 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003164 // This can result in a type that is not legal on the target, e.g.
3165 // 1 or 2 bytes on PPC.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003166 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00003167 VTSize >>= 1;
3168 }
Dan Gohman707e0182008-04-12 04:36:06 +00003169 }
Dan Gohman707e0182008-04-12 04:36:06 +00003170
3171 if (++NumMemOps > Limit)
3172 return false;
3173 MemOps.push_back(VT);
3174 Size -= VTSize;
3175 }
3176
3177 return true;
3178}
3179
Dale Johannesen0f502f62009-02-03 22:26:09 +00003180static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003181 SDValue Chain, SDValue Dst,
3182 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00003183 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003184 const Value *DstSV, uint64_t DstSVOff,
3185 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00003186 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3187
Dan Gohman21323f32008-05-29 19:42:22 +00003188 // Expand memcpy to a series of load and store ops if the size operand falls
3189 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003190 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003191 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00003192 if (!AlwaysInline)
3193 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00003194 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003195 std::string Str;
3196 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003197 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003198 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003199 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003200
Dan Gohman707e0182008-04-12 04:36:06 +00003201
Evan Cheng0ff39b32008-06-30 07:31:25 +00003202 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00003203 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00003204 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00003205 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003206 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003207 MVT VT = MemOps[i];
3208 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00003210
Evan Cheng0ff39b32008-06-30 07:31:25 +00003211 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00003212 // It's unlikely a store of a vector immediate can be done in a single
3213 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003214 // We also handle store a vector with all zero's.
3215 // FIXME: Handle other cases where store of vector immediate is done in
3216 // a single instruction.
Dale Johannesen0f502f62009-02-03 22:26:09 +00003217 Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
3218 Store = DAG.getStore(Chain, dl, Value,
Evan Chengf0df0312008-05-15 08:39:06 +00003219 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00003220 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003221 } else {
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003222 // The type might not be legal for the target. This should only happen
3223 // if the type is smaller than a legal type, as on PPC, so the right
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003224 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
3225 // to Load/Store if NVT==VT.
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003226 // FIXME does the case above also need this?
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003227 MVT NVT = TLI.getTypeToTransformTo(VT);
3228 assert(NVT.bitsGE(VT));
3229 Value = DAG.getExtLoad(ISD::EXTLOAD, dl, NVT, Chain,
3230 getMemBasePlusOffset(Src, SrcOff, DAG),
3231 SrcSV, SrcSVOff + SrcOff, VT, false, Align);
3232 Store = DAG.getTruncStore(Chain, dl, Value,
Dale Johannesen08bc98e2009-06-22 20:59:07 +00003233 getMemBasePlusOffset(Dst, DstOff, DAG),
Dale Johannesen8539cfd2009-06-24 17:11:31 +00003234 DstSV, DstSVOff + DstOff, VT, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00003235 }
3236 OutChains.push_back(Store);
3237 SrcOff += VTSize;
3238 DstOff += VTSize;
3239 }
3240
Dale Johannesen0f502f62009-02-03 22:26:09 +00003241 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003242 &OutChains[0], OutChains.size());
3243}
3244
Dale Johannesen0f502f62009-02-03 22:26:09 +00003245static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003246 SDValue Chain, SDValue Dst,
3247 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003248 unsigned Align, bool AlwaysInline,
3249 const Value *DstSV, uint64_t DstSVOff,
3250 const Value *SrcSV, uint64_t SrcSVOff){
3251 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3252
3253 // Expand memmove to a series of load and store ops if the size operand falls
3254 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003255 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003256 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003257 if (!AlwaysInline)
3258 Limit = TLI.getMaxStoresPerMemmove();
3259 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003260 std::string Str;
3261 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003262 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003263 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003264 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003265
Dan Gohman21323f32008-05-29 19:42:22 +00003266 uint64_t SrcOff = 0, DstOff = 0;
3267
Dan Gohman475871a2008-07-27 21:46:04 +00003268 SmallVector<SDValue, 8> LoadValues;
3269 SmallVector<SDValue, 8> LoadChains;
3270 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003271 unsigned NumMemOps = MemOps.size();
3272 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003273 MVT VT = MemOps[i];
3274 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003275 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003276
Dale Johannesen0f502f62009-02-03 22:26:09 +00003277 Value = DAG.getLoad(VT, dl, Chain,
Dan Gohman21323f32008-05-29 19:42:22 +00003278 getMemBasePlusOffset(Src, SrcOff, DAG),
3279 SrcSV, SrcSVOff + SrcOff, false, Align);
3280 LoadValues.push_back(Value);
3281 LoadChains.push_back(Value.getValue(1));
3282 SrcOff += VTSize;
3283 }
Dale Johannesen0f502f62009-02-03 22:26:09 +00003284 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003285 &LoadChains[0], LoadChains.size());
3286 OutChains.clear();
3287 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003288 MVT VT = MemOps[i];
3289 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003290 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003291
Dale Johannesen0f502f62009-02-03 22:26:09 +00003292 Store = DAG.getStore(Chain, dl, LoadValues[i],
Dan Gohman21323f32008-05-29 19:42:22 +00003293 getMemBasePlusOffset(Dst, DstOff, DAG),
3294 DstSV, DstSVOff + DstOff, false, DstAlign);
3295 OutChains.push_back(Store);
3296 DstOff += VTSize;
3297 }
3298
Dale Johannesen0f502f62009-02-03 22:26:09 +00003299 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman21323f32008-05-29 19:42:22 +00003300 &OutChains[0], OutChains.size());
3301}
3302
Dale Johannesen0f502f62009-02-03 22:26:09 +00003303static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
Dan Gohman475871a2008-07-27 21:46:04 +00003304 SDValue Chain, SDValue Dst,
3305 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003306 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003307 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003308 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3309
3310 // Expand memset to a series of load/store ops if the size operand
3311 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003313 std::string Str;
3314 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003315 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003316 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003317 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003318
Dan Gohman475871a2008-07-27 21:46:04 +00003319 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003320 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003321
3322 unsigned NumMemOps = MemOps.size();
3323 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003324 MVT VT = MemOps[i];
3325 unsigned VTSize = VT.getSizeInBits() / 8;
Dale Johannesen0f502f62009-02-03 22:26:09 +00003326 SDValue Value = getMemsetValue(Src, VT, DAG, dl);
3327 SDValue Store = DAG.getStore(Chain, dl, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003328 getMemBasePlusOffset(Dst, DstOff, DAG),
3329 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003330 OutChains.push_back(Store);
3331 DstOff += VTSize;
3332 }
3333
Dale Johannesen0f502f62009-02-03 22:26:09 +00003334 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman707e0182008-04-12 04:36:06 +00003335 &OutChains[0], OutChains.size());
3336}
3337
Dale Johannesen0f502f62009-02-03 22:26:09 +00003338SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003339 SDValue Src, SDValue Size,
3340 unsigned Align, bool AlwaysInline,
3341 const Value *DstSV, uint64_t DstSVOff,
3342 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003343
3344 // Check to see if we should lower the memcpy to loads and stores first.
3345 // For cases within the target-specified limits, this is the best choice.
3346 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3347 if (ConstantSize) {
3348 // Memcpy with size zero? Just return the original chain.
3349 if (ConstantSize->isNullValue())
3350 return Chain;
3351
Dan Gohman475871a2008-07-27 21:46:04 +00003352 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003353 getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003354 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003355 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003356 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003357 return Result;
3358 }
3359
3360 // Then check to see if we should lower the memcpy with target-specific
3361 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003362 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003363 TLI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman707e0182008-04-12 04:36:06 +00003364 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003365 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003366 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003367 return Result;
3368
3369 // If we really need inline code and the target declined to provide it,
3370 // use a (potentially long) sequence of loads and stores.
3371 if (AlwaysInline) {
3372 assert(ConstantSize && "AlwaysInline requires a constant size!");
Dale Johannesen0f502f62009-02-03 22:26:09 +00003373 return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003374 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003375 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003376 }
3377
3378 // Emit a library call.
3379 TargetLowering::ArgListTy Args;
3380 TargetLowering::ArgListEntry Entry;
3381 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3382 Entry.Node = Dst; Args.push_back(Entry);
3383 Entry.Node = Src; Args.push_back(Entry);
3384 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003385 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003386 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003387 TLI.LowerCallTo(Chain, Type::VoidTy,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00003388 false, false, false, false, 0, CallingConv::C, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003389 /*isReturnValueUsed=*/false,
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003390 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
3391 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003392 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003393 return CallResult.second;
3394}
3395
Dale Johannesen0f502f62009-02-03 22:26:09 +00003396SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003397 SDValue Src, SDValue Size,
3398 unsigned Align,
3399 const Value *DstSV, uint64_t DstSVOff,
3400 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003401
Dan Gohman21323f32008-05-29 19:42:22 +00003402 // Check to see if we should lower the memmove to loads and stores first.
3403 // For cases within the target-specified limits, this is the best choice.
3404 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3405 if (ConstantSize) {
3406 // Memmove with size zero? Just return the original chain.
3407 if (ConstantSize->isNullValue())
3408 return Chain;
3409
Dan Gohman475871a2008-07-27 21:46:04 +00003410 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003411 getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003412 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003413 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003414 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003415 return Result;
3416 }
Dan Gohman707e0182008-04-12 04:36:06 +00003417
3418 // Then check to see if we should lower the memmove with target-specific
3419 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003420 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003421 TLI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003422 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003423 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003424 return Result;
3425
3426 // Emit a library call.
3427 TargetLowering::ArgListTy Args;
3428 TargetLowering::ArgListEntry Entry;
3429 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3430 Entry.Node = Dst; Args.push_back(Entry);
3431 Entry.Node = Src; Args.push_back(Entry);
3432 Entry.Node = Size; Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003433 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003434 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003435 TLI.LowerCallTo(Chain, Type::VoidTy,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00003436 false, false, false, false, 0, CallingConv::C, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003437 /*isReturnValueUsed=*/false,
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003438 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
3439 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003440 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003441 return CallResult.second;
3442}
3443
Dale Johannesen0f502f62009-02-03 22:26:09 +00003444SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
Dan Gohman475871a2008-07-27 21:46:04 +00003445 SDValue Src, SDValue Size,
3446 unsigned Align,
3447 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003448
3449 // Check to see if we should lower the memset to stores first.
3450 // For cases within the target-specified limits, this is the best choice.
3451 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3452 if (ConstantSize) {
3453 // Memset with size zero? Just return the original chain.
3454 if (ConstantSize->isNullValue())
3455 return Chain;
3456
Dan Gohman475871a2008-07-27 21:46:04 +00003457 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003458 getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003459 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003460 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003461 return Result;
3462 }
3463
3464 // Then check to see if we should lower the memset with target-specific
3465 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SDValue Result =
Dale Johannesen0f502f62009-02-03 22:26:09 +00003467 TLI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003468 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003469 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003470 return Result;
3471
3472 // Emit a library call.
3473 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3474 TargetLowering::ArgListTy Args;
3475 TargetLowering::ArgListEntry Entry;
3476 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3477 Args.push_back(Entry);
3478 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003479 if (Src.getValueType().bitsGT(MVT::i32))
Dale Johannesen0f502f62009-02-03 22:26:09 +00003480 Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003481 else
Dale Johannesen0f502f62009-02-03 22:26:09 +00003482 Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
Dan Gohman707e0182008-04-12 04:36:06 +00003483 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3484 Args.push_back(Entry);
3485 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3486 Args.push_back(Entry);
Dale Johannesen7d2ad622009-01-30 23:10:59 +00003487 // FIXME: pass in DebugLoc
Dan Gohman475871a2008-07-27 21:46:04 +00003488 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003489 TLI.LowerCallTo(Chain, Type::VoidTy,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00003490 false, false, false, false, 0, CallingConv::C, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003491 /*isReturnValueUsed=*/false,
Sanjiv Guptaa114baa2009-07-30 09:12:56 +00003492 getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
3493 TLI.getPointerTy()),
Dale Johannesen0f502f62009-02-03 22:26:09 +00003494 Args, *this, dl);
Dan Gohman707e0182008-04-12 04:36:06 +00003495 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003496}
3497
Dale Johannesene8c17332009-01-29 00:47:48 +00003498SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3499 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003500 SDValue Ptr, SDValue Cmp,
Dale Johannesene8c17332009-01-29 00:47:48 +00003501 SDValue Swp, const Value* PtrVal,
3502 unsigned Alignment) {
3503 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
3504 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
3505
3506 MVT VT = Cmp.getValueType();
3507
3508 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3509 Alignment = getMVTAlignment(MemVT);
3510
3511 SDVTList VTs = getVTList(VT, MVT::Other);
3512 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003513 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003514 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
3515 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
3516 void* IP = 0;
3517 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3518 return SDValue(E, 0);
3519 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3520 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3521 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
3522 CSEMap.InsertNode(N, IP);
3523 AllNodes.push_back(N);
3524 return SDValue(N, 0);
3525}
3526
Dale Johannesene8c17332009-01-29 00:47:48 +00003527SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, MVT MemVT,
3528 SDValue Chain,
Scott Michelfdc40a02009-02-17 22:15:04 +00003529 SDValue Ptr, SDValue Val,
Dale Johannesene8c17332009-01-29 00:47:48 +00003530 const Value* PtrVal,
3531 unsigned Alignment) {
3532 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3533 Opcode == ISD::ATOMIC_LOAD_SUB ||
3534 Opcode == ISD::ATOMIC_LOAD_AND ||
3535 Opcode == ISD::ATOMIC_LOAD_OR ||
3536 Opcode == ISD::ATOMIC_LOAD_XOR ||
3537 Opcode == ISD::ATOMIC_LOAD_NAND ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003538 Opcode == ISD::ATOMIC_LOAD_MIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003539 Opcode == ISD::ATOMIC_LOAD_MAX ||
Scott Michelfdc40a02009-02-17 22:15:04 +00003540 Opcode == ISD::ATOMIC_LOAD_UMIN ||
Dale Johannesene8c17332009-01-29 00:47:48 +00003541 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3542 Opcode == ISD::ATOMIC_SWAP) &&
3543 "Invalid Atomic Op");
3544
3545 MVT VT = Val.getValueType();
3546
3547 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3548 Alignment = getMVTAlignment(MemVT);
3549
3550 SDVTList VTs = getVTList(VT, MVT::Other);
3551 FoldingSetNodeID ID;
Dan Gohmana7ce7412009-02-03 00:08:45 +00003552 ID.AddInteger(MemVT.getRawBits());
Dale Johannesene8c17332009-01-29 00:47:48 +00003553 SDValue Ops[] = {Chain, Ptr, Val};
3554 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
3555 void* IP = 0;
3556 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3557 return SDValue(E, 0);
3558 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
3559 new (N) AtomicSDNode(Opcode, dl, VTs, MemVT,
3560 Chain, Ptr, Val, PtrVal, Alignment);
3561 CSEMap.InsertNode(N, IP);
3562 AllNodes.push_back(N);
3563 return SDValue(N, 0);
3564}
3565
Duncan Sands4bdcb612008-07-02 17:40:58 +00003566/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3567/// Allowed to return something different (and simpler) if Simplify is true.
Dale Johannesen54c94522009-02-02 20:47:48 +00003568SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
3569 DebugLoc dl) {
3570 if (NumOps == 1)
3571 return Ops[0];
3572
3573 SmallVector<MVT, 4> VTs;
3574 VTs.reserve(NumOps);
3575 for (unsigned i = 0; i < NumOps; ++i)
3576 VTs.push_back(Ops[i].getValueType());
Scott Michelfdc40a02009-02-17 22:15:04 +00003577 return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
Dale Johannesen54c94522009-02-02 20:47:48 +00003578 Ops, NumOps);
3579}
3580
Dan Gohman475871a2008-07-27 21:46:04 +00003581SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003582SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
3583 const MVT *VTs, unsigned NumVTs,
3584 const SDValue *Ops, unsigned NumOps,
3585 MVT MemVT, const Value *srcValue, int SVOff,
3586 unsigned Align, bool Vol,
3587 bool ReadMem, bool WriteMem) {
3588 return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
3589 MemVT, srcValue, SVOff, Align, Vol,
3590 ReadMem, WriteMem);
3591}
3592
3593SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003594SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
3595 const SDValue *Ops, unsigned NumOps,
3596 MVT MemVT, const Value *srcValue, int SVOff,
3597 unsigned Align, bool Vol,
3598 bool ReadMem, bool WriteMem) {
3599 // Memoize the node unless it returns a flag.
3600 MemIntrinsicSDNode *N;
3601 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3602 FoldingSetNodeID ID;
3603 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3604 void *IP = 0;
3605 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3606 return SDValue(E, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003607
Dale Johannesene8c17332009-01-29 00:47:48 +00003608 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3609 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3610 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3611 CSEMap.InsertNode(N, IP);
3612 } else {
3613 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3614 new (N) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps, MemVT,
3615 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3616 }
3617 AllNodes.push_back(N);
3618 return SDValue(N, 0);
3619}
3620
3621SDValue
Scott Michelfdc40a02009-02-17 22:15:04 +00003622SelectionDAG::getLoad(ISD::MemIndexedMode AM, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00003623 ISD::LoadExtType ExtType, MVT VT, SDValue Chain,
3624 SDValue Ptr, SDValue Offset,
3625 const Value *SV, int SVOffset, MVT EVT,
3626 bool isVolatile, unsigned Alignment) {
3627 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3628 Alignment = getMVTAlignment(VT);
3629
3630 if (VT == EVT) {
3631 ExtType = ISD::NON_EXTLOAD;
3632 } else if (ExtType == ISD::NON_EXTLOAD) {
3633 assert(VT == EVT && "Non-extending load from different memory type!");
3634 } else {
3635 // Extending load.
3636 if (VT.isVector())
3637 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3638 "Invalid vector extload!");
3639 else
3640 assert(EVT.bitsLT(VT) &&
3641 "Should only be an extending load, not truncating!");
3642 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
3643 "Cannot sign/zero extend a FP/Vector load!");
3644 assert(VT.isInteger() == EVT.isInteger() &&
3645 "Cannot convert from FP to Int or Int -> FP!");
3646 }
3647
3648 bool Indexed = AM != ISD::UNINDEXED;
3649 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
3650 "Unindexed load with an offset!");
3651
3652 SDVTList VTs = Indexed ?
3653 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
3654 SDValue Ops[] = { Chain, Ptr, Offset };
3655 FoldingSetNodeID ID;
3656 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Dale Johannesene8c17332009-01-29 00:47:48 +00003657 ID.AddInteger(EVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003658 ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003659 void *IP = 0;
3660 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3661 return SDValue(E, 0);
3662 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
3663 new (N) LoadSDNode(Ops, dl, VTs, AM, ExtType, EVT, SV, SVOffset,
3664 Alignment, isVolatile);
3665 CSEMap.InsertNode(N, IP);
3666 AllNodes.push_back(N);
3667 return SDValue(N, 0);
3668}
3669
Dale Johannesene8c17332009-01-29 00:47:48 +00003670SDValue SelectionDAG::getLoad(MVT VT, DebugLoc dl,
3671 SDValue Chain, SDValue Ptr,
3672 const Value *SV, int SVOffset,
3673 bool isVolatile, unsigned Alignment) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003674 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003675 return getLoad(ISD::UNINDEXED, dl, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3676 SV, SVOffset, VT, isVolatile, Alignment);
3677}
3678
Dale Johannesene8c17332009-01-29 00:47:48 +00003679SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, DebugLoc dl, MVT VT,
3680 SDValue Chain, SDValue Ptr,
3681 const Value *SV,
3682 int SVOffset, MVT EVT,
3683 bool isVolatile, unsigned Alignment) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003684 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003685 return getLoad(ISD::UNINDEXED, dl, ExtType, VT, Chain, Ptr, Undef,
3686 SV, SVOffset, EVT, isVolatile, Alignment);
3687}
3688
Dan Gohman475871a2008-07-27 21:46:04 +00003689SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003690SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
3691 SDValue Offset, ISD::MemIndexedMode AM) {
3692 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
3693 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3694 "Load is already a indexed load!");
3695 return getLoad(AM, dl, LD->getExtensionType(), OrigLoad.getValueType(),
3696 LD->getChain(), Base, Offset, LD->getSrcValue(),
3697 LD->getSrcValueOffset(), LD->getMemoryVT(),
3698 LD->isVolatile(), LD->getAlignment());
3699}
3700
Dale Johannesene8c17332009-01-29 00:47:48 +00003701SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
3702 SDValue Ptr, const Value *SV, int SVOffset,
3703 bool isVolatile, unsigned Alignment) {
3704 MVT VT = Val.getValueType();
3705
3706 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3707 Alignment = getMVTAlignment(VT);
3708
3709 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00003710 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003711 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3712 FoldingSetNodeID ID;
3713 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003714 ID.AddInteger(VT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003715 ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED,
3716 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003717 void *IP = 0;
3718 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3719 return SDValue(E, 0);
3720 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3721 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, false,
3722 VT, SV, SVOffset, Alignment, isVolatile);
3723 CSEMap.InsertNode(N, IP);
3724 AllNodes.push_back(N);
3725 return SDValue(N, 0);
3726}
3727
Dale Johannesene8c17332009-01-29 00:47:48 +00003728SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
3729 SDValue Ptr, const Value *SV,
3730 int SVOffset, MVT SVT,
3731 bool isVolatile, unsigned Alignment) {
3732 MVT VT = Val.getValueType();
3733
3734 if (VT == SVT)
3735 return getStore(Chain, dl, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
3736
3737 assert(VT.bitsGT(SVT) && "Not a truncation?");
3738 assert(VT.isInteger() == SVT.isInteger() &&
3739 "Can't do FP-INT conversion!");
3740
3741 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3742 Alignment = getMVTAlignment(VT);
3743
3744 SDVTList VTs = getVTList(MVT::Other);
Dale Johannesene8d72302009-02-06 23:05:02 +00003745 SDValue Undef = getUNDEF(Ptr.getValueType());
Dale Johannesene8c17332009-01-29 00:47:48 +00003746 SDValue Ops[] = { Chain, Val, Ptr, Undef };
3747 FoldingSetNodeID ID;
3748 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003749 ID.AddInteger(SVT.getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003750 ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED,
3751 isVolatile, Alignment));
Dale Johannesene8c17332009-01-29 00:47:48 +00003752 void *IP = 0;
3753 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3754 return SDValue(E, 0);
3755 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3756 new (N) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED, true,
3757 SVT, SV, SVOffset, Alignment, isVolatile);
3758 CSEMap.InsertNode(N, IP);
3759 AllNodes.push_back(N);
3760 return SDValue(N, 0);
3761}
3762
Dan Gohman475871a2008-07-27 21:46:04 +00003763SDValue
Dale Johannesene8c17332009-01-29 00:47:48 +00003764SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
3765 SDValue Offset, ISD::MemIndexedMode AM) {
3766 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3767 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3768 "Store is already a indexed store!");
3769 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
3770 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
3771 FoldingSetNodeID ID;
3772 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Dale Johannesene8c17332009-01-29 00:47:48 +00003773 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmana7ce7412009-02-03 00:08:45 +00003774 ID.AddInteger(ST->getRawSubclassData());
Dale Johannesene8c17332009-01-29 00:47:48 +00003775 void *IP = 0;
3776 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3777 return SDValue(E, 0);
3778 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
3779 new (N) StoreSDNode(Ops, dl, VTs, AM,
3780 ST->isTruncatingStore(), ST->getMemoryVT(),
3781 ST->getSrcValue(), ST->getSrcValueOffset(),
3782 ST->getAlignment(), ST->isVolatile());
3783 CSEMap.InsertNode(N, IP);
3784 AllNodes.push_back(N);
3785 return SDValue(N, 0);
3786}
3787
Dale Johannesen0f502f62009-02-03 22:26:09 +00003788SDValue SelectionDAG::getVAArg(MVT VT, DebugLoc dl,
3789 SDValue Chain, SDValue Ptr,
3790 SDValue SV) {
3791 SDValue Ops[] = { Chain, Ptr, SV };
3792 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 3);
3793}
3794
Bill Wendling7ade28c2009-01-28 22:17:52 +00003795SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
3796 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003797 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003798 case 0: return getNode(Opcode, DL, VT);
3799 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3800 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3801 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003802 default: break;
3803 }
3804
Dan Gohman475871a2008-07-27 21:46:04 +00003805 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003806 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003808 return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003809}
3810
Bill Wendling7ade28c2009-01-28 22:17:52 +00003811SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, MVT VT,
3812 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003813 switch (NumOps) {
Bill Wendling7ade28c2009-01-28 22:17:52 +00003814 case 0: return getNode(Opcode, DL, VT);
3815 case 1: return getNode(Opcode, DL, VT, Ops[0]);
3816 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
3817 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003818 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003819 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003820
Chris Lattneref847df2005-04-09 03:27:28 +00003821 switch (Opcode) {
3822 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003823 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003824 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003825 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3826 "LHS and RHS of condition must have same type!");
3827 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3828 "True and False arms of SelectCC must have same type!");
3829 assert(Ops[2].getValueType() == VT &&
3830 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003831 break;
3832 }
3833 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003834 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003835 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3836 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003837 break;
3838 }
Chris Lattneref847df2005-04-09 03:27:28 +00003839 }
3840
Chris Lattner385328c2005-05-14 07:42:29 +00003841 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003842 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003843 SDVTList VTs = getVTList(VT);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003844
Chris Lattner43247a12005-08-25 19:12:10 +00003845 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003846 FoldingSetNodeID ID;
3847 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003848 void *IP = 0;
Bill Wendling7ade28c2009-01-28 22:17:52 +00003849
Chris Lattnera5682852006-08-07 23:03:03 +00003850 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003851 return SDValue(E, 0);
Bill Wendling7ade28c2009-01-28 22:17:52 +00003852
Dan Gohmanfed90b62008-07-28 21:51:04 +00003853 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003854 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003855 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003856 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003857 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003858 new (N) SDNode(Opcode, DL, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003859 }
Bill Wendling7ade28c2009-01-28 22:17:52 +00003860
Chris Lattneref847df2005-04-09 03:27:28 +00003861 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003862#ifndef NDEBUG
3863 VerifyNode(N);
3864#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003865 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003866}
3867
Bill Wendling7ade28c2009-01-28 22:17:52 +00003868SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
3869 const std::vector<MVT> &ResultTys,
3870 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00003871 return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003872 Ops, NumOps);
3873}
3874
Bill Wendling7ade28c2009-01-28 22:17:52 +00003875SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
3876 const MVT *VTs, unsigned NumVTs,
3877 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003878 if (NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003879 return getNode(Opcode, DL, VTs[0], Ops, NumOps);
3880 return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
Scott Michelfdc40a02009-02-17 22:15:04 +00003881}
3882
Bill Wendling7ade28c2009-01-28 22:17:52 +00003883SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3884 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003885 if (VTList.NumVTs == 1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003886 return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003887
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00003888#if 0
Chris Lattner5f056bf2005-07-10 01:55:33 +00003889 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003890 // FIXME: figure out how to safely handle things like
3891 // int foo(int x) { return 1 << (x & 255); }
3892 // int bar() { return foo(256); }
Chris Lattnere89083a2005-05-14 07:25:05 +00003893 case ISD::SRA_PARTS:
3894 case ISD::SRL_PARTS:
3895 case ISD::SHL_PARTS:
3896 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003897 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003898 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00003899 else if (N3.getOpcode() == ISD::AND)
3900 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3901 // If the and is only masking out bits that cannot effect the shift,
3902 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003903 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003904 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
Bill Wendling7ade28c2009-01-28 22:17:52 +00003905 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
Chris Lattnere89083a2005-05-14 07:25:05 +00003906 }
3907 break;
Chris Lattner5f056bf2005-07-10 01:55:33 +00003908 }
Daniel Dunbarcfb8a1b2009-07-19 01:38:38 +00003909#endif
Chris Lattner89c34632005-05-14 06:20:26 +00003910
Chris Lattner43247a12005-08-25 19:12:10 +00003911 // Memoize the node unless it returns a flag.
3912 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003913 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003914 FoldingSetNodeID ID;
3915 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003916 void *IP = 0;
3917 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003918 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003919 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003920 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003921 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003922 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003923 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003924 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003925 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003926 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003927 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003928 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003929 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003930 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003931 }
Chris Lattnera5682852006-08-07 23:03:03 +00003932 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003933 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003934 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003935 N = NodeAllocator.Allocate<UnarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003936 new (N) UnarySDNode(Opcode, DL, VTList, Ops[0]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003937 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003938 N = NodeAllocator.Allocate<BinarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003939 new (N) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003940 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003941 N = NodeAllocator.Allocate<TernarySDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003942 new (N) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1], Ops[2]);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003943 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003944 N = NodeAllocator.Allocate<SDNode>();
Bill Wendling7ade28c2009-01-28 22:17:52 +00003945 new (N) SDNode(Opcode, DL, VTList, Ops, NumOps);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003946 }
Chris Lattner43247a12005-08-25 19:12:10 +00003947 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003948 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003949#ifndef NDEBUG
3950 VerifyNode(N);
3951#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003952 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003953}
3954
Bill Wendling7ade28c2009-01-28 22:17:52 +00003955SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
3956 return getNode(Opcode, DL, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003957}
3958
Bill Wendling7ade28c2009-01-28 22:17:52 +00003959SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3960 SDValue N1) {
Dan Gohman475871a2008-07-27 21:46:04 +00003961 SDValue Ops[] = { N1 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003962 return getNode(Opcode, DL, VTList, Ops, 1);
Dan Gohman08ce9762007-10-08 15:49:58 +00003963}
3964
Bill Wendling7ade28c2009-01-28 22:17:52 +00003965SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3966 SDValue N1, SDValue N2) {
Dan Gohman475871a2008-07-27 21:46:04 +00003967 SDValue Ops[] = { N1, N2 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003968 return getNode(Opcode, DL, VTList, Ops, 2);
Dan Gohman08ce9762007-10-08 15:49:58 +00003969}
3970
Bill Wendling7ade28c2009-01-28 22:17:52 +00003971SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3972 SDValue N1, SDValue N2, SDValue N3) {
Dan Gohman475871a2008-07-27 21:46:04 +00003973 SDValue Ops[] = { N1, N2, N3 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003974 return getNode(Opcode, DL, VTList, Ops, 3);
Dan Gohman08ce9762007-10-08 15:49:58 +00003975}
3976
Bill Wendling7ade28c2009-01-28 22:17:52 +00003977SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3978 SDValue N1, SDValue N2, SDValue N3,
3979 SDValue N4) {
Dan Gohman475871a2008-07-27 21:46:04 +00003980 SDValue Ops[] = { N1, N2, N3, N4 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003981 return getNode(Opcode, DL, VTList, Ops, 4);
Dan Gohman08ce9762007-10-08 15:49:58 +00003982}
3983
Bill Wendling7ade28c2009-01-28 22:17:52 +00003984SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
3985 SDValue N1, SDValue N2, SDValue N3,
3986 SDValue N4, SDValue N5) {
Dan Gohman475871a2008-07-27 21:46:04 +00003987 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Bill Wendling7ade28c2009-01-28 22:17:52 +00003988 return getNode(Opcode, DL, VTList, Ops, 5);
Dan Gohman08ce9762007-10-08 15:49:58 +00003989}
3990
Duncan Sands83ec4b62008-06-06 12:08:01 +00003991SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003992 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003993}
3994
Duncan Sands83ec4b62008-06-06 12:08:01 +00003995SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003996 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3997 E = VTList.rend(); I != E; ++I)
3998 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3999 return *I;
4000
4001 MVT *Array = Allocator.Allocate<MVT>(2);
4002 Array[0] = VT1;
4003 Array[1] = VT2;
4004 SDVTList Result = makeVTList(Array, 2);
4005 VTList.push_back(Result);
4006 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00004007}
Dan Gohmane8be6c62008-07-17 19:10:17 +00004008
4009SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
4010 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4011 E = VTList.rend(); I != E; ++I)
4012 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4013 I->VTs[2] == VT3)
4014 return *I;
4015
4016 MVT *Array = Allocator.Allocate<MVT>(3);
4017 Array[0] = VT1;
4018 Array[1] = VT2;
4019 Array[2] = VT3;
4020 SDVTList Result = makeVTList(Array, 3);
4021 VTList.push_back(Result);
4022 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00004023}
4024
Bill Wendling13d6d442008-12-01 23:28:22 +00004025SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
4026 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4027 E = VTList.rend(); I != E; ++I)
4028 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
4029 I->VTs[2] == VT3 && I->VTs[3] == VT4)
4030 return *I;
4031
4032 MVT *Array = Allocator.Allocate<MVT>(3);
4033 Array[0] = VT1;
4034 Array[1] = VT2;
4035 Array[2] = VT3;
4036 Array[3] = VT4;
4037 SDVTList Result = makeVTList(Array, 4);
4038 VTList.push_back(Result);
4039 return Result;
4040}
4041
Duncan Sands83ec4b62008-06-06 12:08:01 +00004042SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00004043 switch (NumVTs) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004044 case 0: llvm_unreachable("Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00004045 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00004046 case 2: return getVTList(VTs[0], VTs[1]);
4047 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
4048 default: break;
4049 }
4050
Dan Gohmane8be6c62008-07-17 19:10:17 +00004051 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
4052 E = VTList.rend(); I != E; ++I) {
4053 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
4054 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00004055
Chris Lattner70046e92006-08-15 17:46:01 +00004056 bool NoMatch = false;
4057 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004058 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00004059 NoMatch = true;
4060 break;
4061 }
4062 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00004063 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00004064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004065
Dan Gohmane8be6c62008-07-17 19:10:17 +00004066 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
4067 std::copy(VTs, VTs+NumVTs, Array);
4068 SDVTList Result = makeVTList(Array, NumVTs);
4069 VTList.push_back(Result);
4070 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00004071}
4072
4073
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004074/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
4075/// specified operands. If the resultant node already exists in the DAG,
4076/// this does not modify the specified node, instead it returns the node that
4077/// already exists. If the resultant node does not exist in the DAG, the
4078/// input node is returned. As a degenerate case, if you specify the same
4079/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00004080SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004081 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004082 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004083
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004084 // Check to see if there is no change.
4085 if (Op == N->getOperand(0)) return InN;
Scott Michelfdc40a02009-02-17 22:15:04 +00004086
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004087 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004088 void *InsertPos = 0;
4089 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004090 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004091
Dan Gohman79acd2b2008-07-21 22:38:59 +00004092 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004093 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004094 if (!RemoveNodeFromCSEMaps(N))
4095 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004096
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004097 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004098 N->OperandList[0].set(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004099
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004100 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004101 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004102 return InN;
4103}
4104
Dan Gohman475871a2008-07-27 21:46:04 +00004105SDValue SelectionDAG::
4106UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004107 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004108 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004109
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004110 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004111 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
4112 return InN; // No operands changed, just return the input node.
Scott Michelfdc40a02009-02-17 22:15:04 +00004113
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004114 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004115 void *InsertPos = 0;
4116 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004117 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004118
Dan Gohman79acd2b2008-07-21 22:38:59 +00004119 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004120 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004121 if (!RemoveNodeFromCSEMaps(N))
4122 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004123
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004124 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004125 if (N->OperandList[0] != Op1)
4126 N->OperandList[0].set(Op1);
4127 if (N->OperandList[1] != Op2)
4128 N->OperandList[1].set(Op2);
Scott Michelfdc40a02009-02-17 22:15:04 +00004129
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004130 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004131 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004132 return InN;
4133}
4134
Dan Gohman475871a2008-07-27 21:46:04 +00004135SDValue SelectionDAG::
4136UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
4137 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004138 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004139}
4140
Dan Gohman475871a2008-07-27 21:46:04 +00004141SDValue SelectionDAG::
Scott Michelfdc40a02009-02-17 22:15:04 +00004142UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004143 SDValue Op3, SDValue Op4) {
4144 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004145 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004146}
4147
Dan Gohman475871a2008-07-27 21:46:04 +00004148SDValue SelectionDAG::
4149UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
4150 SDValue Op3, SDValue Op4, SDValue Op5) {
4151 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004152 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00004153}
4154
Dan Gohman475871a2008-07-27 21:46:04 +00004155SDValue SelectionDAG::
4156UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004157 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004158 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004159 "Update with wrong number of operands");
Scott Michelfdc40a02009-02-17 22:15:04 +00004160
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004161 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004162 bool AnyChange = false;
4163 for (unsigned i = 0; i != NumOps; ++i) {
4164 if (Ops[i] != N->getOperand(i)) {
4165 AnyChange = true;
4166 break;
4167 }
4168 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004169
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004170 // No operands changed, just return the input node.
4171 if (!AnyChange) return InN;
Scott Michelfdc40a02009-02-17 22:15:04 +00004172
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004173 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00004174 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00004175 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00004176 return SDValue(Existing, InN.getResNo());
Scott Michelfdc40a02009-02-17 22:15:04 +00004177
Dan Gohman7ceda162008-05-02 00:05:03 +00004178 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00004179 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00004180 if (!RemoveNodeFromCSEMaps(N))
4181 InsertPos = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004182
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004183 // Now we update the operands.
Dan Gohmane7852d02009-01-26 04:35:06 +00004184 for (unsigned i = 0; i != NumOps; ++i)
4185 if (N->OperandList[i] != Ops[i])
4186 N->OperandList[i].set(Ops[i]);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004187
4188 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00004189 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00004190 return InN;
4191}
4192
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004193/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00004194/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004195void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004196 // Unlike the code in MorphNodeTo that does this, we don't need to
4197 // watch for dead nodes here.
Dan Gohmane7852d02009-01-26 04:35:06 +00004198 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
4199 SDUse &Use = *I++;
4200 Use.set(SDValue());
4201 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00004202}
Chris Lattner149c58c2005-08-16 18:17:10 +00004203
Dan Gohmane8be6c62008-07-17 19:10:17 +00004204/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
4205/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004206///
Dan Gohmane8be6c62008-07-17 19:10:17 +00004207SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004208 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004209 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004210 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00004211}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004212
Dan Gohmane8be6c62008-07-17 19:10:17 +00004213SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004214 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004215 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004216 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004217 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004218}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004219
Dan Gohmane8be6c62008-07-17 19:10:17 +00004220SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004221 MVT VT, SDValue Op1,
4222 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004223 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004224 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004225 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004226}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004227
Dan Gohmane8be6c62008-07-17 19:10:17 +00004228SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004229 MVT VT, SDValue Op1,
4230 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004231 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004233 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004234}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004235
Dan Gohmane8be6c62008-07-17 19:10:17 +00004236SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004237 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004238 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004239 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004240 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004241}
4242
Dan Gohmane8be6c62008-07-17 19:10:17 +00004243SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004244 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004245 unsigned NumOps) {
4246 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004247 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004248}
4249
Dan Gohmane8be6c62008-07-17 19:10:17 +00004250SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004251 MVT VT1, MVT VT2) {
4252 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004253 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004254}
4255
Dan Gohmane8be6c62008-07-17 19:10:17 +00004256SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004257 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004258 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004259 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004260 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004261}
4262
Bill Wendling13d6d442008-12-01 23:28:22 +00004263SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4264 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4265 const SDValue *Ops, unsigned NumOps) {
4266 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4267 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4268}
4269
Scott Michelfdc40a02009-02-17 22:15:04 +00004270SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004271 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004272 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004273 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004274 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004275 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004276}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004277
Scott Michelfdc40a02009-02-17 22:15:04 +00004278SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004279 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004280 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004281 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004282 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004283 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004284}
4285
Dan Gohmane8be6c62008-07-17 19:10:17 +00004286SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 MVT VT1, MVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004288 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004289 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004290 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004292 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004293}
4294
Dan Gohmane8be6c62008-07-17 19:10:17 +00004295SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling13d6d442008-12-01 23:28:22 +00004296 MVT VT1, MVT VT2, MVT VT3,
Scott Michelfdc40a02009-02-17 22:15:04 +00004297 SDValue Op1, SDValue Op2,
Bill Wendling13d6d442008-12-01 23:28:22 +00004298 SDValue Op3) {
4299 SDVTList VTs = getVTList(VT1, VT2, VT3);
4300 SDValue Ops[] = { Op1, Op2, Op3 };
4301 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4302}
4303
4304SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004305 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004306 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004307 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4308}
4309
4310SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4311 MVT VT) {
4312 SDVTList VTs = getVTList(VT);
4313 return MorphNodeTo(N, Opc, VTs, 0, 0);
4314}
4315
4316SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004317 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004318 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004319 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004320 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4321}
4322
4323SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004324 MVT VT, SDValue Op1,
4325 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004326 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004327 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004328 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4329}
4330
4331SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004332 MVT VT, SDValue Op1,
4333 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004334 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004335 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004336 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4337}
4338
4339SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004340 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004341 unsigned NumOps) {
4342 SDVTList VTs = getVTList(VT);
4343 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4344}
4345
4346SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004347 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004348 unsigned NumOps) {
4349 SDVTList VTs = getVTList(VT1, VT2);
4350 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4351}
4352
4353SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4354 MVT VT1, MVT VT2) {
4355 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004356 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004357}
4358
4359SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4360 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004361 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004362 SDVTList VTs = getVTList(VT1, VT2, VT3);
4363 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4364}
4365
Scott Michelfdc40a02009-02-17 22:15:04 +00004366SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004367 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004368 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004369 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004370 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004371 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4372}
4373
Scott Michelfdc40a02009-02-17 22:15:04 +00004374SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004375 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004376 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004377 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004378 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004379 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4380}
4381
4382SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4383 MVT VT1, MVT VT2,
Scott Michelfdc40a02009-02-17 22:15:04 +00004384 SDValue Op1, SDValue Op2,
Dan Gohman475871a2008-07-27 21:46:04 +00004385 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004386 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004387 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004388 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4389}
4390
4391/// MorphNodeTo - These *mutate* the specified node to have the specified
4392/// return type, opcode, and operands.
4393///
4394/// Note that MorphNodeTo returns the resultant node. If there is already a
4395/// node of the specified opcode and operands, it returns that node instead of
Dale Johannesena05dca42009-02-04 23:02:30 +00004396/// the current one. Note that the DebugLoc need not be the same.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004397///
4398/// Using MorphNodeTo is faster than creating a new node and swapping it in
4399/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004400/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004401/// the node's users.
4402///
4403SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004404 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004405 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004406 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004407 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004408 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4409 FoldingSetNodeID ID;
4410 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4411 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4412 return ON;
4413 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004414
Dan Gohman095cc292008-09-13 01:54:27 +00004415 if (!RemoveNodeFromCSEMaps(N))
4416 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004417
Dan Gohmane8be6c62008-07-17 19:10:17 +00004418 // Start the morphing.
4419 N->NodeType = Opc;
4420 N->ValueList = VTs.VTs;
4421 N->NumValues = VTs.NumVTs;
Scott Michelfdc40a02009-02-17 22:15:04 +00004422
Dan Gohmane8be6c62008-07-17 19:10:17 +00004423 // Clear the operands list, updating used nodes to remove this from their
4424 // use list. Keep track of any operands that become dead as a result.
4425 SmallPtrSet<SDNode*, 16> DeadNodeSet;
Dan Gohmane7852d02009-01-26 04:35:06 +00004426 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
4427 SDUse &Use = *I++;
4428 SDNode *Used = Use.getNode();
4429 Use.set(SDValue());
Dan Gohmane8be6c62008-07-17 19:10:17 +00004430 if (Used->use_empty())
4431 DeadNodeSet.insert(Used);
4432 }
4433
4434 // If NumOps is larger than the # of operands we currently have, reallocate
4435 // the operand list.
4436 if (NumOps > N->NumOperands) {
4437 if (N->OperandsNeedDelete)
4438 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004439
Dan Gohmane8be6c62008-07-17 19:10:17 +00004440 if (N->isMachineOpcode()) {
4441 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004442 // remainder of the current SelectionDAG iteration, so we can allocate
4443 // the operands directly out of a pool with no recycling metadata.
4444 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004445 N->OperandsNeedDelete = false;
4446 } else {
4447 N->OperandList = new SDUse[NumOps];
4448 N->OperandsNeedDelete = true;
4449 }
4450 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004451
Dan Gohmane8be6c62008-07-17 19:10:17 +00004452 // Assign the new operands.
4453 N->NumOperands = NumOps;
4454 for (unsigned i = 0, e = NumOps; i != e; ++i) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004455 N->OperandList[i].setUser(N);
Dan Gohmane7852d02009-01-26 04:35:06 +00004456 N->OperandList[i].setInitial(Ops[i]);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004457 }
4458
4459 // Delete any nodes that are still dead after adding the uses for the
4460 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004461 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004462 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4463 E = DeadNodeSet.end(); I != E; ++I)
4464 if ((*I)->use_empty())
4465 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004466 RemoveDeadNodes(DeadNodes);
4467
Dan Gohmane8be6c62008-07-17 19:10:17 +00004468 if (IP)
4469 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004470 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004471}
4472
Chris Lattner0fb094f2005-11-19 01:44:53 +00004473
Evan Cheng6ae46c42006-02-09 07:15:23 +00004474/// getTargetNode - These are used for target selectors to create a new node
4475/// with specified return type(s), target opcode, and operands.
4476///
4477/// Note that getTargetNode returns the resultant node. If there is already a
4478/// node of the specified opcode and operands, it returns that node instead of
4479/// the current one.
Bill Wendling56ab1a22009-01-29 09:01:55 +00004480SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT) {
4481 return getNode(~Opcode, dl, VT).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004482}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004483
Bill Wendling56ab1a22009-01-29 09:01:55 +00004484SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4485 SDValue Op1) {
4486 return getNode(~Opcode, dl, VT, Op1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004487}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004488
Bill Wendling56ab1a22009-01-29 09:01:55 +00004489SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004490 SDValue Op1, SDValue Op2) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004491 return getNode(~Opcode, dl, VT, Op1, Op2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004492}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004493
Bill Wendling56ab1a22009-01-29 09:01:55 +00004494SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
4495 SDValue Op1, SDValue Op2,
4496 SDValue Op3) {
4497 return getNode(~Opcode, dl, VT, Op1, Op2, Op3).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004498}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004499
Bill Wendling56ab1a22009-01-29 09:01:55 +00004500SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT,
Dale Johannesene8c17332009-01-29 00:47:48 +00004501 const SDValue *Ops, unsigned NumOps) {
Bill Wendling56ab1a22009-01-29 09:01:55 +00004502 return getNode(~Opcode, dl, VT, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004503}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004504
Scott Michelfdc40a02009-02-17 22:15:04 +00004505SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004506 MVT VT1, MVT VT2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004507 SDVTList VTs = getVTList(VT1, VT2);
Dale Johannesene8c17332009-01-29 00:47:48 +00004508 SDValue Op;
Dan Gohmanfc166572009-04-09 23:54:40 +00004509 return getNode(~Opcode, dl, VTs, &Op, 0).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004510}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004511
Bill Wendling56ab1a22009-01-29 09:01:55 +00004512SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
Dale Johannesene8c17332009-01-29 00:47:48 +00004513 MVT VT2, SDValue Op1) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004514 SDVTList VTs = getVTList(VT1, VT2);
4515 return getNode(~Opcode, dl, VTs, &Op1, 1).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004516}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004517
Bill Wendling56ab1a22009-01-29 09:01:55 +00004518SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4519 MVT VT2, SDValue Op1,
4520 SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004521 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004522 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004523 return getNode(~Opcode, dl, VTs, Ops, 2).getNode();
Bill Wendling56ab1a22009-01-29 09:01:55 +00004524}
4525
Bill Wendling56ab1a22009-01-29 09:01:55 +00004526SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4527 MVT VT2, SDValue Op1,
4528 SDValue Op2, SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004529 SDVTList VTs = getVTList(VT1, VT2);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004530 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004531 return getNode(~Opcode, dl, VTs, Ops, 3).getNode();
Bill Wendling56ab1a22009-01-29 09:01:55 +00004532}
4533
Scott Michelfdc40a02009-02-17 22:15:04 +00004534SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004535 MVT VT1, MVT VT2,
4536 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004537 SDVTList VTs = getVTList(VT1, VT2);
4538 return getNode(~Opcode, dl, VTs, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004539}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004540
Bill Wendling56ab1a22009-01-29 09:01:55 +00004541SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004542 MVT VT1, MVT VT2, MVT VT3,
4543 SDValue Op1, SDValue Op2) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004544 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dale Johannesene8c17332009-01-29 00:47:48 +00004545 SDValue Ops[] = { Op1, Op2 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004546 return getNode(~Opcode, dl, VTs, Ops, 2).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004547}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004548
Bill Wendling56ab1a22009-01-29 09:01:55 +00004549SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Bill Wendling6e1bb382009-01-29 05:27:31 +00004550 MVT VT1, MVT VT2, MVT VT3,
Bill Wendling56ab1a22009-01-29 09:01:55 +00004551 SDValue Op1, SDValue Op2,
4552 SDValue Op3) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004553 SDVTList VTs = getVTList(VT1, VT2, VT3);
Bill Wendling56ab1a22009-01-29 09:01:55 +00004554 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmanfc166572009-04-09 23:54:40 +00004555 return getNode(~Opcode, dl, VTs, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004556}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004557
Bill Wendling56ab1a22009-01-29 09:01:55 +00004558SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004559 MVT VT1, MVT VT2, MVT VT3,
4560 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004561 SDVTList VTs = getVTList(VT1, VT2, VT3);
4562 return getNode(~Opcode, dl, VTs, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004563}
Bill Wendling56ab1a22009-01-29 09:01:55 +00004564
Bill Wendling56ab1a22009-01-29 09:01:55 +00004565SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl, MVT VT1,
4566 MVT VT2, MVT VT3, MVT VT4,
4567 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004568 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4569 return getNode(~Opcode, dl, VTs, Ops, NumOps).getNode();
Bill Wendling56ab1a22009-01-29 09:01:55 +00004570}
4571
Bill Wendling56ab1a22009-01-29 09:01:55 +00004572SDNode *SelectionDAG::getTargetNode(unsigned Opcode, DebugLoc dl,
Dale Johannesene8c17332009-01-29 00:47:48 +00004573 const std::vector<MVT> &ResultTys,
4574 const SDValue *Ops, unsigned NumOps) {
Dan Gohmanfc166572009-04-09 23:54:40 +00004575 return getNode(~Opcode, dl, ResultTys, Ops, NumOps).getNode();
Dale Johannesene8c17332009-01-29 00:47:48 +00004576}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004577
Evan Cheng08b11732008-03-22 01:55:50 +00004578/// getNodeIfExists - Get the specified node if it's already available, or
4579/// else return NULL.
4580SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004581 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004582 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4583 FoldingSetNodeID ID;
4584 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4585 void *IP = 0;
4586 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4587 return E;
4588 }
4589 return NULL;
4590}
4591
Evan Cheng99157a02006-08-07 22:13:29 +00004592/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004593/// This can cause recursive merging of nodes in the DAG.
4594///
Chris Lattner11d049c2008-02-03 03:35:22 +00004595/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004596///
Dan Gohman475871a2008-07-27 21:46:04 +00004597void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004598 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004599 SDNode *From = FromN.getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004600 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004601 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004602 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004603
Dan Gohman39946102009-01-25 16:29:12 +00004604 // Iterate over all the existing uses of From. New uses will be added
4605 // to the beginning of the use list, which we avoid visiting.
4606 // This specifically avoids visiting uses of From that arise while the
4607 // replacement is happening, because any such uses would be the result
4608 // of CSE: If an existing node looks like From after one of its operands
4609 // is replaced by To, we don't want to replace of all its users with To
4610 // too. See PR3018 for more info.
Dan Gohmandbe664a2009-01-19 21:44:21 +00004611 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4612 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004613 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004614
Chris Lattner8b8749f2005-08-17 19:00:20 +00004615 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004616 RemoveNodeFromCSEMaps(User);
Chris Lattner8b52f212005-08-26 18:36:28 +00004617
Dan Gohman39946102009-01-25 16:29:12 +00004618 // A user can appear in a use list multiple times, and when this
4619 // happens the uses are usually next to each other in the list.
4620 // To help reduce the number of CSE recomputations, process all
4621 // the uses of this user that we can find this way.
4622 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004623 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004624 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004625 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004626 } while (UI != UE && *UI == User);
4627
4628 // Now that we have modified User, add it back to the CSE maps. If it
4629 // already exists there, recursively merge the results together.
4630 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b52f212005-08-26 18:36:28 +00004631 }
4632}
4633
4634/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4635/// This can cause recursive merging of nodes in the DAG.
4636///
Dan Gohmanc23e4962009-04-15 20:06:30 +00004637/// This version assumes that for each value of From, there is a
4638/// corresponding value in To in the same position with the same type.
Chris Lattner8b52f212005-08-26 18:36:28 +00004639///
Chris Lattner1e111c72005-09-07 05:37:01 +00004640void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004641 DAGUpdateListener *UpdateListener) {
Dan Gohmanc23e4962009-04-15 20:06:30 +00004642#ifndef NDEBUG
4643 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
4644 assert((!From->hasAnyUseOfValue(i) ||
4645 From->getValueType(i) == To->getValueType(i)) &&
4646 "Cannot use this version of ReplaceAllUsesWith!");
4647#endif
Dan Gohmane8be6c62008-07-17 19:10:17 +00004648
4649 // Handle the trivial case.
4650 if (From == To)
4651 return;
4652
Dan Gohmandbe664a2009-01-19 21:44:21 +00004653 // Iterate over just the existing users of From. See the comments in
4654 // the ReplaceAllUsesWith above.
4655 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4656 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004657 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004658
Chris Lattner8b52f212005-08-26 18:36:28 +00004659 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004660 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004661
Dan Gohman39946102009-01-25 16:29:12 +00004662 // A user can appear in a use list multiple times, and when this
4663 // happens the uses are usually next to each other in the list.
4664 // To help reduce the number of CSE recomputations, process all
4665 // the uses of this user that we can find this way.
4666 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004667 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004668 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004669 Use.setNode(To);
Dan Gohman39946102009-01-25 16:29:12 +00004670 } while (UI != UE && *UI == User);
4671
4672 // Now that we have modified User, add it back to the CSE maps. If it
4673 // already exists there, recursively merge the results together.
4674 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004675 }
4676}
4677
Chris Lattner8b52f212005-08-26 18:36:28 +00004678/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4679/// This can cause recursive merging of nodes in the DAG.
4680///
4681/// This version can replace From with any result values. To must match the
4682/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004683void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004684 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004685 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004686 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004687 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004688
Dan Gohmandbe664a2009-01-19 21:44:21 +00004689 // Iterate over just the existing users of From. See the comments in
4690 // the ReplaceAllUsesWith above.
4691 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4692 while (UI != UE) {
Dan Gohman39946102009-01-25 16:29:12 +00004693 SDNode *User = *UI;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004694
Chris Lattner7b2880c2005-08-24 22:44:39 +00004695 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohman39946102009-01-25 16:29:12 +00004696 RemoveNodeFromCSEMaps(User);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004697
Dan Gohman39946102009-01-25 16:29:12 +00004698 // A user can appear in a use list multiple times, and when this
4699 // happens the uses are usually next to each other in the list.
4700 // To help reduce the number of CSE recomputations, process all
4701 // the uses of this user that we can find this way.
4702 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004703 SDUse &Use = UI.getUse();
4704 const SDValue &ToOp = To[Use.getResNo()];
Dan Gohman39946102009-01-25 16:29:12 +00004705 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004706 Use.set(ToOp);
Dan Gohman39946102009-01-25 16:29:12 +00004707 } while (UI != UE && *UI == User);
4708
4709 // Now that we have modified User, add it back to the CSE maps. If it
4710 // already exists there, recursively merge the results together.
4711 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004712 }
4713}
4714
Chris Lattner012f2412006-02-17 21:58:01 +00004715/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004716/// uses of other values produced by From.getNode() alone. The Deleted
4717/// vector is handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004718void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004719 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004720 // Handle the really simple, really trivial case efficiently.
4721 if (From == To) return;
4722
Chris Lattner012f2412006-02-17 21:58:01 +00004723 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004724 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004725 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004726 return;
4727 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004728
Dan Gohman39946102009-01-25 16:29:12 +00004729 // Iterate over just the existing users of From. See the comments in
4730 // the ReplaceAllUsesWith above.
4731 SDNode::use_iterator UI = From.getNode()->use_begin(),
4732 UE = From.getNode()->use_end();
4733 while (UI != UE) {
4734 SDNode *User = *UI;
4735 bool UserRemovedFromCSEMaps = false;
Chris Lattner012f2412006-02-17 21:58:01 +00004736
Dan Gohman39946102009-01-25 16:29:12 +00004737 // A user can appear in a use list multiple times, and when this
4738 // happens the uses are usually next to each other in the list.
4739 // To help reduce the number of CSE recomputations, process all
4740 // the uses of this user that we can find this way.
4741 do {
Dan Gohmane7852d02009-01-26 04:35:06 +00004742 SDUse &Use = UI.getUse();
Dan Gohman39946102009-01-25 16:29:12 +00004743
4744 // Skip uses of different values from the same node.
Dan Gohmane7852d02009-01-26 04:35:06 +00004745 if (Use.getResNo() != From.getResNo()) {
Dan Gohman39946102009-01-25 16:29:12 +00004746 ++UI;
4747 continue;
Chris Lattner012f2412006-02-17 21:58:01 +00004748 }
Dan Gohman39946102009-01-25 16:29:12 +00004749
4750 // If this node hasn't been modified yet, it's still in the CSE maps,
4751 // so remove its old self from the CSE maps.
4752 if (!UserRemovedFromCSEMaps) {
4753 RemoveNodeFromCSEMaps(User);
4754 UserRemovedFromCSEMaps = true;
4755 }
4756
4757 ++UI;
Dan Gohmane7852d02009-01-26 04:35:06 +00004758 Use.set(To);
Dan Gohman39946102009-01-25 16:29:12 +00004759 } while (UI != UE && *UI == User);
4760
4761 // We are iterating over all uses of the From node, so if a use
4762 // doesn't use the specific value, no changes are made.
4763 if (!UserRemovedFromCSEMaps)
4764 continue;
4765
Chris Lattner01d029b2007-10-15 06:10:22 +00004766 // Now that we have modified User, add it back to the CSE maps. If it
4767 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004768 AddModifiedNodeToCSEMaps(User, UpdateListener);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004769 }
4770}
4771
Dan Gohman39946102009-01-25 16:29:12 +00004772namespace {
4773 /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
4774 /// to record information about a use.
4775 struct UseMemo {
4776 SDNode *User;
4777 unsigned Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00004778 SDUse *Use;
Dan Gohman39946102009-01-25 16:29:12 +00004779 };
Dan Gohmane7852d02009-01-26 04:35:06 +00004780
4781 /// operator< - Sort Memos by User.
4782 bool operator<(const UseMemo &L, const UseMemo &R) {
4783 return (intptr_t)L.User < (intptr_t)R.User;
4784 }
Dan Gohman39946102009-01-25 16:29:12 +00004785}
4786
Dan Gohmane8be6c62008-07-17 19:10:17 +00004787/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Dan Gohmane7852d02009-01-26 04:35:06 +00004788/// uses of other values produced by From.getNode() alone. The same value
4789/// may appear in both the From and To list. The Deleted vector is
Dan Gohmane8be6c62008-07-17 19:10:17 +00004790/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004791void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4792 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004793 unsigned Num,
4794 DAGUpdateListener *UpdateListener){
4795 // Handle the simple, trivial case efficiently.
4796 if (Num == 1)
4797 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4798
Dan Gohman39946102009-01-25 16:29:12 +00004799 // Read up all the uses and make records of them. This helps
4800 // processing new uses that are introduced during the
4801 // replacement process.
4802 SmallVector<UseMemo, 4> Uses;
4803 for (unsigned i = 0; i != Num; ++i) {
4804 unsigned FromResNo = From[i].getResNo();
4805 SDNode *FromNode = From[i].getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00004806 for (SDNode::use_iterator UI = FromNode->use_begin(),
Dan Gohmane7852d02009-01-26 04:35:06 +00004807 E = FromNode->use_end(); UI != E; ++UI) {
4808 SDUse &Use = UI.getUse();
4809 if (Use.getResNo() == FromResNo) {
4810 UseMemo Memo = { *UI, i, &Use };
Dan Gohman39946102009-01-25 16:29:12 +00004811 Uses.push_back(Memo);
4812 }
Dan Gohmane7852d02009-01-26 04:35:06 +00004813 }
Dan Gohman39946102009-01-25 16:29:12 +00004814 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004815
Dan Gohmane7852d02009-01-26 04:35:06 +00004816 // Sort the uses, so that all the uses from a given User are together.
4817 std::sort(Uses.begin(), Uses.end());
4818
Dan Gohman39946102009-01-25 16:29:12 +00004819 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
4820 UseIndex != UseIndexEnd; ) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004821 // We know that this user uses some value of From. If it is the right
4822 // value, update it.
Dan Gohman39946102009-01-25 16:29:12 +00004823 SDNode *User = Uses[UseIndex].User;
4824
4825 // This node is about to morph, remove its old self from the CSE maps.
Dan Gohmane8be6c62008-07-17 19:10:17 +00004826 RemoveNodeFromCSEMaps(User);
Dan Gohman39946102009-01-25 16:29:12 +00004827
Dan Gohmane7852d02009-01-26 04:35:06 +00004828 // The Uses array is sorted, so all the uses for a given User
4829 // are next to each other in the list.
Dan Gohman39946102009-01-25 16:29:12 +00004830 // To help reduce the number of CSE recomputations, process all
4831 // the uses of this user that we can find this way.
4832 do {
4833 unsigned i = Uses[UseIndex].Index;
Dan Gohmane7852d02009-01-26 04:35:06 +00004834 SDUse &Use = *Uses[UseIndex].Use;
Dan Gohman39946102009-01-25 16:29:12 +00004835 ++UseIndex;
4836
Dan Gohmane7852d02009-01-26 04:35:06 +00004837 Use.set(To[i]);
Dan Gohman39946102009-01-25 16:29:12 +00004838 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
4839
Dan Gohmane8be6c62008-07-17 19:10:17 +00004840 // Now that we have modified User, add it back to the CSE maps. If it
4841 // already exists there, recursively merge the results together.
Dan Gohman39946102009-01-25 16:29:12 +00004842 AddModifiedNodeToCSEMaps(User, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004843 }
4844}
4845
Evan Chenge6f35d82006-08-01 08:20:41 +00004846/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004847/// based on their topological order. It returns the maximum id and a vector
4848/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00004849unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00004850
Dan Gohmanf06c8352008-09-30 18:30:35 +00004851 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004852
Dan Gohmanf06c8352008-09-30 18:30:35 +00004853 // SortedPos tracks the progress of the algorithm. Nodes before it are
4854 // sorted, nodes after it are unsorted. When the algorithm completes
4855 // it is at the end of the list.
4856 allnodes_iterator SortedPos = allnodes_begin();
4857
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004858 // Visit all the nodes. Move nodes with no operands to the front of
4859 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00004860 // operand count. Before we do this, the Node Id fields of the nodes
4861 // may contain arbitrary values. After, the Node Id fields for nodes
4862 // before SortedPos will contain the topological sort index, and the
4863 // Node Id fields for nodes At SortedPos and after will contain the
4864 // count of outstanding operands.
4865 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
4866 SDNode *N = I++;
4867 unsigned Degree = N->getNumOperands();
4868 if (Degree == 0) {
4869 // A node with no uses, add it to the result array immediately.
4870 N->setNodeId(DAGSize++);
4871 allnodes_iterator Q = N;
4872 if (Q != SortedPos)
4873 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
4874 ++SortedPos;
4875 } else {
4876 // Temporarily use the Node Id as scratch space for the degree count.
4877 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004878 }
4879 }
4880
Dan Gohmanf06c8352008-09-30 18:30:35 +00004881 // Visit all the nodes. As we iterate, moves nodes into sorted order,
4882 // such that by the time the end is reached all nodes will be sorted.
4883 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
4884 SDNode *N = I;
4885 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4886 UI != UE; ++UI) {
4887 SDNode *P = *UI;
4888 unsigned Degree = P->getNodeId();
4889 --Degree;
4890 if (Degree == 0) {
4891 // All of P's operands are sorted, so P may sorted now.
4892 P->setNodeId(DAGSize++);
4893 if (P != SortedPos)
4894 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
4895 ++SortedPos;
4896 } else {
4897 // Update P's outstanding operand count.
4898 P->setNodeId(Degree);
4899 }
4900 }
4901 }
4902
4903 assert(SortedPos == AllNodes.end() &&
4904 "Topological sort incomplete!");
4905 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
4906 "First node in topological sort is not the entry token!");
4907 assert(AllNodes.front().getNodeId() == 0 &&
4908 "First node in topological sort has non-zero id!");
4909 assert(AllNodes.front().getNumOperands() == 0 &&
4910 "First node in topological sort has operands!");
4911 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
4912 "Last node in topologic sort has unexpected id!");
4913 assert(AllNodes.back().use_empty() &&
4914 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004915 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00004916 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00004917}
4918
4919
Evan Cheng091cba12006-07-27 06:39:06 +00004920
Jim Laskey58b968b2005-08-17 20:08:02 +00004921//===----------------------------------------------------------------------===//
4922// SDNode Class
4923//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004924
Chris Lattner48b85922007-02-04 02:41:42 +00004925HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004926 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004927}
4928
Chris Lattner505e9822009-06-26 21:14:05 +00004929GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, const GlobalValue *GA,
Chris Lattner2a4ed822009-06-25 21:21:14 +00004930 MVT VT, int64_t o, unsigned char TF)
Chris Lattner505e9822009-06-26 21:14:05 +00004931 : SDNode(Opc, DebugLoc::getUnknownLoc(), getSDVTList(VT)),
Chris Lattner2a4ed822009-06-25 21:21:14 +00004932 Offset(o), TargetFlags(TF) {
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004933 TheGlobal = const_cast<GlobalValue*>(GA);
4934}
Chris Lattner48b85922007-02-04 02:41:42 +00004935
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004936MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, MVT memvt,
4937 const Value *srcValue, int SVO,
4938 unsigned alignment, bool vol)
Dan Gohmana7ce7412009-02-03 00:08:45 +00004939 : SDNode(Opc, dl, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
4940 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004941 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4942 assert(getAlignment() == alignment && "Alignment representation error!");
4943 assert(isVolatile() == vol && "Volatile representation error!");
4944}
4945
Scott Michelfdc40a02009-02-17 22:15:04 +00004946MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004947 const SDValue *Ops,
4948 unsigned NumOps, MVT memvt, const Value *srcValue,
4949 int SVO, unsigned alignment, bool vol)
4950 : SDNode(Opc, dl, VTs, Ops, NumOps),
Dan Gohmana7ce7412009-02-03 00:08:45 +00004951 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO) {
4952 SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, vol, alignment);
Dale Johannesen3edb43e2009-01-28 21:18:29 +00004953 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4954 assert(getAlignment() == alignment && "Alignment representation error!");
4955 assert(isVolatile() == vol && "Volatile representation error!");
4956}
4957
Dan Gohman36b5c132008-04-07 19:35:22 +00004958/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004959/// reference performed by this memory reference.
4960MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00004961 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00004962 if (isa<LoadSDNode>(this))
4963 Flags = MachineMemOperand::MOLoad;
4964 else if (isa<StoreSDNode>(this))
4965 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00004966 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00004967 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4968 }
Mon P Wangc4d10212008-10-17 18:22:58 +00004969 else {
4970 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
4971 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
4972 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
4973 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
4974 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00004975
4976 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004977 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
Scott Michelfdc40a02009-02-17 22:15:04 +00004978
Dan Gohman1ea58a52008-07-09 22:08:04 +00004979 // Check if the memory reference references a frame index
Scott Michelfdc40a02009-02-17 22:15:04 +00004980 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004981 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004982 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004983 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4984 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004985 else
4986 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4987 Size, getAlignment());
4988}
4989
Jim Laskey583bd472006-10-27 23:46:08 +00004990/// Profile - Gather unique data for the node.
4991///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004992void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004993 AddNodeIDNode(ID, this);
4994}
4995
Owen Andersonb4459082009-06-25 17:09:00 +00004996static ManagedStatic<std::set<MVT, MVT::compareRawBits> > EVTs;
4997static MVT VTs[MVT::LAST_VALUETYPE];
4998static ManagedStatic<sys::SmartMutex<true> > VTMutex;
4999
Chris Lattnera3255112005-11-08 23:30:28 +00005000/// getValueTypeList - Return a pointer to the specified value type.
5001///
Duncan Sands83ec4b62008-06-06 12:08:01 +00005002const MVT *SDNode::getValueTypeList(MVT VT) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +00005003 sys::SmartScopedLock<true> Lock(*VTMutex);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005004 if (VT.isExtended()) {
Owen Andersonb4459082009-06-25 17:09:00 +00005005 return &(*EVTs->insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00005006 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005007 VTs[VT.getSimpleVT()] = VT;
5008 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00005009 }
Chris Lattnera3255112005-11-08 23:30:28 +00005010}
Duncan Sandsaf47b112007-10-16 09:56:48 +00005011
Chris Lattner5c884562005-01-12 18:37:47 +00005012/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
5013/// indicated value. This method ignores uses of other values defined by this
5014/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00005015bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00005016 assert(Value < getNumValues() && "Bad value!");
5017
Roman Levensteindc1adac2008-04-07 10:06:32 +00005018 // TODO: Only iterate over uses of a given value of the node
5019 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Dan Gohmane7852d02009-01-26 04:35:06 +00005020 if (UI.getUse().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00005021 if (NUses == 0)
5022 return false;
5023 --NUses;
5024 }
Chris Lattner5c884562005-01-12 18:37:47 +00005025 }
5026
5027 // Found exactly the right number of uses?
5028 return NUses == 0;
5029}
5030
5031
Evan Cheng33d55952007-08-02 05:29:38 +00005032/// hasAnyUseOfValue - Return true if there are any use of the indicated
5033/// value. This method ignores uses of other values defined by this operation.
5034bool SDNode::hasAnyUseOfValue(unsigned Value) const {
5035 assert(Value < getNumValues() && "Bad value!");
5036
Dan Gohman1373c1c2008-07-09 22:39:01 +00005037 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Dan Gohmane7852d02009-01-26 04:35:06 +00005038 if (UI.getUse().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00005039 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00005040
5041 return false;
5042}
5043
5044
Dan Gohman2a629952008-07-27 18:06:42 +00005045/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00005046///
Dan Gohman2a629952008-07-27 18:06:42 +00005047bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00005048 bool Seen = false;
5049 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00005050 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00005051 if (User == this)
5052 Seen = true;
5053 else
5054 return false;
5055 }
5056
5057 return Seen;
5058}
5059
Evan Chenge6e97e62006-11-03 07:31:32 +00005060/// isOperand - Return true if this node is an operand of N.
5061///
Dan Gohman475871a2008-07-27 21:46:04 +00005062bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00005063 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
5064 if (*this == N->getOperand(i))
5065 return true;
5066 return false;
5067}
5068
Evan Cheng917be682008-03-04 00:41:45 +00005069bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005070 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Dan Gohmane7852d02009-01-26 04:35:06 +00005071 if (this == N->OperandList[i].getNode())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00005072 return true;
5073 return false;
5074}
Evan Cheng4ee62112006-02-05 06:29:23 +00005075
Chris Lattner572dee72008-01-16 05:49:24 +00005076/// reachesChainWithoutSideEffects - Return true if this operand (which must
Scott Michelfdc40a02009-02-17 22:15:04 +00005077/// be a chain) reaches the specified operand without crossing any
Chris Lattner572dee72008-01-16 05:49:24 +00005078/// side-effecting instructions. In practice, this looks through token
5079/// factors and non-volatile loads. In order to remain efficient, this only
5080/// looks a couple of nodes in, it does not do an exhaustive search.
Scott Michelfdc40a02009-02-17 22:15:04 +00005081bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00005082 unsigned Depth) const {
5083 if (*this == Dest) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00005084
Chris Lattner572dee72008-01-16 05:49:24 +00005085 // Don't search too deeply, we just want to be able to see through
5086 // TokenFactor's etc.
5087 if (Depth == 0) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005088
Chris Lattner572dee72008-01-16 05:49:24 +00005089 // If this is a token factor, all inputs to the TF happen in parallel. If any
5090 // of the operands of the TF reach dest, then we can do the xform.
5091 if (getOpcode() == ISD::TokenFactor) {
5092 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5093 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
5094 return true;
5095 return false;
5096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005097
Chris Lattner572dee72008-01-16 05:49:24 +00005098 // Loads don't have side effects, look through them.
5099 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
5100 if (!Ld->isVolatile())
5101 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
5102 }
5103 return false;
5104}
5105
5106
Evan Chengc5fc57d2006-11-03 03:05:24 +00005107static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005108 SmallPtrSet<SDNode *, 32> &Visited) {
5109 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00005110 return;
5111
5112 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005113 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00005114 if (Op == P) {
5115 found = true;
5116 return;
5117 }
5118 findPredecessor(Op, P, found, Visited);
5119 }
5120}
5121
Evan Cheng917be682008-03-04 00:41:45 +00005122/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00005123/// is either an operand of N or it can be reached by recursively traversing
5124/// up the operands.
5125/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00005126bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00005127 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00005128 bool found = false;
5129 findPredecessor(N, this, found, Visited);
5130 return found;
5131}
5132
Evan Chengc5484282006-10-04 00:56:09 +00005133uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
5134 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005135 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00005136}
5137
Reid Spencer577cc322007-04-01 07:32:19 +00005138std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005139 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005140 default:
5141 if (getOpcode() < ISD::BUILTIN_OP_END)
5142 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00005143 if (isMachineOpcode()) {
5144 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005145 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00005146 if (getMachineOpcode() < TII->getNumOpcodes())
5147 return TII->get(getMachineOpcode()).getName();
5148 return "<<Unknown Machine Node>>";
5149 }
5150 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00005151 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00005152 const char *Name = TLI.getTargetNodeName(getOpcode());
5153 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00005154 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005155 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00005156 return "<<Unknown Node>>";
Scott Michelfdc40a02009-02-17 22:15:04 +00005157
Dan Gohmane8be6c62008-07-17 19:10:17 +00005158#ifndef NDEBUG
5159 case ISD::DELETED_NODE:
5160 return "<<Deleted Node!>>";
5161#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00005162 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00005163 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00005164 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
5165 case ISD::ATOMIC_SWAP: return "AtomicSwap";
5166 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
5167 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
5168 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
5169 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
5170 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
5171 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
5172 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
5173 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
5174 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
5175 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00005176 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00005177 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005178 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00005179 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005180 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00005181 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00005182 case ISD::AssertSext: return "AssertSext";
5183 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005184
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005185 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005186 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00005187 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005188
5189 case ISD::Constant: return "Constant";
5190 case ISD::ConstantFP: return "ConstantFP";
5191 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005192 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005193 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005194 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005195 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005196 case ISD::RETURNADDR: return "RETURNADDR";
5197 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005198 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005199 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005200 case ISD::EHSELECTION: return "EHSELECTION";
5201 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005202 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005203 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005204 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005205 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005206 return Intrinsic::getName((Intrinsic::ID)IID);
5207 }
5208 case ISD::INTRINSIC_VOID:
5209 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005210 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005211 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005212 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005213
Chris Lattnerb2827b02006-03-19 00:52:58 +00005214 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005215 case ISD::TargetConstant: return "TargetConstant";
5216 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005217 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005218 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005219 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005220 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005221 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005222 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005223
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005224 case ISD::CopyToReg: return "CopyToReg";
5225 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005226 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005227 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005228 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005229 case ISD::DBG_LABEL: return "dbg_label";
5230 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00005231 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005232 case ISD::HANDLENODE: return "handlenode";
Scott Michelfdc40a02009-02-17 22:15:04 +00005233
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005234 // Unary operators
5235 case ISD::FABS: return "fabs";
5236 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005237 case ISD::FSQRT: return "fsqrt";
5238 case ISD::FSIN: return "fsin";
5239 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005240 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005241 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005242 case ISD::FTRUNC: return "ftrunc";
5243 case ISD::FFLOOR: return "ffloor";
5244 case ISD::FCEIL: return "fceil";
5245 case ISD::FRINT: return "frint";
5246 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005247
5248 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005249 case ISD::ADD: return "add";
5250 case ISD::SUB: return "sub";
5251 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005252 case ISD::MULHU: return "mulhu";
5253 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005254 case ISD::SDIV: return "sdiv";
5255 case ISD::UDIV: return "udiv";
5256 case ISD::SREM: return "srem";
5257 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005258 case ISD::SMUL_LOHI: return "smul_lohi";
5259 case ISD::UMUL_LOHI: return "umul_lohi";
5260 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005261 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005262 case ISD::AND: return "and";
5263 case ISD::OR: return "or";
5264 case ISD::XOR: return "xor";
5265 case ISD::SHL: return "shl";
5266 case ISD::SRA: return "sra";
5267 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005268 case ISD::ROTL: return "rotl";
5269 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005270 case ISD::FADD: return "fadd";
5271 case ISD::FSUB: return "fsub";
5272 case ISD::FMUL: return "fmul";
5273 case ISD::FDIV: return "fdiv";
5274 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005275 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005276 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005277
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005278 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005279 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005280 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005281 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005282 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005283 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005284 case ISD::CONCAT_VECTORS: return "concat_vectors";
5285 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005286 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005287 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Dale Johannesen874ae252009-06-02 03:12:52 +00005288 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005289 case ISD::ADDC: return "addc";
5290 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005291 case ISD::SADDO: return "saddo";
5292 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005293 case ISD::SSUBO: return "ssubo";
5294 case ISD::USUBO: return "usubo";
5295 case ISD::SMULO: return "smulo";
5296 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005297 case ISD::SUBC: return "subc";
5298 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005299 case ISD::SHL_PARTS: return "shl_parts";
5300 case ISD::SRA_PARTS: return "sra_parts";
5301 case ISD::SRL_PARTS: return "srl_parts";
Scott Michelfdc40a02009-02-17 22:15:04 +00005302
Chris Lattner7f644642005-04-28 21:44:03 +00005303 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005304 case ISD::SIGN_EXTEND: return "sign_extend";
5305 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005306 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005307 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005308 case ISD::TRUNCATE: return "truncate";
5309 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005310 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005311 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005312 case ISD::FP_EXTEND: return "fp_extend";
5313
5314 case ISD::SINT_TO_FP: return "sint_to_fp";
5315 case ISD::UINT_TO_FP: return "uint_to_fp";
5316 case ISD::FP_TO_SINT: return "fp_to_sint";
5317 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005318 case ISD::BIT_CONVERT: return "bit_convert";
Scott Michelfdc40a02009-02-17 22:15:04 +00005319
Mon P Wang77cdf302008-11-10 20:54:11 +00005320 case ISD::CONVERT_RNDSAT: {
5321 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005322 default: llvm_unreachable("Unknown cvt code!");
Mon P Wang77cdf302008-11-10 20:54:11 +00005323 case ISD::CVT_FF: return "cvt_ff";
5324 case ISD::CVT_FS: return "cvt_fs";
5325 case ISD::CVT_FU: return "cvt_fu";
5326 case ISD::CVT_SF: return "cvt_sf";
5327 case ISD::CVT_UF: return "cvt_uf";
5328 case ISD::CVT_SS: return "cvt_ss";
5329 case ISD::CVT_SU: return "cvt_su";
5330 case ISD::CVT_US: return "cvt_us";
5331 case ISD::CVT_UU: return "cvt_uu";
5332 }
5333 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005334
5335 // Control flow instructions
5336 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005337 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005338 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005339 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005340 case ISD::BR_CC: return "br_cc";
Chris Lattnera364fa12005-05-12 23:51:40 +00005341 case ISD::CALLSEQ_START: return "callseq_start";
5342 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005343
5344 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005345 case ISD::LOAD: return "load";
5346 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005347 case ISD::VAARG: return "vaarg";
5348 case ISD::VACOPY: return "vacopy";
5349 case ISD::VAEND: return "vaend";
5350 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005351 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005352 case ISD::EXTRACT_ELEMENT: return "extract_element";
5353 case ISD::BUILD_PAIR: return "build_pair";
5354 case ISD::STACKSAVE: return "stacksave";
5355 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005356 case ISD::TRAP: return "trap";
5357
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005358 // Bit manipulation
5359 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005360 case ISD::CTPOP: return "ctpop";
5361 case ISD::CTTZ: return "cttz";
5362 case ISD::CTLZ: return "ctlz";
5363
Chris Lattner36ce6912005-11-29 06:21:05 +00005364 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005365 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005366 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005367
Duncan Sands36397f52007-07-27 12:58:54 +00005368 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005369 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005370
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005371 case ISD::CONDCODE:
5372 switch (cast<CondCodeSDNode>(this)->get()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005373 default: llvm_unreachable("Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005374 case ISD::SETOEQ: return "setoeq";
5375 case ISD::SETOGT: return "setogt";
5376 case ISD::SETOGE: return "setoge";
5377 case ISD::SETOLT: return "setolt";
5378 case ISD::SETOLE: return "setole";
5379 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005380
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005381 case ISD::SETO: return "seto";
5382 case ISD::SETUO: return "setuo";
5383 case ISD::SETUEQ: return "setue";
5384 case ISD::SETUGT: return "setugt";
5385 case ISD::SETUGE: return "setuge";
5386 case ISD::SETULT: return "setult";
5387 case ISD::SETULE: return "setule";
5388 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005389
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005390 case ISD::SETEQ: return "seteq";
5391 case ISD::SETGT: return "setgt";
5392 case ISD::SETGE: return "setge";
5393 case ISD::SETLT: return "setlt";
5394 case ISD::SETLE: return "setle";
5395 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005396 }
5397 }
5398}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005399
Evan Cheng144d8f02006-11-09 17:55:04 +00005400const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005401 switch (AM) {
5402 default:
5403 return "";
5404 case ISD::PRE_INC:
5405 return "<pre-inc>";
5406 case ISD::PRE_DEC:
5407 return "<pre-dec>";
5408 case ISD::POST_INC:
5409 return "<post-inc>";
5410 case ISD::POST_DEC:
5411 return "<post-dec>";
5412 }
5413}
5414
Duncan Sands276dcbd2008-03-21 09:14:45 +00005415std::string ISD::ArgFlagsTy::getArgFlagsString() {
5416 std::string S = "< ";
5417
5418 if (isZExt())
5419 S += "zext ";
5420 if (isSExt())
5421 S += "sext ";
5422 if (isInReg())
5423 S += "inreg ";
5424 if (isSRet())
5425 S += "sret ";
5426 if (isByVal())
5427 S += "byval ";
5428 if (isNest())
5429 S += "nest ";
5430 if (getByValAlign())
5431 S += "byval-align:" + utostr(getByValAlign()) + " ";
5432 if (getOrigAlign())
5433 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5434 if (getByValSize())
5435 S += "byval-size:" + utostr(getByValSize()) + " ";
5436 return S + ">";
5437}
5438
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005439void SDNode::dump() const { dump(0); }
5440void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005441 print(errs(), G);
5442}
5443
Stuart Hastings80d69772009-02-04 16:46:19 +00005444void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005445 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005446
5447 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005448 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005449 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005450 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005451 else
Chris Lattner944fac72008-08-23 22:23:09 +00005452 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005453 }
Chris Lattner944fac72008-08-23 22:23:09 +00005454 OS << " = " << getOperationName(G);
Stuart Hastings80d69772009-02-04 16:46:19 +00005455}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005456
Stuart Hastings80d69772009-02-04 16:46:19 +00005457void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
Evan Chengce254432007-12-11 02:08:35 +00005458 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005459 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(this);
Chris Lattner944fac72008-08-23 22:23:09 +00005460 OS << "<";
Nate Begeman9008ca62009-04-27 18:41:29 +00005461 for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
5462 int Idx = SVN->getMaskElt(i);
Chris Lattner944fac72008-08-23 22:23:09 +00005463 if (i) OS << ",";
Nate Begeman9008ca62009-04-27 18:41:29 +00005464 if (Idx < 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005465 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005466 else
Nate Begeman9008ca62009-04-27 18:41:29 +00005467 OS << Idx;
Evan Chengce254432007-12-11 02:08:35 +00005468 }
Chris Lattner944fac72008-08-23 22:23:09 +00005469 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005470 }
5471
Chris Lattnerc3aae252005-01-07 07:46:32 +00005472 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005473 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005474 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005475 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005476 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005477 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005478 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005479 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005480 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005481 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005482 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005483 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005484 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005485 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005486 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005487 OS << '<';
5488 WriteAsOperand(OS, GADN->getGlobal());
5489 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005490 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005491 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005492 else
Chris Lattner944fac72008-08-23 22:23:09 +00005493 OS << " " << offset;
Dan Gohman1944da12009-08-01 19:13:38 +00005494 if (unsigned int TF = GADN->getTargetFlags())
Chris Lattner2a4ed822009-06-25 21:21:14 +00005495 OS << " [TF=" << TF << ']';
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005496 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005497 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005498 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005499 OS << "<" << JTDN->getIndex() << ">";
Dan Gohman1944da12009-08-01 19:13:38 +00005500 if (unsigned int TF = JTDN->getTargetFlags())
Chris Lattnerf5a55462009-06-25 21:35:31 +00005501 OS << " [TF=" << TF << ']';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005502 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005503 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005504 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005505 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005506 else
Chris Lattner944fac72008-08-23 22:23:09 +00005507 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005508 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005509 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005510 else
Chris Lattner944fac72008-08-23 22:23:09 +00005511 OS << " " << offset;
Dan Gohman1944da12009-08-01 19:13:38 +00005512 if (unsigned int TF = CP->getTargetFlags())
Chris Lattnerf5a55462009-06-25 21:35:31 +00005513 OS << " [TF=" << TF << ']';
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005514 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005515 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005516 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5517 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005518 OS << LBB->getName() << " ";
5519 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005520 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005521 if (G && R->getReg() &&
5522 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005523 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005524 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005525 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005526 }
Bill Wendling056292f2008-09-16 21:48:12 +00005527 } else if (const ExternalSymbolSDNode *ES =
5528 dyn_cast<ExternalSymbolSDNode>(this)) {
5529 OS << "'" << ES->getSymbol() << "'";
Dan Gohman1944da12009-08-01 19:13:38 +00005530 if (unsigned int TF = ES->getTargetFlags())
Chris Lattner2a4ed822009-06-25 21:21:14 +00005531 OS << " [TF=" << TF << ']';
Chris Lattner2bf3c262005-05-09 04:08:27 +00005532 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5533 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005534 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005535 else
Chris Lattner944fac72008-08-23 22:23:09 +00005536 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005537 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5538 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005539 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005540 else
Chris Lattner944fac72008-08-23 22:23:09 +00005541 OS << "<null:" << M->MO.getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00005542 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005543 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005544 }
5545 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005546 const Value *SrcValue = LD->getSrcValue();
5547 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005548 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005549 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005550 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005551 else
Chris Lattner944fac72008-08-23 22:23:09 +00005552 OS << "null";
5553 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005554
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005555 bool doExt = true;
5556 switch (LD->getExtensionType()) {
5557 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005558 case ISD::EXTLOAD: OS << " <anyext "; break;
5559 case ISD::SEXTLOAD: OS << " <sext "; break;
5560 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005561 }
5562 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005563 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005564
Evan Cheng144d8f02006-11-09 17:55:04 +00005565 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005566 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005567 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005568 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005569 OS << " <volatile>";
5570 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005571 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005572 const Value *SrcValue = ST->getSrcValue();
5573 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005574 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005575 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005576 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005577 else
Chris Lattner944fac72008-08-23 22:23:09 +00005578 OS << "null";
5579 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005580
5581 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005582 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005583
5584 const char *AM = getIndexedModeName(ST->getAddressingMode());
5585 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005586 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005587 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005588 OS << " <volatile>";
5589 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005590 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5591 const Value *SrcValue = AT->getSrcValue();
5592 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005593 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005594 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005595 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005596 else
Chris Lattner944fac72008-08-23 22:23:09 +00005597 OS << "null";
5598 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005599 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005600 OS << " <volatile>";
5601 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005602 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005603}
5604
Stuart Hastings80d69772009-02-04 16:46:19 +00005605void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5606 print_types(OS, G);
5607 OS << " ";
5608 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
5609 if (i) OS << ", ";
5610 OS << (void*)getOperand(i).getNode();
5611 if (unsigned RN = getOperand(i).getResNo())
5612 OS << ":" << RN;
5613 }
5614 print_details(OS, G);
5615}
5616
Chris Lattnerde202b32005-11-09 23:47:37 +00005617static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005618 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005619 if (N->getOperand(i).getNode()->hasOneUse())
5620 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005621 else
Bill Wendling832171c2006-12-07 20:04:42 +00005622 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005623 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005624
Chris Lattnerea946cd2005-01-09 20:38:33 +00005625
Bill Wendling832171c2006-12-07 20:04:42 +00005626 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005627 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005628}
5629
Chris Lattnerc3aae252005-01-07 07:46:32 +00005630void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005631 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Scott Michelfdc40a02009-02-17 22:15:04 +00005632
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005633 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5634 I != E; ++I) {
5635 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005636 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005637 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005638 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005639
Gabor Greifba36cb52008-08-28 21:40:38 +00005640 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005641
Bill Wendling832171c2006-12-07 20:04:42 +00005642 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005643}
5644
Stuart Hastings80d69772009-02-04 16:46:19 +00005645void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
5646 print_types(OS, G);
5647 print_details(OS, G);
5648}
5649
5650typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
Stuart Hastings4caa0422009-02-04 20:30:10 +00005651static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
Bill Wendling51b16f42009-05-30 01:09:53 +00005652 const SelectionDAG *G, VisitedSDNodeSet &once) {
5653 if (!once.insert(N)) // If we've been here before, return now.
Stuart Hastings80d69772009-02-04 16:46:19 +00005654 return;
5655 // Dump the current SDNode, but don't end the line yet.
5656 OS << std::string(indent, ' ');
5657 N->printr(OS, G);
5658 // Having printed this SDNode, walk the children:
5659 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5660 const SDNode *child = N->getOperand(i).getNode();
5661 if (i) OS << ",";
5662 OS << " ";
5663 if (child->getNumOperands() == 0) {
5664 // This child has no grandchildren; print it inline right here.
5665 child->printr(OS, G);
5666 once.insert(child);
Bill Wendling51b16f42009-05-30 01:09:53 +00005667 } else { // Just the address. FIXME: also print the child's opcode
Stuart Hastings80d69772009-02-04 16:46:19 +00005668 OS << (void*)child;
5669 if (unsigned RN = N->getOperand(i).getResNo())
Bill Wendling51b16f42009-05-30 01:09:53 +00005670 OS << ":" << RN;
Stuart Hastings80d69772009-02-04 16:46:19 +00005671 }
5672 }
5673 OS << "\n";
5674 // Dump children that have grandchildren on their own line(s).
5675 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5676 const SDNode *child = N->getOperand(i).getNode();
5677 DumpNodesr(OS, child, indent+2, G, once);
5678 }
5679}
5680
5681void SDNode::dumpr() const {
5682 VisitedSDNodeSet once;
5683 DumpNodesr(errs(), this, 0, 0, once);
Stuart Hastings80d69772009-02-04 16:46:19 +00005684}
5685
Sanjiv Guptaa3518a12009-04-29 04:43:24 +00005686
5687// getAddressSpace - Return the address space this GlobalAddress belongs to.
5688unsigned GlobalAddressSDNode::getAddressSpace() const {
5689 return getGlobal()->getType()->getAddressSpace();
5690}
5691
5692
Evan Chengd6594ae2006-09-12 21:00:35 +00005693const Type *ConstantPoolSDNode::getType() const {
5694 if (isMachineConstantPoolEntry())
5695 return Val.MachineCPVal->getType();
5696 return Val.ConstVal->getType();
5697}
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005698
Bob Wilson24e338e2009-03-02 23:24:16 +00005699bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
5700 APInt &SplatUndef,
5701 unsigned &SplatBitSize,
5702 bool &HasAnyUndefs,
5703 unsigned MinSplatBits) {
5704 MVT VT = getValueType(0);
5705 assert(VT.isVector() && "Expected a vector type");
5706 unsigned sz = VT.getSizeInBits();
5707 if (MinSplatBits > sz)
5708 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005709
Bob Wilson24e338e2009-03-02 23:24:16 +00005710 SplatValue = APInt(sz, 0);
5711 SplatUndef = APInt(sz, 0);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005712
Bob Wilson24e338e2009-03-02 23:24:16 +00005713 // Get the bits. Bits with undefined values (when the corresponding element
5714 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
5715 // in SplatValue. If any of the values are not constant, give up and return
5716 // false.
5717 unsigned int nOps = getNumOperands();
5718 assert(nOps > 0 && "isConstantSplat has 0-size build vector");
5719 unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
5720 for (unsigned i = 0; i < nOps; ++i) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005721 SDValue OpVal = getOperand(i);
Bob Wilson24e338e2009-03-02 23:24:16 +00005722 unsigned BitPos = i * EltBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005723
Bob Wilson24e338e2009-03-02 23:24:16 +00005724 if (OpVal.getOpcode() == ISD::UNDEF)
5725 SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos +EltBitSize);
5726 else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
Bob Wilsonb1303d02009-04-13 22:05:19 +00005727 SplatValue |= (APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
5728 zextOrTrunc(sz) << BitPos);
Bob Wilson24e338e2009-03-02 23:24:16 +00005729 else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
Bob Wilsond344b882009-03-04 17:47:01 +00005730 SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
Bob Wilson24e338e2009-03-02 23:24:16 +00005731 else
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005732 return false;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005733 }
5734
Bob Wilson24e338e2009-03-02 23:24:16 +00005735 // The build_vector is all constants or undefs. Find the smallest element
5736 // size that splats the vector.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005737
Bob Wilson24e338e2009-03-02 23:24:16 +00005738 HasAnyUndefs = (SplatUndef != 0);
5739 while (sz > 8) {
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005740
Bob Wilson24e338e2009-03-02 23:24:16 +00005741 unsigned HalfSize = sz / 2;
5742 APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
5743 APInt LowValue = APInt(SplatValue).trunc(HalfSize);
5744 APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
5745 APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005746
Bob Wilson24e338e2009-03-02 23:24:16 +00005747 // If the two halves do not match (ignoring undef bits), stop here.
5748 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
5749 MinSplatBits > HalfSize)
5750 break;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005751
Bob Wilson24e338e2009-03-02 23:24:16 +00005752 SplatValue = HighValue | LowValue;
5753 SplatUndef = HighUndef & LowUndef;
5754
5755 sz = HalfSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005756 }
5757
Bob Wilson24e338e2009-03-02 23:24:16 +00005758 SplatBitSize = sz;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005759 return true;
5760}
Nate Begeman9008ca62009-04-27 18:41:29 +00005761
5762bool ShuffleVectorSDNode::isSplatMask(const int *Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00005763 // Find the first non-undef value in the shuffle mask.
5764 unsigned i, e;
5765 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
5766 /* search */;
5767
Nate Begemana6415752009-04-29 18:13:31 +00005768 assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
Nate Begeman5a5ca152009-04-29 05:20:52 +00005769
5770 // Make sure all remaining elements are either undef or the same as the first
5771 // non-undef value.
5772 for (int Idx = Mask[i]; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005773 if (Mask[i] >= 0 && Mask[i] != Idx)
5774 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00005775 return true;
5776}