blob: e498647c447c5effe90838483abf2fa047884fd6 [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"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetMachine.h"
Bill Wendling6f287b22008-09-30 21:22:07 +000032#include "llvm/Support/CommandLine.h"
Chris Lattner944fac72008-08-23 22:23:09 +000033#include "llvm/Support/MathExtras.h"
34#include "llvm/Support/raw_ostream.h"
Chris Lattner012f2412006-02-17 21:58:01 +000035#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000036#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsaf47b112007-10-16 09:56:48 +000037#include "llvm/ADT/SmallSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000038#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000039#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000040#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000041#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000042using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000043
Chris Lattner0b3e5252006-08-15 19:11:05 +000044/// makeVTList - Return an instance of the SDVTList struct initialized with the
45/// specified members.
Duncan Sands83ec4b62008-06-06 12:08:01 +000046static SDVTList makeVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner0b3e5252006-08-15 19:11:05 +000047 SDVTList Res = {VTs, NumVTs};
48 return Res;
49}
50
Duncan Sands83ec4b62008-06-06 12:08:01 +000051static const fltSemantics *MVTToAPFloatSemantics(MVT VT) {
52 switch (VT.getSimpleVT()) {
Chris Lattnerec4a5672008-03-05 06:48:13 +000053 default: assert(0 && "Unknown FP format");
54 case MVT::f32: return &APFloat::IEEEsingle;
55 case MVT::f64: return &APFloat::IEEEdouble;
56 case MVT::f80: return &APFloat::x87DoubleExtended;
57 case MVT::f128: return &APFloat::IEEEquad;
58 case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
59 }
60}
61
Chris Lattnerf8dc0612008-02-03 06:49:24 +000062SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
63
Jim Laskey58b968b2005-08-17 20:08:02 +000064//===----------------------------------------------------------------------===//
65// ConstantFPSDNode Class
66//===----------------------------------------------------------------------===//
67
68/// isExactlyValue - We don't rely on operator== working on double values, as
69/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
70/// As such, this method can be used to do an exact bit-for-bit comparison of
71/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000072bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dan Gohman4fbd7962008-09-12 18:08:03 +000073 return getValueAPF().bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000074}
75
Duncan Sands83ec4b62008-06-06 12:08:01 +000076bool ConstantFPSDNode::isValueValidForType(MVT VT,
Dale Johannesenf04afdb2007-08-30 00:23:21 +000077 const APFloat& Val) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000078 assert(VT.isFloatingPoint() && "Can only convert between FP types");
Chris Lattnerec4a5672008-03-05 06:48:13 +000079
Dale Johannesen9dd2ce42008-04-20 18:23:46 +000080 // PPC long double cannot be converted to any other type.
81 if (VT == MVT::ppcf128 ||
82 &Val.getSemantics() == &APFloat::PPCDoubleDouble)
Chris Lattnerec4a5672008-03-05 06:48:13 +000083 return false;
84
Dale Johannesenf04afdb2007-08-30 00:23:21 +000085 // convert modifies in place, so make a copy.
86 APFloat Val2 = APFloat(Val);
Dale Johannesen23a98552008-10-09 23:00:39 +000087 bool losesInfo;
88 (void) Val2.convert(*MVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
89 &losesInfo);
90 return !losesInfo;
Dale Johannesenf04afdb2007-08-30 00:23:21 +000091}
92
Jim Laskey58b968b2005-08-17 20:08:02 +000093//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000094// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000095//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000096
Evan Chenga8df1662006-03-27 06:58:47 +000097/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000098/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000099bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000100 // Look through a bit convert.
101 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000102 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000103
Evan Chenga8df1662006-03-27 06:58:47 +0000104 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +0000105
106 unsigned i = 0, e = N->getNumOperands();
107
108 // Skip over all of the undef values.
109 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
110 ++i;
111
112 // Do not accept an all-undef vector.
113 if (i == e) return false;
114
115 // Do not accept build_vectors that aren't all constants or which have non-~0
116 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000118 if (isa<ConstantSDNode>(NotZero)) {
119 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
120 return false;
121 } else if (isa<ConstantFPSDNode>(NotZero)) {
Dan Gohman6c231502008-02-29 01:47:35 +0000122 if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
Dale Johannesen23a98552008-10-09 23:00:39 +0000123 bitcastToAPInt().isAllOnesValue())
Dan Gohman6c231502008-02-29 01:47:35 +0000124 return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000125 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000126 return false;
127
128 // Okay, we have at least one ~0 value, check to see if the rest match or are
129 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000130 for (++i; i != e; ++i)
131 if (N->getOperand(i) != NotZero &&
132 N->getOperand(i).getOpcode() != ISD::UNDEF)
133 return false;
134 return true;
135}
136
137
Evan Cheng4a147842006-03-26 09:50:58 +0000138/// isBuildVectorAllZeros - Return true if the specified node is a
139/// BUILD_VECTOR where all of the elements are 0 or undef.
140bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000141 // Look through a bit convert.
142 if (N->getOpcode() == ISD::BIT_CONVERT)
Gabor Greifba36cb52008-08-28 21:40:38 +0000143 N = N->getOperand(0).getNode();
Chris Lattner547a16f2006-04-15 23:38:00 +0000144
Evan Cheng4a147842006-03-26 09:50:58 +0000145 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000146
147 unsigned i = 0, e = N->getNumOperands();
148
149 // Skip over all of the undef values.
150 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
151 ++i;
152
153 // Do not accept an all-undef vector.
154 if (i == e) return false;
155
156 // Do not accept build_vectors that aren't all constants or which have non-~0
157 // elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000158 SDValue Zero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000159 if (isa<ConstantSDNode>(Zero)) {
160 if (!cast<ConstantSDNode>(Zero)->isNullValue())
161 return false;
162 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000163 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000164 return false;
165 } else
166 return false;
167
168 // Okay, we have at least one ~0 value, check to see if the rest match or are
169 // undefs.
170 for (++i; i != e; ++i)
171 if (N->getOperand(i) != Zero &&
172 N->getOperand(i).getOpcode() != ISD::UNDEF)
173 return false;
174 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000175}
176
Evan Chengefec7512008-02-18 23:04:32 +0000177/// isScalarToVector - Return true if the specified node is a
178/// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
179/// element is not an undef.
180bool ISD::isScalarToVector(const SDNode *N) {
181 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
182 return true;
183
184 if (N->getOpcode() != ISD::BUILD_VECTOR)
185 return false;
186 if (N->getOperand(0).getOpcode() == ISD::UNDEF)
187 return false;
188 unsigned NumElems = N->getNumOperands();
189 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000190 SDValue V = N->getOperand(i);
Evan Chengefec7512008-02-18 23:04:32 +0000191 if (V.getOpcode() != ISD::UNDEF)
192 return false;
193 }
194 return true;
195}
196
197
Evan Chengbb81d972008-01-31 09:59:15 +0000198/// isDebugLabel - Return true if the specified node represents a debug
Dan Gohman44066042008-07-01 00:05:16 +0000199/// label (i.e. ISD::DBG_LABEL or TargetInstrInfo::DBG_LABEL node).
Evan Chengbb81d972008-01-31 09:59:15 +0000200bool ISD::isDebugLabel(const SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue Zero;
Dan Gohman44066042008-07-01 00:05:16 +0000202 if (N->getOpcode() == ISD::DBG_LABEL)
203 return true;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000204 if (N->isMachineOpcode() &&
205 N->getMachineOpcode() == TargetInstrInfo::DBG_LABEL)
Dan Gohman44066042008-07-01 00:05:16 +0000206 return true;
207 return false;
Evan Chengbb81d972008-01-31 09:59:15 +0000208}
209
Chris Lattnerc3aae252005-01-07 07:46:32 +0000210/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
211/// when given the operation for (X op Y).
212ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
213 // To perform this operation, we just need to swap the L and G bits of the
214 // operation.
215 unsigned OldL = (Operation >> 2) & 1;
216 unsigned OldG = (Operation >> 1) & 1;
217 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
218 (OldL << 1) | // New G bit
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000219 (OldG << 2)); // New L bit.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000220}
221
222/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
223/// 'op' is a valid SetCC operation.
224ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
225 unsigned Operation = Op;
226 if (isInteger)
227 Operation ^= 7; // Flip L, G, E bits, but not U.
228 else
229 Operation ^= 15; // Flip all of the condition bits.
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000230
Chris Lattnerc3aae252005-01-07 07:46:32 +0000231 if (Operation > ISD::SETTRUE2)
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000232 Operation &= ~8; // Don't let N and U bits get set.
233
Chris Lattnerc3aae252005-01-07 07:46:32 +0000234 return ISD::CondCode(Operation);
235}
236
237
238/// isSignedOp - For an integer comparison, return 1 if the comparison is a
239/// signed operation and 2 if the result is an unsigned comparison. Return zero
240/// if the operation does not depend on the sign of the input (setne and seteq).
241static int isSignedOp(ISD::CondCode Opcode) {
242 switch (Opcode) {
243 default: assert(0 && "Illegal integer setcc operation!");
244 case ISD::SETEQ:
245 case ISD::SETNE: return 0;
246 case ISD::SETLT:
247 case ISD::SETLE:
248 case ISD::SETGT:
249 case ISD::SETGE: return 1;
250 case ISD::SETULT:
251 case ISD::SETULE:
252 case ISD::SETUGT:
253 case ISD::SETUGE: return 2;
254 }
255}
256
257/// getSetCCOrOperation - Return the result of a logical OR between different
258/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
259/// returns SETCC_INVALID if it is not possible to represent the resultant
260/// comparison.
261ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
262 bool isInteger) {
263 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
264 // Cannot fold a signed integer setcc with an unsigned integer setcc.
265 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattnerc3aae252005-01-07 07:46:32 +0000267 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268
Chris Lattnerc3aae252005-01-07 07:46:32 +0000269 // If the N and U bits get set then the resultant comparison DOES suddenly
270 // care about orderedness, and is true when ordered.
271 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000272 Op &= ~16; // Clear the U bit if the N bit is set.
273
274 // Canonicalize illegal integer setcc's.
275 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
276 Op = ISD::SETNE;
277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278 return ISD::CondCode(Op);
279}
280
281/// getSetCCAndOperation - Return the result of a logical AND between different
282/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
283/// function returns zero if it is not possible to represent the resultant
284/// comparison.
285ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
286 bool isInteger) {
287 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
288 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000289 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000290
291 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000292 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
293
294 // Canonicalize illegal integer setcc's.
295 if (isInteger) {
296 switch (Result) {
297 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000298 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
Dan Gohmand64a78c2008-05-14 18:17:09 +0000299 case ISD::SETOEQ: // SETEQ & SETU[LG]E
Chris Lattner883a52d2006-06-28 18:29:47 +0000300 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
301 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
302 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000303 }
304 }
305
306 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000307}
308
Chris Lattnerb48da392005-01-23 04:39:44 +0000309const TargetMachine &SelectionDAG::getTarget() const {
Dan Gohman6448d912008-09-04 15:39:15 +0000310 return MF->getTarget();
Chris Lattnerb48da392005-01-23 04:39:44 +0000311}
312
Jim Laskey58b968b2005-08-17 20:08:02 +0000313//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000314// SDNode Profile Support
315//===----------------------------------------------------------------------===//
316
Jim Laskeydef69b92006-10-27 23:52:51 +0000317/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
318///
Jim Laskey583bd472006-10-27 23:46:08 +0000319static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
320 ID.AddInteger(OpC);
321}
322
323/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
324/// solely with their pointer.
Dan Gohman844731a2008-05-13 00:00:25 +0000325static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
Jim Laskey583bd472006-10-27 23:46:08 +0000326 ID.AddPointer(VTList.VTs);
327}
328
Jim Laskeydef69b92006-10-27 23:52:51 +0000329/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
330///
Jim Laskey583bd472006-10-27 23:46:08 +0000331static void AddNodeIDOperands(FoldingSetNodeID &ID,
Dan Gohman475871a2008-07-27 21:46:04 +0000332 const SDValue *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000333 for (; NumOps; --NumOps, ++Ops) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000334 ID.AddPointer(Ops->getNode());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000335 ID.AddInteger(Ops->getResNo());
Chris Lattner63e3f142007-02-04 07:28:00 +0000336 }
Jim Laskey583bd472006-10-27 23:46:08 +0000337}
338
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000339/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
340///
341static void AddNodeIDOperands(FoldingSetNodeID &ID,
342 const SDUse *Ops, unsigned NumOps) {
343 for (; NumOps; --NumOps, ++Ops) {
Dan Gohman475871a2008-07-27 21:46:04 +0000344 ID.AddPointer(Ops->getVal());
Gabor Greif99a6cb92008-08-26 22:36:50 +0000345 ID.AddInteger(Ops->getSDValue().getResNo());
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000346 }
347}
348
Jim Laskey583bd472006-10-27 23:46:08 +0000349static void AddNodeIDNode(FoldingSetNodeID &ID,
350 unsigned short OpC, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +0000351 const SDValue *OpList, unsigned N) {
Jim Laskey583bd472006-10-27 23:46:08 +0000352 AddNodeIDOpcode(ID, OpC);
353 AddNodeIDValueTypes(ID, VTList);
354 AddNodeIDOperands(ID, OpList, N);
355}
356
Duncan Sands0dc40452008-10-27 15:30:53 +0000357/// AddNodeIDCustom - If this is an SDNode with special info, add this info to
358/// the NodeID data.
359static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000360 switch (N->getOpcode()) {
361 default: break; // Normal nodes don't need extra info.
Duncan Sands276dcbd2008-03-21 09:14:45 +0000362 case ISD::ARG_FLAGS:
363 ID.AddInteger(cast<ARG_FLAGSSDNode>(N)->getArgFlags().getRawBits());
364 break;
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000365 case ISD::TargetConstant:
366 case ISD::Constant:
Dan Gohman4fbd7962008-09-12 18:08:03 +0000367 ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000368 break;
369 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000370 case ISD::ConstantFP: {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000371 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000372 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000373 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000375 case ISD::GlobalAddress:
376 case ISD::TargetGlobalTLSAddress:
377 case ISD::GlobalTLSAddress: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000378 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000379 ID.AddPointer(GA->getGlobal());
380 ID.AddInteger(GA->getOffset());
381 break;
382 }
383 case ISD::BasicBlock:
384 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
385 break;
386 case ISD::Register:
387 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
388 break;
Dan Gohman7f460202008-06-30 20:59:49 +0000389 case ISD::DBG_STOPPOINT: {
390 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(N);
391 ID.AddInteger(DSP->getLine());
392 ID.AddInteger(DSP->getColumn());
393 ID.AddPointer(DSP->getCompileUnit());
394 break;
395 }
Dan Gohman69de1932008-02-06 22:27:42 +0000396 case ISD::SRCVALUE:
397 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
398 break;
399 case ISD::MEMOPERAND: {
Dan Gohman36b5c132008-04-07 19:35:22 +0000400 const MachineMemOperand &MO = cast<MemOperandSDNode>(N)->MO;
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000401 MO.Profile(ID);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000402 break;
403 }
404 case ISD::FrameIndex:
405 case ISD::TargetFrameIndex:
406 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
407 break;
408 case ISD::JumpTable:
409 case ISD::TargetJumpTable:
410 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
411 break;
412 case ISD::ConstantPool:
413 case ISD::TargetConstantPool: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000414 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000415 ID.AddInteger(CP->getAlignment());
416 ID.AddInteger(CP->getOffset());
417 if (CP->isMachineConstantPoolEntry())
418 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
419 else
420 ID.AddPointer(CP->getConstVal());
421 break;
422 }
Dan Gohman5eb0cec2008-09-15 19:46:03 +0000423 case ISD::CALL: {
424 const CallSDNode *Call = cast<CallSDNode>(N);
425 ID.AddInteger(Call->getCallingConv());
426 ID.AddInteger(Call->isVarArg());
427 break;
428 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000429 case ISD::LOAD: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000430 const LoadSDNode *LD = cast<LoadSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000431 ID.AddInteger(LD->getAddressingMode());
432 ID.AddInteger(LD->getExtensionType());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000433 ID.AddInteger(LD->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000434 ID.AddInteger(LD->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000435 break;
436 }
437 case ISD::STORE: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000438 const StoreSDNode *ST = cast<StoreSDNode>(N);
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000439 ID.AddInteger(ST->getAddressingMode());
440 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +0000441 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000442 ID.AddInteger(ST->getRawFlags());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000443 break;
444 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000445 case ISD::ATOMIC_CMP_SWAP:
446 case ISD::ATOMIC_SWAP:
447 case ISD::ATOMIC_LOAD_ADD:
448 case ISD::ATOMIC_LOAD_SUB:
449 case ISD::ATOMIC_LOAD_AND:
450 case ISD::ATOMIC_LOAD_OR:
451 case ISD::ATOMIC_LOAD_XOR:
452 case ISD::ATOMIC_LOAD_NAND:
453 case ISD::ATOMIC_LOAD_MIN:
454 case ISD::ATOMIC_LOAD_MAX:
455 case ISD::ATOMIC_LOAD_UMIN:
456 case ISD::ATOMIC_LOAD_UMAX: {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000457 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
458 ID.AddInteger(AT->getRawFlags());
Mon P Wang28873102008-06-25 08:15:39 +0000459 break;
Jim Laskey583bd472006-10-27 23:46:08 +0000460 }
Mon P Wang28873102008-06-25 08:15:39 +0000461 } // end switch (N->getOpcode())
Jim Laskey583bd472006-10-27 23:46:08 +0000462}
463
Duncan Sands0dc40452008-10-27 15:30:53 +0000464/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
465/// data.
466static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
467 AddNodeIDOpcode(ID, N->getOpcode());
468 // Add the return value info.
469 AddNodeIDValueTypes(ID, N->getVTList());
470 // Add the operand info.
471 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
472
473 // Handle SDNode leafs with special info.
474 AddNodeIDCustom(ID, N);
475}
476
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000477/// encodeMemSDNodeFlags - Generic routine for computing a value for use in
478/// the CSE map that carries both alignment and volatility information.
479///
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000480static inline unsigned
481encodeMemSDNodeFlags(bool isVolatile, unsigned Alignment) {
Dan Gohmanb8d2f552008-08-20 15:58:01 +0000482 return isVolatile | ((Log2_32(Alignment) + 1) << 1);
483}
484
Jim Laskey583bd472006-10-27 23:46:08 +0000485//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000486// SelectionDAG Class
487//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000488
Duncan Sands0dc40452008-10-27 15:30:53 +0000489/// doNotCSE - Return true if CSE should not be performed for this node.
490static bool doNotCSE(SDNode *N) {
491 if (N->getValueType(0) == MVT::Flag)
492 return true; // Never CSE anything that produces a flag.
493
494 switch (N->getOpcode()) {
495 default: break;
496 case ISD::HANDLENODE:
497 case ISD::DBG_LABEL:
498 case ISD::DBG_STOPPOINT:
499 case ISD::EH_LABEL:
500 case ISD::DECLARE:
501 return true; // Never CSE these nodes.
502 }
503
504 // Check that remaining values produced are not flags.
505 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
506 if (N->getValueType(i) == MVT::Flag)
507 return true; // Never CSE anything that produces a flag.
508
509 return false;
510}
511
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000512/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000513/// SelectionDAG.
514void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000515 // Create a dummy node (which is not added to allnodes), that adds a reference
516 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000517 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000518
Chris Lattner190a4182006-08-04 17:45:20 +0000519 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000520
Chris Lattner190a4182006-08-04 17:45:20 +0000521 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000522 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000523 if (I->use_empty())
524 DeadNodes.push_back(I);
525
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000526 RemoveDeadNodes(DeadNodes);
527
528 // If the root changed (e.g. it was a dead load, update the root).
529 setRoot(Dummy.getValue());
530}
531
532/// RemoveDeadNodes - This method deletes the unreachable nodes in the
533/// given list, and any nodes that become unreachable as a result.
534void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
535 DAGUpdateListener *UpdateListener) {
536
Chris Lattner190a4182006-08-04 17:45:20 +0000537 // Process the worklist, deleting the nodes and adding their uses to the
538 // worklist.
539 while (!DeadNodes.empty()) {
540 SDNode *N = DeadNodes.back();
541 DeadNodes.pop_back();
542
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000543 if (UpdateListener)
544 UpdateListener->NodeDeleted(N, 0);
545
Chris Lattner190a4182006-08-04 17:45:20 +0000546 // Take the node out of the appropriate CSE map.
547 RemoveNodeFromCSEMaps(N);
548
549 // Next, brutally remove the operand list. This is safe to do, as there are
550 // no cycles in the graph.
551 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
Roman Levenstein9cac5252008-04-16 16:15:27 +0000552 SDNode *Operand = I->getVal();
Roman Levensteindc1adac2008-04-07 10:06:32 +0000553 Operand->removeUser(std::distance(N->op_begin(), I), N);
Chris Lattner190a4182006-08-04 17:45:20 +0000554
555 // Now that we removed this operand, see if there are no uses of it left.
556 if (Operand->use_empty())
557 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000558 }
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000559
Dan Gohmanc5336122009-01-19 22:39:36 +0000560 DeallocateNode(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000561 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000562}
563
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000564void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +0000565 SmallVector<SDNode*, 16> DeadNodes(1, N);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +0000566 RemoveDeadNodes(DeadNodes, UpdateListener);
Evan Cheng130a6472006-10-12 20:34:05 +0000567}
568
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000569void SelectionDAG::DeleteNode(SDNode *N) {
570 assert(N->use_empty() && "Cannot delete a node that is not dead!");
571
572 // First take this out of the appropriate CSE map.
573 RemoveNodeFromCSEMaps(N);
574
Chris Lattner1e111c72005-09-07 05:37:01 +0000575 // Finally, remove uses due to operands of this node, remove from the
576 // AllNodes list, and delete the node.
577 DeleteNodeNotInCSEMaps(N);
578}
579
580void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
Dan Gohmanc5336122009-01-19 22:39:36 +0000581 assert(N != AllNodes.begin());
582
Dan Gohmanf06c8352008-09-30 18:30:35 +0000583 // Drop all of the operands and decrement used node's use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000584 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
Roman Levenstein9cac5252008-04-16 16:15:27 +0000585 I->getVal()->removeUser(std::distance(N->op_begin(), I), N);
Bill Wendlinga1dc6022008-10-19 20:51:12 +0000586
Dan Gohmanc5336122009-01-19 22:39:36 +0000587 DeallocateNode(N);
588}
589
590void SelectionDAG::DeallocateNode(SDNode *N) {
591 if (N->OperandsNeedDelete)
Chris Lattner63e3f142007-02-04 07:28:00 +0000592 delete[] N->OperandList;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000593
Dan Gohmanc5336122009-01-19 22:39:36 +0000594 // Set the opcode to DELETED_NODE to help catch bugs when node
595 // memory is reallocated.
596 N->NodeType = ISD::DELETED_NODE;
597
Dan Gohman11467282008-08-26 01:44:34 +0000598 NodeAllocator.Deallocate(AllNodes.remove(N));
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000599}
600
Chris Lattner149c58c2005-08-16 18:17:10 +0000601/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
602/// correspond to it. This is useful when we're about to delete or repurpose
603/// the node. We don't want future request for structurally identical nodes
604/// to return N anymore.
Dan Gohman095cc292008-09-13 01:54:27 +0000605bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000606 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000607 switch (N->getOpcode()) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000608 case ISD::EntryToken:
609 assert(0 && "EntryToken should not be in CSEMaps!");
Dan Gohman095cc292008-09-13 01:54:27 +0000610 return false;
611 case ISD::HANDLENODE: return false; // noop.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000612 case ISD::CONDCODE:
613 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
614 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000615 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000616 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
617 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000618 case ISD::ExternalSymbol:
619 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000620 break;
Bill Wendling056292f2008-09-16 21:48:12 +0000621 case ISD::TargetExternalSymbol:
622 Erased =
623 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000624 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000625 case ISD::VALUETYPE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000626 MVT VT = cast<VTSDNode>(N)->getVT();
627 if (VT.isExtended()) {
Duncan Sandsf411b832007-10-17 13:49:58 +0000628 Erased = ExtendedValueTypeNodes.erase(VT);
629 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000630 Erased = ValueTypeNodes[VT.getSimpleVT()] != 0;
631 ValueTypeNodes[VT.getSimpleVT()] = 0;
Duncan Sandsf411b832007-10-17 13:49:58 +0000632 }
Chris Lattner15e4b012005-07-10 00:07:11 +0000633 break;
Duncan Sandsf411b832007-10-17 13:49:58 +0000634 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000635 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000636 // Remove it from the CSE Map.
637 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000638 break;
639 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000640#ifndef NDEBUG
641 // Verify that the node was actually in one of the CSE maps, unless it has a
642 // flag result (which cannot be CSE'd) or is one of the special cases that are
643 // not subject to CSE.
644 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Duncan Sands0dc40452008-10-27 15:30:53 +0000645 !N->isMachineOpcode() && !doNotCSE(N)) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000646 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000647 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000648 assert(0 && "Node is not in map!");
649 }
650#endif
Dan Gohman095cc292008-09-13 01:54:27 +0000651 return Erased;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000652}
653
Chris Lattner8b8749f2005-08-17 19:00:20 +0000654/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
655/// has been taken out and modified in some way. If the specified node already
656/// exists in the CSE maps, do not modify the maps, but return the existing node
657/// instead. If it doesn't exist, add it and return null.
658///
659SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
660 assert(N->getNumOperands() && "This is a leaf node!");
Evan Cheng71e86852008-07-08 20:06:39 +0000661
Duncan Sands0dc40452008-10-27 15:30:53 +0000662 if (doNotCSE(N))
663 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000664
Chris Lattnera5682852006-08-07 23:03:03 +0000665 SDNode *New = CSEMap.GetOrInsertNode(N);
666 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000667 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000668}
669
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000670/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
671/// were replaced with those specified. If this node is never memoized,
672/// return null, otherwise return a pointer to the slot it would take. If a
673/// node already exists with these operands, the slot will be non-null.
Dan Gohman475871a2008-07-27 21:46:04 +0000674SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
Chris Lattnera5682852006-08-07 23:03:03 +0000675 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000676 if (doNotCSE(N))
677 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000678
Dan Gohman475871a2008-07-27 21:46:04 +0000679 SDValue Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000680 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000681 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Duncan Sands0dc40452008-10-27 15:30:53 +0000682 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000683 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000684}
685
686/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
687/// were replaced with those specified. If this node is never memoized,
688/// return null, otherwise return a pointer to the slot it would take. If a
689/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000690SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000691 SDValue Op1, SDValue Op2,
Chris Lattnera5682852006-08-07 23:03:03 +0000692 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000693 if (doNotCSE(N))
694 return 0;
695
Dan Gohman475871a2008-07-27 21:46:04 +0000696 SDValue Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000697 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000698 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Duncan Sands0dc40452008-10-27 15:30:53 +0000699 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000700 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
701}
702
703
704/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
705/// were replaced with those specified. If this node is never memoized,
706/// return null, otherwise return a pointer to the slot it would take. If a
707/// node already exists with these operands, the slot will be non-null.
708SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000709 const SDValue *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000710 void *&InsertPos) {
Duncan Sands0dc40452008-10-27 15:30:53 +0000711 if (doNotCSE(N))
712 return 0;
Evan Cheng71e86852008-07-08 20:06:39 +0000713
Jim Laskey583bd472006-10-27 23:46:08 +0000714 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000715 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Duncan Sands0dc40452008-10-27 15:30:53 +0000716 AddNodeIDCustom(ID, N);
Chris Lattnera5682852006-08-07 23:03:03 +0000717 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000718}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000719
Duncan Sandsd038e042008-07-21 10:20:31 +0000720/// VerifyNode - Sanity check the given node. Aborts if it is invalid.
721void SelectionDAG::VerifyNode(SDNode *N) {
722 switch (N->getOpcode()) {
723 default:
724 break;
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000725 case ISD::BUILD_PAIR: {
726 MVT VT = N->getValueType(0);
727 assert(N->getNumValues() == 1 && "Too many results!");
728 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
729 "Wrong return type!");
730 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
731 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
732 "Mismatched operand types!");
733 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
734 "Wrong operand type!");
735 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
736 "Wrong return type size");
737 break;
738 }
Duncan Sandsd038e042008-07-21 10:20:31 +0000739 case ISD::BUILD_VECTOR: {
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000740 assert(N->getNumValues() == 1 && "Too many results!");
741 assert(N->getValueType(0).isVector() && "Wrong return type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000742 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000743 "Wrong number of operands!");
Duncan Sands0954aef2008-10-22 09:00:33 +0000744 // FIXME: Change vector_shuffle to a variadic node with mask elements being
745 // operands of the node. Currently the mask is a BUILD_VECTOR passed as an
746 // operand, and it is not always possible to legalize it. Turning off the
747 // following checks at least makes it possible to legalize most of the time.
748// MVT EltVT = N->getValueType(0).getVectorElementType();
749// for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
750// assert(I->getSDValue().getValueType() == EltVT &&
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000751// "Wrong operand type!");
Duncan Sandsd038e042008-07-21 10:20:31 +0000752 break;
753 }
754 }
755}
756
Dan Gohman9c6e70e2008-07-08 23:46:32 +0000757/// getMVTAlignment - Compute the default alignment value for the
758/// given type.
759///
760unsigned SelectionDAG::getMVTAlignment(MVT VT) const {
761 const Type *Ty = VT == MVT::iPTR ?
762 PointerType::get(Type::Int8Ty, 0) :
763 VT.getTypeForMVT();
764
765 return TLI.getTargetData()->getABITypeAlignment(Ty);
766}
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000767
Dan Gohman7c3234c2008-08-27 23:52:12 +0000768SelectionDAG::SelectionDAG(TargetLowering &tli, FunctionLoweringInfo &fli)
769 : TLI(tli), FLI(fli),
770 EntryNode(ISD::EntryToken, getVTList(MVT::Other)),
Dan Gohmanf350b272008-08-23 02:25:05 +0000771 Root(getEntryNode()) {
772 AllNodes.push_back(&EntryNode);
Dan Gohman6f179662008-08-23 00:50:30 +0000773}
774
Devang Patel6e7a1612009-01-09 19:11:50 +0000775void SelectionDAG::init(MachineFunction &mf, MachineModuleInfo *mmi,
776 DwarfWriter *dw) {
Dan Gohman7c3234c2008-08-27 23:52:12 +0000777 MF = &mf;
778 MMI = mmi;
Devang Patel6e7a1612009-01-09 19:11:50 +0000779 DW = dw;
Dan Gohman7c3234c2008-08-27 23:52:12 +0000780}
781
Chris Lattner78ec3112003-08-11 14:57:33 +0000782SelectionDAG::~SelectionDAG() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000783 allnodes_clear();
784}
785
786void SelectionDAG::allnodes_clear() {
Dan Gohman11467282008-08-26 01:44:34 +0000787 assert(&*AllNodes.begin() == &EntryNode);
788 AllNodes.remove(AllNodes.begin());
Dan Gohmanc5336122009-01-19 22:39:36 +0000789 while (!AllNodes.empty())
790 DeallocateNode(AllNodes.begin());
Chris Lattner78ec3112003-08-11 14:57:33 +0000791}
792
Dan Gohman7c3234c2008-08-27 23:52:12 +0000793void SelectionDAG::clear() {
Dan Gohmanf350b272008-08-23 02:25:05 +0000794 allnodes_clear();
795 OperandAllocator.Reset();
796 CSEMap.clear();
797
798 ExtendedValueTypeNodes.clear();
Bill Wendling056292f2008-09-16 21:48:12 +0000799 ExternalSymbols.clear();
800 TargetExternalSymbols.clear();
Dan Gohmanf350b272008-08-23 02:25:05 +0000801 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
802 static_cast<CondCodeSDNode*>(0));
803 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
804 static_cast<SDNode*>(0));
805
806 EntryNode.Uses = 0;
807 AllNodes.push_back(&EntryNode);
808 Root = getEntryNode();
809}
810
Dan Gohman475871a2008-07-27 21:46:04 +0000811SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, MVT VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000812 if (Op.getValueType() == VT) return Op;
Dan Gohman6c231502008-02-29 01:47:35 +0000813 APInt Imm = APInt::getLowBitsSet(Op.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +0000814 VT.getSizeInBits());
Chris Lattner0f2287b2005-04-13 02:38:18 +0000815 return getNode(ISD::AND, Op.getValueType(), Op,
816 getConstant(Imm, Op.getValueType()));
817}
818
Dan Gohman475871a2008-07-27 21:46:04 +0000819SDValue SelectionDAG::getConstant(uint64_t Val, MVT VT, bool isT) {
Evan Cheng0ff39b32008-06-30 07:31:25 +0000820 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000821 return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
Dan Gohman6394b092008-02-08 22:59:30 +0000822}
823
Dan Gohman475871a2008-07-27 21:46:04 +0000824SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000825 return getConstant(*ConstantInt::get(Val), VT, isT);
826}
827
828SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000829 assert(VT.isInteger() && "Cannot create FP integer constant!");
Dan Gohman89081322007-12-12 22:21:26 +0000830
Evan Cheng0ff39b32008-06-30 07:31:25 +0000831 MVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000832 assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
Dan Gohman6394b092008-02-08 22:59:30 +0000833 "APInt size does not match type size!");
Chris Lattner61b09412006-08-11 21:01:22 +0000834
835 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000836 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000837 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000838 ID.AddPointer(&Val);
Chris Lattner61b09412006-08-11 21:01:22 +0000839 void *IP = 0;
Dan Gohman89081322007-12-12 22:21:26 +0000840 SDNode *N = NULL;
841 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
Duncan Sands83ec4b62008-06-06 12:08:01 +0000842 if (!VT.isVector())
Dan Gohman475871a2008-07-27 21:46:04 +0000843 return SDValue(N, 0);
Dan Gohman89081322007-12-12 22:21:26 +0000844 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000845 N = NodeAllocator.Allocate<ConstantSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000846 new (N) ConstantSDNode(isT, &Val, EltVT);
Dan Gohman89081322007-12-12 22:21:26 +0000847 CSEMap.InsertNode(N, IP);
848 AllNodes.push_back(N);
849 }
850
Dan Gohman475871a2008-07-27 21:46:04 +0000851 SDValue Result(N, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000852 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000853 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000854 Ops.assign(VT.getVectorNumElements(), Result);
Dan Gohman89081322007-12-12 22:21:26 +0000855 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
856 }
857 return Result;
Chris Lattner78ec3112003-08-11 14:57:33 +0000858}
859
Dan Gohman475871a2008-07-27 21:46:04 +0000860SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
Chris Lattner0bd48932008-01-17 07:00:52 +0000861 return getConstant(Val, TLI.getPointerTy(), isTarget);
862}
863
864
Dan Gohman475871a2008-07-27 21:46:04 +0000865SDValue SelectionDAG::getConstantFP(const APFloat& V, MVT VT, bool isTarget) {
Dan Gohman4fbd7962008-09-12 18:08:03 +0000866 return getConstantFP(*ConstantFP::get(V), VT, isTarget);
867}
868
869SDValue SelectionDAG::getConstantFP(const ConstantFP& V, MVT VT, bool isTarget){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000870 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000871
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 MVT EltVT =
873 VT.isVector() ? VT.getVectorElementType() : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000874
Chris Lattnerd8658612005-02-17 20:17:32 +0000875 // Do the map lookup using the actual bit pattern for the floating point
876 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
877 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000878 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
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(&V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000882 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +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);
Evan Chengc908dcd2007-06-29 21:36:04 +0000887 if (!N) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000888 N = NodeAllocator.Allocate<ConstantFPSDNode>();
Dan Gohman4fbd7962008-09-12 18:08:03 +0000889 new (N) ConstantFPSDNode(isTarget, &V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +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);
Dan Gohman7f321562007-06-25 16:23:39 +0000898 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
899 }
900 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000901}
902
Dan Gohman475871a2008-07-27 21:46:04 +0000903SDValue SelectionDAG::getConstantFP(double Val, MVT VT, bool isTarget) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000904 MVT EltVT =
905 VT.isVector() ? VT.getVectorElementType() : VT;
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000906 if (EltVT==MVT::f32)
907 return getConstantFP(APFloat((float)Val), VT, isTarget);
908 else
909 return getConstantFP(APFloat(Val), VT, isTarget);
910}
911
Dan Gohman475871a2008-07-27 21:46:04 +0000912SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +0000913 MVT VT, int64_t Offset,
Dan Gohman475871a2008-07-27 21:46:04 +0000914 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000915 unsigned Opc;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000916
Dan Gohman6520e202008-10-18 02:06:02 +0000917 // Truncate (with sign-extension) the offset value to the pointer size.
Dan Gohman44013612008-10-21 03:38:42 +0000918 unsigned BitWidth = TLI.getPointerTy().getSizeInBits();
Dan Gohman6520e202008-10-18 02:06:02 +0000919 if (BitWidth < 64)
920 Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
921
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000922 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
923 if (!GVar) {
Anton Korobeynikovc73ede02008-03-22 07:53:40 +0000924 // If GV is an alias then use the aliasee for determining thread-localness.
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000925 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000926 GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000927 }
928
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000929 if (GVar && GVar->isThreadLocal())
930 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
931 else
932 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Anton Korobeynikov4d86e2a2008-03-11 22:38:53 +0000933
Jim Laskey583bd472006-10-27 23:46:08 +0000934 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000935 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000936 ID.AddPointer(GV);
937 ID.AddInteger(Offset);
938 void *IP = 0;
939 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000940 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000941 SDNode *N = NodeAllocator.Allocate<GlobalAddressSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000942 new (N) GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
Chris Lattner61b09412006-08-11 21:01:22 +0000943 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000944 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000945 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000946}
947
Dan Gohman475871a2008-07-27 21:46:04 +0000948SDValue SelectionDAG::getFrameIndex(int FI, MVT VT, bool isTarget) {
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000949 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000950 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000951 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000952 ID.AddInteger(FI);
953 void *IP = 0;
954 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000955 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000956 SDNode *N = NodeAllocator.Allocate<FrameIndexSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000957 new (N) FrameIndexSDNode(FI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000958 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000959 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000960 return SDValue(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000961}
962
Dan Gohman475871a2008-07-27 21:46:04 +0000963SDValue SelectionDAG::getJumpTable(int JTI, MVT VT, bool isTarget){
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000964 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000965 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000966 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000967 ID.AddInteger(JTI);
968 void *IP = 0;
969 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000970 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000971 SDNode *N = NodeAllocator.Allocate<JumpTableSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000972 new (N) JumpTableSDNode(JTI, VT, isTarget);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000973 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000974 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000975 return SDValue(N, 0);
Nate Begeman37efe672006-04-22 18:53:45 +0000976}
977
Dan Gohman475871a2008-07-27 21:46:04 +0000978SDValue SelectionDAG::getConstantPool(Constant *C, MVT VT,
979 unsigned Alignment, int Offset,
980 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +0000981 if (Alignment == 0)
982 Alignment =
983 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000984 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000985 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +0000986 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000987 ID.AddInteger(Alignment);
988 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000989 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000990 void *IP = 0;
991 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +0000992 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +0000993 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +0000994 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000995 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000996 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000997 return SDValue(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000998}
999
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000
Dan Gohman475871a2008-07-27 21:46:04 +00001001SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, MVT VT,
1002 unsigned Alignment, int Offset,
1003 bool isTarget) {
Dan Gohman50284d82008-09-16 22:05:41 +00001004 if (Alignment == 0)
1005 Alignment =
1006 TLI.getTargetData()->getPreferredTypeAlignmentShift(C->getType());
Evan Chengd6594ae2006-09-12 21:00:35 +00001007 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +00001008 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001009 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001010 ID.AddInteger(Alignment);
1011 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +00001012 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +00001013 void *IP = 0;
1014 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001015 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001016 SDNode *N = NodeAllocator.Allocate<ConstantPoolSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001017 new (N) ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
Evan Chengd6594ae2006-09-12 21:00:35 +00001018 CSEMap.InsertNode(N, IP);
1019 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001020 return SDValue(N, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +00001021}
1022
1023
Dan Gohman475871a2008-07-27 21:46:04 +00001024SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +00001025 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001026 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001027 ID.AddPointer(MBB);
1028 void *IP = 0;
1029 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001030 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001031 SDNode *N = NodeAllocator.Allocate<BasicBlockSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001032 new (N) BasicBlockSDNode(MBB);
Chris Lattner61b09412006-08-11 21:01:22 +00001033 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001035 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036}
1037
Dan Gohman475871a2008-07-27 21:46:04 +00001038SDValue SelectionDAG::getArgFlags(ISD::ArgFlagsTy Flags) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001039 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001040 AddNodeIDNode(ID, ISD::ARG_FLAGS, getVTList(MVT::Other), 0, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001041 ID.AddInteger(Flags.getRawBits());
1042 void *IP = 0;
1043 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001044 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001045 SDNode *N = NodeAllocator.Allocate<ARG_FLAGSSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001046 new (N) ARG_FLAGSSDNode(Flags);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001047 CSEMap.InsertNode(N, IP);
1048 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001049 return SDValue(N, 0);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001050}
1051
Dan Gohman475871a2008-07-27 21:46:04 +00001052SDValue SelectionDAG::getValueType(MVT VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053 if (VT.isSimple() && (unsigned)VT.getSimpleVT() >= ValueTypeNodes.size())
1054 ValueTypeNodes.resize(VT.getSimpleVT()+1);
Chris Lattner15e4b012005-07-10 00:07:11 +00001055
Duncan Sands83ec4b62008-06-06 12:08:01 +00001056 SDNode *&N = VT.isExtended() ?
1057 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT()];
Duncan Sandsf411b832007-10-17 13:49:58 +00001058
Dan Gohman475871a2008-07-27 21:46:04 +00001059 if (N) return SDValue(N, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001060 N = NodeAllocator.Allocate<VTSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001061 new (N) VTSDNode(VT);
Duncan Sandsf411b832007-10-17 13:49:58 +00001062 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001063 return SDValue(N, 0);
Chris Lattner15e4b012005-07-10 00:07:11 +00001064}
1065
Bill Wendling056292f2008-09-16 21:48:12 +00001066SDValue SelectionDAG::getExternalSymbol(const char *Sym, MVT VT) {
1067 SDNode *&N = ExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001068 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001069 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1070 new (N) ExternalSymbolSDNode(false, Sym, VT);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001071 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001072 return SDValue(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001073}
1074
Bill Wendling056292f2008-09-16 21:48:12 +00001075SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT VT) {
1076 SDNode *&N = TargetExternalSymbols[Sym];
Dan Gohman475871a2008-07-27 21:46:04 +00001077 if (N) return SDValue(N, 0);
Bill Wendling056292f2008-09-16 21:48:12 +00001078 N = NodeAllocator.Allocate<ExternalSymbolSDNode>();
1079 new (N) ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001081 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082}
1083
Dan Gohman475871a2008-07-27 21:46:04 +00001084SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001085 if ((unsigned)Cond >= CondCodeNodes.size())
1086 CondCodeNodes.resize(Cond+1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001087
Chris Lattner079a27a2005-08-09 20:40:02 +00001088 if (CondCodeNodes[Cond] == 0) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001089 CondCodeSDNode *N = NodeAllocator.Allocate<CondCodeSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001090 new (N) CondCodeSDNode(Cond);
1091 CondCodeNodes[Cond] = N;
1092 AllNodes.push_back(N);
Chris Lattner079a27a2005-08-09 20:40:02 +00001093 }
Dan Gohman475871a2008-07-27 21:46:04 +00001094 return SDValue(CondCodeNodes[Cond], 0);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001095}
1096
Mon P Wang77cdf302008-11-10 20:54:11 +00001097SDValue SelectionDAG::getConvertRndSat(MVT VT, SDValue Val, SDValue DTy,
1098 SDValue STy, SDValue Rnd, SDValue Sat,
1099 ISD::CvtCode Code) {
Mon P Wang6db08c42008-12-11 03:30:13 +00001100 // If the src and dest types are the same, no conversion is necessary.
1101 if (DTy == STy)
1102 return Val;
1103
Mon P Wang77cdf302008-11-10 20:54:11 +00001104 FoldingSetNodeID ID;
1105 void* IP = 0;
1106 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1107 return SDValue(E, 0);
1108 CvtRndSatSDNode *N = NodeAllocator.Allocate<CvtRndSatSDNode>();
1109 SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
1110 new (N) CvtRndSatSDNode(VT, Ops, 5, Code);
1111 CSEMap.InsertNode(N, IP);
1112 AllNodes.push_back(N);
1113 return SDValue(N, 0);
1114}
1115
Dan Gohman475871a2008-07-27 21:46:04 +00001116SDValue SelectionDAG::getRegister(unsigned RegNo, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001117 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001118 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001119 ID.AddInteger(RegNo);
1120 void *IP = 0;
1121 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001122 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001123 SDNode *N = NodeAllocator.Allocate<RegisterSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001124 new (N) RegisterSDNode(RegNo, VT);
Chris Lattner61b09412006-08-11 21:01:22 +00001125 CSEMap.InsertNode(N, IP);
1126 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001127 return SDValue(N, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001128}
1129
Dan Gohman475871a2008-07-27 21:46:04 +00001130SDValue SelectionDAG::getDbgStopPoint(SDValue Root,
Devang Patel83489bb2009-01-13 00:35:13 +00001131 unsigned Line, unsigned Col,
1132 Value *CU) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00001133 SDNode *N = NodeAllocator.Allocate<DbgStopPointSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001134 new (N) DbgStopPointSDNode(Root, Line, Col, CU);
Dan Gohman7f460202008-06-30 20:59:49 +00001135 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001136 return SDValue(N, 0);
Dan Gohman7f460202008-06-30 20:59:49 +00001137}
1138
Dan Gohman475871a2008-07-27 21:46:04 +00001139SDValue SelectionDAG::getLabel(unsigned Opcode,
1140 SDValue Root,
1141 unsigned LabelID) {
Dan Gohman44066042008-07-01 00:05:16 +00001142 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00001143 SDValue Ops[] = { Root };
Dan Gohman44066042008-07-01 00:05:16 +00001144 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), &Ops[0], 1);
1145 ID.AddInteger(LabelID);
1146 void *IP = 0;
1147 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001148 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00001149 SDNode *N = NodeAllocator.Allocate<LabelSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001150 new (N) LabelSDNode(Opcode, Root, LabelID);
Dan Gohman44066042008-07-01 00:05:16 +00001151 CSEMap.InsertNode(N, IP);
1152 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001153 return SDValue(N, 0);
Dan Gohman44066042008-07-01 00:05:16 +00001154}
1155
Dan Gohman475871a2008-07-27 21:46:04 +00001156SDValue SelectionDAG::getSrcValue(const Value *V) {
Chris Lattner61b09412006-08-11 21:01:22 +00001157 assert((!V || isa<PointerType>(V->getType())) &&
1158 "SrcValue is not a pointer?");
1159
Jim Laskey583bd472006-10-27 23:46:08 +00001160 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001161 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +00001162 ID.AddPointer(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001163
Chris Lattner61b09412006-08-11 21:01:22 +00001164 void *IP = 0;
1165 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001166 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001167
Dan Gohmanfed90b62008-07-28 21:51:04 +00001168 SDNode *N = NodeAllocator.Allocate<SrcValueSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001169 new (N) SrcValueSDNode(V);
Dan Gohman69de1932008-02-06 22:27:42 +00001170 CSEMap.InsertNode(N, IP);
1171 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001172 return SDValue(N, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001173}
1174
Dan Gohman475871a2008-07-27 21:46:04 +00001175SDValue SelectionDAG::getMemOperand(const MachineMemOperand &MO) {
Evan Cheng24ac4082008-11-24 07:09:49 +00001176#ifndef NDEBUG
Dan Gohman69de1932008-02-06 22:27:42 +00001177 const Value *v = MO.getValue();
1178 assert((!v || isa<PointerType>(v->getType())) &&
1179 "SrcValue is not a pointer?");
Evan Cheng24ac4082008-11-24 07:09:49 +00001180#endif
Dan Gohman69de1932008-02-06 22:27:42 +00001181
1182 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00001183 AddNodeIDNode(ID, ISD::MEMOPERAND, getVTList(MVT::Other), 0, 0);
Dan Gohmanb8d2f552008-08-20 15:58:01 +00001184 MO.Profile(ID);
Dan Gohman69de1932008-02-06 22:27:42 +00001185
1186 void *IP = 0;
1187 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00001188 return SDValue(E, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001189
Dan Gohmanfed90b62008-07-28 21:51:04 +00001190 SDNode *N = NodeAllocator.Allocate<MemOperandSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00001191 new (N) MemOperandSDNode(MO);
Chris Lattner61b09412006-08-11 21:01:22 +00001192 CSEMap.InsertNode(N, IP);
1193 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001194 return SDValue(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001195}
1196
Chris Lattner37ce9df2007-10-15 17:47:20 +00001197/// CreateStackTemporary - Create a stack temporary, suitable for holding the
1198/// specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00001199SDValue SelectionDAG::CreateStackTemporary(MVT VT, unsigned minAlign) {
Chris Lattner37ce9df2007-10-15 17:47:20 +00001200 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
Duncan Sands9304f2c2008-12-04 18:08:40 +00001201 unsigned ByteSize = VT.getStoreSizeInBits()/8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001202 const Type *Ty = VT.getTypeForMVT();
Mon P Wang364d73d2008-07-05 20:40:31 +00001203 unsigned StackAlign =
1204 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
1205
Chris Lattner37ce9df2007-10-15 17:47:20 +00001206 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
1207 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1208}
1209
Duncan Sands47d9dcc2008-12-09 21:33:20 +00001210/// CreateStackTemporary - Create a stack temporary suitable for holding
1211/// either of the specified value types.
1212SDValue SelectionDAG::CreateStackTemporary(MVT VT1, MVT VT2) {
1213 unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
1214 VT2.getStoreSizeInBits())/8;
1215 const Type *Ty1 = VT1.getTypeForMVT();
1216 const Type *Ty2 = VT2.getTypeForMVT();
1217 const TargetData *TD = TLI.getTargetData();
1218 unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
1219 TD->getPrefTypeAlignment(Ty2));
1220
1221 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
1222 int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align);
1223 return getFrameIndex(FrameIdx, TLI.getPointerTy());
1224}
1225
Dan Gohman475871a2008-07-27 21:46:04 +00001226SDValue SelectionDAG::FoldSetCC(MVT VT, SDValue N1,
1227 SDValue N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228 // These setcc operations always fold.
1229 switch (Cond) {
1230 default: break;
1231 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001232 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001233 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001234 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +00001235
1236 case ISD::SETOEQ:
1237 case ISD::SETOGT:
1238 case ISD::SETOGE:
1239 case ISD::SETOLT:
1240 case ISD::SETOLE:
1241 case ISD::SETONE:
1242 case ISD::SETO:
1243 case ISD::SETUO:
1244 case ISD::SETUEQ:
1245 case ISD::SETUNE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001246 assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
Chris Lattnera83385f2006-04-27 05:01:07 +00001247 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001248 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001249
Gabor Greifba36cb52008-08-28 21:40:38 +00001250 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001251 const APInt &C2 = N2C->getAPIntValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00001253 const APInt &C1 = N1C->getAPIntValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +00001254
Chris Lattnerc3aae252005-01-07 07:46:32 +00001255 switch (Cond) {
1256 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001257 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1258 case ISD::SETNE: return getConstant(C1 != C2, VT);
Dan Gohman6c231502008-02-29 01:47:35 +00001259 case ISD::SETULT: return getConstant(C1.ult(C2), VT);
1260 case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
1261 case ISD::SETULE: return getConstant(C1.ule(C2), VT);
1262 case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
1263 case ISD::SETLT: return getConstant(C1.slt(C2), VT);
1264 case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
1265 case ISD::SETLE: return getConstant(C1.sle(C2), VT);
1266 case ISD::SETGE: return getConstant(C1.sge(C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001267 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268 }
Chris Lattner67255a12005-04-07 18:14:58 +00001269 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001270 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
1271 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +00001272 // No compile time operations on this type yet.
1273 if (N1C->getValueType(0) == MVT::ppcf128)
Dan Gohman475871a2008-07-27 21:46:04 +00001274 return SDValue();
Dale Johanneseneaf08942007-08-31 04:03:46 +00001275
1276 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001277 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +00001278 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +00001279 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
1280 return getNode(ISD::UNDEF, VT);
1281 // fall through
1282 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
1283 case ISD::SETNE: if (R==APFloat::cmpUnordered)
1284 return getNode(ISD::UNDEF, VT);
1285 // fall through
1286 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001287 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001288 case ISD::SETLT: if (R==APFloat::cmpUnordered)
1289 return getNode(ISD::UNDEF, VT);
1290 // fall through
1291 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1292 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1293 return getNode(ISD::UNDEF, VT);
1294 // fall through
1295 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1296 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1297 return getNode(ISD::UNDEF, VT);
1298 // fall through
1299 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001300 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001301 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1302 return getNode(ISD::UNDEF, VT);
1303 // fall through
1304 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001305 R==APFloat::cmpEqual, VT);
1306 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1307 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1308 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1309 R==APFloat::cmpEqual, VT);
1310 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1311 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1312 R==APFloat::cmpLessThan, VT);
1313 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1314 R==APFloat::cmpUnordered, VT);
1315 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1316 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317 }
1318 } else {
1319 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001320 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 }
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00001322 }
1323
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001324 // Could not fold it.
Dan Gohman475871a2008-07-27 21:46:04 +00001325 return SDValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001326}
1327
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001328/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
1329/// use this predicate to simplify operations downstream.
Dan Gohman475871a2008-07-27 21:46:04 +00001330bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001331 unsigned BitWidth = Op.getValueSizeInBits();
1332 return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
1333}
1334
Dan Gohmanea859be2007-06-22 14:59:07 +00001335/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1336/// this predicate to simplify operations downstream. Mask is known to be zero
1337/// for bits that V cannot have.
Dan Gohman475871a2008-07-27 21:46:04 +00001338bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
Dan Gohmanea859be2007-06-22 14:59:07 +00001339 unsigned Depth) const {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001340 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00001341 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1342 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1343 return (KnownZero & Mask) == Mask;
1344}
1345
1346/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1347/// known to be either zero or one and return them in the KnownZero/KnownOne
1348/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1349/// processing.
Dan Gohman475871a2008-07-27 21:46:04 +00001350void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001351 APInt &KnownZero, APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00001352 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001353 unsigned BitWidth = Mask.getBitWidth();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001354 assert(BitWidth == Op.getValueType().getSizeInBits() &&
Dan Gohmand9fe41c2008-02-13 23:13:32 +00001355 "Mask size mismatches value type size!");
1356
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001357 KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
Dan Gohmanea859be2007-06-22 14:59:07 +00001358 if (Depth == 6 || Mask == 0)
1359 return; // Limit search depth.
1360
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001361 APInt KnownZero2, KnownOne2;
Dan Gohmanea859be2007-06-22 14:59:07 +00001362
1363 switch (Op.getOpcode()) {
1364 case ISD::Constant:
1365 // We know all of the bits for a constant!
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001366 KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001367 KnownZero = ~KnownOne & Mask;
1368 return;
1369 case ISD::AND:
1370 // If either the LHS or the RHS are Zero, the result is zero.
1371 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001372 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
1373 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001374 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1375 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1376
1377 // Output known-1 bits are only known if set in both the LHS & RHS.
1378 KnownOne &= KnownOne2;
1379 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1380 KnownZero |= KnownZero2;
1381 return;
1382 case ISD::OR:
1383 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
Dan Gohman977a76f2008-02-13 22:28:48 +00001384 ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
1385 KnownZero2, KnownOne2, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001386 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1387 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1388
1389 // Output known-0 bits are only known if clear in both the LHS & RHS.
1390 KnownZero &= KnownZero2;
1391 // Output known-1 are known to be set if set in either the LHS | RHS.
1392 KnownOne |= KnownOne2;
1393 return;
1394 case ISD::XOR: {
1395 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1396 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1397 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1398 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1399
1400 // Output known-0 bits are known if clear or set in both the LHS & RHS.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001401 APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
Dan Gohmanea859be2007-06-22 14:59:07 +00001402 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1403 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1404 KnownZero = KnownZeroOut;
1405 return;
1406 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001407 case ISD::MUL: {
1408 APInt Mask2 = APInt::getAllOnesValue(BitWidth);
1409 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
1410 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1411 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1412 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1413
1414 // If low bits are zero in either operand, output low known-0 bits.
1415 // Also compute a conserative estimate for high known-0 bits.
1416 // More trickiness is possible, but this is sufficient for the
1417 // interesting case of alignment computation.
1418 KnownOne.clear();
1419 unsigned TrailZ = KnownZero.countTrailingOnes() +
1420 KnownZero2.countTrailingOnes();
1421 unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
Dan Gohman42ac9292008-05-07 00:35:55 +00001422 KnownZero2.countLeadingOnes(),
1423 BitWidth) - BitWidth;
Dan Gohman23e8b712008-04-28 17:02:21 +00001424
1425 TrailZ = std::min(TrailZ, BitWidth);
1426 LeadZ = std::min(LeadZ, BitWidth);
1427 KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
1428 APInt::getHighBitsSet(BitWidth, LeadZ);
1429 KnownZero &= Mask;
1430 return;
1431 }
1432 case ISD::UDIV: {
1433 // For the purposes of computing leading zeros we can conservatively
1434 // treat a udiv as a logical right shift by the power of 2 known to
Dan Gohman1d9cd502008-05-02 21:30:02 +00001435 // be less than the denominator.
Dan Gohman23e8b712008-04-28 17:02:21 +00001436 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1437 ComputeMaskedBits(Op.getOperand(0),
1438 AllOnes, KnownZero2, KnownOne2, Depth+1);
1439 unsigned LeadZ = KnownZero2.countLeadingOnes();
1440
1441 KnownOne2.clear();
1442 KnownZero2.clear();
1443 ComputeMaskedBits(Op.getOperand(1),
1444 AllOnes, KnownZero2, KnownOne2, Depth+1);
Dan Gohman1d9cd502008-05-02 21:30:02 +00001445 unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
1446 if (RHSUnknownLeadingOnes != BitWidth)
1447 LeadZ = std::min(BitWidth,
1448 LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001449
1450 KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
1451 return;
1452 }
Dan Gohmanea859be2007-06-22 14:59:07 +00001453 case ISD::SELECT:
1454 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1455 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1456 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1457 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1458
1459 // Only known if known in both the LHS and RHS.
1460 KnownOne &= KnownOne2;
1461 KnownZero &= KnownZero2;
1462 return;
1463 case ISD::SELECT_CC:
1464 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1465 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1466 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1467 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1468
1469 // Only known if known in both the LHS and RHS.
1470 KnownOne &= KnownOne2;
1471 KnownZero &= KnownZero2;
1472 return;
Bill Wendling253174b2008-11-22 07:24:01 +00001473 case ISD::SADDO:
1474 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001475 case ISD::SSUBO:
1476 case ISD::USUBO:
1477 case ISD::SMULO:
1478 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001479 if (Op.getResNo() != 1)
1480 return;
Duncan Sands03228082008-11-23 15:47:28 +00001481 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001482 case ISD::SETCC:
1483 // If we know the result of a setcc has the top bits zero, use this info.
Duncan Sands03228082008-11-23 15:47:28 +00001484 if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001485 BitWidth > 1)
1486 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001487 return;
1488 case ISD::SHL:
1489 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1490 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001491 unsigned ShAmt = SA->getZExtValue();
Dan Gohmand4cf9922008-02-26 18:50:50 +00001492
1493 // If the shift count is an invalid immediate, don't do anything.
1494 if (ShAmt >= BitWidth)
1495 return;
1496
1497 ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001498 KnownZero, KnownOne, Depth+1);
1499 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmand4cf9922008-02-26 18:50:50 +00001500 KnownZero <<= ShAmt;
1501 KnownOne <<= ShAmt;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001502 // low bits known zero.
Dan Gohmand4cf9922008-02-26 18:50:50 +00001503 KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001504 }
1505 return;
1506 case ISD::SRL:
1507 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1508 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001509 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001510
Dan Gohmand4cf9922008-02-26 18:50:50 +00001511 // If the shift count is an invalid immediate, don't do anything.
1512 if (ShAmt >= BitWidth)
1513 return;
1514
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001515 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
Dan Gohmanea859be2007-06-22 14:59:07 +00001516 KnownZero, KnownOne, Depth+1);
1517 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001518 KnownZero = KnownZero.lshr(ShAmt);
1519 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001520
Dan Gohman72d2fd52008-02-13 22:43:25 +00001521 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001522 KnownZero |= HighBits; // High bits known zero.
1523 }
1524 return;
1525 case ISD::SRA:
1526 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001527 unsigned ShAmt = SA->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001528
Dan Gohmand4cf9922008-02-26 18:50:50 +00001529 // If the shift count is an invalid immediate, don't do anything.
1530 if (ShAmt >= BitWidth)
1531 return;
1532
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001533 APInt InDemandedMask = (Mask << ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001534 // If any of the demanded bits are produced by the sign extension, we also
1535 // demand the input sign bit.
Dan Gohman72d2fd52008-02-13 22:43:25 +00001536 APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
1537 if (HighBits.getBoolValue())
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001538 InDemandedMask |= APInt::getSignBit(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001539
1540 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1541 Depth+1);
1542 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001543 KnownZero = KnownZero.lshr(ShAmt);
1544 KnownOne = KnownOne.lshr(ShAmt);
Dan Gohmanea859be2007-06-22 14:59:07 +00001545
1546 // Handle the sign bits.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001547 APInt SignBit = APInt::getSignBit(BitWidth);
1548 SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
Dan Gohmanea859be2007-06-22 14:59:07 +00001549
Dan Gohmanca93a432008-02-20 16:30:17 +00001550 if (KnownZero.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001551 KnownZero |= HighBits; // New bits are known zero.
Dan Gohmanca93a432008-02-20 16:30:17 +00001552 } else if (KnownOne.intersects(SignBit)) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001553 KnownOne |= HighBits; // New bits are known one.
1554 }
1555 }
1556 return;
1557 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001558 MVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1559 unsigned EBits = EVT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001560
1561 // Sign extension. Compute the demanded bits in the result that are not
1562 // present in the input.
Dan Gohman977a76f2008-02-13 22:28:48 +00001563 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001564
Dan Gohman977a76f2008-02-13 22:28:48 +00001565 APInt InSignBit = APInt::getSignBit(EBits);
1566 APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001567
1568 // If the sign extended bits are demanded, we know that the sign
1569 // bit is demanded.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001570 InSignBit.zext(BitWidth);
Dan Gohman977a76f2008-02-13 22:28:48 +00001571 if (NewBits.getBoolValue())
Dan Gohmanea859be2007-06-22 14:59:07 +00001572 InputDemandedBits |= InSignBit;
1573
1574 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1575 KnownZero, KnownOne, Depth+1);
1576 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1577
1578 // If the sign bit of the input is known set or clear, then we know the
1579 // top bits of the result.
Dan Gohmanca93a432008-02-20 16:30:17 +00001580 if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
Dan Gohmanea859be2007-06-22 14:59:07 +00001581 KnownZero |= NewBits;
1582 KnownOne &= ~NewBits;
Dan Gohmanca93a432008-02-20 16:30:17 +00001583 } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
Dan Gohmanea859be2007-06-22 14:59:07 +00001584 KnownOne |= NewBits;
1585 KnownZero &= ~NewBits;
1586 } else { // Input sign bit unknown
1587 KnownZero &= ~NewBits;
1588 KnownOne &= ~NewBits;
1589 }
1590 return;
1591 }
1592 case ISD::CTTZ:
1593 case ISD::CTLZ:
1594 case ISD::CTPOP: {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001595 unsigned LowBits = Log2_32(BitWidth)+1;
1596 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
Dan Gohman317adcc2008-06-21 22:02:15 +00001597 KnownOne.clear();
Dan Gohmanea859be2007-06-22 14:59:07 +00001598 return;
1599 }
1600 case ISD::LOAD: {
Gabor Greifba36cb52008-08-28 21:40:38 +00001601 if (ISD::isZEXTLoad(Op.getNode())) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001602 LoadSDNode *LD = cast<LoadSDNode>(Op);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001603 MVT VT = LD->getMemoryVT();
1604 unsigned MemBits = VT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001605 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
Dan Gohmanea859be2007-06-22 14:59:07 +00001606 }
1607 return;
1608 }
1609 case ISD::ZERO_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001610 MVT InVT = Op.getOperand(0).getValueType();
1611 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001612 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1613 APInt InMask = Mask;
1614 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001615 KnownZero.trunc(InBits);
1616 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001617 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001618 KnownZero.zext(BitWidth);
1619 KnownOne.zext(BitWidth);
1620 KnownZero |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001621 return;
1622 }
1623 case ISD::SIGN_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001624 MVT InVT = Op.getOperand(0).getValueType();
1625 unsigned InBits = InVT.getSizeInBits();
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001626 APInt InSignBit = APInt::getSignBit(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001627 APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
1628 APInt InMask = Mask;
1629 InMask.trunc(InBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001630
1631 // If any of the sign extended bits are demanded, we know that the sign
Dan Gohman977a76f2008-02-13 22:28:48 +00001632 // bit is demanded. Temporarily set this bit in the mask for our callee.
1633 if (NewBits.getBoolValue())
1634 InMask |= InSignBit;
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001635
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001636 KnownZero.trunc(InBits);
1637 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001638 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
1639
1640 // Note if the sign bit is known to be zero or one.
1641 bool SignBitKnownZero = KnownZero.isNegative();
1642 bool SignBitKnownOne = KnownOne.isNegative();
1643 assert(!(SignBitKnownZero && SignBitKnownOne) &&
1644 "Sign bit can't be known to be both zero and one!");
1645
1646 // If the sign bit wasn't actually demanded by our caller, we don't
1647 // want it set in the KnownZero and KnownOne result values. Reset the
1648 // mask and reapply it to the result values.
1649 InMask = Mask;
1650 InMask.trunc(InBits);
1651 KnownZero &= InMask;
1652 KnownOne &= InMask;
1653
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001654 KnownZero.zext(BitWidth);
1655 KnownOne.zext(BitWidth);
1656
Dan Gohman977a76f2008-02-13 22:28:48 +00001657 // If the sign bit is known zero or one, the top bits match.
1658 if (SignBitKnownZero)
Dan Gohmanea859be2007-06-22 14:59:07 +00001659 KnownZero |= NewBits;
Dan Gohman977a76f2008-02-13 22:28:48 +00001660 else if (SignBitKnownOne)
Dan Gohmanea859be2007-06-22 14:59:07 +00001661 KnownOne |= NewBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001662 return;
1663 }
1664 case ISD::ANY_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001665 MVT InVT = Op.getOperand(0).getValueType();
1666 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001667 APInt InMask = Mask;
1668 InMask.trunc(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001669 KnownZero.trunc(InBits);
1670 KnownOne.trunc(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001671 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001672 KnownZero.zext(BitWidth);
1673 KnownOne.zext(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001674 return;
1675 }
1676 case ISD::TRUNCATE: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001677 MVT InVT = Op.getOperand(0).getValueType();
1678 unsigned InBits = InVT.getSizeInBits();
Dan Gohman977a76f2008-02-13 22:28:48 +00001679 APInt InMask = Mask;
1680 InMask.zext(InBits);
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001681 KnownZero.zext(InBits);
1682 KnownOne.zext(InBits);
Dan Gohman977a76f2008-02-13 22:28:48 +00001683 ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001684 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001685 KnownZero.trunc(BitWidth);
1686 KnownOne.trunc(BitWidth);
Dan Gohmanea859be2007-06-22 14:59:07 +00001687 break;
1688 }
1689 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001690 MVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1691 APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001692 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1693 KnownOne, Depth+1);
1694 KnownZero |= (~InMask) & Mask;
1695 return;
1696 }
Chris Lattnerd268a492007-12-22 21:26:52 +00001697 case ISD::FGETSIGN:
1698 // All bits are zero except the low bit.
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00001699 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
Chris Lattnerd268a492007-12-22 21:26:52 +00001700 return;
1701
Dan Gohman23e8b712008-04-28 17:02:21 +00001702 case ISD::SUB: {
1703 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
1704 // We know that the top bits of C-X are clear if X contains less bits
1705 // than C (i.e. no wrap-around can happen). For example, 20-X is
1706 // positive if we can prove that X is >= 0 and < 16.
1707 if (CLHS->getAPIntValue().isNonNegative()) {
1708 unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
1709 // NLZ can't be BitWidth with no sign bit
1710 APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
1711 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
1712 Depth+1);
1713
1714 // If all of the MaskV bits are known to be zero, then we know the
1715 // output top bits are zero, because we now know that the output is
1716 // from [0-C].
1717 if ((KnownZero2 & MaskV) == MaskV) {
1718 unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
1719 // Top bits known zero.
1720 KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
1721 }
1722 }
1723 }
1724 }
1725 // fall through
Dan Gohmanea859be2007-06-22 14:59:07 +00001726 case ISD::ADD: {
Dan Gohmanea859be2007-06-22 14:59:07 +00001727 // Output known-0 bits are known if clear or set in both the low clear bits
1728 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1729 // low 3 bits clear.
Dan Gohman23e8b712008-04-28 17:02:21 +00001730 APInt Mask2 = APInt::getLowBitsSet(BitWidth, Mask.countTrailingOnes());
1731 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
1732 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1733 unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
1734
1735 ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
1736 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1737 KnownZeroOut = std::min(KnownZeroOut,
1738 KnownZero2.countTrailingOnes());
1739
1740 KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
Dan Gohmanea859be2007-06-22 14:59:07 +00001741 return;
1742 }
Dan Gohman23e8b712008-04-28 17:02:21 +00001743 case ISD::SREM:
1744 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001745 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e8b712008-04-28 17:02:21 +00001746 if (RA.isPowerOf2() || (-RA).isPowerOf2()) {
Dan Gohman23e1df82008-05-06 00:51:48 +00001747 APInt LowBits = RA.isStrictlyPositive() ? (RA - 1) : ~RA;
Dan Gohman23e8b712008-04-28 17:02:21 +00001748 APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
1749 ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001750
Dan Gohmana60832b2008-08-13 23:12:35 +00001751 // If the sign bit of the first operand is zero, the sign bit of
1752 // the result is zero. If the first operand has no one bits below
1753 // the second operand's single 1 bit, its sign will be zero.
Dan Gohman23e8b712008-04-28 17:02:21 +00001754 if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
1755 KnownZero2 |= ~LowBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00001756
Dan Gohman23e8b712008-04-28 17:02:21 +00001757 KnownZero |= KnownZero2 & Mask;
Dan Gohman23e8b712008-04-28 17:02:21 +00001758
1759 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
Dan Gohmanea859be2007-06-22 14:59:07 +00001760 }
1761 }
1762 return;
Dan Gohman23e8b712008-04-28 17:02:21 +00001763 case ISD::UREM: {
1764 if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohman9b44c1f2008-07-03 00:52:03 +00001765 const APInt &RA = Rem->getAPIntValue();
Dan Gohman23e1df82008-05-06 00:51:48 +00001766 if (RA.isPowerOf2()) {
1767 APInt LowBits = (RA - 1);
Dan Gohman23e8b712008-04-28 17:02:21 +00001768 APInt Mask2 = LowBits & Mask;
1769 KnownZero |= ~LowBits & Mask;
1770 ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
1771 assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
1772 break;
1773 }
1774 }
1775
1776 // Since the result is less than or equal to either operand, any leading
1777 // zero bits in either operand must also exist in the result.
1778 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
1779 ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
1780 Depth+1);
1781 ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
1782 Depth+1);
1783
1784 uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
1785 KnownZero2.countLeadingOnes());
1786 KnownOne.clear();
1787 KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
1788 return;
Dan Gohmanea859be2007-06-22 14:59:07 +00001789 }
1790 default:
1791 // Allow the target to implement this method for its nodes.
1792 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1793 case ISD::INTRINSIC_WO_CHAIN:
1794 case ISD::INTRINSIC_W_CHAIN:
1795 case ISD::INTRINSIC_VOID:
1796 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1797 }
1798 return;
1799 }
1800}
1801
1802/// ComputeNumSignBits - Return the number of times the sign bit of the
1803/// register is replicated into the other bits. We know that at least 1 bit
1804/// is always equal to the sign bit (itself), but other cases can give us
1805/// information. For example, immediately after an "SRA X, 2", we know that
1806/// the top 3 bits are all equal to each other, so we return 3.
Dan Gohman475871a2008-07-27 21:46:04 +00001807unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
Duncan Sands83ec4b62008-06-06 12:08:01 +00001808 MVT VT = Op.getValueType();
1809 assert(VT.isInteger() && "Invalid VT!");
1810 unsigned VTBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001811 unsigned Tmp, Tmp2;
Dan Gohmana332f172008-05-23 02:28:01 +00001812 unsigned FirstAnswer = 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00001813
1814 if (Depth == 6)
1815 return 1; // Limit search depth.
1816
1817 switch (Op.getOpcode()) {
1818 default: break;
1819 case ISD::AssertSext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001820 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001821 return VTBits-Tmp+1;
1822 case ISD::AssertZext:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001823 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001824 return VTBits-Tmp;
1825
1826 case ISD::Constant: {
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001827 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
1828 // If negative, return # leading ones.
1829 if (Val.isNegative())
1830 return Val.countLeadingOnes();
Dan Gohmanea859be2007-06-22 14:59:07 +00001831
Dan Gohmanbb271ff2008-03-03 23:35:36 +00001832 // Return # leading zeros.
1833 return Val.countLeadingZeros();
Dan Gohmanea859be2007-06-22 14:59:07 +00001834 }
1835
1836 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001837 Tmp = VTBits-Op.getOperand(0).getValueType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001838 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1839
1840 case ISD::SIGN_EXTEND_INREG:
1841 // Max of the input and what this extends.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001842 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001843 Tmp = VTBits-Tmp+1;
1844
1845 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1846 return std::max(Tmp, Tmp2);
1847
1848 case ISD::SRA:
1849 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1850 // SRA X, C -> adds C sign bits.
1851 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001852 Tmp += C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001853 if (Tmp > VTBits) Tmp = VTBits;
1854 }
1855 return Tmp;
1856 case ISD::SHL:
1857 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1858 // shl destroys sign bits.
1859 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001860 if (C->getZExtValue() >= VTBits || // Bad shift.
1861 C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
1862 return Tmp - C->getZExtValue();
Dan Gohmanea859be2007-06-22 14:59:07 +00001863 }
1864 break;
1865 case ISD::AND:
1866 case ISD::OR:
1867 case ISD::XOR: // NOT is handled here.
Dan Gohmana332f172008-05-23 02:28:01 +00001868 // Logical binary ops preserve the number of sign bits at the worst.
Dan Gohmanea859be2007-06-22 14:59:07 +00001869 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
Dan Gohmana332f172008-05-23 02:28:01 +00001870 if (Tmp != 1) {
1871 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1872 FirstAnswer = std::min(Tmp, Tmp2);
1873 // We computed what we know about the sign bits as our first
1874 // answer. Now proceed to the generic code that uses
1875 // ComputeMaskedBits, and pick whichever answer is better.
1876 }
1877 break;
Dan Gohmanea859be2007-06-22 14:59:07 +00001878
1879 case ISD::SELECT:
Dan Gohmanc84941b2008-05-20 20:59:51 +00001880 Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001881 if (Tmp == 1) return 1; // Early out.
Dan Gohmanc84941b2008-05-20 20:59:51 +00001882 Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001883 return std::min(Tmp, Tmp2);
Bill Wendling253174b2008-11-22 07:24:01 +00001884
1885 case ISD::SADDO:
1886 case ISD::UADDO:
Bill Wendling74c37652008-12-09 22:08:41 +00001887 case ISD::SSUBO:
1888 case ISD::USUBO:
1889 case ISD::SMULO:
1890 case ISD::UMULO:
Bill Wendling253174b2008-11-22 07:24:01 +00001891 if (Op.getResNo() != 1)
1892 break;
Duncan Sands03228082008-11-23 15:47:28 +00001893 // The boolean result conforms to getBooleanContents. Fall through.
Dan Gohmanea859be2007-06-22 14:59:07 +00001894 case ISD::SETCC:
1895 // If setcc returns 0/-1, all bits are sign bits.
Duncan Sands03228082008-11-23 15:47:28 +00001896 if (TLI.getBooleanContents() ==
1897 TargetLowering::ZeroOrNegativeOneBooleanContent)
Dan Gohmanea859be2007-06-22 14:59:07 +00001898 return VTBits;
1899 break;
1900 case ISD::ROTL:
1901 case ISD::ROTR:
1902 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001903 unsigned RotAmt = C->getZExtValue() & (VTBits-1);
Dan Gohmanea859be2007-06-22 14:59:07 +00001904
1905 // Handle rotate right by N like a rotate left by 32-N.
1906 if (Op.getOpcode() == ISD::ROTR)
1907 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1908
1909 // If we aren't rotating out all of the known-in sign bits, return the
1910 // number that are left. This handles rotl(sext(x), 1) for example.
1911 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1912 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1913 }
1914 break;
1915 case ISD::ADD:
1916 // Add can have at most one carry bit. Thus we know that the output
1917 // is, at worst, one more bit than the inputs.
1918 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1919 if (Tmp == 1) return 1; // Early out.
1920
1921 // Special case decrementing a value (ADD X, -1):
1922 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1923 if (CRHS->isAllOnesValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001924 APInt KnownZero, KnownOne;
1925 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001926 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1927
1928 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1929 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001930 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001931 return VTBits;
1932
1933 // If we are subtracting one from a positive number, there is no carry
1934 // out of the result.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001935 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001936 return Tmp;
1937 }
1938
1939 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1940 if (Tmp2 == 1) return 1;
1941 return std::min(Tmp, Tmp2)-1;
1942 break;
1943
1944 case ISD::SUB:
1945 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1946 if (Tmp2 == 1) return 1;
1947
1948 // Handle NEG.
1949 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001950 if (CLHS->isNullValue()) {
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001951 APInt KnownZero, KnownOne;
1952 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00001953 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1954 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1955 // sign bits set.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001956 if ((KnownZero | APInt(VTBits, 1)) == Mask)
Dan Gohmanea859be2007-06-22 14:59:07 +00001957 return VTBits;
1958
1959 // If the input is known to be positive (the sign bit is known clear),
1960 // the output of the NEG has the same number of sign bits as the input.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001961 if (KnownZero.isNegative())
Dan Gohmanea859be2007-06-22 14:59:07 +00001962 return Tmp2;
1963
1964 // Otherwise, we treat this like a SUB.
1965 }
1966
1967 // Sub can have at most one carry bit. Thus we know that the output
1968 // is, at worst, one more bit than the inputs.
1969 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1970 if (Tmp == 1) return 1; // Early out.
1971 return std::min(Tmp, Tmp2)-1;
1972 break;
1973 case ISD::TRUNCATE:
1974 // FIXME: it's tricky to do anything useful for this, but it is an important
1975 // case for targets like X86.
1976 break;
1977 }
1978
1979 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1980 if (Op.getOpcode() == ISD::LOAD) {
1981 LoadSDNode *LD = cast<LoadSDNode>(Op);
1982 unsigned ExtType = LD->getExtensionType();
1983 switch (ExtType) {
1984 default: break;
1985 case ISD::SEXTLOAD: // '17' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001986 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 return VTBits-Tmp+1;
1988 case ISD::ZEXTLOAD: // '16' bits known
Duncan Sands83ec4b62008-06-06 12:08:01 +00001989 Tmp = LD->getMemoryVT().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001990 return VTBits-Tmp;
1991 }
1992 }
1993
1994 // Allow the target to implement this method for its nodes.
1995 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1996 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1997 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1998 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1999 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
Dan Gohmana332f172008-05-23 02:28:01 +00002000 if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002001 }
2002
2003 // Finally, if we can prove that the top bits of the result are 0's or 1's,
2004 // use this information.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002005 APInt KnownZero, KnownOne;
2006 APInt Mask = APInt::getAllOnesValue(VTBits);
Dan Gohmanea859be2007-06-22 14:59:07 +00002007 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
2008
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002009 if (KnownZero.isNegative()) { // sign bit is 0
Dan Gohmanea859be2007-06-22 14:59:07 +00002010 Mask = KnownZero;
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002011 } else if (KnownOne.isNegative()) { // sign bit is 1;
Dan Gohmanea859be2007-06-22 14:59:07 +00002012 Mask = KnownOne;
2013 } else {
2014 // Nothing known.
Dan Gohmana332f172008-05-23 02:28:01 +00002015 return FirstAnswer;
Dan Gohmanea859be2007-06-22 14:59:07 +00002016 }
2017
2018 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
2019 // the number of identical bits in the top of the input value.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00002020 Mask = ~Mask;
2021 Mask <<= Mask.getBitWidth()-VTBits;
Dan Gohmanea859be2007-06-22 14:59:07 +00002022 // Return # leading zeros. We use 'min' here in case Val was zero before
2023 // shifting. We don't want to return '64' as for an i32 "0".
Dan Gohmana332f172008-05-23 02:28:01 +00002024 return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
Dan Gohmanea859be2007-06-22 14:59:07 +00002025}
2026
Chris Lattner51dabfb2006-10-14 00:41:01 +00002027
Dan Gohman475871a2008-07-27 21:46:04 +00002028bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
Evan Chenga844bde2008-02-02 04:07:54 +00002029 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
2030 if (!GA) return false;
Dan Gohman6520e202008-10-18 02:06:02 +00002031 if (GA->getOffset() != 0) return false;
Evan Chenga844bde2008-02-02 04:07:54 +00002032 GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
2033 if (!GV) return false;
Devang Patel35fe7342009-01-13 22:54:57 +00002034 MachineModuleInfo *MMI = getMachineModuleInfo();
Devang Patelbbdc8202009-01-13 23:54:55 +00002035 return MMI && MMI->hasDebugInfo();
Evan Chenga844bde2008-02-02 04:07:54 +00002036}
2037
2038
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002039/// getShuffleScalarElt - Returns the scalar element that will make up the ith
2040/// element of the result of the vector shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00002041SDValue SelectionDAG::getShuffleScalarElt(const SDNode *N, unsigned i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002042 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00002043 SDValue PermMask = N->getOperand(2);
2044 SDValue Idx = PermMask.getOperand(i);
Evan Chengab262272008-06-25 20:52:59 +00002045 if (Idx.getOpcode() == ISD::UNDEF)
2046 return getNode(ISD::UNDEF, VT.getVectorElementType());
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002047 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002048 unsigned NumElems = PermMask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue V = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
Evan Chengab262272008-06-25 20:52:59 +00002050 Index %= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002051
2052 if (V.getOpcode() == ISD::BIT_CONVERT) {
2053 V = V.getOperand(0);
Mon P Wange9f10152008-12-09 05:46:39 +00002054 MVT VVT = V.getValueType();
2055 if (!VVT.isVector() || VVT.getVectorNumElements() != NumElems)
Dan Gohman475871a2008-07-27 21:46:04 +00002056 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002057 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002058 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002059 return (Index == 0) ? V.getOperand(0)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002060 : getNode(ISD::UNDEF, VT.getVectorElementType());
Evan Chengf26ffe92008-05-29 08:22:04 +00002061 if (V.getOpcode() == ISD::BUILD_VECTOR)
Evan Chengab262272008-06-25 20:52:59 +00002062 return V.getOperand(Index);
2063 if (V.getOpcode() == ISD::VECTOR_SHUFFLE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002064 return getShuffleScalarElt(V.getNode(), Index);
Dan Gohman475871a2008-07-27 21:46:04 +00002065 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00002066}
2067
2068
Chris Lattnerc3aae252005-01-07 07:46:32 +00002069/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00002070///
Dan Gohman475871a2008-07-27 21:46:04 +00002071SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00002072 FoldingSetNodeID ID;
Dan Gohman6d9cdd52008-07-07 18:26:29 +00002073 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002074 void *IP = 0;
2075 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002076 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002077 SDNode *N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002078 new (N) SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00002079 CSEMap.InsertNode(N, IP);
2080
2081 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002082#ifndef NDEBUG
2083 VerifyNode(N);
2084#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002085 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002086}
2087
Dan Gohman475871a2008-07-27 21:46:04 +00002088SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT, SDValue Operand) {
Chris Lattner94683772005-12-23 05:30:37 +00002089 // Constant fold unary operations with an integer constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002090 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
Dan Gohman6c231502008-02-29 01:47:35 +00002091 const APInt &Val = C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002092 unsigned BitWidth = VT.getSizeInBits();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002093 switch (Opcode) {
2094 default: break;
Evan Chengeb49c4e2008-03-06 17:42:34 +00002095 case ISD::SIGN_EXTEND:
2096 return getConstant(APInt(Val).sextOrTrunc(BitWidth), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00002097 case ISD::ANY_EXTEND:
Dan Gohman6c231502008-02-29 01:47:35 +00002098 case ISD::ZERO_EXTEND:
Evan Chengeb49c4e2008-03-06 17:42:34 +00002099 case ISD::TRUNCATE:
2100 return getConstant(APInt(Val).zextOrTrunc(BitWidth), VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00002101 case ISD::UINT_TO_FP:
2102 case ISD::SINT_TO_FP: {
2103 const uint64_t zero[] = {0, 0};
Dale Johannesendb44bf82007-10-16 23:38:29 +00002104 // No compile time operations on this type.
2105 if (VT==MVT::ppcf128)
2106 break;
Dan Gohman6c231502008-02-29 01:47:35 +00002107 APFloat apf = APFloat(APInt(BitWidth, 2, zero));
2108 (void)apf.convertFromAPInt(Val,
2109 Opcode==ISD::SINT_TO_FP,
2110 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00002111 return getConstantFP(apf, VT);
2112 }
Chris Lattner94683772005-12-23 05:30:37 +00002113 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002114 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Dan Gohman6c231502008-02-29 01:47:35 +00002115 return getConstantFP(Val.bitsToFloat(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00002116 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Dan Gohman6c231502008-02-29 01:47:35 +00002117 return getConstantFP(Val.bitsToDouble(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00002118 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002119 case ISD::BSWAP:
Dan Gohman6c231502008-02-29 01:47:35 +00002120 return getConstant(Val.byteSwap(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002121 case ISD::CTPOP:
Dan Gohman6c231502008-02-29 01:47:35 +00002122 return getConstant(Val.countPopulation(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002123 case ISD::CTLZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002124 return getConstant(Val.countLeadingZeros(), VT);
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002125 case ISD::CTTZ:
Dan Gohman6c231502008-02-29 01:47:35 +00002126 return getConstant(Val.countTrailingZeros(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002127 }
2128 }
2129
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002130 // Constant fold unary operations with a floating point constant operand.
Gabor Greifba36cb52008-08-28 21:40:38 +00002131 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002132 APFloat V = C->getValueAPF(); // make copy
Chris Lattner0bd48932008-01-17 07:00:52 +00002133 if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
Dale Johannesendb44bf82007-10-16 23:38:29 +00002134 switch (Opcode) {
2135 case ISD::FNEG:
2136 V.changeSign();
2137 return getConstantFP(V, VT);
2138 case ISD::FABS:
2139 V.clearSign();
2140 return getConstantFP(V, VT);
2141 case ISD::FP_ROUND:
Dale Johannesen23a98552008-10-09 23:00:39 +00002142 case ISD::FP_EXTEND: {
2143 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002144 // This can return overflow, underflow, or inexact; we don't care.
2145 // FIXME need to be more flexible about rounding mode.
Chris Lattnerec4a5672008-03-05 06:48:13 +00002146 (void)V.convert(*MVTToAPFloatSemantics(VT),
Dale Johannesen23a98552008-10-09 23:00:39 +00002147 APFloat::rmNearestTiesToEven, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002148 return getConstantFP(V, VT);
Dale Johannesen23a98552008-10-09 23:00:39 +00002149 }
Dale Johannesendb44bf82007-10-16 23:38:29 +00002150 case ISD::FP_TO_SINT:
2151 case ISD::FP_TO_UINT: {
2152 integerPart x;
Dale Johannesen23a98552008-10-09 23:00:39 +00002153 bool ignored;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002154 assert(integerPartWidth >= 64);
2155 // FIXME need to be more flexible about rounding mode.
2156 APFloat::opStatus s = V.convertToInteger(&x, 64U,
2157 Opcode==ISD::FP_TO_SINT,
Dale Johannesen23a98552008-10-09 23:00:39 +00002158 APFloat::rmTowardZero, &ignored);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002159 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
2160 break;
2161 return getConstant(x, VT);
2162 }
2163 case ISD::BIT_CONVERT:
2164 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen23a98552008-10-09 23:00:39 +00002165 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesendb44bf82007-10-16 23:38:29 +00002166 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen23a98552008-10-09 23:00:39 +00002167 return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002168 break;
Dale Johannesendb44bf82007-10-16 23:38:29 +00002169 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002170 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002171 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002172
Gabor Greifba36cb52008-08-28 21:40:38 +00002173 unsigned OpOpcode = Operand.getNode()->getOpcode();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002174 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00002175 case ISD::TokenFactor:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002176 case ISD::MERGE_VALUES:
Dan Gohman7f8613e2008-08-14 20:04:46 +00002177 case ISD::CONCAT_VECTORS:
Duncan Sandsaaffa052008-12-01 11:41:29 +00002178 return Operand; // Factor, merge or concat of one node? No need.
Chris Lattner0bd48932008-01-17 07:00:52 +00002179 case ISD::FP_ROUND: assert(0 && "Invalid method to make FP_ROUND node");
Chris Lattnerff33cc42007-04-09 05:23:13 +00002180 case ISD::FP_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002181 assert(VT.isFloatingPoint() &&
2182 Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
Chris Lattner7e2e0332008-01-16 17:59:31 +00002183 if (Operand.getValueType() == VT) return Operand; // noop conversion.
Chris Lattner5d03f212008-03-11 06:21:08 +00002184 if (Operand.getOpcode() == ISD::UNDEF)
2185 return getNode(ISD::UNDEF, VT);
Chris Lattnerff33cc42007-04-09 05:23:13 +00002186 break;
Chris Lattner5d03f212008-03-11 06:21:08 +00002187 case ISD::SIGN_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002188 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002189 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002190 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002191 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002192 && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002193 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
Gabor Greifba36cb52008-08-28 21:40:38 +00002194 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002195 break;
2196 case ISD::ZERO_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002197 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002198 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002199 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002200 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002201 && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00002202 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002203 return getNode(ISD::ZERO_EXTEND, VT, Operand.getNode()->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002204 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00002205 case ISD::ANY_EXTEND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002207 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002208 if (Operand.getValueType() == VT) return Operand; // noop extension
Duncan Sands8e4eb092008-06-08 20:54:56 +00002209 assert(Operand.getValueType().bitsLT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002210 && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00002211 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
2212 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002213 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002214 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002215 case ISD::TRUNCATE:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002216 assert(VT.isInteger() && Operand.getValueType().isInteger() &&
Chris Lattnerff33cc42007-04-09 05:23:13 +00002217 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002218 if (Operand.getValueType() == VT) return Operand; // noop truncate
Duncan Sands8e4eb092008-06-08 20:54:56 +00002219 assert(Operand.getValueType().bitsGT(VT)
Duncan Sandsaf47b112007-10-16 09:56:48 +00002220 && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00002221 if (OpOpcode == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00002222 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00002223 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
2224 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002225 // If the source is smaller than the dest, we still need an extend.
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 if (Operand.getNode()->getOperand(0).getValueType().bitsLT(VT))
2227 return getNode(OpOpcode, VT, Operand.getNode()->getOperand(0));
2228 else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
2229 return getNode(ISD::TRUNCATE, VT, Operand.getNode()->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002230 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002231 return Operand.getNode()->getOperand(0);
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00002232 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002233 break;
Chris Lattner35481892005-12-23 00:16:34 +00002234 case ISD::BIT_CONVERT:
2235 // Basic sanity checking.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002236 assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
Reid Spencera07d5b92006-11-11 20:07:59 +00002237 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00002238 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00002239 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
2240 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00002241 if (OpOpcode == ISD::UNDEF)
2242 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00002243 break;
Chris Lattnerce872152006-03-19 06:31:19 +00002244 case ISD::SCALAR_TO_VECTOR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002245 assert(VT.isVector() && !Operand.getValueType().isVector() &&
2246 VT.getVectorElementType() == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00002247 "Illegal SCALAR_TO_VECTOR node!");
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002248 if (OpOpcode == ISD::UNDEF)
2249 return getNode(ISD::UNDEF, VT);
2250 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
2251 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
2252 isa<ConstantSDNode>(Operand.getOperand(1)) &&
2253 Operand.getConstantOperandVal(1) == 0 &&
2254 Operand.getOperand(0).getValueType() == VT)
2255 return Operand.getOperand(0);
Chris Lattnerce872152006-03-19 06:31:19 +00002256 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00002257 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00002258 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002259 return getNode(ISD::FSUB, VT, Operand.getNode()->getOperand(1),
2260 Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002261 if (OpOpcode == ISD::FNEG) // --X -> X
Gabor Greifba36cb52008-08-28 21:40:38 +00002262 return Operand.getNode()->getOperand(0);
Chris Lattner485df9b2005-04-09 03:02:46 +00002263 break;
2264 case ISD::FABS:
2265 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
Gabor Greifba36cb52008-08-28 21:40:38 +00002266 return getNode(ISD::FABS, VT, Operand.getNode()->getOperand(0));
Chris Lattner485df9b2005-04-09 03:02:46 +00002267 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002268 }
2269
Chris Lattner43247a12005-08-25 19:12:10 +00002270 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002271 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002272 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00002273 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00002274 SDValue Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00002275 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002276 void *IP = 0;
2277 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002278 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002279 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002280 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00002281 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002282 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002283 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002284 new (N) UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00002285 }
Duncan Sandsd038e042008-07-21 10:20:31 +00002286
Chris Lattnerc3aae252005-01-07 07:46:32 +00002287 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002288#ifndef NDEBUG
2289 VerifyNode(N);
2290#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002291 return SDValue(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00002292}
2293
Bill Wendling688d1c42008-09-24 10:16:24 +00002294SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
2295 MVT VT,
2296 ConstantSDNode *Cst1,
2297 ConstantSDNode *Cst2) {
2298 const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
2299
2300 switch (Opcode) {
2301 case ISD::ADD: return getConstant(C1 + C2, VT);
2302 case ISD::SUB: return getConstant(C1 - C2, VT);
2303 case ISD::MUL: return getConstant(C1 * C2, VT);
2304 case ISD::UDIV:
2305 if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
2306 break;
2307 case ISD::UREM:
2308 if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
2309 break;
2310 case ISD::SDIV:
2311 if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
2312 break;
2313 case ISD::SREM:
2314 if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
2315 break;
2316 case ISD::AND: return getConstant(C1 & C2, VT);
2317 case ISD::OR: return getConstant(C1 | C2, VT);
2318 case ISD::XOR: return getConstant(C1 ^ C2, VT);
2319 case ISD::SHL: return getConstant(C1 << C2, VT);
2320 case ISD::SRL: return getConstant(C1.lshr(C2), VT);
2321 case ISD::SRA: return getConstant(C1.ashr(C2), VT);
2322 case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
2323 case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
2324 default: break;
2325 }
2326
2327 return SDValue();
2328}
2329
Dan Gohman475871a2008-07-27 21:46:04 +00002330SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2331 SDValue N1, SDValue N2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002332 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2333 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattner76365122005-01-16 02:23:22 +00002334 switch (Opcode) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002335 default: break;
Chris Lattner39908e02005-01-19 18:01:40 +00002336 case ISD::TokenFactor:
2337 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
2338 N2.getValueType() == MVT::Other && "Invalid token factor!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002339 // Fold trivial token factors.
2340 if (N1.getOpcode() == ISD::EntryToken) return N2;
2341 if (N2.getOpcode() == ISD::EntryToken) return N1;
Dan Gohman38ac0622008-10-01 15:11:19 +00002342 if (N1 == N2) return N1;
Chris Lattner39908e02005-01-19 18:01:40 +00002343 break;
Dan Gohman7f8613e2008-08-14 20:04:46 +00002344 case ISD::CONCAT_VECTORS:
2345 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2346 // one big BUILD_VECTOR.
2347 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2348 N2.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002349 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2350 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002351 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2352 }
2353 break;
Chris Lattner76365122005-01-16 02:23:22 +00002354 case ISD::AND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002355 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002356 N1.getValueType() == VT && "Binary operator types must match!");
2357 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2358 // worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002359 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002360 return N2;
Chris Lattner9967c152008-01-26 01:05:42 +00002361 if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
2362 return N1;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002363 break;
Chris Lattner76365122005-01-16 02:23:22 +00002364 case ISD::OR:
2365 case ISD::XOR:
Dan Gohman33b625b2008-06-02 22:27:05 +00002366 case ISD::ADD:
2367 case ISD::SUB:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002368 assert(VT.isInteger() && N1.getValueType() == N2.getValueType() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002369 N1.getValueType() == VT && "Binary operator types must match!");
Dan Gohman33b625b2008-06-02 22:27:05 +00002370 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
2371 // it's worth handling here.
Dan Gohman002e5d02008-03-13 22:13:53 +00002372 if (N2C && N2C->isNullValue())
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002373 return N1;
2374 break;
Chris Lattner76365122005-01-16 02:23:22 +00002375 case ISD::UDIV:
2376 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00002377 case ISD::MULHU:
2378 case ISD::MULHS:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002379 assert(VT.isInteger() && "This operator does not apply to FP types!");
Chris Lattner76365122005-01-16 02:23:22 +00002380 // fall through
Chris Lattner76365122005-01-16 02:23:22 +00002381 case ISD::MUL:
2382 case ISD::SDIV:
2383 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00002384 case ISD::FADD:
2385 case ISD::FSUB:
2386 case ISD::FMUL:
2387 case ISD::FDIV:
2388 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00002389 assert(N1.getValueType() == N2.getValueType() &&
2390 N1.getValueType() == VT && "Binary operator types must match!");
2391 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00002392 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
2393 assert(N1.getValueType() == VT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002394 N1.getValueType().isFloatingPoint() &&
2395 N2.getValueType().isFloatingPoint() &&
Chris Lattnera09f8482006-03-05 05:09:38 +00002396 "Invalid FCOPYSIGN!");
2397 break;
Chris Lattner76365122005-01-16 02:23:22 +00002398 case ISD::SHL:
2399 case ISD::SRA:
2400 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00002401 case ISD::ROTL:
2402 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00002403 assert(VT == N1.getValueType() &&
2404 "Shift operators return type must be the same as their first arg");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002405 assert(VT.isInteger() && N2.getValueType().isInteger() &&
Chris Lattner349db172008-07-02 17:01:57 +00002406 "Shifts only work on integers");
Mon P Wange9f10152008-12-09 05:46:39 +00002407 assert((N2.getValueType() == TLI.getShiftAmountTy() ||
2408 (N2.getValueType().isVector() && N2.getValueType().isInteger())) &&
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002409 "Wrong type for shift amount");
Chris Lattner349db172008-07-02 17:01:57 +00002410
2411 // Always fold shifts of i1 values so the code generator doesn't need to
2412 // handle them. Since we know the size of the shift has to be less than the
2413 // size of the value, the shift/rotate count is guaranteed to be zero.
2414 if (VT == MVT::i1)
2415 return N1;
Chris Lattner76365122005-01-16 02:23:22 +00002416 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002417 case ISD::FP_ROUND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002418 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002419 assert(VT == N1.getValueType() && "Not an inreg round!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002420 assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002421 "Cannot FP_ROUND_INREG integer types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002422 assert(EVT.bitsLE(VT) && "Not rounding down!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002423 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
Chris Lattner15e4b012005-07-10 00:07:11 +00002424 break;
2425 }
Chris Lattner0bd48932008-01-17 07:00:52 +00002426 case ISD::FP_ROUND:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002427 assert(VT.isFloatingPoint() &&
2428 N1.getValueType().isFloatingPoint() &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00002429 VT.bitsLE(N1.getValueType()) &&
Chris Lattner0bd48932008-01-17 07:00:52 +00002430 isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002431 if (N1.getValueType() == VT) return N1; // noop conversion.
Chris Lattner0bd48932008-01-17 07:00:52 +00002432 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00002433 case ISD::AssertSext:
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002434 case ISD::AssertZext: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002435 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002436 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002437 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002438 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002439 assert(EVT.bitsLE(VT) && "Not extending!");
Duncan Sandsd885dbd2008-02-10 10:08:52 +00002440 if (VT == EVT) return N1; // noop assertion.
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002441 break;
2442 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002443 case ISD::SIGN_EXTEND_INREG: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002444 MVT EVT = cast<VTSDNode>(N2)->getVT();
Chris Lattner15e4b012005-07-10 00:07:11 +00002445 assert(VT == N1.getValueType() && "Not an inreg extend!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002446 assert(VT.isInteger() && EVT.isInteger() &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002447 "Cannot *_EXTEND_INREG FP types");
Duncan Sands8e4eb092008-06-08 20:54:56 +00002448 assert(EVT.bitsLE(VT) && "Not extending!");
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002449 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002450
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002451 if (N1C) {
Dan Gohman6c231502008-02-29 01:47:35 +00002452 APInt Val = N1C->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002453 unsigned FromBits = cast<VTSDNode>(N2)->getVT().getSizeInBits();
Dan Gohman6c231502008-02-29 01:47:35 +00002454 Val <<= Val.getBitWidth()-FromBits;
Evan Cheng433f6f62008-03-06 08:20:51 +00002455 Val = Val.ashr(Val.getBitWidth()-FromBits);
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002456 return getConstant(Val, VT);
2457 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002458 break;
2459 }
2460 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002461 // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
2462 if (N1.getOpcode() == ISD::UNDEF)
2463 return getNode(ISD::UNDEF, VT);
2464
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002465 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
2466 // expanding copies of large vectors from registers.
Dan Gohman6ab64222008-08-13 21:51:37 +00002467 if (N2C &&
2468 N1.getOpcode() == ISD::CONCAT_VECTORS &&
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002469 N1.getNumOperands() > 0) {
2470 unsigned Factor =
Duncan Sands83ec4b62008-06-06 12:08:01 +00002471 N1.getOperand(0).getValueType().getVectorNumElements();
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002472 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002473 N1.getOperand(N2C->getZExtValue() / Factor),
2474 getConstant(N2C->getZExtValue() % Factor,
2475 N2.getValueType()));
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002476 }
2477
2478 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2479 // expanding large vector constants.
Dan Gohman6ab64222008-08-13 21:51:37 +00002480 if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002481 return N1.getOperand(N2C->getZExtValue());
Chris Lattnerf3ba4342008-03-08 23:43:36 +00002482
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002483 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2484 // operations are lowered to scalars.
Dan Gohman6ab64222008-08-13 21:51:37 +00002485 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
2486 if (N1.getOperand(2) == N2)
2487 return N1.getOperand(1);
2488 else
2489 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2490 }
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002491 break;
2492 case ISD::EXTRACT_ELEMENT:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002493 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
Duncan Sands041cde22008-06-25 20:24:48 +00002494 assert(!N1.getValueType().isVector() && !VT.isVector() &&
2495 (N1.getValueType().isInteger() == VT.isInteger()) &&
2496 "Wrong types for EXTRACT_ELEMENT!");
Duncan Sands25eb0432008-03-12 20:30:08 +00002497
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002498 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2499 // 64-bit integers into 32-bit parts. Instead of building the extract of
2500 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
2501 if (N1.getOpcode() == ISD::BUILD_PAIR)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002502 return N1.getOperand(N2C->getZExtValue());
Duncan Sands25eb0432008-03-12 20:30:08 +00002503
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002504 // EXTRACT_ELEMENT of a constant int is also very common.
2505 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002506 unsigned ElementSize = VT.getSizeInBits();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002507 unsigned Shift = ElementSize * N2C->getZExtValue();
Dan Gohman4c931fc2008-03-24 16:38:05 +00002508 APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
2509 return getConstant(ShiftedVal.trunc(ElementSize), VT);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002510 }
2511 break;
Duncan Sandsf83b1f62008-02-20 17:38:09 +00002512 case ISD::EXTRACT_SUBVECTOR:
2513 if (N1.getValueType() == VT) // Trivial extraction.
2514 return N1;
2515 break;
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002516 }
2517
2518 if (N1C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002519 if (N2C) {
Bill Wendling688d1c42008-09-24 10:16:24 +00002520 SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
2521 if (SV.getNode()) return SV;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002522 } else { // Cannonicalize constant to RHS if commutative
2523 if (isCommutativeBinOp(Opcode)) {
2524 std::swap(N1C, N2C);
2525 std::swap(N1, N2);
2526 }
2527 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002528 }
2529
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002530 // Constant fold FP operations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002531 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
2532 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
Chris Lattner15e4b012005-07-10 00:07:11 +00002533 if (N1CFP) {
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002534 if (!N2CFP && isCommutativeBinOp(Opcode)) {
2535 // Cannonicalize constant to RHS if commutative
2536 std::swap(N1CFP, N2CFP);
2537 std::swap(N1, N2);
2538 } else if (N2CFP && VT != MVT::ppcf128) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002539 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
2540 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002541 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002542 case ISD::FADD:
2543 s = V1.add(V2, APFloat::rmNearestTiesToEven);
Chris Lattner8e1f7ac2008-01-22 19:09:33 +00002544 if (s != APFloat::opInvalidOp)
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002545 return getConstantFP(V1, VT);
2546 break;
2547 case ISD::FSUB:
2548 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
2549 if (s!=APFloat::opInvalidOp)
2550 return getConstantFP(V1, VT);
2551 break;
2552 case ISD::FMUL:
2553 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
2554 if (s!=APFloat::opInvalidOp)
2555 return getConstantFP(V1, VT);
2556 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002557 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002558 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
2559 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2560 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002561 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002562 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002563 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
2564 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
2565 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002566 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00002567 case ISD::FCOPYSIGN:
2568 V1.copySign(V2);
2569 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002570 default: break;
2571 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002572 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002573 }
Chris Lattner62b57722006-04-20 05:39:12 +00002574
2575 // Canonicalize an UNDEF to the RHS, even over a constant.
2576 if (N1.getOpcode() == ISD::UNDEF) {
2577 if (isCommutativeBinOp(Opcode)) {
2578 std::swap(N1, N2);
2579 } else {
2580 switch (Opcode) {
2581 case ISD::FP_ROUND_INREG:
2582 case ISD::SIGN_EXTEND_INREG:
2583 case ISD::SUB:
2584 case ISD::FSUB:
2585 case ISD::FDIV:
2586 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002587 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002588 return N1; // fold op(undef, arg2) -> undef
2589 case ISD::UDIV:
2590 case ISD::SDIV:
2591 case ISD::UREM:
2592 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002593 case ISD::SRL:
2594 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002595 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002596 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2597 // For vectors, we can't easily build an all zero vector, just return
2598 // the LHS.
2599 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002600 }
2601 }
2602 }
2603
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002604 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002605 if (N2.getOpcode() == ISD::UNDEF) {
2606 switch (Opcode) {
Evan Cheng26471c42008-03-25 20:08:07 +00002607 case ISD::XOR:
2608 if (N1.getOpcode() == ISD::UNDEF)
2609 // Handle undef ^ undef -> 0 special case. This is a common
2610 // idiom (misuse).
2611 return getConstant(0, VT);
2612 // fallthrough
Chris Lattner62b57722006-04-20 05:39:12 +00002613 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002614 case ISD::ADDC:
2615 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002616 case ISD::SUB:
2617 case ISD::FADD:
2618 case ISD::FSUB:
2619 case ISD::FMUL:
2620 case ISD::FDIV:
2621 case ISD::FREM:
2622 case ISD::UDIV:
2623 case ISD::SDIV:
2624 case ISD::UREM:
2625 case ISD::SREM:
Chris Lattner62b57722006-04-20 05:39:12 +00002626 return N2; // fold op(arg1, undef) -> undef
2627 case ISD::MUL:
2628 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002629 case ISD::SRL:
2630 case ISD::SHL:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002631 if (!VT.isVector())
Chris Lattner964dd862007-04-25 00:00:45 +00002632 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2633 // For vectors, we can't easily build an all zero vector, just return
2634 // the LHS.
2635 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002636 case ISD::OR:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002637 if (!VT.isVector())
2638 return getConstant(VT.getIntegerVTBitMask(), VT);
Chris Lattner964dd862007-04-25 00:00:45 +00002639 // For vectors, we can't easily build an all one vector, just return
2640 // the LHS.
2641 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002642 case ISD::SRA:
2643 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002644 }
2645 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002646
Chris Lattner27e9b412005-05-11 18:57:39 +00002647 // Memoize this node if possible.
2648 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002649 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002650 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SDValue Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002652 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002653 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002654 void *IP = 0;
2655 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002656 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002657 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002658 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002659 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002660 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002661 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002662 new (N) BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002663 }
2664
Chris Lattnerc3aae252005-01-07 07:46:32 +00002665 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002666#ifndef NDEBUG
2667 VerifyNode(N);
2668#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002669 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002670}
2671
Dan Gohman475871a2008-07-27 21:46:04 +00002672SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2673 SDValue N1, SDValue N2, SDValue N3) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002674 // Perform various simplifications.
Gabor Greifba36cb52008-08-28 21:40:38 +00002675 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
2676 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002677 switch (Opcode) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00002678 case ISD::CONCAT_VECTORS:
2679 // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
2680 // one big BUILD_VECTOR.
2681 if (N1.getOpcode() == ISD::BUILD_VECTOR &&
2682 N2.getOpcode() == ISD::BUILD_VECTOR &&
2683 N3.getOpcode() == ISD::BUILD_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002684 SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(), N1.getNode()->op_end());
2685 Elts.insert(Elts.end(), N2.getNode()->op_begin(), N2.getNode()->op_end());
2686 Elts.insert(Elts.end(), N3.getNode()->op_begin(), N3.getNode()->op_end());
Dan Gohman7f8613e2008-08-14 20:04:46 +00002687 return getNode(ISD::BUILD_VECTOR, VT, &Elts[0], Elts.size());
2688 }
2689 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002690 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002691 // Use FoldSetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00002692 SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Gabor Greifba36cb52008-08-28 21:40:38 +00002693 if (Simp.getNode()) return Simp;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002694 break;
2695 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002696 case ISD::SELECT:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002697 if (N1C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002698 if (N1C->getZExtValue())
Chris Lattnerc3aae252005-01-07 07:46:32 +00002699 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002700 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002701 return N3; // select false, X, Y -> Y
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002702 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002703
2704 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002705 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002706 case ISD::BRCOND:
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002707 if (N2C) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002708 if (N2C->getZExtValue()) // Unconditional branch
Chris Lattner5351e9b2005-01-07 22:49:57 +00002709 return getNode(ISD::BR, MVT::Other, N1, N3);
2710 else
2711 return N1; // Never-taken branch
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002712 }
Chris Lattner7c68ec62005-01-07 23:32:00 +00002713 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002714 case ISD::VECTOR_SHUFFLE:
Mon P Wangaeb06d22008-11-10 04:46:22 +00002715 assert(N1.getValueType() == N2.getValueType() &&
2716 N1.getValueType().isVector() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002717 VT.isVector() && N3.getValueType().isVector() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002718 N3.getOpcode() == ISD::BUILD_VECTOR &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002719 VT.getVectorNumElements() == N3.getNumOperands() &&
Chris Lattnerfb194b92006-03-19 23:56:04 +00002720 "Illegal VECTOR_SHUFFLE node!");
2721 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002722 case ISD::BIT_CONVERT:
2723 // Fold bit_convert nodes from a type to themselves.
2724 if (N1.getValueType() == VT)
2725 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002726 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002727 }
2728
Chris Lattner43247a12005-08-25 19:12:10 +00002729 // Memoize node if it doesn't produce a flag.
2730 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002731 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002732 if (VT != MVT::Flag) {
Dan Gohman475871a2008-07-27 21:46:04 +00002733 SDValue Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002734 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002735 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002736 void *IP = 0;
2737 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00002738 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00002739 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002740 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002741 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002742 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00002743 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00002744 new (N) TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002745 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002746 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00002747#ifndef NDEBUG
2748 VerifyNode(N);
2749#endif
Dan Gohman475871a2008-07-27 21:46:04 +00002750 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002751}
2752
Dan Gohman475871a2008-07-27 21:46:04 +00002753SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2754 SDValue N1, SDValue N2, SDValue N3,
2755 SDValue N4) {
2756 SDValue Ops[] = { N1, N2, N3, N4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002757 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002758}
2759
Dan Gohman475871a2008-07-27 21:46:04 +00002760SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
2761 SDValue N1, SDValue N2, SDValue N3,
2762 SDValue N4, SDValue N5) {
2763 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002764 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002765}
2766
Dan Gohman707e0182008-04-12 04:36:06 +00002767/// getMemsetValue - Vectorized representation of the memset value
2768/// operand.
Dan Gohman475871a2008-07-27 21:46:04 +00002769static SDValue getMemsetValue(SDValue Value, MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002770 unsigned NumBits = VT.isVector() ?
2771 VT.getVectorElementType().getSizeInBits() : VT.getSizeInBits();
Dan Gohman707e0182008-04-12 04:36:06 +00002772 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002773 APInt Val = APInt(NumBits, C->getZExtValue() & 255);
Dan Gohman707e0182008-04-12 04:36:06 +00002774 unsigned Shift = 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002775 for (unsigned i = NumBits; i > 8; i >>= 1) {
Dan Gohman707e0182008-04-12 04:36:06 +00002776 Val = (Val << Shift) | Val;
2777 Shift <<= 1;
Dan Gohman707e0182008-04-12 04:36:06 +00002778 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002779 if (VT.isInteger())
Evan Chengf0df0312008-05-15 08:39:06 +00002780 return DAG.getConstant(Val, VT);
2781 return DAG.getConstantFP(APFloat(Val), VT);
Dan Gohman707e0182008-04-12 04:36:06 +00002782 }
Evan Chengf0df0312008-05-15 08:39:06 +00002783
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002784 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Evan Chengf0df0312008-05-15 08:39:06 +00002785 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
2786 unsigned Shift = 8;
2787 for (unsigned i = NumBits; i > 8; i >>= 1) {
2788 Value = DAG.getNode(ISD::OR, VT,
2789 DAG.getNode(ISD::SHL, VT, Value,
Duncan Sands3d0f5af2008-10-30 20:26:50 +00002790 DAG.getConstant(Shift,
2791 TLI.getShiftAmountTy())),
2792 Value);
Evan Chengf0df0312008-05-15 08:39:06 +00002793 Shift <<= 1;
2794 }
2795
2796 return Value;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002797}
2798
Dan Gohman707e0182008-04-12 04:36:06 +00002799/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2800/// used when a memcpy is turned into a memset when the source is a constant
2801/// string ptr.
Dan Gohman475871a2008-07-27 21:46:04 +00002802static SDValue getMemsetStringVal(MVT VT, SelectionDAG &DAG,
Dan Gohman707e0182008-04-12 04:36:06 +00002803 const TargetLowering &TLI,
2804 std::string &Str, unsigned Offset) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002805 // Handle vector with all elements zero.
2806 if (Str.empty()) {
2807 if (VT.isInteger())
2808 return DAG.getConstant(0, VT);
2809 unsigned NumElts = VT.getVectorNumElements();
2810 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
2811 return DAG.getNode(ISD::BIT_CONVERT, VT,
2812 DAG.getConstant(0, MVT::getVectorVT(EltVT, NumElts)));
2813 }
2814
Duncan Sands83ec4b62008-06-06 12:08:01 +00002815 assert(!VT.isVector() && "Can't handle vector type here!");
2816 unsigned NumBits = VT.getSizeInBits();
Evan Chengf0df0312008-05-15 08:39:06 +00002817 unsigned MSB = NumBits / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002818 uint64_t Val = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002819 if (TLI.isLittleEndian())
2820 Offset = Offset + MSB - 1;
2821 for (unsigned i = 0; i != MSB; ++i) {
2822 Val = (Val << 8) | (unsigned char)Str[Offset];
2823 Offset += TLI.isLittleEndian() ? -1 : 1;
2824 }
2825 return DAG.getConstant(Val, VT);
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00002826}
2827
Dan Gohman707e0182008-04-12 04:36:06 +00002828/// getMemBasePlusOffset - Returns base and offset node for the
Evan Chengf0df0312008-05-15 08:39:06 +00002829///
Dan Gohman475871a2008-07-27 21:46:04 +00002830static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
Dan Gohman707e0182008-04-12 04:36:06 +00002831 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002832 MVT VT = Base.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00002833 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2834}
2835
Evan Chengf0df0312008-05-15 08:39:06 +00002836/// isMemSrcFromString - Returns true if memcpy source is a string constant.
2837///
Dan Gohman475871a2008-07-27 21:46:04 +00002838static bool isMemSrcFromString(SDValue Src, std::string &Str) {
Evan Chengf0df0312008-05-15 08:39:06 +00002839 unsigned SrcDelta = 0;
2840 GlobalAddressSDNode *G = NULL;
2841 if (Src.getOpcode() == ISD::GlobalAddress)
2842 G = cast<GlobalAddressSDNode>(Src);
2843 else if (Src.getOpcode() == ISD::ADD &&
2844 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2845 Src.getOperand(1).getOpcode() == ISD::Constant) {
2846 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002847 SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
Evan Chengf0df0312008-05-15 08:39:06 +00002848 }
2849 if (!G)
2850 return false;
Dan Gohman707e0182008-04-12 04:36:06 +00002851
Evan Chengf0df0312008-05-15 08:39:06 +00002852 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Cheng0ff39b32008-06-30 07:31:25 +00002853 if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
2854 return true;
Dan Gohman707e0182008-04-12 04:36:06 +00002855
Evan Chengf0df0312008-05-15 08:39:06 +00002856 return false;
2857}
Dan Gohman707e0182008-04-12 04:36:06 +00002858
Evan Chengf0df0312008-05-15 08:39:06 +00002859/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
2860/// to replace the memset / memcpy is below the threshold. It also returns the
2861/// types of the sequence of memory ops to perform memset / memcpy.
2862static
Duncan Sands83ec4b62008-06-06 12:08:01 +00002863bool MeetsMaxMemopRequirement(std::vector<MVT> &MemOps,
Dan Gohman475871a2008-07-27 21:46:04 +00002864 SDValue Dst, SDValue Src,
Evan Chengf0df0312008-05-15 08:39:06 +00002865 unsigned Limit, uint64_t Size, unsigned &Align,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002866 std::string &Str, bool &isSrcStr,
Evan Chengf0df0312008-05-15 08:39:06 +00002867 SelectionDAG &DAG,
2868 const TargetLowering &TLI) {
Evan Cheng0ff39b32008-06-30 07:31:25 +00002869 isSrcStr = isMemSrcFromString(Src, Str);
Evan Chengf0df0312008-05-15 08:39:06 +00002870 bool isSrcConst = isa<ConstantSDNode>(Src);
Evan Cheng0ff39b32008-06-30 07:31:25 +00002871 bool AllowUnalign = TLI.allowsUnalignedMemoryAccesses();
Chris Lattner7fe5e182008-10-28 07:10:51 +00002872 MVT VT = TLI.getOptimalMemOpType(Size, Align, isSrcConst, isSrcStr);
Evan Chengf0df0312008-05-15 08:39:06 +00002873 if (VT != MVT::iAny) {
2874 unsigned NewAlign = (unsigned)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002875 TLI.getTargetData()->getABITypeAlignment(VT.getTypeForMVT());
Evan Chengf0df0312008-05-15 08:39:06 +00002876 // If source is a string constant, this will require an unaligned load.
2877 if (NewAlign > Align && (isSrcConst || AllowUnalign)) {
2878 if (Dst.getOpcode() != ISD::FrameIndex) {
2879 // Can't change destination alignment. It requires a unaligned store.
2880 if (AllowUnalign)
2881 VT = MVT::iAny;
2882 } else {
2883 int FI = cast<FrameIndexSDNode>(Dst)->getIndex();
2884 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2885 if (MFI->isFixedObjectIndex(FI)) {
2886 // Can't change destination alignment. It requires a unaligned store.
2887 if (AllowUnalign)
2888 VT = MVT::iAny;
2889 } else {
Evan Chengfb4db312008-06-04 23:37:54 +00002890 // Give the stack frame object a larger alignment if needed.
2891 if (MFI->getObjectAlignment(FI) < NewAlign)
2892 MFI->setObjectAlignment(FI, NewAlign);
Evan Chengf0df0312008-05-15 08:39:06 +00002893 Align = NewAlign;
2894 }
2895 }
2896 }
2897 }
2898
2899 if (VT == MVT::iAny) {
2900 if (AllowUnalign) {
2901 VT = MVT::i64;
2902 } else {
2903 switch (Align & 7) {
2904 case 0: VT = MVT::i64; break;
2905 case 4: VT = MVT::i32; break;
2906 case 2: VT = MVT::i16; break;
2907 default: VT = MVT::i8; break;
2908 }
2909 }
2910
Duncan Sands83ec4b62008-06-06 12:08:01 +00002911 MVT LVT = MVT::i64;
Evan Chengf0df0312008-05-15 08:39:06 +00002912 while (!TLI.isTypeLegal(LVT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002913 LVT = (MVT::SimpleValueType)(LVT.getSimpleVT() - 1);
2914 assert(LVT.isInteger());
Evan Chengf0df0312008-05-15 08:39:06 +00002915
Duncan Sands8e4eb092008-06-08 20:54:56 +00002916 if (VT.bitsGT(LVT))
Evan Chengf0df0312008-05-15 08:39:06 +00002917 VT = LVT;
2918 }
Dan Gohman707e0182008-04-12 04:36:06 +00002919
2920 unsigned NumMemOps = 0;
2921 while (Size != 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002922 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00002923 while (VTSize > Size) {
Evan Chengf0df0312008-05-15 08:39:06 +00002924 // For now, only use non-vector load / store's for the left-over pieces.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002925 if (VT.isVector()) {
Evan Chengf0df0312008-05-15 08:39:06 +00002926 VT = MVT::i64;
2927 while (!TLI.isTypeLegal(VT))
Duncan Sands83ec4b62008-06-06 12:08:01 +00002928 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
2929 VTSize = VT.getSizeInBits() / 8;
Evan Chengf0df0312008-05-15 08:39:06 +00002930 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002931 VT = (MVT::SimpleValueType)(VT.getSimpleVT() - 1);
Evan Chengf0df0312008-05-15 08:39:06 +00002932 VTSize >>= 1;
2933 }
Dan Gohman707e0182008-04-12 04:36:06 +00002934 }
Dan Gohman707e0182008-04-12 04:36:06 +00002935
2936 if (++NumMemOps > Limit)
2937 return false;
2938 MemOps.push_back(VT);
2939 Size -= VTSize;
2940 }
2941
2942 return true;
2943}
2944
Dan Gohman475871a2008-07-27 21:46:04 +00002945static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG,
2946 SDValue Chain, SDValue Dst,
2947 SDValue Src, uint64_t Size,
Evan Chengf0df0312008-05-15 08:39:06 +00002948 unsigned Align, bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00002949 const Value *DstSV, uint64_t DstSVOff,
2950 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00002951 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2952
Dan Gohman21323f32008-05-29 19:42:22 +00002953 // Expand memcpy to a series of load and store ops if the size operand falls
2954 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002955 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00002956 uint64_t Limit = -1ULL;
Dan Gohman707e0182008-04-12 04:36:06 +00002957 if (!AlwaysInline)
2958 Limit = TLI.getMaxStoresPerMemcpy();
Evan Chengf0df0312008-05-15 08:39:06 +00002959 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002960 std::string Str;
2961 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00002962 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00002963 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00002964 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00002965
Dan Gohman707e0182008-04-12 04:36:06 +00002966
Evan Cheng0ff39b32008-06-30 07:31:25 +00002967 bool isZeroStr = CopyFromStr && Str.empty();
Dan Gohman475871a2008-07-27 21:46:04 +00002968 SmallVector<SDValue, 8> OutChains;
Evan Chengf0df0312008-05-15 08:39:06 +00002969 unsigned NumMemOps = MemOps.size();
Evan Cheng0ff39b32008-06-30 07:31:25 +00002970 uint64_t SrcOff = 0, DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00002971 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002972 MVT VT = MemOps[i];
2973 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00002974 SDValue Value, Store;
Dan Gohman707e0182008-04-12 04:36:06 +00002975
Evan Cheng0ff39b32008-06-30 07:31:25 +00002976 if (CopyFromStr && (isZeroStr || !VT.isVector())) {
Evan Chengf0df0312008-05-15 08:39:06 +00002977 // It's unlikely a store of a vector immediate can be done in a single
2978 // instruction. It would require a load from a constantpool first.
Evan Cheng0ff39b32008-06-30 07:31:25 +00002979 // We also handle store a vector with all zero's.
2980 // FIXME: Handle other cases where store of vector immediate is done in
2981 // a single instruction.
Dan Gohman707e0182008-04-12 04:36:06 +00002982 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
Evan Chengf0df0312008-05-15 08:39:06 +00002983 Store = DAG.getStore(Chain, Value,
2984 getMemBasePlusOffset(Dst, DstOff, DAG),
Evan Cheng1afe5c32008-07-09 06:38:06 +00002985 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002986 } else {
2987 Value = DAG.getLoad(VT, Chain,
2988 getMemBasePlusOffset(Src, SrcOff, DAG),
Dan Gohman1f13c682008-04-28 17:15:20 +00002989 SrcSV, SrcSVOff + SrcOff, false, Align);
Evan Chengf0df0312008-05-15 08:39:06 +00002990 Store = DAG.getStore(Chain, Value,
2991 getMemBasePlusOffset(Dst, DstOff, DAG),
2992 DstSV, DstSVOff + DstOff, false, DstAlign);
Dan Gohman707e0182008-04-12 04:36:06 +00002993 }
2994 OutChains.push_back(Store);
2995 SrcOff += VTSize;
2996 DstOff += VTSize;
2997 }
2998
2999 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3000 &OutChains[0], OutChains.size());
3001}
3002
Dan Gohman475871a2008-07-27 21:46:04 +00003003static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG,
3004 SDValue Chain, SDValue Dst,
3005 SDValue Src, uint64_t Size,
Dan Gohman21323f32008-05-29 19:42:22 +00003006 unsigned Align, bool AlwaysInline,
3007 const Value *DstSV, uint64_t DstSVOff,
3008 const Value *SrcSV, uint64_t SrcSVOff){
3009 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3010
3011 // Expand memmove to a series of load and store ops if the size operand falls
3012 // below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003013 std::vector<MVT> MemOps;
Dan Gohmanca0a5d92008-10-03 17:56:45 +00003014 uint64_t Limit = -1ULL;
Dan Gohman21323f32008-05-29 19:42:22 +00003015 if (!AlwaysInline)
3016 Limit = TLI.getMaxStoresPerMemmove();
3017 unsigned DstAlign = Align; // Destination alignment can change.
Evan Cheng0ff39b32008-06-30 07:31:25 +00003018 std::string Str;
3019 bool CopyFromStr;
Dan Gohman21323f32008-05-29 19:42:22 +00003020 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, Limit, Size, DstAlign,
Evan Cheng0ff39b32008-06-30 07:31:25 +00003021 Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003022 return SDValue();
Dan Gohman21323f32008-05-29 19:42:22 +00003023
Dan Gohman21323f32008-05-29 19:42:22 +00003024 uint64_t SrcOff = 0, DstOff = 0;
3025
Dan Gohman475871a2008-07-27 21:46:04 +00003026 SmallVector<SDValue, 8> LoadValues;
3027 SmallVector<SDValue, 8> LoadChains;
3028 SmallVector<SDValue, 8> OutChains;
Dan Gohman21323f32008-05-29 19:42:22 +00003029 unsigned NumMemOps = MemOps.size();
3030 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003031 MVT VT = MemOps[i];
3032 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003033 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003034
3035 Value = DAG.getLoad(VT, Chain,
3036 getMemBasePlusOffset(Src, SrcOff, DAG),
3037 SrcSV, SrcSVOff + SrcOff, false, Align);
3038 LoadValues.push_back(Value);
3039 LoadChains.push_back(Value.getValue(1));
3040 SrcOff += VTSize;
3041 }
3042 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
3043 &LoadChains[0], LoadChains.size());
3044 OutChains.clear();
3045 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003046 MVT VT = MemOps[i];
3047 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue Value, Store;
Dan Gohman21323f32008-05-29 19:42:22 +00003049
3050 Store = DAG.getStore(Chain, LoadValues[i],
3051 getMemBasePlusOffset(Dst, DstOff, DAG),
3052 DstSV, DstSVOff + DstOff, false, DstAlign);
3053 OutChains.push_back(Store);
3054 DstOff += VTSize;
3055 }
3056
3057 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3058 &OutChains[0], OutChains.size());
3059}
3060
Dan Gohman475871a2008-07-27 21:46:04 +00003061static SDValue getMemsetStores(SelectionDAG &DAG,
3062 SDValue Chain, SDValue Dst,
3063 SDValue Src, uint64_t Size,
Dan Gohman707e0182008-04-12 04:36:06 +00003064 unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003065 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003066 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3067
3068 // Expand memset to a series of load/store ops if the size operand
3069 // falls below a certain threshold.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003070 std::vector<MVT> MemOps;
Evan Cheng0ff39b32008-06-30 07:31:25 +00003071 std::string Str;
3072 bool CopyFromStr;
Evan Chengf0df0312008-05-15 08:39:06 +00003073 if (!MeetsMaxMemopRequirement(MemOps, Dst, Src, TLI.getMaxStoresPerMemset(),
Evan Cheng0ff39b32008-06-30 07:31:25 +00003074 Size, Align, Str, CopyFromStr, DAG, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00003075 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00003076
Dan Gohman475871a2008-07-27 21:46:04 +00003077 SmallVector<SDValue, 8> OutChains;
Dan Gohman1f13c682008-04-28 17:15:20 +00003078 uint64_t DstOff = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00003079
3080 unsigned NumMemOps = MemOps.size();
3081 for (unsigned i = 0; i < NumMemOps; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003082 MVT VT = MemOps[i];
3083 unsigned VTSize = VT.getSizeInBits() / 8;
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SDValue Value = getMemsetValue(Src, VT, DAG);
3085 SDValue Store = DAG.getStore(Chain, Value,
Chris Lattner7fe5e182008-10-28 07:10:51 +00003086 getMemBasePlusOffset(Dst, DstOff, DAG),
3087 DstSV, DstSVOff + DstOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003088 OutChains.push_back(Store);
3089 DstOff += VTSize;
3090 }
3091
3092 return DAG.getNode(ISD::TokenFactor, MVT::Other,
3093 &OutChains[0], OutChains.size());
3094}
3095
Dan Gohman475871a2008-07-27 21:46:04 +00003096SDValue SelectionDAG::getMemcpy(SDValue Chain, SDValue Dst,
3097 SDValue Src, SDValue Size,
3098 unsigned Align, bool AlwaysInline,
3099 const Value *DstSV, uint64_t DstSVOff,
3100 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003101
3102 // Check to see if we should lower the memcpy to loads and stores first.
3103 // For cases within the target-specified limits, this is the best choice.
3104 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3105 if (ConstantSize) {
3106 // Memcpy with size zero? Just return the original chain.
3107 if (ConstantSize->isNullValue())
3108 return Chain;
3109
Dan Gohman475871a2008-07-27 21:46:04 +00003110 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003111 getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
3112 ConstantSize->getZExtValue(),
Dan Gohman1f13c682008-04-28 17:15:20 +00003113 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003114 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003115 return Result;
3116 }
3117
3118 // Then check to see if we should lower the memcpy with target-specific
3119 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003120 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003121 TLI.EmitTargetCodeForMemcpy(*this, Chain, Dst, Src, Size, Align,
3122 AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00003123 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003124 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003125 return Result;
3126
3127 // If we really need inline code and the target declined to provide it,
3128 // use a (potentially long) sequence of loads and stores.
3129 if (AlwaysInline) {
3130 assert(ConstantSize && "AlwaysInline requires a constant size!");
3131 return getMemcpyLoadsAndStores(*this, Chain, Dst, Src,
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003132 ConstantSize->getZExtValue(), Align, true,
Dan Gohman1f13c682008-04-28 17:15:20 +00003133 DstSV, DstSVOff, SrcSV, SrcSVOff);
Dan Gohman707e0182008-04-12 04:36:06 +00003134 }
3135
3136 // Emit a library call.
3137 TargetLowering::ArgListTy Args;
3138 TargetLowering::ArgListEntry Entry;
3139 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3140 Entry.Node = Dst; Args.push_back(Entry);
3141 Entry.Node = Src; Args.push_back(Entry);
3142 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003143 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003144 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003145 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003146 getExternalSymbol("memcpy", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003147 Args, *this);
3148 return CallResult.second;
3149}
3150
Dan Gohman475871a2008-07-27 21:46:04 +00003151SDValue SelectionDAG::getMemmove(SDValue Chain, SDValue Dst,
3152 SDValue Src, SDValue Size,
3153 unsigned Align,
3154 const Value *DstSV, uint64_t DstSVOff,
3155 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003156
Dan Gohman21323f32008-05-29 19:42:22 +00003157 // Check to see if we should lower the memmove to loads and stores first.
3158 // For cases within the target-specified limits, this is the best choice.
3159 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3160 if (ConstantSize) {
3161 // Memmove with size zero? Just return the original chain.
3162 if (ConstantSize->isNullValue())
3163 return Chain;
3164
Dan Gohman475871a2008-07-27 21:46:04 +00003165 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003166 getMemmoveLoadsAndStores(*this, Chain, Dst, Src,
3167 ConstantSize->getZExtValue(),
Dan Gohman21323f32008-05-29 19:42:22 +00003168 Align, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003169 if (Result.getNode())
Dan Gohman21323f32008-05-29 19:42:22 +00003170 return Result;
3171 }
Dan Gohman707e0182008-04-12 04:36:06 +00003172
3173 // Then check to see if we should lower the memmove with target-specific
3174 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003175 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003176 TLI.EmitTargetCodeForMemmove(*this, Chain, Dst, Src, Size, Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00003177 DstSV, DstSVOff, SrcSV, SrcSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003178 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003179 return Result;
3180
3181 // Emit a library call.
3182 TargetLowering::ArgListTy Args;
3183 TargetLowering::ArgListEntry Entry;
3184 Entry.Ty = TLI.getTargetData()->getIntPtrType();
3185 Entry.Node = Dst; Args.push_back(Entry);
3186 Entry.Node = Src; Args.push_back(Entry);
3187 Entry.Node = Size; Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003188 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003189 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003190 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003191 getExternalSymbol("memmove", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003192 Args, *this);
3193 return CallResult.second;
3194}
3195
Dan Gohman475871a2008-07-27 21:46:04 +00003196SDValue SelectionDAG::getMemset(SDValue Chain, SDValue Dst,
3197 SDValue Src, SDValue Size,
3198 unsigned Align,
3199 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00003200
3201 // Check to see if we should lower the memset to stores first.
3202 // For cases within the target-specified limits, this is the best choice.
3203 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
3204 if (ConstantSize) {
3205 // Memset with size zero? Just return the original chain.
3206 if (ConstantSize->isNullValue())
3207 return Chain;
3208
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SDValue Result =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003210 getMemsetStores(*this, Chain, Dst, Src, ConstantSize->getZExtValue(),
3211 Align, DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003212 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003213 return Result;
3214 }
3215
3216 // Then check to see if we should lower the memset with target-specific
3217 // code. If the target chooses to do this, this is the next best.
Dan Gohman475871a2008-07-27 21:46:04 +00003218 SDValue Result =
Dan Gohman707e0182008-04-12 04:36:06 +00003219 TLI.EmitTargetCodeForMemset(*this, Chain, Dst, Src, Size, Align,
Bill Wendling6158d842008-10-01 00:59:58 +00003220 DstSV, DstSVOff);
Gabor Greifba36cb52008-08-28 21:40:38 +00003221 if (Result.getNode())
Dan Gohman707e0182008-04-12 04:36:06 +00003222 return Result;
3223
3224 // Emit a library call.
3225 const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType();
3226 TargetLowering::ArgListTy Args;
3227 TargetLowering::ArgListEntry Entry;
3228 Entry.Node = Dst; Entry.Ty = IntPtrTy;
3229 Args.push_back(Entry);
3230 // Extend or truncate the argument to be an i32 value for the call.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003231 if (Src.getValueType().bitsGT(MVT::i32))
Dan Gohman707e0182008-04-12 04:36:06 +00003232 Src = getNode(ISD::TRUNCATE, MVT::i32, Src);
3233 else
3234 Src = getNode(ISD::ZERO_EXTEND, MVT::i32, Src);
3235 Entry.Node = Src; Entry.Ty = Type::Int32Ty; Entry.isSExt = true;
3236 Args.push_back(Entry);
3237 Entry.Node = Size; Entry.Ty = IntPtrTy; Entry.isSExt = false;
3238 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00003239 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00003240 TLI.LowerCallTo(Chain, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003241 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00003242 getExternalSymbol("memset", TLI.getPointerTy()),
Dan Gohman707e0182008-04-12 04:36:06 +00003243 Args, *this);
3244 return CallResult.second;
Rafael Espindola5c0d6ed2007-10-19 10:41:11 +00003245}
3246
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003247SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3248 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003249 SDValue Ptr, SDValue Cmp,
3250 SDValue Swp, const Value* PtrVal,
3251 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003252 assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
Andrew Lenharthc1c7bd62008-02-21 16:11:38 +00003253 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003254
3255 MVT VT = Cmp.getValueType();
3256
3257 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003258 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003259
3260 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003261 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003262 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003263 AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003264 void* IP = 0;
3265 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003266 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003267 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003268 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3269 Chain, Ptr, Cmp, Swp, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003270 CSEMap.InsertNode(N, IP);
3271 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003272 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003273}
3274
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003275SDValue SelectionDAG::getAtomic(unsigned Opcode, MVT MemVT,
3276 SDValue Chain,
Dan Gohman475871a2008-07-27 21:46:04 +00003277 SDValue Ptr, SDValue Val,
3278 const Value* PtrVal,
3279 unsigned Alignment) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003280 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
3281 Opcode == ISD::ATOMIC_LOAD_SUB ||
3282 Opcode == ISD::ATOMIC_LOAD_AND ||
3283 Opcode == ISD::ATOMIC_LOAD_OR ||
3284 Opcode == ISD::ATOMIC_LOAD_XOR ||
3285 Opcode == ISD::ATOMIC_LOAD_NAND ||
3286 Opcode == ISD::ATOMIC_LOAD_MIN ||
3287 Opcode == ISD::ATOMIC_LOAD_MAX ||
3288 Opcode == ISD::ATOMIC_LOAD_UMIN ||
3289 Opcode == ISD::ATOMIC_LOAD_UMAX ||
3290 Opcode == ISD::ATOMIC_SWAP) &&
3291 "Invalid Atomic Op");
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003292
3293 MVT VT = Val.getValueType();
3294
3295 if (Alignment == 0) // Ensure that codegen never sees alignment 0
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003296 Alignment = getMVTAlignment(MemVT);
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003297
3298 SDVTList VTs = getVTList(VT, MVT::Other);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003299 FoldingSetNodeID ID;
Dan Gohman475871a2008-07-27 21:46:04 +00003300 SDValue Ops[] = {Chain, Ptr, Val};
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003301 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003302 void* IP = 0;
3303 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003304 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003305 SDNode* N = NodeAllocator.Allocate<AtomicSDNode>();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003306 new (N) AtomicSDNode(Opcode, VTs, MemVT,
3307 Chain, Ptr, Val, PtrVal, Alignment);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003308 CSEMap.InsertNode(N, IP);
3309 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003310 return SDValue(N, 0);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00003311}
3312
Duncan Sands4bdcb612008-07-02 17:40:58 +00003313/// getMergeValues - Create a MERGE_VALUES node from the given operands.
3314/// Allowed to return something different (and simpler) if Simplify is true.
Duncan Sandsaaffa052008-12-01 11:41:29 +00003315SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps) {
3316 if (NumOps == 1)
Duncan Sands4bdcb612008-07-02 17:40:58 +00003317 return Ops[0];
3318
3319 SmallVector<MVT, 4> VTs;
3320 VTs.reserve(NumOps);
3321 for (unsigned i = 0; i < NumOps; ++i)
3322 VTs.push_back(Ops[i].getValueType());
3323 return getNode(ISD::MERGE_VALUES, getVTList(&VTs[0], NumOps), Ops, NumOps);
3324}
3325
Dan Gohman475871a2008-07-27 21:46:04 +00003326SDValue
Mon P Wangc4d10212008-10-17 18:22:58 +00003327SelectionDAG::getMemIntrinsicNode(unsigned Opcode,
3328 const MVT *VTs, unsigned NumVTs,
3329 const SDValue *Ops, unsigned NumOps,
3330 MVT MemVT, const Value *srcValue, int SVOff,
3331 unsigned Align, bool Vol,
3332 bool ReadMem, bool WriteMem) {
3333 return getMemIntrinsicNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps,
3334 MemVT, srcValue, SVOff, Align, Vol,
3335 ReadMem, WriteMem);
3336}
3337
3338SDValue
3339SelectionDAG::getMemIntrinsicNode(unsigned Opcode, SDVTList VTList,
3340 const SDValue *Ops, unsigned NumOps,
3341 MVT MemVT, const Value *srcValue, int SVOff,
3342 unsigned Align, bool Vol,
3343 bool ReadMem, bool WriteMem) {
3344 // Memoize the node unless it returns a flag.
3345 MemIntrinsicSDNode *N;
3346 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
3347 FoldingSetNodeID ID;
3348 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
3349 void *IP = 0;
3350 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
3351 return SDValue(E, 0);
3352
3353 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3354 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3355 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3356 CSEMap.InsertNode(N, IP);
3357 } else {
3358 N = NodeAllocator.Allocate<MemIntrinsicSDNode>();
3359 new (N) MemIntrinsicSDNode(Opcode, VTList, Ops, NumOps, MemVT,
3360 srcValue, SVOff, Align, Vol, ReadMem, WriteMem);
3361 }
3362 AllNodes.push_back(N);
3363 return SDValue(N, 0);
3364}
3365
3366SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00003367SelectionDAG::getCall(unsigned CallingConv, bool IsVarArgs, bool IsTailCall,
Dale Johannesen86098bd2008-09-26 19:31:26 +00003368 bool IsInreg, SDVTList VTs,
Dan Gohman095cc292008-09-13 01:54:27 +00003369 const SDValue *Operands, unsigned NumOperands) {
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003370 // Do not include isTailCall in the folding set profile.
3371 FoldingSetNodeID ID;
3372 AddNodeIDNode(ID, ISD::CALL, VTs, Operands, NumOperands);
3373 ID.AddInteger(CallingConv);
3374 ID.AddInteger(IsVarArgs);
3375 void *IP = 0;
3376 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
3377 // Instead of including isTailCall in the folding set, we just
3378 // set the flag of the existing node.
3379 if (!IsTailCall)
3380 cast<CallSDNode>(E)->setNotTailCall();
3381 return SDValue(E, 0);
3382 }
Dan Gohman095cc292008-09-13 01:54:27 +00003383 SDNode *N = NodeAllocator.Allocate<CallSDNode>();
Dale Johannesen86098bd2008-09-26 19:31:26 +00003384 new (N) CallSDNode(CallingConv, IsVarArgs, IsTailCall, IsInreg,
Dan Gohman095cc292008-09-13 01:54:27 +00003385 VTs, Operands, NumOperands);
Dan Gohman5eb0cec2008-09-15 19:46:03 +00003386 CSEMap.InsertNode(N, IP);
Dan Gohman095cc292008-09-13 01:54:27 +00003387 AllNodes.push_back(N);
3388 return SDValue(N, 0);
3389}
3390
3391SDValue
Duncan Sandse10efce2008-03-28 09:45:24 +00003392SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
Dan Gohman475871a2008-07-27 21:46:04 +00003393 MVT VT, SDValue Chain,
3394 SDValue Ptr, SDValue Offset,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003395 const Value *SV, int SVOffset, MVT EVT,
Duncan Sandse10efce2008-03-28 09:45:24 +00003396 bool isVolatile, unsigned Alignment) {
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003397 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3398 Alignment = getMVTAlignment(VT);
Evan Cheng466685d2006-10-09 20:57:25 +00003399
Duncan Sands14ea39c2008-03-27 20:23:40 +00003400 if (VT == EVT) {
3401 ExtType = ISD::NON_EXTLOAD;
3402 } else if (ExtType == ISD::NON_EXTLOAD) {
3403 assert(VT == EVT && "Non-extending load from different memory type!");
3404 } else {
3405 // Extending load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003406 if (VT.isVector())
Dan Gohman7f8613e2008-08-14 20:04:46 +00003407 assert(EVT.getVectorNumElements() == VT.getVectorNumElements() &&
3408 "Invalid vector extload!");
Duncan Sands14ea39c2008-03-27 20:23:40 +00003409 else
Duncan Sands8e4eb092008-06-08 20:54:56 +00003410 assert(EVT.bitsLT(VT) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003411 "Should only be an extending load, not truncating!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003412 assert((ExtType == ISD::EXTLOAD || VT.isInteger()) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003413 "Cannot sign/zero extend a FP/Vector load!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003414 assert(VT.isInteger() == EVT.isInteger() &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003415 "Cannot convert from FP to Int or Int -> FP!");
Dan Gohman575e2f42007-06-04 15:49:41 +00003416 }
Duncan Sands14ea39c2008-03-27 20:23:40 +00003417
3418 bool Indexed = AM != ISD::UNINDEXED;
Duncan Sands43e2a032008-05-27 11:50:51 +00003419 assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
Duncan Sands14ea39c2008-03-27 20:23:40 +00003420 "Unindexed load with an offset!");
3421
3422 SDVTList VTs = Indexed ?
3423 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003424 SDValue Ops[] = { Chain, Ptr, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00003425 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003426 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003427 ID.AddInteger(AM);
Evan Cheng466685d2006-10-09 20:57:25 +00003428 ID.AddInteger(ExtType);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003429 ID.AddInteger(EVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003430 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng466685d2006-10-09 20:57:25 +00003431 void *IP = 0;
3432 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003433 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003434 SDNode *N = NodeAllocator.Allocate<LoadSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003435 new (N) LoadSDNode(Ops, VTs, AM, ExtType, EVT, SV, SVOffset,
3436 Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00003437 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00003438 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003439 return SDValue(N, 0);
Evan Cheng7038daf2005-12-10 00:37:58 +00003440}
3441
Dan Gohman475871a2008-07-27 21:46:04 +00003442SDValue SelectionDAG::getLoad(MVT VT,
3443 SDValue Chain, SDValue Ptr,
3444 const Value *SV, int SVOffset,
3445 bool isVolatile, unsigned Alignment) {
3446 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003447 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, Chain, Ptr, Undef,
3448 SV, SVOffset, VT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003449}
3450
Dan Gohman475871a2008-07-27 21:46:04 +00003451SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT VT,
3452 SDValue Chain, SDValue Ptr,
3453 const Value *SV,
3454 int SVOffset, MVT EVT,
3455 bool isVolatile, unsigned Alignment) {
3456 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Duncan Sandse10efce2008-03-28 09:45:24 +00003457 return getLoad(ISD::UNINDEXED, ExtType, VT, Chain, Ptr, Undef,
3458 SV, SVOffset, EVT, isVolatile, Alignment);
Duncan Sands14ea39c2008-03-27 20:23:40 +00003459}
3460
Dan Gohman475871a2008-07-27 21:46:04 +00003461SDValue
3462SelectionDAG::getIndexedLoad(SDValue OrigLoad, SDValue Base,
3463 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003464 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00003465 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
3466 "Load is already a indexed load!");
Duncan Sandse10efce2008-03-28 09:45:24 +00003467 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(),
3468 LD->getChain(), Base, Offset, LD->getSrcValue(),
3469 LD->getSrcValueOffset(), LD->getMemoryVT(),
3470 LD->isVolatile(), LD->getAlignment());
Evan Cheng2caccca2006-10-17 21:14:32 +00003471}
3472
Dan Gohman475871a2008-07-27 21:46:04 +00003473SDValue SelectionDAG::getStore(SDValue Chain, SDValue Val,
3474 SDValue Ptr, const Value *SV, int SVOffset,
3475 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003476 MVT VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00003477
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003478 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3479 Alignment = getMVTAlignment(VT);
3480
Evan Chengad071e12006-10-05 22:57:11 +00003481 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003482 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3483 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003484 FoldingSetNodeID ID;
3485 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003486 ID.AddInteger(ISD::UNINDEXED);
3487 ID.AddInteger(false);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003488 ID.AddInteger(VT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003489 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Chengad071e12006-10-05 22:57:11 +00003490 void *IP = 0;
3491 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003492 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003493 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003494 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
3495 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003496 CSEMap.InsertNode(N, IP);
3497 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003498 return SDValue(N, 0);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003499}
3500
Dan Gohman475871a2008-07-27 21:46:04 +00003501SDValue SelectionDAG::getTruncStore(SDValue Chain, SDValue Val,
3502 SDValue Ptr, const Value *SV,
3503 int SVOffset, MVT SVT,
3504 bool isVolatile, unsigned Alignment) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003505 MVT VT = Val.getValueType();
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003506
3507 if (VT == SVT)
3508 return getStore(Chain, Val, Ptr, SV, SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003509
Duncan Sands8e4eb092008-06-08 20:54:56 +00003510 assert(VT.bitsGT(SVT) && "Not a truncation?");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003511 assert(VT.isInteger() == SVT.isInteger() &&
Evan Cheng8b2794a2006-10-13 21:14:26 +00003512 "Can't do FP-INT conversion!");
3513
Dan Gohman9c6e70e2008-07-08 23:46:32 +00003514 if (Alignment == 0) // Ensure that codegen never sees alignment 0
3515 Alignment = getMVTAlignment(VT);
3516
Evan Cheng8b2794a2006-10-13 21:14:26 +00003517 SDVTList VTs = getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003518 SDValue Undef = getNode(ISD::UNDEF, Ptr.getValueType());
3519 SDValue Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00003520 FoldingSetNodeID ID;
3521 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00003522 ID.AddInteger(ISD::UNINDEXED);
Duncan Sandsba3b1d12007-10-30 12:40:58 +00003523 ID.AddInteger(1);
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003524 ID.AddInteger(SVT.getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003525 ID.AddInteger(encodeMemSDNodeFlags(isVolatile, Alignment));
Evan Cheng8b2794a2006-10-13 21:14:26 +00003526 void *IP = 0;
3527 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003528 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003529 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003530 new (N) StoreSDNode(Ops, VTs, ISD::UNINDEXED, true,
3531 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00003532 CSEMap.InsertNode(N, IP);
3533 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003534 return SDValue(N, 0);
Evan Chengad071e12006-10-05 22:57:11 +00003535}
3536
Dan Gohman475871a2008-07-27 21:46:04 +00003537SDValue
3538SelectionDAG::getIndexedStore(SDValue OrigStore, SDValue Base,
3539 SDValue Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00003540 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
3541 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
3542 "Store is already a indexed store!");
3543 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00003544 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
Evan Cheng9109fb12006-11-05 09:30:09 +00003545 FoldingSetNodeID ID;
3546 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
3547 ID.AddInteger(AM);
3548 ID.AddInteger(ST->isTruncatingStore());
Duncan Sands3b3adbb2008-06-06 12:49:32 +00003549 ID.AddInteger(ST->getMemoryVT().getRawBits());
Dan Gohmanb8d2f552008-08-20 15:58:01 +00003550 ID.AddInteger(ST->getRawFlags());
Evan Cheng9109fb12006-11-05 09:30:09 +00003551 void *IP = 0;
3552 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003553 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003554 SDNode *N = NodeAllocator.Allocate<StoreSDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003555 new (N) StoreSDNode(Ops, VTs, AM,
3556 ST->isTruncatingStore(), ST->getMemoryVT(),
3557 ST->getSrcValue(), ST->getSrcValueOffset(),
3558 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00003559 CSEMap.InsertNode(N, IP);
3560 AllNodes.push_back(N);
Dan Gohman475871a2008-07-27 21:46:04 +00003561 return SDValue(N, 0);
Evan Cheng9109fb12006-11-05 09:30:09 +00003562}
3563
Dan Gohman475871a2008-07-27 21:46:04 +00003564SDValue SelectionDAG::getVAArg(MVT VT,
3565 SDValue Chain, SDValue Ptr,
3566 SDValue SV) {
3567 SDValue Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00003568 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00003569}
3570
Dan Gohman475871a2008-07-27 21:46:04 +00003571SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3572 const SDUse *Ops, unsigned NumOps) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003573 switch (NumOps) {
3574 case 0: return getNode(Opcode, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00003575 case 1: return getNode(Opcode, VT, Ops[0]);
3576 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3577 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003578 default: break;
3579 }
3580
Dan Gohman475871a2008-07-27 21:46:04 +00003581 // Copy from an SDUse array into an SDValue array for use with
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003582 // the regular getNode logic.
Dan Gohman475871a2008-07-27 21:46:04 +00003583 SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
3584 return getNode(Opcode, VT, &NewOps[0], NumOps);
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003585}
3586
Dan Gohman475871a2008-07-27 21:46:04 +00003587SDValue SelectionDAG::getNode(unsigned Opcode, MVT VT,
3588 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003589 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003590 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00003591 case 1: return getNode(Opcode, VT, Ops[0]);
3592 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
3593 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00003594 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003595 }
Chris Lattnerde202b32005-11-09 23:47:37 +00003596
Chris Lattneref847df2005-04-09 03:27:28 +00003597 switch (Opcode) {
3598 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003599 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003600 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003601 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
3602 "LHS and RHS of condition must have same type!");
3603 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3604 "True and False arms of SelectCC must have same type!");
3605 assert(Ops[2].getValueType() == VT &&
3606 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003607 break;
3608 }
3609 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003610 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003611 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
3612 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00003613 break;
3614 }
Chris Lattneref847df2005-04-09 03:27:28 +00003615 }
3616
Chris Lattner385328c2005-05-14 07:42:29 +00003617 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00003618 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00003619 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00003620 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003621 FoldingSetNodeID ID;
3622 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003623 void *IP = 0;
3624 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003625 return SDValue(E, 0);
Dan Gohmanfed90b62008-07-28 21:51:04 +00003626 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003627 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003628 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003629 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003630 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003631 new (N) SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00003632 }
Chris Lattneref847df2005-04-09 03:27:28 +00003633 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003634#ifndef NDEBUG
3635 VerifyNode(N);
3636#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003637 return SDValue(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003638}
3639
Dan Gohman475871a2008-07-27 21:46:04 +00003640SDValue SelectionDAG::getNode(unsigned Opcode,
3641 const std::vector<MVT> &ResultTys,
3642 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003643 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
3644 Ops, NumOps);
3645}
3646
Dan Gohman475871a2008-07-27 21:46:04 +00003647SDValue SelectionDAG::getNode(unsigned Opcode,
3648 const MVT *VTs, unsigned NumVTs,
3649 const SDValue *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003650 if (NumVTs == 1)
3651 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00003652 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
3653}
3654
Dan Gohman475871a2008-07-27 21:46:04 +00003655SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3656 const SDValue *Ops, unsigned NumOps) {
Chris Lattnerbe384162006-08-16 22:57:46 +00003657 if (VTList.NumVTs == 1)
3658 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00003659
Chris Lattner5f056bf2005-07-10 01:55:33 +00003660 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00003661 // FIXME: figure out how to safely handle things like
3662 // int foo(int x) { return 1 << (x & 255); }
3663 // int bar() { return foo(256); }
3664#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00003665 case ISD::SRA_PARTS:
3666 case ISD::SRL_PARTS:
3667 case ISD::SHL_PARTS:
3668 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00003669 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00003670 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3671 else if (N3.getOpcode() == ISD::AND)
3672 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
3673 // If the and is only masking out bits that cannot effect the shift,
3674 // eliminate the and.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003675 unsigned NumBits = VT.getSizeInBits()*2;
Chris Lattnere89083a2005-05-14 07:25:05 +00003676 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
3677 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
3678 }
3679 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00003680#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00003681 }
Chris Lattner89c34632005-05-14 06:20:26 +00003682
Chris Lattner43247a12005-08-25 19:12:10 +00003683 // Memoize the node unless it returns a flag.
3684 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00003685 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00003686 FoldingSetNodeID ID;
3687 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00003688 void *IP = 0;
3689 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
Dan Gohman475871a2008-07-27 21:46:04 +00003690 return SDValue(E, 0);
Dan Gohman0e5f1302008-07-07 23:02:41 +00003691 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003692 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003693 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3694 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003695 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003696 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3697 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003698 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003699 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3700 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003701 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003702 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3703 }
Chris Lattnera5682852006-08-07 23:03:03 +00003704 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00003705 } else {
Dan Gohman0e5f1302008-07-07 23:02:41 +00003706 if (NumOps == 1) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003707 N = NodeAllocator.Allocate<UnarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003708 new (N) UnarySDNode(Opcode, VTList, Ops[0]);
3709 } else if (NumOps == 2) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003710 N = NodeAllocator.Allocate<BinarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003711 new (N) BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
3712 } else if (NumOps == 3) {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003713 N = NodeAllocator.Allocate<TernarySDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003714 new (N) TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
3715 } else {
Dan Gohmanfed90b62008-07-28 21:51:04 +00003716 N = NodeAllocator.Allocate<SDNode>();
Dan Gohman0e5f1302008-07-07 23:02:41 +00003717 new (N) SDNode(Opcode, VTList, Ops, NumOps);
3718 }
Chris Lattner43247a12005-08-25 19:12:10 +00003719 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00003720 AllNodes.push_back(N);
Duncan Sandsd038e042008-07-21 10:20:31 +00003721#ifndef NDEBUG
3722 VerifyNode(N);
3723#endif
Dan Gohman475871a2008-07-27 21:46:04 +00003724 return SDValue(N, 0);
Chris Lattner89c34632005-05-14 06:20:26 +00003725}
3726
Dan Gohman475871a2008-07-27 21:46:04 +00003727SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
Dan Gohman6d9cdd52008-07-07 18:26:29 +00003728 return getNode(Opcode, VTList, 0, 0);
Dan Gohman08ce9762007-10-08 15:49:58 +00003729}
3730
Dan Gohman475871a2008-07-27 21:46:04 +00003731SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3732 SDValue N1) {
3733 SDValue Ops[] = { N1 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003734 return getNode(Opcode, VTList, Ops, 1);
3735}
3736
Dan Gohman475871a2008-07-27 21:46:04 +00003737SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3738 SDValue N1, SDValue N2) {
3739 SDValue Ops[] = { N1, N2 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003740 return getNode(Opcode, VTList, Ops, 2);
3741}
3742
Dan Gohman475871a2008-07-27 21:46:04 +00003743SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3744 SDValue N1, SDValue N2, SDValue N3) {
3745 SDValue Ops[] = { N1, N2, N3 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003746 return getNode(Opcode, VTList, Ops, 3);
3747}
3748
Dan Gohman475871a2008-07-27 21:46:04 +00003749SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3750 SDValue N1, SDValue N2, SDValue N3,
3751 SDValue N4) {
3752 SDValue Ops[] = { N1, N2, N3, N4 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003753 return getNode(Opcode, VTList, Ops, 4);
3754}
3755
Dan Gohman475871a2008-07-27 21:46:04 +00003756SDValue SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
3757 SDValue N1, SDValue N2, SDValue N3,
3758 SDValue N4, SDValue N5) {
3759 SDValue Ops[] = { N1, N2, N3, N4, N5 };
Dan Gohman08ce9762007-10-08 15:49:58 +00003760 return getNode(Opcode, VTList, Ops, 5);
3761}
3762
Duncan Sands83ec4b62008-06-06 12:08:01 +00003763SDVTList SelectionDAG::getVTList(MVT VT) {
Duncan Sandsaf47b112007-10-16 09:56:48 +00003764 return makeVTList(SDNode::getValueTypeList(VT), 1);
Chris Lattnera3255112005-11-08 23:30:28 +00003765}
3766
Duncan Sands83ec4b62008-06-06 12:08:01 +00003767SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00003768 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3769 E = VTList.rend(); I != E; ++I)
3770 if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
3771 return *I;
3772
3773 MVT *Array = Allocator.Allocate<MVT>(2);
3774 Array[0] = VT1;
3775 Array[1] = VT2;
3776 SDVTList Result = makeVTList(Array, 2);
3777 VTList.push_back(Result);
3778 return Result;
Chris Lattnera3255112005-11-08 23:30:28 +00003779}
Dan Gohmane8be6c62008-07-17 19:10:17 +00003780
3781SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3) {
3782 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3783 E = VTList.rend(); I != E; ++I)
3784 if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3785 I->VTs[2] == VT3)
3786 return *I;
3787
3788 MVT *Array = Allocator.Allocate<MVT>(3);
3789 Array[0] = VT1;
3790 Array[1] = VT2;
3791 Array[2] = VT3;
3792 SDVTList Result = makeVTList(Array, 3);
3793 VTList.push_back(Result);
3794 return Result;
Chris Lattner70046e92006-08-15 17:46:01 +00003795}
3796
Bill Wendling13d6d442008-12-01 23:28:22 +00003797SDVTList SelectionDAG::getVTList(MVT VT1, MVT VT2, MVT VT3, MVT VT4) {
3798 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3799 E = VTList.rend(); I != E; ++I)
3800 if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
3801 I->VTs[2] == VT3 && I->VTs[3] == VT4)
3802 return *I;
3803
3804 MVT *Array = Allocator.Allocate<MVT>(3);
3805 Array[0] = VT1;
3806 Array[1] = VT2;
3807 Array[2] = VT3;
3808 Array[3] = VT4;
3809 SDVTList Result = makeVTList(Array, 4);
3810 VTList.push_back(Result);
3811 return Result;
3812}
3813
Duncan Sands83ec4b62008-06-06 12:08:01 +00003814SDVTList SelectionDAG::getVTList(const MVT *VTs, unsigned NumVTs) {
Chris Lattner70046e92006-08-15 17:46:01 +00003815 switch (NumVTs) {
3816 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00003817 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00003818 case 2: return getVTList(VTs[0], VTs[1]);
3819 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
3820 default: break;
3821 }
3822
Dan Gohmane8be6c62008-07-17 19:10:17 +00003823 for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
3824 E = VTList.rend(); I != E; ++I) {
3825 if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
3826 continue;
Chris Lattner70046e92006-08-15 17:46:01 +00003827
3828 bool NoMatch = false;
3829 for (unsigned i = 2; i != NumVTs; ++i)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003830 if (VTs[i] != I->VTs[i]) {
Chris Lattner70046e92006-08-15 17:46:01 +00003831 NoMatch = true;
3832 break;
3833 }
3834 if (!NoMatch)
Dan Gohmane8be6c62008-07-17 19:10:17 +00003835 return *I;
Chris Lattner70046e92006-08-15 17:46:01 +00003836 }
3837
Dan Gohmane8be6c62008-07-17 19:10:17 +00003838 MVT *Array = Allocator.Allocate<MVT>(NumVTs);
3839 std::copy(VTs, VTs+NumVTs, Array);
3840 SDVTList Result = makeVTList(Array, NumVTs);
3841 VTList.push_back(Result);
3842 return Result;
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003843}
3844
3845
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003846/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
3847/// specified operands. If the resultant node already exists in the DAG,
3848/// this does not modify the specified node, instead it returns the node that
3849/// already exists. If the resultant node does not exist in the DAG, the
3850/// input node is returned. As a degenerate case, if you specify the same
3851/// input operands as the node already has, the input node is returned.
Dan Gohman475871a2008-07-27 21:46:04 +00003852SDValue SelectionDAG::UpdateNodeOperands(SDValue InN, SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003853 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003854 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
3855
3856 // Check to see if there is no change.
3857 if (Op == N->getOperand(0)) return InN;
3858
3859 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003860 void *InsertPos = 0;
3861 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003862 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003863
Dan Gohman79acd2b2008-07-21 22:38:59 +00003864 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003865 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003866 if (!RemoveNodeFromCSEMaps(N))
3867 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003868
3869 // Now we update the operands.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003870 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003871 N->OperandList[0] = Op;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003872 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003873 Op.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003874
3875 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003876 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003877 return InN;
3878}
3879
Dan Gohman475871a2008-07-27 21:46:04 +00003880SDValue SelectionDAG::
3881UpdateNodeOperands(SDValue InN, SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003882 SDNode *N = InN.getNode();
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003883 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
3884
3885 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003886 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
3887 return InN; // No operands changed, just return the input node.
3888
3889 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003890 void *InsertPos = 0;
3891 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003892 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003893
Dan Gohman79acd2b2008-07-21 22:38:59 +00003894 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003895 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003896 if (!RemoveNodeFromCSEMaps(N))
3897 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003898
3899 // Now we update the operands.
3900 if (N->OperandList[0] != Op1) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003901 N->OperandList[0].getVal()->removeUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003902 N->OperandList[0] = Op1;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003903 N->OperandList[0].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003904 Op1.getNode()->addUser(0, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003905 }
3906 if (N->OperandList[1] != Op2) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003907 N->OperandList[1].getVal()->removeUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003908 N->OperandList[1] = Op2;
Roman Levensteindc1adac2008-04-07 10:06:32 +00003909 N->OperandList[1].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003910 Op2.getNode()->addUser(1, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003911 }
3912
3913 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003914 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003915 return InN;
3916}
3917
Dan Gohman475871a2008-07-27 21:46:04 +00003918SDValue SelectionDAG::
3919UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2, SDValue Op3) {
3920 SDValue Ops[] = { Op1, Op2, Op3 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003921 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003922}
3923
Dan Gohman475871a2008-07-27 21:46:04 +00003924SDValue SelectionDAG::
3925UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3926 SDValue Op3, SDValue Op4) {
3927 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003928 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003929}
3930
Dan Gohman475871a2008-07-27 21:46:04 +00003931SDValue SelectionDAG::
3932UpdateNodeOperands(SDValue N, SDValue Op1, SDValue Op2,
3933 SDValue Op3, SDValue Op4, SDValue Op5) {
3934 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003935 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00003936}
3937
Dan Gohman475871a2008-07-27 21:46:04 +00003938SDValue SelectionDAG::
3939UpdateNodeOperands(SDValue InN, const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003940 SDNode *N = InN.getNode();
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003941 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003942 "Update with wrong number of operands");
3943
3944 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003945 bool AnyChange = false;
3946 for (unsigned i = 0; i != NumOps; ++i) {
3947 if (Ops[i] != N->getOperand(i)) {
3948 AnyChange = true;
3949 break;
3950 }
3951 }
3952
3953 // No operands changed, just return the input node.
3954 if (!AnyChange) return InN;
3955
3956 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00003957 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003958 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Gabor Greif99a6cb92008-08-26 22:36:50 +00003959 return SDValue(Existing, InN.getResNo());
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003960
Dan Gohman7ceda162008-05-02 00:05:03 +00003961 // Nope it doesn't. Remove the node from its current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00003962 if (InsertPos)
Dan Gohman095cc292008-09-13 01:54:27 +00003963 if (!RemoveNodeFromCSEMaps(N))
3964 InsertPos = 0;
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003965
3966 // Now we update the operands.
3967 for (unsigned i = 0; i != NumOps; ++i) {
3968 if (N->OperandList[i] != Ops[i]) {
Roman Levenstein9cac5252008-04-16 16:15:27 +00003969 N->OperandList[i].getVal()->removeUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003970 N->OperandList[i] = Ops[i];
Roman Levensteindc1adac2008-04-07 10:06:32 +00003971 N->OperandList[i].setUser(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003972 Ops[i].getNode()->addUser(i, N);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003973 }
3974 }
3975
3976 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00003977 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00003978 return InN;
3979}
3980
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003981/// DropOperands - Release the operands and set this node to have
Dan Gohmane8be6c62008-07-17 19:10:17 +00003982/// zero operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003983void SDNode::DropOperands() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00003984 // Unlike the code in MorphNodeTo that does this, we don't need to
3985 // watch for dead nodes here.
3986 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
3987 I->getVal()->removeUser(std::distance(op_begin(), I), this);
3988
3989 NumOperands = 0;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003990}
Chris Lattner149c58c2005-08-16 18:17:10 +00003991
Dan Gohmane8be6c62008-07-17 19:10:17 +00003992/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
3993/// machine opcode.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003994///
Dan Gohmane8be6c62008-07-17 19:10:17 +00003995SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003996 MVT VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00003997 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00003998 return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00003999}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004000
Dan Gohmane8be6c62008-07-17 19:10:17 +00004001SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004002 MVT VT, SDValue Op1) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004003 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004004 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004005 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Chris Lattner149c58c2005-08-16 18:17:10 +00004006}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004007
Dan Gohmane8be6c62008-07-17 19:10:17 +00004008SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004009 MVT VT, SDValue Op1,
4010 SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004011 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004012 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004013 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner149c58c2005-08-16 18:17:10 +00004014}
Chris Lattner0fb094f2005-11-19 01:44:53 +00004015
Dan Gohmane8be6c62008-07-17 19:10:17 +00004016SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004017 MVT VT, SDValue Op1,
4018 SDValue Op2, SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004019 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004020 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004021 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Chris Lattner149c58c2005-08-16 18:17:10 +00004022}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00004023
Dan Gohmane8be6c62008-07-17 19:10:17 +00004024SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004025 MVT VT, const SDValue *Ops,
Evan Cheng694481e2006-08-27 08:08:54 +00004026 unsigned NumOps) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004027 SDVTList VTs = getVTList(VT);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004028 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004029}
4030
Dan Gohmane8be6c62008-07-17 19:10:17 +00004031SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004032 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004033 unsigned NumOps) {
4034 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004035 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004036}
4037
Dan Gohmane8be6c62008-07-17 19:10:17 +00004038SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004039 MVT VT1, MVT VT2) {
4040 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004041 return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
Dan Gohmancd920d92008-07-02 23:23:19 +00004042}
4043
Dan Gohmane8be6c62008-07-17 19:10:17 +00004044SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman6d9cdd52008-07-07 18:26:29 +00004045 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004046 const SDValue *Ops, unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004047 SDVTList VTs = getVTList(VT1, VT2, VT3);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004048 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
Dan Gohmancd920d92008-07-02 23:23:19 +00004049}
4050
Bill Wendling13d6d442008-12-01 23:28:22 +00004051SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
4052 MVT VT1, MVT VT2, MVT VT3, MVT VT4,
4053 const SDValue *Ops, unsigned NumOps) {
4054 SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
4055 return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
4056}
4057
Dan Gohmane8be6c62008-07-17 19:10:17 +00004058SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohmancd920d92008-07-02 23:23:19 +00004059 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004060 SDValue Op1) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004061 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004062 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004063 return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00004064}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00004065
Dan Gohmane8be6c62008-07-17 19:10:17 +00004066SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004067 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004068 SDValue Op1, SDValue Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004069 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004070 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004071 return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
Chris Lattner0fb094f2005-11-19 01:44:53 +00004072}
4073
Dan Gohmane8be6c62008-07-17 19:10:17 +00004074SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004075 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004076 SDValue Op1, SDValue Op2,
4077 SDValue Op3) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00004078 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004079 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004080 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
Dan Gohmancd920d92008-07-02 23:23:19 +00004081}
4082
Dan Gohmane8be6c62008-07-17 19:10:17 +00004083SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Bill Wendling13d6d442008-12-01 23:28:22 +00004084 MVT VT1, MVT VT2, MVT VT3,
4085 SDValue Op1, SDValue Op2,
4086 SDValue Op3) {
4087 SDVTList VTs = getVTList(VT1, VT2, VT3);
4088 SDValue Ops[] = { Op1, Op2, Op3 };
4089 return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
4090}
4091
4092SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDVTList VTs, const SDValue *Ops,
Dan Gohmancd920d92008-07-02 23:23:19 +00004094 unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004095 return MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
4096}
4097
4098SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4099 MVT VT) {
4100 SDVTList VTs = getVTList(VT);
4101 return MorphNodeTo(N, Opc, VTs, 0, 0);
4102}
4103
4104SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004105 MVT VT, SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004106 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004108 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4109}
4110
4111SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004112 MVT VT, SDValue Op1,
4113 SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004114 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004115 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004116 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4117}
4118
4119SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004120 MVT VT, SDValue Op1,
4121 SDValue Op2, SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004122 SDVTList VTs = getVTList(VT);
Dan Gohman475871a2008-07-27 21:46:04 +00004123 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004124 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4125}
4126
4127SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004128 MVT VT, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004129 unsigned NumOps) {
4130 SDVTList VTs = getVTList(VT);
4131 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4132}
4133
4134SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004135 MVT VT1, MVT VT2, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004136 unsigned NumOps) {
4137 SDVTList VTs = getVTList(VT1, VT2);
4138 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4139}
4140
4141SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4142 MVT VT1, MVT VT2) {
4143 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004144 return MorphNodeTo(N, Opc, VTs, (SDValue *)0, 0);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004145}
4146
4147SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4148 MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004149 const SDValue *Ops, unsigned NumOps) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004150 SDVTList VTs = getVTList(VT1, VT2, VT3);
4151 return MorphNodeTo(N, Opc, VTs, Ops, NumOps);
4152}
4153
4154SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4155 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue Op1) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004157 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004158 SDValue Ops[] = { Op1 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004159 return MorphNodeTo(N, Opc, VTs, Ops, 1);
4160}
4161
4162SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4163 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004164 SDValue Op1, SDValue Op2) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004165 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue Ops[] = { Op1, Op2 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004167 return MorphNodeTo(N, Opc, VTs, Ops, 2);
4168}
4169
4170SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
4171 MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004172 SDValue Op1, SDValue Op2,
4173 SDValue Op3) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004174 SDVTList VTs = getVTList(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004175 SDValue Ops[] = { Op1, Op2, Op3 };
Dan Gohmane8be6c62008-07-17 19:10:17 +00004176 return MorphNodeTo(N, Opc, VTs, Ops, 3);
4177}
4178
4179/// MorphNodeTo - These *mutate* the specified node to have the specified
4180/// return type, opcode, and operands.
4181///
4182/// Note that MorphNodeTo returns the resultant node. If there is already a
4183/// node of the specified opcode and operands, it returns that node instead of
4184/// the current one.
4185///
4186/// Using MorphNodeTo is faster than creating a new node and swapping it in
4187/// with ReplaceAllUsesWith both because it often avoids allocating a new
Gabor Greifdc715632008-08-30 22:16:05 +00004188/// node, and because it doesn't require CSE recalculation for any of
Dan Gohmane8be6c62008-07-17 19:10:17 +00004189/// the node's users.
4190///
4191SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
Dan Gohman475871a2008-07-27 21:46:04 +00004192 SDVTList VTs, const SDValue *Ops,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004193 unsigned NumOps) {
Dan Gohmancd920d92008-07-02 23:23:19 +00004194 // If an identical node already exists, use it.
Chris Lattnera5682852006-08-07 23:03:03 +00004195 void *IP = 0;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004196 if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
4197 FoldingSetNodeID ID;
4198 AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
4199 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
4200 return ON;
4201 }
Chris Lattnerc5e6c642005-12-01 18:00:57 +00004202
Dan Gohman095cc292008-09-13 01:54:27 +00004203 if (!RemoveNodeFromCSEMaps(N))
4204 IP = 0;
Chris Lattner67612a12007-02-04 02:32:44 +00004205
Dan Gohmane8be6c62008-07-17 19:10:17 +00004206 // Start the morphing.
4207 N->NodeType = Opc;
4208 N->ValueList = VTs.VTs;
4209 N->NumValues = VTs.NumVTs;
4210
4211 // Clear the operands list, updating used nodes to remove this from their
4212 // use list. Keep track of any operands that become dead as a result.
4213 SmallPtrSet<SDNode*, 16> DeadNodeSet;
4214 for (SDNode::op_iterator B = N->op_begin(), I = B, E = N->op_end();
4215 I != E; ++I) {
4216 SDNode *Used = I->getVal();
4217 Used->removeUser(std::distance(B, I), N);
4218 if (Used->use_empty())
4219 DeadNodeSet.insert(Used);
4220 }
4221
4222 // If NumOps is larger than the # of operands we currently have, reallocate
4223 // the operand list.
4224 if (NumOps > N->NumOperands) {
4225 if (N->OperandsNeedDelete)
4226 delete[] N->OperandList;
Bill Wendlinga1dc6022008-10-19 20:51:12 +00004227
Dan Gohmane8be6c62008-07-17 19:10:17 +00004228 if (N->isMachineOpcode()) {
4229 // We're creating a final node that will live unmorphed for the
Dan Gohmanf350b272008-08-23 02:25:05 +00004230 // remainder of the current SelectionDAG iteration, so we can allocate
4231 // the operands directly out of a pool with no recycling metadata.
4232 N->OperandList = OperandAllocator.Allocate<SDUse>(NumOps);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004233 N->OperandsNeedDelete = false;
4234 } else {
4235 N->OperandList = new SDUse[NumOps];
4236 N->OperandsNeedDelete = true;
4237 }
4238 }
4239
4240 // Assign the new operands.
4241 N->NumOperands = NumOps;
4242 for (unsigned i = 0, e = NumOps; i != e; ++i) {
4243 N->OperandList[i] = Ops[i];
4244 N->OperandList[i].setUser(N);
4245 SDNode *ToUse = N->OperandList[i].getVal();
4246 ToUse->addUser(i, N);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004247 }
4248
4249 // Delete any nodes that are still dead after adding the uses for the
4250 // new operands.
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004251 SmallVector<SDNode *, 16> DeadNodes;
Dan Gohmane8be6c62008-07-17 19:10:17 +00004252 for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
4253 E = DeadNodeSet.end(); I != E; ++I)
4254 if ((*I)->use_empty())
4255 DeadNodes.push_back(*I);
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004256 RemoveDeadNodes(DeadNodes);
4257
Dan Gohmane8be6c62008-07-17 19:10:17 +00004258 if (IP)
4259 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00004260 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00004261}
4262
Chris Lattner0fb094f2005-11-19 01:44:53 +00004263
Evan Cheng6ae46c42006-02-09 07:15:23 +00004264/// getTargetNode - These are used for target selectors to create a new node
4265/// with specified return type(s), target opcode, and operands.
4266///
4267/// Note that getTargetNode returns the resultant node. If there is already a
4268/// node of the specified opcode and operands, it returns that node instead of
4269/// the current one.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004270SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004271 return getNode(~Opcode, VT).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004272}
Dan Gohman475871a2008-07-27 21:46:04 +00004273SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT, SDValue Op1) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004274 return getNode(~Opcode, VT, Op1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004275}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004276SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004277 SDValue Op1, SDValue Op2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004278 return getNode(~Opcode, VT, Op1, Op2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004279}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004280SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue Op1, SDValue Op2,
4282 SDValue Op3) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004283 return getNode(~Opcode, VT, Op1, Op2, Op3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004284}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004285SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00004286 const SDValue *Ops, unsigned NumOps) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004287 return getNode(~Opcode, VT, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004288}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004289SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2) {
4290 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue Op;
Gabor Greifba36cb52008-08-28 21:40:38 +00004292 return getNode(~Opcode, VTs, 2, &Op, 0).getNode();
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004293}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004294SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004295 MVT VT2, SDValue Op1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004296 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004297 return getNode(~Opcode, VTs, 2, &Op1, 1).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004298}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004299SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004300 MVT VT2, SDValue Op1,
4301 SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004302 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004303 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004304 return getNode(~Opcode, VTs, 2, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004305}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004306SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
Dan Gohman475871a2008-07-27 21:46:04 +00004307 MVT VT2, SDValue Op1,
4308 SDValue Op2, SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004309 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Dan Gohman475871a2008-07-27 21:46:04 +00004310 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004311 return getNode(~Opcode, VTs, 2, Ops, 3).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004312}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004313SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2,
Dan Gohman475871a2008-07-27 21:46:04 +00004314 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004315 const MVT *VTs = getNodeValueTypes(VT1, VT2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004316 return getNode(~Opcode, VTs, 2, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004317}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004318SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004319 SDValue Op1, SDValue Op2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004320 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004321 SDValue Ops[] = { Op1, Op2 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004322 return getNode(~Opcode, VTs, 3, Ops, 2).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004323}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004324SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004325 SDValue Op1, SDValue Op2,
4326 SDValue Op3) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004327 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Dan Gohman475871a2008-07-27 21:46:04 +00004328 SDValue Ops[] = { Op1, Op2, Op3 };
Gabor Greifba36cb52008-08-28 21:40:38 +00004329 return getNode(~Opcode, VTs, 3, Ops, 3).getNode();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004330}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004331SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1, MVT VT2, MVT VT3,
Dan Gohman475871a2008-07-27 21:46:04 +00004332 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004333 const MVT *VTs = getNodeValueTypes(VT1, VT2, VT3);
Gabor Greifba36cb52008-08-28 21:40:38 +00004334 return getNode(~Opcode, VTs, 3, Ops, NumOps).getNode();
Evan Cheng6ae46c42006-02-09 07:15:23 +00004335}
Duncan Sands83ec4b62008-06-06 12:08:01 +00004336SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT VT1,
4337 MVT VT2, MVT VT3, MVT VT4,
Dan Gohman475871a2008-07-27 21:46:04 +00004338 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004339 std::vector<MVT> VTList;
Evan Cheng05e69c12007-09-12 23:39:49 +00004340 VTList.push_back(VT1);
4341 VTList.push_back(VT2);
4342 VTList.push_back(VT3);
4343 VTList.push_back(VT4);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004344 const MVT *VTs = getNodeValueTypes(VTList);
Gabor Greifba36cb52008-08-28 21:40:38 +00004345 return getNode(~Opcode, VTs, 4, Ops, NumOps).getNode();
Evan Cheng05e69c12007-09-12 23:39:49 +00004346}
Evan Cheng39305cf2007-10-05 01:10:49 +00004347SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
Dan Gohmanf877b732008-07-09 00:00:42 +00004348 const std::vector<MVT> &ResultTys,
Dan Gohman475871a2008-07-27 21:46:04 +00004349 const SDValue *Ops, unsigned NumOps) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004350 const MVT *VTs = getNodeValueTypes(ResultTys);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004351 return getNode(~Opcode, VTs, ResultTys.size(),
Gabor Greifba36cb52008-08-28 21:40:38 +00004352 Ops, NumOps).getNode();
Evan Cheng39305cf2007-10-05 01:10:49 +00004353}
Evan Cheng6ae46c42006-02-09 07:15:23 +00004354
Evan Cheng08b11732008-03-22 01:55:50 +00004355/// getNodeIfExists - Get the specified node if it's already available, or
4356/// else return NULL.
4357SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
Dan Gohman475871a2008-07-27 21:46:04 +00004358 const SDValue *Ops, unsigned NumOps) {
Evan Cheng08b11732008-03-22 01:55:50 +00004359 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
4360 FoldingSetNodeID ID;
4361 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
4362 void *IP = 0;
4363 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
4364 return E;
4365 }
4366 return NULL;
4367}
4368
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004369
Evan Cheng99157a02006-08-07 22:13:29 +00004370/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00004371/// This can cause recursive merging of nodes in the DAG.
4372///
Chris Lattner11d049c2008-02-03 03:35:22 +00004373/// This version assumes From has a single result value.
Chris Lattner8b52f212005-08-26 18:36:28 +00004374///
Dan Gohman475871a2008-07-27 21:46:04 +00004375void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004376 DAGUpdateListener *UpdateListener) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004377 SDNode *From = FromN.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00004378 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004379 "Cannot replace with this method!");
Gabor Greifba36cb52008-08-28 21:40:38 +00004380 assert(From != To.getNode() && "Cannot replace uses of with self");
Roman Levensteindc1adac2008-04-07 10:06:32 +00004381
Dan Gohmandbe664a2009-01-19 21:44:21 +00004382 // Iterate over all the existing uses of From. This specifically avoids
Chris Lattnere70909b2009-01-22 07:21:55 +00004383 // visiting any new uses of From that arise while the replacement is
Dan Gohmandbe664a2009-01-19 21:44:21 +00004384 // happening, because any such uses would be the result of CSE: If an
4385 // existing node looks like From after one of its operands is replaced
4386 // by To, we don't want to replace of all its users with To too.
4387 // See PR3018 for more info.
4388 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4389 while (UI != UE) {
Dan Gohman89684502008-07-27 20:43:25 +00004390 SDNode *U = *UI;
Dan Gohmandbe664a2009-01-19 21:44:21 +00004391 do ++UI; while (UI != UE && *UI == U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004392
Chris Lattner8b8749f2005-08-17 19:00:20 +00004393 // This node is about to morph, remove its old self from the CSE maps.
4394 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004395 int operandNum = 0;
4396 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4397 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004398 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004399 From->removeUser(operandNum, U);
Chris Lattner11d049c2008-02-03 03:35:22 +00004400 *I = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004401 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004402 To.getNode()->addUser(operandNum, U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004403 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004404
4405 // Now that we have modified U, add it back to the CSE maps. If it already
4406 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004407 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004408 ReplaceAllUsesWith(U, Existing, UpdateListener);
4409 // U is now dead. Inform the listener if it exists and delete it.
4410 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004411 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004412 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004413 } else {
4414 // If the node doesn't already exist, we updated it. Inform a listener if
4415 // it exists.
4416 if (UpdateListener)
4417 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004418 }
Chris Lattner8b52f212005-08-26 18:36:28 +00004419 }
4420}
4421
4422/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4423/// This can cause recursive merging of nodes in the DAG.
4424///
4425/// This version assumes From/To have matching types and numbers of result
4426/// values.
4427///
Chris Lattner1e111c72005-09-07 05:37:01 +00004428void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004429 DAGUpdateListener *UpdateListener) {
Dan Gohmane8be6c62008-07-17 19:10:17 +00004430 assert(From->getVTList().VTs == To->getVTList().VTs &&
4431 From->getNumValues() == To->getNumValues() &&
Chris Lattner8b52f212005-08-26 18:36:28 +00004432 "Cannot use this version of ReplaceAllUsesWith!");
Dan Gohmane8be6c62008-07-17 19:10:17 +00004433
4434 // Handle the trivial case.
4435 if (From == To)
4436 return;
4437
Dan Gohmandbe664a2009-01-19 21:44:21 +00004438 // Iterate over just the existing users of From. See the comments in
4439 // the ReplaceAllUsesWith above.
4440 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4441 while (UI != UE) {
Dan Gohman89684502008-07-27 20:43:25 +00004442 SDNode *U = *UI;
Dan Gohmandbe664a2009-01-19 21:44:21 +00004443 do ++UI; while (UI != UE && *UI == U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004444
Chris Lattner8b52f212005-08-26 18:36:28 +00004445 // This node is about to morph, remove its old self from the CSE maps.
4446 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004447 int operandNum = 0;
4448 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4449 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004450 if (I->getVal() == From) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004451 From->removeUser(operandNum, U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004452 I->getSDValue().setNode(To);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004453 To->addUser(operandNum, U);
Chris Lattner8b8749f2005-08-17 19:00:20 +00004454 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004455
Chris Lattner8b8749f2005-08-17 19:00:20 +00004456 // Now that we have modified U, add it back to the CSE maps. If it already
4457 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004458 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004459 ReplaceAllUsesWith(U, Existing, UpdateListener);
4460 // U is now dead. Inform the listener if it exists and delete it.
4461 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004462 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004463 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004464 } else {
4465 // If the node doesn't already exist, we updated it. Inform a listener if
4466 // it exists.
4467 if (UpdateListener)
4468 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004469 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00004470 }
4471}
4472
Chris Lattner8b52f212005-08-26 18:36:28 +00004473/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
4474/// This can cause recursive merging of nodes in the DAG.
4475///
4476/// This version can replace From with any result values. To must match the
4477/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00004478void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Dan Gohman475871a2008-07-27 21:46:04 +00004479 const SDValue *To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004480 DAGUpdateListener *UpdateListener) {
Chris Lattner11d049c2008-02-03 03:35:22 +00004481 if (From->getNumValues() == 1) // Handle the simple case efficiently.
Dan Gohman475871a2008-07-27 21:46:04 +00004482 return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004483
Dan Gohmandbe664a2009-01-19 21:44:21 +00004484 // Iterate over just the existing users of From. See the comments in
4485 // the ReplaceAllUsesWith above.
4486 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
4487 while (UI != UE) {
Dan Gohman89684502008-07-27 20:43:25 +00004488 SDNode *U = *UI;
Dan Gohmandbe664a2009-01-19 21:44:21 +00004489 do ++UI; while (UI != UE && *UI == U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004490
Chris Lattner7b2880c2005-08-24 22:44:39 +00004491 // This node is about to morph, remove its old self from the CSE maps.
4492 RemoveNodeFromCSEMaps(U);
Roman Levensteindc1adac2008-04-07 10:06:32 +00004493 int operandNum = 0;
4494 for (SDNode::op_iterator I = U->op_begin(), E = U->op_end();
4495 I != E; ++I, ++operandNum)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004496 if (I->getVal() == From) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004497 const SDValue &ToOp = To[I->getSDValue().getResNo()];
Roman Levensteindc1adac2008-04-07 10:06:32 +00004498 From->removeUser(operandNum, U);
Chris Lattner65113b22005-11-08 22:07:03 +00004499 *I = ToOp;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004500 I->setUser(U);
Gabor Greifba36cb52008-08-28 21:40:38 +00004501 ToOp.getNode()->addUser(operandNum, U);
Chris Lattner7b2880c2005-08-24 22:44:39 +00004502 }
Roman Levensteindc1adac2008-04-07 10:06:32 +00004503
Chris Lattner7b2880c2005-08-24 22:44:39 +00004504 // Now that we have modified U, add it back to the CSE maps. If it already
4505 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00004506 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004507 ReplaceAllUsesWith(U, Existing, UpdateListener);
4508 // U is now dead. Inform the listener if it exists and delete it.
4509 if (UpdateListener)
Duncan Sandsedfcf592008-06-11 11:42:12 +00004510 UpdateListener->NodeDeleted(U, Existing);
Chris Lattner1e111c72005-09-07 05:37:01 +00004511 DeleteNodeNotInCSEMaps(U);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004512 } else {
4513 // If the node doesn't already exist, we updated it. Inform a listener if
4514 // it exists.
4515 if (UpdateListener)
4516 UpdateListener->NodeUpdated(U);
Chris Lattner1e111c72005-09-07 05:37:01 +00004517 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00004518 }
4519}
4520
Chris Lattner012f2412006-02-17 21:58:01 +00004521/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004522/// uses of other values produced by From.getVal() alone. The Deleted vector is
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004523/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004524void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004525 DAGUpdateListener *UpdateListener){
Dan Gohmane8be6c62008-07-17 19:10:17 +00004526 // Handle the really simple, really trivial case efficiently.
4527 if (From == To) return;
4528
Chris Lattner012f2412006-02-17 21:58:01 +00004529 // Handle the simple, trivial, case efficiently.
Gabor Greifba36cb52008-08-28 21:40:38 +00004530 if (From.getNode()->getNumValues() == 1) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004531 ReplaceAllUsesWith(From, To, UpdateListener);
Chris Lattner012f2412006-02-17 21:58:01 +00004532 return;
4533 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004534
Gabor Greifba36cb52008-08-28 21:40:38 +00004535 // Get all of the users of From.getNode(). We want these in a nice,
Chris Lattnercf5640b2007-02-04 00:14:31 +00004536 // deterministically ordered and uniqued set, so we use a SmallSetVector.
Gabor Greifba36cb52008-08-28 21:40:38 +00004537 SmallSetVector<SDNode*, 16> Users(From.getNode()->use_begin(), From.getNode()->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00004538
4539 while (!Users.empty()) {
4540 // We know that this user uses some value of From. If it is the right
4541 // value, update it.
4542 SDNode *User = Users.back();
4543 Users.pop_back();
4544
Chris Lattner01d029b2007-10-15 06:10:22 +00004545 // Scan for an operand that matches From.
Roman Levensteindc1adac2008-04-07 10:06:32 +00004546 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
Chris Lattner01d029b2007-10-15 06:10:22 +00004547 for (; Op != E; ++Op)
4548 if (*Op == From) break;
4549
4550 // If there are no matches, the user must use some other result of From.
4551 if (Op == E) continue;
4552
4553 // Okay, we know this user needs to be updated. Remove its old self
4554 // from the CSE maps.
4555 RemoveNodeFromCSEMaps(User);
4556
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004557 // Update all operands that match "From" in case there are multiple uses.
Chris Lattner01d029b2007-10-15 06:10:22 +00004558 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00004559 if (*Op == From) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004560 From.getNode()->removeUser(Op-User->op_begin(), User);
Gabor Greif78256a12008-04-11 09:34:57 +00004561 *Op = To;
Roman Levensteindc1adac2008-04-07 10:06:32 +00004562 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004563 To.getNode()->addUser(Op-User->op_begin(), User);
Chris Lattner012f2412006-02-17 21:58:01 +00004564 }
4565 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004566
4567 // Now that we have modified User, add it back to the CSE maps. If it
4568 // already exists there, recursively merge the results together.
4569 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004570 if (!Existing) {
4571 if (UpdateListener) UpdateListener->NodeUpdated(User);
4572 continue; // Continue on to next user.
4573 }
Chris Lattner01d029b2007-10-15 06:10:22 +00004574
4575 // If there was already an existing matching node, use ReplaceAllUsesWith
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004576 // to replace the dead one with the existing one. This can cause
Dan Gohmane8be6c62008-07-17 19:10:17 +00004577 // recursive merging of other unrelated nodes down the line.
4578 ReplaceAllUsesWith(User, Existing, UpdateListener);
4579
4580 // User is now dead. Notify a listener if present.
4581 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
4582 DeleteNodeNotInCSEMaps(User);
4583 }
4584}
4585
4586/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
Gabor Greifba36cb52008-08-28 21:40:38 +00004587/// uses of other values produced by From.getVal() alone. The same value may
Dan Gohmane8be6c62008-07-17 19:10:17 +00004588/// appear in both the From and To list. The Deleted vector is
4589/// handled the same way as for ReplaceAllUsesWith.
Dan Gohman475871a2008-07-27 21:46:04 +00004590void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
4591 const SDValue *To,
Dan Gohmane8be6c62008-07-17 19:10:17 +00004592 unsigned Num,
4593 DAGUpdateListener *UpdateListener){
4594 // Handle the simple, trivial case efficiently.
4595 if (Num == 1)
4596 return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
4597
4598 SmallVector<std::pair<SDNode *, unsigned>, 16> Users;
4599 for (unsigned i = 0; i != Num; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00004600 for (SDNode::use_iterator UI = From[i].getNode()->use_begin(),
4601 E = From[i].getNode()->use_end(); UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004602 Users.push_back(std::make_pair(*UI, i));
Dan Gohmane8be6c62008-07-17 19:10:17 +00004603
4604 while (!Users.empty()) {
4605 // We know that this user uses some value of From. If it is the right
4606 // value, update it.
4607 SDNode *User = Users.back().first;
4608 unsigned i = Users.back().second;
4609 Users.pop_back();
4610
4611 // Scan for an operand that matches From.
4612 SDNode::op_iterator Op = User->op_begin(), E = User->op_end();
4613 for (; Op != E; ++Op)
4614 if (*Op == From[i]) break;
4615
4616 // If there are no matches, the user must use some other result of From.
4617 if (Op == E) continue;
4618
4619 // Okay, we know this user needs to be updated. Remove its old self
4620 // from the CSE maps.
4621 RemoveNodeFromCSEMaps(User);
4622
4623 // Update all operands that match "From" in case there are multiple uses.
4624 for (; Op != E; ++Op) {
4625 if (*Op == From[i]) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004626 From[i].getNode()->removeUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004627 *Op = To[i];
4628 Op->setUser(User);
Gabor Greifba36cb52008-08-28 21:40:38 +00004629 To[i].getNode()->addUser(Op-User->op_begin(), User);
Dan Gohmane8be6c62008-07-17 19:10:17 +00004630 }
4631 }
4632
4633 // Now that we have modified User, add it back to the CSE maps. If it
4634 // already exists there, recursively merge the results together.
4635 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
4636 if (!Existing) {
4637 if (UpdateListener) UpdateListener->NodeUpdated(User);
4638 continue; // Continue on to next user.
4639 }
4640
4641 // If there was already an existing matching node, use ReplaceAllUsesWith
4642 // to replace the dead one with the existing one. This can cause
4643 // recursive merging of other unrelated nodes down the line.
4644 ReplaceAllUsesWith(User, Existing, UpdateListener);
Chris Lattner01d029b2007-10-15 06:10:22 +00004645
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004646 // User is now dead. Notify a listener if present.
Duncan Sandsedfcf592008-06-11 11:42:12 +00004647 if (UpdateListener) UpdateListener->NodeDeleted(User, Existing);
Chris Lattner01d029b2007-10-15 06:10:22 +00004648 DeleteNodeNotInCSEMaps(User);
Chris Lattner012f2412006-02-17 21:58:01 +00004649 }
4650}
4651
Evan Chenge6f35d82006-08-01 08:20:41 +00004652/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00004653/// based on their topological order. It returns the maximum id and a vector
4654/// of the SDNodes* in assigned order by reference.
Dan Gohmanf06c8352008-09-30 18:30:35 +00004655unsigned SelectionDAG::AssignTopologicalOrder() {
Evan Chengc384d6c2006-08-02 22:00:34 +00004656
Dan Gohmanf06c8352008-09-30 18:30:35 +00004657 unsigned DAGSize = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00004658
Dan Gohmanf06c8352008-09-30 18:30:35 +00004659 // SortedPos tracks the progress of the algorithm. Nodes before it are
4660 // sorted, nodes after it are unsorted. When the algorithm completes
4661 // it is at the end of the list.
4662 allnodes_iterator SortedPos = allnodes_begin();
4663
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004664 // Visit all the nodes. Move nodes with no operands to the front of
4665 // the list immediately. Annotate nodes that do have operands with their
Dan Gohmanf06c8352008-09-30 18:30:35 +00004666 // operand count. Before we do this, the Node Id fields of the nodes
4667 // may contain arbitrary values. After, the Node Id fields for nodes
4668 // before SortedPos will contain the topological sort index, and the
4669 // Node Id fields for nodes At SortedPos and after will contain the
4670 // count of outstanding operands.
4671 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
4672 SDNode *N = I++;
4673 unsigned Degree = N->getNumOperands();
4674 if (Degree == 0) {
4675 // A node with no uses, add it to the result array immediately.
4676 N->setNodeId(DAGSize++);
4677 allnodes_iterator Q = N;
4678 if (Q != SortedPos)
4679 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
4680 ++SortedPos;
4681 } else {
4682 // Temporarily use the Node Id as scratch space for the degree count.
4683 N->setNodeId(Degree);
Evan Chenge6f35d82006-08-01 08:20:41 +00004684 }
4685 }
4686
Dan Gohmanf06c8352008-09-30 18:30:35 +00004687 // Visit all the nodes. As we iterate, moves nodes into sorted order,
4688 // such that by the time the end is reached all nodes will be sorted.
4689 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
4690 SDNode *N = I;
4691 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4692 UI != UE; ++UI) {
4693 SDNode *P = *UI;
4694 unsigned Degree = P->getNodeId();
4695 --Degree;
4696 if (Degree == 0) {
4697 // All of P's operands are sorted, so P may sorted now.
4698 P->setNodeId(DAGSize++);
4699 if (P != SortedPos)
4700 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
4701 ++SortedPos;
4702 } else {
4703 // Update P's outstanding operand count.
4704 P->setNodeId(Degree);
4705 }
4706 }
4707 }
4708
4709 assert(SortedPos == AllNodes.end() &&
4710 "Topological sort incomplete!");
4711 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
4712 "First node in topological sort is not the entry token!");
4713 assert(AllNodes.front().getNodeId() == 0 &&
4714 "First node in topological sort has non-zero id!");
4715 assert(AllNodes.front().getNumOperands() == 0 &&
4716 "First node in topological sort has operands!");
4717 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
4718 "Last node in topologic sort has unexpected id!");
4719 assert(AllNodes.back().use_empty() &&
4720 "Last node in topologic sort has users!");
Dan Gohman3ebd0ee2008-11-21 19:10:41 +00004721 assert(DAGSize == allnodes_size() && "Node count mismatch!");
Dan Gohmanf06c8352008-09-30 18:30:35 +00004722 return DAGSize;
Evan Chenge6f35d82006-08-01 08:20:41 +00004723}
4724
4725
Evan Cheng091cba12006-07-27 06:39:06 +00004726
Jim Laskey58b968b2005-08-17 20:08:02 +00004727//===----------------------------------------------------------------------===//
4728// SDNode Class
4729//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00004730
Chris Lattner48b85922007-02-04 02:41:42 +00004731HandleSDNode::~HandleSDNode() {
Dan Gohman0fe9c6e2008-07-07 20:57:48 +00004732 DropOperands();
Chris Lattner48b85922007-02-04 02:41:42 +00004733}
4734
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004735GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
Dan Gohman6520e202008-10-18 02:06:02 +00004736 MVT VT, int64_t o)
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004737 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00004738 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004739 // Thread Local
4740 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
4741 // Non Thread Local
4742 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
4743 getSDVTList(VT)), Offset(o) {
4744 TheGlobal = const_cast<GlobalValue*>(GA);
4745}
Chris Lattner48b85922007-02-04 02:41:42 +00004746
Dan Gohman1ea58a52008-07-09 22:08:04 +00004747MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, MVT memvt,
Dan Gohman492f2762008-07-09 21:23:02 +00004748 const Value *srcValue, int SVO,
4749 unsigned alignment, bool vol)
Dan Gohman1ea58a52008-07-09 22:08:04 +00004750 : SDNode(Opc, VTs), MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004751 Flags(encodeMemSDNodeFlags(vol, alignment)) {
Dan Gohman492f2762008-07-09 21:23:02 +00004752
4753 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4754 assert(getAlignment() == alignment && "Alignment representation error!");
4755 assert(isVolatile() == vol && "Volatile representation error!");
4756}
4757
Mon P Wangc4d10212008-10-17 18:22:58 +00004758MemSDNode::MemSDNode(unsigned Opc, SDVTList VTs, const SDValue *Ops,
4759 unsigned NumOps, MVT memvt, const Value *srcValue,
4760 int SVO, unsigned alignment, bool vol)
4761 : SDNode(Opc, VTs, Ops, NumOps),
4762 MemoryVT(memvt), SrcValue(srcValue), SVOffset(SVO),
4763 Flags(vol | ((Log2_32(alignment) + 1) << 1)) {
4764 assert(isPowerOf2_32(alignment) && "Alignment is not a power of 2!");
4765 assert(getAlignment() == alignment && "Alignment representation error!");
4766 assert(isVolatile() == vol && "Volatile representation error!");
4767}
4768
Dan Gohman36b5c132008-04-07 19:35:22 +00004769/// getMemOperand - Return a MachineMemOperand object describing the memory
Dan Gohman1ea58a52008-07-09 22:08:04 +00004770/// reference performed by this memory reference.
4771MachineMemOperand MemSDNode::getMemOperand() const {
Dale Johannesenfea90882008-10-24 01:06:58 +00004772 int Flags = 0;
Dan Gohman1ea58a52008-07-09 22:08:04 +00004773 if (isa<LoadSDNode>(this))
4774 Flags = MachineMemOperand::MOLoad;
4775 else if (isa<StoreSDNode>(this))
4776 Flags = MachineMemOperand::MOStore;
Mon P Wangc4d10212008-10-17 18:22:58 +00004777 else if (isa<AtomicSDNode>(this)) {
Dan Gohman1ea58a52008-07-09 22:08:04 +00004778 Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
4779 }
Mon P Wangc4d10212008-10-17 18:22:58 +00004780 else {
4781 const MemIntrinsicSDNode* MemIntrinNode = dyn_cast<MemIntrinsicSDNode>(this);
4782 assert(MemIntrinNode && "Unknown MemSDNode opcode!");
4783 if (MemIntrinNode->readMem()) Flags |= MachineMemOperand::MOLoad;
4784 if (MemIntrinNode->writeMem()) Flags |= MachineMemOperand::MOStore;
4785 }
Dan Gohman1ea58a52008-07-09 22:08:04 +00004786
4787 int Size = (getMemoryVT().getSizeInBits() + 7) >> 3;
Mon P Wang28873102008-06-25 08:15:39 +00004788 if (isVolatile()) Flags |= MachineMemOperand::MOVolatile;
4789
Dan Gohman1ea58a52008-07-09 22:08:04 +00004790 // Check if the memory reference references a frame index
Mon P Wang28873102008-06-25 08:15:39 +00004791 const FrameIndexSDNode *FI =
Gabor Greifba36cb52008-08-28 21:40:38 +00004792 dyn_cast<const FrameIndexSDNode>(getBasePtr().getNode());
Mon P Wang28873102008-06-25 08:15:39 +00004793 if (!getSrcValue() && FI)
Dan Gohmana54cf172008-07-11 22:44:52 +00004794 return MachineMemOperand(PseudoSourceValue::getFixedStack(FI->getIndex()),
4795 Flags, 0, Size, getAlignment());
Mon P Wang28873102008-06-25 08:15:39 +00004796 else
4797 return MachineMemOperand(getSrcValue(), Flags, getSrcValueOffset(),
4798 Size, getAlignment());
4799}
4800
Jim Laskey583bd472006-10-27 23:46:08 +00004801/// Profile - Gather unique data for the node.
4802///
Dan Gohmanb8d2f552008-08-20 15:58:01 +00004803void SDNode::Profile(FoldingSetNodeID &ID) const {
Jim Laskey583bd472006-10-27 23:46:08 +00004804 AddNodeIDNode(ID, this);
4805}
4806
Chris Lattnera3255112005-11-08 23:30:28 +00004807/// getValueTypeList - Return a pointer to the specified value type.
4808///
Duncan Sands83ec4b62008-06-06 12:08:01 +00004809const MVT *SDNode::getValueTypeList(MVT VT) {
4810 if (VT.isExtended()) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004811 static std::set<MVT, MVT::compareRawBits> EVTs;
Dan Gohman547ca532008-02-08 03:26:46 +00004812 return &(*EVTs.insert(VT).first);
Duncan Sandsaf47b112007-10-16 09:56:48 +00004813 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004814 static MVT VTs[MVT::LAST_VALUETYPE];
4815 VTs[VT.getSimpleVT()] = VT;
4816 return &VTs[VT.getSimpleVT()];
Duncan Sandsaf47b112007-10-16 09:56:48 +00004817 }
Chris Lattnera3255112005-11-08 23:30:28 +00004818}
Duncan Sandsaf47b112007-10-16 09:56:48 +00004819
Chris Lattner5c884562005-01-12 18:37:47 +00004820/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
4821/// indicated value. This method ignores uses of other values defined by this
4822/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00004823bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00004824 assert(Value < getNumValues() && "Bad value!");
4825
Roman Levensteindc1adac2008-04-07 10:06:32 +00004826 // TODO: Only iterate over uses of a given value of the node
4827 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
Gabor Greif99a6cb92008-08-26 22:36:50 +00004828 if (UI.getUse().getSDValue().getResNo() == Value) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004829 if (NUses == 0)
4830 return false;
4831 --NUses;
4832 }
Chris Lattner5c884562005-01-12 18:37:47 +00004833 }
4834
4835 // Found exactly the right number of uses?
4836 return NUses == 0;
4837}
4838
4839
Evan Cheng33d55952007-08-02 05:29:38 +00004840/// hasAnyUseOfValue - Return true if there are any use of the indicated
4841/// value. This method ignores uses of other values defined by this operation.
4842bool SDNode::hasAnyUseOfValue(unsigned Value) const {
4843 assert(Value < getNumValues() && "Bad value!");
4844
Dan Gohman1373c1c2008-07-09 22:39:01 +00004845 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
Gabor Greif99a6cb92008-08-26 22:36:50 +00004846 if (UI.getUse().getSDValue().getResNo() == Value)
Dan Gohman1373c1c2008-07-09 22:39:01 +00004847 return true;
Evan Cheng33d55952007-08-02 05:29:38 +00004848
4849 return false;
4850}
4851
4852
Dan Gohman2a629952008-07-27 18:06:42 +00004853/// isOnlyUserOf - Return true if this node is the only use of N.
Evan Chenge6e97e62006-11-03 07:31:32 +00004854///
Dan Gohman2a629952008-07-27 18:06:42 +00004855bool SDNode::isOnlyUserOf(SDNode *N) const {
Evan Cheng4ee62112006-02-05 06:29:23 +00004856 bool Seen = false;
4857 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004858 SDNode *User = *I;
Evan Cheng4ee62112006-02-05 06:29:23 +00004859 if (User == this)
4860 Seen = true;
4861 else
4862 return false;
4863 }
4864
4865 return Seen;
4866}
4867
Evan Chenge6e97e62006-11-03 07:31:32 +00004868/// isOperand - Return true if this node is an operand of N.
4869///
Dan Gohman475871a2008-07-27 21:46:04 +00004870bool SDValue::isOperandOf(SDNode *N) const {
Evan Chengbfa284f2006-03-03 06:42:32 +00004871 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
4872 if (*this == N->getOperand(i))
4873 return true;
4874 return false;
4875}
4876
Evan Cheng917be682008-03-04 00:41:45 +00004877bool SDNode::isOperandOf(SDNode *N) const {
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004878 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
Roman Levenstein9cac5252008-04-16 16:15:27 +00004879 if (this == N->OperandList[i].getVal())
Evan Cheng80d8eaa2006-03-03 06:24:54 +00004880 return true;
4881 return false;
4882}
Evan Cheng4ee62112006-02-05 06:29:23 +00004883
Chris Lattner572dee72008-01-16 05:49:24 +00004884/// reachesChainWithoutSideEffects - Return true if this operand (which must
4885/// be a chain) reaches the specified operand without crossing any
4886/// side-effecting instructions. In practice, this looks through token
4887/// factors and non-volatile loads. In order to remain efficient, this only
4888/// looks a couple of nodes in, it does not do an exhaustive search.
Dan Gohman475871a2008-07-27 21:46:04 +00004889bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
Chris Lattner572dee72008-01-16 05:49:24 +00004890 unsigned Depth) const {
4891 if (*this == Dest) return true;
4892
4893 // Don't search too deeply, we just want to be able to see through
4894 // TokenFactor's etc.
4895 if (Depth == 0) return false;
4896
4897 // If this is a token factor, all inputs to the TF happen in parallel. If any
4898 // of the operands of the TF reach dest, then we can do the xform.
4899 if (getOpcode() == ISD::TokenFactor) {
4900 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4901 if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
4902 return true;
4903 return false;
4904 }
4905
4906 // Loads don't have side effects, look through them.
4907 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
4908 if (!Ld->isVolatile())
4909 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
4910 }
4911 return false;
4912}
4913
4914
Evan Chengc5fc57d2006-11-03 03:05:24 +00004915static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004916 SmallPtrSet<SDNode *, 32> &Visited) {
4917 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00004918 return;
4919
4920 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004921 SDNode *Op = N->getOperand(i).getNode();
Evan Chengc5fc57d2006-11-03 03:05:24 +00004922 if (Op == P) {
4923 found = true;
4924 return;
4925 }
4926 findPredecessor(Op, P, found, Visited);
4927 }
4928}
4929
Evan Cheng917be682008-03-04 00:41:45 +00004930/// isPredecessorOf - Return true if this node is a predecessor of N. This node
Evan Chenge6e97e62006-11-03 07:31:32 +00004931/// is either an operand of N or it can be reached by recursively traversing
4932/// up the operands.
4933/// NOTE: this is an expensive method. Use it carefully.
Evan Cheng917be682008-03-04 00:41:45 +00004934bool SDNode::isPredecessorOf(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00004935 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00004936 bool found = false;
4937 findPredecessor(N, this, found, Visited);
4938 return found;
4939}
4940
Evan Chengc5484282006-10-04 00:56:09 +00004941uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
4942 assert(Num < NumOperands && "Invalid child # of SDNode!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004943 return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
Evan Chengc5484282006-10-04 00:56:09 +00004944}
4945
Reid Spencer577cc322007-04-01 07:32:19 +00004946std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004947 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004948 default:
4949 if (getOpcode() < ISD::BUILTIN_OP_END)
4950 return "<<Unknown DAG Node>>";
Dan Gohmane8be6c62008-07-17 19:10:17 +00004951 if (isMachineOpcode()) {
4952 if (G)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004953 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Dan Gohmane8be6c62008-07-17 19:10:17 +00004954 if (getMachineOpcode() < TII->getNumOpcodes())
4955 return TII->get(getMachineOpcode()).getName();
4956 return "<<Unknown Machine Node>>";
4957 }
4958 if (G) {
Dan Gohmane9530ec2009-01-15 16:58:17 +00004959 const TargetLowering &TLI = G->getTargetLoweringInfo();
Dan Gohmane8be6c62008-07-17 19:10:17 +00004960 const char *Name = TLI.getTargetNodeName(getOpcode());
4961 if (Name) return Name;
Evan Cheng72261582005-12-20 06:22:03 +00004962 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004963 }
Dan Gohmane8be6c62008-07-17 19:10:17 +00004964 return "<<Unknown Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00004965
Dan Gohmane8be6c62008-07-17 19:10:17 +00004966#ifndef NDEBUG
4967 case ISD::DELETED_NODE:
4968 return "<<Deleted Node!>>";
4969#endif
Evan Cheng27b7db52008-03-08 00:58:38 +00004970 case ISD::PREFETCH: return "Prefetch";
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00004971 case ISD::MEMBARRIER: return "MemBarrier";
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004972 case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
4973 case ISD::ATOMIC_SWAP: return "AtomicSwap";
4974 case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
4975 case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
4976 case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
4977 case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
4978 case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
4979 case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
4980 case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
4981 case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
4982 case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
4983 case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
Andrew Lenharth95762122005-03-31 21:24:06 +00004984 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00004985 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00004986 case ISD::SRCVALUE: return "SrcValue";
Dan Gohman69de1932008-02-06 22:27:42 +00004987 case ISD::MEMOPERAND: return "MemOperand";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004988 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00004989 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00004990 case ISD::AssertSext: return "AssertSext";
4991 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004992
Chris Lattnerd75f19f2005-01-10 23:25:25 +00004993 case ISD::BasicBlock: return "BasicBlock";
Duncan Sands276dcbd2008-03-21 09:14:45 +00004994 case ISD::ARG_FLAGS: return "ArgFlags";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004995 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004996 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00004997
4998 case ISD::Constant: return "Constant";
4999 case ISD::ConstantFP: return "ConstantFP";
5000 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005001 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005002 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005003 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00005004 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Bill Wendling056292f2008-09-16 21:48:12 +00005005 case ISD::RETURNADDR: return "RETURNADDR";
5006 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005007 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00005008 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
Bill Wendling056292f2008-09-16 21:48:12 +00005009 case ISD::EHSELECTION: return "EHSELECTION";
5010 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00005011 case ISD::ConstantPool: return "ConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005012 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00005013 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005014 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getZExtValue();
Chris Lattner48b61a72006-03-28 00:40:33 +00005015 return Intrinsic::getName((Intrinsic::ID)IID);
5016 }
5017 case ISD::INTRINSIC_VOID:
5018 case ISD::INTRINSIC_W_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005019 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getZExtValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00005020 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00005021 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00005022
Chris Lattnerb2827b02006-03-19 00:52:58 +00005023 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005024 case ISD::TargetConstant: return "TargetConstant";
5025 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005026 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005027 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005028 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00005029 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00005030 case ISD::TargetConstantPool: return "TargetConstantPool";
Bill Wendling056292f2008-09-16 21:48:12 +00005031 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00005032
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005033 case ISD::CopyToReg: return "CopyToReg";
5034 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00005035 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00005036 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005037 case ISD::INLINEASM: return "inlineasm";
Dan Gohman44066042008-07-01 00:05:16 +00005038 case ISD::DBG_LABEL: return "dbg_label";
5039 case ISD::EH_LABEL: return "eh_label";
Evan Chenga844bde2008-02-02 04:07:54 +00005040 case ISD::DECLARE: return "declare";
Evan Cheng9fda2f92006-02-03 01:33:01 +00005041 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00005042 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00005043 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00005044
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005045 // Unary operators
5046 case ISD::FABS: return "fabs";
5047 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00005048 case ISD::FSQRT: return "fsqrt";
5049 case ISD::FSIN: return "fsin";
5050 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00005051 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00005052 case ISD::FPOW: return "fpow";
Dan Gohman509e84f2008-08-21 17:55:02 +00005053 case ISD::FTRUNC: return "ftrunc";
5054 case ISD::FFLOOR: return "ffloor";
5055 case ISD::FCEIL: return "fceil";
5056 case ISD::FRINT: return "frint";
5057 case ISD::FNEARBYINT: return "fnearbyint";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00005058
5059 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005060 case ISD::ADD: return "add";
5061 case ISD::SUB: return "sub";
5062 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00005063 case ISD::MULHU: return "mulhu";
5064 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005065 case ISD::SDIV: return "sdiv";
5066 case ISD::UDIV: return "udiv";
5067 case ISD::SREM: return "srem";
5068 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00005069 case ISD::SMUL_LOHI: return "smul_lohi";
5070 case ISD::UMUL_LOHI: return "umul_lohi";
5071 case ISD::SDIVREM: return "sdivrem";
Dan Gohmana47916d2008-09-08 16:30:29 +00005072 case ISD::UDIVREM: return "udivrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005073 case ISD::AND: return "and";
5074 case ISD::OR: return "or";
5075 case ISD::XOR: return "xor";
5076 case ISD::SHL: return "shl";
5077 case ISD::SRA: return "sra";
5078 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00005079 case ISD::ROTL: return "rotl";
5080 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00005081 case ISD::FADD: return "fadd";
5082 case ISD::FSUB: return "fsub";
5083 case ISD::FMUL: return "fmul";
5084 case ISD::FDIV: return "fdiv";
5085 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00005086 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattnerd268a492007-12-22 21:26:52 +00005087 case ISD::FGETSIGN: return "fgetsign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00005088
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005089 case ISD::SETCC: return "setcc";
Nate Begemanb43e9c12008-05-12 19:40:03 +00005090 case ISD::VSETCC: return "vsetcc";
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005091 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00005092 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005093 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005094 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00005095 case ISD::CONCAT_VECTORS: return "concat_vectors";
5096 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005097 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00005098 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00005099 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005100 case ISD::ADDC: return "addc";
5101 case ISD::ADDE: return "adde";
Bill Wendling1c55a9c2008-11-21 02:12:42 +00005102 case ISD::SADDO: return "saddo";
5103 case ISD::UADDO: return "uaddo";
Bill Wendling74c37652008-12-09 22:08:41 +00005104 case ISD::SSUBO: return "ssubo";
5105 case ISD::USUBO: return "usubo";
5106 case ISD::SMULO: return "smulo";
5107 case ISD::UMULO: return "umulo";
Nate Begeman551bf3f2006-02-17 05:43:56 +00005108 case ISD::SUBC: return "subc";
5109 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00005110 case ISD::SHL_PARTS: return "shl_parts";
5111 case ISD::SRA_PARTS: return "sra_parts";
5112 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00005113
5114 case ISD::EXTRACT_SUBREG: return "extract_subreg";
5115 case ISD::INSERT_SUBREG: return "insert_subreg";
5116
Chris Lattner7f644642005-04-28 21:44:03 +00005117 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005118 case ISD::SIGN_EXTEND: return "sign_extend";
5119 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00005120 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00005121 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005122 case ISD::TRUNCATE: return "truncate";
5123 case ISD::FP_ROUND: return "fp_round";
Dan Gohman1a024862008-01-31 00:41:03 +00005124 case ISD::FLT_ROUNDS_: return "flt_rounds";
Chris Lattner859157d2005-01-15 06:17:04 +00005125 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005126 case ISD::FP_EXTEND: return "fp_extend";
5127
5128 case ISD::SINT_TO_FP: return "sint_to_fp";
5129 case ISD::UINT_TO_FP: return "uint_to_fp";
5130 case ISD::FP_TO_SINT: return "fp_to_sint";
5131 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00005132 case ISD::BIT_CONVERT: return "bit_convert";
Mon P Wang77cdf302008-11-10 20:54:11 +00005133
5134 case ISD::CONVERT_RNDSAT: {
5135 switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
5136 default: assert(0 && "Unknown cvt code!");
5137 case ISD::CVT_FF: return "cvt_ff";
5138 case ISD::CVT_FS: return "cvt_fs";
5139 case ISD::CVT_FU: return "cvt_fu";
5140 case ISD::CVT_SF: return "cvt_sf";
5141 case ISD::CVT_UF: return "cvt_uf";
5142 case ISD::CVT_SS: return "cvt_ss";
5143 case ISD::CVT_SU: return "cvt_su";
5144 case ISD::CVT_US: return "cvt_us";
5145 case ISD::CVT_UU: return "cvt_uu";
5146 }
5147 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005148
5149 // Control flow instructions
5150 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00005151 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00005152 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005153 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00005154 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005155 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00005156 case ISD::CALLSEQ_START: return "callseq_start";
5157 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005158
5159 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00005160 case ISD::LOAD: return "load";
5161 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00005162 case ISD::VAARG: return "vaarg";
5163 case ISD::VACOPY: return "vacopy";
5164 case ISD::VAEND: return "vaend";
5165 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005166 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00005167 case ISD::EXTRACT_ELEMENT: return "extract_element";
5168 case ISD::BUILD_PAIR: return "build_pair";
5169 case ISD::STACKSAVE: return "stacksave";
5170 case ISD::STACKRESTORE: return "stackrestore";
Anton Korobeynikov66fac792008-01-15 07:02:33 +00005171 case ISD::TRAP: return "trap";
5172
Nate Begeman1b5db7a2006-01-16 08:07:10 +00005173 // Bit manipulation
5174 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00005175 case ISD::CTPOP: return "ctpop";
5176 case ISD::CTTZ: return "cttz";
5177 case ISD::CTLZ: return "ctlz";
5178
Chris Lattner36ce6912005-11-29 06:21:05 +00005179 // Debug info
Dan Gohman7f460202008-06-30 20:59:49 +00005180 case ISD::DBG_STOPPOINT: return "dbg_stoppoint";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00005181 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00005182
Duncan Sands36397f52007-07-27 12:58:54 +00005183 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00005184 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00005185
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005186 case ISD::CONDCODE:
5187 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005188 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005189 case ISD::SETOEQ: return "setoeq";
5190 case ISD::SETOGT: return "setogt";
5191 case ISD::SETOGE: return "setoge";
5192 case ISD::SETOLT: return "setolt";
5193 case ISD::SETOLE: return "setole";
5194 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005195
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005196 case ISD::SETO: return "seto";
5197 case ISD::SETUO: return "setuo";
5198 case ISD::SETUEQ: return "setue";
5199 case ISD::SETUGT: return "setugt";
5200 case ISD::SETUGE: return "setuge";
5201 case ISD::SETULT: return "setult";
5202 case ISD::SETULE: return "setule";
5203 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005204
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005205 case ISD::SETEQ: return "seteq";
5206 case ISD::SETGT: return "setgt";
5207 case ISD::SETGE: return "setge";
5208 case ISD::SETLT: return "setlt";
5209 case ISD::SETLE: return "setle";
5210 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00005211 }
5212 }
5213}
Chris Lattnerc3aae252005-01-07 07:46:32 +00005214
Evan Cheng144d8f02006-11-09 17:55:04 +00005215const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00005216 switch (AM) {
5217 default:
5218 return "";
5219 case ISD::PRE_INC:
5220 return "<pre-inc>";
5221 case ISD::PRE_DEC:
5222 return "<pre-dec>";
5223 case ISD::POST_INC:
5224 return "<post-inc>";
5225 case ISD::POST_DEC:
5226 return "<post-dec>";
5227 }
5228}
5229
Duncan Sands276dcbd2008-03-21 09:14:45 +00005230std::string ISD::ArgFlagsTy::getArgFlagsString() {
5231 std::string S = "< ";
5232
5233 if (isZExt())
5234 S += "zext ";
5235 if (isSExt())
5236 S += "sext ";
5237 if (isInReg())
5238 S += "inreg ";
5239 if (isSRet())
5240 S += "sret ";
5241 if (isByVal())
5242 S += "byval ";
5243 if (isNest())
5244 S += "nest ";
5245 if (getByValAlign())
5246 S += "byval-align:" + utostr(getByValAlign()) + " ";
5247 if (getOrigAlign())
5248 S += "orig-align:" + utostr(getOrigAlign()) + " ";
5249 if (getByValSize())
5250 S += "byval-size:" + utostr(getByValSize()) + " ";
5251 return S + ">";
5252}
5253
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005254void SDNode::dump() const { dump(0); }
5255void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner944fac72008-08-23 22:23:09 +00005256 print(errs(), G);
Chris Lattnerc56711c2008-08-24 18:28:30 +00005257 errs().flush();
Chris Lattner944fac72008-08-23 22:23:09 +00005258}
5259
5260void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
5261 OS << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005262
5263 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005264 if (i) OS << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00005265 if (getValueType(i) == MVT::Other)
Chris Lattner944fac72008-08-23 22:23:09 +00005266 OS << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00005267 else
Chris Lattner944fac72008-08-23 22:23:09 +00005268 OS << getValueType(i).getMVTString();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005269 }
Chris Lattner944fac72008-08-23 22:23:09 +00005270 OS << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005271
Chris Lattner944fac72008-08-23 22:23:09 +00005272 OS << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005273 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005274 if (i) OS << ", ";
Gabor Greifba36cb52008-08-28 21:40:38 +00005275 OS << (void*)getOperand(i).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00005276 if (unsigned RN = getOperand(i).getResNo())
Chris Lattner944fac72008-08-23 22:23:09 +00005277 OS << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00005278 }
5279
Evan Chengce254432007-12-11 02:08:35 +00005280 if (!isTargetOpcode() && getOpcode() == ISD::VECTOR_SHUFFLE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005281 SDNode *Mask = getOperand(2).getNode();
Chris Lattner944fac72008-08-23 22:23:09 +00005282 OS << "<";
Evan Chengce254432007-12-11 02:08:35 +00005283 for (unsigned i = 0, e = Mask->getNumOperands(); i != e; ++i) {
Chris Lattner944fac72008-08-23 22:23:09 +00005284 if (i) OS << ",";
Evan Chengce254432007-12-11 02:08:35 +00005285 if (Mask->getOperand(i).getOpcode() == ISD::UNDEF)
Chris Lattner944fac72008-08-23 22:23:09 +00005286 OS << "u";
Evan Chengce254432007-12-11 02:08:35 +00005287 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005288 OS << cast<ConstantSDNode>(Mask->getOperand(i))->getZExtValue();
Evan Chengce254432007-12-11 02:08:35 +00005289 }
Chris Lattner944fac72008-08-23 22:23:09 +00005290 OS << ">";
Evan Chengce254432007-12-11 02:08:35 +00005291 }
5292
Chris Lattnerc3aae252005-01-07 07:46:32 +00005293 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005294 OS << '<' << CSDN->getAPIntValue() << '>';
Chris Lattnerc3aae252005-01-07 07:46:32 +00005295 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005296 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
Chris Lattner944fac72008-08-23 22:23:09 +00005297 OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005298 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
Chris Lattner944fac72008-08-23 22:23:09 +00005299 OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005300 else {
Chris Lattner944fac72008-08-23 22:23:09 +00005301 OS << "<APFloat(";
Dale Johannesen23a98552008-10-09 23:00:39 +00005302 CSDN->getValueAPF().bitcastToAPInt().dump();
Chris Lattner944fac72008-08-23 22:23:09 +00005303 OS << ")>";
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005304 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00005305 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00005306 dyn_cast<GlobalAddressSDNode>(this)) {
Dan Gohman668aff62008-10-18 18:22:42 +00005307 int64_t offset = GADN->getOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005308 OS << '<';
5309 WriteAsOperand(OS, GADN->getGlobal());
5310 OS << '>';
Evan Cheng61ca74b2005-11-30 02:04:11 +00005311 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005312 OS << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00005313 else
Chris Lattner944fac72008-08-23 22:23:09 +00005314 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005315 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005316 OS << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00005317 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005318 OS << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005319 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00005320 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00005321 if (CP->isMachineConstantPoolEntry())
Chris Lattner944fac72008-08-23 22:23:09 +00005322 OS << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00005323 else
Chris Lattner944fac72008-08-23 22:23:09 +00005324 OS << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00005325 if (offset > 0)
Chris Lattner944fac72008-08-23 22:23:09 +00005326 OS << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00005327 else
Chris Lattner944fac72008-08-23 22:23:09 +00005328 OS << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00005329 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005330 OS << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005331 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
5332 if (LBB)
Chris Lattner944fac72008-08-23 22:23:09 +00005333 OS << LBB->getName() << " ";
5334 OS << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00005335 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +00005336 if (G && R->getReg() &&
5337 TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner944fac72008-08-23 22:23:09 +00005338 OS << " " << G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00005339 } else {
Chris Lattner944fac72008-08-23 22:23:09 +00005340 OS << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00005341 }
Bill Wendling056292f2008-09-16 21:48:12 +00005342 } else if (const ExternalSymbolSDNode *ES =
5343 dyn_cast<ExternalSymbolSDNode>(this)) {
5344 OS << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005345 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
5346 if (M->getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005347 OS << "<" << M->getValue() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00005348 else
Chris Lattner944fac72008-08-23 22:23:09 +00005349 OS << "<null>";
Dan Gohman69de1932008-02-06 22:27:42 +00005350 } else if (const MemOperandSDNode *M = dyn_cast<MemOperandSDNode>(this)) {
5351 if (M->MO.getValue())
Chris Lattner944fac72008-08-23 22:23:09 +00005352 OS << "<" << M->MO.getValue() << ":" << M->MO.getOffset() << ">";
Dan Gohman69de1932008-02-06 22:27:42 +00005353 else
Chris Lattner944fac72008-08-23 22:23:09 +00005354 OS << "<null:" << M->MO.getOffset() << ">";
Duncan Sands276dcbd2008-03-21 09:14:45 +00005355 } else if (const ARG_FLAGSSDNode *N = dyn_cast<ARG_FLAGSSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005356 OS << N->getArgFlags().getArgFlagsString();
Chris Lattnera23e8152005-08-18 03:31:02 +00005357 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Chris Lattner944fac72008-08-23 22:23:09 +00005358 OS << ":" << N->getVT().getMVTString();
Mon P Wang28873102008-06-25 08:15:39 +00005359 }
5360 else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
Evan Cheng81310132007-12-18 19:06:30 +00005361 const Value *SrcValue = LD->getSrcValue();
5362 int SrcOffset = LD->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005363 OS << " <";
Evan Cheng81310132007-12-18 19:06:30 +00005364 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005365 OS << SrcValue;
Evan Cheng81310132007-12-18 19:06:30 +00005366 else
Chris Lattner944fac72008-08-23 22:23:09 +00005367 OS << "null";
5368 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005369
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005370 bool doExt = true;
5371 switch (LD->getExtensionType()) {
5372 default: doExt = false; break;
Chris Lattner944fac72008-08-23 22:23:09 +00005373 case ISD::EXTLOAD: OS << " <anyext "; break;
5374 case ISD::SEXTLOAD: OS << " <sext "; break;
5375 case ISD::ZEXTLOAD: OS << " <zext "; break;
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005376 }
5377 if (doExt)
Chris Lattner944fac72008-08-23 22:23:09 +00005378 OS << LD->getMemoryVT().getMVTString() << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00005379
Evan Cheng144d8f02006-11-09 17:55:04 +00005380 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00005381 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005382 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005383 if (LD->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005384 OS << " <volatile>";
5385 OS << " alignment=" << LD->getAlignment();
Evan Cheng2caccca2006-10-17 21:14:32 +00005386 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
Evan Cheng88ce93e2007-12-18 07:02:08 +00005387 const Value *SrcValue = ST->getSrcValue();
5388 int SrcOffset = ST->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005389 OS << " <";
Evan Cheng88ce93e2007-12-18 07:02:08 +00005390 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005391 OS << SrcValue;
Evan Cheng88ce93e2007-12-18 07:02:08 +00005392 else
Chris Lattner944fac72008-08-23 22:23:09 +00005393 OS << "null";
5394 OS << ":" << SrcOffset << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005395
5396 if (ST->isTruncatingStore())
Chris Lattner944fac72008-08-23 22:23:09 +00005397 OS << " <trunc " << ST->getMemoryVT().getMVTString() << ">";
Evan Cheng81310132007-12-18 19:06:30 +00005398
5399 const char *AM = getIndexedModeName(ST->getAddressingMode());
5400 if (*AM)
Chris Lattner944fac72008-08-23 22:23:09 +00005401 OS << " " << AM;
Evan Cheng81310132007-12-18 19:06:30 +00005402 if (ST->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005403 OS << " <volatile>";
5404 OS << " alignment=" << ST->getAlignment();
Mon P Wang28873102008-06-25 08:15:39 +00005405 } else if (const AtomicSDNode* AT = dyn_cast<AtomicSDNode>(this)) {
5406 const Value *SrcValue = AT->getSrcValue();
5407 int SrcOffset = AT->getSrcValueOffset();
Chris Lattner944fac72008-08-23 22:23:09 +00005408 OS << " <";
Mon P Wang28873102008-06-25 08:15:39 +00005409 if (SrcValue)
Chris Lattner944fac72008-08-23 22:23:09 +00005410 OS << SrcValue;
Mon P Wang28873102008-06-25 08:15:39 +00005411 else
Chris Lattner944fac72008-08-23 22:23:09 +00005412 OS << "null";
5413 OS << ":" << SrcOffset << ">";
Mon P Wang28873102008-06-25 08:15:39 +00005414 if (AT->isVolatile())
Chris Lattner944fac72008-08-23 22:23:09 +00005415 OS << " <volatile>";
5416 OS << " alignment=" << AT->getAlignment();
Chris Lattnerc3aae252005-01-07 07:46:32 +00005417 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00005418}
5419
Chris Lattnerde202b32005-11-09 23:47:37 +00005420static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00005421 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00005422 if (N->getOperand(i).getNode()->hasOneUse())
5423 DumpNodes(N->getOperand(i).getNode(), indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005424 else
Bill Wendling832171c2006-12-07 20:04:42 +00005425 cerr << "\n" << std::string(indent+2, ' ')
Gabor Greifba36cb52008-08-28 21:40:38 +00005426 << (void*)N->getOperand(i).getNode() << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00005427
Chris Lattnerea946cd2005-01-09 20:38:33 +00005428
Bill Wendling832171c2006-12-07 20:04:42 +00005429 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00005430 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005431}
5432
Chris Lattnerc3aae252005-01-07 07:46:32 +00005433void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00005434 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00005435
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005436 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
5437 I != E; ++I) {
5438 const SDNode *N = I;
Gabor Greifba36cb52008-08-28 21:40:38 +00005439 if (!N->hasOneUse() && N != getRoot().getNode())
Dan Gohman90a7b8f2008-07-15 18:18:54 +00005440 DumpNodes(N, 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00005441 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00005442
Gabor Greifba36cb52008-08-28 21:40:38 +00005443 if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00005444
Bill Wendling832171c2006-12-07 20:04:42 +00005445 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00005446}
5447
Evan Chengd6594ae2006-09-12 21:00:35 +00005448const Type *ConstantPoolSDNode::getType() const {
5449 if (isMachineConstantPoolEntry())
5450 return Val.MachineCPVal->getType();
5451 return Val.ConstVal->getType();
5452}