blob: 9fcfc61af79e7cec08df338ac31331eac95b977d [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.
89 if (!isa<ConstantSDNode>(N) || !cast<ConstantSDNode>(N)->isAllOnesValue())
90 return false;
91
92 // Okay, we have at least one ~0 value, check to see if the rest match or are
93 // undefs.
94 SDOperand NotZero = N->getOperand(i);
95 for (++i; i != e; ++i)
96 if (N->getOperand(i) != NotZero &&
97 N->getOperand(i).getOpcode() != ISD::UNDEF)
98 return false;
99 return true;
100}
101
102
Chris Lattnerc3aae252005-01-07 07:46:32 +0000103/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
104/// when given the operation for (X op Y).
105ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
106 // To perform this operation, we just need to swap the L and G bits of the
107 // operation.
108 unsigned OldL = (Operation >> 2) & 1;
109 unsigned OldG = (Operation >> 1) & 1;
110 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
111 (OldL << 1) | // New G bit
112 (OldG << 2)); // New L bit.
113}
114
115/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
116/// 'op' is a valid SetCC operation.
117ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
118 unsigned Operation = Op;
119 if (isInteger)
120 Operation ^= 7; // Flip L, G, E bits, but not U.
121 else
122 Operation ^= 15; // Flip all of the condition bits.
123 if (Operation > ISD::SETTRUE2)
124 Operation &= ~8; // Don't let N and U bits get set.
125 return ISD::CondCode(Operation);
126}
127
128
129/// isSignedOp - For an integer comparison, return 1 if the comparison is a
130/// signed operation and 2 if the result is an unsigned comparison. Return zero
131/// if the operation does not depend on the sign of the input (setne and seteq).
132static int isSignedOp(ISD::CondCode Opcode) {
133 switch (Opcode) {
134 default: assert(0 && "Illegal integer setcc operation!");
135 case ISD::SETEQ:
136 case ISD::SETNE: return 0;
137 case ISD::SETLT:
138 case ISD::SETLE:
139 case ISD::SETGT:
140 case ISD::SETGE: return 1;
141 case ISD::SETULT:
142 case ISD::SETULE:
143 case ISD::SETUGT:
144 case ISD::SETUGE: return 2;
145 }
146}
147
148/// getSetCCOrOperation - Return the result of a logical OR between different
149/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
150/// returns SETCC_INVALID if it is not possible to represent the resultant
151/// comparison.
152ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
153 bool isInteger) {
154 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
155 // Cannot fold a signed integer setcc with an unsigned integer setcc.
156 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000157
Chris Lattnerc3aae252005-01-07 07:46:32 +0000158 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000159
Chris Lattnerc3aae252005-01-07 07:46:32 +0000160 // If the N and U bits get set then the resultant comparison DOES suddenly
161 // care about orderedness, and is true when ordered.
162 if (Op > ISD::SETTRUE2)
163 Op &= ~16; // Clear the N bit.
164 return ISD::CondCode(Op);
165}
166
167/// getSetCCAndOperation - Return the result of a logical AND between different
168/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
169/// function returns zero if it is not possible to represent the resultant
170/// comparison.
171ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
172 bool isInteger) {
173 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
174 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000175 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000176
177 // Combine all of the condition bits.
178 return ISD::CondCode(Op1 & Op2);
179}
180
Chris Lattnerb48da392005-01-23 04:39:44 +0000181const TargetMachine &SelectionDAG::getTarget() const {
182 return TLI.getTargetMachine();
183}
184
Jim Laskey58b968b2005-08-17 20:08:02 +0000185//===----------------------------------------------------------------------===//
186// SelectionDAG Class
187//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000188
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000189/// RemoveDeadNodes - This method deletes all unreachable nodes in the
190/// SelectionDAG, including nodes (like loads) that have uses of their token
191/// chain but no other uses and no side effect. If a node is passed in as an
192/// argument, it is used as the seed for node deletion.
193void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000194 // Create a dummy node (which is not added to allnodes), that adds a reference
195 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000196 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000197
Chris Lattnerf469cb62005-11-08 18:52:27 +0000198 bool MadeChange = false;
199
Chris Lattner8b8749f2005-08-17 19:00:20 +0000200 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000201 if (N && N->use_empty()) {
202 DestroyDeadNode(N);
203 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000204 }
205
Chris Lattnerde202b32005-11-09 23:47:37 +0000206 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
207 if (I->use_empty() && I->getOpcode() != 65535) {
208 // Node is dead, recursively delete newly dead uses.
209 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000210 MadeChange = true;
211 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000212
213 // Walk the nodes list, removing the nodes we've marked as dead.
214 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000215 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
216 SDNode *N = I++;
217 if (N->use_empty())
218 AllNodes.erase(N);
219 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000220 }
221
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000222 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000223 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000224}
225
Chris Lattnerf469cb62005-11-08 18:52:27 +0000226/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
227/// graph. If it is the last user of any of its operands, recursively process
228/// them the same way.
229///
230void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000231 // Okay, we really are going to delete this node. First take this out of the
232 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000233 RemoveNodeFromCSEMaps(N);
234
235 // Next, brutally remove the operand list. This is safe to do, as there are
236 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000237 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
238 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000239 O->removeUser(N);
240
241 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000242 if (O->use_empty())
243 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000244 }
Chris Lattner65113b22005-11-08 22:07:03 +0000245 delete[] N->OperandList;
246 N->OperandList = 0;
247 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000248
249 // Mark the node as dead.
250 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000251}
252
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000253void SelectionDAG::DeleteNode(SDNode *N) {
254 assert(N->use_empty() && "Cannot delete a node that is not dead!");
255
256 // First take this out of the appropriate CSE map.
257 RemoveNodeFromCSEMaps(N);
258
Chris Lattner1e111c72005-09-07 05:37:01 +0000259 // Finally, remove uses due to operands of this node, remove from the
260 // AllNodes list, and delete the node.
261 DeleteNodeNotInCSEMaps(N);
262}
263
264void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
265
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000266 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000267 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000268
269 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000270 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
271 I->Val->removeUser(N);
272 delete[] N->OperandList;
273 N->OperandList = 0;
274 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000275
276 delete N;
277}
278
Chris Lattner149c58c2005-08-16 18:17:10 +0000279/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
280/// correspond to it. This is useful when we're about to delete or repurpose
281/// the node. We don't want future request for structurally identical nodes
282/// to return N anymore.
283void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000284 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000285 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000286 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000287 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000288 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
289 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000290 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000291 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000292 Erased = TargetConstants.erase(std::make_pair(
293 cast<ConstantSDNode>(N)->getValue(),
294 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000295 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000296 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000297 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000298 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000300 }
Chris Lattner3181a772006-01-29 06:26:56 +0000301 case ISD::TargetConstantFP: {
302 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
303 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
304 break;
305 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000306 case ISD::STRING:
307 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
308 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000309 case ISD::CONDCODE:
310 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
311 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000312 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000313 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
314 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000315 case ISD::GlobalAddress: {
316 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
317 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
318 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000319 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000320 }
321 case ISD::TargetGlobalAddress: {
322 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
323 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
324 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000325 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000326 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000327 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000328 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000329 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000330 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000331 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000332 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000333 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000334 Erased = ConstantPoolIndices.
335 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000336 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
337 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000338 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000339 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000340 Erased = TargetConstantPoolIndices.
341 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000342 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
343 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000344 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000345 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 break;
348 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000349 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000350 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000351 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000352 Erased =
353 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000354 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000355 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000356 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000357 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
358 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000359 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000360 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
361 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000362 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000363 case ISD::SRCVALUE: {
364 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000365 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000366 break;
367 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000368 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 Erased = Loads.erase(std::make_pair(N->getOperand(1),
370 std::make_pair(N->getOperand(0),
371 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000372 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000373 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000374 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000375 if (N->getNumOperands() == 0) {
376 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
377 N->getValueType(0)));
378 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000379 Erased =
380 UnaryOps.erase(std::make_pair(N->getOpcode(),
381 std::make_pair(N->getOperand(0),
382 N->getValueType(0))));
383 } else if (N->getNumOperands() == 2) {
384 Erased =
385 BinaryOps.erase(std::make_pair(N->getOpcode(),
386 std::make_pair(N->getOperand(0),
387 N->getOperand(1))));
388 } else {
389 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
390 Erased =
391 OneResultNodes.erase(std::make_pair(N->getOpcode(),
392 std::make_pair(N->getValueType(0),
393 Ops)));
394 }
Chris Lattner385328c2005-05-14 07:42:29 +0000395 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000396 // Remove the node from the ArbitraryNodes map.
397 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
398 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000399 Erased =
400 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
401 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000402 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000403 break;
404 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000405#ifndef NDEBUG
406 // Verify that the node was actually in one of the CSE maps, unless it has a
407 // flag result (which cannot be CSE'd) or is one of the special cases that are
408 // not subject to CSE.
409 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000410 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000411 N->dump();
412 assert(0 && "Node is not in map!");
413 }
414#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000415}
416
Chris Lattner8b8749f2005-08-17 19:00:20 +0000417/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
418/// has been taken out and modified in some way. If the specified node already
419/// exists in the CSE maps, do not modify the maps, but return the existing node
420/// instead. If it doesn't exist, add it and return null.
421///
422SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
423 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000424 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000425 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000426
Chris Lattner9f8cc692005-12-19 22:21:21 +0000427 // Check that remaining values produced are not flags.
428 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
429 if (N->getValueType(i) == MVT::Flag)
430 return 0; // Never CSE anything that produces a flag.
431
Chris Lattnerfe14b342005-12-01 23:14:50 +0000432 if (N->getNumValues() == 1) {
433 if (N->getNumOperands() == 1) {
434 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
435 std::make_pair(N->getOperand(0),
436 N->getValueType(0)))];
437 if (U) return U;
438 U = N;
439 } else if (N->getNumOperands() == 2) {
440 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
441 std::make_pair(N->getOperand(0),
442 N->getOperand(1)))];
443 if (B) return B;
444 B = N;
445 } else {
446 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
447 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000448 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000449 if (ORN) return ORN;
450 ORN = N;
451 }
452 } else {
453 if (N->getOpcode() == ISD::LOAD) {
454 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
455 std::make_pair(N->getOperand(0),
456 N->getValueType(0)))];
457 if (L) return L;
458 L = N;
459 } else {
460 // Remove the node from the ArbitraryNodes map.
461 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
462 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
463 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
464 std::make_pair(RV, Ops))];
465 if (AN) return AN;
466 AN = N;
467 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000468 }
469 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000470}
471
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000472/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
473/// were replaced with those specified. If this node is never memoized,
474/// return null, otherwise return a pointer to the slot it would take. If a
475/// node already exists with these operands, the slot will be non-null.
476SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000477 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000478 return 0; // Never add these nodes.
479
480 // Check that remaining values produced are not flags.
481 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
482 if (N->getValueType(i) == MVT::Flag)
483 return 0; // Never CSE anything that produces a flag.
484
485 if (N->getNumValues() == 1) {
486 return &UnaryOps[std::make_pair(N->getOpcode(),
487 std::make_pair(Op, N->getValueType(0)))];
488 } else {
489 // Remove the node from the ArbitraryNodes map.
490 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
491 std::vector<SDOperand> Ops;
492 Ops.push_back(Op);
493 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
494 std::make_pair(RV, Ops))];
495 }
496 return 0;
497}
498
499/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
500/// were replaced with those specified. If this node is never memoized,
501/// return null, otherwise return a pointer to the slot it would take. If a
502/// node already exists with these operands, the slot will be non-null.
503SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
504 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000505 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000506 return 0; // Never add these nodes.
507
508 // Check that remaining values produced are not flags.
509 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
510 if (N->getValueType(i) == MVT::Flag)
511 return 0; // Never CSE anything that produces a flag.
512
513 if (N->getNumValues() == 1) {
514 return &BinaryOps[std::make_pair(N->getOpcode(),
515 std::make_pair(Op1, Op2))];
516 } else {
517 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
518 std::vector<SDOperand> Ops;
519 Ops.push_back(Op1);
520 Ops.push_back(Op2);
521 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
522 std::make_pair(RV, Ops))];
523 }
524 return 0;
525}
526
527
528/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
529/// were replaced with those specified. If this node is never memoized,
530/// return null, otherwise return a pointer to the slot it would take. If a
531/// node already exists with these operands, the slot will be non-null.
532SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
533 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000534 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000535 return 0; // Never add these nodes.
536
537 // Check that remaining values produced are not flags.
538 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
539 if (N->getValueType(i) == MVT::Flag)
540 return 0; // Never CSE anything that produces a flag.
541
542 if (N->getNumValues() == 1) {
543 if (N->getNumOperands() == 1) {
544 return &UnaryOps[std::make_pair(N->getOpcode(),
545 std::make_pair(Ops[0],
546 N->getValueType(0)))];
547 } else if (N->getNumOperands() == 2) {
548 return &BinaryOps[std::make_pair(N->getOpcode(),
549 std::make_pair(Ops[0], Ops[1]))];
550 } else {
551 return &OneResultNodes[std::make_pair(N->getOpcode(),
552 std::make_pair(N->getValueType(0),
553 Ops))];
554 }
555 } else {
556 if (N->getOpcode() == ISD::LOAD) {
557 return &Loads[std::make_pair(Ops[1],
558 std::make_pair(Ops[0], N->getValueType(0)))];
559 } else {
560 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
561 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
562 std::make_pair(RV, Ops))];
563 }
564 }
565 return 0;
566}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000567
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000568
Chris Lattner78ec3112003-08-11 14:57:33 +0000569SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000570 while (!AllNodes.empty()) {
571 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000572 delete [] N->OperandList;
573 N->OperandList = 0;
574 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000575 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000576 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000577}
578
Chris Lattner0f2287b2005-04-13 02:38:18 +0000579SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000580 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000581 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000582 return getNode(ISD::AND, Op.getValueType(), Op,
583 getConstant(Imm, Op.getValueType()));
584}
585
Chris Lattnerc3aae252005-01-07 07:46:32 +0000586SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
587 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
588 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000589 if (VT != MVT::i64)
590 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000591
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 SDNode *&N = Constants[std::make_pair(Val, VT)];
593 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000594 N = new ConstantSDNode(false, Val, VT);
595 AllNodes.push_back(N);
596 return SDOperand(N, 0);
597}
598
Chris Lattner36ce6912005-11-29 06:21:05 +0000599SDOperand SelectionDAG::getString(const std::string &Val) {
600 StringSDNode *&N = StringNodes[Val];
601 if (!N) {
602 N = new StringSDNode(Val);
603 AllNodes.push_back(N);
604 }
605 return SDOperand(N, 0);
606}
607
Chris Lattner37bfbb42005-08-17 00:34:06 +0000608SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
609 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
610 // Mask out any bits that are not valid for this constant.
611 if (VT != MVT::i64)
612 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
613
614 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
615 if (N) return SDOperand(N, 0);
616 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617 AllNodes.push_back(N);
618 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000619}
620
Chris Lattnerc3aae252005-01-07 07:46:32 +0000621SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
622 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
623 if (VT == MVT::f32)
624 Val = (float)Val; // Mask out extra precision.
625
Chris Lattnerd8658612005-02-17 20:17:32 +0000626 // Do the map lookup using the actual bit pattern for the floating point
627 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
628 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000629 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000630 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000631 N = new ConstantFPSDNode(false, Val, VT);
632 AllNodes.push_back(N);
633 return SDOperand(N, 0);
634}
635
636SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
637 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
638 if (VT == MVT::f32)
639 Val = (float)Val; // Mask out extra precision.
640
641 // Do the map lookup using the actual bit pattern for the floating point
642 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
643 // we don't have issues with SNANs.
644 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
645 if (N) return SDOperand(N, 0);
646 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000647 AllNodes.push_back(N);
648 return SDOperand(N, 0);
649}
650
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000652 MVT::ValueType VT, int offset) {
653 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000654 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000655 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000656 AllNodes.push_back(N);
657 return SDOperand(N, 0);
658}
659
660SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000661 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000662 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000663 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000664 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000665 AllNodes.push_back(N);
666 return SDOperand(N, 0);
667}
668
669SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
670 SDNode *&N = FrameIndices[FI];
671 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000672 N = new FrameIndexSDNode(FI, VT, false);
673 AllNodes.push_back(N);
674 return SDOperand(N, 0);
675}
676
677SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
678 SDNode *&N = TargetFrameIndices[FI];
679 if (N) return SDOperand(N, 0);
680 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000681 AllNodes.push_back(N);
682 return SDOperand(N, 0);
683}
684
Evan Chengb8973bd2006-01-31 22:23:14 +0000685SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000686 unsigned Alignment, int Offset) {
687 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
688 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000690 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000691 AllNodes.push_back(N);
692 return SDOperand(N, 0);
693}
694
Evan Chengb8973bd2006-01-31 22:23:14 +0000695SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000696 unsigned Alignment, int Offset) {
697 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
698 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000699 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000700 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000701 AllNodes.push_back(N);
702 return SDOperand(N, 0);
703}
704
705SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
706 SDNode *&N = BBNodes[MBB];
707 if (N) return SDOperand(N, 0);
708 N = new BasicBlockSDNode(MBB);
709 AllNodes.push_back(N);
710 return SDOperand(N, 0);
711}
712
Chris Lattner15e4b012005-07-10 00:07:11 +0000713SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
714 if ((unsigned)VT >= ValueTypeNodes.size())
715 ValueTypeNodes.resize(VT+1);
716 if (ValueTypeNodes[VT] == 0) {
717 ValueTypeNodes[VT] = new VTSDNode(VT);
718 AllNodes.push_back(ValueTypeNodes[VT]);
719 }
720
721 return SDOperand(ValueTypeNodes[VT], 0);
722}
723
Chris Lattnerc3aae252005-01-07 07:46:32 +0000724SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
725 SDNode *&N = ExternalSymbols[Sym];
726 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000727 N = new ExternalSymbolSDNode(false, Sym, VT);
728 AllNodes.push_back(N);
729 return SDOperand(N, 0);
730}
731
Chris Lattner809ec112006-01-28 10:09:25 +0000732SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
733 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000734 SDNode *&N = TargetExternalSymbols[Sym];
735 if (N) return SDOperand(N, 0);
736 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 AllNodes.push_back(N);
738 return SDOperand(N, 0);
739}
740
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000741SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
742 if ((unsigned)Cond >= CondCodeNodes.size())
743 CondCodeNodes.resize(Cond+1);
744
Chris Lattner079a27a2005-08-09 20:40:02 +0000745 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000746 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000747 AllNodes.push_back(CondCodeNodes[Cond]);
748 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000749 return SDOperand(CondCodeNodes[Cond], 0);
750}
751
Chris Lattner0fdd7682005-08-30 22:38:38 +0000752SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
753 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
754 if (!Reg) {
755 Reg = new RegisterSDNode(RegNo, VT);
756 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000757 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000758 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000759}
760
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000761SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
762 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763 // These setcc operations always fold.
764 switch (Cond) {
765 default: break;
766 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000767 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000768 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000769 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000770 }
771
Chris Lattner67255a12005-04-07 18:14:58 +0000772 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
773 uint64_t C2 = N2C->getValue();
774 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
775 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000776
Chris Lattnerc3aae252005-01-07 07:46:32 +0000777 // Sign extend the operands if required
778 if (ISD::isSignedIntSetCC(Cond)) {
779 C1 = N1C->getSignExtended();
780 C2 = N2C->getSignExtended();
781 }
782
783 switch (Cond) {
784 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000785 case ISD::SETEQ: return getConstant(C1 == C2, VT);
786 case ISD::SETNE: return getConstant(C1 != C2, VT);
787 case ISD::SETULT: return getConstant(C1 < C2, VT);
788 case ISD::SETUGT: return getConstant(C1 > C2, VT);
789 case ISD::SETULE: return getConstant(C1 <= C2, VT);
790 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
791 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
792 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
793 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
794 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000795 }
Chris Lattner24673922005-04-07 18:58:54 +0000796 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000797 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000798 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
799 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
800
801 // If the comparison constant has bits in the upper part, the
802 // zero-extended value could never match.
803 if (C2 & (~0ULL << InSize)) {
804 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
805 switch (Cond) {
806 case ISD::SETUGT:
807 case ISD::SETUGE:
808 case ISD::SETEQ: return getConstant(0, VT);
809 case ISD::SETULT:
810 case ISD::SETULE:
811 case ISD::SETNE: return getConstant(1, VT);
812 case ISD::SETGT:
813 case ISD::SETGE:
814 // True if the sign bit of C2 is set.
815 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
816 case ISD::SETLT:
817 case ISD::SETLE:
818 // True if the sign bit of C2 isn't set.
819 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
820 default:
821 break;
822 }
823 }
824
825 // Otherwise, we can perform the comparison with the low bits.
826 switch (Cond) {
827 case ISD::SETEQ:
828 case ISD::SETNE:
829 case ISD::SETUGT:
830 case ISD::SETUGE:
831 case ISD::SETULT:
832 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000833 return getSetCC(VT, N1.getOperand(0),
834 getConstant(C2, N1.getOperand(0).getValueType()),
835 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000836 default:
837 break; // todo, be more careful with signed comparisons
838 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000839 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
840 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
841 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
842 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
843 MVT::ValueType ExtDstTy = N1.getValueType();
844 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000845
Chris Lattner7b2880c2005-08-24 22:44:39 +0000846 // If the extended part has any inconsistent bits, it cannot ever
847 // compare equal. In other words, they have to be all ones or all
848 // zeros.
849 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000850 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000851 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
852 return getConstant(Cond == ISD::SETNE, VT);
853
854 // Otherwise, make this a use of a zext.
855 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000856 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000857 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000858 }
859
Chris Lattner67255a12005-04-07 18:14:58 +0000860 uint64_t MinVal, MaxVal;
861 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
862 if (ISD::isSignedIntSetCC(Cond)) {
863 MinVal = 1ULL << (OperandBitSize-1);
864 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
865 MaxVal = ~0ULL >> (65-OperandBitSize);
866 else
867 MaxVal = 0;
868 } else {
869 MinVal = 0;
870 MaxVal = ~0ULL >> (64-OperandBitSize);
871 }
872
873 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
874 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
875 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
876 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000877 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
878 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000879 }
880
881 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
882 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
883 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000884 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
885 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000886 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000887
Nate Begeman72ea2812005-04-14 08:56:52 +0000888 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
889 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000890
Nate Begeman72ea2812005-04-14 08:56:52 +0000891 // Canonicalize setgt X, Min --> setne X, Min
892 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000893 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000894
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000895 // If we have setult X, 1, turn it into seteq X, 0
896 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000897 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
898 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000899 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000900 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000901 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
902 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000903
904 // If we have "setcc X, C1", check to see if we can shrink the immediate
905 // by changing cc.
906
907 // SETUGT X, SINTMAX -> SETLT X, 0
908 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
909 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000910 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000911
912 // FIXME: Implement the rest of these.
913
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000914
915 // Fold bit comparisons when we can.
916 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
917 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
918 if (ConstantSDNode *AndRHS =
919 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
920 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
921 // Perform the xform if the AND RHS is a single bit.
922 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
923 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000924 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000925 TLI.getShiftAmountTy()));
926 }
927 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
928 // (X & 8) == 8 --> (X & 8) >> 3
929 // Perform the xform if C2 is a single bit.
930 if ((C2 & (C2-1)) == 0) {
931 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000932 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000933 }
934 }
935 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936 }
Chris Lattner67255a12005-04-07 18:14:58 +0000937 } else if (isa<ConstantSDNode>(N1.Val)) {
938 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000939 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000940 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941
942 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
943 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
944 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000945
Chris Lattnerc3aae252005-01-07 07:46:32 +0000946 switch (Cond) {
947 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000948 case ISD::SETEQ: return getConstant(C1 == C2, VT);
949 case ISD::SETNE: return getConstant(C1 != C2, VT);
950 case ISD::SETLT: return getConstant(C1 < C2, VT);
951 case ISD::SETGT: return getConstant(C1 > C2, VT);
952 case ISD::SETLE: return getConstant(C1 <= C2, VT);
953 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954 }
955 } else {
956 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000957 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 }
959
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000960 // Could not fold it.
961 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000962}
963
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000965///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000966SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000967 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
968 if (!N) {
969 N = new SDNode(Opcode, VT);
970 AllNodes.push_back(N);
971 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000972 return SDOperand(N, 0);
973}
974
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
976 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000977 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000978 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000979 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
980 uint64_t Val = C->getValue();
981 switch (Opcode) {
982 default: break;
983 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000984 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
986 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000987 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
988 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000989 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000990 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +0000991 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +0000992 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +0000993 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000994 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000995 case ISD::BSWAP:
996 switch(VT) {
997 default: assert(0 && "Invalid bswap!"); break;
998 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
999 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1000 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1001 }
1002 break;
1003 case ISD::CTPOP:
1004 switch(VT) {
1005 default: assert(0 && "Invalid ctpop!"); break;
1006 case MVT::i1: return getConstant(Val != 0, VT);
1007 case MVT::i8:
1008 Tmp1 = (unsigned)Val & 0xFF;
1009 return getConstant(CountPopulation_32(Tmp1), VT);
1010 case MVT::i16:
1011 Tmp1 = (unsigned)Val & 0xFFFF;
1012 return getConstant(CountPopulation_32(Tmp1), VT);
1013 case MVT::i32:
1014 return getConstant(CountPopulation_32((unsigned)Val), VT);
1015 case MVT::i64:
1016 return getConstant(CountPopulation_64(Val), VT);
1017 }
1018 case ISD::CTLZ:
1019 switch(VT) {
1020 default: assert(0 && "Invalid ctlz!"); break;
1021 case MVT::i1: return getConstant(Val == 0, VT);
1022 case MVT::i8:
1023 Tmp1 = (unsigned)Val & 0xFF;
1024 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1025 case MVT::i16:
1026 Tmp1 = (unsigned)Val & 0xFFFF;
1027 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1028 case MVT::i32:
1029 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1030 case MVT::i64:
1031 return getConstant(CountLeadingZeros_64(Val), VT);
1032 }
1033 case ISD::CTTZ:
1034 switch(VT) {
1035 default: assert(0 && "Invalid cttz!"); break;
1036 case MVT::i1: return getConstant(Val == 0, VT);
1037 case MVT::i8:
1038 Tmp1 = (unsigned)Val | 0x100;
1039 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1040 case MVT::i16:
1041 Tmp1 = (unsigned)Val | 0x10000;
1042 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1043 case MVT::i32:
1044 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1045 case MVT::i64:
1046 return getConstant(CountTrailingZeros_64(Val), VT);
1047 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048 }
1049 }
1050
Chris Lattner94683772005-12-23 05:30:37 +00001051 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1053 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001054 case ISD::FNEG:
1055 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001056 case ISD::FABS:
1057 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058 case ISD::FP_ROUND:
1059 case ISD::FP_EXTEND:
1060 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001061 case ISD::FP_TO_SINT:
1062 return getConstant((int64_t)C->getValue(), VT);
1063 case ISD::FP_TO_UINT:
1064 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001065 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001066 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001067 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001068 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001069 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001070 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 }
1072
1073 unsigned OpOpcode = Operand.Val->getOpcode();
1074 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001075 case ISD::TokenFactor:
1076 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 case ISD::SIGN_EXTEND:
1078 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001079 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1081 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1082 break;
1083 case ISD::ZERO_EXTEND:
1084 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001085 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001086 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001087 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001089 case ISD::ANY_EXTEND:
1090 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001091 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001092 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1093 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1094 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1095 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 case ISD::TRUNCATE:
1097 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001098 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001099 if (OpOpcode == ISD::TRUNCATE)
1100 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001101 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1102 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001103 // If the source is smaller than the dest, we still need an extend.
1104 if (Operand.Val->getOperand(0).getValueType() < VT)
1105 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1106 else if (Operand.Val->getOperand(0).getValueType() > VT)
1107 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1108 else
1109 return Operand.Val->getOperand(0);
1110 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111 break;
Chris Lattner35481892005-12-23 00:16:34 +00001112 case ISD::BIT_CONVERT:
1113 // Basic sanity checking.
Chris Lattner1c6191f2006-03-21 19:20:37 +00001114 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1115 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001116 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001117 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001118 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1119 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001120 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001121 case ISD::SCALAR_TO_VECTOR:
1122 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1123 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1124 "Illegal SCALAR_TO_VECTOR node!");
1125 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001126 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001127 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1128 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001129 Operand.Val->getOperand(0));
1130 if (OpOpcode == ISD::FNEG) // --X -> X
1131 return Operand.Val->getOperand(0);
1132 break;
1133 case ISD::FABS:
1134 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1135 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1136 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 }
1138
Chris Lattner43247a12005-08-25 19:12:10 +00001139 SDNode *N;
1140 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1141 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001142 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001143 E = N = new SDNode(Opcode, Operand);
1144 } else {
1145 N = new SDNode(Opcode, Operand);
1146 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 N->setValueTypes(VT);
1148 AllNodes.push_back(N);
1149 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001150}
1151
Chris Lattner36019aa2005-04-18 03:48:41 +00001152
1153
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1155 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001156#ifndef NDEBUG
1157 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001158 case ISD::TokenFactor:
1159 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1160 N2.getValueType() == MVT::Other && "Invalid token factor!");
1161 break;
Chris Lattner76365122005-01-16 02:23:22 +00001162 case ISD::AND:
1163 case ISD::OR:
1164 case ISD::XOR:
1165 case ISD::UDIV:
1166 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001167 case ISD::MULHU:
1168 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001169 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1170 // fall through
1171 case ISD::ADD:
1172 case ISD::SUB:
1173 case ISD::MUL:
1174 case ISD::SDIV:
1175 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001176 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1177 // fall through.
1178 case ISD::FADD:
1179 case ISD::FSUB:
1180 case ISD::FMUL:
1181 case ISD::FDIV:
1182 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001183 assert(N1.getValueType() == N2.getValueType() &&
1184 N1.getValueType() == VT && "Binary operator types must match!");
1185 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001186 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1187 assert(N1.getValueType() == VT &&
1188 MVT::isFloatingPoint(N1.getValueType()) &&
1189 MVT::isFloatingPoint(N2.getValueType()) &&
1190 "Invalid FCOPYSIGN!");
1191 break;
Chris Lattner76365122005-01-16 02:23:22 +00001192 case ISD::SHL:
1193 case ISD::SRA:
1194 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001195 case ISD::ROTL:
1196 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001197 assert(VT == N1.getValueType() &&
1198 "Shift operators return type must be the same as their first arg");
1199 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001200 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001201 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001202 case ISD::FP_ROUND_INREG: {
1203 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1204 assert(VT == N1.getValueType() && "Not an inreg round!");
1205 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1206 "Cannot FP_ROUND_INREG integer types");
1207 assert(EVT <= VT && "Not rounding down!");
1208 break;
1209 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001210 case ISD::AssertSext:
1211 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001212 case ISD::SIGN_EXTEND_INREG: {
1213 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1214 assert(VT == N1.getValueType() && "Not an inreg extend!");
1215 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1216 "Cannot *_EXTEND_INREG FP types");
1217 assert(EVT <= VT && "Not extending!");
1218 }
1219
Chris Lattner76365122005-01-16 02:23:22 +00001220 default: break;
1221 }
1222#endif
1223
Chris Lattnerc3aae252005-01-07 07:46:32 +00001224 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1225 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1226 if (N1C) {
1227 if (N2C) {
1228 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1229 switch (Opcode) {
1230 case ISD::ADD: return getConstant(C1 + C2, VT);
1231 case ISD::SUB: return getConstant(C1 - C2, VT);
1232 case ISD::MUL: return getConstant(C1 * C2, VT);
1233 case ISD::UDIV:
1234 if (C2) return getConstant(C1 / C2, VT);
1235 break;
1236 case ISD::UREM :
1237 if (C2) return getConstant(C1 % C2, VT);
1238 break;
1239 case ISD::SDIV :
1240 if (C2) return getConstant(N1C->getSignExtended() /
1241 N2C->getSignExtended(), VT);
1242 break;
1243 case ISD::SREM :
1244 if (C2) return getConstant(N1C->getSignExtended() %
1245 N2C->getSignExtended(), VT);
1246 break;
1247 case ISD::AND : return getConstant(C1 & C2, VT);
1248 case ISD::OR : return getConstant(C1 | C2, VT);
1249 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001250 case ISD::SHL : return getConstant(C1 << C2, VT);
1251 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001252 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001253 case ISD::ROTL :
1254 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1255 VT);
1256 case ISD::ROTR :
1257 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1258 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001259 default: break;
1260 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261 } else { // Cannonicalize constant to RHS if commutative
1262 if (isCommutativeBinOp(Opcode)) {
1263 std::swap(N1C, N2C);
1264 std::swap(N1, N2);
1265 }
1266 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001267 }
1268
1269 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1270 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001271 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001272 if (N2CFP) {
1273 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1274 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001275 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1276 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1277 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1278 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001279 if (C2) return getConstantFP(C1 / C2, VT);
1280 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001281 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001282 if (C2) return getConstantFP(fmod(C1, C2), VT);
1283 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001284 case ISD::FCOPYSIGN: {
1285 union {
1286 double F;
1287 uint64_t I;
1288 } u1;
1289 union {
1290 double F;
1291 int64_t I;
1292 } u2;
1293 u1.F = C1;
1294 u2.F = C2;
1295 if (u2.I < 0) // Sign bit of RHS set?
1296 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1297 else
1298 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1299 return getConstantFP(u1.F, VT);
1300 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 default: break;
1302 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001303 } else { // Cannonicalize constant to RHS if commutative
1304 if (isCommutativeBinOp(Opcode)) {
1305 std::swap(N1CFP, N2CFP);
1306 std::swap(N1, N2);
1307 }
1308 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001309 }
1310
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 // Finally, fold operations that do not require constants.
1312 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001313 case ISD::FP_ROUND_INREG:
1314 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1315 break;
1316 case ISD::SIGN_EXTEND_INREG: {
1317 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1318 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001319 break;
1320 }
1321
Nate Begemaneea805e2005-04-13 21:23:31 +00001322 // FIXME: figure out how to safely handle things like
1323 // int foo(int x) { return 1 << (x & 255); }
1324 // int bar() { return foo(256); }
1325#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001326 case ISD::SHL:
1327 case ISD::SRL:
1328 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001329 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001330 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001331 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001332 else if (N2.getOpcode() == ISD::AND)
1333 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1334 // If the and is only masking out bits that cannot effect the shift,
1335 // eliminate the and.
1336 unsigned NumBits = MVT::getSizeInBits(VT);
1337 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1338 return getNode(Opcode, VT, N1, N2.getOperand(0));
1339 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001340 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001341#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342 }
1343
Chris Lattner27e9b412005-05-11 18:57:39 +00001344 // Memoize this node if possible.
1345 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001346 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001347 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1348 if (BON) return SDOperand(BON, 0);
1349
1350 BON = N = new SDNode(Opcode, N1, N2);
1351 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001352 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001353 }
1354
Chris Lattner3e011362005-05-14 07:45:46 +00001355 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001356 AllNodes.push_back(N);
1357 return SDOperand(N, 0);
1358}
1359
Chris Lattnerc3aae252005-01-07 07:46:32 +00001360SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1361 SDOperand N1, SDOperand N2, SDOperand N3) {
1362 // Perform various simplifications.
1363 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1364 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1365 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1366 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001367 case ISD::SETCC: {
1368 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001369 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001370 if (Simp.Val) return Simp;
1371 break;
1372 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001373 case ISD::SELECT:
1374 if (N1C)
1375 if (N1C->getValue())
1376 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001377 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001378 return N3; // select false, X, Y -> Y
1379
1380 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001382 case ISD::BRCOND:
1383 if (N2C)
1384 if (N2C->getValue()) // Unconditional branch
1385 return getNode(ISD::BR, MVT::Other, N1, N3);
1386 else
1387 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001388 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001389 case ISD::VECTOR_SHUFFLE:
1390 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1391 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1392 N3.getOpcode() == ISD::BUILD_VECTOR &&
1393 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1394 "Illegal VECTOR_SHUFFLE node!");
1395 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001396 }
1397
Chris Lattner385328c2005-05-14 07:42:29 +00001398 std::vector<SDOperand> Ops;
1399 Ops.reserve(3);
1400 Ops.push_back(N1);
1401 Ops.push_back(N2);
1402 Ops.push_back(N3);
1403
Chris Lattner43247a12005-08-25 19:12:10 +00001404 // Memoize node if it doesn't produce a flag.
1405 SDNode *N;
1406 if (VT != MVT::Flag) {
1407 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1408 if (E) return SDOperand(E, 0);
1409 E = N = new SDNode(Opcode, N1, N2, N3);
1410 } else {
1411 N = new SDNode(Opcode, N1, N2, N3);
1412 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001413 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001414 AllNodes.push_back(N);
1415 return SDOperand(N, 0);
1416}
1417
1418SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001419 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001420 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001421 std::vector<SDOperand> Ops;
1422 Ops.reserve(4);
1423 Ops.push_back(N1);
1424 Ops.push_back(N2);
1425 Ops.push_back(N3);
1426 Ops.push_back(N4);
1427 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001428}
1429
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001430SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1431 SDOperand N1, SDOperand N2, SDOperand N3,
1432 SDOperand N4, SDOperand N5) {
1433 std::vector<SDOperand> Ops;
1434 Ops.reserve(5);
1435 Ops.push_back(N1);
1436 Ops.push_back(N2);
1437 Ops.push_back(N3);
1438 Ops.push_back(N4);
1439 Ops.push_back(N5);
1440 return getNode(Opcode, VT, Ops);
1441}
1442
Evan Cheng7038daf2005-12-10 00:37:58 +00001443SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1444 SDOperand Chain, SDOperand Ptr,
1445 SDOperand SV) {
1446 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1447 if (N) return SDOperand(N, 0);
1448 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1449
1450 // Loads have a token chain.
1451 setNodeValueTypes(N, VT, MVT::Other);
1452 AllNodes.push_back(N);
1453 return SDOperand(N, 0);
1454}
1455
1456SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1457 SDOperand Chain, SDOperand Ptr,
1458 SDOperand SV) {
1459 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1460 if (N) return SDOperand(N, 0);
1461 std::vector<SDOperand> Ops;
1462 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001463 Ops.push_back(Chain);
1464 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001465 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001466 Ops.push_back(getConstant(Count, MVT::i32));
1467 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001468 std::vector<MVT::ValueType> VTs;
1469 VTs.reserve(2);
1470 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1471 return getNode(ISD::VLOAD, VTs, Ops);
1472}
1473
1474SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1475 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1476 MVT::ValueType EVT) {
1477 std::vector<SDOperand> Ops;
1478 Ops.reserve(4);
1479 Ops.push_back(Chain);
1480 Ops.push_back(Ptr);
1481 Ops.push_back(SV);
1482 Ops.push_back(getValueType(EVT));
1483 std::vector<MVT::ValueType> VTs;
1484 VTs.reserve(2);
1485 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1486 return getNode(Opcode, VTs, Ops);
1487}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001488
Chris Lattner0437cdd2005-05-09 04:14:13 +00001489SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001490 assert((!V || isa<PointerType>(V->getType())) &&
1491 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001492 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1493 if (N) return SDOperand(N, 0);
1494
1495 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001496 AllNodes.push_back(N);
1497 return SDOperand(N, 0);
1498}
1499
Nate Begemanacc398c2006-01-25 18:21:52 +00001500SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1501 SDOperand Chain, SDOperand Ptr,
1502 SDOperand SV) {
1503 std::vector<SDOperand> Ops;
1504 Ops.reserve(3);
1505 Ops.push_back(Chain);
1506 Ops.push_back(Ptr);
1507 Ops.push_back(SV);
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(ISD::VAARG, VTs, Ops);
1512}
1513
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001514SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001515 std::vector<SDOperand> &Ops) {
1516 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001517 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001518 case 1: return getNode(Opcode, VT, Ops[0]);
1519 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1520 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001521 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001522 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001523
Chris Lattner89c34632005-05-14 06:20:26 +00001524 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001525 switch (Opcode) {
1526 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001527 case ISD::TRUNCSTORE: {
1528 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1529 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1530#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1531 // If this is a truncating store of a constant, convert to the desired type
1532 // and store it instead.
1533 if (isa<Constant>(Ops[0])) {
1534 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1535 if (isa<Constant>(Op))
1536 N1 = Op;
1537 }
1538 // Also for ConstantFP?
1539#endif
1540 if (Ops[0].getValueType() == EVT) // Normal store?
1541 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1542 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1543 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1544 "Can't do FP-INT conversion!");
1545 break;
1546 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001547 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001548 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001549 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1550 "LHS and RHS of condition must have same type!");
1551 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1552 "True and False arms of SelectCC must have same type!");
1553 assert(Ops[2].getValueType() == VT &&
1554 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001555 break;
1556 }
1557 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001558 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001559 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1560 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001561 break;
1562 }
Chris Lattneref847df2005-04-09 03:27:28 +00001563 }
1564
Chris Lattner385328c2005-05-14 07:42:29 +00001565 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001566 SDNode *N;
1567 if (VT != MVT::Flag) {
1568 SDNode *&E =
1569 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1570 if (E) return SDOperand(E, 0);
1571 E = N = new SDNode(Opcode, Ops);
1572 } else {
1573 N = new SDNode(Opcode, Ops);
1574 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001575 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001576 AllNodes.push_back(N);
1577 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001578}
1579
Chris Lattner89c34632005-05-14 06:20:26 +00001580SDOperand SelectionDAG::getNode(unsigned Opcode,
1581 std::vector<MVT::ValueType> &ResultTys,
1582 std::vector<SDOperand> &Ops) {
1583 if (ResultTys.size() == 1)
1584 return getNode(Opcode, ResultTys[0], Ops);
1585
Chris Lattner5f056bf2005-07-10 01:55:33 +00001586 switch (Opcode) {
1587 case ISD::EXTLOAD:
1588 case ISD::SEXTLOAD:
1589 case ISD::ZEXTLOAD: {
1590 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1591 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1592 // If they are asking for an extending load from/to the same thing, return a
1593 // normal load.
1594 if (ResultTys[0] == EVT)
1595 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001596 if (MVT::isVector(ResultTys[0])) {
1597 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1598 "Invalid vector extload!");
1599 } else {
1600 assert(EVT < ResultTys[0] &&
1601 "Should only be an extending load, not truncating!");
1602 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001603 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001604 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001605 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1606 "Cannot convert from FP to Int or Int -> FP!");
1607 break;
1608 }
1609
Chris Lattnere89083a2005-05-14 07:25:05 +00001610 // FIXME: figure out how to safely handle things like
1611 // int foo(int x) { return 1 << (x & 255); }
1612 // int bar() { return foo(256); }
1613#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001614 case ISD::SRA_PARTS:
1615 case ISD::SRL_PARTS:
1616 case ISD::SHL_PARTS:
1617 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001618 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001619 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1620 else if (N3.getOpcode() == ISD::AND)
1621 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1622 // If the and is only masking out bits that cannot effect the shift,
1623 // eliminate the and.
1624 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1625 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1626 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1627 }
1628 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001629#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001630 }
Chris Lattner89c34632005-05-14 06:20:26 +00001631
Chris Lattner43247a12005-08-25 19:12:10 +00001632 // Memoize the node unless it returns a flag.
1633 SDNode *N;
1634 if (ResultTys.back() != MVT::Flag) {
1635 SDNode *&E =
1636 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1637 if (E) return SDOperand(E, 0);
1638 E = N = new SDNode(Opcode, Ops);
1639 } else {
1640 N = new SDNode(Opcode, Ops);
1641 }
Chris Lattnera3255112005-11-08 23:30:28 +00001642 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001643 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001644 return SDOperand(N, 0);
1645}
1646
Chris Lattnera3255112005-11-08 23:30:28 +00001647void SelectionDAG::setNodeValueTypes(SDNode *N,
1648 std::vector<MVT::ValueType> &RetVals) {
1649 switch (RetVals.size()) {
1650 case 0: return;
1651 case 1: N->setValueTypes(RetVals[0]); return;
1652 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1653 default: break;
1654 }
1655
1656 std::list<std::vector<MVT::ValueType> >::iterator I =
1657 std::find(VTList.begin(), VTList.end(), RetVals);
1658 if (I == VTList.end()) {
1659 VTList.push_front(RetVals);
1660 I = VTList.begin();
1661 }
1662
1663 N->setValueTypes(&(*I)[0], I->size());
1664}
1665
1666void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1667 MVT::ValueType VT2) {
1668 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1669 E = VTList.end(); I != E; ++I) {
1670 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1671 N->setValueTypes(&(*I)[0], 2);
1672 return;
1673 }
1674 }
1675 std::vector<MVT::ValueType> V;
1676 V.push_back(VT1);
1677 V.push_back(VT2);
1678 VTList.push_front(V);
1679 N->setValueTypes(&(*VTList.begin())[0], 2);
1680}
1681
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001682/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1683/// specified operands. If the resultant node already exists in the DAG,
1684/// this does not modify the specified node, instead it returns the node that
1685/// already exists. If the resultant node does not exist in the DAG, the
1686/// input node is returned. As a degenerate case, if you specify the same
1687/// input operands as the node already has, the input node is returned.
1688SDOperand SelectionDAG::
1689UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1690 SDNode *N = InN.Val;
1691 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1692
1693 // Check to see if there is no change.
1694 if (Op == N->getOperand(0)) return InN;
1695
1696 // See if the modified node already exists.
1697 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1698 if (NewSlot && *NewSlot)
1699 return SDOperand(*NewSlot, InN.ResNo);
1700
1701 // Nope it doesn't. Remove the node from it's current place in the maps.
1702 if (NewSlot)
1703 RemoveNodeFromCSEMaps(N);
1704
1705 // Now we update the operands.
1706 N->OperandList[0].Val->removeUser(N);
1707 Op.Val->addUser(N);
1708 N->OperandList[0] = Op;
1709
1710 // If this gets put into a CSE map, add it.
1711 if (NewSlot) *NewSlot = N;
1712 return InN;
1713}
1714
1715SDOperand SelectionDAG::
1716UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1717 SDNode *N = InN.Val;
1718 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1719
1720 // Check to see if there is no change.
1721 bool AnyChange = false;
1722 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1723 return InN; // No operands changed, just return the input node.
1724
1725 // See if the modified node already exists.
1726 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1727 if (NewSlot && *NewSlot)
1728 return SDOperand(*NewSlot, InN.ResNo);
1729
1730 // Nope it doesn't. Remove the node from it's current place in the maps.
1731 if (NewSlot)
1732 RemoveNodeFromCSEMaps(N);
1733
1734 // Now we update the operands.
1735 if (N->OperandList[0] != Op1) {
1736 N->OperandList[0].Val->removeUser(N);
1737 Op1.Val->addUser(N);
1738 N->OperandList[0] = Op1;
1739 }
1740 if (N->OperandList[1] != Op2) {
1741 N->OperandList[1].Val->removeUser(N);
1742 Op2.Val->addUser(N);
1743 N->OperandList[1] = Op2;
1744 }
1745
1746 // If this gets put into a CSE map, add it.
1747 if (NewSlot) *NewSlot = N;
1748 return InN;
1749}
1750
1751SDOperand SelectionDAG::
1752UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1753 std::vector<SDOperand> Ops;
1754 Ops.push_back(Op1);
1755 Ops.push_back(Op2);
1756 Ops.push_back(Op3);
1757 return UpdateNodeOperands(N, Ops);
1758}
1759
1760SDOperand SelectionDAG::
1761UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1762 SDOperand Op3, SDOperand Op4) {
1763 std::vector<SDOperand> Ops;
1764 Ops.push_back(Op1);
1765 Ops.push_back(Op2);
1766 Ops.push_back(Op3);
1767 Ops.push_back(Op4);
1768 return UpdateNodeOperands(N, Ops);
1769}
1770
1771SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001772UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1773 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1774 std::vector<SDOperand> Ops;
1775 Ops.push_back(Op1);
1776 Ops.push_back(Op2);
1777 Ops.push_back(Op3);
1778 Ops.push_back(Op4);
1779 Ops.push_back(Op5);
1780 return UpdateNodeOperands(N, Ops);
1781}
1782
1783
1784SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001785UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1786 SDNode *N = InN.Val;
1787 assert(N->getNumOperands() == Ops.size() &&
1788 "Update with wrong number of operands");
1789
1790 // Check to see if there is no change.
1791 unsigned NumOps = Ops.size();
1792 bool AnyChange = false;
1793 for (unsigned i = 0; i != NumOps; ++i) {
1794 if (Ops[i] != N->getOperand(i)) {
1795 AnyChange = true;
1796 break;
1797 }
1798 }
1799
1800 // No operands changed, just return the input node.
1801 if (!AnyChange) return InN;
1802
1803 // See if the modified node already exists.
1804 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1805 if (NewSlot && *NewSlot)
1806 return SDOperand(*NewSlot, InN.ResNo);
1807
1808 // Nope it doesn't. Remove the node from it's current place in the maps.
1809 if (NewSlot)
1810 RemoveNodeFromCSEMaps(N);
1811
1812 // Now we update the operands.
1813 for (unsigned i = 0; i != NumOps; ++i) {
1814 if (N->OperandList[i] != Ops[i]) {
1815 N->OperandList[i].Val->removeUser(N);
1816 Ops[i].Val->addUser(N);
1817 N->OperandList[i] = Ops[i];
1818 }
1819 }
1820
1821 // If this gets put into a CSE map, add it.
1822 if (NewSlot) *NewSlot = N;
1823 return InN;
1824}
1825
1826
1827
Chris Lattner149c58c2005-08-16 18:17:10 +00001828
1829/// SelectNodeTo - These are used for target selectors to *mutate* the
1830/// specified node to have the specified return type, Target opcode, and
1831/// operands. Note that target opcodes are stored as
1832/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001833///
1834/// Note that SelectNodeTo returns the resultant node. If there is already a
1835/// node of the specified opcode and operands, it returns that node instead of
1836/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001837SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1838 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001839 // If an identical node already exists, use it.
1840 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1841 if (ON) return SDOperand(ON, 0);
1842
Chris Lattner7651fa42005-08-24 23:00:29 +00001843 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001844
Chris Lattner7651fa42005-08-24 23:00:29 +00001845 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1846 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001847
1848 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001849 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001850}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001851
Chris Lattnereb19e402005-11-30 22:45:14 +00001852SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1853 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001854 // If an identical node already exists, use it.
1855 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1856 std::make_pair(Op1, VT))];
1857 if (ON) return SDOperand(ON, 0);
1858
Chris Lattner149c58c2005-08-16 18:17:10 +00001859 RemoveNodeFromCSEMaps(N);
1860 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1861 N->setValueTypes(VT);
1862 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001863
1864 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001865 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001866}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001867
Chris Lattnereb19e402005-11-30 22:45:14 +00001868SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1869 MVT::ValueType VT, SDOperand Op1,
1870 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001871 // If an identical node already exists, use it.
1872 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1873 std::make_pair(Op1, Op2))];
1874 if (ON) return SDOperand(ON, 0);
1875
Chris Lattner149c58c2005-08-16 18:17:10 +00001876 RemoveNodeFromCSEMaps(N);
1877 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1878 N->setValueTypes(VT);
1879 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001880
1881 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001882 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001883}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001884
Chris Lattnereb19e402005-11-30 22:45:14 +00001885SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1886 MVT::ValueType VT, SDOperand Op1,
1887 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001888 // If an identical node already exists, use it.
1889 std::vector<SDOperand> OpList;
1890 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1891 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1892 std::make_pair(VT, OpList))];
1893 if (ON) return SDOperand(ON, 0);
1894
Chris Lattner149c58c2005-08-16 18:17:10 +00001895 RemoveNodeFromCSEMaps(N);
1896 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1897 N->setValueTypes(VT);
1898 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001899
1900 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001901 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001902}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001903
Chris Lattnereb19e402005-11-30 22:45:14 +00001904SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1905 MVT::ValueType VT, SDOperand Op1,
1906 SDOperand Op2, SDOperand Op3,
1907 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001908 // If an identical node already exists, use it.
1909 std::vector<SDOperand> OpList;
1910 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1911 OpList.push_back(Op4);
1912 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1913 std::make_pair(VT, OpList))];
1914 if (ON) return SDOperand(ON, 0);
1915
Nate Begeman294a0a12005-08-18 07:30:15 +00001916 RemoveNodeFromCSEMaps(N);
1917 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1918 N->setValueTypes(VT);
1919 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001920
1921 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001922 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001923}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001924
Chris Lattnereb19e402005-11-30 22:45:14 +00001925SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1926 MVT::ValueType VT, SDOperand Op1,
1927 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1928 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001929 // If an identical node already exists, use it.
1930 std::vector<SDOperand> OpList;
1931 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1932 OpList.push_back(Op4); OpList.push_back(Op5);
1933 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1934 std::make_pair(VT, OpList))];
1935 if (ON) return SDOperand(ON, 0);
1936
Chris Lattner6b09a292005-08-21 18:49:33 +00001937 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1939 N->setValueTypes(VT);
1940 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001941
1942 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001943 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001944}
Chris Lattner149c58c2005-08-16 18:17:10 +00001945
Chris Lattnereb19e402005-11-30 22:45:14 +00001946SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1947 MVT::ValueType VT, SDOperand Op1,
1948 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1949 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001950 // If an identical node already exists, use it.
1951 std::vector<SDOperand> OpList;
1952 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1953 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1954 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1955 std::make_pair(VT, OpList))];
1956 if (ON) return SDOperand(ON, 0);
1957
Evan Cheng61ca74b2005-11-30 02:04:11 +00001958 RemoveNodeFromCSEMaps(N);
1959 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1960 N->setValueTypes(VT);
1961 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001962
1963 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001964 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001965}
1966
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001967SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1968 MVT::ValueType VT, SDOperand Op1,
1969 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1970 SDOperand Op5, SDOperand Op6,
1971 SDOperand Op7) {
1972 // If an identical node already exists, use it.
1973 std::vector<SDOperand> OpList;
1974 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1975 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1976 OpList.push_back(Op7);
1977 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1978 std::make_pair(VT, OpList))];
1979 if (ON) return SDOperand(ON, 0);
1980
1981 RemoveNodeFromCSEMaps(N);
1982 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1983 N->setValueTypes(VT);
1984 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1985
1986 ON = N; // Memoize the new node.
1987 return SDOperand(N, 0);
1988}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001989SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1990 MVT::ValueType VT, SDOperand Op1,
1991 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1992 SDOperand Op5, SDOperand Op6,
1993 SDOperand Op7, SDOperand Op8) {
1994 // If an identical node already exists, use it.
1995 std::vector<SDOperand> OpList;
1996 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1997 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1998 OpList.push_back(Op7); OpList.push_back(Op8);
1999 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2000 std::make_pair(VT, OpList))];
2001 if (ON) return SDOperand(ON, 0);
2002
2003 RemoveNodeFromCSEMaps(N);
2004 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2005 N->setValueTypes(VT);
2006 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2007
2008 ON = N; // Memoize the new node.
2009 return SDOperand(N, 0);
2010}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002011
Chris Lattnereb19e402005-11-30 22:45:14 +00002012SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2013 MVT::ValueType VT1, MVT::ValueType VT2,
2014 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002015 // If an identical node already exists, use it.
2016 std::vector<SDOperand> OpList;
2017 OpList.push_back(Op1); OpList.push_back(Op2);
2018 std::vector<MVT::ValueType> VTList;
2019 VTList.push_back(VT1); VTList.push_back(VT2);
2020 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2021 std::make_pair(VTList, OpList))];
2022 if (ON) return SDOperand(ON, 0);
2023
Chris Lattner0fb094f2005-11-19 01:44:53 +00002024 RemoveNodeFromCSEMaps(N);
2025 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2026 setNodeValueTypes(N, VT1, VT2);
2027 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002028
2029 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002030 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002031}
2032
Chris Lattnereb19e402005-11-30 22:45:14 +00002033SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2034 MVT::ValueType VT1, MVT::ValueType VT2,
2035 SDOperand Op1, SDOperand Op2,
2036 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002037 // If an identical node already exists, use it.
2038 std::vector<SDOperand> OpList;
2039 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2040 std::vector<MVT::ValueType> VTList;
2041 VTList.push_back(VT1); VTList.push_back(VT2);
2042 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2043 std::make_pair(VTList, OpList))];
2044 if (ON) return SDOperand(ON, 0);
2045
Chris Lattner0fb094f2005-11-19 01:44:53 +00002046 RemoveNodeFromCSEMaps(N);
2047 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2048 setNodeValueTypes(N, VT1, VT2);
2049 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002050
2051 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002052 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002053}
2054
Chris Lattnereb19e402005-11-30 22:45:14 +00002055SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2056 MVT::ValueType VT1, MVT::ValueType VT2,
2057 SDOperand Op1, SDOperand Op2,
2058 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002059 // If an identical node already exists, use it.
2060 std::vector<SDOperand> OpList;
2061 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2062 OpList.push_back(Op4);
2063 std::vector<MVT::ValueType> VTList;
2064 VTList.push_back(VT1); VTList.push_back(VT2);
2065 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2066 std::make_pair(VTList, OpList))];
2067 if (ON) return SDOperand(ON, 0);
2068
Chris Lattner0fb094f2005-11-19 01:44:53 +00002069 RemoveNodeFromCSEMaps(N);
2070 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2071 setNodeValueTypes(N, VT1, VT2);
2072 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002073
2074 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002075 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002076}
2077
Chris Lattnereb19e402005-11-30 22:45:14 +00002078SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2079 MVT::ValueType VT1, MVT::ValueType VT2,
2080 SDOperand Op1, SDOperand Op2,
2081 SDOperand Op3, SDOperand Op4,
2082 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002083 // If an identical node already exists, use it.
2084 std::vector<SDOperand> OpList;
2085 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2086 OpList.push_back(Op4); OpList.push_back(Op5);
2087 std::vector<MVT::ValueType> VTList;
2088 VTList.push_back(VT1); VTList.push_back(VT2);
2089 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2090 std::make_pair(VTList, OpList))];
2091 if (ON) return SDOperand(ON, 0);
2092
Chris Lattner0fb094f2005-11-19 01:44:53 +00002093 RemoveNodeFromCSEMaps(N);
2094 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2095 setNodeValueTypes(N, VT1, VT2);
2096 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002097
2098 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002099 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002100}
2101
Evan Cheng6ae46c42006-02-09 07:15:23 +00002102/// getTargetNode - These are used for target selectors to create a new node
2103/// with specified return type(s), target opcode, and operands.
2104///
2105/// Note that getTargetNode returns the resultant node. If there is already a
2106/// node of the specified opcode and operands, it returns that node instead of
2107/// the current one.
2108SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2109 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2110}
2111SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2112 SDOperand Op1) {
2113 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2114}
2115SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2116 SDOperand Op1, SDOperand Op2) {
2117 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2118}
2119SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2120 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2121 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2122}
2123SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2124 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2125 SDOperand Op4) {
2126 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2127}
2128SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2129 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2130 SDOperand Op4, SDOperand Op5) {
2131 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2132}
2133SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2134 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2135 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2136 std::vector<SDOperand> Ops;
2137 Ops.reserve(6);
2138 Ops.push_back(Op1);
2139 Ops.push_back(Op2);
2140 Ops.push_back(Op3);
2141 Ops.push_back(Op4);
2142 Ops.push_back(Op5);
2143 Ops.push_back(Op6);
2144 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2145}
2146SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2147 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2148 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2149 SDOperand Op7) {
2150 std::vector<SDOperand> Ops;
2151 Ops.reserve(7);
2152 Ops.push_back(Op1);
2153 Ops.push_back(Op2);
2154 Ops.push_back(Op3);
2155 Ops.push_back(Op4);
2156 Ops.push_back(Op5);
2157 Ops.push_back(Op6);
2158 Ops.push_back(Op7);
2159 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2160}
2161SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2162 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2163 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2164 SDOperand Op7, SDOperand Op8) {
2165 std::vector<SDOperand> Ops;
2166 Ops.reserve(8);
2167 Ops.push_back(Op1);
2168 Ops.push_back(Op2);
2169 Ops.push_back(Op3);
2170 Ops.push_back(Op4);
2171 Ops.push_back(Op5);
2172 Ops.push_back(Op6);
2173 Ops.push_back(Op7);
2174 Ops.push_back(Op8);
2175 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2176}
2177SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2178 std::vector<SDOperand> &Ops) {
2179 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2180}
2181SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2182 MVT::ValueType VT2, SDOperand Op1) {
2183 std::vector<MVT::ValueType> ResultTys;
2184 ResultTys.push_back(VT1);
2185 ResultTys.push_back(VT2);
2186 std::vector<SDOperand> Ops;
2187 Ops.push_back(Op1);
2188 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2189}
2190SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2191 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2192 std::vector<MVT::ValueType> ResultTys;
2193 ResultTys.push_back(VT1);
2194 ResultTys.push_back(VT2);
2195 std::vector<SDOperand> Ops;
2196 Ops.push_back(Op1);
2197 Ops.push_back(Op2);
2198 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2199}
2200SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2201 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2202 SDOperand Op3) {
2203 std::vector<MVT::ValueType> ResultTys;
2204 ResultTys.push_back(VT1);
2205 ResultTys.push_back(VT2);
2206 std::vector<SDOperand> Ops;
2207 Ops.push_back(Op1);
2208 Ops.push_back(Op2);
2209 Ops.push_back(Op3);
2210 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2211}
2212SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2213 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2214 SDOperand Op3, SDOperand Op4) {
2215 std::vector<MVT::ValueType> ResultTys;
2216 ResultTys.push_back(VT1);
2217 ResultTys.push_back(VT2);
2218 std::vector<SDOperand> Ops;
2219 Ops.push_back(Op1);
2220 Ops.push_back(Op2);
2221 Ops.push_back(Op3);
2222 Ops.push_back(Op4);
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, SDOperand Op4, SDOperand Op5) {
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 Ops.push_back(Op4);
2236 Ops.push_back(Op5);
2237 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2238}
2239SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2240 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2241 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2242 SDOperand Op6) {
2243 std::vector<MVT::ValueType> ResultTys;
2244 ResultTys.push_back(VT1);
2245 ResultTys.push_back(VT2);
2246 std::vector<SDOperand> Ops;
2247 Ops.push_back(Op1);
2248 Ops.push_back(Op2);
2249 Ops.push_back(Op3);
2250 Ops.push_back(Op4);
2251 Ops.push_back(Op5);
2252 Ops.push_back(Op6);
2253 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2254}
2255SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2256 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2257 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2258 SDOperand Op6, SDOperand Op7) {
2259 std::vector<MVT::ValueType> ResultTys;
2260 ResultTys.push_back(VT1);
2261 ResultTys.push_back(VT2);
2262 std::vector<SDOperand> Ops;
2263 Ops.push_back(Op1);
2264 Ops.push_back(Op2);
2265 Ops.push_back(Op3);
2266 Ops.push_back(Op4);
2267 Ops.push_back(Op5);
2268 Ops.push_back(Op6);
2269 Ops.push_back(Op7);
2270 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2271}
2272SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2273 MVT::ValueType VT2, MVT::ValueType VT3,
2274 SDOperand Op1, SDOperand Op2) {
2275 std::vector<MVT::ValueType> ResultTys;
2276 ResultTys.push_back(VT1);
2277 ResultTys.push_back(VT2);
2278 ResultTys.push_back(VT3);
2279 std::vector<SDOperand> Ops;
2280 Ops.push_back(Op1);
2281 Ops.push_back(Op2);
2282 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2283}
2284SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2285 MVT::ValueType VT2, MVT::ValueType VT3,
2286 SDOperand Op1, SDOperand Op2,
2287 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2288 std::vector<MVT::ValueType> ResultTys;
2289 ResultTys.push_back(VT1);
2290 ResultTys.push_back(VT2);
2291 ResultTys.push_back(VT3);
2292 std::vector<SDOperand> Ops;
2293 Ops.push_back(Op1);
2294 Ops.push_back(Op2);
2295 Ops.push_back(Op3);
2296 Ops.push_back(Op4);
2297 Ops.push_back(Op5);
2298 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2299}
2300SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2301 MVT::ValueType VT2, MVT::ValueType VT3,
2302 SDOperand Op1, SDOperand Op2,
2303 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2304 SDOperand Op6) {
2305 std::vector<MVT::ValueType> ResultTys;
2306 ResultTys.push_back(VT1);
2307 ResultTys.push_back(VT2);
2308 ResultTys.push_back(VT3);
2309 std::vector<SDOperand> Ops;
2310 Ops.push_back(Op1);
2311 Ops.push_back(Op2);
2312 Ops.push_back(Op3);
2313 Ops.push_back(Op4);
2314 Ops.push_back(Op5);
2315 Ops.push_back(Op6);
2316 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2317}
2318SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2319 MVT::ValueType VT2, MVT::ValueType VT3,
2320 SDOperand Op1, SDOperand Op2,
2321 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2322 SDOperand Op6, SDOperand Op7) {
2323 std::vector<MVT::ValueType> ResultTys;
2324 ResultTys.push_back(VT1);
2325 ResultTys.push_back(VT2);
2326 ResultTys.push_back(VT3);
2327 std::vector<SDOperand> Ops;
2328 Ops.push_back(Op1);
2329 Ops.push_back(Op2);
2330 Ops.push_back(Op3);
2331 Ops.push_back(Op4);
2332 Ops.push_back(Op5);
2333 Ops.push_back(Op6);
2334 Ops.push_back(Op7);
2335 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2336}
2337SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2338 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2339 std::vector<MVT::ValueType> ResultTys;
2340 ResultTys.push_back(VT1);
2341 ResultTys.push_back(VT2);
2342 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2343}
2344
Chris Lattner0fb094f2005-11-19 01:44:53 +00002345// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002346/// This can cause recursive merging of nodes in the DAG.
2347///
Chris Lattner8b52f212005-08-26 18:36:28 +00002348/// This version assumes From/To have a single result value.
2349///
Chris Lattner1e111c72005-09-07 05:37:01 +00002350void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2351 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002352 SDNode *From = FromN.Val, *To = ToN.Val;
2353 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2354 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002355 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002356
Chris Lattner8b8749f2005-08-17 19:00:20 +00002357 while (!From->use_empty()) {
2358 // Process users until they are all gone.
2359 SDNode *U = *From->use_begin();
2360
2361 // This node is about to morph, remove its old self from the CSE maps.
2362 RemoveNodeFromCSEMaps(U);
2363
Chris Lattner65113b22005-11-08 22:07:03 +00002364 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2365 I != E; ++I)
2366 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002367 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002368 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002369 To->addUser(U);
2370 }
2371
2372 // Now that we have modified U, add it back to the CSE maps. If it already
2373 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002374 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2375 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002376 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002377 if (Deleted) Deleted->push_back(U);
2378 DeleteNodeNotInCSEMaps(U);
2379 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002380 }
2381}
2382
2383/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2384/// This can cause recursive merging of nodes in the DAG.
2385///
2386/// This version assumes From/To have matching types and numbers of result
2387/// values.
2388///
Chris Lattner1e111c72005-09-07 05:37:01 +00002389void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2390 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002391 assert(From != To && "Cannot replace uses of with self");
2392 assert(From->getNumValues() == To->getNumValues() &&
2393 "Cannot use this version of ReplaceAllUsesWith!");
2394 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002395 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002396 return;
2397 }
2398
2399 while (!From->use_empty()) {
2400 // Process users until they are all gone.
2401 SDNode *U = *From->use_begin();
2402
2403 // This node is about to morph, remove its old self from the CSE maps.
2404 RemoveNodeFromCSEMaps(U);
2405
Chris Lattner65113b22005-11-08 22:07:03 +00002406 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2407 I != E; ++I)
2408 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002409 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002410 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002411 To->addUser(U);
2412 }
2413
2414 // Now that we have modified U, add it back to the CSE maps. If it already
2415 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002416 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2417 ReplaceAllUsesWith(U, Existing, Deleted);
2418 // U is now dead.
2419 if (Deleted) Deleted->push_back(U);
2420 DeleteNodeNotInCSEMaps(U);
2421 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002422 }
2423}
2424
Chris Lattner8b52f212005-08-26 18:36:28 +00002425/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2426/// This can cause recursive merging of nodes in the DAG.
2427///
2428/// This version can replace From with any result values. To must match the
2429/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002430void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002431 const std::vector<SDOperand> &To,
2432 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002433 assert(From->getNumValues() == To.size() &&
2434 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002435 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002436 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002437 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002438 return;
2439 }
2440
2441 while (!From->use_empty()) {
2442 // Process users until they are all gone.
2443 SDNode *U = *From->use_begin();
2444
2445 // This node is about to morph, remove its old self from the CSE maps.
2446 RemoveNodeFromCSEMaps(U);
2447
Chris Lattner65113b22005-11-08 22:07:03 +00002448 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2449 I != E; ++I)
2450 if (I->Val == From) {
2451 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002452 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002453 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002454 ToOp.Val->addUser(U);
2455 }
2456
2457 // Now that we have modified U, add it back to the CSE maps. If it already
2458 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002459 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2460 ReplaceAllUsesWith(U, Existing, Deleted);
2461 // U is now dead.
2462 if (Deleted) Deleted->push_back(U);
2463 DeleteNodeNotInCSEMaps(U);
2464 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002465 }
2466}
2467
Chris Lattner012f2412006-02-17 21:58:01 +00002468/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2469/// uses of other values produced by From.Val alone. The Deleted vector is
2470/// handled the same was as for ReplaceAllUsesWith.
2471void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2472 std::vector<SDNode*> &Deleted) {
2473 assert(From != To && "Cannot replace a value with itself");
2474 // Handle the simple, trivial, case efficiently.
2475 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2476 ReplaceAllUsesWith(From, To, &Deleted);
2477 return;
2478 }
2479
2480 // Get all of the users in a nice, deterministically ordered, uniqued set.
2481 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2482
2483 while (!Users.empty()) {
2484 // We know that this user uses some value of From. If it is the right
2485 // value, update it.
2486 SDNode *User = Users.back();
2487 Users.pop_back();
2488
2489 for (SDOperand *Op = User->OperandList,
2490 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2491 if (*Op == From) {
2492 // Okay, we know this user needs to be updated. Remove its old self
2493 // from the CSE maps.
2494 RemoveNodeFromCSEMaps(User);
2495
2496 // Update all operands that match "From".
2497 for (; Op != E; ++Op) {
2498 if (*Op == From) {
2499 From.Val->removeUser(User);
2500 *Op = To;
2501 To.Val->addUser(User);
2502 }
2503 }
2504
2505 // Now that we have modified User, add it back to the CSE maps. If it
2506 // already exists there, recursively merge the results together.
2507 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2508 unsigned NumDeleted = Deleted.size();
2509 ReplaceAllUsesWith(User, Existing, &Deleted);
2510
2511 // User is now dead.
2512 Deleted.push_back(User);
2513 DeleteNodeNotInCSEMaps(User);
2514
2515 // We have to be careful here, because ReplaceAllUsesWith could have
2516 // deleted a user of From, which means there may be dangling pointers
2517 // in the "Users" setvector. Scan over the deleted node pointers and
2518 // remove them from the setvector.
2519 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2520 Users.remove(Deleted[i]);
2521 }
2522 break; // Exit the operand scanning loop.
2523 }
2524 }
2525 }
2526}
2527
Chris Lattner7b2880c2005-08-24 22:44:39 +00002528
Jim Laskey58b968b2005-08-17 20:08:02 +00002529//===----------------------------------------------------------------------===//
2530// SDNode Class
2531//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002532
Chris Lattnera3255112005-11-08 23:30:28 +00002533
2534/// getValueTypeList - Return a pointer to the specified value type.
2535///
2536MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2537 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2538 VTs[VT] = VT;
2539 return &VTs[VT];
2540}
2541
Chris Lattner5c884562005-01-12 18:37:47 +00002542/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2543/// indicated value. This method ignores uses of other values defined by this
2544/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002545bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002546 assert(Value < getNumValues() && "Bad value!");
2547
2548 // If there is only one value, this is easy.
2549 if (getNumValues() == 1)
2550 return use_size() == NUses;
2551 if (Uses.size() < NUses) return false;
2552
Evan Cheng4ee62112006-02-05 06:29:23 +00002553 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002554
2555 std::set<SDNode*> UsersHandled;
2556
Evan Cheng4ee62112006-02-05 06:29:23 +00002557 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002558 UI != E; ++UI) {
2559 SDNode *User = *UI;
2560 if (User->getNumOperands() == 1 ||
2561 UsersHandled.insert(User).second) // First time we've seen this?
2562 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2563 if (User->getOperand(i) == TheValue) {
2564 if (NUses == 0)
2565 return false; // too many uses
2566 --NUses;
2567 }
2568 }
2569
2570 // Found exactly the right number of uses?
2571 return NUses == 0;
2572}
2573
2574
Evan Cheng4ee62112006-02-05 06:29:23 +00002575// isOnlyUse - Return true if this node is the only use of N.
2576bool SDNode::isOnlyUse(SDNode *N) const {
2577 bool Seen = false;
2578 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2579 SDNode *User = *I;
2580 if (User == this)
2581 Seen = true;
2582 else
2583 return false;
2584 }
2585
2586 return Seen;
2587}
2588
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002589// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002590bool SDOperand::isOperand(SDNode *N) const {
2591 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2592 if (*this == N->getOperand(i))
2593 return true;
2594 return false;
2595}
2596
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002597bool SDNode::isOperand(SDNode *N) const {
2598 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2599 if (this == N->OperandList[i].Val)
2600 return true;
2601 return false;
2602}
Evan Cheng4ee62112006-02-05 06:29:23 +00002603
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002604const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002605 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002606 default:
2607 if (getOpcode() < ISD::BUILTIN_OP_END)
2608 return "<<Unknown DAG Node>>";
2609 else {
Evan Cheng72261582005-12-20 06:22:03 +00002610 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002611 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002612 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2613 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002614
Evan Cheng72261582005-12-20 06:22:03 +00002615 TargetLowering &TLI = G->getTargetLoweringInfo();
2616 const char *Name =
2617 TLI.getTargetNodeName(getOpcode());
2618 if (Name) return Name;
2619 }
2620
2621 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002622 }
2623
Andrew Lenharth95762122005-03-31 21:24:06 +00002624 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002625 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002626 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002627 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002628 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002629 case ISD::AssertSext: return "AssertSext";
2630 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002631
2632 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002633 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002634 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002635 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002636
2637 case ISD::Constant: return "Constant";
2638 case ISD::ConstantFP: return "ConstantFP";
2639 case ISD::GlobalAddress: return "GlobalAddress";
2640 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002641 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002642 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner3e8f3ce2006-03-24 01:04:30 +00002643 case ISD::INTRINSIC: return "INTRINSIC";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002644
Chris Lattnerb2827b02006-03-19 00:52:58 +00002645 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002646 case ISD::TargetConstant: return "TargetConstant";
2647 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002648 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2649 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002650 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002651 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002652
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002653 case ISD::CopyToReg: return "CopyToReg";
2654 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002655 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002656 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002657 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002658 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002659
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002660 // Unary operators
2661 case ISD::FABS: return "fabs";
2662 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002663 case ISD::FSQRT: return "fsqrt";
2664 case ISD::FSIN: return "fsin";
2665 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002666
2667 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002668 case ISD::ADD: return "add";
2669 case ISD::SUB: return "sub";
2670 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002671 case ISD::MULHU: return "mulhu";
2672 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002673 case ISD::SDIV: return "sdiv";
2674 case ISD::UDIV: return "udiv";
2675 case ISD::SREM: return "srem";
2676 case ISD::UREM: return "urem";
2677 case ISD::AND: return "and";
2678 case ISD::OR: return "or";
2679 case ISD::XOR: return "xor";
2680 case ISD::SHL: return "shl";
2681 case ISD::SRA: return "sra";
2682 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002683 case ISD::ROTL: return "rotl";
2684 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002685 case ISD::FADD: return "fadd";
2686 case ISD::FSUB: return "fsub";
2687 case ISD::FMUL: return "fmul";
2688 case ISD::FDIV: return "fdiv";
2689 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002690 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002691 case ISD::VADD: return "vadd";
2692 case ISD::VSUB: return "vsub";
2693 case ISD::VMUL: return "vmul";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002694
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002695 case ISD::SETCC: return "setcc";
2696 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002697 case ISD::SELECT_CC: return "select_cc";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002698 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2699 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002700 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2701 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerce872152006-03-19 06:31:19 +00002702 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerfb194b92006-03-19 23:56:04 +00002703 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2704 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnere25ca692006-03-22 20:09:35 +00002705 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002706 case ISD::ADDC: return "addc";
2707 case ISD::ADDE: return "adde";
2708 case ISD::SUBC: return "subc";
2709 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002710 case ISD::SHL_PARTS: return "shl_parts";
2711 case ISD::SRA_PARTS: return "sra_parts";
2712 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002713
Chris Lattner7f644642005-04-28 21:44:03 +00002714 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002715 case ISD::SIGN_EXTEND: return "sign_extend";
2716 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002717 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002718 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002719 case ISD::TRUNCATE: return "truncate";
2720 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002721 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002722 case ISD::FP_EXTEND: return "fp_extend";
2723
2724 case ISD::SINT_TO_FP: return "sint_to_fp";
2725 case ISD::UINT_TO_FP: return "uint_to_fp";
2726 case ISD::FP_TO_SINT: return "fp_to_sint";
2727 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002728 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002729
2730 // Control flow instructions
2731 case ISD::BR: return "br";
2732 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002733 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002734 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002735 case ISD::CALLSEQ_START: return "callseq_start";
2736 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002737
2738 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002739 case ISD::LOAD: return "load";
2740 case ISD::STORE: return "store";
2741 case ISD::VLOAD: return "vload";
2742 case ISD::EXTLOAD: return "extload";
2743 case ISD::SEXTLOAD: return "sextload";
2744 case ISD::ZEXTLOAD: return "zextload";
2745 case ISD::TRUNCSTORE: return "truncstore";
2746 case ISD::VAARG: return "vaarg";
2747 case ISD::VACOPY: return "vacopy";
2748 case ISD::VAEND: return "vaend";
2749 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002750 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002751 case ISD::EXTRACT_ELEMENT: return "extract_element";
2752 case ISD::BUILD_PAIR: return "build_pair";
2753 case ISD::STACKSAVE: return "stacksave";
2754 case ISD::STACKRESTORE: return "stackrestore";
2755
2756 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002757 case ISD::MEMSET: return "memset";
2758 case ISD::MEMCPY: return "memcpy";
2759 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002760
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002761 // Bit manipulation
2762 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002763 case ISD::CTPOP: return "ctpop";
2764 case ISD::CTTZ: return "cttz";
2765 case ISD::CTLZ: return "ctlz";
2766
Chris Lattner36ce6912005-11-29 06:21:05 +00002767 // Debug info
2768 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002769 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002770 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002771
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002772 case ISD::CONDCODE:
2773 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002774 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002775 case ISD::SETOEQ: return "setoeq";
2776 case ISD::SETOGT: return "setogt";
2777 case ISD::SETOGE: return "setoge";
2778 case ISD::SETOLT: return "setolt";
2779 case ISD::SETOLE: return "setole";
2780 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002781
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002782 case ISD::SETO: return "seto";
2783 case ISD::SETUO: return "setuo";
2784 case ISD::SETUEQ: return "setue";
2785 case ISD::SETUGT: return "setugt";
2786 case ISD::SETUGE: return "setuge";
2787 case ISD::SETULT: return "setult";
2788 case ISD::SETULE: return "setule";
2789 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002790
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002791 case ISD::SETEQ: return "seteq";
2792 case ISD::SETGT: return "setgt";
2793 case ISD::SETGE: return "setge";
2794 case ISD::SETLT: return "setlt";
2795 case ISD::SETLE: return "setle";
2796 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002797 }
2798 }
2799}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002800
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002801void SDNode::dump() const { dump(0); }
2802void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002803 std::cerr << (void*)this << ": ";
2804
2805 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2806 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002807 if (getValueType(i) == MVT::Other)
2808 std::cerr << "ch";
2809 else
2810 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002811 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002812 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002813
2814 std::cerr << " ";
2815 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2816 if (i) std::cerr << ", ";
2817 std::cerr << (void*)getOperand(i).Val;
2818 if (unsigned RN = getOperand(i).ResNo)
2819 std::cerr << ":" << RN;
2820 }
2821
2822 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2823 std::cerr << "<" << CSDN->getValue() << ">";
2824 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2825 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002826 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002827 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002828 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002829 std::cerr << "<";
2830 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002831 if (offset > 0)
2832 std::cerr << " + " << offset;
2833 else
2834 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002835 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002836 std::cerr << "<" << FIDN->getIndex() << ">";
2837 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002838 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002839 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002840 if (offset > 0)
2841 std::cerr << " + " << offset;
2842 else
2843 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002844 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002845 std::cerr << "<";
2846 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2847 if (LBB)
2848 std::cerr << LBB->getName() << " ";
2849 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002850 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002851 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002852 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2853 } else {
2854 std::cerr << " #" << R->getReg();
2855 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002856 } else if (const ExternalSymbolSDNode *ES =
2857 dyn_cast<ExternalSymbolSDNode>(this)) {
2858 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002859 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2860 if (M->getValue())
2861 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2862 else
2863 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002864 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2865 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002866 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002867}
2868
Chris Lattnerde202b32005-11-09 23:47:37 +00002869static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002870 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2871 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002872 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002873 else
2874 std::cerr << "\n" << std::string(indent+2, ' ')
2875 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002876
Chris Lattnerea946cd2005-01-09 20:38:33 +00002877
2878 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002879 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002880}
2881
Chris Lattnerc3aae252005-01-07 07:46:32 +00002882void SelectionDAG::dump() const {
2883 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002884 std::vector<const SDNode*> Nodes;
2885 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2886 I != E; ++I)
2887 Nodes.push_back(I);
2888
Chris Lattner49d24712005-01-09 20:26:36 +00002889 std::sort(Nodes.begin(), Nodes.end());
2890
2891 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002892 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002893 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002894 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002895
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002896 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002897
Chris Lattnerc3aae252005-01-07 07:46:32 +00002898 std::cerr << "\n\n";
2899}
2900
Evan Chengfae9f1c2006-02-09 22:11:03 +00002901/// InsertISelMapEntry - A helper function to insert a key / element pair
2902/// into a SDOperand to SDOperand map. This is added to avoid the map
2903/// insertion operator from being inlined.
2904void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2905 SDNode *Key, unsigned KeyResNo,
2906 SDNode *Element, unsigned ElementResNo) {
2907 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2908 SDOperand(Element, ElementResNo)));
2909}