blob: c0cd64670117b263e1a28e6ebbbd264dece1e95e [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"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000024#include "llvm/ADT/SetVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000025#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000027#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000028#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000029#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner5cdcc582005-01-09 20:52:51 +000032static bool isCommutativeBinOp(unsigned Opcode) {
33 switch (Opcode) {
34 case ISD::ADD:
35 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000036 case ISD::MULHU:
37 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000038 case ISD::FADD:
39 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000040 case ISD::AND:
41 case ISD::OR:
42 case ISD::XOR: return true;
43 default: return false; // FIXME: Need commutative info for user ops!
44 }
45}
46
Chris Lattner5cdcc582005-01-09 20:52:51 +000047// isInvertibleForFree - Return true if there is no cost to emitting the logical
48// inverse of this node.
49static bool isInvertibleForFree(SDOperand N) {
50 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000051 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000052 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000053 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000054}
55
Jim Laskey58b968b2005-08-17 20:08:02 +000056//===----------------------------------------------------------------------===//
57// ConstantFPSDNode Class
58//===----------------------------------------------------------------------===//
59
60/// isExactlyValue - We don't rely on operator== working on double values, as
61/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
62/// As such, this method can be used to do an exact bit-for-bit comparison of
63/// two floating point values.
64bool ConstantFPSDNode::isExactlyValue(double V) const {
65 return DoubleToBits(V) == DoubleToBits(Value);
66}
67
68//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000069// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000070//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000071
Chris Lattner61d43992006-03-25 22:57:01 +000072/// isBuildVectorAllOnesInteger - Return true if the specified node is a
73/// BUILD_VECTOR where all of the elements are ~0 or undef.
74bool ISD::isBuildVectorAllOnesInteger(const SDNode *N) {
75 if (N->getOpcode() != ISD::BUILD_VECTOR ||
76 !MVT::isInteger(N->getOperand(0).getValueType())) return false;
77
78 unsigned i = 0, e = N->getNumOperands();
79
80 // Skip over all of the undef values.
81 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
82 ++i;
83
84 // Do not accept an all-undef vector.
85 if (i == e) return false;
86
87 // Do not accept build_vectors that aren't all constants or which have non-~0
88 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000089 SDOperand NotZero = N->getOperand(i);
90 if (!isa<ConstantSDNode>(NotZero) ||
91 !cast<ConstantSDNode>(NotZero)->isAllOnesValue())
Chris Lattner61d43992006-03-25 22:57:01 +000092 return false;
93
94 // Okay, we have at least one ~0 value, check to see if the rest match or are
95 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +000096 for (++i; i != e; ++i)
97 if (N->getOperand(i) != NotZero &&
98 N->getOperand(i).getOpcode() != ISD::UNDEF)
99 return false;
100 return true;
101}
102
103
Evan Cheng4a147842006-03-26 09:50:58 +0000104/// isBuildVectorAllZeros - Return true if the specified node is a
105/// BUILD_VECTOR where all of the elements are 0 or undef.
106bool ISD::isBuildVectorAllZeros(const SDNode *N) {
107 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
108
109 bool AllUndef = true;
110 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
111 SDOperand Elt = N->getOperand(i);
112 if (Elt.getOpcode() != ISD::UNDEF) {
113 AllUndef = false;
114 if (isa<ConstantSDNode>(Elt)) {
115 if (!cast<ConstantSDNode>(Elt)->isNullValue())
116 return false;
117 } else if (isa<ConstantFPSDNode>(Elt)) {
118 if (!cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0))
119 return false;
120 } else
121 return false;
122 }
123 }
124
125 return !AllUndef;
126}
127
Chris Lattnerc3aae252005-01-07 07:46:32 +0000128/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
129/// when given the operation for (X op Y).
130ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
131 // To perform this operation, we just need to swap the L and G bits of the
132 // operation.
133 unsigned OldL = (Operation >> 2) & 1;
134 unsigned OldG = (Operation >> 1) & 1;
135 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
136 (OldL << 1) | // New G bit
137 (OldG << 2)); // New L bit.
138}
139
140/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
141/// 'op' is a valid SetCC operation.
142ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
143 unsigned Operation = Op;
144 if (isInteger)
145 Operation ^= 7; // Flip L, G, E bits, but not U.
146 else
147 Operation ^= 15; // Flip all of the condition bits.
148 if (Operation > ISD::SETTRUE2)
149 Operation &= ~8; // Don't let N and U bits get set.
150 return ISD::CondCode(Operation);
151}
152
153
154/// isSignedOp - For an integer comparison, return 1 if the comparison is a
155/// signed operation and 2 if the result is an unsigned comparison. Return zero
156/// if the operation does not depend on the sign of the input (setne and seteq).
157static int isSignedOp(ISD::CondCode Opcode) {
158 switch (Opcode) {
159 default: assert(0 && "Illegal integer setcc operation!");
160 case ISD::SETEQ:
161 case ISD::SETNE: return 0;
162 case ISD::SETLT:
163 case ISD::SETLE:
164 case ISD::SETGT:
165 case ISD::SETGE: return 1;
166 case ISD::SETULT:
167 case ISD::SETULE:
168 case ISD::SETUGT:
169 case ISD::SETUGE: return 2;
170 }
171}
172
173/// getSetCCOrOperation - Return the result of a logical OR between different
174/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
175/// returns SETCC_INVALID if it is not possible to represent the resultant
176/// comparison.
177ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
178 bool isInteger) {
179 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
180 // Cannot fold a signed integer setcc with an unsigned integer setcc.
181 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000182
Chris Lattnerc3aae252005-01-07 07:46:32 +0000183 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000184
Chris Lattnerc3aae252005-01-07 07:46:32 +0000185 // If the N and U bits get set then the resultant comparison DOES suddenly
186 // care about orderedness, and is true when ordered.
187 if (Op > ISD::SETTRUE2)
188 Op &= ~16; // Clear the N bit.
189 return ISD::CondCode(Op);
190}
191
192/// getSetCCAndOperation - Return the result of a logical AND between different
193/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
194/// function returns zero if it is not possible to represent the resultant
195/// comparison.
196ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
197 bool isInteger) {
198 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
199 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000200 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000201
202 // Combine all of the condition bits.
203 return ISD::CondCode(Op1 & Op2);
204}
205
Chris Lattnerb48da392005-01-23 04:39:44 +0000206const TargetMachine &SelectionDAG::getTarget() const {
207 return TLI.getTargetMachine();
208}
209
Jim Laskey58b968b2005-08-17 20:08:02 +0000210//===----------------------------------------------------------------------===//
211// SelectionDAG Class
212//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000213
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000214/// RemoveDeadNodes - This method deletes all unreachable nodes in the
215/// SelectionDAG, including nodes (like loads) that have uses of their token
216/// chain but no other uses and no side effect. If a node is passed in as an
217/// argument, it is used as the seed for node deletion.
218void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000219 // Create a dummy node (which is not added to allnodes), that adds a reference
220 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000221 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000222
Chris Lattnerf469cb62005-11-08 18:52:27 +0000223 bool MadeChange = false;
224
Chris Lattner8b8749f2005-08-17 19:00:20 +0000225 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000226 if (N && N->use_empty()) {
227 DestroyDeadNode(N);
228 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000229 }
230
Chris Lattnerde202b32005-11-09 23:47:37 +0000231 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
232 if (I->use_empty() && I->getOpcode() != 65535) {
233 // Node is dead, recursively delete newly dead uses.
234 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000235 MadeChange = true;
236 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000237
238 // Walk the nodes list, removing the nodes we've marked as dead.
239 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000240 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
241 SDNode *N = I++;
242 if (N->use_empty())
243 AllNodes.erase(N);
244 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000245 }
246
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000247 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000248 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000249}
250
Chris Lattnerf469cb62005-11-08 18:52:27 +0000251/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
252/// graph. If it is the last user of any of its operands, recursively process
253/// them the same way.
254///
255void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000256 // Okay, we really are going to delete this node. First take this out of the
257 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000258 RemoveNodeFromCSEMaps(N);
259
260 // Next, brutally remove the operand list. This is safe to do, as there are
261 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000262 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
263 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000264 O->removeUser(N);
265
266 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000267 if (O->use_empty())
268 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000269 }
Chris Lattner65113b22005-11-08 22:07:03 +0000270 delete[] N->OperandList;
271 N->OperandList = 0;
272 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000273
274 // Mark the node as dead.
275 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000276}
277
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000278void SelectionDAG::DeleteNode(SDNode *N) {
279 assert(N->use_empty() && "Cannot delete a node that is not dead!");
280
281 // First take this out of the appropriate CSE map.
282 RemoveNodeFromCSEMaps(N);
283
Chris Lattner1e111c72005-09-07 05:37:01 +0000284 // Finally, remove uses due to operands of this node, remove from the
285 // AllNodes list, and delete the node.
286 DeleteNodeNotInCSEMaps(N);
287}
288
289void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
290
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000291 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000292 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000293
294 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000295 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
296 I->Val->removeUser(N);
297 delete[] N->OperandList;
298 N->OperandList = 0;
299 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000300
301 delete N;
302}
303
Chris Lattner149c58c2005-08-16 18:17:10 +0000304/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
305/// correspond to it. This is useful when we're about to delete or repurpose
306/// the node. We don't want future request for structurally identical nodes
307/// to return N anymore.
308void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000309 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000310 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000311 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000313 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
314 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000316 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000317 Erased = TargetConstants.erase(std::make_pair(
318 cast<ConstantSDNode>(N)->getValue(),
319 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000320 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000321 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000322 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000324 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000325 }
Chris Lattner3181a772006-01-29 06:26:56 +0000326 case ISD::TargetConstantFP: {
327 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
328 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
329 break;
330 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000331 case ISD::STRING:
332 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
333 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000334 case ISD::CONDCODE:
335 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
336 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000337 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000338 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
339 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000340 case ISD::GlobalAddress: {
341 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
342 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
343 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000344 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000345 }
346 case ISD::TargetGlobalAddress: {
347 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
348 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
349 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000350 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000351 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000352 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000353 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000354 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000355 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000356 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000357 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000358 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000359 Erased = ConstantPoolIndices.
360 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000361 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
362 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000363 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000364 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000365 Erased = TargetConstantPoolIndices.
366 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000367 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
368 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000369 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000371 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000372 break;
373 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000374 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000375 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000376 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000377 Erased =
378 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000379 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000380 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000381 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000382 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
383 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000384 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000385 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
386 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000387 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000388 case ISD::SRCVALUE: {
389 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000390 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000391 break;
392 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000393 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000394 Erased = Loads.erase(std::make_pair(N->getOperand(1),
395 std::make_pair(N->getOperand(0),
396 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000397 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000398 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000399 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000400 if (N->getNumOperands() == 0) {
401 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
402 N->getValueType(0)));
403 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000404 Erased =
405 UnaryOps.erase(std::make_pair(N->getOpcode(),
406 std::make_pair(N->getOperand(0),
407 N->getValueType(0))));
408 } else if (N->getNumOperands() == 2) {
409 Erased =
410 BinaryOps.erase(std::make_pair(N->getOpcode(),
411 std::make_pair(N->getOperand(0),
412 N->getOperand(1))));
413 } else {
414 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
415 Erased =
416 OneResultNodes.erase(std::make_pair(N->getOpcode(),
417 std::make_pair(N->getValueType(0),
418 Ops)));
419 }
Chris Lattner385328c2005-05-14 07:42:29 +0000420 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000421 // Remove the node from the ArbitraryNodes map.
422 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
423 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000424 Erased =
425 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
426 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000427 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000428 break;
429 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000430#ifndef NDEBUG
431 // Verify that the node was actually in one of the CSE maps, unless it has a
432 // flag result (which cannot be CSE'd) or is one of the special cases that are
433 // not subject to CSE.
434 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000435 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000436 N->dump();
437 assert(0 && "Node is not in map!");
438 }
439#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000440}
441
Chris Lattner8b8749f2005-08-17 19:00:20 +0000442/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
443/// has been taken out and modified in some way. If the specified node already
444/// exists in the CSE maps, do not modify the maps, but return the existing node
445/// instead. If it doesn't exist, add it and return null.
446///
447SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
448 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000449 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000450 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000451
Chris Lattner9f8cc692005-12-19 22:21:21 +0000452 // Check that remaining values produced are not flags.
453 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
454 if (N->getValueType(i) == MVT::Flag)
455 return 0; // Never CSE anything that produces a flag.
456
Chris Lattnerfe14b342005-12-01 23:14:50 +0000457 if (N->getNumValues() == 1) {
458 if (N->getNumOperands() == 1) {
459 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
460 std::make_pair(N->getOperand(0),
461 N->getValueType(0)))];
462 if (U) return U;
463 U = N;
464 } else if (N->getNumOperands() == 2) {
465 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
466 std::make_pair(N->getOperand(0),
467 N->getOperand(1)))];
468 if (B) return B;
469 B = N;
470 } else {
471 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
472 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000473 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000474 if (ORN) return ORN;
475 ORN = N;
476 }
477 } else {
478 if (N->getOpcode() == ISD::LOAD) {
479 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
480 std::make_pair(N->getOperand(0),
481 N->getValueType(0)))];
482 if (L) return L;
483 L = N;
484 } else {
485 // Remove the node from the ArbitraryNodes map.
486 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
487 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
488 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
489 std::make_pair(RV, Ops))];
490 if (AN) return AN;
491 AN = N;
492 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000493 }
494 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000495}
496
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000497/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
498/// were replaced with those specified. If this node is never memoized,
499/// return null, otherwise return a pointer to the slot it would take. If a
500/// node already exists with these operands, the slot will be non-null.
501SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000502 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000503 return 0; // Never add these nodes.
504
505 // Check that remaining values produced are not flags.
506 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
507 if (N->getValueType(i) == MVT::Flag)
508 return 0; // Never CSE anything that produces a flag.
509
510 if (N->getNumValues() == 1) {
511 return &UnaryOps[std::make_pair(N->getOpcode(),
512 std::make_pair(Op, N->getValueType(0)))];
513 } else {
514 // Remove the node from the ArbitraryNodes map.
515 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
516 std::vector<SDOperand> Ops;
517 Ops.push_back(Op);
518 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
519 std::make_pair(RV, Ops))];
520 }
521 return 0;
522}
523
524/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
525/// were replaced with those specified. If this node is never memoized,
526/// return null, otherwise return a pointer to the slot it would take. If a
527/// node already exists with these operands, the slot will be non-null.
528SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
529 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000530 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000531 return 0; // Never add these nodes.
532
533 // Check that remaining values produced are not flags.
534 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
535 if (N->getValueType(i) == MVT::Flag)
536 return 0; // Never CSE anything that produces a flag.
537
538 if (N->getNumValues() == 1) {
539 return &BinaryOps[std::make_pair(N->getOpcode(),
540 std::make_pair(Op1, Op2))];
541 } else {
542 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
543 std::vector<SDOperand> Ops;
544 Ops.push_back(Op1);
545 Ops.push_back(Op2);
546 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
547 std::make_pair(RV, Ops))];
548 }
549 return 0;
550}
551
552
553/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
554/// were replaced with those specified. If this node is never memoized,
555/// return null, otherwise return a pointer to the slot it would take. If a
556/// node already exists with these operands, the slot will be non-null.
557SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
558 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000559 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000560 return 0; // Never add these nodes.
561
562 // Check that remaining values produced are not flags.
563 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
564 if (N->getValueType(i) == MVT::Flag)
565 return 0; // Never CSE anything that produces a flag.
566
567 if (N->getNumValues() == 1) {
568 if (N->getNumOperands() == 1) {
569 return &UnaryOps[std::make_pair(N->getOpcode(),
570 std::make_pair(Ops[0],
571 N->getValueType(0)))];
572 } else if (N->getNumOperands() == 2) {
573 return &BinaryOps[std::make_pair(N->getOpcode(),
574 std::make_pair(Ops[0], Ops[1]))];
575 } else {
576 return &OneResultNodes[std::make_pair(N->getOpcode(),
577 std::make_pair(N->getValueType(0),
578 Ops))];
579 }
580 } else {
581 if (N->getOpcode() == ISD::LOAD) {
582 return &Loads[std::make_pair(Ops[1],
583 std::make_pair(Ops[0], N->getValueType(0)))];
584 } else {
585 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
586 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
587 std::make_pair(RV, Ops))];
588 }
589 }
590 return 0;
591}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000592
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000593
Chris Lattner78ec3112003-08-11 14:57:33 +0000594SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000595 while (!AllNodes.empty()) {
596 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000597 delete [] N->OperandList;
598 N->OperandList = 0;
599 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000600 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000601 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000602}
603
Chris Lattner0f2287b2005-04-13 02:38:18 +0000604SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000605 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000606 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000607 return getNode(ISD::AND, Op.getValueType(), Op,
608 getConstant(Imm, Op.getValueType()));
609}
610
Chris Lattnerc3aae252005-01-07 07:46:32 +0000611SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
612 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
613 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000614 if (VT != MVT::i64)
615 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000616
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617 SDNode *&N = Constants[std::make_pair(Val, VT)];
618 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000619 N = new ConstantSDNode(false, Val, VT);
620 AllNodes.push_back(N);
621 return SDOperand(N, 0);
622}
623
Chris Lattner36ce6912005-11-29 06:21:05 +0000624SDOperand SelectionDAG::getString(const std::string &Val) {
625 StringSDNode *&N = StringNodes[Val];
626 if (!N) {
627 N = new StringSDNode(Val);
628 AllNodes.push_back(N);
629 }
630 return SDOperand(N, 0);
631}
632
Chris Lattner37bfbb42005-08-17 00:34:06 +0000633SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
634 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
635 // Mask out any bits that are not valid for this constant.
636 if (VT != MVT::i64)
637 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
638
639 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
640 if (N) return SDOperand(N, 0);
641 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000642 AllNodes.push_back(N);
643 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000644}
645
Chris Lattnerc3aae252005-01-07 07:46:32 +0000646SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
647 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
648 if (VT == MVT::f32)
649 Val = (float)Val; // Mask out extra precision.
650
Chris Lattnerd8658612005-02-17 20:17:32 +0000651 // Do the map lookup using the actual bit pattern for the floating point
652 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
653 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000654 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000655 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000656 N = new ConstantFPSDNode(false, Val, VT);
657 AllNodes.push_back(N);
658 return SDOperand(N, 0);
659}
660
661SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
662 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
663 if (VT == MVT::f32)
664 Val = (float)Val; // Mask out extra precision.
665
666 // Do the map lookup using the actual bit pattern for the floating point
667 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
668 // we don't have issues with SNANs.
669 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
670 if (N) return SDOperand(N, 0);
671 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000672 AllNodes.push_back(N);
673 return SDOperand(N, 0);
674}
675
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000677 MVT::ValueType VT, int offset) {
678 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000679 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000680 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000681 AllNodes.push_back(N);
682 return SDOperand(N, 0);
683}
684
685SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000686 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000687 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000688 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000689 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000690 AllNodes.push_back(N);
691 return SDOperand(N, 0);
692}
693
694SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
695 SDNode *&N = FrameIndices[FI];
696 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000697 N = new FrameIndexSDNode(FI, VT, false);
698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
700}
701
702SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
703 SDNode *&N = TargetFrameIndices[FI];
704 if (N) return SDOperand(N, 0);
705 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 AllNodes.push_back(N);
707 return SDOperand(N, 0);
708}
709
Evan Chengb8973bd2006-01-31 22:23:14 +0000710SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000711 unsigned Alignment, int Offset) {
712 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
713 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000714 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000715 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000716 AllNodes.push_back(N);
717 return SDOperand(N, 0);
718}
719
Evan Chengb8973bd2006-01-31 22:23:14 +0000720SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000721 unsigned Alignment, int Offset) {
722 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
723 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000724 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000725 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000726 AllNodes.push_back(N);
727 return SDOperand(N, 0);
728}
729
730SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
731 SDNode *&N = BBNodes[MBB];
732 if (N) return SDOperand(N, 0);
733 N = new BasicBlockSDNode(MBB);
734 AllNodes.push_back(N);
735 return SDOperand(N, 0);
736}
737
Chris Lattner15e4b012005-07-10 00:07:11 +0000738SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
739 if ((unsigned)VT >= ValueTypeNodes.size())
740 ValueTypeNodes.resize(VT+1);
741 if (ValueTypeNodes[VT] == 0) {
742 ValueTypeNodes[VT] = new VTSDNode(VT);
743 AllNodes.push_back(ValueTypeNodes[VT]);
744 }
745
746 return SDOperand(ValueTypeNodes[VT], 0);
747}
748
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
750 SDNode *&N = ExternalSymbols[Sym];
751 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000752 N = new ExternalSymbolSDNode(false, Sym, VT);
753 AllNodes.push_back(N);
754 return SDOperand(N, 0);
755}
756
Chris Lattner809ec112006-01-28 10:09:25 +0000757SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
758 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000759 SDNode *&N = TargetExternalSymbols[Sym];
760 if (N) return SDOperand(N, 0);
761 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000762 AllNodes.push_back(N);
763 return SDOperand(N, 0);
764}
765
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000766SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
767 if ((unsigned)Cond >= CondCodeNodes.size())
768 CondCodeNodes.resize(Cond+1);
769
Chris Lattner079a27a2005-08-09 20:40:02 +0000770 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000771 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000772 AllNodes.push_back(CondCodeNodes[Cond]);
773 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000774 return SDOperand(CondCodeNodes[Cond], 0);
775}
776
Chris Lattner0fdd7682005-08-30 22:38:38 +0000777SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
778 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
779 if (!Reg) {
780 Reg = new RegisterSDNode(RegNo, VT);
781 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000782 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000783 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000784}
785
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000786SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
787 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000788 // These setcc operations always fold.
789 switch (Cond) {
790 default: break;
791 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000792 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000793 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000794 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000795 }
796
Chris Lattner67255a12005-04-07 18:14:58 +0000797 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
798 uint64_t C2 = N2C->getValue();
799 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
800 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000801
Chris Lattnerc3aae252005-01-07 07:46:32 +0000802 // Sign extend the operands if required
803 if (ISD::isSignedIntSetCC(Cond)) {
804 C1 = N1C->getSignExtended();
805 C2 = N2C->getSignExtended();
806 }
807
808 switch (Cond) {
809 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000810 case ISD::SETEQ: return getConstant(C1 == C2, VT);
811 case ISD::SETNE: return getConstant(C1 != C2, VT);
812 case ISD::SETULT: return getConstant(C1 < C2, VT);
813 case ISD::SETUGT: return getConstant(C1 > C2, VT);
814 case ISD::SETULE: return getConstant(C1 <= C2, VT);
815 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
816 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
817 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
818 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
819 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820 }
Chris Lattner24673922005-04-07 18:58:54 +0000821 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000822 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000823 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
824 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
825
826 // If the comparison constant has bits in the upper part, the
827 // zero-extended value could never match.
828 if (C2 & (~0ULL << InSize)) {
829 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
830 switch (Cond) {
831 case ISD::SETUGT:
832 case ISD::SETUGE:
833 case ISD::SETEQ: return getConstant(0, VT);
834 case ISD::SETULT:
835 case ISD::SETULE:
836 case ISD::SETNE: return getConstant(1, VT);
837 case ISD::SETGT:
838 case ISD::SETGE:
839 // True if the sign bit of C2 is set.
840 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
841 case ISD::SETLT:
842 case ISD::SETLE:
843 // True if the sign bit of C2 isn't set.
844 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
845 default:
846 break;
847 }
848 }
849
850 // Otherwise, we can perform the comparison with the low bits.
851 switch (Cond) {
852 case ISD::SETEQ:
853 case ISD::SETNE:
854 case ISD::SETUGT:
855 case ISD::SETUGE:
856 case ISD::SETULT:
857 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000858 return getSetCC(VT, N1.getOperand(0),
859 getConstant(C2, N1.getOperand(0).getValueType()),
860 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000861 default:
862 break; // todo, be more careful with signed comparisons
863 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000864 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
865 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
866 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
867 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
868 MVT::ValueType ExtDstTy = N1.getValueType();
869 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000870
Chris Lattner7b2880c2005-08-24 22:44:39 +0000871 // If the extended part has any inconsistent bits, it cannot ever
872 // compare equal. In other words, they have to be all ones or all
873 // zeros.
874 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000875 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000876 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
877 return getConstant(Cond == ISD::SETNE, VT);
878
879 // Otherwise, make this a use of a zext.
880 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000881 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000882 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000883 }
884
Chris Lattner67255a12005-04-07 18:14:58 +0000885 uint64_t MinVal, MaxVal;
886 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
887 if (ISD::isSignedIntSetCC(Cond)) {
888 MinVal = 1ULL << (OperandBitSize-1);
889 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
890 MaxVal = ~0ULL >> (65-OperandBitSize);
891 else
892 MaxVal = 0;
893 } else {
894 MinVal = 0;
895 MaxVal = ~0ULL >> (64-OperandBitSize);
896 }
897
898 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
899 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
900 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
901 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000902 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
903 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000904 }
905
906 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
907 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
908 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000909 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
910 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000911 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000912
Nate Begeman72ea2812005-04-14 08:56:52 +0000913 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
914 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000915
Nate Begeman72ea2812005-04-14 08:56:52 +0000916 // Canonicalize setgt X, Min --> setne X, Min
917 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000918 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000919
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000920 // If we have setult X, 1, turn it into seteq X, 0
921 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000922 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
923 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000924 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000925 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000926 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
927 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000928
929 // If we have "setcc X, C1", check to see if we can shrink the immediate
930 // by changing cc.
931
932 // SETUGT X, SINTMAX -> SETLT X, 0
933 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
934 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000935 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000936
937 // FIXME: Implement the rest of these.
938
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000939
940 // Fold bit comparisons when we can.
941 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
942 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
943 if (ConstantSDNode *AndRHS =
944 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
945 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
946 // Perform the xform if the AND RHS is a single bit.
947 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
948 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000949 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000950 TLI.getShiftAmountTy()));
951 }
952 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
953 // (X & 8) == 8 --> (X & 8) >> 3
954 // Perform the xform if C2 is a single bit.
955 if ((C2 & (C2-1)) == 0) {
956 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000957 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000958 }
959 }
960 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000961 }
Chris Lattner67255a12005-04-07 18:14:58 +0000962 } else if (isa<ConstantSDNode>(N1.Val)) {
963 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000964 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000965 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966
967 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
968 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
969 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000970
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 switch (Cond) {
972 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000973 case ISD::SETEQ: return getConstant(C1 == C2, VT);
974 case ISD::SETNE: return getConstant(C1 != C2, VT);
975 case ISD::SETLT: return getConstant(C1 < C2, VT);
976 case ISD::SETGT: return getConstant(C1 > C2, VT);
977 case ISD::SETLE: return getConstant(C1 <= C2, VT);
978 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 }
980 } else {
981 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000982 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983 }
984
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000985 // Could not fold it.
986 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987}
988
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000990///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000991SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000992 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
993 if (!N) {
994 N = new SDNode(Opcode, VT);
995 AllNodes.push_back(N);
996 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997 return SDOperand(N, 0);
998}
999
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1001 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001002 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001003 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1005 uint64_t Val = C->getValue();
1006 switch (Opcode) {
1007 default: break;
1008 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001009 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1011 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001012 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1013 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001014 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001015 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001016 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001017 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001018 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001019 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001020 case ISD::BSWAP:
1021 switch(VT) {
1022 default: assert(0 && "Invalid bswap!"); break;
1023 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1024 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1025 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1026 }
1027 break;
1028 case ISD::CTPOP:
1029 switch(VT) {
1030 default: assert(0 && "Invalid ctpop!"); break;
1031 case MVT::i1: return getConstant(Val != 0, VT);
1032 case MVT::i8:
1033 Tmp1 = (unsigned)Val & 0xFF;
1034 return getConstant(CountPopulation_32(Tmp1), VT);
1035 case MVT::i16:
1036 Tmp1 = (unsigned)Val & 0xFFFF;
1037 return getConstant(CountPopulation_32(Tmp1), VT);
1038 case MVT::i32:
1039 return getConstant(CountPopulation_32((unsigned)Val), VT);
1040 case MVT::i64:
1041 return getConstant(CountPopulation_64(Val), VT);
1042 }
1043 case ISD::CTLZ:
1044 switch(VT) {
1045 default: assert(0 && "Invalid ctlz!"); break;
1046 case MVT::i1: return getConstant(Val == 0, VT);
1047 case MVT::i8:
1048 Tmp1 = (unsigned)Val & 0xFF;
1049 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1050 case MVT::i16:
1051 Tmp1 = (unsigned)Val & 0xFFFF;
1052 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1053 case MVT::i32:
1054 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1055 case MVT::i64:
1056 return getConstant(CountLeadingZeros_64(Val), VT);
1057 }
1058 case ISD::CTTZ:
1059 switch(VT) {
1060 default: assert(0 && "Invalid cttz!"); break;
1061 case MVT::i1: return getConstant(Val == 0, VT);
1062 case MVT::i8:
1063 Tmp1 = (unsigned)Val | 0x100;
1064 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1065 case MVT::i16:
1066 Tmp1 = (unsigned)Val | 0x10000;
1067 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1068 case MVT::i32:
1069 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1070 case MVT::i64:
1071 return getConstant(CountTrailingZeros_64(Val), VT);
1072 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001073 }
1074 }
1075
Chris Lattner94683772005-12-23 05:30:37 +00001076 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1078 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001079 case ISD::FNEG:
1080 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001081 case ISD::FABS:
1082 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001083 case ISD::FP_ROUND:
1084 case ISD::FP_EXTEND:
1085 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001086 case ISD::FP_TO_SINT:
1087 return getConstant((int64_t)C->getValue(), VT);
1088 case ISD::FP_TO_UINT:
1089 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001090 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001091 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001092 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001093 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001094 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001095 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 }
1097
1098 unsigned OpOpcode = Operand.Val->getOpcode();
1099 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001100 case ISD::TokenFactor:
1101 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 case ISD::SIGN_EXTEND:
1103 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001104 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1106 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1107 break;
1108 case ISD::ZERO_EXTEND:
1109 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001110 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001111 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001112 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001114 case ISD::ANY_EXTEND:
1115 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001116 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001117 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1118 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1119 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1120 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001121 case ISD::TRUNCATE:
1122 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001123 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 if (OpOpcode == ISD::TRUNCATE)
1125 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001126 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1127 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001128 // If the source is smaller than the dest, we still need an extend.
1129 if (Operand.Val->getOperand(0).getValueType() < VT)
1130 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1131 else if (Operand.Val->getOperand(0).getValueType() > VT)
1132 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1133 else
1134 return Operand.Val->getOperand(0);
1135 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001136 break;
Chris Lattner35481892005-12-23 00:16:34 +00001137 case ISD::BIT_CONVERT:
1138 // Basic sanity checking.
Chris Lattner1c6191f2006-03-21 19:20:37 +00001139 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1140 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001141 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001142 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001143 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1144 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001145 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001146 case ISD::SCALAR_TO_VECTOR:
1147 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1148 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1149 "Illegal SCALAR_TO_VECTOR node!");
1150 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001151 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001152 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1153 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001154 Operand.Val->getOperand(0));
1155 if (OpOpcode == ISD::FNEG) // --X -> X
1156 return Operand.Val->getOperand(0);
1157 break;
1158 case ISD::FABS:
1159 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1160 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1161 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 }
1163
Chris Lattner43247a12005-08-25 19:12:10 +00001164 SDNode *N;
1165 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1166 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001167 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001168 E = N = new SDNode(Opcode, Operand);
1169 } else {
1170 N = new SDNode(Opcode, Operand);
1171 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 N->setValueTypes(VT);
1173 AllNodes.push_back(N);
1174 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001175}
1176
Chris Lattner36019aa2005-04-18 03:48:41 +00001177
1178
Chris Lattnerc3aae252005-01-07 07:46:32 +00001179SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1180 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001181#ifndef NDEBUG
1182 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001183 case ISD::TokenFactor:
1184 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1185 N2.getValueType() == MVT::Other && "Invalid token factor!");
1186 break;
Chris Lattner76365122005-01-16 02:23:22 +00001187 case ISD::AND:
1188 case ISD::OR:
1189 case ISD::XOR:
1190 case ISD::UDIV:
1191 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001192 case ISD::MULHU:
1193 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001194 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1195 // fall through
1196 case ISD::ADD:
1197 case ISD::SUB:
1198 case ISD::MUL:
1199 case ISD::SDIV:
1200 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001201 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1202 // fall through.
1203 case ISD::FADD:
1204 case ISD::FSUB:
1205 case ISD::FMUL:
1206 case ISD::FDIV:
1207 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001208 assert(N1.getValueType() == N2.getValueType() &&
1209 N1.getValueType() == VT && "Binary operator types must match!");
1210 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001211 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1212 assert(N1.getValueType() == VT &&
1213 MVT::isFloatingPoint(N1.getValueType()) &&
1214 MVT::isFloatingPoint(N2.getValueType()) &&
1215 "Invalid FCOPYSIGN!");
1216 break;
Chris Lattner76365122005-01-16 02:23:22 +00001217 case ISD::SHL:
1218 case ISD::SRA:
1219 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001220 case ISD::ROTL:
1221 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001222 assert(VT == N1.getValueType() &&
1223 "Shift operators return type must be the same as their first arg");
1224 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001225 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001226 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001227 case ISD::FP_ROUND_INREG: {
1228 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1229 assert(VT == N1.getValueType() && "Not an inreg round!");
1230 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1231 "Cannot FP_ROUND_INREG integer types");
1232 assert(EVT <= VT && "Not rounding down!");
1233 break;
1234 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001235 case ISD::AssertSext:
1236 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001237 case ISD::SIGN_EXTEND_INREG: {
1238 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1239 assert(VT == N1.getValueType() && "Not an inreg extend!");
1240 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1241 "Cannot *_EXTEND_INREG FP types");
1242 assert(EVT <= VT && "Not extending!");
1243 }
1244
Chris Lattner76365122005-01-16 02:23:22 +00001245 default: break;
1246 }
1247#endif
1248
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1250 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1251 if (N1C) {
1252 if (N2C) {
1253 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1254 switch (Opcode) {
1255 case ISD::ADD: return getConstant(C1 + C2, VT);
1256 case ISD::SUB: return getConstant(C1 - C2, VT);
1257 case ISD::MUL: return getConstant(C1 * C2, VT);
1258 case ISD::UDIV:
1259 if (C2) return getConstant(C1 / C2, VT);
1260 break;
1261 case ISD::UREM :
1262 if (C2) return getConstant(C1 % C2, VT);
1263 break;
1264 case ISD::SDIV :
1265 if (C2) return getConstant(N1C->getSignExtended() /
1266 N2C->getSignExtended(), VT);
1267 break;
1268 case ISD::SREM :
1269 if (C2) return getConstant(N1C->getSignExtended() %
1270 N2C->getSignExtended(), VT);
1271 break;
1272 case ISD::AND : return getConstant(C1 & C2, VT);
1273 case ISD::OR : return getConstant(C1 | C2, VT);
1274 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001275 case ISD::SHL : return getConstant(C1 << C2, VT);
1276 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001277 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001278 case ISD::ROTL :
1279 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1280 VT);
1281 case ISD::ROTR :
1282 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1283 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001284 default: break;
1285 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001286 } else { // Cannonicalize constant to RHS if commutative
1287 if (isCommutativeBinOp(Opcode)) {
1288 std::swap(N1C, N2C);
1289 std::swap(N1, N2);
1290 }
1291 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001292 }
1293
1294 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1295 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001296 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001297 if (N2CFP) {
1298 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1299 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001300 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1301 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1302 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1303 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001304 if (C2) return getConstantFP(C1 / C2, VT);
1305 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001306 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001307 if (C2) return getConstantFP(fmod(C1, C2), VT);
1308 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001309 case ISD::FCOPYSIGN: {
1310 union {
1311 double F;
1312 uint64_t I;
1313 } u1;
1314 union {
1315 double F;
1316 int64_t I;
1317 } u2;
1318 u1.F = C1;
1319 u2.F = C2;
1320 if (u2.I < 0) // Sign bit of RHS set?
1321 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1322 else
1323 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1324 return getConstantFP(u1.F, VT);
1325 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001326 default: break;
1327 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 } else { // Cannonicalize constant to RHS if commutative
1329 if (isCommutativeBinOp(Opcode)) {
1330 std::swap(N1CFP, N2CFP);
1331 std::swap(N1, N2);
1332 }
1333 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001334 }
1335
Chris Lattnerc3aae252005-01-07 07:46:32 +00001336 // Finally, fold operations that do not require constants.
1337 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001338 case ISD::FP_ROUND_INREG:
1339 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1340 break;
1341 case ISD::SIGN_EXTEND_INREG: {
1342 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1343 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001344 break;
1345 }
1346
Nate Begemaneea805e2005-04-13 21:23:31 +00001347 // FIXME: figure out how to safely handle things like
1348 // int foo(int x) { return 1 << (x & 255); }
1349 // int bar() { return foo(256); }
1350#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001351 case ISD::SHL:
1352 case ISD::SRL:
1353 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001354 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001355 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001356 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001357 else if (N2.getOpcode() == ISD::AND)
1358 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1359 // If the and is only masking out bits that cannot effect the shift,
1360 // eliminate the and.
1361 unsigned NumBits = MVT::getSizeInBits(VT);
1362 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1363 return getNode(Opcode, VT, N1, N2.getOperand(0));
1364 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001365 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001366#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001367 }
1368
Chris Lattner27e9b412005-05-11 18:57:39 +00001369 // Memoize this node if possible.
1370 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001371 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001372 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1373 if (BON) return SDOperand(BON, 0);
1374
1375 BON = N = new SDNode(Opcode, N1, N2);
1376 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001377 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001378 }
1379
Chris Lattner3e011362005-05-14 07:45:46 +00001380 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 AllNodes.push_back(N);
1382 return SDOperand(N, 0);
1383}
1384
Chris Lattnerc3aae252005-01-07 07:46:32 +00001385SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1386 SDOperand N1, SDOperand N2, SDOperand N3) {
1387 // Perform various simplifications.
1388 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1389 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1390 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1391 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001392 case ISD::SETCC: {
1393 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001394 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001395 if (Simp.Val) return Simp;
1396 break;
1397 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001398 case ISD::SELECT:
1399 if (N1C)
1400 if (N1C->getValue())
1401 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001402 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001403 return N3; // select false, X, Y -> Y
1404
1405 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001406 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001407 case ISD::BRCOND:
1408 if (N2C)
1409 if (N2C->getValue()) // Unconditional branch
1410 return getNode(ISD::BR, MVT::Other, N1, N3);
1411 else
1412 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001413 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001414 case ISD::VECTOR_SHUFFLE:
1415 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1416 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1417 N3.getOpcode() == ISD::BUILD_VECTOR &&
1418 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1419 "Illegal VECTOR_SHUFFLE node!");
1420 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001421 }
1422
Chris Lattner385328c2005-05-14 07:42:29 +00001423 std::vector<SDOperand> Ops;
1424 Ops.reserve(3);
1425 Ops.push_back(N1);
1426 Ops.push_back(N2);
1427 Ops.push_back(N3);
1428
Chris Lattner43247a12005-08-25 19:12:10 +00001429 // Memoize node if it doesn't produce a flag.
1430 SDNode *N;
1431 if (VT != MVT::Flag) {
1432 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1433 if (E) return SDOperand(E, 0);
1434 E = N = new SDNode(Opcode, N1, N2, N3);
1435 } else {
1436 N = new SDNode(Opcode, N1, N2, N3);
1437 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001438 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001439 AllNodes.push_back(N);
1440 return SDOperand(N, 0);
1441}
1442
1443SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001444 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001445 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001446 std::vector<SDOperand> Ops;
1447 Ops.reserve(4);
1448 Ops.push_back(N1);
1449 Ops.push_back(N2);
1450 Ops.push_back(N3);
1451 Ops.push_back(N4);
1452 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001453}
1454
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001455SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1456 SDOperand N1, SDOperand N2, SDOperand N3,
1457 SDOperand N4, SDOperand N5) {
1458 std::vector<SDOperand> Ops;
1459 Ops.reserve(5);
1460 Ops.push_back(N1);
1461 Ops.push_back(N2);
1462 Ops.push_back(N3);
1463 Ops.push_back(N4);
1464 Ops.push_back(N5);
1465 return getNode(Opcode, VT, Ops);
1466}
1467
Evan Cheng7038daf2005-12-10 00:37:58 +00001468SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1469 SDOperand Chain, SDOperand Ptr,
1470 SDOperand SV) {
1471 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1472 if (N) return SDOperand(N, 0);
1473 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1474
1475 // Loads have a token chain.
1476 setNodeValueTypes(N, VT, MVT::Other);
1477 AllNodes.push_back(N);
1478 return SDOperand(N, 0);
1479}
1480
1481SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1482 SDOperand Chain, SDOperand Ptr,
1483 SDOperand SV) {
1484 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1485 if (N) return SDOperand(N, 0);
1486 std::vector<SDOperand> Ops;
1487 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001488 Ops.push_back(Chain);
1489 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001490 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001491 Ops.push_back(getConstant(Count, MVT::i32));
1492 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001493 std::vector<MVT::ValueType> VTs;
1494 VTs.reserve(2);
1495 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1496 return getNode(ISD::VLOAD, VTs, Ops);
1497}
1498
1499SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1500 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1501 MVT::ValueType EVT) {
1502 std::vector<SDOperand> Ops;
1503 Ops.reserve(4);
1504 Ops.push_back(Chain);
1505 Ops.push_back(Ptr);
1506 Ops.push_back(SV);
1507 Ops.push_back(getValueType(EVT));
1508 std::vector<MVT::ValueType> VTs;
1509 VTs.reserve(2);
1510 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1511 return getNode(Opcode, VTs, Ops);
1512}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001513
Chris Lattner0437cdd2005-05-09 04:14:13 +00001514SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001515 assert((!V || isa<PointerType>(V->getType())) &&
1516 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001517 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1518 if (N) return SDOperand(N, 0);
1519
1520 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001521 AllNodes.push_back(N);
1522 return SDOperand(N, 0);
1523}
1524
Nate Begemanacc398c2006-01-25 18:21:52 +00001525SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1526 SDOperand Chain, SDOperand Ptr,
1527 SDOperand SV) {
1528 std::vector<SDOperand> Ops;
1529 Ops.reserve(3);
1530 Ops.push_back(Chain);
1531 Ops.push_back(Ptr);
1532 Ops.push_back(SV);
1533 std::vector<MVT::ValueType> VTs;
1534 VTs.reserve(2);
1535 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1536 return getNode(ISD::VAARG, VTs, Ops);
1537}
1538
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001539SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001540 std::vector<SDOperand> &Ops) {
1541 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001542 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001543 case 1: return getNode(Opcode, VT, Ops[0]);
1544 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1545 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001546 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001547 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001548
Chris Lattner89c34632005-05-14 06:20:26 +00001549 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001550 switch (Opcode) {
1551 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001552 case ISD::TRUNCSTORE: {
1553 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1554 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1555#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1556 // If this is a truncating store of a constant, convert to the desired type
1557 // and store it instead.
1558 if (isa<Constant>(Ops[0])) {
1559 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1560 if (isa<Constant>(Op))
1561 N1 = Op;
1562 }
1563 // Also for ConstantFP?
1564#endif
1565 if (Ops[0].getValueType() == EVT) // Normal store?
1566 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1567 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1568 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1569 "Can't do FP-INT conversion!");
1570 break;
1571 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001572 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001573 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001574 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1575 "LHS and RHS of condition must have same type!");
1576 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1577 "True and False arms of SelectCC must have same type!");
1578 assert(Ops[2].getValueType() == VT &&
1579 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001580 break;
1581 }
1582 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001583 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001584 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1585 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001586 break;
1587 }
Chris Lattneref847df2005-04-09 03:27:28 +00001588 }
1589
Chris Lattner385328c2005-05-14 07:42:29 +00001590 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001591 SDNode *N;
1592 if (VT != MVT::Flag) {
1593 SDNode *&E =
1594 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1595 if (E) return SDOperand(E, 0);
1596 E = N = new SDNode(Opcode, Ops);
1597 } else {
1598 N = new SDNode(Opcode, Ops);
1599 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001600 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001601 AllNodes.push_back(N);
1602 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001603}
1604
Chris Lattner89c34632005-05-14 06:20:26 +00001605SDOperand SelectionDAG::getNode(unsigned Opcode,
1606 std::vector<MVT::ValueType> &ResultTys,
1607 std::vector<SDOperand> &Ops) {
1608 if (ResultTys.size() == 1)
1609 return getNode(Opcode, ResultTys[0], Ops);
1610
Chris Lattner5f056bf2005-07-10 01:55:33 +00001611 switch (Opcode) {
1612 case ISD::EXTLOAD:
1613 case ISD::SEXTLOAD:
1614 case ISD::ZEXTLOAD: {
1615 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1616 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1617 // If they are asking for an extending load from/to the same thing, return a
1618 // normal load.
1619 if (ResultTys[0] == EVT)
1620 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001621 if (MVT::isVector(ResultTys[0])) {
1622 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1623 "Invalid vector extload!");
1624 } else {
1625 assert(EVT < ResultTys[0] &&
1626 "Should only be an extending load, not truncating!");
1627 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001628 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001629 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001630 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1631 "Cannot convert from FP to Int or Int -> FP!");
1632 break;
1633 }
1634
Chris Lattnere89083a2005-05-14 07:25:05 +00001635 // FIXME: figure out how to safely handle things like
1636 // int foo(int x) { return 1 << (x & 255); }
1637 // int bar() { return foo(256); }
1638#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001639 case ISD::SRA_PARTS:
1640 case ISD::SRL_PARTS:
1641 case ISD::SHL_PARTS:
1642 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001643 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001644 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1645 else if (N3.getOpcode() == ISD::AND)
1646 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1647 // If the and is only masking out bits that cannot effect the shift,
1648 // eliminate the and.
1649 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1650 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1651 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1652 }
1653 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001654#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001655 }
Chris Lattner89c34632005-05-14 06:20:26 +00001656
Chris Lattner43247a12005-08-25 19:12:10 +00001657 // Memoize the node unless it returns a flag.
1658 SDNode *N;
1659 if (ResultTys.back() != MVT::Flag) {
1660 SDNode *&E =
1661 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1662 if (E) return SDOperand(E, 0);
1663 E = N = new SDNode(Opcode, Ops);
1664 } else {
1665 N = new SDNode(Opcode, Ops);
1666 }
Chris Lattnera3255112005-11-08 23:30:28 +00001667 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001668 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001669 return SDOperand(N, 0);
1670}
1671
Chris Lattnera3255112005-11-08 23:30:28 +00001672void SelectionDAG::setNodeValueTypes(SDNode *N,
1673 std::vector<MVT::ValueType> &RetVals) {
1674 switch (RetVals.size()) {
1675 case 0: return;
1676 case 1: N->setValueTypes(RetVals[0]); return;
1677 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1678 default: break;
1679 }
1680
1681 std::list<std::vector<MVT::ValueType> >::iterator I =
1682 std::find(VTList.begin(), VTList.end(), RetVals);
1683 if (I == VTList.end()) {
1684 VTList.push_front(RetVals);
1685 I = VTList.begin();
1686 }
1687
1688 N->setValueTypes(&(*I)[0], I->size());
1689}
1690
1691void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1692 MVT::ValueType VT2) {
1693 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1694 E = VTList.end(); I != E; ++I) {
1695 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1696 N->setValueTypes(&(*I)[0], 2);
1697 return;
1698 }
1699 }
1700 std::vector<MVT::ValueType> V;
1701 V.push_back(VT1);
1702 V.push_back(VT2);
1703 VTList.push_front(V);
1704 N->setValueTypes(&(*VTList.begin())[0], 2);
1705}
1706
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001707/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1708/// specified operands. If the resultant node already exists in the DAG,
1709/// this does not modify the specified node, instead it returns the node that
1710/// already exists. If the resultant node does not exist in the DAG, the
1711/// input node is returned. As a degenerate case, if you specify the same
1712/// input operands as the node already has, the input node is returned.
1713SDOperand SelectionDAG::
1714UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1715 SDNode *N = InN.Val;
1716 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1717
1718 // Check to see if there is no change.
1719 if (Op == N->getOperand(0)) return InN;
1720
1721 // See if the modified node already exists.
1722 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1723 if (NewSlot && *NewSlot)
1724 return SDOperand(*NewSlot, InN.ResNo);
1725
1726 // Nope it doesn't. Remove the node from it's current place in the maps.
1727 if (NewSlot)
1728 RemoveNodeFromCSEMaps(N);
1729
1730 // Now we update the operands.
1731 N->OperandList[0].Val->removeUser(N);
1732 Op.Val->addUser(N);
1733 N->OperandList[0] = Op;
1734
1735 // If this gets put into a CSE map, add it.
1736 if (NewSlot) *NewSlot = N;
1737 return InN;
1738}
1739
1740SDOperand SelectionDAG::
1741UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1742 SDNode *N = InN.Val;
1743 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1744
1745 // Check to see if there is no change.
1746 bool AnyChange = false;
1747 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1748 return InN; // No operands changed, just return the input node.
1749
1750 // See if the modified node already exists.
1751 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1752 if (NewSlot && *NewSlot)
1753 return SDOperand(*NewSlot, InN.ResNo);
1754
1755 // Nope it doesn't. Remove the node from it's current place in the maps.
1756 if (NewSlot)
1757 RemoveNodeFromCSEMaps(N);
1758
1759 // Now we update the operands.
1760 if (N->OperandList[0] != Op1) {
1761 N->OperandList[0].Val->removeUser(N);
1762 Op1.Val->addUser(N);
1763 N->OperandList[0] = Op1;
1764 }
1765 if (N->OperandList[1] != Op2) {
1766 N->OperandList[1].Val->removeUser(N);
1767 Op2.Val->addUser(N);
1768 N->OperandList[1] = Op2;
1769 }
1770
1771 // If this gets put into a CSE map, add it.
1772 if (NewSlot) *NewSlot = N;
1773 return InN;
1774}
1775
1776SDOperand SelectionDAG::
1777UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1778 std::vector<SDOperand> Ops;
1779 Ops.push_back(Op1);
1780 Ops.push_back(Op2);
1781 Ops.push_back(Op3);
1782 return UpdateNodeOperands(N, Ops);
1783}
1784
1785SDOperand SelectionDAG::
1786UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1787 SDOperand Op3, SDOperand Op4) {
1788 std::vector<SDOperand> Ops;
1789 Ops.push_back(Op1);
1790 Ops.push_back(Op2);
1791 Ops.push_back(Op3);
1792 Ops.push_back(Op4);
1793 return UpdateNodeOperands(N, Ops);
1794}
1795
1796SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001797UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1798 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1799 std::vector<SDOperand> Ops;
1800 Ops.push_back(Op1);
1801 Ops.push_back(Op2);
1802 Ops.push_back(Op3);
1803 Ops.push_back(Op4);
1804 Ops.push_back(Op5);
1805 return UpdateNodeOperands(N, Ops);
1806}
1807
1808
1809SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001810UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1811 SDNode *N = InN.Val;
1812 assert(N->getNumOperands() == Ops.size() &&
1813 "Update with wrong number of operands");
1814
1815 // Check to see if there is no change.
1816 unsigned NumOps = Ops.size();
1817 bool AnyChange = false;
1818 for (unsigned i = 0; i != NumOps; ++i) {
1819 if (Ops[i] != N->getOperand(i)) {
1820 AnyChange = true;
1821 break;
1822 }
1823 }
1824
1825 // No operands changed, just return the input node.
1826 if (!AnyChange) return InN;
1827
1828 // See if the modified node already exists.
1829 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1830 if (NewSlot && *NewSlot)
1831 return SDOperand(*NewSlot, InN.ResNo);
1832
1833 // Nope it doesn't. Remove the node from it's current place in the maps.
1834 if (NewSlot)
1835 RemoveNodeFromCSEMaps(N);
1836
1837 // Now we update the operands.
1838 for (unsigned i = 0; i != NumOps; ++i) {
1839 if (N->OperandList[i] != Ops[i]) {
1840 N->OperandList[i].Val->removeUser(N);
1841 Ops[i].Val->addUser(N);
1842 N->OperandList[i] = Ops[i];
1843 }
1844 }
1845
1846 // If this gets put into a CSE map, add it.
1847 if (NewSlot) *NewSlot = N;
1848 return InN;
1849}
1850
1851
1852
Chris Lattner149c58c2005-08-16 18:17:10 +00001853
1854/// SelectNodeTo - These are used for target selectors to *mutate* the
1855/// specified node to have the specified return type, Target opcode, and
1856/// operands. Note that target opcodes are stored as
1857/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001858///
1859/// Note that SelectNodeTo returns the resultant node. If there is already a
1860/// node of the specified opcode and operands, it returns that node instead of
1861/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001862SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1863 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001864 // If an identical node already exists, use it.
1865 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1866 if (ON) return SDOperand(ON, 0);
1867
Chris Lattner7651fa42005-08-24 23:00:29 +00001868 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001869
Chris Lattner7651fa42005-08-24 23:00:29 +00001870 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1871 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001872
1873 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001874 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001875}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001876
Chris Lattnereb19e402005-11-30 22:45:14 +00001877SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1878 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001879 // If an identical node already exists, use it.
1880 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1881 std::make_pair(Op1, VT))];
1882 if (ON) return SDOperand(ON, 0);
1883
Chris Lattner149c58c2005-08-16 18:17:10 +00001884 RemoveNodeFromCSEMaps(N);
1885 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1886 N->setValueTypes(VT);
1887 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001888
1889 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001890 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001891}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001892
Chris Lattnereb19e402005-11-30 22:45:14 +00001893SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1894 MVT::ValueType VT, SDOperand Op1,
1895 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001896 // If an identical node already exists, use it.
1897 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1898 std::make_pair(Op1, Op2))];
1899 if (ON) return SDOperand(ON, 0);
1900
Chris Lattner149c58c2005-08-16 18:17:10 +00001901 RemoveNodeFromCSEMaps(N);
1902 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1903 N->setValueTypes(VT);
1904 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001905
1906 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001907 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001908}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001909
Chris Lattnereb19e402005-11-30 22:45:14 +00001910SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1911 MVT::ValueType VT, SDOperand Op1,
1912 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001913 // If an identical node already exists, use it.
1914 std::vector<SDOperand> OpList;
1915 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1916 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1917 std::make_pair(VT, OpList))];
1918 if (ON) return SDOperand(ON, 0);
1919
Chris Lattner149c58c2005-08-16 18:17:10 +00001920 RemoveNodeFromCSEMaps(N);
1921 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1922 N->setValueTypes(VT);
1923 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001924
1925 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001926 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001927}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001928
Chris Lattnereb19e402005-11-30 22:45:14 +00001929SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1930 MVT::ValueType VT, SDOperand Op1,
1931 SDOperand Op2, SDOperand Op3,
1932 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001933 // If an identical node already exists, use it.
1934 std::vector<SDOperand> OpList;
1935 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1936 OpList.push_back(Op4);
1937 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1938 std::make_pair(VT, OpList))];
1939 if (ON) return SDOperand(ON, 0);
1940
Nate Begeman294a0a12005-08-18 07:30:15 +00001941 RemoveNodeFromCSEMaps(N);
1942 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1943 N->setValueTypes(VT);
1944 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001945
1946 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001947 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001948}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001949
Chris Lattnereb19e402005-11-30 22:45:14 +00001950SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1951 MVT::ValueType VT, SDOperand Op1,
1952 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1953 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001954 // If an identical node already exists, use it.
1955 std::vector<SDOperand> OpList;
1956 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1957 OpList.push_back(Op4); OpList.push_back(Op5);
1958 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1959 std::make_pair(VT, OpList))];
1960 if (ON) return SDOperand(ON, 0);
1961
Chris Lattner6b09a292005-08-21 18:49:33 +00001962 RemoveNodeFromCSEMaps(N);
1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1964 N->setValueTypes(VT);
1965 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001966
1967 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001968 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001969}
Chris Lattner149c58c2005-08-16 18:17:10 +00001970
Chris Lattnereb19e402005-11-30 22:45:14 +00001971SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1972 MVT::ValueType VT, SDOperand Op1,
1973 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1974 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001975 // If an identical node already exists, use it.
1976 std::vector<SDOperand> OpList;
1977 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1978 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1979 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1980 std::make_pair(VT, OpList))];
1981 if (ON) return SDOperand(ON, 0);
1982
Evan Cheng61ca74b2005-11-30 02:04:11 +00001983 RemoveNodeFromCSEMaps(N);
1984 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1985 N->setValueTypes(VT);
1986 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001987
1988 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001989 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001990}
1991
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001992SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1993 MVT::ValueType VT, SDOperand Op1,
1994 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1995 SDOperand Op5, SDOperand Op6,
1996 SDOperand Op7) {
1997 // If an identical node already exists, use it.
1998 std::vector<SDOperand> OpList;
1999 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2000 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2001 OpList.push_back(Op7);
2002 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2003 std::make_pair(VT, OpList))];
2004 if (ON) return SDOperand(ON, 0);
2005
2006 RemoveNodeFromCSEMaps(N);
2007 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2008 N->setValueTypes(VT);
2009 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2010
2011 ON = N; // Memoize the new node.
2012 return SDOperand(N, 0);
2013}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002014SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2015 MVT::ValueType VT, SDOperand Op1,
2016 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2017 SDOperand Op5, SDOperand Op6,
2018 SDOperand Op7, SDOperand Op8) {
2019 // If an identical node already exists, use it.
2020 std::vector<SDOperand> OpList;
2021 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2022 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2023 OpList.push_back(Op7); OpList.push_back(Op8);
2024 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2025 std::make_pair(VT, OpList))];
2026 if (ON) return SDOperand(ON, 0);
2027
2028 RemoveNodeFromCSEMaps(N);
2029 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2030 N->setValueTypes(VT);
2031 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2032
2033 ON = N; // Memoize the new node.
2034 return SDOperand(N, 0);
2035}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002036
Chris Lattnereb19e402005-11-30 22:45:14 +00002037SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2038 MVT::ValueType VT1, MVT::ValueType VT2,
2039 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002040 // If an identical node already exists, use it.
2041 std::vector<SDOperand> OpList;
2042 OpList.push_back(Op1); OpList.push_back(Op2);
2043 std::vector<MVT::ValueType> VTList;
2044 VTList.push_back(VT1); VTList.push_back(VT2);
2045 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2046 std::make_pair(VTList, OpList))];
2047 if (ON) return SDOperand(ON, 0);
2048
Chris Lattner0fb094f2005-11-19 01:44:53 +00002049 RemoveNodeFromCSEMaps(N);
2050 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2051 setNodeValueTypes(N, VT1, VT2);
2052 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002053
2054 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002055 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002056}
2057
Chris Lattnereb19e402005-11-30 22:45:14 +00002058SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2059 MVT::ValueType VT1, MVT::ValueType VT2,
2060 SDOperand Op1, SDOperand Op2,
2061 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002062 // If an identical node already exists, use it.
2063 std::vector<SDOperand> OpList;
2064 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2065 std::vector<MVT::ValueType> VTList;
2066 VTList.push_back(VT1); VTList.push_back(VT2);
2067 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2068 std::make_pair(VTList, OpList))];
2069 if (ON) return SDOperand(ON, 0);
2070
Chris Lattner0fb094f2005-11-19 01:44:53 +00002071 RemoveNodeFromCSEMaps(N);
2072 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2073 setNodeValueTypes(N, VT1, VT2);
2074 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002075
2076 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002077 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002078}
2079
Chris Lattnereb19e402005-11-30 22:45:14 +00002080SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2081 MVT::ValueType VT1, MVT::ValueType VT2,
2082 SDOperand Op1, SDOperand Op2,
2083 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002084 // If an identical node already exists, use it.
2085 std::vector<SDOperand> OpList;
2086 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2087 OpList.push_back(Op4);
2088 std::vector<MVT::ValueType> VTList;
2089 VTList.push_back(VT1); VTList.push_back(VT2);
2090 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2091 std::make_pair(VTList, OpList))];
2092 if (ON) return SDOperand(ON, 0);
2093
Chris Lattner0fb094f2005-11-19 01:44:53 +00002094 RemoveNodeFromCSEMaps(N);
2095 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2096 setNodeValueTypes(N, VT1, VT2);
2097 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002098
2099 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002100 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002101}
2102
Chris Lattnereb19e402005-11-30 22:45:14 +00002103SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2104 MVT::ValueType VT1, MVT::ValueType VT2,
2105 SDOperand Op1, SDOperand Op2,
2106 SDOperand Op3, SDOperand Op4,
2107 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002108 // If an identical node already exists, use it.
2109 std::vector<SDOperand> OpList;
2110 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2111 OpList.push_back(Op4); OpList.push_back(Op5);
2112 std::vector<MVT::ValueType> VTList;
2113 VTList.push_back(VT1); VTList.push_back(VT2);
2114 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2115 std::make_pair(VTList, OpList))];
2116 if (ON) return SDOperand(ON, 0);
2117
Chris Lattner0fb094f2005-11-19 01:44:53 +00002118 RemoveNodeFromCSEMaps(N);
2119 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2120 setNodeValueTypes(N, VT1, VT2);
2121 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002122
2123 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002124 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002125}
2126
Evan Cheng6ae46c42006-02-09 07:15:23 +00002127/// getTargetNode - These are used for target selectors to create a new node
2128/// with specified return type(s), target opcode, and operands.
2129///
2130/// Note that getTargetNode returns the resultant node. If there is already a
2131/// node of the specified opcode and operands, it returns that node instead of
2132/// the current one.
2133SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2134 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2135}
2136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2137 SDOperand Op1) {
2138 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2139}
2140SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2141 SDOperand Op1, SDOperand Op2) {
2142 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2143}
2144SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2145 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2146 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2147}
2148SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2149 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2150 SDOperand Op4) {
2151 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2152}
2153SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2154 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2155 SDOperand Op4, SDOperand Op5) {
2156 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2157}
2158SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2159 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2160 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2161 std::vector<SDOperand> Ops;
2162 Ops.reserve(6);
2163 Ops.push_back(Op1);
2164 Ops.push_back(Op2);
2165 Ops.push_back(Op3);
2166 Ops.push_back(Op4);
2167 Ops.push_back(Op5);
2168 Ops.push_back(Op6);
2169 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2170}
2171SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2172 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2173 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2174 SDOperand Op7) {
2175 std::vector<SDOperand> Ops;
2176 Ops.reserve(7);
2177 Ops.push_back(Op1);
2178 Ops.push_back(Op2);
2179 Ops.push_back(Op3);
2180 Ops.push_back(Op4);
2181 Ops.push_back(Op5);
2182 Ops.push_back(Op6);
2183 Ops.push_back(Op7);
2184 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2185}
2186SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2187 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2188 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2189 SDOperand Op7, SDOperand Op8) {
2190 std::vector<SDOperand> Ops;
2191 Ops.reserve(8);
2192 Ops.push_back(Op1);
2193 Ops.push_back(Op2);
2194 Ops.push_back(Op3);
2195 Ops.push_back(Op4);
2196 Ops.push_back(Op5);
2197 Ops.push_back(Op6);
2198 Ops.push_back(Op7);
2199 Ops.push_back(Op8);
2200 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2201}
2202SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2203 std::vector<SDOperand> &Ops) {
2204 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2205}
2206SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2207 MVT::ValueType VT2, SDOperand Op1) {
2208 std::vector<MVT::ValueType> ResultTys;
2209 ResultTys.push_back(VT1);
2210 ResultTys.push_back(VT2);
2211 std::vector<SDOperand> Ops;
2212 Ops.push_back(Op1);
2213 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2214}
2215SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2216 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2217 std::vector<MVT::ValueType> ResultTys;
2218 ResultTys.push_back(VT1);
2219 ResultTys.push_back(VT2);
2220 std::vector<SDOperand> Ops;
2221 Ops.push_back(Op1);
2222 Ops.push_back(Op2);
2223 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2224}
2225SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2226 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2227 SDOperand Op3) {
2228 std::vector<MVT::ValueType> ResultTys;
2229 ResultTys.push_back(VT1);
2230 ResultTys.push_back(VT2);
2231 std::vector<SDOperand> Ops;
2232 Ops.push_back(Op1);
2233 Ops.push_back(Op2);
2234 Ops.push_back(Op3);
2235 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2236}
2237SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2238 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2239 SDOperand Op3, SDOperand Op4) {
2240 std::vector<MVT::ValueType> ResultTys;
2241 ResultTys.push_back(VT1);
2242 ResultTys.push_back(VT2);
2243 std::vector<SDOperand> Ops;
2244 Ops.push_back(Op1);
2245 Ops.push_back(Op2);
2246 Ops.push_back(Op3);
2247 Ops.push_back(Op4);
2248 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2249}
2250SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2251 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2252 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2253 std::vector<MVT::ValueType> ResultTys;
2254 ResultTys.push_back(VT1);
2255 ResultTys.push_back(VT2);
2256 std::vector<SDOperand> Ops;
2257 Ops.push_back(Op1);
2258 Ops.push_back(Op2);
2259 Ops.push_back(Op3);
2260 Ops.push_back(Op4);
2261 Ops.push_back(Op5);
2262 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2263}
2264SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2265 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2266 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2267 SDOperand Op6) {
2268 std::vector<MVT::ValueType> ResultTys;
2269 ResultTys.push_back(VT1);
2270 ResultTys.push_back(VT2);
2271 std::vector<SDOperand> Ops;
2272 Ops.push_back(Op1);
2273 Ops.push_back(Op2);
2274 Ops.push_back(Op3);
2275 Ops.push_back(Op4);
2276 Ops.push_back(Op5);
2277 Ops.push_back(Op6);
2278 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2279}
2280SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2281 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2282 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2283 SDOperand Op6, SDOperand Op7) {
2284 std::vector<MVT::ValueType> ResultTys;
2285 ResultTys.push_back(VT1);
2286 ResultTys.push_back(VT2);
2287 std::vector<SDOperand> Ops;
2288 Ops.push_back(Op1);
2289 Ops.push_back(Op2);
2290 Ops.push_back(Op3);
2291 Ops.push_back(Op4);
2292 Ops.push_back(Op5);
2293 Ops.push_back(Op6);
2294 Ops.push_back(Op7);
2295 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2296}
2297SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2298 MVT::ValueType VT2, MVT::ValueType VT3,
2299 SDOperand Op1, SDOperand Op2) {
2300 std::vector<MVT::ValueType> ResultTys;
2301 ResultTys.push_back(VT1);
2302 ResultTys.push_back(VT2);
2303 ResultTys.push_back(VT3);
2304 std::vector<SDOperand> Ops;
2305 Ops.push_back(Op1);
2306 Ops.push_back(Op2);
2307 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2308}
2309SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2310 MVT::ValueType VT2, MVT::ValueType VT3,
2311 SDOperand Op1, SDOperand Op2,
2312 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2313 std::vector<MVT::ValueType> ResultTys;
2314 ResultTys.push_back(VT1);
2315 ResultTys.push_back(VT2);
2316 ResultTys.push_back(VT3);
2317 std::vector<SDOperand> Ops;
2318 Ops.push_back(Op1);
2319 Ops.push_back(Op2);
2320 Ops.push_back(Op3);
2321 Ops.push_back(Op4);
2322 Ops.push_back(Op5);
2323 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2324}
2325SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2326 MVT::ValueType VT2, MVT::ValueType VT3,
2327 SDOperand Op1, SDOperand Op2,
2328 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2329 SDOperand Op6) {
2330 std::vector<MVT::ValueType> ResultTys;
2331 ResultTys.push_back(VT1);
2332 ResultTys.push_back(VT2);
2333 ResultTys.push_back(VT3);
2334 std::vector<SDOperand> Ops;
2335 Ops.push_back(Op1);
2336 Ops.push_back(Op2);
2337 Ops.push_back(Op3);
2338 Ops.push_back(Op4);
2339 Ops.push_back(Op5);
2340 Ops.push_back(Op6);
2341 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2342}
2343SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2344 MVT::ValueType VT2, MVT::ValueType VT3,
2345 SDOperand Op1, SDOperand Op2,
2346 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2347 SDOperand Op6, SDOperand Op7) {
2348 std::vector<MVT::ValueType> ResultTys;
2349 ResultTys.push_back(VT1);
2350 ResultTys.push_back(VT2);
2351 ResultTys.push_back(VT3);
2352 std::vector<SDOperand> Ops;
2353 Ops.push_back(Op1);
2354 Ops.push_back(Op2);
2355 Ops.push_back(Op3);
2356 Ops.push_back(Op4);
2357 Ops.push_back(Op5);
2358 Ops.push_back(Op6);
2359 Ops.push_back(Op7);
2360 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2361}
2362SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2363 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2364 std::vector<MVT::ValueType> ResultTys;
2365 ResultTys.push_back(VT1);
2366 ResultTys.push_back(VT2);
2367 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2368}
2369
Chris Lattner0fb094f2005-11-19 01:44:53 +00002370// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002371/// This can cause recursive merging of nodes in the DAG.
2372///
Chris Lattner8b52f212005-08-26 18:36:28 +00002373/// This version assumes From/To have a single result value.
2374///
Chris Lattner1e111c72005-09-07 05:37:01 +00002375void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2376 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002377 SDNode *From = FromN.Val, *To = ToN.Val;
2378 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2379 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002380 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002381
Chris Lattner8b8749f2005-08-17 19:00:20 +00002382 while (!From->use_empty()) {
2383 // Process users until they are all gone.
2384 SDNode *U = *From->use_begin();
2385
2386 // This node is about to morph, remove its old self from the CSE maps.
2387 RemoveNodeFromCSEMaps(U);
2388
Chris Lattner65113b22005-11-08 22:07:03 +00002389 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2390 I != E; ++I)
2391 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002392 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002393 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002394 To->addUser(U);
2395 }
2396
2397 // Now that we have modified U, add it back to the CSE maps. If it already
2398 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002399 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2400 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002401 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002402 if (Deleted) Deleted->push_back(U);
2403 DeleteNodeNotInCSEMaps(U);
2404 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002405 }
2406}
2407
2408/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2409/// This can cause recursive merging of nodes in the DAG.
2410///
2411/// This version assumes From/To have matching types and numbers of result
2412/// values.
2413///
Chris Lattner1e111c72005-09-07 05:37:01 +00002414void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2415 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002416 assert(From != To && "Cannot replace uses of with self");
2417 assert(From->getNumValues() == To->getNumValues() &&
2418 "Cannot use this version of ReplaceAllUsesWith!");
2419 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002420 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002421 return;
2422 }
2423
2424 while (!From->use_empty()) {
2425 // Process users until they are all gone.
2426 SDNode *U = *From->use_begin();
2427
2428 // This node is about to morph, remove its old self from the CSE maps.
2429 RemoveNodeFromCSEMaps(U);
2430
Chris Lattner65113b22005-11-08 22:07:03 +00002431 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2432 I != E; ++I)
2433 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002434 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002435 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002436 To->addUser(U);
2437 }
2438
2439 // Now that we have modified U, add it back to the CSE maps. If it already
2440 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002441 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2442 ReplaceAllUsesWith(U, Existing, Deleted);
2443 // U is now dead.
2444 if (Deleted) Deleted->push_back(U);
2445 DeleteNodeNotInCSEMaps(U);
2446 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002447 }
2448}
2449
Chris Lattner8b52f212005-08-26 18:36:28 +00002450/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2451/// This can cause recursive merging of nodes in the DAG.
2452///
2453/// This version can replace From with any result values. To must match the
2454/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002455void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002456 const std::vector<SDOperand> &To,
2457 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002458 assert(From->getNumValues() == To.size() &&
2459 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002460 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002461 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002462 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002463 return;
2464 }
2465
2466 while (!From->use_empty()) {
2467 // Process users until they are all gone.
2468 SDNode *U = *From->use_begin();
2469
2470 // This node is about to morph, remove its old self from the CSE maps.
2471 RemoveNodeFromCSEMaps(U);
2472
Chris Lattner65113b22005-11-08 22:07:03 +00002473 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2474 I != E; ++I)
2475 if (I->Val == From) {
2476 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002477 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002478 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002479 ToOp.Val->addUser(U);
2480 }
2481
2482 // Now that we have modified U, add it back to the CSE maps. If it already
2483 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002484 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2485 ReplaceAllUsesWith(U, Existing, Deleted);
2486 // U is now dead.
2487 if (Deleted) Deleted->push_back(U);
2488 DeleteNodeNotInCSEMaps(U);
2489 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002490 }
2491}
2492
Chris Lattner012f2412006-02-17 21:58:01 +00002493/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2494/// uses of other values produced by From.Val alone. The Deleted vector is
2495/// handled the same was as for ReplaceAllUsesWith.
2496void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2497 std::vector<SDNode*> &Deleted) {
2498 assert(From != To && "Cannot replace a value with itself");
2499 // Handle the simple, trivial, case efficiently.
2500 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2501 ReplaceAllUsesWith(From, To, &Deleted);
2502 return;
2503 }
2504
2505 // Get all of the users in a nice, deterministically ordered, uniqued set.
2506 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2507
2508 while (!Users.empty()) {
2509 // We know that this user uses some value of From. If it is the right
2510 // value, update it.
2511 SDNode *User = Users.back();
2512 Users.pop_back();
2513
2514 for (SDOperand *Op = User->OperandList,
2515 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2516 if (*Op == From) {
2517 // Okay, we know this user needs to be updated. Remove its old self
2518 // from the CSE maps.
2519 RemoveNodeFromCSEMaps(User);
2520
2521 // Update all operands that match "From".
2522 for (; Op != E; ++Op) {
2523 if (*Op == From) {
2524 From.Val->removeUser(User);
2525 *Op = To;
2526 To.Val->addUser(User);
2527 }
2528 }
2529
2530 // Now that we have modified User, add it back to the CSE maps. If it
2531 // already exists there, recursively merge the results together.
2532 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2533 unsigned NumDeleted = Deleted.size();
2534 ReplaceAllUsesWith(User, Existing, &Deleted);
2535
2536 // User is now dead.
2537 Deleted.push_back(User);
2538 DeleteNodeNotInCSEMaps(User);
2539
2540 // We have to be careful here, because ReplaceAllUsesWith could have
2541 // deleted a user of From, which means there may be dangling pointers
2542 // in the "Users" setvector. Scan over the deleted node pointers and
2543 // remove them from the setvector.
2544 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2545 Users.remove(Deleted[i]);
2546 }
2547 break; // Exit the operand scanning loop.
2548 }
2549 }
2550 }
2551}
2552
Chris Lattner7b2880c2005-08-24 22:44:39 +00002553
Jim Laskey58b968b2005-08-17 20:08:02 +00002554//===----------------------------------------------------------------------===//
2555// SDNode Class
2556//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002557
Chris Lattnera3255112005-11-08 23:30:28 +00002558
2559/// getValueTypeList - Return a pointer to the specified value type.
2560///
2561MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2562 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2563 VTs[VT] = VT;
2564 return &VTs[VT];
2565}
2566
Chris Lattner5c884562005-01-12 18:37:47 +00002567/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2568/// indicated value. This method ignores uses of other values defined by this
2569/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002570bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002571 assert(Value < getNumValues() && "Bad value!");
2572
2573 // If there is only one value, this is easy.
2574 if (getNumValues() == 1)
2575 return use_size() == NUses;
2576 if (Uses.size() < NUses) return false;
2577
Evan Cheng4ee62112006-02-05 06:29:23 +00002578 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002579
2580 std::set<SDNode*> UsersHandled;
2581
Evan Cheng4ee62112006-02-05 06:29:23 +00002582 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002583 UI != E; ++UI) {
2584 SDNode *User = *UI;
2585 if (User->getNumOperands() == 1 ||
2586 UsersHandled.insert(User).second) // First time we've seen this?
2587 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2588 if (User->getOperand(i) == TheValue) {
2589 if (NUses == 0)
2590 return false; // too many uses
2591 --NUses;
2592 }
2593 }
2594
2595 // Found exactly the right number of uses?
2596 return NUses == 0;
2597}
2598
2599
Evan Cheng4ee62112006-02-05 06:29:23 +00002600// isOnlyUse - Return true if this node is the only use of N.
2601bool SDNode::isOnlyUse(SDNode *N) const {
2602 bool Seen = false;
2603 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2604 SDNode *User = *I;
2605 if (User == this)
2606 Seen = true;
2607 else
2608 return false;
2609 }
2610
2611 return Seen;
2612}
2613
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002614// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002615bool SDOperand::isOperand(SDNode *N) const {
2616 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2617 if (*this == N->getOperand(i))
2618 return true;
2619 return false;
2620}
2621
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002622bool SDNode::isOperand(SDNode *N) const {
2623 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2624 if (this == N->OperandList[i].Val)
2625 return true;
2626 return false;
2627}
Evan Cheng4ee62112006-02-05 06:29:23 +00002628
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002629const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002630 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002631 default:
2632 if (getOpcode() < ISD::BUILTIN_OP_END)
2633 return "<<Unknown DAG Node>>";
2634 else {
Evan Cheng72261582005-12-20 06:22:03 +00002635 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002636 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002637 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2638 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002639
Evan Cheng72261582005-12-20 06:22:03 +00002640 TargetLowering &TLI = G->getTargetLoweringInfo();
2641 const char *Name =
2642 TLI.getTargetNodeName(getOpcode());
2643 if (Name) return Name;
2644 }
2645
2646 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002647 }
2648
Andrew Lenharth95762122005-03-31 21:24:06 +00002649 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002650 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002651 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002652 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002653 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002654 case ISD::AssertSext: return "AssertSext";
2655 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002656
2657 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002658 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002659 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002660 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002661
2662 case ISD::Constant: return "Constant";
2663 case ISD::ConstantFP: return "ConstantFP";
2664 case ISD::GlobalAddress: return "GlobalAddress";
2665 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002666 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002667 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner3e8f3ce2006-03-24 01:04:30 +00002668 case ISD::INTRINSIC: return "INTRINSIC";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002669
Chris Lattnerb2827b02006-03-19 00:52:58 +00002670 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002671 case ISD::TargetConstant: return "TargetConstant";
2672 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002673 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2674 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002675 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002676 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002677
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002678 case ISD::CopyToReg: return "CopyToReg";
2679 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002680 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002681 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002682 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002683 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002684
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002685 // Unary operators
2686 case ISD::FABS: return "fabs";
2687 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002688 case ISD::FSQRT: return "fsqrt";
2689 case ISD::FSIN: return "fsin";
2690 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002691
2692 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002693 case ISD::ADD: return "add";
2694 case ISD::SUB: return "sub";
2695 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002696 case ISD::MULHU: return "mulhu";
2697 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002698 case ISD::SDIV: return "sdiv";
2699 case ISD::UDIV: return "udiv";
2700 case ISD::SREM: return "srem";
2701 case ISD::UREM: return "urem";
2702 case ISD::AND: return "and";
2703 case ISD::OR: return "or";
2704 case ISD::XOR: return "xor";
2705 case ISD::SHL: return "shl";
2706 case ISD::SRA: return "sra";
2707 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002708 case ISD::ROTL: return "rotl";
2709 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002710 case ISD::FADD: return "fadd";
2711 case ISD::FSUB: return "fsub";
2712 case ISD::FMUL: return "fmul";
2713 case ISD::FDIV: return "fdiv";
2714 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002715 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002716 case ISD::VADD: return "vadd";
2717 case ISD::VSUB: return "vsub";
2718 case ISD::VMUL: return "vmul";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002719
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002720 case ISD::SETCC: return "setcc";
2721 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002722 case ISD::SELECT_CC: return "select_cc";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002723 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2724 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002725 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2726 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerce872152006-03-19 06:31:19 +00002727 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerfb194b92006-03-19 23:56:04 +00002728 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2729 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnere25ca692006-03-22 20:09:35 +00002730 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002731 case ISD::ADDC: return "addc";
2732 case ISD::ADDE: return "adde";
2733 case ISD::SUBC: return "subc";
2734 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002735 case ISD::SHL_PARTS: return "shl_parts";
2736 case ISD::SRA_PARTS: return "sra_parts";
2737 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002738
Chris Lattner7f644642005-04-28 21:44:03 +00002739 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002740 case ISD::SIGN_EXTEND: return "sign_extend";
2741 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002742 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002743 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002744 case ISD::TRUNCATE: return "truncate";
2745 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002746 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002747 case ISD::FP_EXTEND: return "fp_extend";
2748
2749 case ISD::SINT_TO_FP: return "sint_to_fp";
2750 case ISD::UINT_TO_FP: return "uint_to_fp";
2751 case ISD::FP_TO_SINT: return "fp_to_sint";
2752 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002753 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002754
2755 // Control flow instructions
2756 case ISD::BR: return "br";
2757 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002758 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002759 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002760 case ISD::CALLSEQ_START: return "callseq_start";
2761 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002762
2763 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002764 case ISD::LOAD: return "load";
2765 case ISD::STORE: return "store";
2766 case ISD::VLOAD: return "vload";
2767 case ISD::EXTLOAD: return "extload";
2768 case ISD::SEXTLOAD: return "sextload";
2769 case ISD::ZEXTLOAD: return "zextload";
2770 case ISD::TRUNCSTORE: return "truncstore";
2771 case ISD::VAARG: return "vaarg";
2772 case ISD::VACOPY: return "vacopy";
2773 case ISD::VAEND: return "vaend";
2774 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002775 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002776 case ISD::EXTRACT_ELEMENT: return "extract_element";
2777 case ISD::BUILD_PAIR: return "build_pair";
2778 case ISD::STACKSAVE: return "stacksave";
2779 case ISD::STACKRESTORE: return "stackrestore";
2780
2781 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002782 case ISD::MEMSET: return "memset";
2783 case ISD::MEMCPY: return "memcpy";
2784 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002785
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002786 // Bit manipulation
2787 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002788 case ISD::CTPOP: return "ctpop";
2789 case ISD::CTTZ: return "cttz";
2790 case ISD::CTLZ: return "ctlz";
2791
Chris Lattner36ce6912005-11-29 06:21:05 +00002792 // Debug info
2793 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002794 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002795 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002796
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002797 case ISD::CONDCODE:
2798 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002799 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002800 case ISD::SETOEQ: return "setoeq";
2801 case ISD::SETOGT: return "setogt";
2802 case ISD::SETOGE: return "setoge";
2803 case ISD::SETOLT: return "setolt";
2804 case ISD::SETOLE: return "setole";
2805 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002806
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002807 case ISD::SETO: return "seto";
2808 case ISD::SETUO: return "setuo";
2809 case ISD::SETUEQ: return "setue";
2810 case ISD::SETUGT: return "setugt";
2811 case ISD::SETUGE: return "setuge";
2812 case ISD::SETULT: return "setult";
2813 case ISD::SETULE: return "setule";
2814 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002815
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002816 case ISD::SETEQ: return "seteq";
2817 case ISD::SETGT: return "setgt";
2818 case ISD::SETGE: return "setge";
2819 case ISD::SETLT: return "setlt";
2820 case ISD::SETLE: return "setle";
2821 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002822 }
2823 }
2824}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002825
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002826void SDNode::dump() const { dump(0); }
2827void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002828 std::cerr << (void*)this << ": ";
2829
2830 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2831 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002832 if (getValueType(i) == MVT::Other)
2833 std::cerr << "ch";
2834 else
2835 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002836 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002837 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002838
2839 std::cerr << " ";
2840 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2841 if (i) std::cerr << ", ";
2842 std::cerr << (void*)getOperand(i).Val;
2843 if (unsigned RN = getOperand(i).ResNo)
2844 std::cerr << ":" << RN;
2845 }
2846
2847 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2848 std::cerr << "<" << CSDN->getValue() << ">";
2849 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2850 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002851 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002852 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002853 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002854 std::cerr << "<";
2855 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002856 if (offset > 0)
2857 std::cerr << " + " << offset;
2858 else
2859 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002860 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002861 std::cerr << "<" << FIDN->getIndex() << ">";
2862 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002863 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002864 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002865 if (offset > 0)
2866 std::cerr << " + " << offset;
2867 else
2868 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002869 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002870 std::cerr << "<";
2871 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2872 if (LBB)
2873 std::cerr << LBB->getName() << " ";
2874 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002875 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002876 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002877 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2878 } else {
2879 std::cerr << " #" << R->getReg();
2880 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002881 } else if (const ExternalSymbolSDNode *ES =
2882 dyn_cast<ExternalSymbolSDNode>(this)) {
2883 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002884 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2885 if (M->getValue())
2886 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2887 else
2888 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002889 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2890 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002891 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002892}
2893
Chris Lattnerde202b32005-11-09 23:47:37 +00002894static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002895 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2896 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002897 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002898 else
2899 std::cerr << "\n" << std::string(indent+2, ' ')
2900 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002901
Chris Lattnerea946cd2005-01-09 20:38:33 +00002902
2903 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002904 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002905}
2906
Chris Lattnerc3aae252005-01-07 07:46:32 +00002907void SelectionDAG::dump() const {
2908 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002909 std::vector<const SDNode*> Nodes;
2910 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2911 I != E; ++I)
2912 Nodes.push_back(I);
2913
Chris Lattner49d24712005-01-09 20:26:36 +00002914 std::sort(Nodes.begin(), Nodes.end());
2915
2916 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002917 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002918 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002919 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002920
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002921 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002922
Chris Lattnerc3aae252005-01-07 07:46:32 +00002923 std::cerr << "\n\n";
2924}
2925
Evan Chengfae9f1c2006-02-09 22:11:03 +00002926/// InsertISelMapEntry - A helper function to insert a key / element pair
2927/// into a SDOperand to SDOperand map. This is added to avoid the map
2928/// insertion operator from being inlined.
2929void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2930 SDNode *Key, unsigned KeyResNo,
2931 SDNode *Element, unsigned ElementResNo) {
2932 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2933 SDOperand(Element, ElementResNo)));
2934}