blob: b9a6ec0d452e291704fd505fbf9bfb175ede5252 [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"
Evan Cheng115c0362005-12-19 23:11:49 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000026#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000027#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000028#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner5cdcc582005-01-09 20:52:51 +000031static bool isCommutativeBinOp(unsigned Opcode) {
32 switch (Opcode) {
33 case ISD::ADD:
34 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000035 case ISD::MULHU:
36 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000037 case ISD::FADD:
38 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000039 case ISD::AND:
40 case ISD::OR:
41 case ISD::XOR: return true;
42 default: return false; // FIXME: Need commutative info for user ops!
43 }
44}
45
46static bool isAssociativeBinOp(unsigned Opcode) {
47 switch (Opcode) {
48 case ISD::ADD:
49 case ISD::MUL:
50 case ISD::AND:
51 case ISD::OR:
52 case ISD::XOR: return true;
53 default: return false; // FIXME: Need associative info for user ops!
54 }
55}
56
Chris Lattner5cdcc582005-01-09 20:52:51 +000057// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000061 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000062 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000063 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000064}
65
Jim Laskey58b968b2005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
74bool ConstantFPSDNode::isExactlyValue(double V) const {
75 return DoubleToBits(V) == DoubleToBits(Value);
76}
77
78//===----------------------------------------------------------------------===//
79// ISD Class
80//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000081
Chris Lattnerc3aae252005-01-07 07:46:32 +000082/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
83/// when given the operation for (X op Y).
84ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
85 // To perform this operation, we just need to swap the L and G bits of the
86 // operation.
87 unsigned OldL = (Operation >> 2) & 1;
88 unsigned OldG = (Operation >> 1) & 1;
89 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
90 (OldL << 1) | // New G bit
91 (OldG << 2)); // New L bit.
92}
93
94/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
95/// 'op' is a valid SetCC operation.
96ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
97 unsigned Operation = Op;
98 if (isInteger)
99 Operation ^= 7; // Flip L, G, E bits, but not U.
100 else
101 Operation ^= 15; // Flip all of the condition bits.
102 if (Operation > ISD::SETTRUE2)
103 Operation &= ~8; // Don't let N and U bits get set.
104 return ISD::CondCode(Operation);
105}
106
107
108/// isSignedOp - For an integer comparison, return 1 if the comparison is a
109/// signed operation and 2 if the result is an unsigned comparison. Return zero
110/// if the operation does not depend on the sign of the input (setne and seteq).
111static int isSignedOp(ISD::CondCode Opcode) {
112 switch (Opcode) {
113 default: assert(0 && "Illegal integer setcc operation!");
114 case ISD::SETEQ:
115 case ISD::SETNE: return 0;
116 case ISD::SETLT:
117 case ISD::SETLE:
118 case ISD::SETGT:
119 case ISD::SETGE: return 1;
120 case ISD::SETULT:
121 case ISD::SETULE:
122 case ISD::SETUGT:
123 case ISD::SETUGE: return 2;
124 }
125}
126
127/// getSetCCOrOperation - Return the result of a logical OR between different
128/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
129/// returns SETCC_INVALID if it is not possible to represent the resultant
130/// comparison.
131ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
132 bool isInteger) {
133 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
134 // Cannot fold a signed integer setcc with an unsigned integer setcc.
135 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnerc3aae252005-01-07 07:46:32 +0000137 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000138
Chris Lattnerc3aae252005-01-07 07:46:32 +0000139 // If the N and U bits get set then the resultant comparison DOES suddenly
140 // care about orderedness, and is true when ordered.
141 if (Op > ISD::SETTRUE2)
142 Op &= ~16; // Clear the N bit.
143 return ISD::CondCode(Op);
144}
145
146/// getSetCCAndOperation - Return the result of a logical AND between different
147/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
148/// function returns zero if it is not possible to represent the resultant
149/// comparison.
150ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
151 bool isInteger) {
152 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
153 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000154 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000155
156 // Combine all of the condition bits.
157 return ISD::CondCode(Op1 & Op2);
158}
159
Chris Lattnerb48da392005-01-23 04:39:44 +0000160const TargetMachine &SelectionDAG::getTarget() const {
161 return TLI.getTargetMachine();
162}
163
Jim Laskey58b968b2005-08-17 20:08:02 +0000164//===----------------------------------------------------------------------===//
165// SelectionDAG Class
166//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000167
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000168/// RemoveDeadNodes - This method deletes all unreachable nodes in the
169/// SelectionDAG, including nodes (like loads) that have uses of their token
170/// chain but no other uses and no side effect. If a node is passed in as an
171/// argument, it is used as the seed for node deletion.
172void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173 // Create a dummy node (which is not added to allnodes), that adds a reference
174 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000175 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000176
Chris Lattnerf469cb62005-11-08 18:52:27 +0000177 bool MadeChange = false;
178
Chris Lattner8b8749f2005-08-17 19:00:20 +0000179 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000180 if (N && N->use_empty()) {
181 DestroyDeadNode(N);
182 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000183 }
184
Chris Lattnerde202b32005-11-09 23:47:37 +0000185 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
186 if (I->use_empty() && I->getOpcode() != 65535) {
187 // Node is dead, recursively delete newly dead uses.
188 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000189 MadeChange = true;
190 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000191
192 // Walk the nodes list, removing the nodes we've marked as dead.
193 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000194 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
195 SDNode *N = I++;
196 if (N->use_empty())
197 AllNodes.erase(N);
198 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000199 }
200
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000201 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000202 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203}
204
Chris Lattnerf469cb62005-11-08 18:52:27 +0000205/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
206/// graph. If it is the last user of any of its operands, recursively process
207/// them the same way.
208///
209void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000210 // Okay, we really are going to delete this node. First take this out of the
211 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000212 RemoveNodeFromCSEMaps(N);
213
214 // Next, brutally remove the operand list. This is safe to do, as there are
215 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000216 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
217 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000218 O->removeUser(N);
219
220 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000221 if (O->use_empty())
222 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000223 }
Chris Lattner65113b22005-11-08 22:07:03 +0000224 delete[] N->OperandList;
225 N->OperandList = 0;
226 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000227
228 // Mark the node as dead.
229 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000230}
231
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000232void SelectionDAG::DeleteNode(SDNode *N) {
233 assert(N->use_empty() && "Cannot delete a node that is not dead!");
234
235 // First take this out of the appropriate CSE map.
236 RemoveNodeFromCSEMaps(N);
237
Chris Lattner1e111c72005-09-07 05:37:01 +0000238 // Finally, remove uses due to operands of this node, remove from the
239 // AllNodes list, and delete the node.
240 DeleteNodeNotInCSEMaps(N);
241}
242
243void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
244
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000245 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000246 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000247
248 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000249 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
250 I->Val->removeUser(N);
251 delete[] N->OperandList;
252 N->OperandList = 0;
253 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000254
255 delete N;
256}
257
Chris Lattner149c58c2005-08-16 18:17:10 +0000258/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259/// correspond to it. This is useful when we're about to delete or repurpose
260/// the node. We don't want future request for structurally identical nodes
261/// to return N anymore.
262void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000263 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000265 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000267 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
268 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000270 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000271 Erased = TargetConstants.erase(std::make_pair(
272 cast<ConstantSDNode>(N)->getValue(),
273 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000274 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000275 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000276 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000277 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000278 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000279 }
Chris Lattner3181a772006-01-29 06:26:56 +0000280 case ISD::TargetConstantFP: {
281 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
282 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
283 break;
284 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000285 case ISD::STRING:
286 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
287 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000288 case ISD::CONDCODE:
289 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
290 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000291 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000292 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 case ISD::GlobalAddress: {
295 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
296 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
297 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000299 }
300 case ISD::TargetGlobalAddress: {
301 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
302 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
303 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000304 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000305 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000307 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000309 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000310 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000311 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000313 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000314 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000315 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000316 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000317 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000319 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000320 break;
321 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000322 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000323 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000324 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000325 Erased =
326 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000327 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000328 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000329 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000330 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
331 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000332 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000333 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
334 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000335 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000336 case ISD::SRCVALUE: {
337 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000338 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000339 break;
340 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000341 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000342 Erased = Loads.erase(std::make_pair(N->getOperand(1),
343 std::make_pair(N->getOperand(0),
344 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000345 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000346 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000347 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000348 if (N->getNumOperands() == 0) {
349 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
350 N->getValueType(0)));
351 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000352 Erased =
353 UnaryOps.erase(std::make_pair(N->getOpcode(),
354 std::make_pair(N->getOperand(0),
355 N->getValueType(0))));
356 } else if (N->getNumOperands() == 2) {
357 Erased =
358 BinaryOps.erase(std::make_pair(N->getOpcode(),
359 std::make_pair(N->getOperand(0),
360 N->getOperand(1))));
361 } else {
362 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
363 Erased =
364 OneResultNodes.erase(std::make_pair(N->getOpcode(),
365 std::make_pair(N->getValueType(0),
366 Ops)));
367 }
Chris Lattner385328c2005-05-14 07:42:29 +0000368 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000369 // Remove the node from the ArbitraryNodes map.
370 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
371 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372 Erased =
373 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
374 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000375 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 break;
377 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000378#ifndef NDEBUG
379 // Verify that the node was actually in one of the CSE maps, unless it has a
380 // flag result (which cannot be CSE'd) or is one of the special cases that are
381 // not subject to CSE.
382 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner0ff5c272006-01-28 00:18:58 +0000383 N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000384 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000385
386 N->dump();
387 assert(0 && "Node is not in map!");
388 }
389#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000390}
391
Chris Lattner8b8749f2005-08-17 19:00:20 +0000392/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
393/// has been taken out and modified in some way. If the specified node already
394/// exists in the CSE maps, do not modify the maps, but return the existing node
395/// instead. If it doesn't exist, add it and return null.
396///
397SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
398 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000399 if (N->getOpcode() == ISD::CALLSEQ_START ||
Chris Lattnerfe14b342005-12-01 23:14:50 +0000400 N->getOpcode() == ISD::CALLSEQ_END ||
Chris Lattner9f8cc692005-12-19 22:21:21 +0000401 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000402 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000403
Chris Lattner9f8cc692005-12-19 22:21:21 +0000404 // Check that remaining values produced are not flags.
405 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
406 if (N->getValueType(i) == MVT::Flag)
407 return 0; // Never CSE anything that produces a flag.
408
Chris Lattnerfe14b342005-12-01 23:14:50 +0000409 if (N->getNumValues() == 1) {
410 if (N->getNumOperands() == 1) {
411 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getOperand(0),
413 N->getValueType(0)))];
414 if (U) return U;
415 U = N;
416 } else if (N->getNumOperands() == 2) {
417 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
418 std::make_pair(N->getOperand(0),
419 N->getOperand(1)))];
420 if (B) return B;
421 B = N;
422 } else {
423 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
424 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000425 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000426 if (ORN) return ORN;
427 ORN = N;
428 }
429 } else {
430 if (N->getOpcode() == ISD::LOAD) {
431 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
432 std::make_pair(N->getOperand(0),
433 N->getValueType(0)))];
434 if (L) return L;
435 L = N;
436 } else {
437 // Remove the node from the ArbitraryNodes map.
438 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
439 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
440 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
441 std::make_pair(RV, Ops))];
442 if (AN) return AN;
443 AN = N;
444 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000445 }
446 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000447}
448
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000449/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
450/// were replaced with those specified. If this node is never memoized,
451/// return null, otherwise return a pointer to the slot it would take. If a
452/// node already exists with these operands, the slot will be non-null.
453SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
454 if (N->getOpcode() == ISD::CALLSEQ_START ||
455 N->getOpcode() == ISD::CALLSEQ_END ||
456 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
457 return 0; // Never add these nodes.
458
459 // Check that remaining values produced are not flags.
460 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
461 if (N->getValueType(i) == MVT::Flag)
462 return 0; // Never CSE anything that produces a flag.
463
464 if (N->getNumValues() == 1) {
465 return &UnaryOps[std::make_pair(N->getOpcode(),
466 std::make_pair(Op, N->getValueType(0)))];
467 } else {
468 // Remove the node from the ArbitraryNodes map.
469 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
470 std::vector<SDOperand> Ops;
471 Ops.push_back(Op);
472 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
473 std::make_pair(RV, Ops))];
474 }
475 return 0;
476}
477
478/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
479/// were replaced with those specified. If this node is never memoized,
480/// return null, otherwise return a pointer to the slot it would take. If a
481/// node already exists with these operands, the slot will be non-null.
482SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
483 SDOperand Op1, SDOperand Op2) {
484 if (N->getOpcode() == ISD::CALLSEQ_START ||
485 N->getOpcode() == ISD::CALLSEQ_END ||
486 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
487 return 0; // Never add these nodes.
488
489 // Check that remaining values produced are not flags.
490 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
491 if (N->getValueType(i) == MVT::Flag)
492 return 0; // Never CSE anything that produces a flag.
493
494 if (N->getNumValues() == 1) {
495 return &BinaryOps[std::make_pair(N->getOpcode(),
496 std::make_pair(Op1, Op2))];
497 } else {
498 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
499 std::vector<SDOperand> Ops;
500 Ops.push_back(Op1);
501 Ops.push_back(Op2);
502 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
503 std::make_pair(RV, Ops))];
504 }
505 return 0;
506}
507
508
509/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
510/// were replaced with those specified. If this node is never memoized,
511/// return null, otherwise return a pointer to the slot it would take. If a
512/// node already exists with these operands, the slot will be non-null.
513SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
514 const std::vector<SDOperand> &Ops) {
515 if (N->getOpcode() == ISD::CALLSEQ_START ||
516 N->getOpcode() == ISD::CALLSEQ_END ||
517 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
518 return 0; // Never add these nodes.
519
520 // Check that remaining values produced are not flags.
521 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
522 if (N->getValueType(i) == MVT::Flag)
523 return 0; // Never CSE anything that produces a flag.
524
525 if (N->getNumValues() == 1) {
526 if (N->getNumOperands() == 1) {
527 return &UnaryOps[std::make_pair(N->getOpcode(),
528 std::make_pair(Ops[0],
529 N->getValueType(0)))];
530 } else if (N->getNumOperands() == 2) {
531 return &BinaryOps[std::make_pair(N->getOpcode(),
532 std::make_pair(Ops[0], Ops[1]))];
533 } else {
534 return &OneResultNodes[std::make_pair(N->getOpcode(),
535 std::make_pair(N->getValueType(0),
536 Ops))];
537 }
538 } else {
539 if (N->getOpcode() == ISD::LOAD) {
540 return &Loads[std::make_pair(Ops[1],
541 std::make_pair(Ops[0], N->getValueType(0)))];
542 } else {
543 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
544 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
545 std::make_pair(RV, Ops))];
546 }
547 }
548 return 0;
549}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000550
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000551
Chris Lattner78ec3112003-08-11 14:57:33 +0000552SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000553 while (!AllNodes.empty()) {
554 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000555 delete [] N->OperandList;
556 N->OperandList = 0;
557 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000558 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000559 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000560}
561
Chris Lattner0f2287b2005-04-13 02:38:18 +0000562SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000563 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000564 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000565 return getNode(ISD::AND, Op.getValueType(), Op,
566 getConstant(Imm, Op.getValueType()));
567}
568
Chris Lattnerc3aae252005-01-07 07:46:32 +0000569SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
570 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
571 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000572 if (VT != MVT::i64)
573 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000574
Chris Lattnerc3aae252005-01-07 07:46:32 +0000575 SDNode *&N = Constants[std::make_pair(Val, VT)];
576 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000577 N = new ConstantSDNode(false, Val, VT);
578 AllNodes.push_back(N);
579 return SDOperand(N, 0);
580}
581
Chris Lattner36ce6912005-11-29 06:21:05 +0000582SDOperand SelectionDAG::getString(const std::string &Val) {
583 StringSDNode *&N = StringNodes[Val];
584 if (!N) {
585 N = new StringSDNode(Val);
586 AllNodes.push_back(N);
587 }
588 return SDOperand(N, 0);
589}
590
Chris Lattner37bfbb42005-08-17 00:34:06 +0000591SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
592 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
593 // Mask out any bits that are not valid for this constant.
594 if (VT != MVT::i64)
595 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
596
597 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
598 if (N) return SDOperand(N, 0);
599 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000600 AllNodes.push_back(N);
601 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000602}
603
Chris Lattnerc3aae252005-01-07 07:46:32 +0000604SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
605 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
606 if (VT == MVT::f32)
607 Val = (float)Val; // Mask out extra precision.
608
Chris Lattnerd8658612005-02-17 20:17:32 +0000609 // Do the map lookup using the actual bit pattern for the floating point
610 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
611 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000612 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000613 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000614 N = new ConstantFPSDNode(false, Val, VT);
615 AllNodes.push_back(N);
616 return SDOperand(N, 0);
617}
618
619SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
620 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
621 if (VT == MVT::f32)
622 Val = (float)Val; // Mask out extra precision.
623
624 // Do the map lookup using the actual bit pattern for the floating point
625 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
626 // we don't have issues with SNANs.
627 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
628 if (N) return SDOperand(N, 0);
629 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000630 AllNodes.push_back(N);
631 return SDOperand(N, 0);
632}
633
Chris Lattnerc3aae252005-01-07 07:46:32 +0000634SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000635 MVT::ValueType VT, int offset) {
636 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000637 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000638 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000639 AllNodes.push_back(N);
640 return SDOperand(N, 0);
641}
642
643SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000644 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000645 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000646 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000647 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000648 AllNodes.push_back(N);
649 return SDOperand(N, 0);
650}
651
652SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
653 SDNode *&N = FrameIndices[FI];
654 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000655 N = new FrameIndexSDNode(FI, VT, false);
656 AllNodes.push_back(N);
657 return SDOperand(N, 0);
658}
659
660SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
661 SDNode *&N = TargetFrameIndices[FI];
662 if (N) return SDOperand(N, 0);
663 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000664 AllNodes.push_back(N);
665 return SDOperand(N, 0);
666}
667
Chris Lattner5839bf22005-08-26 17:15:30 +0000668SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
669 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000670 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000671 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000672 AllNodes.push_back(N);
673 return SDOperand(N, 0);
674}
675
Chris Lattner5839bf22005-08-26 17:15:30 +0000676SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
677 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000678 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000679 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000680 AllNodes.push_back(N);
681 return SDOperand(N, 0);
682}
683
684SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
685 SDNode *&N = BBNodes[MBB];
686 if (N) return SDOperand(N, 0);
687 N = new BasicBlockSDNode(MBB);
688 AllNodes.push_back(N);
689 return SDOperand(N, 0);
690}
691
Chris Lattner15e4b012005-07-10 00:07:11 +0000692SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
693 if ((unsigned)VT >= ValueTypeNodes.size())
694 ValueTypeNodes.resize(VT+1);
695 if (ValueTypeNodes[VT] == 0) {
696 ValueTypeNodes[VT] = new VTSDNode(VT);
697 AllNodes.push_back(ValueTypeNodes[VT]);
698 }
699
700 return SDOperand(ValueTypeNodes[VT], 0);
701}
702
Chris Lattnerc3aae252005-01-07 07:46:32 +0000703SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
704 SDNode *&N = ExternalSymbols[Sym];
705 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000706 N = new ExternalSymbolSDNode(false, Sym, VT);
707 AllNodes.push_back(N);
708 return SDOperand(N, 0);
709}
710
Chris Lattner809ec112006-01-28 10:09:25 +0000711SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
712 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000713 SDNode *&N = TargetExternalSymbols[Sym];
714 if (N) return SDOperand(N, 0);
715 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000716 AllNodes.push_back(N);
717 return SDOperand(N, 0);
718}
719
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000720SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
721 if ((unsigned)Cond >= CondCodeNodes.size())
722 CondCodeNodes.resize(Cond+1);
723
Chris Lattner079a27a2005-08-09 20:40:02 +0000724 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000725 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000726 AllNodes.push_back(CondCodeNodes[Cond]);
727 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000728 return SDOperand(CondCodeNodes[Cond], 0);
729}
730
Chris Lattner0fdd7682005-08-30 22:38:38 +0000731SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
732 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
733 if (!Reg) {
734 Reg = new RegisterSDNode(RegNo, VT);
735 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000736 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000737 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000738}
739
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000740SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
741 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000742 // These setcc operations always fold.
743 switch (Cond) {
744 default: break;
745 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000746 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000747 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000748 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749 }
750
Chris Lattner67255a12005-04-07 18:14:58 +0000751 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
752 uint64_t C2 = N2C->getValue();
753 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
754 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000755
Chris Lattnerc3aae252005-01-07 07:46:32 +0000756 // Sign extend the operands if required
757 if (ISD::isSignedIntSetCC(Cond)) {
758 C1 = N1C->getSignExtended();
759 C2 = N2C->getSignExtended();
760 }
761
762 switch (Cond) {
763 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000764 case ISD::SETEQ: return getConstant(C1 == C2, VT);
765 case ISD::SETNE: return getConstant(C1 != C2, VT);
766 case ISD::SETULT: return getConstant(C1 < C2, VT);
767 case ISD::SETUGT: return getConstant(C1 > C2, VT);
768 case ISD::SETULE: return getConstant(C1 <= C2, VT);
769 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
770 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
771 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
772 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
773 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000774 }
Chris Lattner24673922005-04-07 18:58:54 +0000775 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000776 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000777 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
778 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
779
780 // If the comparison constant has bits in the upper part, the
781 // zero-extended value could never match.
782 if (C2 & (~0ULL << InSize)) {
783 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
784 switch (Cond) {
785 case ISD::SETUGT:
786 case ISD::SETUGE:
787 case ISD::SETEQ: return getConstant(0, VT);
788 case ISD::SETULT:
789 case ISD::SETULE:
790 case ISD::SETNE: return getConstant(1, VT);
791 case ISD::SETGT:
792 case ISD::SETGE:
793 // True if the sign bit of C2 is set.
794 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
795 case ISD::SETLT:
796 case ISD::SETLE:
797 // True if the sign bit of C2 isn't set.
798 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
799 default:
800 break;
801 }
802 }
803
804 // Otherwise, we can perform the comparison with the low bits.
805 switch (Cond) {
806 case ISD::SETEQ:
807 case ISD::SETNE:
808 case ISD::SETUGT:
809 case ISD::SETUGE:
810 case ISD::SETULT:
811 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000812 return getSetCC(VT, N1.getOperand(0),
813 getConstant(C2, N1.getOperand(0).getValueType()),
814 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000815 default:
816 break; // todo, be more careful with signed comparisons
817 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000818 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
819 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
820 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
821 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
822 MVT::ValueType ExtDstTy = N1.getValueType();
823 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000824
Chris Lattner7b2880c2005-08-24 22:44:39 +0000825 // If the extended part has any inconsistent bits, it cannot ever
826 // compare equal. In other words, they have to be all ones or all
827 // zeros.
828 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000829 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000830 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
831 return getConstant(Cond == ISD::SETNE, VT);
832
833 // Otherwise, make this a use of a zext.
834 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000835 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000836 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000837 }
838
Chris Lattner67255a12005-04-07 18:14:58 +0000839 uint64_t MinVal, MaxVal;
840 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
841 if (ISD::isSignedIntSetCC(Cond)) {
842 MinVal = 1ULL << (OperandBitSize-1);
843 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
844 MaxVal = ~0ULL >> (65-OperandBitSize);
845 else
846 MaxVal = 0;
847 } else {
848 MinVal = 0;
849 MaxVal = ~0ULL >> (64-OperandBitSize);
850 }
851
852 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
853 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
854 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
855 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000856 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
857 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000858 }
859
860 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
861 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
862 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000863 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
864 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000865 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000866
Nate Begeman72ea2812005-04-14 08:56:52 +0000867 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
868 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000869
Nate Begeman72ea2812005-04-14 08:56:52 +0000870 // Canonicalize setgt X, Min --> setne X, Min
871 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000872 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000873
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000874 // If we have setult X, 1, turn it into seteq X, 0
875 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000876 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
877 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000878 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000879 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000880 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
881 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000882
883 // If we have "setcc X, C1", check to see if we can shrink the immediate
884 // by changing cc.
885
886 // SETUGT X, SINTMAX -> SETLT X, 0
887 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
888 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000889 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000890
891 // FIXME: Implement the rest of these.
892
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000893
894 // Fold bit comparisons when we can.
895 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
896 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
897 if (ConstantSDNode *AndRHS =
898 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
899 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
900 // Perform the xform if the AND RHS is a single bit.
901 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
902 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000903 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000904 TLI.getShiftAmountTy()));
905 }
906 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
907 // (X & 8) == 8 --> (X & 8) >> 3
908 // Perform the xform if C2 is a single bit.
909 if ((C2 & (C2-1)) == 0) {
910 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000911 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000912 }
913 }
914 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915 }
Chris Lattner67255a12005-04-07 18:14:58 +0000916 } else if (isa<ConstantSDNode>(N1.Val)) {
917 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000918 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000919 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000920
921 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
922 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
923 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000924
Chris Lattnerc3aae252005-01-07 07:46:32 +0000925 switch (Cond) {
926 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000927 case ISD::SETEQ: return getConstant(C1 == C2, VT);
928 case ISD::SETNE: return getConstant(C1 != C2, VT);
929 case ISD::SETLT: return getConstant(C1 < C2, VT);
930 case ISD::SETGT: return getConstant(C1 > C2, VT);
931 case ISD::SETLE: return getConstant(C1 <= C2, VT);
932 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 }
934 } else {
935 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000936 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937 }
938
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000939 // Could not fold it.
940 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941}
942
Chris Lattnerc3aae252005-01-07 07:46:32 +0000943/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000944///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000946 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
947 if (!N) {
948 N = new SDNode(Opcode, VT);
949 AllNodes.push_back(N);
950 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000951 return SDOperand(N, 0);
952}
953
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
955 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000956 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000957 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
959 uint64_t Val = C->getValue();
960 switch (Opcode) {
961 default: break;
962 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000963 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000964 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
965 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000966 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
967 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000968 case ISD::BIT_CONVERT:
969 if (VT == MVT::f32) {
970 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
971 return getConstantFP(BitsToFloat(Val), VT);
972 } else if (VT == MVT::f64) {
973 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
974 return getConstantFP(BitsToDouble(Val), VT);
975 }
976 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000977 case ISD::BSWAP:
978 switch(VT) {
979 default: assert(0 && "Invalid bswap!"); break;
980 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
981 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
982 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
983 }
984 break;
985 case ISD::CTPOP:
986 switch(VT) {
987 default: assert(0 && "Invalid ctpop!"); break;
988 case MVT::i1: return getConstant(Val != 0, VT);
989 case MVT::i8:
990 Tmp1 = (unsigned)Val & 0xFF;
991 return getConstant(CountPopulation_32(Tmp1), VT);
992 case MVT::i16:
993 Tmp1 = (unsigned)Val & 0xFFFF;
994 return getConstant(CountPopulation_32(Tmp1), VT);
995 case MVT::i32:
996 return getConstant(CountPopulation_32((unsigned)Val), VT);
997 case MVT::i64:
998 return getConstant(CountPopulation_64(Val), VT);
999 }
1000 case ISD::CTLZ:
1001 switch(VT) {
1002 default: assert(0 && "Invalid ctlz!"); break;
1003 case MVT::i1: return getConstant(Val == 0, VT);
1004 case MVT::i8:
1005 Tmp1 = (unsigned)Val & 0xFF;
1006 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1007 case MVT::i16:
1008 Tmp1 = (unsigned)Val & 0xFFFF;
1009 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1010 case MVT::i32:
1011 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1012 case MVT::i64:
1013 return getConstant(CountLeadingZeros_64(Val), VT);
1014 }
1015 case ISD::CTTZ:
1016 switch(VT) {
1017 default: assert(0 && "Invalid cttz!"); break;
1018 case MVT::i1: return getConstant(Val == 0, VT);
1019 case MVT::i8:
1020 Tmp1 = (unsigned)Val | 0x100;
1021 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1022 case MVT::i16:
1023 Tmp1 = (unsigned)Val | 0x10000;
1024 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1025 case MVT::i32:
1026 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1027 case MVT::i64:
1028 return getConstant(CountTrailingZeros_64(Val), VT);
1029 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 }
1031 }
1032
Chris Lattner94683772005-12-23 05:30:37 +00001033 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1035 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001036 case ISD::FNEG:
1037 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001038 case ISD::FABS:
1039 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 case ISD::FP_ROUND:
1041 case ISD::FP_EXTEND:
1042 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001043 case ISD::FP_TO_SINT:
1044 return getConstant((int64_t)C->getValue(), VT);
1045 case ISD::FP_TO_UINT:
1046 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001047 case ISD::BIT_CONVERT:
1048 if (VT == MVT::i32) {
1049 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1050 return getConstant(FloatToBits(C->getValue()), VT);
1051 } else if (VT == MVT::i64) {
1052 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1053 return getConstant(DoubleToBits(C->getValue()), VT);
1054 }
1055 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056 }
1057
1058 unsigned OpOpcode = Operand.Val->getOpcode();
1059 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001060 case ISD::TokenFactor:
1061 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 case ISD::SIGN_EXTEND:
1063 if (Operand.getValueType() == VT) return Operand; // noop extension
1064 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1065 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1066 break;
1067 case ISD::ZERO_EXTEND:
1068 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001069 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001070 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001072 case ISD::ANY_EXTEND:
1073 if (Operand.getValueType() == VT) return Operand; // noop extension
1074 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1075 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1076 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1077 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 case ISD::TRUNCATE:
1079 if (Operand.getValueType() == VT) return Operand; // noop truncate
1080 if (OpOpcode == ISD::TRUNCATE)
1081 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001082 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1083 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001084 // If the source is smaller than the dest, we still need an extend.
1085 if (Operand.Val->getOperand(0).getValueType() < VT)
1086 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1087 else if (Operand.Val->getOperand(0).getValueType() > VT)
1088 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1089 else
1090 return Operand.Val->getOperand(0);
1091 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092 break;
Chris Lattner35481892005-12-23 00:16:34 +00001093 case ISD::BIT_CONVERT:
1094 // Basic sanity checking.
1095 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1096 "Cannot BIT_CONVERT between two different types!");
1097 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001098 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1099 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001100 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001101 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001102 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1103 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001104 Operand.Val->getOperand(0));
1105 if (OpOpcode == ISD::FNEG) // --X -> X
1106 return Operand.Val->getOperand(0);
1107 break;
1108 case ISD::FABS:
1109 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1110 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1111 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001112 }
1113
Chris Lattner43247a12005-08-25 19:12:10 +00001114 SDNode *N;
1115 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1116 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001117 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001118 E = N = new SDNode(Opcode, Operand);
1119 } else {
1120 N = new SDNode(Opcode, Operand);
1121 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 N->setValueTypes(VT);
1123 AllNodes.push_back(N);
1124 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001125}
1126
Chris Lattner36019aa2005-04-18 03:48:41 +00001127
1128
Chris Lattnerc3aae252005-01-07 07:46:32 +00001129SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1130 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001131#ifndef NDEBUG
1132 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001133 case ISD::TokenFactor:
1134 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1135 N2.getValueType() == MVT::Other && "Invalid token factor!");
1136 break;
Chris Lattner76365122005-01-16 02:23:22 +00001137 case ISD::AND:
1138 case ISD::OR:
1139 case ISD::XOR:
1140 case ISD::UDIV:
1141 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001142 case ISD::MULHU:
1143 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001144 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1145 // fall through
1146 case ISD::ADD:
1147 case ISD::SUB:
1148 case ISD::MUL:
1149 case ISD::SDIV:
1150 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001151 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1152 // fall through.
1153 case ISD::FADD:
1154 case ISD::FSUB:
1155 case ISD::FMUL:
1156 case ISD::FDIV:
1157 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001158 assert(N1.getValueType() == N2.getValueType() &&
1159 N1.getValueType() == VT && "Binary operator types must match!");
1160 break;
1161
1162 case ISD::SHL:
1163 case ISD::SRA:
1164 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001165 case ISD::ROTL:
1166 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001167 assert(VT == N1.getValueType() &&
1168 "Shift operators return type must be the same as their first arg");
1169 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001170 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001171 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001172 case ISD::FP_ROUND_INREG: {
1173 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1174 assert(VT == N1.getValueType() && "Not an inreg round!");
1175 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1176 "Cannot FP_ROUND_INREG integer types");
1177 assert(EVT <= VT && "Not rounding down!");
1178 break;
1179 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001180 case ISD::AssertSext:
1181 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001182 case ISD::SIGN_EXTEND_INREG: {
1183 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1184 assert(VT == N1.getValueType() && "Not an inreg extend!");
1185 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1186 "Cannot *_EXTEND_INREG FP types");
1187 assert(EVT <= VT && "Not extending!");
1188 }
1189
Chris Lattner76365122005-01-16 02:23:22 +00001190 default: break;
1191 }
1192#endif
1193
Chris Lattnerc3aae252005-01-07 07:46:32 +00001194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1195 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1196 if (N1C) {
1197 if (N2C) {
1198 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1199 switch (Opcode) {
1200 case ISD::ADD: return getConstant(C1 + C2, VT);
1201 case ISD::SUB: return getConstant(C1 - C2, VT);
1202 case ISD::MUL: return getConstant(C1 * C2, VT);
1203 case ISD::UDIV:
1204 if (C2) return getConstant(C1 / C2, VT);
1205 break;
1206 case ISD::UREM :
1207 if (C2) return getConstant(C1 % C2, VT);
1208 break;
1209 case ISD::SDIV :
1210 if (C2) return getConstant(N1C->getSignExtended() /
1211 N2C->getSignExtended(), VT);
1212 break;
1213 case ISD::SREM :
1214 if (C2) return getConstant(N1C->getSignExtended() %
1215 N2C->getSignExtended(), VT);
1216 break;
1217 case ISD::AND : return getConstant(C1 & C2, VT);
1218 case ISD::OR : return getConstant(C1 | C2, VT);
1219 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001220 case ISD::SHL : return getConstant(C1 << C2, VT);
1221 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001222 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001223 case ISD::ROTL :
1224 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1225 VT);
1226 case ISD::ROTR :
1227 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1228 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001229 default: break;
1230 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001231 } else { // Cannonicalize constant to RHS if commutative
1232 if (isCommutativeBinOp(Opcode)) {
1233 std::swap(N1C, N2C);
1234 std::swap(N1, N2);
1235 }
1236 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001237 }
1238
1239 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1240 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001241 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 if (N2CFP) {
1243 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1244 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001245 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1246 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1247 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1248 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 if (C2) return getConstantFP(C1 / C2, VT);
1250 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001251 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001252 if (C2) return getConstantFP(fmod(C1, C2), VT);
1253 break;
1254 default: break;
1255 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001256 } else { // Cannonicalize constant to RHS if commutative
1257 if (isCommutativeBinOp(Opcode)) {
1258 std::swap(N1CFP, N2CFP);
1259 std::swap(N1, N2);
1260 }
1261 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 }
1263
Chris Lattnerc3aae252005-01-07 07:46:32 +00001264 // Finally, fold operations that do not require constants.
1265 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001266 case ISD::FP_ROUND_INREG:
1267 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1268 break;
1269 case ISD::SIGN_EXTEND_INREG: {
1270 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1271 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001272 break;
1273 }
1274
Nate Begemaneea805e2005-04-13 21:23:31 +00001275 // FIXME: figure out how to safely handle things like
1276 // int foo(int x) { return 1 << (x & 255); }
1277 // int bar() { return foo(256); }
1278#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001279 case ISD::SHL:
1280 case ISD::SRL:
1281 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001282 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001283 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001284 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001285 else if (N2.getOpcode() == ISD::AND)
1286 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1287 // If the and is only masking out bits that cannot effect the shift,
1288 // eliminate the and.
1289 unsigned NumBits = MVT::getSizeInBits(VT);
1290 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1291 return getNode(Opcode, VT, N1, N2.getOperand(0));
1292 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001293 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001294#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001295 }
1296
Chris Lattner27e9b412005-05-11 18:57:39 +00001297 // Memoize this node if possible.
1298 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001299 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1300 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001301 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1302 if (BON) return SDOperand(BON, 0);
1303
1304 BON = N = new SDNode(Opcode, N1, N2);
1305 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001306 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001307 }
1308
Chris Lattner3e011362005-05-14 07:45:46 +00001309 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310 AllNodes.push_back(N);
1311 return SDOperand(N, 0);
1312}
1313
Chris Lattnerc3aae252005-01-07 07:46:32 +00001314SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1315 SDOperand N1, SDOperand N2, SDOperand N3) {
1316 // Perform various simplifications.
1317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1318 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1319 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1320 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001321 case ISD::SETCC: {
1322 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001323 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001324 if (Simp.Val) return Simp;
1325 break;
1326 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001327 case ISD::SELECT:
1328 if (N1C)
1329 if (N1C->getValue())
1330 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001331 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 return N3; // select false, X, Y -> Y
1333
1334 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001335 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001336 case ISD::BRCOND:
1337 if (N2C)
1338 if (N2C->getValue()) // Unconditional branch
1339 return getNode(ISD::BR, MVT::Other, N1, N3);
1340 else
1341 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001342 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001343 }
1344
Chris Lattner385328c2005-05-14 07:42:29 +00001345 std::vector<SDOperand> Ops;
1346 Ops.reserve(3);
1347 Ops.push_back(N1);
1348 Ops.push_back(N2);
1349 Ops.push_back(N3);
1350
Chris Lattner43247a12005-08-25 19:12:10 +00001351 // Memoize node if it doesn't produce a flag.
1352 SDNode *N;
1353 if (VT != MVT::Flag) {
1354 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1355 if (E) return SDOperand(E, 0);
1356 E = N = new SDNode(Opcode, N1, N2, N3);
1357 } else {
1358 N = new SDNode(Opcode, N1, N2, N3);
1359 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001360 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 AllNodes.push_back(N);
1362 return SDOperand(N, 0);
1363}
1364
1365SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001366 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001367 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001368 std::vector<SDOperand> Ops;
1369 Ops.reserve(4);
1370 Ops.push_back(N1);
1371 Ops.push_back(N2);
1372 Ops.push_back(N3);
1373 Ops.push_back(N4);
1374 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001375}
1376
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001377SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1378 SDOperand N1, SDOperand N2, SDOperand N3,
1379 SDOperand N4, SDOperand N5) {
1380 std::vector<SDOperand> Ops;
1381 Ops.reserve(5);
1382 Ops.push_back(N1);
1383 Ops.push_back(N2);
1384 Ops.push_back(N3);
1385 Ops.push_back(N4);
1386 Ops.push_back(N5);
1387 return getNode(Opcode, VT, Ops);
1388}
1389
Evan Cheng7038daf2005-12-10 00:37:58 +00001390// setAdjCallChain - This method changes the token chain of an
1391// CALLSEQ_START/END node to be the specified operand.
1392void SDNode::setAdjCallChain(SDOperand N) {
1393 assert(N.getValueType() == MVT::Other);
1394 assert((getOpcode() == ISD::CALLSEQ_START ||
1395 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1396
1397 OperandList[0].Val->removeUser(this);
1398 OperandList[0] = N;
1399 OperandList[0].Val->Uses.push_back(this);
1400}
1401
Chris Lattner6a542892006-01-24 05:48:21 +00001402// setAdjCallFlag - This method changes the flag input of an
1403// CALLSEQ_START/END node to be the specified operand.
1404void SDNode::setAdjCallFlag(SDOperand N) {
1405 assert(N.getValueType() == MVT::Flag);
1406 assert((getOpcode() == ISD::CALLSEQ_START ||
1407 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1408
1409 SDOperand &FlagOp = OperandList[getNumOperands()-1];
1410 assert(FlagOp.getValueType() == MVT::Flag);
1411 FlagOp.Val->removeUser(this);
1412 FlagOp = N;
1413 FlagOp.Val->Uses.push_back(this);
1414}
Evan Cheng7038daf2005-12-10 00:37:58 +00001415
1416
1417SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1418 SDOperand Chain, SDOperand Ptr,
1419 SDOperand SV) {
1420 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1421 if (N) return SDOperand(N, 0);
1422 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1423
1424 // Loads have a token chain.
1425 setNodeValueTypes(N, VT, MVT::Other);
1426 AllNodes.push_back(N);
1427 return SDOperand(N, 0);
1428}
1429
1430SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1431 SDOperand Chain, SDOperand Ptr,
1432 SDOperand SV) {
1433 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1434 if (N) return SDOperand(N, 0);
1435 std::vector<SDOperand> Ops;
1436 Ops.reserve(5);
1437 Ops.push_back(Chain);
1438 Ops.push_back(Ptr);
1439 Ops.push_back(getConstant(Count, MVT::i32));
1440 Ops.push_back(getValueType(EVT));
1441 Ops.push_back(SV);
1442 std::vector<MVT::ValueType> VTs;
1443 VTs.reserve(2);
1444 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1445 return getNode(ISD::VLOAD, VTs, Ops);
1446}
1447
1448SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1449 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1450 MVT::ValueType EVT) {
1451 std::vector<SDOperand> Ops;
1452 Ops.reserve(4);
1453 Ops.push_back(Chain);
1454 Ops.push_back(Ptr);
1455 Ops.push_back(SV);
1456 Ops.push_back(getValueType(EVT));
1457 std::vector<MVT::ValueType> VTs;
1458 VTs.reserve(2);
1459 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1460 return getNode(Opcode, VTs, Ops);
1461}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001462
Chris Lattner0437cdd2005-05-09 04:14:13 +00001463SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001464 assert((!V || isa<PointerType>(V->getType())) &&
1465 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001466 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1467 if (N) return SDOperand(N, 0);
1468
1469 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001470 AllNodes.push_back(N);
1471 return SDOperand(N, 0);
1472}
1473
Nate Begemanacc398c2006-01-25 18:21:52 +00001474SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1475 SDOperand Chain, SDOperand Ptr,
1476 SDOperand SV) {
1477 std::vector<SDOperand> Ops;
1478 Ops.reserve(3);
1479 Ops.push_back(Chain);
1480 Ops.push_back(Ptr);
1481 Ops.push_back(SV);
1482 std::vector<MVT::ValueType> VTs;
1483 VTs.reserve(2);
1484 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1485 return getNode(ISD::VAARG, VTs, Ops);
1486}
1487
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001488SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001489 std::vector<SDOperand> &Ops) {
1490 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001491 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001492 case 1: return getNode(Opcode, VT, Ops[0]);
1493 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1494 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001495 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001496 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001497
Chris Lattner89c34632005-05-14 06:20:26 +00001498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001499 switch (Opcode) {
1500 default: break;
1501 case ISD::BRCONDTWOWAY:
1502 if (N1C)
1503 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001504 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001505 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001506 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001507 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001508 case ISD::BRTWOWAY_CC:
1509 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1510 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1511 "LHS and RHS of comparison must have same type!");
1512 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001513 case ISD::TRUNCSTORE: {
1514 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1515 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1516#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1517 // If this is a truncating store of a constant, convert to the desired type
1518 // and store it instead.
1519 if (isa<Constant>(Ops[0])) {
1520 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1521 if (isa<Constant>(Op))
1522 N1 = Op;
1523 }
1524 // Also for ConstantFP?
1525#endif
1526 if (Ops[0].getValueType() == EVT) // Normal store?
1527 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1528 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1529 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1530 "Can't do FP-INT conversion!");
1531 break;
1532 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001533 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001534 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001535 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1536 "LHS and RHS of condition must have same type!");
1537 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1538 "True and False arms of SelectCC must have same type!");
1539 assert(Ops[2].getValueType() == VT &&
1540 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001541 break;
1542 }
1543 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001544 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001545 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1546 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001547 break;
1548 }
Chris Lattneref847df2005-04-09 03:27:28 +00001549 }
1550
Chris Lattner385328c2005-05-14 07:42:29 +00001551 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001552 SDNode *N;
1553 if (VT != MVT::Flag) {
1554 SDNode *&E =
1555 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1556 if (E) return SDOperand(E, 0);
1557 E = N = new SDNode(Opcode, Ops);
1558 } else {
1559 N = new SDNode(Opcode, Ops);
1560 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001561 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001562 AllNodes.push_back(N);
1563 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001564}
1565
Chris Lattner89c34632005-05-14 06:20:26 +00001566SDOperand SelectionDAG::getNode(unsigned Opcode,
1567 std::vector<MVT::ValueType> &ResultTys,
1568 std::vector<SDOperand> &Ops) {
1569 if (ResultTys.size() == 1)
1570 return getNode(Opcode, ResultTys[0], Ops);
1571
Chris Lattner5f056bf2005-07-10 01:55:33 +00001572 switch (Opcode) {
1573 case ISD::EXTLOAD:
1574 case ISD::SEXTLOAD:
1575 case ISD::ZEXTLOAD: {
1576 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1577 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1578 // If they are asking for an extending load from/to the same thing, return a
1579 // normal load.
1580 if (ResultTys[0] == EVT)
1581 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1582 assert(EVT < ResultTys[0] &&
1583 "Should only be an extending load, not truncating!");
1584 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1585 "Cannot sign/zero extend a FP load!");
1586 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1587 "Cannot convert from FP to Int or Int -> FP!");
1588 break;
1589 }
1590
Chris Lattnere89083a2005-05-14 07:25:05 +00001591 // FIXME: figure out how to safely handle things like
1592 // int foo(int x) { return 1 << (x & 255); }
1593 // int bar() { return foo(256); }
1594#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001595 case ISD::SRA_PARTS:
1596 case ISD::SRL_PARTS:
1597 case ISD::SHL_PARTS:
1598 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001599 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001600 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1601 else if (N3.getOpcode() == ISD::AND)
1602 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1603 // If the and is only masking out bits that cannot effect the shift,
1604 // eliminate the and.
1605 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1606 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1607 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1608 }
1609 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001610#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001611 }
Chris Lattner89c34632005-05-14 06:20:26 +00001612
Chris Lattner43247a12005-08-25 19:12:10 +00001613 // Memoize the node unless it returns a flag.
1614 SDNode *N;
1615 if (ResultTys.back() != MVT::Flag) {
1616 SDNode *&E =
1617 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1618 if (E) return SDOperand(E, 0);
1619 E = N = new SDNode(Opcode, Ops);
1620 } else {
1621 N = new SDNode(Opcode, Ops);
1622 }
Chris Lattnera3255112005-11-08 23:30:28 +00001623 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001624 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001625 return SDOperand(N, 0);
1626}
1627
Chris Lattnera3255112005-11-08 23:30:28 +00001628void SelectionDAG::setNodeValueTypes(SDNode *N,
1629 std::vector<MVT::ValueType> &RetVals) {
1630 switch (RetVals.size()) {
1631 case 0: return;
1632 case 1: N->setValueTypes(RetVals[0]); return;
1633 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1634 default: break;
1635 }
1636
1637 std::list<std::vector<MVT::ValueType> >::iterator I =
1638 std::find(VTList.begin(), VTList.end(), RetVals);
1639 if (I == VTList.end()) {
1640 VTList.push_front(RetVals);
1641 I = VTList.begin();
1642 }
1643
1644 N->setValueTypes(&(*I)[0], I->size());
1645}
1646
1647void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1648 MVT::ValueType VT2) {
1649 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1650 E = VTList.end(); I != E; ++I) {
1651 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1652 N->setValueTypes(&(*I)[0], 2);
1653 return;
1654 }
1655 }
1656 std::vector<MVT::ValueType> V;
1657 V.push_back(VT1);
1658 V.push_back(VT2);
1659 VTList.push_front(V);
1660 N->setValueTypes(&(*VTList.begin())[0], 2);
1661}
1662
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001663/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1664/// specified operands. If the resultant node already exists in the DAG,
1665/// this does not modify the specified node, instead it returns the node that
1666/// already exists. If the resultant node does not exist in the DAG, the
1667/// input node is returned. As a degenerate case, if you specify the same
1668/// input operands as the node already has, the input node is returned.
1669SDOperand SelectionDAG::
1670UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1671 SDNode *N = InN.Val;
1672 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1673
1674 // Check to see if there is no change.
1675 if (Op == N->getOperand(0)) return InN;
1676
1677 // See if the modified node already exists.
1678 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1679 if (NewSlot && *NewSlot)
1680 return SDOperand(*NewSlot, InN.ResNo);
1681
1682 // Nope it doesn't. Remove the node from it's current place in the maps.
1683 if (NewSlot)
1684 RemoveNodeFromCSEMaps(N);
1685
1686 // Now we update the operands.
1687 N->OperandList[0].Val->removeUser(N);
1688 Op.Val->addUser(N);
1689 N->OperandList[0] = Op;
1690
1691 // If this gets put into a CSE map, add it.
1692 if (NewSlot) *NewSlot = N;
1693 return InN;
1694}
1695
1696SDOperand SelectionDAG::
1697UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1698 SDNode *N = InN.Val;
1699 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1700
1701 // Check to see if there is no change.
1702 bool AnyChange = false;
1703 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1704 return InN; // No operands changed, just return the input node.
1705
1706 // See if the modified node already exists.
1707 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1708 if (NewSlot && *NewSlot)
1709 return SDOperand(*NewSlot, InN.ResNo);
1710
1711 // Nope it doesn't. Remove the node from it's current place in the maps.
1712 if (NewSlot)
1713 RemoveNodeFromCSEMaps(N);
1714
1715 // Now we update the operands.
1716 if (N->OperandList[0] != Op1) {
1717 N->OperandList[0].Val->removeUser(N);
1718 Op1.Val->addUser(N);
1719 N->OperandList[0] = Op1;
1720 }
1721 if (N->OperandList[1] != Op2) {
1722 N->OperandList[1].Val->removeUser(N);
1723 Op2.Val->addUser(N);
1724 N->OperandList[1] = Op2;
1725 }
1726
1727 // If this gets put into a CSE map, add it.
1728 if (NewSlot) *NewSlot = N;
1729 return InN;
1730}
1731
1732SDOperand SelectionDAG::
1733UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1734 std::vector<SDOperand> Ops;
1735 Ops.push_back(Op1);
1736 Ops.push_back(Op2);
1737 Ops.push_back(Op3);
1738 return UpdateNodeOperands(N, Ops);
1739}
1740
1741SDOperand SelectionDAG::
1742UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1743 SDOperand Op3, SDOperand Op4) {
1744 std::vector<SDOperand> Ops;
1745 Ops.push_back(Op1);
1746 Ops.push_back(Op2);
1747 Ops.push_back(Op3);
1748 Ops.push_back(Op4);
1749 return UpdateNodeOperands(N, Ops);
1750}
1751
1752SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001753UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1754 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1755 std::vector<SDOperand> Ops;
1756 Ops.push_back(Op1);
1757 Ops.push_back(Op2);
1758 Ops.push_back(Op3);
1759 Ops.push_back(Op4);
1760 Ops.push_back(Op5);
1761 return UpdateNodeOperands(N, Ops);
1762}
1763
1764
1765SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001766UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1767 SDNode *N = InN.Val;
1768 assert(N->getNumOperands() == Ops.size() &&
1769 "Update with wrong number of operands");
1770
1771 // Check to see if there is no change.
1772 unsigned NumOps = Ops.size();
1773 bool AnyChange = false;
1774 for (unsigned i = 0; i != NumOps; ++i) {
1775 if (Ops[i] != N->getOperand(i)) {
1776 AnyChange = true;
1777 break;
1778 }
1779 }
1780
1781 // No operands changed, just return the input node.
1782 if (!AnyChange) return InN;
1783
1784 // See if the modified node already exists.
1785 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1786 if (NewSlot && *NewSlot)
1787 return SDOperand(*NewSlot, InN.ResNo);
1788
1789 // Nope it doesn't. Remove the node from it's current place in the maps.
1790 if (NewSlot)
1791 RemoveNodeFromCSEMaps(N);
1792
1793 // Now we update the operands.
1794 for (unsigned i = 0; i != NumOps; ++i) {
1795 if (N->OperandList[i] != Ops[i]) {
1796 N->OperandList[i].Val->removeUser(N);
1797 Ops[i].Val->addUser(N);
1798 N->OperandList[i] = Ops[i];
1799 }
1800 }
1801
1802 // If this gets put into a CSE map, add it.
1803 if (NewSlot) *NewSlot = N;
1804 return InN;
1805}
1806
1807
1808
Chris Lattner149c58c2005-08-16 18:17:10 +00001809
1810/// SelectNodeTo - These are used for target selectors to *mutate* the
1811/// specified node to have the specified return type, Target opcode, and
1812/// operands. Note that target opcodes are stored as
1813/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001814///
1815/// Note that SelectNodeTo returns the resultant node. If there is already a
1816/// node of the specified opcode and operands, it returns that node instead of
1817/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001818SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1819 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001820 // If an identical node already exists, use it.
1821 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1822 if (ON) return SDOperand(ON, 0);
1823
Chris Lattner7651fa42005-08-24 23:00:29 +00001824 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001825
Chris Lattner7651fa42005-08-24 23:00:29 +00001826 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1827 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001828
1829 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001830 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001831}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001832
Chris Lattnereb19e402005-11-30 22:45:14 +00001833SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1834 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001835 // If an identical node already exists, use it.
1836 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1837 std::make_pair(Op1, VT))];
1838 if (ON) return SDOperand(ON, 0);
1839
Chris Lattner149c58c2005-08-16 18:17:10 +00001840 RemoveNodeFromCSEMaps(N);
1841 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1842 N->setValueTypes(VT);
1843 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001844
1845 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001846 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001847}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001848
Chris Lattnereb19e402005-11-30 22:45:14 +00001849SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1850 MVT::ValueType VT, SDOperand Op1,
1851 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001852 // If an identical node already exists, use it.
1853 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1854 std::make_pair(Op1, Op2))];
1855 if (ON) return SDOperand(ON, 0);
1856
Chris Lattner149c58c2005-08-16 18:17:10 +00001857 RemoveNodeFromCSEMaps(N);
1858 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1859 N->setValueTypes(VT);
1860 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001861
1862 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001863 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001864}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001865
Chris Lattnereb19e402005-11-30 22:45:14 +00001866SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1867 MVT::ValueType VT, SDOperand Op1,
1868 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001869 // If an identical node already exists, use it.
1870 std::vector<SDOperand> OpList;
1871 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1872 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1873 std::make_pair(VT, OpList))];
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, Op3);
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 Lattnerc975e1d2005-08-21 22:30:30 +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,
1888 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001889 // If an identical node already exists, use it.
1890 std::vector<SDOperand> OpList;
1891 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1892 OpList.push_back(Op4);
1893 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1894 std::make_pair(VT, OpList))];
1895 if (ON) return SDOperand(ON, 0);
1896
Nate Begeman294a0a12005-08-18 07:30:15 +00001897 RemoveNodeFromCSEMaps(N);
1898 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1899 N->setValueTypes(VT);
1900 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001901
1902 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001903 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001904}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001905
Chris Lattnereb19e402005-11-30 22:45:14 +00001906SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1907 MVT::ValueType VT, SDOperand Op1,
1908 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1909 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001910 // If an identical node already exists, use it.
1911 std::vector<SDOperand> OpList;
1912 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1913 OpList.push_back(Op4); OpList.push_back(Op5);
1914 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1915 std::make_pair(VT, OpList))];
1916 if (ON) return SDOperand(ON, 0);
1917
Chris Lattner6b09a292005-08-21 18:49:33 +00001918 RemoveNodeFromCSEMaps(N);
1919 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1920 N->setValueTypes(VT);
1921 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001922
1923 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001924 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001925}
Chris Lattner149c58c2005-08-16 18:17:10 +00001926
Chris Lattnereb19e402005-11-30 22:45:14 +00001927SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1928 MVT::ValueType VT, SDOperand Op1,
1929 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1930 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001931 // If an identical node already exists, use it.
1932 std::vector<SDOperand> OpList;
1933 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1934 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1935 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1936 std::make_pair(VT, OpList))];
1937 if (ON) return SDOperand(ON, 0);
1938
Evan Cheng61ca74b2005-11-30 02:04:11 +00001939 RemoveNodeFromCSEMaps(N);
1940 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1941 N->setValueTypes(VT);
1942 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001943
1944 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001945 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001946}
1947
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001948SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1949 MVT::ValueType VT, SDOperand Op1,
1950 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1951 SDOperand Op5, SDOperand Op6,
1952 SDOperand Op7) {
1953 // If an identical node already exists, use it.
1954 std::vector<SDOperand> OpList;
1955 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1956 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1957 OpList.push_back(Op7);
1958 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1959 std::make_pair(VT, OpList))];
1960 if (ON) return SDOperand(ON, 0);
1961
1962 RemoveNodeFromCSEMaps(N);
1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1964 N->setValueTypes(VT);
1965 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1966
1967 ON = N; // Memoize the new node.
1968 return SDOperand(N, 0);
1969}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001970SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1971 MVT::ValueType VT, SDOperand Op1,
1972 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1973 SDOperand Op5, SDOperand Op6,
1974 SDOperand Op7, SDOperand Op8) {
1975 // If an identical node already exists, use it.
1976 std::vector<SDOperand> OpList;
1977 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1978 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1979 OpList.push_back(Op7); OpList.push_back(Op8);
1980 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1981 std::make_pair(VT, OpList))];
1982 if (ON) return SDOperand(ON, 0);
1983
1984 RemoveNodeFromCSEMaps(N);
1985 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1986 N->setValueTypes(VT);
1987 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1988
1989 ON = N; // Memoize the new node.
1990 return SDOperand(N, 0);
1991}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001992
Chris Lattnereb19e402005-11-30 22:45:14 +00001993SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1994 MVT::ValueType VT1, MVT::ValueType VT2,
1995 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001996 // If an identical node already exists, use it.
1997 std::vector<SDOperand> OpList;
1998 OpList.push_back(Op1); OpList.push_back(Op2);
1999 std::vector<MVT::ValueType> VTList;
2000 VTList.push_back(VT1); VTList.push_back(VT2);
2001 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2002 std::make_pair(VTList, OpList))];
2003 if (ON) return SDOperand(ON, 0);
2004
Chris Lattner0fb094f2005-11-19 01:44:53 +00002005 RemoveNodeFromCSEMaps(N);
2006 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2007 setNodeValueTypes(N, VT1, VT2);
2008 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002009
2010 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002011 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002012}
2013
Chris Lattnereb19e402005-11-30 22:45:14 +00002014SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2015 MVT::ValueType VT1, MVT::ValueType VT2,
2016 SDOperand Op1, SDOperand Op2,
2017 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002018 // If an identical node already exists, use it.
2019 std::vector<SDOperand> OpList;
2020 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2021 std::vector<MVT::ValueType> VTList;
2022 VTList.push_back(VT1); VTList.push_back(VT2);
2023 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2024 std::make_pair(VTList, OpList))];
2025 if (ON) return SDOperand(ON, 0);
2026
Chris Lattner0fb094f2005-11-19 01:44:53 +00002027 RemoveNodeFromCSEMaps(N);
2028 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2029 setNodeValueTypes(N, VT1, VT2);
2030 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002031
2032 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002033 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002034}
2035
Chris Lattnereb19e402005-11-30 22:45:14 +00002036SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2037 MVT::ValueType VT1, MVT::ValueType VT2,
2038 SDOperand Op1, SDOperand Op2,
2039 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002040 // If an identical node already exists, use it.
2041 std::vector<SDOperand> OpList;
2042 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2043 OpList.push_back(Op4);
2044 std::vector<MVT::ValueType> VTList;
2045 VTList.push_back(VT1); VTList.push_back(VT2);
2046 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2047 std::make_pair(VTList, OpList))];
2048 if (ON) return SDOperand(ON, 0);
2049
Chris Lattner0fb094f2005-11-19 01:44:53 +00002050 RemoveNodeFromCSEMaps(N);
2051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2052 setNodeValueTypes(N, VT1, VT2);
2053 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002054
2055 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002056 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002057}
2058
Chris Lattnereb19e402005-11-30 22:45:14 +00002059SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2060 MVT::ValueType VT1, MVT::ValueType VT2,
2061 SDOperand Op1, SDOperand Op2,
2062 SDOperand Op3, SDOperand Op4,
2063 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002064 // If an identical node already exists, use it.
2065 std::vector<SDOperand> OpList;
2066 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2067 OpList.push_back(Op4); OpList.push_back(Op5);
2068 std::vector<MVT::ValueType> VTList;
2069 VTList.push_back(VT1); VTList.push_back(VT2);
2070 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2071 std::make_pair(VTList, OpList))];
2072 if (ON) return SDOperand(ON, 0);
2073
Chris Lattner0fb094f2005-11-19 01:44:53 +00002074 RemoveNodeFromCSEMaps(N);
2075 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2076 setNodeValueTypes(N, VT1, VT2);
2077 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002078
2079 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002080 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002081}
2082
2083// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002084/// This can cause recursive merging of nodes in the DAG.
2085///
Chris Lattner8b52f212005-08-26 18:36:28 +00002086/// This version assumes From/To have a single result value.
2087///
Chris Lattner1e111c72005-09-07 05:37:01 +00002088void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2089 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002090 SDNode *From = FromN.Val, *To = ToN.Val;
2091 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2092 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002093 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002094
Chris Lattner8b8749f2005-08-17 19:00:20 +00002095 while (!From->use_empty()) {
2096 // Process users until they are all gone.
2097 SDNode *U = *From->use_begin();
2098
2099 // This node is about to morph, remove its old self from the CSE maps.
2100 RemoveNodeFromCSEMaps(U);
2101
Chris Lattner65113b22005-11-08 22:07:03 +00002102 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2103 I != E; ++I)
2104 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002105 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002106 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002107 To->addUser(U);
2108 }
2109
2110 // Now that we have modified U, add it back to the CSE maps. If it already
2111 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002112 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2113 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002114 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002115 if (Deleted) Deleted->push_back(U);
2116 DeleteNodeNotInCSEMaps(U);
2117 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002118 }
2119}
2120
2121/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2122/// This can cause recursive merging of nodes in the DAG.
2123///
2124/// This version assumes From/To have matching types and numbers of result
2125/// values.
2126///
Chris Lattner1e111c72005-09-07 05:37:01 +00002127void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2128 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002129 assert(From != To && "Cannot replace uses of with self");
2130 assert(From->getNumValues() == To->getNumValues() &&
2131 "Cannot use this version of ReplaceAllUsesWith!");
2132 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002133 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002134 return;
2135 }
2136
2137 while (!From->use_empty()) {
2138 // Process users until they are all gone.
2139 SDNode *U = *From->use_begin();
2140
2141 // This node is about to morph, remove its old self from the CSE maps.
2142 RemoveNodeFromCSEMaps(U);
2143
Chris Lattner65113b22005-11-08 22:07:03 +00002144 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2145 I != E; ++I)
2146 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002147 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002148 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002149 To->addUser(U);
2150 }
2151
2152 // Now that we have modified U, add it back to the CSE maps. If it already
2153 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002154 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2155 ReplaceAllUsesWith(U, Existing, Deleted);
2156 // U is now dead.
2157 if (Deleted) Deleted->push_back(U);
2158 DeleteNodeNotInCSEMaps(U);
2159 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002160 }
2161}
2162
Chris Lattner8b52f212005-08-26 18:36:28 +00002163/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2164/// This can cause recursive merging of nodes in the DAG.
2165///
2166/// This version can replace From with any result values. To must match the
2167/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002168void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002169 const std::vector<SDOperand> &To,
2170 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002171 assert(From->getNumValues() == To.size() &&
2172 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002173 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002174 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002175 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002176 return;
2177 }
2178
2179 while (!From->use_empty()) {
2180 // Process users until they are all gone.
2181 SDNode *U = *From->use_begin();
2182
2183 // This node is about to morph, remove its old self from the CSE maps.
2184 RemoveNodeFromCSEMaps(U);
2185
Chris Lattner65113b22005-11-08 22:07:03 +00002186 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2187 I != E; ++I)
2188 if (I->Val == From) {
2189 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002190 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002191 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002192 ToOp.Val->addUser(U);
2193 }
2194
2195 // Now that we have modified U, add it back to the CSE maps. If it already
2196 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002197 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2198 ReplaceAllUsesWith(U, Existing, Deleted);
2199 // U is now dead.
2200 if (Deleted) Deleted->push_back(U);
2201 DeleteNodeNotInCSEMaps(U);
2202 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002203 }
2204}
2205
2206
Jim Laskey58b968b2005-08-17 20:08:02 +00002207//===----------------------------------------------------------------------===//
2208// SDNode Class
2209//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002210
Chris Lattnera3255112005-11-08 23:30:28 +00002211
2212/// getValueTypeList - Return a pointer to the specified value type.
2213///
2214MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2215 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2216 VTs[VT] = VT;
2217 return &VTs[VT];
2218}
2219
Chris Lattner5c884562005-01-12 18:37:47 +00002220/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2221/// indicated value. This method ignores uses of other values defined by this
2222/// operation.
2223bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2224 assert(Value < getNumValues() && "Bad value!");
2225
2226 // If there is only one value, this is easy.
2227 if (getNumValues() == 1)
2228 return use_size() == NUses;
2229 if (Uses.size() < NUses) return false;
2230
2231 SDOperand TheValue(this, Value);
2232
2233 std::set<SDNode*> UsersHandled;
2234
2235 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2236 UI != E; ++UI) {
2237 SDNode *User = *UI;
2238 if (User->getNumOperands() == 1 ||
2239 UsersHandled.insert(User).second) // First time we've seen this?
2240 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2241 if (User->getOperand(i) == TheValue) {
2242 if (NUses == 0)
2243 return false; // too many uses
2244 --NUses;
2245 }
2246 }
2247
2248 // Found exactly the right number of uses?
2249 return NUses == 0;
2250}
2251
2252
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002253const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002254 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002255 default:
2256 if (getOpcode() < ISD::BUILTIN_OP_END)
2257 return "<<Unknown DAG Node>>";
2258 else {
Evan Cheng72261582005-12-20 06:22:03 +00002259 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002260 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002261 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2262 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002263
Evan Cheng72261582005-12-20 06:22:03 +00002264 TargetLowering &TLI = G->getTargetLoweringInfo();
2265 const char *Name =
2266 TLI.getTargetNodeName(getOpcode());
2267 if (Name) return Name;
2268 }
2269
2270 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002271 }
2272
Andrew Lenharth95762122005-03-31 21:24:06 +00002273 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002274 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002275 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002276 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002277 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002278 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002279 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002280 case ISD::AssertSext: return "AssertSext";
2281 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002282 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002283 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002284 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002285 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002286 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002287 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002288 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002289 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002290 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002291 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002292 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002293 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002294 case ISD::ConstantPool: return "ConstantPool";
2295 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002296 case ISD::CopyToReg: return "CopyToReg";
2297 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002298 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002299 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002300 case ISD::INLINEASM: return "inlineasm";
2301
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002302 // Unary operators
2303 case ISD::FABS: return "fabs";
2304 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002305 case ISD::FSQRT: return "fsqrt";
2306 case ISD::FSIN: return "fsin";
2307 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002308
2309 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002310 case ISD::ADD: return "add";
2311 case ISD::SUB: return "sub";
2312 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002313 case ISD::MULHU: return "mulhu";
2314 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002315 case ISD::SDIV: return "sdiv";
2316 case ISD::UDIV: return "udiv";
2317 case ISD::SREM: return "srem";
2318 case ISD::UREM: return "urem";
2319 case ISD::AND: return "and";
2320 case ISD::OR: return "or";
2321 case ISD::XOR: return "xor";
2322 case ISD::SHL: return "shl";
2323 case ISD::SRA: return "sra";
2324 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002325 case ISD::ROTL: return "rotl";
2326 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002327 case ISD::FADD: return "fadd";
2328 case ISD::FSUB: return "fsub";
2329 case ISD::FMUL: return "fmul";
2330 case ISD::FDIV: return "fdiv";
2331 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002332 case ISD::VADD: return "vadd";
2333 case ISD::VSUB: return "vsub";
2334 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002335
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002336 case ISD::SETCC: return "setcc";
2337 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002338 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002339 case ISD::ADD_PARTS: return "add_parts";
2340 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002341 case ISD::SHL_PARTS: return "shl_parts";
2342 case ISD::SRA_PARTS: return "sra_parts";
2343 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002344
Chris Lattner7f644642005-04-28 21:44:03 +00002345 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002346 case ISD::SIGN_EXTEND: return "sign_extend";
2347 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002348 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002349 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002350 case ISD::TRUNCATE: return "truncate";
2351 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002352 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002353 case ISD::FP_EXTEND: return "fp_extend";
2354
2355 case ISD::SINT_TO_FP: return "sint_to_fp";
2356 case ISD::UINT_TO_FP: return "uint_to_fp";
2357 case ISD::FP_TO_SINT: return "fp_to_sint";
2358 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002359 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002360
2361 // Control flow instructions
2362 case ISD::BR: return "br";
2363 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002364 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002365 case ISD::BR_CC: return "br_cc";
2366 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002367 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002368 case ISD::CALLSEQ_START: return "callseq_start";
2369 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002370
2371 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002372 case ISD::LOAD: return "load";
2373 case ISD::STORE: return "store";
2374 case ISD::VLOAD: return "vload";
2375 case ISD::EXTLOAD: return "extload";
2376 case ISD::SEXTLOAD: return "sextload";
2377 case ISD::ZEXTLOAD: return "zextload";
2378 case ISD::TRUNCSTORE: return "truncstore";
2379 case ISD::VAARG: return "vaarg";
2380 case ISD::VACOPY: return "vacopy";
2381 case ISD::VAEND: return "vaend";
2382 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002383 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002384 case ISD::EXTRACT_ELEMENT: return "extract_element";
2385 case ISD::BUILD_PAIR: return "build_pair";
2386 case ISD::STACKSAVE: return "stacksave";
2387 case ISD::STACKRESTORE: return "stackrestore";
2388
2389 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002390 case ISD::MEMSET: return "memset";
2391 case ISD::MEMCPY: return "memcpy";
2392 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002393
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002394 // Bit manipulation
2395 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002396 case ISD::CTPOP: return "ctpop";
2397 case ISD::CTTZ: return "cttz";
2398 case ISD::CTLZ: return "ctlz";
2399
2400 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002401 case ISD::READPORT: return "readport";
2402 case ISD::WRITEPORT: return "writeport";
2403 case ISD::READIO: return "readio";
2404 case ISD::WRITEIO: return "writeio";
2405
Chris Lattner36ce6912005-11-29 06:21:05 +00002406 // Debug info
2407 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002408 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002409 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002410
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002411 case ISD::CONDCODE:
2412 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002413 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002414 case ISD::SETOEQ: return "setoeq";
2415 case ISD::SETOGT: return "setogt";
2416 case ISD::SETOGE: return "setoge";
2417 case ISD::SETOLT: return "setolt";
2418 case ISD::SETOLE: return "setole";
2419 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002420
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002421 case ISD::SETO: return "seto";
2422 case ISD::SETUO: return "setuo";
2423 case ISD::SETUEQ: return "setue";
2424 case ISD::SETUGT: return "setugt";
2425 case ISD::SETUGE: return "setuge";
2426 case ISD::SETULT: return "setult";
2427 case ISD::SETULE: return "setule";
2428 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002429
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002430 case ISD::SETEQ: return "seteq";
2431 case ISD::SETGT: return "setgt";
2432 case ISD::SETGE: return "setge";
2433 case ISD::SETLT: return "setlt";
2434 case ISD::SETLE: return "setle";
2435 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002436 }
2437 }
2438}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002439
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002440void SDNode::dump() const { dump(0); }
2441void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002442 std::cerr << (void*)this << ": ";
2443
2444 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2445 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002446 if (getValueType(i) == MVT::Other)
2447 std::cerr << "ch";
2448 else
2449 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002450 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002451 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002452
2453 std::cerr << " ";
2454 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2455 if (i) std::cerr << ", ";
2456 std::cerr << (void*)getOperand(i).Val;
2457 if (unsigned RN = getOperand(i).ResNo)
2458 std::cerr << ":" << RN;
2459 }
2460
2461 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2462 std::cerr << "<" << CSDN->getValue() << ">";
2463 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2464 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002465 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002466 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002467 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002468 std::cerr << "<";
2469 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002470 if (offset > 0)
2471 std::cerr << " + " << offset;
2472 else
2473 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002474 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002475 std::cerr << "<" << FIDN->getIndex() << ">";
2476 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002477 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002478 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002479 std::cerr << "<";
2480 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2481 if (LBB)
2482 std::cerr << LBB->getName() << " ";
2483 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002484 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002485 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002486 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2487 } else {
2488 std::cerr << " #" << R->getReg();
2489 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002490 } else if (const ExternalSymbolSDNode *ES =
2491 dyn_cast<ExternalSymbolSDNode>(this)) {
2492 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002493 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2494 if (M->getValue())
2495 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2496 else
2497 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002498 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2499 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002501}
2502
Chris Lattnerde202b32005-11-09 23:47:37 +00002503static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002504 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2505 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002506 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002507 else
2508 std::cerr << "\n" << std::string(indent+2, ' ')
2509 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002510
Chris Lattnerea946cd2005-01-09 20:38:33 +00002511
2512 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002513 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002514}
2515
Chris Lattnerc3aae252005-01-07 07:46:32 +00002516void SelectionDAG::dump() const {
2517 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002518 std::vector<const SDNode*> Nodes;
2519 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2520 I != E; ++I)
2521 Nodes.push_back(I);
2522
Chris Lattner49d24712005-01-09 20:26:36 +00002523 std::sort(Nodes.begin(), Nodes.end());
2524
2525 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002526 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002527 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002528 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002529
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002530 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002531
Chris Lattnerc3aae252005-01-07 07:46:32 +00002532 std::cerr << "\n\n";
2533}
2534