blob: b1b3bd4fb856da120a1ac4f25b1be29bf0c59722 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000016#include "llvm/GlobalVariable.h"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Evan Chengd6594ae2006-09-12 21:00:35 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner37ce9df2007-10-15 17:47:20 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000023#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000024#include "llvm/Target/MRegisterInfo.h"
Christopher Lamb95c218a2007-04-22 23:15:30 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000026#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000027#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000029#include "llvm/ADT/SetVector.h"
Chris Lattnerd48c5e82007-02-04 00:24:41 +000030#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner190a4182006-08-04 17:45:20 +000031#include "llvm/ADT/SmallVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000032#include "llvm/ADT/StringExtras.h"
Jeff Cohenfd161e92005-01-09 20:41:56 +000033#include <algorithm>
Jeff Cohen97af7512006-12-02 02:22:01 +000034#include <cmath>
Chris Lattnere25738c2004-06-02 04:28:06 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner0b3e5252006-08-15 19:11:05 +000037/// makeVTList - Return an instance of the SDVTList struct initialized with the
38/// specified members.
39static SDVTList makeVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
40 SDVTList Res = {VTs, NumVTs};
41 return Res;
42}
43
Jim Laskey58b968b2005-08-17 20:08:02 +000044//===----------------------------------------------------------------------===//
45// ConstantFPSDNode Class
46//===----------------------------------------------------------------------===//
47
48/// isExactlyValue - We don't rely on operator== working on double values, as
49/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
50/// As such, this method can be used to do an exact bit-for-bit comparison of
51/// two floating point values.
Dale Johannesene6c17422007-08-26 01:18:27 +000052bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
Dale Johannesen87503a62007-08-25 22:10:57 +000053 return Value.bitwiseIsEqual(V);
Jim Laskey58b968b2005-08-17 20:08:02 +000054}
55
Dale Johannesenf04afdb2007-08-30 00:23:21 +000056bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT,
57 const APFloat& Val) {
58 // convert modifies in place, so make a copy.
59 APFloat Val2 = APFloat(Val);
60 switch (VT) {
61 default:
62 return false; // These can't be represented as floating point!
63
64 // FIXME rounding mode needs to be more flexible
65 case MVT::f32:
66 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
67 Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) ==
68 APFloat::opOK;
69 case MVT::f64:
70 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
71 &Val2.getSemantics() == &APFloat::IEEEdouble ||
72 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
73 APFloat::opOK;
74 // TODO: Figure out how to test if we can use a shorter type instead!
75 case MVT::f80:
76 case MVT::f128:
77 case MVT::ppcf128:
78 return true;
79 }
80}
81
Jim Laskey58b968b2005-08-17 20:08:02 +000082//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000083// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000084//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000085
Evan Chenga8df1662006-03-27 06:58:47 +000086/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000087/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000088bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000089 // Look through a bit convert.
90 if (N->getOpcode() == ISD::BIT_CONVERT)
91 N = N->getOperand(0).Val;
92
Evan Chenga8df1662006-03-27 06:58:47 +000093 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000094
95 unsigned i = 0, e = N->getNumOperands();
96
97 // Skip over all of the undef values.
98 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
99 ++i;
100
101 // Do not accept an all-undef vector.
102 if (i == e) return false;
103
104 // Do not accept build_vectors that aren't all constants or which have non-~0
105 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +0000106 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +0000107 if (isa<ConstantSDNode>(NotZero)) {
108 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
109 return false;
110 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +0000111 MVT::ValueType VT = NotZero.getValueType();
112 if (VT== MVT::f64) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000113 if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
114 convertToAPInt().getZExtValue())) != (uint64_t)-1)
Evan Cheng23cc8702006-03-27 08:10:26 +0000115 return false;
116 } else {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000117 if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
118 getValueAPF().convertToAPInt().getZExtValue() !=
Evan Cheng23cc8702006-03-27 08:10:26 +0000119 (uint32_t)-1)
120 return false;
121 }
Evan Chenga8df1662006-03-27 06:58:47 +0000122 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000123 return false;
124
125 // Okay, we have at least one ~0 value, check to see if the rest match or are
126 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000127 for (++i; i != e; ++i)
128 if (N->getOperand(i) != NotZero &&
129 N->getOperand(i).getOpcode() != ISD::UNDEF)
130 return false;
131 return true;
132}
133
134
Evan Cheng4a147842006-03-26 09:50:58 +0000135/// isBuildVectorAllZeros - Return true if the specified node is a
136/// BUILD_VECTOR where all of the elements are 0 or undef.
137bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000138 // Look through a bit convert.
139 if (N->getOpcode() == ISD::BIT_CONVERT)
140 N = N->getOperand(0).Val;
141
Evan Cheng4a147842006-03-26 09:50:58 +0000142 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000143
144 unsigned i = 0, e = N->getNumOperands();
145
146 // Skip over all of the undef values.
147 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
148 ++i;
149
150 // Do not accept an all-undef vector.
151 if (i == e) return false;
152
153 // Do not accept build_vectors that aren't all constants or which have non-~0
154 // elements.
155 SDOperand Zero = N->getOperand(i);
156 if (isa<ConstantSDNode>(Zero)) {
157 if (!cast<ConstantSDNode>(Zero)->isNullValue())
158 return false;
159 } else if (isa<ConstantFPSDNode>(Zero)) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000160 if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
Evan Chenga8df1662006-03-27 06:58:47 +0000161 return false;
162 } else
163 return false;
164
165 // Okay, we have at least one ~0 value, check to see if the rest match or are
166 // undefs.
167 for (++i; i != e; ++i)
168 if (N->getOperand(i) != Zero &&
169 N->getOperand(i).getOpcode() != ISD::UNDEF)
170 return false;
171 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000172}
173
Chris Lattnerc3aae252005-01-07 07:46:32 +0000174/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
175/// when given the operation for (X op Y).
176ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
177 // To perform this operation, we just need to swap the L and G bits of the
178 // operation.
179 unsigned OldL = (Operation >> 2) & 1;
180 unsigned OldG = (Operation >> 1) & 1;
181 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
182 (OldL << 1) | // New G bit
183 (OldG << 2)); // New L bit.
184}
185
186/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
187/// 'op' is a valid SetCC operation.
188ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
189 unsigned Operation = Op;
190 if (isInteger)
191 Operation ^= 7; // Flip L, G, E bits, but not U.
192 else
193 Operation ^= 15; // Flip all of the condition bits.
194 if (Operation > ISD::SETTRUE2)
195 Operation &= ~8; // Don't let N and U bits get set.
196 return ISD::CondCode(Operation);
197}
198
199
200/// isSignedOp - For an integer comparison, return 1 if the comparison is a
201/// signed operation and 2 if the result is an unsigned comparison. Return zero
202/// if the operation does not depend on the sign of the input (setne and seteq).
203static int isSignedOp(ISD::CondCode Opcode) {
204 switch (Opcode) {
205 default: assert(0 && "Illegal integer setcc operation!");
206 case ISD::SETEQ:
207 case ISD::SETNE: return 0;
208 case ISD::SETLT:
209 case ISD::SETLE:
210 case ISD::SETGT:
211 case ISD::SETGE: return 1;
212 case ISD::SETULT:
213 case ISD::SETULE:
214 case ISD::SETUGT:
215 case ISD::SETUGE: return 2;
216 }
217}
218
219/// getSetCCOrOperation - Return the result of a logical OR between different
220/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
221/// returns SETCC_INVALID if it is not possible to represent the resultant
222/// comparison.
223ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
224 bool isInteger) {
225 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
226 // Cannot fold a signed integer setcc with an unsigned integer setcc.
227 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000228
Chris Lattnerc3aae252005-01-07 07:46:32 +0000229 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000230
Chris Lattnerc3aae252005-01-07 07:46:32 +0000231 // If the N and U bits get set then the resultant comparison DOES suddenly
232 // care about orderedness, and is true when ordered.
233 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000234 Op &= ~16; // Clear the U bit if the N bit is set.
235
236 // Canonicalize illegal integer setcc's.
237 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
238 Op = ISD::SETNE;
239
Chris Lattnerc3aae252005-01-07 07:46:32 +0000240 return ISD::CondCode(Op);
241}
242
243/// getSetCCAndOperation - Return the result of a logical AND between different
244/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
245/// function returns zero if it is not possible to represent the resultant
246/// comparison.
247ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
248 bool isInteger) {
249 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
250 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000251 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000252
253 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000254 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
255
256 // Canonicalize illegal integer setcc's.
257 if (isInteger) {
258 switch (Result) {
259 default: break;
Chris Lattner883a52d2006-06-28 18:29:47 +0000260 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
261 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
262 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
263 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
Chris Lattnera83385f2006-04-27 05:01:07 +0000264 }
265 }
266
267 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000268}
269
Chris Lattnerb48da392005-01-23 04:39:44 +0000270const TargetMachine &SelectionDAG::getTarget() const {
271 return TLI.getTargetMachine();
272}
273
Jim Laskey58b968b2005-08-17 20:08:02 +0000274//===----------------------------------------------------------------------===//
Jim Laskey583bd472006-10-27 23:46:08 +0000275// SDNode Profile Support
276//===----------------------------------------------------------------------===//
277
Jim Laskeydef69b92006-10-27 23:52:51 +0000278/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
279///
Jim Laskey583bd472006-10-27 23:46:08 +0000280static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
281 ID.AddInteger(OpC);
282}
283
284/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
285/// solely with their pointer.
286void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
287 ID.AddPointer(VTList.VTs);
288}
289
Jim Laskeydef69b92006-10-27 23:52:51 +0000290/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
291///
Jim Laskey583bd472006-10-27 23:46:08 +0000292static void AddNodeIDOperands(FoldingSetNodeID &ID,
293 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner63e3f142007-02-04 07:28:00 +0000294 for (; NumOps; --NumOps, ++Ops) {
295 ID.AddPointer(Ops->Val);
296 ID.AddInteger(Ops->ResNo);
297 }
Jim Laskey583bd472006-10-27 23:46:08 +0000298}
299
Jim Laskey583bd472006-10-27 23:46:08 +0000300static void AddNodeIDNode(FoldingSetNodeID &ID,
301 unsigned short OpC, SDVTList VTList,
302 const SDOperand *OpList, unsigned N) {
303 AddNodeIDOpcode(ID, OpC);
304 AddNodeIDValueTypes(ID, VTList);
305 AddNodeIDOperands(ID, OpList, N);
306}
307
Jim Laskeydef69b92006-10-27 23:52:51 +0000308/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
309/// data.
Jim Laskey583bd472006-10-27 23:46:08 +0000310static void AddNodeIDNode(FoldingSetNodeID &ID, SDNode *N) {
311 AddNodeIDOpcode(ID, N->getOpcode());
312 // Add the return value info.
313 AddNodeIDValueTypes(ID, N->getVTList());
314 // Add the operand info.
315 AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
316
317 // Handle SDNode leafs with special info.
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000318 switch (N->getOpcode()) {
319 default: break; // Normal nodes don't need extra info.
320 case ISD::TargetConstant:
321 case ISD::Constant:
322 ID.AddInteger(cast<ConstantSDNode>(N)->getValue());
323 break;
324 case ISD::TargetConstantFP:
Dale Johanneseneaf08942007-08-31 04:03:46 +0000325 case ISD::ConstantFP: {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000326 ID.AddAPFloat(cast<ConstantFPSDNode>(N)->getValueAPF());
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000327 break;
Dale Johanneseneaf08942007-08-31 04:03:46 +0000328 }
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000329 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000330 case ISD::GlobalAddress:
331 case ISD::TargetGlobalTLSAddress:
332 case ISD::GlobalTLSAddress: {
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000333 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
334 ID.AddPointer(GA->getGlobal());
335 ID.AddInteger(GA->getOffset());
336 break;
337 }
338 case ISD::BasicBlock:
339 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
340 break;
341 case ISD::Register:
342 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
343 break;
344 case ISD::SRCVALUE: {
345 SrcValueSDNode *SV = cast<SrcValueSDNode>(N);
346 ID.AddPointer(SV->getValue());
347 ID.AddInteger(SV->getOffset());
348 break;
349 }
350 case ISD::FrameIndex:
351 case ISD::TargetFrameIndex:
352 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
353 break;
354 case ISD::JumpTable:
355 case ISD::TargetJumpTable:
356 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
357 break;
358 case ISD::ConstantPool:
359 case ISD::TargetConstantPool: {
360 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
361 ID.AddInteger(CP->getAlignment());
362 ID.AddInteger(CP->getOffset());
363 if (CP->isMachineConstantPoolEntry())
364 CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
365 else
366 ID.AddPointer(CP->getConstVal());
367 break;
368 }
369 case ISD::LOAD: {
370 LoadSDNode *LD = cast<LoadSDNode>(N);
371 ID.AddInteger(LD->getAddressingMode());
372 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000373 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000374 ID.AddPointer(LD->getSrcValue());
375 ID.AddInteger(LD->getSrcValueOffset());
376 ID.AddInteger(LD->getAlignment());
377 ID.AddInteger(LD->isVolatile());
378 break;
379 }
380 case ISD::STORE: {
381 StoreSDNode *ST = cast<StoreSDNode>(N);
382 ID.AddInteger(ST->getAddressingMode());
383 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000384 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Dale Johannesen2041a0e2007-03-30 21:38:07 +0000385 ID.AddPointer(ST->getSrcValue());
386 ID.AddInteger(ST->getSrcValueOffset());
387 ID.AddInteger(ST->getAlignment());
388 ID.AddInteger(ST->isVolatile());
389 break;
390 }
Jim Laskey583bd472006-10-27 23:46:08 +0000391 }
392}
393
394//===----------------------------------------------------------------------===//
Jim Laskey58b968b2005-08-17 20:08:02 +0000395// SelectionDAG Class
396//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000397
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000398/// RemoveDeadNodes - This method deletes all unreachable nodes in the
Chris Lattner190a4182006-08-04 17:45:20 +0000399/// SelectionDAG.
400void SelectionDAG::RemoveDeadNodes() {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000401 // Create a dummy node (which is not added to allnodes), that adds a reference
402 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000403 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000404
Chris Lattner190a4182006-08-04 17:45:20 +0000405 SmallVector<SDNode*, 128> DeadNodes;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000406
Chris Lattner190a4182006-08-04 17:45:20 +0000407 // Add all obviously-dead nodes to the DeadNodes worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000408 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
Chris Lattner190a4182006-08-04 17:45:20 +0000409 if (I->use_empty())
410 DeadNodes.push_back(I);
411
412 // Process the worklist, deleting the nodes and adding their uses to the
413 // worklist.
414 while (!DeadNodes.empty()) {
415 SDNode *N = DeadNodes.back();
416 DeadNodes.pop_back();
417
418 // Take the node out of the appropriate CSE map.
419 RemoveNodeFromCSEMaps(N);
420
421 // Next, brutally remove the operand list. This is safe to do, as there are
422 // no cycles in the graph.
423 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
424 SDNode *Operand = I->Val;
425 Operand->removeUser(N);
426
427 // Now that we removed this operand, see if there are no uses of it left.
428 if (Operand->use_empty())
429 DeadNodes.push_back(Operand);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000430 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000431 if (N->OperandsNeedDelete)
432 delete[] N->OperandList;
Chris Lattner190a4182006-08-04 17:45:20 +0000433 N->OperandList = 0;
434 N->NumOperands = 0;
435
436 // Finally, remove N itself.
437 AllNodes.erase(N);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000438 }
439
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000440 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000441 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000442}
443
Evan Cheng130a6472006-10-12 20:34:05 +0000444void SelectionDAG::RemoveDeadNode(SDNode *N, std::vector<SDNode*> &Deleted) {
445 SmallVector<SDNode*, 16> DeadNodes;
446 DeadNodes.push_back(N);
447
448 // Process the worklist, deleting the nodes and adding their uses to the
449 // worklist.
450 while (!DeadNodes.empty()) {
451 SDNode *N = DeadNodes.back();
452 DeadNodes.pop_back();
453
454 // Take the node out of the appropriate CSE map.
455 RemoveNodeFromCSEMaps(N);
456
457 // Next, brutally remove the operand list. This is safe to do, as there are
458 // no cycles in the graph.
459 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
460 SDNode *Operand = I->Val;
461 Operand->removeUser(N);
462
463 // Now that we removed this operand, see if there are no uses of it left.
464 if (Operand->use_empty())
465 DeadNodes.push_back(Operand);
466 }
Chris Lattner63e3f142007-02-04 07:28:00 +0000467 if (N->OperandsNeedDelete)
468 delete[] N->OperandList;
Evan Cheng130a6472006-10-12 20:34:05 +0000469 N->OperandList = 0;
470 N->NumOperands = 0;
471
472 // Finally, remove N itself.
473 Deleted.push_back(N);
474 AllNodes.erase(N);
475 }
476}
477
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000478void SelectionDAG::DeleteNode(SDNode *N) {
479 assert(N->use_empty() && "Cannot delete a node that is not dead!");
480
481 // First take this out of the appropriate CSE map.
482 RemoveNodeFromCSEMaps(N);
483
Chris Lattner1e111c72005-09-07 05:37:01 +0000484 // Finally, remove uses due to operands of this node, remove from the
485 // AllNodes list, and delete the node.
486 DeleteNodeNotInCSEMaps(N);
487}
488
489void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
490
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000491 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000492 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000493
494 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000495 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
496 I->Val->removeUser(N);
Chris Lattner63e3f142007-02-04 07:28:00 +0000497 if (N->OperandsNeedDelete)
498 delete[] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000499 N->OperandList = 0;
500 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000501
502 delete N;
503}
504
Chris Lattner149c58c2005-08-16 18:17:10 +0000505/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
506/// correspond to it. This is useful when we're about to delete or repurpose
507/// the node. We don't want future request for structurally identical nodes
508/// to return N anymore.
509void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000510 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000511 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000512 case ISD::HANDLENODE: return; // noop.
Chris Lattner36ce6912005-11-29 06:21:05 +0000513 case ISD::STRING:
514 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
515 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000516 case ISD::CONDCODE:
517 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
518 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000519 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000520 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
521 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000522 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000523 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000524 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000525 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000526 Erased =
527 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000528 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000529 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000530 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000531 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
532 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000533 default:
Chris Lattner4a283e92006-08-11 18:38:11 +0000534 // Remove it from the CSE Map.
535 Erased = CSEMap.RemoveNode(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000536 break;
537 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000538#ifndef NDEBUG
539 // Verify that the node was actually in one of the CSE maps, unless it has a
540 // flag result (which cannot be CSE'd) or is one of the special cases that are
541 // not subject to CSE.
542 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000543 !N->isTargetOpcode()) {
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000544 N->dump(this);
Bill Wendling832171c2006-12-07 20:04:42 +0000545 cerr << "\n";
Chris Lattner6621e3b2005-09-02 19:15:44 +0000546 assert(0 && "Node is not in map!");
547 }
548#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000549}
550
Chris Lattner8b8749f2005-08-17 19:00:20 +0000551/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
552/// has been taken out and modified in some way. If the specified node already
553/// exists in the CSE maps, do not modify the maps, but return the existing node
554/// instead. If it doesn't exist, add it and return null.
555///
556SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
557 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000558 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000559 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000560
Chris Lattner9f8cc692005-12-19 22:21:21 +0000561 // Check that remaining values produced are not flags.
562 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
563 if (N->getValueType(i) == MVT::Flag)
564 return 0; // Never CSE anything that produces a flag.
565
Chris Lattnera5682852006-08-07 23:03:03 +0000566 SDNode *New = CSEMap.GetOrInsertNode(N);
567 if (New != N) return New; // Node already existed.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000568 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000569}
570
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000571/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
572/// were replaced with those specified. If this node is never memoized,
573/// return null, otherwise return a pointer to the slot it would take. If a
574/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000575SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op,
576 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000577 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000578 return 0; // Never add these nodes.
579
580 // Check that remaining values produced are not flags.
581 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
582 if (N->getValueType(i) == MVT::Flag)
583 return 0; // Never CSE anything that produces a flag.
584
Chris Lattner63e3f142007-02-04 07:28:00 +0000585 SDOperand Ops[] = { Op };
Jim Laskey583bd472006-10-27 23:46:08 +0000586 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000587 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +0000588 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000589}
590
591/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
592/// were replaced with those specified. If this node is never memoized,
593/// return null, otherwise return a pointer to the slot it would take. If a
594/// node already exists with these operands, the slot will be non-null.
Chris Lattnera5682852006-08-07 23:03:03 +0000595SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
596 SDOperand Op1, SDOperand Op2,
597 void *&InsertPos) {
598 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
599 return 0; // Never add these nodes.
600
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
603 if (N->getValueType(i) == MVT::Flag)
604 return 0; // Never CSE anything that produces a flag.
605
Chris Lattner63e3f142007-02-04 07:28:00 +0000606 SDOperand Ops[] = { Op1, Op2 };
Jim Laskey583bd472006-10-27 23:46:08 +0000607 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000608 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +0000609 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
610}
611
612
613/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
614/// were replaced with those specified. If this node is never memoized,
615/// return null, otherwise return a pointer to the slot it would take. If a
616/// node already exists with these operands, the slot will be non-null.
617SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000618 const SDOperand *Ops,unsigned NumOps,
Chris Lattnera5682852006-08-07 23:03:03 +0000619 void *&InsertPos) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000620 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000621 return 0; // Never add these nodes.
622
623 // Check that remaining values produced are not flags.
624 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
625 if (N->getValueType(i) == MVT::Flag)
626 return 0; // Never CSE anything that produces a flag.
627
Jim Laskey583bd472006-10-27 23:46:08 +0000628 FoldingSetNodeID ID;
Dan Gohman575e2f42007-06-04 15:49:41 +0000629 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
Jim Laskey583bd472006-10-27 23:46:08 +0000630
Evan Cheng9629aba2006-10-11 01:47:58 +0000631 if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
632 ID.AddInteger(LD->getAddressingMode());
633 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000634 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng9629aba2006-10-11 01:47:58 +0000635 ID.AddPointer(LD->getSrcValue());
636 ID.AddInteger(LD->getSrcValueOffset());
637 ID.AddInteger(LD->getAlignment());
638 ID.AddInteger(LD->isVolatile());
Evan Cheng8b2794a2006-10-13 21:14:26 +0000639 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
640 ID.AddInteger(ST->getAddressingMode());
641 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +0000642 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng8b2794a2006-10-13 21:14:26 +0000643 ID.AddPointer(ST->getSrcValue());
644 ID.AddInteger(ST->getSrcValueOffset());
645 ID.AddInteger(ST->getAlignment());
646 ID.AddInteger(ST->isVolatile());
Evan Cheng9629aba2006-10-11 01:47:58 +0000647 }
Jim Laskey583bd472006-10-27 23:46:08 +0000648
Chris Lattnera5682852006-08-07 23:03:03 +0000649 return CSEMap.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000650}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000651
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000652
Chris Lattner78ec3112003-08-11 14:57:33 +0000653SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000654 while (!AllNodes.empty()) {
655 SDNode *N = AllNodes.begin();
Chris Lattner213a16c2006-08-14 22:19:25 +0000656 N->SetNextInBucket(0);
Chris Lattner63e3f142007-02-04 07:28:00 +0000657 if (N->OperandsNeedDelete)
658 delete [] N->OperandList;
Chris Lattner65113b22005-11-08 22:07:03 +0000659 N->OperandList = 0;
660 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000661 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000662 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000663}
664
Chris Lattner0f2287b2005-04-13 02:38:18 +0000665SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000666 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000667 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000668 return getNode(ISD::AND, Op.getValueType(), Op,
669 getConstant(Imm, Op.getValueType()));
670}
671
Chris Lattner36ce6912005-11-29 06:21:05 +0000672SDOperand SelectionDAG::getString(const std::string &Val) {
673 StringSDNode *&N = StringNodes[Val];
674 if (!N) {
675 N = new StringSDNode(Val);
676 AllNodes.push_back(N);
677 }
678 return SDOperand(N, 0);
679}
680
Chris Lattner61b09412006-08-11 21:01:22 +0000681SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT, bool isT) {
Chris Lattner37bfbb42005-08-17 00:34:06 +0000682 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattner61b09412006-08-11 21:01:22 +0000683 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
Chris Lattner37bfbb42005-08-17 00:34:06 +0000684
Chris Lattner61b09412006-08-11 21:01:22 +0000685 // Mask out any bits that are not valid for this constant.
686 Val &= MVT::getIntVTBitMask(VT);
687
688 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
Jim Laskey583bd472006-10-27 23:46:08 +0000689 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000690 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000691 ID.AddInteger(Val);
692 void *IP = 0;
693 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
694 return SDOperand(E, 0);
695 SDNode *N = new ConstantSDNode(isT, Val, VT);
696 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000699}
700
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000701SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000702 bool isTarget) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000703 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000704
Dan Gohman7f321562007-06-25 16:23:39 +0000705 MVT::ValueType EltVT =
706 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000707
Chris Lattnerd8658612005-02-17 20:17:32 +0000708 // Do the map lookup using the actual bit pattern for the floating point
709 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
710 // we don't have issues with SNANs.
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000711 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
Jim Laskey583bd472006-10-27 23:46:08 +0000712 FoldingSetNodeID ID;
Dan Gohman7f321562007-06-25 16:23:39 +0000713 AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000714 ID.AddAPFloat(V);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000715 void *IP = 0;
Evan Chengc908dcd2007-06-29 21:36:04 +0000716 SDNode *N = NULL;
717 if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
718 if (!MVT::isVector(VT))
719 return SDOperand(N, 0);
720 if (!N) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000721 N = new ConstantFPSDNode(isTarget, V, EltVT);
Evan Chengc908dcd2007-06-29 21:36:04 +0000722 CSEMap.InsertNode(N, IP);
723 AllNodes.push_back(N);
724 }
725
Dan Gohman7f321562007-06-25 16:23:39 +0000726 SDOperand Result(N, 0);
727 if (MVT::isVector(VT)) {
728 SmallVector<SDOperand, 8> Ops;
729 Ops.assign(MVT::getVectorNumElements(VT), Result);
730 Result = getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
731 }
732 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733}
734
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000735SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
736 bool isTarget) {
737 MVT::ValueType EltVT =
738 MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
739 if (EltVT==MVT::f32)
740 return getConstantFP(APFloat((float)Val), VT, isTarget);
741 else
742 return getConstantFP(APFloat(Val), VT, isTarget);
743}
744
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Chris Lattner61b09412006-08-11 21:01:22 +0000746 MVT::ValueType VT, int Offset,
747 bool isTargetGA) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000748 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
749 unsigned Opc;
750 if (GVar && GVar->isThreadLocal())
751 Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
752 else
753 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
Jim Laskey583bd472006-10-27 23:46:08 +0000754 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000755 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000756 ID.AddPointer(GV);
757 ID.AddInteger(Offset);
758 void *IP = 0;
759 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
760 return SDOperand(E, 0);
761 SDNode *N = new GlobalAddressSDNode(isTargetGA, GV, VT, Offset);
762 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763 AllNodes.push_back(N);
764 return SDOperand(N, 0);
765}
766
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000767SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT,
768 bool isTarget) {
769 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
Jim Laskey583bd472006-10-27 23:46:08 +0000770 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000771 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000772 ID.AddInteger(FI);
773 void *IP = 0;
774 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
775 return SDOperand(E, 0);
776 SDNode *N = new FrameIndexSDNode(FI, VT, isTarget);
777 CSEMap.InsertNode(N, IP);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000778 AllNodes.push_back(N);
779 return SDOperand(N, 0);
780}
781
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000782SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT, bool isTarget){
783 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
Jim Laskey583bd472006-10-27 23:46:08 +0000784 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000785 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000786 ID.AddInteger(JTI);
787 void *IP = 0;
788 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
789 return SDOperand(E, 0);
790 SDNode *N = new JumpTableSDNode(JTI, VT, isTarget);
791 CSEMap.InsertNode(N, IP);
Nate Begeman37efe672006-04-22 18:53:45 +0000792 AllNodes.push_back(N);
793 return SDOperand(N, 0);
794}
795
Evan Chengb8973bd2006-01-31 22:23:14 +0000796SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000797 unsigned Alignment, int Offset,
798 bool isTarget) {
799 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000800 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000801 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000802 ID.AddInteger(Alignment);
803 ID.AddInteger(Offset);
Chris Lattner130fc132006-08-14 20:12:44 +0000804 ID.AddPointer(C);
Chris Lattnerc9f8f412006-08-11 21:55:30 +0000805 void *IP = 0;
806 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
807 return SDOperand(E, 0);
808 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
809 CSEMap.InsertNode(N, IP);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000810 AllNodes.push_back(N);
811 return SDOperand(N, 0);
812}
813
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814
Evan Chengd6594ae2006-09-12 21:00:35 +0000815SDOperand SelectionDAG::getConstantPool(MachineConstantPoolValue *C,
816 MVT::ValueType VT,
817 unsigned Alignment, int Offset,
818 bool isTarget) {
819 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
Jim Laskey583bd472006-10-27 23:46:08 +0000820 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000821 AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
Evan Chengd6594ae2006-09-12 21:00:35 +0000822 ID.AddInteger(Alignment);
823 ID.AddInteger(Offset);
Jim Laskey583bd472006-10-27 23:46:08 +0000824 C->AddSelectionDAGCSEId(ID);
Evan Chengd6594ae2006-09-12 21:00:35 +0000825 void *IP = 0;
826 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
827 return SDOperand(E, 0);
828 SDNode *N = new ConstantPoolSDNode(isTarget, C, VT, Offset, Alignment);
829 CSEMap.InsertNode(N, IP);
830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
832}
833
834
Chris Lattnerc3aae252005-01-07 07:46:32 +0000835SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
Jim Laskey583bd472006-10-27 23:46:08 +0000836 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000837 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000838 ID.AddPointer(MBB);
839 void *IP = 0;
840 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
841 return SDOperand(E, 0);
842 SDNode *N = new BasicBlockSDNode(MBB);
843 CSEMap.InsertNode(N, IP);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000844 AllNodes.push_back(N);
845 return SDOperand(N, 0);
846}
847
Chris Lattner15e4b012005-07-10 00:07:11 +0000848SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
849 if ((unsigned)VT >= ValueTypeNodes.size())
850 ValueTypeNodes.resize(VT+1);
851 if (ValueTypeNodes[VT] == 0) {
852 ValueTypeNodes[VT] = new VTSDNode(VT);
853 AllNodes.push_back(ValueTypeNodes[VT]);
854 }
855
856 return SDOperand(ValueTypeNodes[VT], 0);
857}
858
Chris Lattnerc3aae252005-01-07 07:46:32 +0000859SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
860 SDNode *&N = ExternalSymbols[Sym];
861 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000862 N = new ExternalSymbolSDNode(false, Sym, VT);
863 AllNodes.push_back(N);
864 return SDOperand(N, 0);
865}
866
Chris Lattner809ec112006-01-28 10:09:25 +0000867SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
868 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000869 SDNode *&N = TargetExternalSymbols[Sym];
870 if (N) return SDOperand(N, 0);
871 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000872 AllNodes.push_back(N);
873 return SDOperand(N, 0);
874}
875
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000876SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
877 if ((unsigned)Cond >= CondCodeNodes.size())
878 CondCodeNodes.resize(Cond+1);
879
Chris Lattner079a27a2005-08-09 20:40:02 +0000880 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000881 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000882 AllNodes.push_back(CondCodeNodes[Cond]);
883 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000884 return SDOperand(CondCodeNodes[Cond], 0);
885}
886
Chris Lattner0fdd7682005-08-30 22:38:38 +0000887SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +0000888 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000889 AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000890 ID.AddInteger(RegNo);
891 void *IP = 0;
892 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
893 return SDOperand(E, 0);
894 SDNode *N = new RegisterSDNode(RegNo, VT);
895 CSEMap.InsertNode(N, IP);
896 AllNodes.push_back(N);
897 return SDOperand(N, 0);
898}
899
900SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
901 assert((!V || isa<PointerType>(V->getType())) &&
902 "SrcValue is not a pointer?");
903
Jim Laskey583bd472006-10-27 23:46:08 +0000904 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +0000905 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
Chris Lattner61b09412006-08-11 21:01:22 +0000906 ID.AddPointer(V);
907 ID.AddInteger(Offset);
908 void *IP = 0;
909 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
910 return SDOperand(E, 0);
911 SDNode *N = new SrcValueSDNode(V, Offset);
912 CSEMap.InsertNode(N, IP);
913 AllNodes.push_back(N);
914 return SDOperand(N, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000915}
916
Chris Lattner37ce9df2007-10-15 17:47:20 +0000917/// CreateStackTemporary - Create a stack temporary, suitable for holding the
918/// specified value type.
919SDOperand SelectionDAG::CreateStackTemporary(MVT::ValueType VT) {
920 MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
921 unsigned ByteSize = MVT::getSizeInBits(VT)/8;
922 const Type *Ty = MVT::getTypeForValueType(VT);
923 unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty);
924 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign);
925 return getFrameIndex(FrameIdx, TLI.getPointerTy());
926}
927
928
Chris Lattner51dabfb2006-10-14 00:41:01 +0000929SDOperand SelectionDAG::FoldSetCC(MVT::ValueType VT, SDOperand N1,
930 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000931 // These setcc operations always fold.
932 switch (Cond) {
933 default: break;
934 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000935 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000937 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000938
939 case ISD::SETOEQ:
940 case ISD::SETOGT:
941 case ISD::SETOGE:
942 case ISD::SETOLT:
943 case ISD::SETOLE:
944 case ISD::SETONE:
945 case ISD::SETO:
946 case ISD::SETUO:
947 case ISD::SETUEQ:
948 case ISD::SETUNE:
949 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
950 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000951 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000952
Chris Lattner67255a12005-04-07 18:14:58 +0000953 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
954 uint64_t C2 = N2C->getValue();
955 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
956 uint64_t C1 = N1C->getValue();
Chris Lattner51dabfb2006-10-14 00:41:01 +0000957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 // Sign extend the operands if required
959 if (ISD::isSignedIntSetCC(Cond)) {
960 C1 = N1C->getSignExtended();
961 C2 = N2C->getSignExtended();
962 }
Chris Lattner51dabfb2006-10-14 00:41:01 +0000963
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964 switch (Cond) {
965 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000966 case ISD::SETEQ: return getConstant(C1 == C2, VT);
967 case ISD::SETNE: return getConstant(C1 != C2, VT);
968 case ISD::SETULT: return getConstant(C1 < C2, VT);
969 case ISD::SETUGT: return getConstant(C1 > C2, VT);
970 case ISD::SETULE: return getConstant(C1 <= C2, VT);
971 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
972 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
973 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
974 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
975 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000977 }
Chris Lattner67255a12005-04-07 18:14:58 +0000978 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
980 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
Dale Johannesen5927d8e2007-10-14 01:56:47 +0000981 // No compile time operations on this type yet.
982 if (N1C->getValueType(0) == MVT::ppcf128)
983 return SDOperand();
Dale Johanneseneaf08942007-08-31 04:03:46 +0000984
985 APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 switch (Cond) {
Dale Johanneseneaf08942007-08-31 04:03:46 +0000987 default: break;
Dale Johannesenee847682007-08-31 17:03:33 +0000988 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
989 return getNode(ISD::UNDEF, VT);
990 // fall through
991 case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
992 case ISD::SETNE: if (R==APFloat::cmpUnordered)
993 return getNode(ISD::UNDEF, VT);
994 // fall through
995 case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +0000996 R==APFloat::cmpLessThan, VT);
Dale Johannesenee847682007-08-31 17:03:33 +0000997 case ISD::SETLT: if (R==APFloat::cmpUnordered)
998 return getNode(ISD::UNDEF, VT);
999 // fall through
1000 case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
1001 case ISD::SETGT: if (R==APFloat::cmpUnordered)
1002 return getNode(ISD::UNDEF, VT);
1003 // fall through
1004 case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
1005 case ISD::SETLE: if (R==APFloat::cmpUnordered)
1006 return getNode(ISD::UNDEF, VT);
1007 // fall through
1008 case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001009 R==APFloat::cmpEqual, VT);
Dale Johannesenee847682007-08-31 17:03:33 +00001010 case ISD::SETGE: if (R==APFloat::cmpUnordered)
1011 return getNode(ISD::UNDEF, VT);
1012 // fall through
1013 case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
Dale Johanneseneaf08942007-08-31 04:03:46 +00001014 R==APFloat::cmpEqual, VT);
1015 case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
1016 case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
1017 case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
1018 R==APFloat::cmpEqual, VT);
1019 case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
1020 case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
1021 R==APFloat::cmpLessThan, VT);
1022 case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
1023 R==APFloat::cmpUnordered, VT);
1024 case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
1025 case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 }
1027 } else {
1028 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001029 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 }
Chris Lattner51dabfb2006-10-14 00:41:01 +00001031
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001032 // Could not fold it.
1033 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034}
1035
Dan Gohmanea859be2007-06-22 14:59:07 +00001036/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1037/// this predicate to simplify operations downstream. Mask is known to be zero
1038/// for bits that V cannot have.
1039bool SelectionDAG::MaskedValueIsZero(SDOperand Op, uint64_t Mask,
1040 unsigned Depth) const {
1041 // The masks are not wide enough to represent this type! Should use APInt.
1042 if (Op.getValueType() == MVT::i128)
1043 return false;
1044
1045 uint64_t KnownZero, KnownOne;
1046 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1047 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1048 return (KnownZero & Mask) == Mask;
1049}
1050
1051/// ComputeMaskedBits - Determine which of the bits specified in Mask are
1052/// known to be either zero or one and return them in the KnownZero/KnownOne
1053/// bitsets. This code only analyzes bits in Mask, in order to short-circuit
1054/// processing.
1055void SelectionDAG::ComputeMaskedBits(SDOperand Op, uint64_t Mask,
1056 uint64_t &KnownZero, uint64_t &KnownOne,
1057 unsigned Depth) const {
1058 KnownZero = KnownOne = 0; // Don't know anything.
1059 if (Depth == 6 || Mask == 0)
1060 return; // Limit search depth.
1061
1062 // The masks are not wide enough to represent this type! Should use APInt.
1063 if (Op.getValueType() == MVT::i128)
1064 return;
1065
1066 uint64_t KnownZero2, KnownOne2;
1067
1068 switch (Op.getOpcode()) {
1069 case ISD::Constant:
1070 // We know all of the bits for a constant!
1071 KnownOne = cast<ConstantSDNode>(Op)->getValue() & Mask;
1072 KnownZero = ~KnownOne & Mask;
1073 return;
1074 case ISD::AND:
1075 // If either the LHS or the RHS are Zero, the result is zero.
1076 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1077 Mask &= ~KnownZero;
1078 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1079 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1080 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1081
1082 // Output known-1 bits are only known if set in both the LHS & RHS.
1083 KnownOne &= KnownOne2;
1084 // Output known-0 are known to be clear if zero in either the LHS | RHS.
1085 KnownZero |= KnownZero2;
1086 return;
1087 case ISD::OR:
1088 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1089 Mask &= ~KnownOne;
1090 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1091 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1092 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1093
1094 // Output known-0 bits are only known if clear in both the LHS & RHS.
1095 KnownZero &= KnownZero2;
1096 // Output known-1 are known to be set if set in either the LHS | RHS.
1097 KnownOne |= KnownOne2;
1098 return;
1099 case ISD::XOR: {
1100 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1101 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1102 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1103 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1104
1105 // Output known-0 bits are known if clear or set in both the LHS & RHS.
1106 uint64_t KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
1107 // Output known-1 are known to be set if set in only one of the LHS, RHS.
1108 KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
1109 KnownZero = KnownZeroOut;
1110 return;
1111 }
1112 case ISD::SELECT:
1113 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
1114 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
1115 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1116 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1117
1118 // Only known if known in both the LHS and RHS.
1119 KnownOne &= KnownOne2;
1120 KnownZero &= KnownZero2;
1121 return;
1122 case ISD::SELECT_CC:
1123 ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
1124 ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
1125 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1126 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1127
1128 // Only known if known in both the LHS and RHS.
1129 KnownOne &= KnownOne2;
1130 KnownZero &= KnownZero2;
1131 return;
1132 case ISD::SETCC:
1133 // If we know the result of a setcc has the top bits zero, use this info.
1134 if (TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
1135 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
1136 return;
1137 case ISD::SHL:
1138 // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1139 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1140 ComputeMaskedBits(Op.getOperand(0), Mask >> SA->getValue(),
1141 KnownZero, KnownOne, Depth+1);
1142 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1143 KnownZero <<= SA->getValue();
1144 KnownOne <<= SA->getValue();
1145 KnownZero |= (1ULL << SA->getValue())-1; // low bits known zero.
1146 }
1147 return;
1148 case ISD::SRL:
1149 // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
1150 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1151 MVT::ValueType VT = Op.getValueType();
1152 unsigned ShAmt = SA->getValue();
1153
1154 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1155 ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt) & TypeMask,
1156 KnownZero, KnownOne, Depth+1);
1157 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1158 KnownZero &= TypeMask;
1159 KnownOne &= TypeMask;
1160 KnownZero >>= ShAmt;
1161 KnownOne >>= ShAmt;
1162
1163 uint64_t HighBits = (1ULL << ShAmt)-1;
1164 HighBits <<= MVT::getSizeInBits(VT)-ShAmt;
1165 KnownZero |= HighBits; // High bits known zero.
1166 }
1167 return;
1168 case ISD::SRA:
1169 if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1170 MVT::ValueType VT = Op.getValueType();
1171 unsigned ShAmt = SA->getValue();
1172
1173 // Compute the new bits that are at the top now.
1174 uint64_t TypeMask = MVT::getIntVTBitMask(VT);
1175
1176 uint64_t InDemandedMask = (Mask << ShAmt) & TypeMask;
1177 // If any of the demanded bits are produced by the sign extension, we also
1178 // demand the input sign bit.
1179 uint64_t HighBits = (1ULL << ShAmt)-1;
1180 HighBits <<= MVT::getSizeInBits(VT) - ShAmt;
1181 if (HighBits & Mask)
1182 InDemandedMask |= MVT::getIntVTSignBit(VT);
1183
1184 ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
1185 Depth+1);
1186 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1187 KnownZero &= TypeMask;
1188 KnownOne &= TypeMask;
1189 KnownZero >>= ShAmt;
1190 KnownOne >>= ShAmt;
1191
1192 // Handle the sign bits.
1193 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1194 SignBit >>= ShAmt; // Adjust to where it is now in the mask.
1195
1196 if (KnownZero & SignBit) {
1197 KnownZero |= HighBits; // New bits are known zero.
1198 } else if (KnownOne & SignBit) {
1199 KnownOne |= HighBits; // New bits are known one.
1200 }
1201 }
1202 return;
1203 case ISD::SIGN_EXTEND_INREG: {
1204 MVT::ValueType EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1205
1206 // Sign extension. Compute the demanded bits in the result that are not
1207 // present in the input.
1208 uint64_t NewBits = ~MVT::getIntVTBitMask(EVT) & Mask;
1209
1210 uint64_t InSignBit = MVT::getIntVTSignBit(EVT);
1211 int64_t InputDemandedBits = Mask & MVT::getIntVTBitMask(EVT);
1212
1213 // If the sign extended bits are demanded, we know that the sign
1214 // bit is demanded.
1215 if (NewBits)
1216 InputDemandedBits |= InSignBit;
1217
1218 ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
1219 KnownZero, KnownOne, Depth+1);
1220 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1221
1222 // If the sign bit of the input is known set or clear, then we know the
1223 // top bits of the result.
1224 if (KnownZero & InSignBit) { // Input sign bit known clear
1225 KnownZero |= NewBits;
1226 KnownOne &= ~NewBits;
1227 } else if (KnownOne & InSignBit) { // Input sign bit known set
1228 KnownOne |= NewBits;
1229 KnownZero &= ~NewBits;
1230 } else { // Input sign bit unknown
1231 KnownZero &= ~NewBits;
1232 KnownOne &= ~NewBits;
1233 }
1234 return;
1235 }
1236 case ISD::CTTZ:
1237 case ISD::CTLZ:
1238 case ISD::CTPOP: {
1239 MVT::ValueType VT = Op.getValueType();
1240 unsigned LowBits = Log2_32(MVT::getSizeInBits(VT))+1;
1241 KnownZero = ~((1ULL << LowBits)-1) & MVT::getIntVTBitMask(VT);
1242 KnownOne = 0;
1243 return;
1244 }
1245 case ISD::LOAD: {
1246 if (ISD::isZEXTLoad(Op.Val)) {
1247 LoadSDNode *LD = cast<LoadSDNode>(Op);
1248 MVT::ValueType VT = LD->getLoadedVT();
1249 KnownZero |= ~MVT::getIntVTBitMask(VT) & Mask;
1250 }
1251 return;
1252 }
1253 case ISD::ZERO_EXTEND: {
1254 uint64_t InMask = MVT::getIntVTBitMask(Op.getOperand(0).getValueType());
1255 uint64_t NewBits = (~InMask) & Mask;
1256 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1257 KnownOne, Depth+1);
1258 KnownZero |= NewBits & Mask;
1259 KnownOne &= ~NewBits;
1260 return;
1261 }
1262 case ISD::SIGN_EXTEND: {
1263 MVT::ValueType InVT = Op.getOperand(0).getValueType();
1264 unsigned InBits = MVT::getSizeInBits(InVT);
1265 uint64_t InMask = MVT::getIntVTBitMask(InVT);
1266 uint64_t InSignBit = 1ULL << (InBits-1);
1267 uint64_t NewBits = (~InMask) & Mask;
1268 uint64_t InDemandedBits = Mask & InMask;
1269
1270 // If any of the sign extended bits are demanded, we know that the sign
1271 // bit is demanded.
1272 if (NewBits & Mask)
1273 InDemandedBits |= InSignBit;
1274
1275 ComputeMaskedBits(Op.getOperand(0), InDemandedBits, KnownZero,
1276 KnownOne, Depth+1);
1277 // If the sign bit is known zero or one, the top bits match.
1278 if (KnownZero & InSignBit) {
1279 KnownZero |= NewBits;
1280 KnownOne &= ~NewBits;
1281 } else if (KnownOne & InSignBit) {
1282 KnownOne |= NewBits;
1283 KnownZero &= ~NewBits;
1284 } else { // Otherwise, top bits aren't known.
1285 KnownOne &= ~NewBits;
1286 KnownZero &= ~NewBits;
1287 }
1288 return;
1289 }
1290 case ISD::ANY_EXTEND: {
1291 MVT::ValueType VT = Op.getOperand(0).getValueType();
1292 ComputeMaskedBits(Op.getOperand(0), Mask & MVT::getIntVTBitMask(VT),
1293 KnownZero, KnownOne, Depth+1);
1294 return;
1295 }
1296 case ISD::TRUNCATE: {
1297 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1298 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1299 uint64_t OutMask = MVT::getIntVTBitMask(Op.getValueType());
1300 KnownZero &= OutMask;
1301 KnownOne &= OutMask;
1302 break;
1303 }
1304 case ISD::AssertZext: {
1305 MVT::ValueType VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1306 uint64_t InMask = MVT::getIntVTBitMask(VT);
1307 ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
1308 KnownOne, Depth+1);
1309 KnownZero |= (~InMask) & Mask;
1310 return;
1311 }
1312 case ISD::ADD: {
1313 // If either the LHS or the RHS are Zero, the result is zero.
1314 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1315 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
1316 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1317 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1318
1319 // Output known-0 bits are known if clear or set in both the low clear bits
1320 // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
1321 // low 3 bits clear.
1322 uint64_t KnownZeroOut = std::min(CountTrailingZeros_64(~KnownZero),
1323 CountTrailingZeros_64(~KnownZero2));
1324
1325 KnownZero = (1ULL << KnownZeroOut) - 1;
1326 KnownOne = 0;
1327 return;
1328 }
1329 case ISD::SUB: {
1330 ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0));
1331 if (!CLHS) return;
1332
1333 // We know that the top bits of C-X are clear if X contains less bits
1334 // than C (i.e. no wrap-around can happen). For example, 20-X is
1335 // positive if we can prove that X is >= 0 and < 16.
1336 MVT::ValueType VT = CLHS->getValueType(0);
1337 if ((CLHS->getValue() & MVT::getIntVTSignBit(VT)) == 0) { // sign bit clear
1338 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
1339 uint64_t MaskV = (1ULL << (63-NLZ))-1; // NLZ can't be 64 with no sign bit
1340 MaskV = ~MaskV & MVT::getIntVTBitMask(VT);
1341 ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero, KnownOne, Depth+1);
1342
1343 // If all of the MaskV bits are known to be zero, then we know the output
1344 // top bits are zero, because we now know that the output is from [0-C].
1345 if ((KnownZero & MaskV) == MaskV) {
1346 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
1347 KnownZero = ~((1ULL << (64-NLZ2))-1) & Mask; // Top bits known zero.
1348 KnownOne = 0; // No one bits known.
1349 } else {
1350 KnownZero = KnownOne = 0; // Otherwise, nothing known.
1351 }
1352 }
1353 return;
1354 }
1355 default:
1356 // Allow the target to implement this method for its nodes.
1357 if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
1358 case ISD::INTRINSIC_WO_CHAIN:
1359 case ISD::INTRINSIC_W_CHAIN:
1360 case ISD::INTRINSIC_VOID:
1361 TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this);
1362 }
1363 return;
1364 }
1365}
1366
1367/// ComputeNumSignBits - Return the number of times the sign bit of the
1368/// register is replicated into the other bits. We know that at least 1 bit
1369/// is always equal to the sign bit (itself), but other cases can give us
1370/// information. For example, immediately after an "SRA X, 2", we know that
1371/// the top 3 bits are all equal to each other, so we return 3.
1372unsigned SelectionDAG::ComputeNumSignBits(SDOperand Op, unsigned Depth) const{
1373 MVT::ValueType VT = Op.getValueType();
1374 assert(MVT::isInteger(VT) && "Invalid VT!");
1375 unsigned VTBits = MVT::getSizeInBits(VT);
1376 unsigned Tmp, Tmp2;
1377
1378 if (Depth == 6)
1379 return 1; // Limit search depth.
1380
1381 switch (Op.getOpcode()) {
1382 default: break;
1383 case ISD::AssertSext:
1384 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1385 return VTBits-Tmp+1;
1386 case ISD::AssertZext:
1387 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1388 return VTBits-Tmp;
1389
1390 case ISD::Constant: {
1391 uint64_t Val = cast<ConstantSDNode>(Op)->getValue();
1392 // If negative, invert the bits, then look at it.
1393 if (Val & MVT::getIntVTSignBit(VT))
1394 Val = ~Val;
1395
1396 // Shift the bits so they are the leading bits in the int64_t.
1397 Val <<= 64-VTBits;
1398
1399 // Return # leading zeros. We use 'min' here in case Val was zero before
1400 // shifting. We don't want to return '64' as for an i32 "0".
1401 return std::min(VTBits, CountLeadingZeros_64(Val));
1402 }
1403
1404 case ISD::SIGN_EXTEND:
1405 Tmp = VTBits-MVT::getSizeInBits(Op.getOperand(0).getValueType());
1406 return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
1407
1408 case ISD::SIGN_EXTEND_INREG:
1409 // Max of the input and what this extends.
1410 Tmp = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
1411 Tmp = VTBits-Tmp+1;
1412
1413 Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1414 return std::max(Tmp, Tmp2);
1415
1416 case ISD::SRA:
1417 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1418 // SRA X, C -> adds C sign bits.
1419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1420 Tmp += C->getValue();
1421 if (Tmp > VTBits) Tmp = VTBits;
1422 }
1423 return Tmp;
1424 case ISD::SHL:
1425 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1426 // shl destroys sign bits.
1427 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1428 if (C->getValue() >= VTBits || // Bad shift.
1429 C->getValue() >= Tmp) break; // Shifted all sign bits out.
1430 return Tmp - C->getValue();
1431 }
1432 break;
1433 case ISD::AND:
1434 case ISD::OR:
1435 case ISD::XOR: // NOT is handled here.
1436 // Logical binary ops preserve the number of sign bits.
1437 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1438 if (Tmp == 1) return 1; // Early out.
1439 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1440 return std::min(Tmp, Tmp2);
1441
1442 case ISD::SELECT:
1443 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1444 if (Tmp == 1) return 1; // Early out.
1445 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1446 return std::min(Tmp, Tmp2);
1447
1448 case ISD::SETCC:
1449 // If setcc returns 0/-1, all bits are sign bits.
1450 if (TLI.getSetCCResultContents() ==
1451 TargetLowering::ZeroOrNegativeOneSetCCResult)
1452 return VTBits;
1453 break;
1454 case ISD::ROTL:
1455 case ISD::ROTR:
1456 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1457 unsigned RotAmt = C->getValue() & (VTBits-1);
1458
1459 // Handle rotate right by N like a rotate left by 32-N.
1460 if (Op.getOpcode() == ISD::ROTR)
1461 RotAmt = (VTBits-RotAmt) & (VTBits-1);
1462
1463 // If we aren't rotating out all of the known-in sign bits, return the
1464 // number that are left. This handles rotl(sext(x), 1) for example.
1465 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1466 if (Tmp > RotAmt+1) return Tmp-RotAmt;
1467 }
1468 break;
1469 case ISD::ADD:
1470 // Add can have at most one carry bit. Thus we know that the output
1471 // is, at worst, one more bit than the inputs.
1472 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1473 if (Tmp == 1) return 1; // Early out.
1474
1475 // Special case decrementing a value (ADD X, -1):
1476 if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1477 if (CRHS->isAllOnesValue()) {
1478 uint64_t KnownZero, KnownOne;
1479 uint64_t Mask = MVT::getIntVTBitMask(VT);
1480 ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
1481
1482 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1483 // sign bits set.
1484 if ((KnownZero|1) == Mask)
1485 return VTBits;
1486
1487 // If we are subtracting one from a positive number, there is no carry
1488 // out of the result.
1489 if (KnownZero & MVT::getIntVTSignBit(VT))
1490 return Tmp;
1491 }
1492
1493 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1494 if (Tmp2 == 1) return 1;
1495 return std::min(Tmp, Tmp2)-1;
1496 break;
1497
1498 case ISD::SUB:
1499 Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
1500 if (Tmp2 == 1) return 1;
1501
1502 // Handle NEG.
1503 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
1504 if (CLHS->getValue() == 0) {
1505 uint64_t KnownZero, KnownOne;
1506 uint64_t Mask = MVT::getIntVTBitMask(VT);
1507 ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
1508 // If the input is known to be 0 or 1, the output is 0/-1, which is all
1509 // sign bits set.
1510 if ((KnownZero|1) == Mask)
1511 return VTBits;
1512
1513 // If the input is known to be positive (the sign bit is known clear),
1514 // the output of the NEG has the same number of sign bits as the input.
1515 if (KnownZero & MVT::getIntVTSignBit(VT))
1516 return Tmp2;
1517
1518 // Otherwise, we treat this like a SUB.
1519 }
1520
1521 // Sub can have at most one carry bit. Thus we know that the output
1522 // is, at worst, one more bit than the inputs.
1523 Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
1524 if (Tmp == 1) return 1; // Early out.
1525 return std::min(Tmp, Tmp2)-1;
1526 break;
1527 case ISD::TRUNCATE:
1528 // FIXME: it's tricky to do anything useful for this, but it is an important
1529 // case for targets like X86.
1530 break;
1531 }
1532
1533 // Handle LOADX separately here. EXTLOAD case will fallthrough.
1534 if (Op.getOpcode() == ISD::LOAD) {
1535 LoadSDNode *LD = cast<LoadSDNode>(Op);
1536 unsigned ExtType = LD->getExtensionType();
1537 switch (ExtType) {
1538 default: break;
1539 case ISD::SEXTLOAD: // '17' bits known
1540 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1541 return VTBits-Tmp+1;
1542 case ISD::ZEXTLOAD: // '16' bits known
1543 Tmp = MVT::getSizeInBits(LD->getLoadedVT());
1544 return VTBits-Tmp;
1545 }
1546 }
1547
1548 // Allow the target to implement this method for its nodes.
1549 if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
1550 Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
1551 Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
1552 Op.getOpcode() == ISD::INTRINSIC_VOID) {
1553 unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
1554 if (NumBits > 1) return NumBits;
1555 }
1556
1557 // Finally, if we can prove that the top bits of the result are 0's or 1's,
1558 // use this information.
1559 uint64_t KnownZero, KnownOne;
1560 uint64_t Mask = MVT::getIntVTBitMask(VT);
1561 ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
1562
1563 uint64_t SignBit = MVT::getIntVTSignBit(VT);
1564 if (KnownZero & SignBit) { // SignBit is 0
1565 Mask = KnownZero;
1566 } else if (KnownOne & SignBit) { // SignBit is 1;
1567 Mask = KnownOne;
1568 } else {
1569 // Nothing known.
1570 return 1;
1571 }
1572
1573 // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
1574 // the number of identical bits in the top of the input value.
1575 Mask ^= ~0ULL;
1576 Mask <<= 64-VTBits;
1577 // Return # leading zeros. We use 'min' here in case Val was zero before
1578 // shifting. We don't want to return '64' as for an i32 "0".
1579 return std::min(VTBits, CountLeadingZeros_64(Mask));
1580}
1581
Chris Lattner51dabfb2006-10-14 00:41:01 +00001582
Chris Lattnerc3aae252005-01-07 07:46:32 +00001583/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001584///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001585SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Jim Laskey583bd472006-10-27 23:46:08 +00001586 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00001587 AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00001588 void *IP = 0;
1589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1590 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001591 SDNode *N = new SDNode(Opcode, SDNode::getSDVTList(VT));
Chris Lattner4a283e92006-08-11 18:38:11 +00001592 CSEMap.InsertNode(N, IP);
1593
1594 AllNodes.push_back(N);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001595 return SDOperand(N, 0);
1596}
1597
Chris Lattnerc3aae252005-01-07 07:46:32 +00001598SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1599 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001600 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001601 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001602 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1603 uint64_t Val = C->getValue();
1604 switch (Opcode) {
1605 default: break;
1606 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001607 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001608 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1609 case ISD::TRUNCATE: return getConstant(Val, VT);
Dale Johannesen73328d12007-09-19 23:55:34 +00001610 case ISD::UINT_TO_FP:
1611 case ISD::SINT_TO_FP: {
1612 const uint64_t zero[] = {0, 0};
1613 APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
Neil Boothccf596a2007-10-07 11:45:55 +00001614 (void)apf.convertFromZeroExtendedInteger(&Val,
Dale Johannesen910993e2007-09-21 22:09:37 +00001615 MVT::getSizeInBits(Operand.getValueType()),
1616 Opcode==ISD::SINT_TO_FP,
Dale Johannesen88216af2007-09-30 18:19:03 +00001617 APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00001618 return getConstantFP(apf, VT);
1619 }
Chris Lattner94683772005-12-23 05:30:37 +00001620 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001621 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001622 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001623 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001624 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001625 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001626 case ISD::BSWAP:
1627 switch(VT) {
1628 default: assert(0 && "Invalid bswap!"); break;
1629 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1630 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1631 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1632 }
1633 break;
1634 case ISD::CTPOP:
1635 switch(VT) {
1636 default: assert(0 && "Invalid ctpop!"); break;
1637 case MVT::i1: return getConstant(Val != 0, VT);
1638 case MVT::i8:
1639 Tmp1 = (unsigned)Val & 0xFF;
1640 return getConstant(CountPopulation_32(Tmp1), VT);
1641 case MVT::i16:
1642 Tmp1 = (unsigned)Val & 0xFFFF;
1643 return getConstant(CountPopulation_32(Tmp1), VT);
1644 case MVT::i32:
1645 return getConstant(CountPopulation_32((unsigned)Val), VT);
1646 case MVT::i64:
1647 return getConstant(CountPopulation_64(Val), VT);
1648 }
1649 case ISD::CTLZ:
1650 switch(VT) {
1651 default: assert(0 && "Invalid ctlz!"); break;
1652 case MVT::i1: return getConstant(Val == 0, VT);
1653 case MVT::i8:
1654 Tmp1 = (unsigned)Val & 0xFF;
1655 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1656 case MVT::i16:
1657 Tmp1 = (unsigned)Val & 0xFFFF;
1658 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1659 case MVT::i32:
1660 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1661 case MVT::i64:
1662 return getConstant(CountLeadingZeros_64(Val), VT);
1663 }
1664 case ISD::CTTZ:
1665 switch(VT) {
1666 default: assert(0 && "Invalid cttz!"); break;
1667 case MVT::i1: return getConstant(Val == 0, VT);
1668 case MVT::i8:
1669 Tmp1 = (unsigned)Val | 0x100;
1670 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1671 case MVT::i16:
1672 Tmp1 = (unsigned)Val | 0x10000;
1673 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1674 case MVT::i32:
1675 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1676 case MVT::i64:
1677 return getConstant(CountTrailingZeros_64(Val), VT);
1678 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001679 }
1680 }
1681
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001682 // Constant fold unary operations with a floating point constant operand.
1683 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) {
1684 APFloat V = C->getValueAPF(); // make copy
Chris Lattnerc3aae252005-01-07 07:46:32 +00001685 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001686 case ISD::FNEG:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001687 V.changeSign();
1688 return getConstantFP(V, VT);
Chris Lattner94683772005-12-23 05:30:37 +00001689 case ISD::FABS:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001690 V.clearSign();
1691 return getConstantFP(V, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001692 case ISD::FP_ROUND:
1693 case ISD::FP_EXTEND:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001694 // This can return overflow, underflow, or inexact; we don't care.
1695 // FIXME need to be more flexible about rounding mode.
1696 (void) V.convert(VT==MVT::f32 ? APFloat::IEEEsingle :
Dale Johannesen73328d12007-09-19 23:55:34 +00001697 VT==MVT::f64 ? APFloat::IEEEdouble :
1698 VT==MVT::f80 ? APFloat::x87DoubleExtended :
1699 VT==MVT::f128 ? APFloat::IEEEquad :
1700 APFloat::Bogus,
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001701 APFloat::rmNearestTiesToEven);
1702 return getConstantFP(V, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001703 case ISD::FP_TO_SINT:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001704 case ISD::FP_TO_UINT: {
1705 integerPart x;
1706 assert(integerPartWidth >= 64);
1707 // FIXME need to be more flexible about rounding mode.
1708 APFloat::opStatus s = V.convertToInteger(&x, 64U,
1709 Opcode==ISD::FP_TO_SINT,
1710 APFloat::rmTowardZero);
1711 if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
1712 break;
1713 return getConstant(x, VT);
1714 }
Chris Lattner94683772005-12-23 05:30:37 +00001715 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001716 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001717 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001718 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001719 return getConstant(V.convertToAPInt().getZExtValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001720 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001721 }
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001722 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001723
1724 unsigned OpOpcode = Operand.Val->getOpcode();
1725 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001726 case ISD::TokenFactor:
1727 return Operand; // Factor of one node? No factor.
Chris Lattnerff33cc42007-04-09 05:23:13 +00001728 case ISD::FP_ROUND:
1729 case ISD::FP_EXTEND:
1730 assert(MVT::isFloatingPoint(VT) &&
1731 MVT::isFloatingPoint(Operand.getValueType()) && "Invalid FP cast!");
1732 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001733 case ISD::SIGN_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001734 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1735 "Invalid SIGN_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001736 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001737 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001738 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1739 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1740 break;
1741 case ISD::ZERO_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001742 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1743 "Invalid ZERO_EXTEND!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001744 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001745 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001746 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001747 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001748 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001749 case ISD::ANY_EXTEND:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001750 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1751 "Invalid ANY_EXTEND!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001752 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001753 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001754 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1755 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1756 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1757 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 case ISD::TRUNCATE:
Chris Lattnerff33cc42007-04-09 05:23:13 +00001759 assert(MVT::isInteger(VT) && MVT::isInteger(Operand.getValueType()) &&
1760 "Invalid TRUNCATE!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001761 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001762 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001763 if (OpOpcode == ISD::TRUNCATE)
1764 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001765 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1766 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001767 // If the source is smaller than the dest, we still need an extend.
1768 if (Operand.Val->getOperand(0).getValueType() < VT)
1769 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1770 else if (Operand.Val->getOperand(0).getValueType() > VT)
1771 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1772 else
1773 return Operand.Val->getOperand(0);
1774 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001775 break;
Chris Lattner35481892005-12-23 00:16:34 +00001776 case ISD::BIT_CONVERT:
1777 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001778 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Reid Spencera07d5b92006-11-11 20:07:59 +00001779 && "Cannot BIT_CONVERT between types of different sizes!");
Chris Lattner35481892005-12-23 00:16:34 +00001780 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001781 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1782 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001783 if (OpOpcode == ISD::UNDEF)
1784 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001785 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001786 case ISD::SCALAR_TO_VECTOR:
1787 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
Dan Gohman51eaa862007-06-14 22:58:02 +00001788 MVT::getVectorElementType(VT) == Operand.getValueType() &&
Chris Lattnerce872152006-03-19 06:31:19 +00001789 "Illegal SCALAR_TO_VECTOR node!");
1790 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001791 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001792 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1793 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001794 Operand.Val->getOperand(0));
1795 if (OpOpcode == ISD::FNEG) // --X -> X
1796 return Operand.Val->getOperand(0);
1797 break;
1798 case ISD::FABS:
1799 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1800 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1801 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001802 }
1803
Chris Lattner43247a12005-08-25 19:12:10 +00001804 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00001805 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00001806 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
Jim Laskey583bd472006-10-27 23:46:08 +00001807 FoldingSetNodeID ID;
Chris Lattner3f97eb42007-02-04 08:35:21 +00001808 SDOperand Ops[1] = { Operand };
Chris Lattner63e3f142007-02-04 07:28:00 +00001809 AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00001810 void *IP = 0;
1811 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
1812 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00001813 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattnera5682852006-08-07 23:03:03 +00001814 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00001815 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00001816 N = new UnarySDNode(Opcode, VTs, Operand);
Chris Lattner43247a12005-08-25 19:12:10 +00001817 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001818 AllNodes.push_back(N);
1819 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001820}
1821
Chris Lattner36019aa2005-04-18 03:48:41 +00001822
1823
Chris Lattnerc3aae252005-01-07 07:46:32 +00001824SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1825 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001826#ifndef NDEBUG
1827 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001828 case ISD::TokenFactor:
1829 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1830 N2.getValueType() == MVT::Other && "Invalid token factor!");
1831 break;
Chris Lattner76365122005-01-16 02:23:22 +00001832 case ISD::AND:
1833 case ISD::OR:
1834 case ISD::XOR:
1835 case ISD::UDIV:
1836 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001837 case ISD::MULHU:
1838 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001839 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1840 // fall through
1841 case ISD::ADD:
1842 case ISD::SUB:
1843 case ISD::MUL:
1844 case ISD::SDIV:
1845 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001846 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1847 // fall through.
1848 case ISD::FADD:
1849 case ISD::FSUB:
1850 case ISD::FMUL:
1851 case ISD::FDIV:
1852 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001853 assert(N1.getValueType() == N2.getValueType() &&
1854 N1.getValueType() == VT && "Binary operator types must match!");
1855 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001856 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1857 assert(N1.getValueType() == VT &&
1858 MVT::isFloatingPoint(N1.getValueType()) &&
1859 MVT::isFloatingPoint(N2.getValueType()) &&
1860 "Invalid FCOPYSIGN!");
1861 break;
Chris Lattner76365122005-01-16 02:23:22 +00001862 case ISD::SHL:
1863 case ISD::SRA:
1864 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001865 case ISD::ROTL:
1866 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001867 assert(VT == N1.getValueType() &&
1868 "Shift operators return type must be the same as their first arg");
1869 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001870 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001871 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001872 case ISD::FP_ROUND_INREG: {
1873 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1874 assert(VT == N1.getValueType() && "Not an inreg round!");
1875 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1876 "Cannot FP_ROUND_INREG integer types");
1877 assert(EVT <= VT && "Not rounding down!");
1878 break;
1879 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001880 case ISD::AssertSext:
1881 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001882 case ISD::SIGN_EXTEND_INREG: {
1883 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1884 assert(VT == N1.getValueType() && "Not an inreg extend!");
1885 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1886 "Cannot *_EXTEND_INREG FP types");
1887 assert(EVT <= VT && "Not extending!");
1888 }
1889
Chris Lattner76365122005-01-16 02:23:22 +00001890 default: break;
1891 }
1892#endif
1893
Chris Lattnerc3aae252005-01-07 07:46:32 +00001894 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1895 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1896 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001897 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1898 int64_t Val = N1C->getValue();
1899 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1900 Val <<= 64-FromBits;
1901 Val >>= 64-FromBits;
1902 return getConstant(Val, VT);
1903 }
1904
Chris Lattnerc3aae252005-01-07 07:46:32 +00001905 if (N2C) {
1906 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1907 switch (Opcode) {
1908 case ISD::ADD: return getConstant(C1 + C2, VT);
1909 case ISD::SUB: return getConstant(C1 - C2, VT);
1910 case ISD::MUL: return getConstant(C1 * C2, VT);
1911 case ISD::UDIV:
1912 if (C2) return getConstant(C1 / C2, VT);
1913 break;
1914 case ISD::UREM :
1915 if (C2) return getConstant(C1 % C2, VT);
1916 break;
1917 case ISD::SDIV :
1918 if (C2) return getConstant(N1C->getSignExtended() /
1919 N2C->getSignExtended(), VT);
1920 break;
1921 case ISD::SREM :
1922 if (C2) return getConstant(N1C->getSignExtended() %
1923 N2C->getSignExtended(), VT);
1924 break;
1925 case ISD::AND : return getConstant(C1 & C2, VT);
1926 case ISD::OR : return getConstant(C1 | C2, VT);
1927 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001928 case ISD::SHL : return getConstant(C1 << C2, VT);
1929 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001930 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001931 case ISD::ROTL :
1932 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1933 VT);
1934 case ISD::ROTR :
1935 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1936 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001937 default: break;
1938 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001939 } else { // Cannonicalize constant to RHS if commutative
1940 if (isCommutativeBinOp(Opcode)) {
1941 std::swap(N1C, N2C);
1942 std::swap(N1, N2);
1943 }
1944 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001945 }
1946
1947 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1948 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001949 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001950 if (N2CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001951 APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
1952 APFloat::opStatus s;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001953 switch (Opcode) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001954 case ISD::FADD:
1955 s = V1.add(V2, APFloat::rmNearestTiesToEven);
1956 if (s!=APFloat::opInvalidOp)
1957 return getConstantFP(V1, VT);
1958 break;
1959 case ISD::FSUB:
1960 s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
1961 if (s!=APFloat::opInvalidOp)
1962 return getConstantFP(V1, VT);
1963 break;
1964 case ISD::FMUL:
1965 s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
1966 if (s!=APFloat::opInvalidOp)
1967 return getConstantFP(V1, VT);
1968 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001969 case ISD::FDIV:
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001970 s = V1.divide(V2, APFloat::rmNearestTiesToEven);
1971 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1972 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001973 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001974 case ISD::FREM :
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001975 s = V1.mod(V2, APFloat::rmNearestTiesToEven);
1976 if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
1977 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001978 break;
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00001979 case ISD::FCOPYSIGN:
1980 V1.copySign(V2);
1981 return getConstantFP(V1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001982 default: break;
1983 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001984 } else { // Cannonicalize constant to RHS if commutative
1985 if (isCommutativeBinOp(Opcode)) {
1986 std::swap(N1CFP, N2CFP);
1987 std::swap(N1, N2);
1988 }
1989 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001990 }
Chris Lattner62b57722006-04-20 05:39:12 +00001991
1992 // Canonicalize an UNDEF to the RHS, even over a constant.
1993 if (N1.getOpcode() == ISD::UNDEF) {
1994 if (isCommutativeBinOp(Opcode)) {
1995 std::swap(N1, N2);
1996 } else {
1997 switch (Opcode) {
1998 case ISD::FP_ROUND_INREG:
1999 case ISD::SIGN_EXTEND_INREG:
2000 case ISD::SUB:
2001 case ISD::FSUB:
2002 case ISD::FDIV:
2003 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002004 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00002005 return N1; // fold op(undef, arg2) -> undef
2006 case ISD::UDIV:
2007 case ISD::SDIV:
2008 case ISD::UREM:
2009 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002010 case ISD::SRL:
2011 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002012 if (!MVT::isVector(VT))
2013 return getConstant(0, VT); // fold op(undef, arg2) -> 0
2014 // For vectors, we can't easily build an all zero vector, just return
2015 // the LHS.
2016 return N2;
Chris Lattner62b57722006-04-20 05:39:12 +00002017 }
2018 }
2019 }
2020
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00002021 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00002022 if (N2.getOpcode() == ISD::UNDEF) {
2023 switch (Opcode) {
2024 case ISD::ADD:
Chris Lattner175415e2007-03-04 20:01:46 +00002025 case ISD::ADDC:
2026 case ISD::ADDE:
Chris Lattner62b57722006-04-20 05:39:12 +00002027 case ISD::SUB:
2028 case ISD::FADD:
2029 case ISD::FSUB:
2030 case ISD::FMUL:
2031 case ISD::FDIV:
2032 case ISD::FREM:
2033 case ISD::UDIV:
2034 case ISD::SDIV:
2035 case ISD::UREM:
2036 case ISD::SREM:
2037 case ISD::XOR:
2038 return N2; // fold op(arg1, undef) -> undef
2039 case ISD::MUL:
2040 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00002041 case ISD::SRL:
2042 case ISD::SHL:
Chris Lattner964dd862007-04-25 00:00:45 +00002043 if (!MVT::isVector(VT))
2044 return getConstant(0, VT); // fold op(arg1, undef) -> 0
2045 // For vectors, we can't easily build an all zero vector, just return
2046 // the LHS.
2047 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002048 case ISD::OR:
Chris Lattner964dd862007-04-25 00:00:45 +00002049 if (!MVT::isVector(VT))
2050 return getConstant(MVT::getIntVTBitMask(VT), VT);
2051 // For vectors, we can't easily build an all one vector, just return
2052 // the LHS.
2053 return N1;
Chris Lattner2cfd6742006-05-08 17:29:49 +00002054 case ISD::SRA:
2055 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00002056 }
2057 }
Chris Lattner15e4b012005-07-10 00:07:11 +00002058
Chris Lattner51dabfb2006-10-14 00:41:01 +00002059 // Fold operations.
Chris Lattnerc3aae252005-01-07 07:46:32 +00002060 switch (Opcode) {
Chris Lattner753d9cb2007-02-25 08:24:27 +00002061 case ISD::TokenFactor:
2062 // Fold trivial token factors.
2063 if (N1.getOpcode() == ISD::EntryToken) return N2;
2064 if (N2.getOpcode() == ISD::EntryToken) return N1;
2065 break;
2066
Chris Lattner51dabfb2006-10-14 00:41:01 +00002067 case ISD::AND:
2068 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
2069 // worth handling here.
2070 if (N2C && N2C->getValue() == 0)
2071 return N2;
2072 break;
Chris Lattnerb3607292006-10-17 21:47:13 +00002073 case ISD::OR:
2074 case ISD::XOR:
2075 // (X ^| 0) -> X. This commonly occurs when legalizing i64 values, so it's
2076 // worth handling here.
2077 if (N2C && N2C->getValue() == 0)
2078 return N1;
2079 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002080 case ISD::FP_ROUND_INREG:
2081 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
2082 break;
2083 case ISD::SIGN_EXTEND_INREG: {
2084 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
2085 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00002086 break;
2087 }
Dan Gohman7f321562007-06-25 16:23:39 +00002088 case ISD::EXTRACT_VECTOR_ELT:
2089 assert(N2C && "Bad EXTRACT_VECTOR_ELT!");
2090
Dan Gohman743d3a72007-07-10 18:20:44 +00002091 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
Dan Gohman7f321562007-06-25 16:23:39 +00002092 // expanding copies of large vectors from registers.
Dan Gohman743d3a72007-07-10 18:20:44 +00002093 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
2094 N1.getNumOperands() > 0) {
2095 unsigned Factor =
2096 MVT::getVectorNumElements(N1.getOperand(0).getValueType());
2097 return getNode(ISD::EXTRACT_VECTOR_ELT, VT,
2098 N1.getOperand(N2C->getValue() / Factor),
2099 getConstant(N2C->getValue() % Factor, N2.getValueType()));
Dan Gohman7f321562007-06-25 16:23:39 +00002100 }
Dan Gohman743d3a72007-07-10 18:20:44 +00002101
Dan Gohman7f321562007-06-25 16:23:39 +00002102 // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
2103 // expanding large vector constants.
2104 if (N1.getOpcode() == ISD::BUILD_VECTOR)
2105 return N1.getOperand(N2C->getValue());
Dan Gohman743d3a72007-07-10 18:20:44 +00002106
2107 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
2108 // operations are lowered to scalars.
2109 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT)
2110 if (ConstantSDNode *IEC = dyn_cast<ConstantSDNode>(N1.getOperand(2))) {
2111 if (IEC == N2C)
2112 return N1.getOperand(1);
2113 else
2114 return getNode(ISD::EXTRACT_VECTOR_ELT, VT, N1.getOperand(0), N2);
2115 }
Dan Gohman7f321562007-06-25 16:23:39 +00002116 break;
Chris Lattner5c6621c2006-09-19 04:51:23 +00002117 case ISD::EXTRACT_ELEMENT:
Chris Lattner863ac762006-09-19 05:02:39 +00002118 assert(N2C && (unsigned)N2C->getValue() < 2 && "Bad EXTRACT_ELEMENT!");
2119
Chris Lattner5c6621c2006-09-19 04:51:23 +00002120 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
2121 // 64-bit integers into 32-bit parts. Instead of building the extract of
2122 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
Chris Lattner863ac762006-09-19 05:02:39 +00002123 if (N1.getOpcode() == ISD::BUILD_PAIR)
Chris Lattner5c6621c2006-09-19 04:51:23 +00002124 return N1.getOperand(N2C->getValue());
Chris Lattner863ac762006-09-19 05:02:39 +00002125
2126 // EXTRACT_ELEMENT of a constant int is also very common.
2127 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2128 unsigned Shift = MVT::getSizeInBits(VT) * N2C->getValue();
2129 return getConstant(C->getValue() >> Shift, VT);
Chris Lattner5c6621c2006-09-19 04:51:23 +00002130 }
2131 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00002132
Nate Begemaneea805e2005-04-13 21:23:31 +00002133 // FIXME: figure out how to safely handle things like
2134 // int foo(int x) { return 1 << (x & 255); }
2135 // int bar() { return foo(256); }
2136#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00002137 case ISD::SHL:
2138 case ISD::SRL:
2139 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00002140 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002141 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00002142 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00002143 else if (N2.getOpcode() == ISD::AND)
2144 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
2145 // If the and is only masking out bits that cannot effect the shift,
2146 // eliminate the and.
2147 unsigned NumBits = MVT::getSizeInBits(VT);
2148 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2149 return getNode(Opcode, VT, N1, N2.getOperand(0));
2150 }
Nate Begemandb81eba2005-04-12 23:32:28 +00002151 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00002152#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00002153 }
2154
Chris Lattner27e9b412005-05-11 18:57:39 +00002155 // Memoize this node if possible.
2156 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002157 SDVTList VTs = getVTList(VT);
Chris Lattner70814bc2006-01-29 07:58:15 +00002158 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002159 SDOperand Ops[] = { N1, N2 };
Jim Laskey583bd472006-10-27 23:46:08 +00002160 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002161 AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002162 void *IP = 0;
2163 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2164 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002165 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattnera5682852006-08-07 23:03:03 +00002166 CSEMap.InsertNode(N, IP);
Chris Lattner27e9b412005-05-11 18:57:39 +00002167 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002168 N = new BinarySDNode(Opcode, VTs, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00002169 }
2170
Chris Lattnerc3aae252005-01-07 07:46:32 +00002171 AllNodes.push_back(N);
2172 return SDOperand(N, 0);
2173}
2174
Chris Lattnerc3aae252005-01-07 07:46:32 +00002175SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2176 SDOperand N1, SDOperand N2, SDOperand N3) {
2177 // Perform various simplifications.
2178 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
2179 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002180 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002181 case ISD::SETCC: {
Chris Lattner51dabfb2006-10-14 00:41:01 +00002182 // Use FoldSetCC to simplify SETCC's.
2183 SDOperand Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002184 if (Simp.Val) return Simp;
2185 break;
2186 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002187 case ISD::SELECT:
2188 if (N1C)
2189 if (N1C->getValue())
2190 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00002191 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00002192 return N3; // select false, X, Y -> Y
2193
2194 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00002195 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00002196 case ISD::BRCOND:
2197 if (N2C)
2198 if (N2C->getValue()) // Unconditional branch
2199 return getNode(ISD::BR, MVT::Other, N1, N3);
2200 else
2201 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00002202 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00002203 case ISD::VECTOR_SHUFFLE:
2204 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2205 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
2206 N3.getOpcode() == ISD::BUILD_VECTOR &&
2207 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
2208 "Illegal VECTOR_SHUFFLE node!");
2209 break;
Dan Gohman7f321562007-06-25 16:23:39 +00002210 case ISD::BIT_CONVERT:
2211 // Fold bit_convert nodes from a type to themselves.
2212 if (N1.getValueType() == VT)
2213 return N1;
Chris Lattner4829b1c2007-04-12 05:58:43 +00002214 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002215 }
2216
Chris Lattner43247a12005-08-25 19:12:10 +00002217 // Memoize node if it doesn't produce a flag.
2218 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002219 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002220 if (VT != MVT::Flag) {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002221 SDOperand Ops[] = { N1, N2, N3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002222 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002223 AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002224 void *IP = 0;
2225 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2226 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002227 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattnera5682852006-08-07 23:03:03 +00002228 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002229 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002230 N = new TernarySDNode(Opcode, VTs, N1, N2, N3);
Chris Lattner43247a12005-08-25 19:12:10 +00002231 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002232 AllNodes.push_back(N);
2233 return SDOperand(N, 0);
2234}
2235
2236SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00002237 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002238 SDOperand N4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002239 SDOperand Ops[] = { N1, N2, N3, N4 };
2240 return getNode(Opcode, VT, Ops, 4);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002241}
2242
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002243SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
2244 SDOperand N1, SDOperand N2, SDOperand N3,
2245 SDOperand N4, SDOperand N5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002246 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2247 return getNode(Opcode, VT, Ops, 5);
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002248}
2249
Evan Cheng7038daf2005-12-10 00:37:58 +00002250SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
2251 SDOperand Chain, SDOperand Ptr,
Evan Cheng466685d2006-10-09 20:57:25 +00002252 const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002253 bool isVolatile, unsigned Alignment) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002254 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2255 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002256 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002257 Ty = MVT::getTypeForValueType(VT);
2258 } else if (SV) {
2259 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2260 assert(PT && "Value for load must be a pointer");
2261 Ty = PT->getElementType();
2262 }
2263 assert(Ty && "Could not get type information for load");
2264 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2265 }
Chris Lattner0b3e5252006-08-15 19:11:05 +00002266 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002267 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002268 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002269 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002270 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002271 ID.AddInteger(ISD::UNINDEXED);
2272 ID.AddInteger(ISD::NON_EXTLOAD);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002273 ID.AddInteger((unsigned int)VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002274 ID.AddPointer(SV);
2275 ID.AddInteger(SVOffset);
2276 ID.AddInteger(Alignment);
2277 ID.AddInteger(isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002278 void *IP = 0;
2279 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2280 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002281 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002282 ISD::NON_EXTLOAD, VT, SV, SVOffset, Alignment,
2283 isVolatile);
Evan Cheng466685d2006-10-09 20:57:25 +00002284 CSEMap.InsertNode(N, IP);
2285 AllNodes.push_back(N);
2286 return SDOperand(N, 0);
2287}
2288
2289SDOperand SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00002290 SDOperand Chain, SDOperand Ptr,
2291 const Value *SV,
Evan Cheng466685d2006-10-09 20:57:25 +00002292 int SVOffset, MVT::ValueType EVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002293 bool isVolatile, unsigned Alignment) {
Evan Cheng466685d2006-10-09 20:57:25 +00002294 // If they are asking for an extending load from/to the same thing, return a
2295 // normal load.
2296 if (VT == EVT)
2297 ExtType = ISD::NON_EXTLOAD;
2298
2299 if (MVT::isVector(VT))
Dan Gohman51eaa862007-06-14 22:58:02 +00002300 assert(EVT == MVT::getVectorElementType(VT) && "Invalid vector extload!");
Evan Cheng466685d2006-10-09 20:57:25 +00002301 else
2302 assert(EVT < VT && "Should only be an extending load, not truncating!");
2303 assert((ExtType == ISD::EXTLOAD || MVT::isInteger(VT)) &&
2304 "Cannot sign/zero extend a FP/Vector load!");
2305 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
2306 "Cannot convert from FP to Int or Int -> FP!");
2307
Dan Gohman575e2f42007-06-04 15:49:41 +00002308 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2309 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002310 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002311 Ty = MVT::getTypeForValueType(VT);
2312 } else if (SV) {
2313 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2314 assert(PT && "Value for load must be a pointer");
2315 Ty = PT->getElementType();
2316 }
2317 assert(Ty && "Could not get type information for load");
2318 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2319 }
Evan Cheng466685d2006-10-09 20:57:25 +00002320 SDVTList VTs = getVTList(VT, MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002321 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Chris Lattner63e3f142007-02-04 07:28:00 +00002322 SDOperand Ops[] = { Chain, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002323 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002324 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng466685d2006-10-09 20:57:25 +00002325 ID.AddInteger(ISD::UNINDEXED);
2326 ID.AddInteger(ExtType);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002327 ID.AddInteger((unsigned int)EVT);
Evan Cheng466685d2006-10-09 20:57:25 +00002328 ID.AddPointer(SV);
2329 ID.AddInteger(SVOffset);
2330 ID.AddInteger(Alignment);
2331 ID.AddInteger(isVolatile);
2332 void *IP = 0;
2333 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2334 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002335 SDNode *N = new LoadSDNode(Ops, VTs, ISD::UNINDEXED, ExtType, EVT,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002336 SV, SVOffset, Alignment, isVolatile);
Chris Lattnera5682852006-08-07 23:03:03 +00002337 CSEMap.InsertNode(N, IP);
Evan Cheng7038daf2005-12-10 00:37:58 +00002338 AllNodes.push_back(N);
2339 return SDOperand(N, 0);
2340}
2341
Evan Cheng144d8f02006-11-09 17:55:04 +00002342SDOperand
2343SelectionDAG::getIndexedLoad(SDOperand OrigLoad, SDOperand Base,
2344 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00002345 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
Evan Cheng5270cf12006-10-26 21:53:40 +00002346 assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
2347 "Load is already a indexed load!");
Evan Cheng2caccca2006-10-17 21:14:32 +00002348 MVT::ValueType VT = OrigLoad.getValueType();
Evan Cheng5270cf12006-10-26 21:53:40 +00002349 SDVTList VTs = getVTList(VT, Base.getValueType(), MVT::Other);
Chris Lattner63e3f142007-02-04 07:28:00 +00002350 SDOperand Ops[] = { LD->getChain(), Base, Offset };
Jim Laskey583bd472006-10-27 23:46:08 +00002351 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002352 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
Evan Cheng2caccca2006-10-17 21:14:32 +00002353 ID.AddInteger(AM);
2354 ID.AddInteger(LD->getExtensionType());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002355 ID.AddInteger((unsigned int)(LD->getLoadedVT()));
Evan Cheng2caccca2006-10-17 21:14:32 +00002356 ID.AddPointer(LD->getSrcValue());
2357 ID.AddInteger(LD->getSrcValueOffset());
2358 ID.AddInteger(LD->getAlignment());
2359 ID.AddInteger(LD->isVolatile());
2360 void *IP = 0;
2361 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2362 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002363 SDNode *N = new LoadSDNode(Ops, VTs, AM,
Evan Cheng2caccca2006-10-17 21:14:32 +00002364 LD->getExtensionType(), LD->getLoadedVT(),
2365 LD->getSrcValue(), LD->getSrcValueOffset(),
2366 LD->getAlignment(), LD->isVolatile());
Evan Cheng2caccca2006-10-17 21:14:32 +00002367 CSEMap.InsertNode(N, IP);
2368 AllNodes.push_back(N);
2369 return SDOperand(N, 0);
2370}
2371
Jeff Cohend41b30d2006-11-05 19:31:28 +00002372SDOperand SelectionDAG::getStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002373 SDOperand Ptr, const Value *SV, int SVOffset,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002374 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002375 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002376
Dan Gohman575e2f42007-06-04 15:49:41 +00002377 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2378 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002379 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002380 Ty = MVT::getTypeForValueType(VT);
2381 } else if (SV) {
2382 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2383 assert(PT && "Value for store must be a pointer");
2384 Ty = PT->getElementType();
2385 }
2386 assert(Ty && "Could not get type information for store");
2387 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2388 }
Evan Chengad071e12006-10-05 22:57:11 +00002389 SDVTList VTs = getVTList(MVT::Other);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002390 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002391 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002392 FoldingSetNodeID ID;
2393 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002394 ID.AddInteger(ISD::UNINDEXED);
2395 ID.AddInteger(false);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002396 ID.AddInteger((unsigned int)VT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002397 ID.AddPointer(SV);
2398 ID.AddInteger(SVOffset);
2399 ID.AddInteger(Alignment);
2400 ID.AddInteger(isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002401 void *IP = 0;
2402 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2403 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002404 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, false,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002405 VT, SV, SVOffset, Alignment, isVolatile);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002406 CSEMap.InsertNode(N, IP);
2407 AllNodes.push_back(N);
2408 return SDOperand(N, 0);
2409}
2410
Jeff Cohend41b30d2006-11-05 19:31:28 +00002411SDOperand SelectionDAG::getTruncStore(SDOperand Chain, SDOperand Val,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002412 SDOperand Ptr, const Value *SV,
2413 int SVOffset, MVT::ValueType SVT,
Christopher Lamb95c218a2007-04-22 23:15:30 +00002414 bool isVolatile, unsigned Alignment) {
Jeff Cohend41b30d2006-11-05 19:31:28 +00002415 MVT::ValueType VT = Val.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002416 bool isTrunc = VT != SVT;
2417
2418 assert(VT > SVT && "Not a truncation?");
2419 assert(MVT::isInteger(VT) == MVT::isInteger(SVT) &&
2420 "Can't do FP-INT conversion!");
2421
Dan Gohman575e2f42007-06-04 15:49:41 +00002422 if (Alignment == 0) { // Ensure that codegen never sees alignment 0
2423 const Type *Ty = 0;
Dan Gohman7f321562007-06-25 16:23:39 +00002424 if (VT != MVT::iPTR) {
Dan Gohman575e2f42007-06-04 15:49:41 +00002425 Ty = MVT::getTypeForValueType(VT);
2426 } else if (SV) {
2427 const PointerType *PT = dyn_cast<PointerType>(SV->getType());
2428 assert(PT && "Value for store must be a pointer");
2429 Ty = PT->getElementType();
2430 }
2431 assert(Ty && "Could not get type information for store");
2432 Alignment = TLI.getTargetData()->getABITypeAlignment(Ty);
2433 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002434 SDVTList VTs = getVTList(MVT::Other);
2435 SDOperand Undef = getNode(ISD::UNDEF, Ptr.getValueType());
Jeff Cohend41b30d2006-11-05 19:31:28 +00002436 SDOperand Ops[] = { Chain, Val, Ptr, Undef };
Jim Laskey583bd472006-10-27 23:46:08 +00002437 FoldingSetNodeID ID;
2438 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002439 ID.AddInteger(ISD::UNINDEXED);
2440 ID.AddInteger(isTrunc);
Chris Lattner3d6992f2007-09-13 06:09:48 +00002441 ID.AddInteger((unsigned int)SVT);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002442 ID.AddPointer(SV);
2443 ID.AddInteger(SVOffset);
2444 ID.AddInteger(Alignment);
2445 ID.AddInteger(isVolatile);
2446 void *IP = 0;
2447 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2448 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002449 SDNode *N = new StoreSDNode(Ops, VTs, ISD::UNINDEXED, isTrunc,
Evan Cheng8b2794a2006-10-13 21:14:26 +00002450 SVT, SV, SVOffset, Alignment, isVolatile);
Evan Chengad071e12006-10-05 22:57:11 +00002451 CSEMap.InsertNode(N, IP);
2452 AllNodes.push_back(N);
2453 return SDOperand(N, 0);
2454}
2455
Evan Cheng144d8f02006-11-09 17:55:04 +00002456SDOperand
2457SelectionDAG::getIndexedStore(SDOperand OrigStore, SDOperand Base,
2458 SDOperand Offset, ISD::MemIndexedMode AM) {
Evan Cheng9109fb12006-11-05 09:30:09 +00002459 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
2460 assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
2461 "Store is already a indexed store!");
2462 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
2463 SDOperand Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
2464 FoldingSetNodeID ID;
2465 AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
2466 ID.AddInteger(AM);
2467 ID.AddInteger(ST->isTruncatingStore());
Chris Lattner3d6992f2007-09-13 06:09:48 +00002468 ID.AddInteger((unsigned int)(ST->getStoredVT()));
Evan Cheng9109fb12006-11-05 09:30:09 +00002469 ID.AddPointer(ST->getSrcValue());
2470 ID.AddInteger(ST->getSrcValueOffset());
2471 ID.AddInteger(ST->getAlignment());
2472 ID.AddInteger(ST->isVolatile());
2473 void *IP = 0;
2474 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2475 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002476 SDNode *N = new StoreSDNode(Ops, VTs, AM,
Evan Cheng9109fb12006-11-05 09:30:09 +00002477 ST->isTruncatingStore(), ST->getStoredVT(),
2478 ST->getSrcValue(), ST->getSrcValueOffset(),
2479 ST->getAlignment(), ST->isVolatile());
Evan Cheng9109fb12006-11-05 09:30:09 +00002480 CSEMap.InsertNode(N, IP);
2481 AllNodes.push_back(N);
2482 return SDOperand(N, 0);
2483}
2484
Nate Begemanacc398c2006-01-25 18:21:52 +00002485SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
2486 SDOperand Chain, SDOperand Ptr,
2487 SDOperand SV) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002488 SDOperand Ops[] = { Chain, Ptr, SV };
Chris Lattnerbe384162006-08-16 22:57:46 +00002489 return getNode(ISD::VAARG, getVTList(VT, MVT::Other), Ops, 3);
Nate Begemanacc398c2006-01-25 18:21:52 +00002490}
2491
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002492SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002493 const SDOperand *Ops, unsigned NumOps) {
2494 switch (NumOps) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002495 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00002496 case 1: return getNode(Opcode, VT, Ops[0]);
2497 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
2498 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00002499 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 }
Chris Lattnerde202b32005-11-09 23:47:37 +00002501
Chris Lattneref847df2005-04-09 03:27:28 +00002502 switch (Opcode) {
2503 default: break;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002504 case ISD::SELECT_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002505 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002506 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
2507 "LHS and RHS of condition must have same type!");
2508 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2509 "True and False arms of SelectCC must have same type!");
2510 assert(Ops[2].getValueType() == VT &&
2511 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002512 break;
2513 }
2514 case ISD::BR_CC: {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002515 assert(NumOps == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002516 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2517 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002518 break;
2519 }
Chris Lattneref847df2005-04-09 03:27:28 +00002520 }
2521
Chris Lattner385328c2005-05-14 07:42:29 +00002522 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002523 SDNode *N;
Chris Lattner0b3e5252006-08-15 19:11:05 +00002524 SDVTList VTs = getVTList(VT);
Chris Lattner43247a12005-08-25 19:12:10 +00002525 if (VT != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002526 FoldingSetNodeID ID;
2527 AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002528 void *IP = 0;
2529 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2530 return SDOperand(E, 0);
Chris Lattnerab4ed592007-02-04 07:37:24 +00002531 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002532 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002533 } else {
Chris Lattnerab4ed592007-02-04 07:37:24 +00002534 N = new SDNode(Opcode, VTs, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002535 }
Chris Lattneref847df2005-04-09 03:27:28 +00002536 AllNodes.push_back(N);
2537 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002538}
2539
Chris Lattner89c34632005-05-14 06:20:26 +00002540SDOperand SelectionDAG::getNode(unsigned Opcode,
2541 std::vector<MVT::ValueType> &ResultTys,
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002542 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002543 return getNode(Opcode, getNodeValueTypes(ResultTys), ResultTys.size(),
2544 Ops, NumOps);
2545}
2546
2547SDOperand SelectionDAG::getNode(unsigned Opcode,
2548 const MVT::ValueType *VTs, unsigned NumVTs,
2549 const SDOperand *Ops, unsigned NumOps) {
2550 if (NumVTs == 1)
2551 return getNode(Opcode, VTs[0], Ops, NumOps);
Chris Lattnerbe384162006-08-16 22:57:46 +00002552 return getNode(Opcode, makeVTList(VTs, NumVTs), Ops, NumOps);
2553}
2554
2555SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2556 const SDOperand *Ops, unsigned NumOps) {
2557 if (VTList.NumVTs == 1)
2558 return getNode(Opcode, VTList.VTs[0], Ops, NumOps);
Chris Lattner89c34632005-05-14 06:20:26 +00002559
Chris Lattner5f056bf2005-07-10 01:55:33 +00002560 switch (Opcode) {
Chris Lattnere89083a2005-05-14 07:25:05 +00002561 // FIXME: figure out how to safely handle things like
2562 // int foo(int x) { return 1 << (x & 255); }
2563 // int bar() { return foo(256); }
2564#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002565 case ISD::SRA_PARTS:
2566 case ISD::SRL_PARTS:
2567 case ISD::SHL_PARTS:
2568 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002569 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002570 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2571 else if (N3.getOpcode() == ISD::AND)
2572 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2573 // If the and is only masking out bits that cannot effect the shift,
2574 // eliminate the and.
2575 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2576 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2577 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2578 }
2579 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002580#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002581 }
Chris Lattner89c34632005-05-14 06:20:26 +00002582
Chris Lattner43247a12005-08-25 19:12:10 +00002583 // Memoize the node unless it returns a flag.
2584 SDNode *N;
Chris Lattnerbe384162006-08-16 22:57:46 +00002585 if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
Jim Laskey583bd472006-10-27 23:46:08 +00002586 FoldingSetNodeID ID;
2587 AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002588 void *IP = 0;
2589 if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
2590 return SDOperand(E, 0);
Chris Lattner3f97eb42007-02-04 08:35:21 +00002591 if (NumOps == 1)
2592 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2593 else if (NumOps == 2)
2594 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2595 else if (NumOps == 3)
2596 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2597 else
2598 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002599 CSEMap.InsertNode(N, IP);
Chris Lattner43247a12005-08-25 19:12:10 +00002600 } else {
Chris Lattner3f97eb42007-02-04 08:35:21 +00002601 if (NumOps == 1)
2602 N = new UnarySDNode(Opcode, VTList, Ops[0]);
2603 else if (NumOps == 2)
2604 N = new BinarySDNode(Opcode, VTList, Ops[0], Ops[1]);
2605 else if (NumOps == 3)
2606 N = new TernarySDNode(Opcode, VTList, Ops[0], Ops[1], Ops[2]);
2607 else
2608 N = new SDNode(Opcode, VTList, Ops, NumOps);
Chris Lattner43247a12005-08-25 19:12:10 +00002609 }
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002610 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002611 return SDOperand(N, 0);
2612}
2613
Dan Gohman08ce9762007-10-08 15:49:58 +00002614SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList) {
2615 return getNode(Opcode, VTList, 0, 0);
2616}
2617
2618SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2619 SDOperand N1) {
2620 SDOperand Ops[] = { N1 };
2621 return getNode(Opcode, VTList, Ops, 1);
2622}
2623
2624SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2625 SDOperand N1, SDOperand N2) {
2626 SDOperand Ops[] = { N1, N2 };
2627 return getNode(Opcode, VTList, Ops, 2);
2628}
2629
2630SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2631 SDOperand N1, SDOperand N2, SDOperand N3) {
2632 SDOperand Ops[] = { N1, N2, N3 };
2633 return getNode(Opcode, VTList, Ops, 3);
2634}
2635
2636SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2637 SDOperand N1, SDOperand N2, SDOperand N3,
2638 SDOperand N4) {
2639 SDOperand Ops[] = { N1, N2, N3, N4 };
2640 return getNode(Opcode, VTList, Ops, 4);
2641}
2642
2643SDOperand SelectionDAG::getNode(unsigned Opcode, SDVTList VTList,
2644 SDOperand N1, SDOperand N2, SDOperand N3,
2645 SDOperand N4, SDOperand N5) {
2646 SDOperand Ops[] = { N1, N2, N3, N4, N5 };
2647 return getNode(Opcode, VTList, Ops, 5);
2648}
2649
Chris Lattner70046e92006-08-15 17:46:01 +00002650SDVTList SelectionDAG::getVTList(MVT::ValueType VT) {
Dan Gohman6595cb32007-06-27 16:08:04 +00002651 if (!MVT::isExtendedVT(VT))
Dan Gohman7f321562007-06-25 16:23:39 +00002652 return makeVTList(SDNode::getValueTypeList(VT), 1);
2653
2654 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2655 E = VTList.end(); I != E; ++I) {
2656 if (I->size() == 1 && (*I)[0] == VT)
2657 return makeVTList(&(*I)[0], 1);
2658 }
2659 std::vector<MVT::ValueType> V;
2660 V.push_back(VT);
2661 VTList.push_front(V);
2662 return makeVTList(&(*VTList.begin())[0], 1);
Chris Lattnera3255112005-11-08 23:30:28 +00002663}
2664
Chris Lattner70046e92006-08-15 17:46:01 +00002665SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2) {
Chris Lattnera3255112005-11-08 23:30:28 +00002666 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2667 E = VTList.end(); I != E; ++I) {
Chris Lattnera5682852006-08-07 23:03:03 +00002668 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2)
Chris Lattner70046e92006-08-15 17:46:01 +00002669 return makeVTList(&(*I)[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002670 }
2671 std::vector<MVT::ValueType> V;
2672 V.push_back(VT1);
2673 V.push_back(VT2);
2674 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002675 return makeVTList(&(*VTList.begin())[0], 2);
Chris Lattnera3255112005-11-08 23:30:28 +00002676}
Chris Lattner70046e92006-08-15 17:46:01 +00002677SDVTList SelectionDAG::getVTList(MVT::ValueType VT1, MVT::ValueType VT2,
2678 MVT::ValueType VT3) {
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002679 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
Chris Lattner70046e92006-08-15 17:46:01 +00002680 E = VTList.end(); I != E; ++I) {
2681 if (I->size() == 3 && (*I)[0] == VT1 && (*I)[1] == VT2 &&
2682 (*I)[2] == VT3)
2683 return makeVTList(&(*I)[0], 3);
2684 }
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002685 std::vector<MVT::ValueType> V;
2686 V.push_back(VT1);
2687 V.push_back(VT2);
2688 V.push_back(VT3);
2689 VTList.push_front(V);
Chris Lattner70046e92006-08-15 17:46:01 +00002690 return makeVTList(&(*VTList.begin())[0], 3);
2691}
2692
2693SDVTList SelectionDAG::getVTList(const MVT::ValueType *VTs, unsigned NumVTs) {
2694 switch (NumVTs) {
2695 case 0: assert(0 && "Cannot have nodes without results!");
Dan Gohman7f321562007-06-25 16:23:39 +00002696 case 1: return getVTList(VTs[0]);
Chris Lattner70046e92006-08-15 17:46:01 +00002697 case 2: return getVTList(VTs[0], VTs[1]);
2698 case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
2699 default: break;
2700 }
2701
2702 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
2703 E = VTList.end(); I != E; ++I) {
2704 if (I->size() != NumVTs || VTs[0] != (*I)[0] || VTs[1] != (*I)[1]) continue;
2705
2706 bool NoMatch = false;
2707 for (unsigned i = 2; i != NumVTs; ++i)
2708 if (VTs[i] != (*I)[i]) {
2709 NoMatch = true;
2710 break;
2711 }
2712 if (!NoMatch)
2713 return makeVTList(&*I->begin(), NumVTs);
2714 }
2715
2716 VTList.push_front(std::vector<MVT::ValueType>(VTs, VTs+NumVTs));
2717 return makeVTList(&*VTList.begin()->begin(), NumVTs);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00002718}
2719
2720
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002721/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
2722/// specified operands. If the resultant node already exists in the DAG,
2723/// this does not modify the specified node, instead it returns the node that
2724/// already exists. If the resultant node does not exist in the DAG, the
2725/// input node is returned. As a degenerate case, if you specify the same
2726/// input operands as the node already has, the input node is returned.
2727SDOperand SelectionDAG::
2728UpdateNodeOperands(SDOperand InN, SDOperand Op) {
2729 SDNode *N = InN.Val;
2730 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
2731
2732 // Check to see if there is no change.
2733 if (Op == N->getOperand(0)) return InN;
2734
2735 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002736 void *InsertPos = 0;
2737 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
2738 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002739
2740 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002741 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002742 RemoveNodeFromCSEMaps(N);
2743
2744 // Now we update the operands.
2745 N->OperandList[0].Val->removeUser(N);
2746 Op.Val->addUser(N);
2747 N->OperandList[0] = Op;
2748
2749 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002750 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002751 return InN;
2752}
2753
2754SDOperand SelectionDAG::
2755UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
2756 SDNode *N = InN.Val;
2757 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
2758
2759 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002760 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
2761 return InN; // No operands changed, just return the input node.
2762
2763 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002764 void *InsertPos = 0;
2765 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
2766 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002767
2768 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002769 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002770 RemoveNodeFromCSEMaps(N);
2771
2772 // Now we update the operands.
2773 if (N->OperandList[0] != Op1) {
2774 N->OperandList[0].Val->removeUser(N);
2775 Op1.Val->addUser(N);
2776 N->OperandList[0] = Op1;
2777 }
2778 if (N->OperandList[1] != Op2) {
2779 N->OperandList[1].Val->removeUser(N);
2780 Op2.Val->addUser(N);
2781 N->OperandList[1] = Op2;
2782 }
2783
2784 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002785 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002786 return InN;
2787}
2788
2789SDOperand SelectionDAG::
2790UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002791 SDOperand Ops[] = { Op1, Op2, Op3 };
2792 return UpdateNodeOperands(N, Ops, 3);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002793}
2794
2795SDOperand SelectionDAG::
2796UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2797 SDOperand Op3, SDOperand Op4) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002798 SDOperand Ops[] = { Op1, Op2, Op3, Op4 };
2799 return UpdateNodeOperands(N, Ops, 4);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002800}
2801
2802SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00002803UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
2804 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002805 SDOperand Ops[] = { Op1, Op2, Op3, Op4, Op5 };
2806 return UpdateNodeOperands(N, Ops, 5);
Chris Lattner809ec112006-01-28 10:09:25 +00002807}
2808
2809
2810SDOperand SelectionDAG::
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002811UpdateNodeOperands(SDOperand InN, SDOperand *Ops, unsigned NumOps) {
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002812 SDNode *N = InN.Val;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002813 assert(N->getNumOperands() == NumOps &&
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002814 "Update with wrong number of operands");
2815
2816 // Check to see if there is no change.
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002817 bool AnyChange = false;
2818 for (unsigned i = 0; i != NumOps; ++i) {
2819 if (Ops[i] != N->getOperand(i)) {
2820 AnyChange = true;
2821 break;
2822 }
2823 }
2824
2825 // No operands changed, just return the input node.
2826 if (!AnyChange) return InN;
2827
2828 // See if the modified node already exists.
Chris Lattnera5682852006-08-07 23:03:03 +00002829 void *InsertPos = 0;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002830 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
Chris Lattnera5682852006-08-07 23:03:03 +00002831 return SDOperand(Existing, InN.ResNo);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002832
2833 // Nope it doesn't. Remove the node from it's current place in the maps.
Chris Lattnera5682852006-08-07 23:03:03 +00002834 if (InsertPos)
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002835 RemoveNodeFromCSEMaps(N);
2836
2837 // Now we update the operands.
2838 for (unsigned i = 0; i != NumOps; ++i) {
2839 if (N->OperandList[i] != Ops[i]) {
2840 N->OperandList[i].Val->removeUser(N);
2841 Ops[i].Val->addUser(N);
2842 N->OperandList[i] = Ops[i];
2843 }
2844 }
2845
2846 // If this gets put into a CSE map, add it.
Chris Lattnera5682852006-08-07 23:03:03 +00002847 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
Chris Lattnerdf6eb302006-01-28 09:32:45 +00002848 return InN;
2849}
2850
2851
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002852/// MorphNodeTo - This frees the operands of the current node, resets the
2853/// opcode, types, and operands to the specified value. This should only be
2854/// used by the SelectionDAG class.
2855void SDNode::MorphNodeTo(unsigned Opc, SDVTList L,
2856 const SDOperand *Ops, unsigned NumOps) {
2857 NodeType = Opc;
2858 ValueList = L.VTs;
2859 NumValues = L.NumVTs;
2860
2861 // Clear the operands list, updating used nodes to remove this from their
2862 // use list.
2863 for (op_iterator I = op_begin(), E = op_end(); I != E; ++I)
2864 I->Val->removeUser(this);
Chris Lattner63e3f142007-02-04 07:28:00 +00002865
2866 // If NumOps is larger than the # of operands we currently have, reallocate
2867 // the operand list.
2868 if (NumOps > NumOperands) {
2869 if (OperandsNeedDelete)
2870 delete [] OperandList;
2871 OperandList = new SDOperand[NumOps];
2872 OperandsNeedDelete = true;
2873 }
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002874
2875 // Assign the new operands.
2876 NumOperands = NumOps;
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002877
2878 for (unsigned i = 0, e = NumOps; i != e; ++i) {
2879 OperandList[i] = Ops[i];
2880 SDNode *N = OperandList[i].Val;
2881 N->Uses.push_back(this);
2882 }
2883}
Chris Lattner149c58c2005-08-16 18:17:10 +00002884
2885/// SelectNodeTo - These are used for target selectors to *mutate* the
2886/// specified node to have the specified return type, Target opcode, and
2887/// operands. Note that target opcodes are stored as
2888/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002889///
2890/// Note that SelectNodeTo returns the resultant node. If there is already a
2891/// node of the specified opcode and operands, it returns that node instead of
2892/// the current one.
Evan Cheng95514ba2006-08-26 08:00:10 +00002893SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2894 MVT::ValueType VT) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002895 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002896 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002897 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattner4a283e92006-08-11 18:38:11 +00002898 void *IP = 0;
2899 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002900 return ON;
Chris Lattner4a283e92006-08-11 18:38:11 +00002901
Chris Lattner7651fa42005-08-24 23:00:29 +00002902 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002903
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002904 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, 0, 0);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002905
Chris Lattner4a283e92006-08-11 18:38:11 +00002906 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002907 return N;
Chris Lattner7651fa42005-08-24 23:00:29 +00002908}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002909
Evan Cheng95514ba2006-08-26 08:00:10 +00002910SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2911 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002912 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002913 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002914 SDOperand Ops[] = { Op1 };
2915
Jim Laskey583bd472006-10-27 23:46:08 +00002916 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002917 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002918 void *IP = 0;
2919 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002920 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002921
Chris Lattner149c58c2005-08-16 18:17:10 +00002922 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002923 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 1);
Chris Lattnera5682852006-08-07 23:03:03 +00002924 CSEMap.InsertNode(N, IP);
Evan Cheng95514ba2006-08-26 08:00:10 +00002925 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002926}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002927
Evan Cheng95514ba2006-08-26 08:00:10 +00002928SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2929 MVT::ValueType VT, SDOperand Op1,
2930 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002931 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002932 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002933 SDOperand Ops[] = { Op1, Op2 };
2934
Jim Laskey583bd472006-10-27 23:46:08 +00002935 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002936 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002937 void *IP = 0;
2938 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002939 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002940
Chris Lattner149c58c2005-08-16 18:17:10 +00002941 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002942
Chris Lattner63e3f142007-02-04 07:28:00 +00002943 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002944
Chris Lattnera5682852006-08-07 23:03:03 +00002945 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002946 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002947}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002948
Evan Cheng95514ba2006-08-26 08:00:10 +00002949SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2950 MVT::ValueType VT, SDOperand Op1,
2951 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002952 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002953 SDVTList VTs = getVTList(VT);
Chris Lattner63e3f142007-02-04 07:28:00 +00002954 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00002955 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002956 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00002957 void *IP = 0;
2958 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002959 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002960
Chris Lattner149c58c2005-08-16 18:17:10 +00002961 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00002962
Chris Lattner63e3f142007-02-04 07:28:00 +00002963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002964
Chris Lattnera5682852006-08-07 23:03:03 +00002965 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002966 return N;
Chris Lattner149c58c2005-08-16 18:17:10 +00002967}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002968
Evan Cheng95514ba2006-08-26 08:00:10 +00002969SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Evan Cheng694481e2006-08-27 08:08:54 +00002970 MVT::ValueType VT, const SDOperand *Ops,
2971 unsigned NumOps) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002972 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00002973 SDVTList VTs = getVTList(VT);
Jim Laskey583bd472006-10-27 23:46:08 +00002974 FoldingSetNodeID ID;
2975 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Chris Lattnera5682852006-08-07 23:03:03 +00002976 void *IP = 0;
2977 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002978 return ON;
Chris Lattnera5682852006-08-07 23:03:03 +00002979
Chris Lattner6b09a292005-08-21 18:49:33 +00002980 RemoveNodeFromCSEMaps(N);
Chris Lattnerd429bcd2007-02-04 02:49:29 +00002981 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, NumOps);
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002982
Chris Lattnera5682852006-08-07 23:03:03 +00002983 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00002984 return N;
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002985}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002986
Evan Cheng95514ba2006-08-26 08:00:10 +00002987SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2988 MVT::ValueType VT1, MVT::ValueType VT2,
2989 SDOperand Op1, SDOperand Op2) {
Chris Lattner0b3e5252006-08-15 19:11:05 +00002990 SDVTList VTs = getVTList(VT1, VT2);
Jim Laskey583bd472006-10-27 23:46:08 +00002991 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00002992 SDOperand Ops[] = { Op1, Op2 };
2993 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00002994 void *IP = 0;
2995 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00002996 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002997
Chris Lattner0fb094f2005-11-19 01:44:53 +00002998 RemoveNodeFromCSEMaps(N);
Chris Lattner63e3f142007-02-04 07:28:00 +00002999 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 2);
Chris Lattnera5682852006-08-07 23:03:03 +00003000 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003001 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003002}
3003
Evan Cheng95514ba2006-08-26 08:00:10 +00003004SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
3005 MVT::ValueType VT1, MVT::ValueType VT2,
3006 SDOperand Op1, SDOperand Op2,
3007 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003008 // If an identical node already exists, use it.
Chris Lattner0b3e5252006-08-15 19:11:05 +00003009 SDVTList VTs = getVTList(VT1, VT2);
Chris Lattner63e3f142007-02-04 07:28:00 +00003010 SDOperand Ops[] = { Op1, Op2, Op3 };
Jim Laskey583bd472006-10-27 23:46:08 +00003011 FoldingSetNodeID ID;
Chris Lattner63e3f142007-02-04 07:28:00 +00003012 AddNodeIDNode(ID, ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003013 void *IP = 0;
3014 if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
Evan Cheng95514ba2006-08-26 08:00:10 +00003015 return ON;
Chris Lattnerc5e6c642005-12-01 18:00:57 +00003016
Chris Lattner0fb094f2005-11-19 01:44:53 +00003017 RemoveNodeFromCSEMaps(N);
Chris Lattner67612a12007-02-04 02:32:44 +00003018
Chris Lattner63e3f142007-02-04 07:28:00 +00003019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc, VTs, Ops, 3);
Chris Lattnera5682852006-08-07 23:03:03 +00003020 CSEMap.InsertNode(N, IP); // Memoize the new node.
Evan Cheng95514ba2006-08-26 08:00:10 +00003021 return N;
Chris Lattner0fb094f2005-11-19 01:44:53 +00003022}
3023
Chris Lattner0fb094f2005-11-19 01:44:53 +00003024
Evan Cheng6ae46c42006-02-09 07:15:23 +00003025/// getTargetNode - These are used for target selectors to create a new node
3026/// with specified return type(s), target opcode, and operands.
3027///
3028/// Note that getTargetNode returns the resultant node. If there is already a
3029/// node of the specified opcode and operands, it returns that node instead of
3030/// the current one.
3031SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
3032 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
3033}
3034SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3035 SDOperand Op1) {
3036 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
3037}
3038SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
3039 SDOperand Op1, SDOperand Op2) {
3040 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
3041}
3042SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerfea997a2007-02-01 04:55:59 +00003043 SDOperand Op1, SDOperand Op2,
3044 SDOperand Op3) {
Evan Cheng6ae46c42006-02-09 07:15:23 +00003045 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
3046}
3047SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003048 const SDOperand *Ops, unsigned NumOps) {
3049 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003050}
3051SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003052 MVT::ValueType VT2) {
3053 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
3054 SDOperand Op;
3055 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op, 0).Val;
3056}
3057SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng6ae46c42006-02-09 07:15:23 +00003058 MVT::ValueType VT2, SDOperand Op1) {
Chris Lattner70046e92006-08-15 17:46:01 +00003059 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003060 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, &Op1, 1).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003061}
3062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003063 MVT::ValueType VT2, SDOperand Op1,
3064 SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003065 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003066 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003067 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003068}
3069SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Chris Lattnera5682852006-08-07 23:03:03 +00003070 MVT::ValueType VT2, SDOperand Op1,
3071 SDOperand Op2, SDOperand Op3) {
Chris Lattner70046e92006-08-15 17:46:01 +00003072 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003073 SDOperand Ops[] = { Op1, Op2, Op3 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003074 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, 3).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003075}
Evan Cheng694481e2006-08-27 08:08:54 +00003076SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3077 MVT::ValueType VT2,
3078 const SDOperand *Ops, unsigned NumOps) {
Chris Lattner70046e92006-08-15 17:46:01 +00003079 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2);
Evan Cheng694481e2006-08-27 08:08:54 +00003080 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 2, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003081}
3082SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3083 MVT::ValueType VT2, MVT::ValueType VT3,
3084 SDOperand Op1, SDOperand Op2) {
Chris Lattner70046e92006-08-15 17:46:01 +00003085 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003086 SDOperand Ops[] = { Op1, Op2 };
Chris Lattner2fa6d3b2006-08-14 23:31:51 +00003087 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 2).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003088}
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003089SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3090 MVT::ValueType VT2, MVT::ValueType VT3,
3091 SDOperand Op1, SDOperand Op2,
3092 SDOperand Op3) {
3093 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3094 SDOperand Ops[] = { Op1, Op2, Op3 };
3095 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, 3).Val;
3096}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003097SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
Evan Cheng694481e2006-08-27 08:08:54 +00003098 MVT::ValueType VT2, MVT::ValueType VT3,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003099 const SDOperand *Ops, unsigned NumOps) {
Evan Cheng694481e2006-08-27 08:08:54 +00003100 const MVT::ValueType *VTs = getNodeValueTypes(VT1, VT2, VT3);
3101 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 3, Ops, NumOps).Val;
Evan Cheng6ae46c42006-02-09 07:15:23 +00003102}
Evan Cheng05e69c12007-09-12 23:39:49 +00003103SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
3104 MVT::ValueType VT2, MVT::ValueType VT3,
3105 MVT::ValueType VT4,
3106 const SDOperand *Ops, unsigned NumOps) {
3107 std::vector<MVT::ValueType> VTList;
3108 VTList.push_back(VT1);
3109 VTList.push_back(VT2);
3110 VTList.push_back(VT3);
3111 VTList.push_back(VT4);
3112 const MVT::ValueType *VTs = getNodeValueTypes(VTList);
3113 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, 4, Ops, NumOps).Val;
3114}
Evan Cheng39305cf2007-10-05 01:10:49 +00003115SDNode *SelectionDAG::getTargetNode(unsigned Opcode,
3116 std::vector<MVT::ValueType> &ResultTys,
3117 const SDOperand *Ops, unsigned NumOps) {
3118 const MVT::ValueType *VTs = getNodeValueTypes(ResultTys);
3119 return getNode(ISD::BUILTIN_OP_END+Opcode, VTs, ResultTys.size(),
3120 Ops, NumOps).Val;
3121}
Evan Cheng6ae46c42006-02-09 07:15:23 +00003122
Evan Cheng99157a02006-08-07 22:13:29 +00003123/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00003124/// This can cause recursive merging of nodes in the DAG.
3125///
Chris Lattner8b52f212005-08-26 18:36:28 +00003126/// This version assumes From/To have a single result value.
3127///
Chris Lattner1e111c72005-09-07 05:37:01 +00003128void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
3129 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003130 SDNode *From = FromN.Val, *To = ToN.Val;
3131 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
3132 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00003133 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00003134
Chris Lattner8b8749f2005-08-17 19:00:20 +00003135 while (!From->use_empty()) {
3136 // Process users until they are all gone.
3137 SDNode *U = *From->use_begin();
3138
3139 // This node is about to morph, remove its old self from the CSE maps.
3140 RemoveNodeFromCSEMaps(U);
3141
Chris Lattner65113b22005-11-08 22:07:03 +00003142 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3143 I != E; ++I)
3144 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003145 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003146 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00003147 To->addUser(U);
3148 }
3149
3150 // Now that we have modified U, add it back to the CSE maps. If it already
3151 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003152 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3153 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003154 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00003155 if (Deleted) Deleted->push_back(U);
3156 DeleteNodeNotInCSEMaps(U);
3157 }
Chris Lattner8b52f212005-08-26 18:36:28 +00003158 }
3159}
3160
3161/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3162/// This can cause recursive merging of nodes in the DAG.
3163///
3164/// This version assumes From/To have matching types and numbers of result
3165/// values.
3166///
Chris Lattner1e111c72005-09-07 05:37:01 +00003167void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
3168 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00003169 assert(From != To && "Cannot replace uses of with self");
3170 assert(From->getNumValues() == To->getNumValues() &&
3171 "Cannot use this version of ReplaceAllUsesWith!");
3172 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00003173 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00003174 return;
3175 }
3176
3177 while (!From->use_empty()) {
3178 // Process users until they are all gone.
3179 SDNode *U = *From->use_begin();
3180
3181 // This node is about to morph, remove its old self from the CSE maps.
3182 RemoveNodeFromCSEMaps(U);
3183
Chris Lattner65113b22005-11-08 22:07:03 +00003184 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3185 I != E; ++I)
3186 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00003187 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003188 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00003189 To->addUser(U);
3190 }
3191
3192 // Now that we have modified U, add it back to the CSE maps. If it already
3193 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003194 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3195 ReplaceAllUsesWith(U, Existing, Deleted);
3196 // U is now dead.
3197 if (Deleted) Deleted->push_back(U);
3198 DeleteNodeNotInCSEMaps(U);
3199 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00003200 }
3201}
3202
Chris Lattner8b52f212005-08-26 18:36:28 +00003203/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
3204/// This can cause recursive merging of nodes in the DAG.
3205///
3206/// This version can replace From with any result values. To must match the
3207/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00003208void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003209 const SDOperand *To,
Chris Lattner1e111c72005-09-07 05:37:01 +00003210 std::vector<SDNode*> *Deleted) {
Chris Lattnerb9ea4a32006-08-11 17:46:28 +00003211 if (From->getNumValues() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00003212 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00003213 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00003214 return;
3215 }
3216
3217 while (!From->use_empty()) {
3218 // Process users until they are all gone.
3219 SDNode *U = *From->use_begin();
3220
3221 // This node is about to morph, remove its old self from the CSE maps.
3222 RemoveNodeFromCSEMaps(U);
3223
Chris Lattner65113b22005-11-08 22:07:03 +00003224 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
3225 I != E; ++I)
3226 if (I->Val == From) {
3227 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00003228 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00003229 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00003230 ToOp.Val->addUser(U);
3231 }
3232
3233 // Now that we have modified U, add it back to the CSE maps. If it already
3234 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00003235 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
3236 ReplaceAllUsesWith(U, Existing, Deleted);
3237 // U is now dead.
3238 if (Deleted) Deleted->push_back(U);
3239 DeleteNodeNotInCSEMaps(U);
3240 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00003241 }
3242}
3243
Chris Lattner012f2412006-02-17 21:58:01 +00003244/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
3245/// uses of other values produced by From.Val alone. The Deleted vector is
3246/// handled the same was as for ReplaceAllUsesWith.
3247void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
Chris Lattner01d029b2007-10-15 06:10:22 +00003248 std::vector<SDNode*> *Deleted) {
Chris Lattner012f2412006-02-17 21:58:01 +00003249 assert(From != To && "Cannot replace a value with itself");
3250 // Handle the simple, trivial, case efficiently.
3251 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003252 ReplaceAllUsesWith(From, To, Deleted);
Chris Lattner012f2412006-02-17 21:58:01 +00003253 return;
3254 }
3255
Chris Lattnercf5640b2007-02-04 00:14:31 +00003256 // Get all of the users of From.Val. We want these in a nice,
3257 // deterministically ordered and uniqued set, so we use a SmallSetVector.
3258 SmallSetVector<SDNode*, 16> Users(From.Val->use_begin(), From.Val->use_end());
Chris Lattner012f2412006-02-17 21:58:01 +00003259
Chris Lattner01d029b2007-10-15 06:10:22 +00003260 std::vector<SDNode*> LocalDeletionVector;
3261
3262 // Pick a deletion vector to use. If the user specified one, use theirs,
3263 // otherwise use a local one.
3264 std::vector<SDNode*> *DeleteVector = Deleted ? Deleted : &LocalDeletionVector;
Chris Lattner012f2412006-02-17 21:58:01 +00003265 while (!Users.empty()) {
3266 // We know that this user uses some value of From. If it is the right
3267 // value, update it.
3268 SDNode *User = Users.back();
3269 Users.pop_back();
3270
Chris Lattner01d029b2007-10-15 06:10:22 +00003271 // Scan for an operand that matches From.
3272 SDOperand *Op = User->OperandList, *E = User->OperandList+User->NumOperands;
3273 for (; Op != E; ++Op)
3274 if (*Op == From) break;
3275
3276 // If there are no matches, the user must use some other result of From.
3277 if (Op == E) continue;
3278
3279 // Okay, we know this user needs to be updated. Remove its old self
3280 // from the CSE maps.
3281 RemoveNodeFromCSEMaps(User);
3282
3283 // Update all operands that match "From".
3284 for (; Op != E; ++Op) {
Chris Lattner012f2412006-02-17 21:58:01 +00003285 if (*Op == From) {
Chris Lattner01d029b2007-10-15 06:10:22 +00003286 From.Val->removeUser(User);
3287 *Op = To;
3288 To.Val->addUser(User);
Chris Lattner012f2412006-02-17 21:58:01 +00003289 }
3290 }
Chris Lattner01d029b2007-10-15 06:10:22 +00003291
3292 // Now that we have modified User, add it back to the CSE maps. If it
3293 // already exists there, recursively merge the results together.
3294 SDNode *Existing = AddNonLeafNodeToCSEMaps(User);
3295 if (!Existing) continue; // Continue on to next user.
3296
3297 // If there was already an existing matching node, use ReplaceAllUsesWith
3298 // to replace the dead one with the existing one. However, this can cause
3299 // recursive merging of other unrelated nodes down the line. The merging
3300 // can cause deletion of nodes that used the old value. In this case,
3301 // we have to be certain to remove them from the Users set.
3302 unsigned NumDeleted = DeleteVector->size();
3303 ReplaceAllUsesWith(User, Existing, DeleteVector);
3304
3305 // User is now dead.
3306 DeleteVector->push_back(User);
3307 DeleteNodeNotInCSEMaps(User);
3308
3309 // We have to be careful here, because ReplaceAllUsesWith could have
3310 // deleted a user of From, which means there may be dangling pointers
3311 // in the "Users" setvector. Scan over the deleted node pointers and
3312 // remove them from the setvector.
3313 for (unsigned i = NumDeleted, e = DeleteVector->size(); i != e; ++i)
3314 Users.remove((*DeleteVector)[i]);
3315
3316 // If the user doesn't need the set of deleted elements, don't retain them
3317 // to the next loop iteration.
3318 if (Deleted == 0)
3319 LocalDeletionVector.clear();
Chris Lattner012f2412006-02-17 21:58:01 +00003320 }
3321}
3322
Chris Lattner7b2880c2005-08-24 22:44:39 +00003323
Evan Chenge6f35d82006-08-01 08:20:41 +00003324/// AssignNodeIds - Assign a unique node id for each node in the DAG based on
3325/// their allnodes order. It returns the maximum id.
Evan Cheng7c16d772006-07-27 07:36:47 +00003326unsigned SelectionDAG::AssignNodeIds() {
3327 unsigned Id = 0;
Evan Cheng091cba12006-07-27 06:39:06 +00003328 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I){
3329 SDNode *N = I;
3330 N->setNodeId(Id++);
3331 }
3332 return Id;
3333}
3334
Evan Chenge6f35d82006-08-01 08:20:41 +00003335/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
Evan Chengc384d6c2006-08-02 22:00:34 +00003336/// based on their topological order. It returns the maximum id and a vector
3337/// of the SDNodes* in assigned order by reference.
3338unsigned SelectionDAG::AssignTopologicalOrder(std::vector<SDNode*> &TopOrder) {
Evan Chenge6f35d82006-08-01 08:20:41 +00003339 unsigned DAGSize = AllNodes.size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003340 std::vector<unsigned> InDegree(DAGSize);
3341 std::vector<SDNode*> Sources;
3342
3343 // Use a two pass approach to avoid using a std::map which is slow.
3344 unsigned Id = 0;
Evan Chenge6f35d82006-08-01 08:20:41 +00003345 for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I){
3346 SDNode *N = I;
Evan Chengc384d6c2006-08-02 22:00:34 +00003347 N->setNodeId(Id++);
Evan Chenge6f35d82006-08-01 08:20:41 +00003348 unsigned Degree = N->use_size();
Evan Chengc384d6c2006-08-02 22:00:34 +00003349 InDegree[N->getNodeId()] = Degree;
Evan Chenge6f35d82006-08-01 08:20:41 +00003350 if (Degree == 0)
Evan Chengc384d6c2006-08-02 22:00:34 +00003351 Sources.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003352 }
3353
Evan Cheng99157a02006-08-07 22:13:29 +00003354 TopOrder.clear();
Evan Chenge6f35d82006-08-01 08:20:41 +00003355 while (!Sources.empty()) {
Evan Chengc384d6c2006-08-02 22:00:34 +00003356 SDNode *N = Sources.back();
3357 Sources.pop_back();
Evan Chenge6f35d82006-08-01 08:20:41 +00003358 TopOrder.push_back(N);
Evan Chenge6f35d82006-08-01 08:20:41 +00003359 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
3360 SDNode *P = I->Val;
Evan Chengc384d6c2006-08-02 22:00:34 +00003361 unsigned Degree = --InDegree[P->getNodeId()];
Evan Chenge6f35d82006-08-01 08:20:41 +00003362 if (Degree == 0)
3363 Sources.push_back(P);
Evan Chenge6f35d82006-08-01 08:20:41 +00003364 }
3365 }
3366
Evan Chengc384d6c2006-08-02 22:00:34 +00003367 // Second pass, assign the actual topological order as node ids.
3368 Id = 0;
3369 for (std::vector<SDNode*>::iterator TI = TopOrder.begin(),TE = TopOrder.end();
3370 TI != TE; ++TI)
3371 (*TI)->setNodeId(Id++);
3372
3373 return Id;
Evan Chenge6f35d82006-08-01 08:20:41 +00003374}
3375
3376
Evan Cheng091cba12006-07-27 06:39:06 +00003377
Jim Laskey58b968b2005-08-17 20:08:02 +00003378//===----------------------------------------------------------------------===//
3379// SDNode Class
3380//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00003381
Chris Lattner917d2c92006-07-19 00:00:37 +00003382// Out-of-line virtual method to give class a home.
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003383void SDNode::ANCHOR() {}
Chris Lattner3f97eb42007-02-04 08:35:21 +00003384void UnarySDNode::ANCHOR() {}
3385void BinarySDNode::ANCHOR() {}
3386void TernarySDNode::ANCHOR() {}
Chris Lattnerc76e3c82007-02-04 02:23:32 +00003387void HandleSDNode::ANCHOR() {}
3388void StringSDNode::ANCHOR() {}
3389void ConstantSDNode::ANCHOR() {}
3390void ConstantFPSDNode::ANCHOR() {}
3391void GlobalAddressSDNode::ANCHOR() {}
3392void FrameIndexSDNode::ANCHOR() {}
3393void JumpTableSDNode::ANCHOR() {}
3394void ConstantPoolSDNode::ANCHOR() {}
3395void BasicBlockSDNode::ANCHOR() {}
3396void SrcValueSDNode::ANCHOR() {}
3397void RegisterSDNode::ANCHOR() {}
3398void ExternalSymbolSDNode::ANCHOR() {}
3399void CondCodeSDNode::ANCHOR() {}
3400void VTSDNode::ANCHOR() {}
3401void LoadSDNode::ANCHOR() {}
3402void StoreSDNode::ANCHOR() {}
Chris Lattner917d2c92006-07-19 00:00:37 +00003403
Chris Lattner48b85922007-02-04 02:41:42 +00003404HandleSDNode::~HandleSDNode() {
3405 SDVTList VTs = { 0, 0 };
Chris Lattnerd429bcd2007-02-04 02:49:29 +00003406 MorphNodeTo(ISD::HANDLENODE, VTs, 0, 0); // Drops operand uses.
Chris Lattner48b85922007-02-04 02:41:42 +00003407}
3408
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003409GlobalAddressSDNode::GlobalAddressSDNode(bool isTarget, const GlobalValue *GA,
3410 MVT::ValueType VT, int o)
3411 : SDNode(isa<GlobalVariable>(GA) &&
Dan Gohman275769a2007-07-23 20:24:29 +00003412 cast<GlobalVariable>(GA)->isThreadLocal() ?
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003413 // Thread Local
3414 (isTarget ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress) :
3415 // Non Thread Local
3416 (isTarget ? ISD::TargetGlobalAddress : ISD::GlobalAddress),
3417 getSDVTList(VT)), Offset(o) {
3418 TheGlobal = const_cast<GlobalValue*>(GA);
3419}
Chris Lattner48b85922007-02-04 02:41:42 +00003420
Jim Laskey583bd472006-10-27 23:46:08 +00003421/// Profile - Gather unique data for the node.
3422///
3423void SDNode::Profile(FoldingSetNodeID &ID) {
3424 AddNodeIDNode(ID, this);
3425}
3426
Chris Lattnera3255112005-11-08 23:30:28 +00003427/// getValueTypeList - Return a pointer to the specified value type.
3428///
3429MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
3430 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
3431 VTs[VT] = VT;
3432 return &VTs[VT];
3433}
Chris Lattnera5682852006-08-07 23:03:03 +00003434
Chris Lattner5c884562005-01-12 18:37:47 +00003435/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
3436/// indicated value. This method ignores uses of other values defined by this
3437/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00003438bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00003439 assert(Value < getNumValues() && "Bad value!");
3440
3441 // If there is only one value, this is easy.
3442 if (getNumValues() == 1)
3443 return use_size() == NUses;
Evan Cheng33d55952007-08-02 05:29:38 +00003444 if (use_size() < NUses) return false;
Chris Lattner5c884562005-01-12 18:37:47 +00003445
Evan Cheng4ee62112006-02-05 06:29:23 +00003446 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00003447
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003448 SmallPtrSet<SDNode*, 32> UsersHandled;
Chris Lattner5c884562005-01-12 18:37:47 +00003449
Chris Lattnerf83482d2006-08-16 20:59:32 +00003450 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
Chris Lattner5c884562005-01-12 18:37:47 +00003451 SDNode *User = *UI;
3452 if (User->getNumOperands() == 1 ||
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003453 UsersHandled.insert(User)) // First time we've seen this?
Chris Lattner5c884562005-01-12 18:37:47 +00003454 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3455 if (User->getOperand(i) == TheValue) {
3456 if (NUses == 0)
3457 return false; // too many uses
3458 --NUses;
3459 }
3460 }
3461
3462 // Found exactly the right number of uses?
3463 return NUses == 0;
3464}
3465
3466
Evan Cheng33d55952007-08-02 05:29:38 +00003467/// hasAnyUseOfValue - Return true if there are any use of the indicated
3468/// value. This method ignores uses of other values defined by this operation.
3469bool SDNode::hasAnyUseOfValue(unsigned Value) const {
3470 assert(Value < getNumValues() && "Bad value!");
3471
3472 if (use_size() == 0) return false;
3473
3474 SDOperand TheValue(const_cast<SDNode *>(this), Value);
3475
3476 SmallPtrSet<SDNode*, 32> UsersHandled;
3477
3478 for (SDNode::use_iterator UI = Uses.begin(), E = Uses.end(); UI != E; ++UI) {
3479 SDNode *User = *UI;
3480 if (User->getNumOperands() == 1 ||
3481 UsersHandled.insert(User)) // First time we've seen this?
3482 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
3483 if (User->getOperand(i) == TheValue) {
3484 return true;
3485 }
3486 }
3487
3488 return false;
3489}
3490
3491
Evan Chenge6e97e62006-11-03 07:31:32 +00003492/// isOnlyUse - Return true if this node is the only use of N.
3493///
Evan Cheng4ee62112006-02-05 06:29:23 +00003494bool SDNode::isOnlyUse(SDNode *N) const {
3495 bool Seen = false;
3496 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
3497 SDNode *User = *I;
3498 if (User == this)
3499 Seen = true;
3500 else
3501 return false;
3502 }
3503
3504 return Seen;
3505}
3506
Evan Chenge6e97e62006-11-03 07:31:32 +00003507/// isOperand - Return true if this node is an operand of N.
3508///
Evan Chengbfa284f2006-03-03 06:42:32 +00003509bool SDOperand::isOperand(SDNode *N) const {
3510 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3511 if (*this == N->getOperand(i))
3512 return true;
3513 return false;
3514}
3515
Evan Cheng80d8eaa2006-03-03 06:24:54 +00003516bool SDNode::isOperand(SDNode *N) const {
3517 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
3518 if (this == N->OperandList[i].Val)
3519 return true;
3520 return false;
3521}
Evan Cheng4ee62112006-02-05 06:29:23 +00003522
Evan Chengc5fc57d2006-11-03 03:05:24 +00003523static void findPredecessor(SDNode *N, const SDNode *P, bool &found,
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003524 SmallPtrSet<SDNode *, 32> &Visited) {
3525 if (found || !Visited.insert(N))
Evan Chengc5fc57d2006-11-03 03:05:24 +00003526 return;
3527
3528 for (unsigned i = 0, e = N->getNumOperands(); !found && i != e; ++i) {
3529 SDNode *Op = N->getOperand(i).Val;
3530 if (Op == P) {
3531 found = true;
3532 return;
3533 }
3534 findPredecessor(Op, P, found, Visited);
3535 }
3536}
3537
Evan Chenge6e97e62006-11-03 07:31:32 +00003538/// isPredecessor - Return true if this node is a predecessor of N. This node
3539/// is either an operand of N or it can be reached by recursively traversing
3540/// up the operands.
3541/// NOTE: this is an expensive method. Use it carefully.
Evan Chengc5fc57d2006-11-03 03:05:24 +00003542bool SDNode::isPredecessor(SDNode *N) const {
Chris Lattnerd48c5e82007-02-04 00:24:41 +00003543 SmallPtrSet<SDNode *, 32> Visited;
Evan Chengc5fc57d2006-11-03 03:05:24 +00003544 bool found = false;
3545 findPredecessor(N, this, found, Visited);
3546 return found;
3547}
3548
Evan Chengc5484282006-10-04 00:56:09 +00003549uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
3550 assert(Num < NumOperands && "Invalid child # of SDNode!");
3551 return cast<ConstantSDNode>(OperandList[Num])->getValue();
3552}
3553
Reid Spencer577cc322007-04-01 07:32:19 +00003554std::string SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003555 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003556 default:
3557 if (getOpcode() < ISD::BUILTIN_OP_END)
3558 return "<<Unknown DAG Node>>";
3559 else {
Evan Cheng72261582005-12-20 06:22:03 +00003560 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003561 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00003562 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
3563 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00003564
Evan Cheng72261582005-12-20 06:22:03 +00003565 TargetLowering &TLI = G->getTargetLoweringInfo();
3566 const char *Name =
3567 TLI.getTargetNodeName(getOpcode());
3568 if (Name) return Name;
3569 }
3570
3571 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003572 }
3573
Andrew Lenharth95762122005-03-31 21:24:06 +00003574 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00003575 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003576 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003577 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00003578 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00003579 case ISD::AssertSext: return "AssertSext";
3580 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003581
3582 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003583 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003584 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003585 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003586
3587 case ISD::Constant: return "Constant";
3588 case ISD::ConstantFP: return "ConstantFP";
3589 case ISD::GlobalAddress: return "GlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003590 case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003591 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003592 case ISD::JumpTable: return "JumpTable";
Andrew Lenharth82c3d8f2006-10-11 04:29:42 +00003593 case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
Nate Begemanbcc5f362007-01-29 22:58:52 +00003594 case ISD::RETURNADDR: return "RETURNADDR";
3595 case ISD::FRAMEADDR: return "FRAMEADDR";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003596 case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
Jim Laskeyb180aa12007-02-21 22:53:45 +00003597 case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
3598 case ISD::EHSELECTION: return "EHSELECTION";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00003599 case ISD::EH_RETURN: return "EH_RETURN";
Chris Lattner5839bf22005-08-26 17:15:30 +00003600 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003601 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00003602 case ISD::INTRINSIC_WO_CHAIN: {
3603 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
3604 return Intrinsic::getName((Intrinsic::ID)IID);
3605 }
3606 case ISD::INTRINSIC_VOID:
3607 case ISD::INTRINSIC_W_CHAIN: {
3608 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00003609 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00003610 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00003611
Chris Lattnerb2827b02006-03-19 00:52:58 +00003612 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003613 case ISD::TargetConstant: return "TargetConstant";
3614 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003615 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003616 case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003617 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00003618 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00003619 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003620 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00003621
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003622 case ISD::CopyToReg: return "CopyToReg";
3623 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003624 case ISD::UNDEF: return "undef";
Dan Gohman50b15332007-07-05 20:15:43 +00003625 case ISD::MERGE_VALUES: return "merge_values";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003626 case ISD::INLINEASM: return "inlineasm";
Jim Laskey1ee29252007-01-26 14:34:52 +00003627 case ISD::LABEL: return "label";
Evan Cheng9fda2f92006-02-03 01:33:01 +00003628 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00003629 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerf4ec8172006-05-16 22:53:20 +00003630 case ISD::CALL: return "call";
Chris Lattnerce7518c2006-01-26 22:24:51 +00003631
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003632 // Unary operators
3633 case ISD::FABS: return "fabs";
3634 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00003635 case ISD::FSQRT: return "fsqrt";
3636 case ISD::FSIN: return "fsin";
3637 case ISD::FCOS: return "fcos";
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003638 case ISD::FPOWI: return "fpowi";
Dan Gohman07f04fd2007-10-11 23:06:37 +00003639 case ISD::FPOW: return "fpow";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00003640
3641 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003642 case ISD::ADD: return "add";
3643 case ISD::SUB: return "sub";
3644 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00003645 case ISD::MULHU: return "mulhu";
3646 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003647 case ISD::SDIV: return "sdiv";
3648 case ISD::UDIV: return "udiv";
3649 case ISD::SREM: return "srem";
3650 case ISD::UREM: return "urem";
Dan Gohmanccd60792007-10-05 14:11:04 +00003651 case ISD::SMUL_LOHI: return "smul_lohi";
3652 case ISD::UMUL_LOHI: return "umul_lohi";
3653 case ISD::SDIVREM: return "sdivrem";
3654 case ISD::UDIVREM: return "divrem";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003655 case ISD::AND: return "and";
3656 case ISD::OR: return "or";
3657 case ISD::XOR: return "xor";
3658 case ISD::SHL: return "shl";
3659 case ISD::SRA: return "sra";
3660 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00003661 case ISD::ROTL: return "rotl";
3662 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00003663 case ISD::FADD: return "fadd";
3664 case ISD::FSUB: return "fsub";
3665 case ISD::FMUL: return "fmul";
3666 case ISD::FDIV: return "fdiv";
3667 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00003668 case ISD::FCOPYSIGN: return "fcopysign";
Chris Lattner0c486bd2006-03-17 19:53:59 +00003669
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003670 case ISD::SETCC: return "setcc";
3671 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00003672 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003673 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003674 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Dan Gohman7f321562007-06-25 16:23:39 +00003675 case ISD::CONCAT_VECTORS: return "concat_vectors";
3676 case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003677 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00003678 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnerb6541762007-03-04 20:40:38 +00003679 case ISD::CARRY_FALSE: return "carry_false";
Nate Begeman551bf3f2006-02-17 05:43:56 +00003680 case ISD::ADDC: return "addc";
3681 case ISD::ADDE: return "adde";
3682 case ISD::SUBC: return "subc";
3683 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00003684 case ISD::SHL_PARTS: return "shl_parts";
3685 case ISD::SRA_PARTS: return "sra_parts";
3686 case ISD::SRL_PARTS: return "srl_parts";
Christopher Lamb557c3632007-07-26 07:34:40 +00003687
3688 case ISD::EXTRACT_SUBREG: return "extract_subreg";
3689 case ISD::INSERT_SUBREG: return "insert_subreg";
3690
Chris Lattner7f644642005-04-28 21:44:03 +00003691 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003692 case ISD::SIGN_EXTEND: return "sign_extend";
3693 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00003694 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00003695 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003696 case ISD::TRUNCATE: return "truncate";
3697 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00003698 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003699 case ISD::FP_EXTEND: return "fp_extend";
3700
3701 case ISD::SINT_TO_FP: return "sint_to_fp";
3702 case ISD::UINT_TO_FP: return "uint_to_fp";
3703 case ISD::FP_TO_SINT: return "fp_to_sint";
3704 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00003705 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003706
3707 // Control flow instructions
3708 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00003709 case ISD::BRIND: return "brind";
Evan Chengc41cd9c2006-10-30 07:59:36 +00003710 case ISD::BR_JT: return "br_jt";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003711 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00003712 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003713 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00003714 case ISD::CALLSEQ_START: return "callseq_start";
3715 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003716
3717 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00003718 case ISD::LOAD: return "load";
3719 case ISD::STORE: return "store";
Nate Begemanacc398c2006-01-25 18:21:52 +00003720 case ISD::VAARG: return "vaarg";
3721 case ISD::VACOPY: return "vacopy";
3722 case ISD::VAEND: return "vaend";
3723 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003724 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00003725 case ISD::EXTRACT_ELEMENT: return "extract_element";
3726 case ISD::BUILD_PAIR: return "build_pair";
3727 case ISD::STACKSAVE: return "stacksave";
3728 case ISD::STACKRESTORE: return "stackrestore";
3729
3730 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00003731 case ISD::MEMSET: return "memset";
3732 case ISD::MEMCPY: return "memcpy";
3733 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003734
Nate Begeman1b5db7a2006-01-16 08:07:10 +00003735 // Bit manipulation
3736 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00003737 case ISD::CTPOP: return "ctpop";
3738 case ISD::CTTZ: return "cttz";
3739 case ISD::CTLZ: return "ctlz";
3740
Chris Lattner36ce6912005-11-29 06:21:05 +00003741 // Debug info
3742 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00003743 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00003744
Duncan Sands36397f52007-07-27 12:58:54 +00003745 // Trampolines
Duncan Sandsf7331b32007-09-11 14:10:23 +00003746 case ISD::TRAMPOLINE: return "trampoline";
Duncan Sands36397f52007-07-27 12:58:54 +00003747
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003748 case ISD::CONDCODE:
3749 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003750 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003751 case ISD::SETOEQ: return "setoeq";
3752 case ISD::SETOGT: return "setogt";
3753 case ISD::SETOGE: return "setoge";
3754 case ISD::SETOLT: return "setolt";
3755 case ISD::SETOLE: return "setole";
3756 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003757
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003758 case ISD::SETO: return "seto";
3759 case ISD::SETUO: return "setuo";
3760 case ISD::SETUEQ: return "setue";
3761 case ISD::SETUGT: return "setugt";
3762 case ISD::SETUGE: return "setuge";
3763 case ISD::SETULT: return "setult";
3764 case ISD::SETULE: return "setule";
3765 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003766
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003767 case ISD::SETEQ: return "seteq";
3768 case ISD::SETGT: return "setgt";
3769 case ISD::SETGE: return "setge";
3770 case ISD::SETLT: return "setlt";
3771 case ISD::SETLE: return "setle";
3772 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00003773 }
3774 }
3775}
Chris Lattnerc3aae252005-01-07 07:46:32 +00003776
Evan Cheng144d8f02006-11-09 17:55:04 +00003777const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
Evan Cheng2caccca2006-10-17 21:14:32 +00003778 switch (AM) {
3779 default:
3780 return "";
3781 case ISD::PRE_INC:
3782 return "<pre-inc>";
3783 case ISD::PRE_DEC:
3784 return "<pre-dec>";
3785 case ISD::POST_INC:
3786 return "<post-inc>";
3787 case ISD::POST_DEC:
3788 return "<post-dec>";
3789 }
3790}
3791
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003792void SDNode::dump() const { dump(0); }
3793void SDNode::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00003794 cerr << (void*)this << ": ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003795
3796 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003797 if (i) cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003798 if (getValueType(i) == MVT::Other)
Bill Wendling832171c2006-12-07 20:04:42 +00003799 cerr << "ch";
Chris Lattner4ea69242005-01-15 07:14:32 +00003800 else
Bill Wendling832171c2006-12-07 20:04:42 +00003801 cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003802 }
Bill Wendling832171c2006-12-07 20:04:42 +00003803 cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003804
Bill Wendling832171c2006-12-07 20:04:42 +00003805 cerr << " ";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003806 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Bill Wendling832171c2006-12-07 20:04:42 +00003807 if (i) cerr << ", ";
3808 cerr << (void*)getOperand(i).Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003809 if (unsigned RN = getOperand(i).ResNo)
Bill Wendling832171c2006-12-07 20:04:42 +00003810 cerr << ":" << RN;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003811 }
3812
3813 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003814 cerr << "<" << CSDN->getValue() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003815 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003816 if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
3817 cerr << "<" << CSDN->getValueAPF().convertToFloat() << ">";
3818 else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
3819 cerr << "<" << CSDN->getValueAPF().convertToDouble() << ">";
3820 else {
3821 cerr << "<APFloat(";
3822 CSDN->getValueAPF().convertToAPInt().dump();
3823 cerr << ")>";
3824 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00003825 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003826 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003827 int offset = GADN->getOffset();
Bill Wendling832171c2006-12-07 20:04:42 +00003828 cerr << "<";
Bill Wendlingbcd24982006-12-07 20:28:15 +00003829 WriteAsOperand(*cerr.stream(), GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003830 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003831 cerr << " + " << offset;
Evan Cheng61ca74b2005-11-30 02:04:11 +00003832 else
Bill Wendling832171c2006-12-07 20:04:42 +00003833 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003834 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003835 cerr << "<" << FIDN->getIndex() << ">";
Evan Cheng6cc31ae2006-11-01 04:48:30 +00003836 } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003837 cerr << "<" << JTDN->getIndex() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003838 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003839 int offset = CP->getOffset();
Evan Chengd6594ae2006-09-12 21:00:35 +00003840 if (CP->isMachineConstantPoolEntry())
Bill Wendling832171c2006-12-07 20:04:42 +00003841 cerr << "<" << *CP->getMachineCPVal() << ">";
Evan Chengd6594ae2006-09-12 21:00:35 +00003842 else
Bill Wendling832171c2006-12-07 20:04:42 +00003843 cerr << "<" << *CP->getConstVal() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003844 if (offset > 0)
Bill Wendling832171c2006-12-07 20:04:42 +00003845 cerr << " + " << offset;
Evan Cheng38b73272006-02-26 08:36:57 +00003846 else
Bill Wendling832171c2006-12-07 20:04:42 +00003847 cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003848 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003849 cerr << "<";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003850 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3851 if (LBB)
Bill Wendling832171c2006-12-07 20:04:42 +00003852 cerr << LBB->getName() << " ";
3853 cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003854 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003855 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Bill Wendling832171c2006-12-07 20:04:42 +00003856 cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
Chris Lattner7228aa72005-08-19 21:21:16 +00003857 } else {
Bill Wendling832171c2006-12-07 20:04:42 +00003858 cerr << " #" << R->getReg();
Chris Lattner7228aa72005-08-19 21:21:16 +00003859 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003860 } else if (const ExternalSymbolSDNode *ES =
3861 dyn_cast<ExternalSymbolSDNode>(this)) {
Bill Wendling832171c2006-12-07 20:04:42 +00003862 cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003863 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3864 if (M->getValue())
Bill Wendling832171c2006-12-07 20:04:42 +00003865 cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003866 else
Bill Wendling832171c2006-12-07 20:04:42 +00003867 cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003868 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
Dan Gohman237898a2007-05-24 14:33:05 +00003869 cerr << ":" << MVT::getValueTypeString(N->getVT());
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003870 } else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
3871 bool doExt = true;
3872 switch (LD->getExtensionType()) {
3873 default: doExt = false; break;
3874 case ISD::EXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003875 cerr << " <anyext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003876 break;
3877 case ISD::SEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003878 cerr << " <sext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003879 break;
3880 case ISD::ZEXTLOAD:
Bill Wendling832171c2006-12-07 20:04:42 +00003881 cerr << " <zext ";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003882 break;
3883 }
3884 if (doExt)
Bill Wendling832171c2006-12-07 20:04:42 +00003885 cerr << MVT::getValueTypeString(LD->getLoadedVT()) << ">";
Evan Cheng0ac1c6a2006-10-10 20:05:10 +00003886
Evan Cheng144d8f02006-11-09 17:55:04 +00003887 const char *AM = getIndexedModeName(LD->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003888 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003889 cerr << " " << AM;
Evan Cheng2caccca2006-10-17 21:14:32 +00003890 } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
3891 if (ST->isTruncatingStore())
Bill Wendling832171c2006-12-07 20:04:42 +00003892 cerr << " <trunc "
3893 << MVT::getValueTypeString(ST->getStoredVT()) << ">";
Evan Cheng2caccca2006-10-17 21:14:32 +00003894
Evan Cheng144d8f02006-11-09 17:55:04 +00003895 const char *AM = getIndexedModeName(ST->getAddressingMode());
Duncan Sands70d0bd12007-07-19 07:31:58 +00003896 if (*AM)
Bill Wendling832171c2006-12-07 20:04:42 +00003897 cerr << " " << AM;
Chris Lattnerc3aae252005-01-07 07:46:32 +00003898 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003899}
3900
Chris Lattnerde202b32005-11-09 23:47:37 +00003901static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003902 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3903 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003904 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003905 else
Bill Wendling832171c2006-12-07 20:04:42 +00003906 cerr << "\n" << std::string(indent+2, ' ')
3907 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003908
Chris Lattnerea946cd2005-01-09 20:38:33 +00003909
Bill Wendling832171c2006-12-07 20:04:42 +00003910 cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003911 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003912}
3913
Chris Lattnerc3aae252005-01-07 07:46:32 +00003914void SelectionDAG::dump() const {
Bill Wendling832171c2006-12-07 20:04:42 +00003915 cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003916 std::vector<const SDNode*> Nodes;
3917 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3918 I != E; ++I)
3919 Nodes.push_back(I);
3920
Chris Lattner49d24712005-01-09 20:26:36 +00003921 std::sort(Nodes.begin(), Nodes.end());
3922
3923 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003924 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003925 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003926 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003927
Jim Laskey26f7fa72006-10-17 19:33:52 +00003928 if (getRoot().Val) DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003929
Bill Wendling832171c2006-12-07 20:04:42 +00003930 cerr << "\n\n";
Chris Lattnerc3aae252005-01-07 07:46:32 +00003931}
3932
Evan Chengd6594ae2006-09-12 21:00:35 +00003933const Type *ConstantPoolSDNode::getType() const {
3934 if (isMachineConstantPoolEntry())
3935 return Val.MachineCPVal->getType();
3936 return Val.ConstVal->getType();
3937}