blob: aa9ea638f8e44341644b2d56d7a763e6b39549b2 [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 Lattner70814bc2006-01-29 07:58:15 +0000383 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000384 N->dump();
385 assert(0 && "Node is not in map!");
386 }
387#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000388}
389
Chris Lattner8b8749f2005-08-17 19:00:20 +0000390/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
391/// has been taken out and modified in some way. If the specified node already
392/// exists in the CSE maps, do not modify the maps, but return the existing node
393/// instead. If it doesn't exist, add it and return null.
394///
395SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
396 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000397 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000398 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000399
Chris Lattner9f8cc692005-12-19 22:21:21 +0000400 // Check that remaining values produced are not flags.
401 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
402 if (N->getValueType(i) == MVT::Flag)
403 return 0; // Never CSE anything that produces a flag.
404
Chris Lattnerfe14b342005-12-01 23:14:50 +0000405 if (N->getNumValues() == 1) {
406 if (N->getNumOperands() == 1) {
407 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
408 std::make_pair(N->getOperand(0),
409 N->getValueType(0)))];
410 if (U) return U;
411 U = N;
412 } else if (N->getNumOperands() == 2) {
413 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
414 std::make_pair(N->getOperand(0),
415 N->getOperand(1)))];
416 if (B) return B;
417 B = N;
418 } else {
419 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
420 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000421 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000422 if (ORN) return ORN;
423 ORN = N;
424 }
425 } else {
426 if (N->getOpcode() == ISD::LOAD) {
427 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
428 std::make_pair(N->getOperand(0),
429 N->getValueType(0)))];
430 if (L) return L;
431 L = N;
432 } else {
433 // Remove the node from the ArbitraryNodes map.
434 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
435 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
436 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
437 std::make_pair(RV, Ops))];
438 if (AN) return AN;
439 AN = N;
440 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000441 }
442 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000443}
444
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000445/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
446/// were replaced with those specified. If this node is never memoized,
447/// return null, otherwise return a pointer to the slot it would take. If a
448/// node already exists with these operands, the slot will be non-null.
449SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000450 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000451 return 0; // Never add these nodes.
452
453 // Check that remaining values produced are not flags.
454 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
455 if (N->getValueType(i) == MVT::Flag)
456 return 0; // Never CSE anything that produces a flag.
457
458 if (N->getNumValues() == 1) {
459 return &UnaryOps[std::make_pair(N->getOpcode(),
460 std::make_pair(Op, N->getValueType(0)))];
461 } else {
462 // Remove the node from the ArbitraryNodes map.
463 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
464 std::vector<SDOperand> Ops;
465 Ops.push_back(Op);
466 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
467 std::make_pair(RV, Ops))];
468 }
469 return 0;
470}
471
472/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
473/// were replaced with those specified. If this node is never memoized,
474/// return null, otherwise return a pointer to the slot it would take. If a
475/// node already exists with these operands, the slot will be non-null.
476SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
477 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000478 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000479 return 0; // Never add these nodes.
480
481 // Check that remaining values produced are not flags.
482 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
483 if (N->getValueType(i) == MVT::Flag)
484 return 0; // Never CSE anything that produces a flag.
485
486 if (N->getNumValues() == 1) {
487 return &BinaryOps[std::make_pair(N->getOpcode(),
488 std::make_pair(Op1, Op2))];
489 } else {
490 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
491 std::vector<SDOperand> Ops;
492 Ops.push_back(Op1);
493 Ops.push_back(Op2);
494 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
495 std::make_pair(RV, Ops))];
496 }
497 return 0;
498}
499
500
501/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
502/// were replaced with those specified. If this node is never memoized,
503/// return null, otherwise return a pointer to the slot it would take. If a
504/// node already exists with these operands, the slot will be non-null.
505SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
506 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000507 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000508 return 0; // Never add these nodes.
509
510 // Check that remaining values produced are not flags.
511 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
512 if (N->getValueType(i) == MVT::Flag)
513 return 0; // Never CSE anything that produces a flag.
514
515 if (N->getNumValues() == 1) {
516 if (N->getNumOperands() == 1) {
517 return &UnaryOps[std::make_pair(N->getOpcode(),
518 std::make_pair(Ops[0],
519 N->getValueType(0)))];
520 } else if (N->getNumOperands() == 2) {
521 return &BinaryOps[std::make_pair(N->getOpcode(),
522 std::make_pair(Ops[0], Ops[1]))];
523 } else {
524 return &OneResultNodes[std::make_pair(N->getOpcode(),
525 std::make_pair(N->getValueType(0),
526 Ops))];
527 }
528 } else {
529 if (N->getOpcode() == ISD::LOAD) {
530 return &Loads[std::make_pair(Ops[1],
531 std::make_pair(Ops[0], N->getValueType(0)))];
532 } else {
533 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
534 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
535 std::make_pair(RV, Ops))];
536 }
537 }
538 return 0;
539}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000540
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000541
Chris Lattner78ec3112003-08-11 14:57:33 +0000542SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000543 while (!AllNodes.empty()) {
544 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000545 delete [] N->OperandList;
546 N->OperandList = 0;
547 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000548 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000549 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000550}
551
Chris Lattner0f2287b2005-04-13 02:38:18 +0000552SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000553 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000554 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000555 return getNode(ISD::AND, Op.getValueType(), Op,
556 getConstant(Imm, Op.getValueType()));
557}
558
Chris Lattnerc3aae252005-01-07 07:46:32 +0000559SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
560 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
561 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000562 if (VT != MVT::i64)
563 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000564
Chris Lattnerc3aae252005-01-07 07:46:32 +0000565 SDNode *&N = Constants[std::make_pair(Val, VT)];
566 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000567 N = new ConstantSDNode(false, Val, VT);
568 AllNodes.push_back(N);
569 return SDOperand(N, 0);
570}
571
Chris Lattner36ce6912005-11-29 06:21:05 +0000572SDOperand SelectionDAG::getString(const std::string &Val) {
573 StringSDNode *&N = StringNodes[Val];
574 if (!N) {
575 N = new StringSDNode(Val);
576 AllNodes.push_back(N);
577 }
578 return SDOperand(N, 0);
579}
580
Chris Lattner37bfbb42005-08-17 00:34:06 +0000581SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
582 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
583 // Mask out any bits that are not valid for this constant.
584 if (VT != MVT::i64)
585 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
586
587 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
588 if (N) return SDOperand(N, 0);
589 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000590 AllNodes.push_back(N);
591 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000592}
593
Chris Lattnerc3aae252005-01-07 07:46:32 +0000594SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
595 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
596 if (VT == MVT::f32)
597 Val = (float)Val; // Mask out extra precision.
598
Chris Lattnerd8658612005-02-17 20:17:32 +0000599 // Do the map lookup using the actual bit pattern for the floating point
600 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
601 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000602 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000603 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000604 N = new ConstantFPSDNode(false, Val, VT);
605 AllNodes.push_back(N);
606 return SDOperand(N, 0);
607}
608
609SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
610 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
611 if (VT == MVT::f32)
612 Val = (float)Val; // Mask out extra precision.
613
614 // Do the map lookup using the actual bit pattern for the floating point
615 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
616 // we don't have issues with SNANs.
617 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
618 if (N) return SDOperand(N, 0);
619 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000620 AllNodes.push_back(N);
621 return SDOperand(N, 0);
622}
623
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000625 MVT::ValueType VT, int offset) {
626 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000627 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000628 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000629 AllNodes.push_back(N);
630 return SDOperand(N, 0);
631}
632
633SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000634 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000635 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000636 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000637 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000638 AllNodes.push_back(N);
639 return SDOperand(N, 0);
640}
641
642SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
643 SDNode *&N = FrameIndices[FI];
644 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000645 N = new FrameIndexSDNode(FI, VT, false);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
648}
649
650SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
651 SDNode *&N = TargetFrameIndices[FI];
652 if (N) return SDOperand(N, 0);
653 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000654 AllNodes.push_back(N);
655 return SDOperand(N, 0);
656}
657
Chris Lattner5839bf22005-08-26 17:15:30 +0000658SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
659 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000660 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000661 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000662 AllNodes.push_back(N);
663 return SDOperand(N, 0);
664}
665
Chris Lattner5839bf22005-08-26 17:15:30 +0000666SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
667 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000668 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000669 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000670 AllNodes.push_back(N);
671 return SDOperand(N, 0);
672}
673
674SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
675 SDNode *&N = BBNodes[MBB];
676 if (N) return SDOperand(N, 0);
677 N = new BasicBlockSDNode(MBB);
678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
680}
681
Chris Lattner15e4b012005-07-10 00:07:11 +0000682SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
683 if ((unsigned)VT >= ValueTypeNodes.size())
684 ValueTypeNodes.resize(VT+1);
685 if (ValueTypeNodes[VT] == 0) {
686 ValueTypeNodes[VT] = new VTSDNode(VT);
687 AllNodes.push_back(ValueTypeNodes[VT]);
688 }
689
690 return SDOperand(ValueTypeNodes[VT], 0);
691}
692
Chris Lattnerc3aae252005-01-07 07:46:32 +0000693SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
694 SDNode *&N = ExternalSymbols[Sym];
695 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000696 N = new ExternalSymbolSDNode(false, Sym, VT);
697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
699}
700
Chris Lattner809ec112006-01-28 10:09:25 +0000701SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
702 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000703 SDNode *&N = TargetExternalSymbols[Sym];
704 if (N) return SDOperand(N, 0);
705 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 AllNodes.push_back(N);
707 return SDOperand(N, 0);
708}
709
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000710SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
711 if ((unsigned)Cond >= CondCodeNodes.size())
712 CondCodeNodes.resize(Cond+1);
713
Chris Lattner079a27a2005-08-09 20:40:02 +0000714 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000715 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000716 AllNodes.push_back(CondCodeNodes[Cond]);
717 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000718 return SDOperand(CondCodeNodes[Cond], 0);
719}
720
Chris Lattner0fdd7682005-08-30 22:38:38 +0000721SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
722 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
723 if (!Reg) {
724 Reg = new RegisterSDNode(RegNo, VT);
725 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000726 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000727 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000728}
729
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000730SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
731 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 // These setcc operations always fold.
733 switch (Cond) {
734 default: break;
735 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000736 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000738 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000739 }
740
Chris Lattner67255a12005-04-07 18:14:58 +0000741 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
742 uint64_t C2 = N2C->getValue();
743 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
744 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000745
Chris Lattnerc3aae252005-01-07 07:46:32 +0000746 // Sign extend the operands if required
747 if (ISD::isSignedIntSetCC(Cond)) {
748 C1 = N1C->getSignExtended();
749 C2 = N2C->getSignExtended();
750 }
751
752 switch (Cond) {
753 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000754 case ISD::SETEQ: return getConstant(C1 == C2, VT);
755 case ISD::SETNE: return getConstant(C1 != C2, VT);
756 case ISD::SETULT: return getConstant(C1 < C2, VT);
757 case ISD::SETUGT: return getConstant(C1 > C2, VT);
758 case ISD::SETULE: return getConstant(C1 <= C2, VT);
759 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
760 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
761 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
762 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
763 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000764 }
Chris Lattner24673922005-04-07 18:58:54 +0000765 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000766 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000767 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
768 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
769
770 // If the comparison constant has bits in the upper part, the
771 // zero-extended value could never match.
772 if (C2 & (~0ULL << InSize)) {
773 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
774 switch (Cond) {
775 case ISD::SETUGT:
776 case ISD::SETUGE:
777 case ISD::SETEQ: return getConstant(0, VT);
778 case ISD::SETULT:
779 case ISD::SETULE:
780 case ISD::SETNE: return getConstant(1, VT);
781 case ISD::SETGT:
782 case ISD::SETGE:
783 // True if the sign bit of C2 is set.
784 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
785 case ISD::SETLT:
786 case ISD::SETLE:
787 // True if the sign bit of C2 isn't set.
788 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
789 default:
790 break;
791 }
792 }
793
794 // Otherwise, we can perform the comparison with the low bits.
795 switch (Cond) {
796 case ISD::SETEQ:
797 case ISD::SETNE:
798 case ISD::SETUGT:
799 case ISD::SETUGE:
800 case ISD::SETULT:
801 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000802 return getSetCC(VT, N1.getOperand(0),
803 getConstant(C2, N1.getOperand(0).getValueType()),
804 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000805 default:
806 break; // todo, be more careful with signed comparisons
807 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000808 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
809 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
810 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
811 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
812 MVT::ValueType ExtDstTy = N1.getValueType();
813 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000814
Chris Lattner7b2880c2005-08-24 22:44:39 +0000815 // If the extended part has any inconsistent bits, it cannot ever
816 // compare equal. In other words, they have to be all ones or all
817 // zeros.
818 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000819 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000820 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
821 return getConstant(Cond == ISD::SETNE, VT);
822
823 // Otherwise, make this a use of a zext.
824 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000825 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000826 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000827 }
828
Chris Lattner67255a12005-04-07 18:14:58 +0000829 uint64_t MinVal, MaxVal;
830 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
831 if (ISD::isSignedIntSetCC(Cond)) {
832 MinVal = 1ULL << (OperandBitSize-1);
833 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
834 MaxVal = ~0ULL >> (65-OperandBitSize);
835 else
836 MaxVal = 0;
837 } else {
838 MinVal = 0;
839 MaxVal = ~0ULL >> (64-OperandBitSize);
840 }
841
842 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
843 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
844 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
845 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000846 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
847 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000848 }
849
850 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
851 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
852 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000853 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
854 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000855 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000856
Nate Begeman72ea2812005-04-14 08:56:52 +0000857 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
858 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000859
Nate Begeman72ea2812005-04-14 08:56:52 +0000860 // Canonicalize setgt X, Min --> setne X, Min
861 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000862 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000863
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000864 // If we have setult X, 1, turn it into seteq X, 0
865 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000866 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
867 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000868 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000869 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000870 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
871 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000872
873 // If we have "setcc X, C1", check to see if we can shrink the immediate
874 // by changing cc.
875
876 // SETUGT X, SINTMAX -> SETLT X, 0
877 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
878 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000879 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000880
881 // FIXME: Implement the rest of these.
882
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000883
884 // Fold bit comparisons when we can.
885 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
886 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
887 if (ConstantSDNode *AndRHS =
888 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
889 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
890 // Perform the xform if the AND RHS is a single bit.
891 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
892 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000893 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000894 TLI.getShiftAmountTy()));
895 }
896 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
897 // (X & 8) == 8 --> (X & 8) >> 3
898 // Perform the xform if C2 is a single bit.
899 if ((C2 & (C2-1)) == 0) {
900 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000901 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000902 }
903 }
904 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905 }
Chris Lattner67255a12005-04-07 18:14:58 +0000906 } else if (isa<ConstantSDNode>(N1.Val)) {
907 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000908 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000909 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910
911 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
912 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
913 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000914
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915 switch (Cond) {
916 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000917 case ISD::SETEQ: return getConstant(C1 == C2, VT);
918 case ISD::SETNE: return getConstant(C1 != C2, VT);
919 case ISD::SETLT: return getConstant(C1 < C2, VT);
920 case ISD::SETGT: return getConstant(C1 > C2, VT);
921 case ISD::SETLE: return getConstant(C1 <= C2, VT);
922 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 }
924 } else {
925 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000926 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000927 }
928
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000929 // Could not fold it.
930 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000931}
932
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000934///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000935SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000936 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
937 if (!N) {
938 N = new SDNode(Opcode, VT);
939 AllNodes.push_back(N);
940 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941 return SDOperand(N, 0);
942}
943
Chris Lattnerc3aae252005-01-07 07:46:32 +0000944SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
945 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000946 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000947 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
949 uint64_t Val = C->getValue();
950 switch (Opcode) {
951 default: break;
952 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000953 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
955 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000956 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
957 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000958 case ISD::BIT_CONVERT:
959 if (VT == MVT::f32) {
960 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
961 return getConstantFP(BitsToFloat(Val), VT);
962 } else if (VT == MVT::f64) {
963 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
964 return getConstantFP(BitsToDouble(Val), VT);
965 }
966 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000967 case ISD::BSWAP:
968 switch(VT) {
969 default: assert(0 && "Invalid bswap!"); break;
970 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
971 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
972 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
973 }
974 break;
975 case ISD::CTPOP:
976 switch(VT) {
977 default: assert(0 && "Invalid ctpop!"); break;
978 case MVT::i1: return getConstant(Val != 0, VT);
979 case MVT::i8:
980 Tmp1 = (unsigned)Val & 0xFF;
981 return getConstant(CountPopulation_32(Tmp1), VT);
982 case MVT::i16:
983 Tmp1 = (unsigned)Val & 0xFFFF;
984 return getConstant(CountPopulation_32(Tmp1), VT);
985 case MVT::i32:
986 return getConstant(CountPopulation_32((unsigned)Val), VT);
987 case MVT::i64:
988 return getConstant(CountPopulation_64(Val), VT);
989 }
990 case ISD::CTLZ:
991 switch(VT) {
992 default: assert(0 && "Invalid ctlz!"); break;
993 case MVT::i1: return getConstant(Val == 0, VT);
994 case MVT::i8:
995 Tmp1 = (unsigned)Val & 0xFF;
996 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
997 case MVT::i16:
998 Tmp1 = (unsigned)Val & 0xFFFF;
999 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1000 case MVT::i32:
1001 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1002 case MVT::i64:
1003 return getConstant(CountLeadingZeros_64(Val), VT);
1004 }
1005 case ISD::CTTZ:
1006 switch(VT) {
1007 default: assert(0 && "Invalid cttz!"); break;
1008 case MVT::i1: return getConstant(Val == 0, VT);
1009 case MVT::i8:
1010 Tmp1 = (unsigned)Val | 0x100;
1011 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1012 case MVT::i16:
1013 Tmp1 = (unsigned)Val | 0x10000;
1014 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1015 case MVT::i32:
1016 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1017 case MVT::i64:
1018 return getConstant(CountTrailingZeros_64(Val), VT);
1019 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 }
1021 }
1022
Chris Lattner94683772005-12-23 05:30:37 +00001023 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001024 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1025 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001026 case ISD::FNEG:
1027 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001028 case ISD::FABS:
1029 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 case ISD::FP_ROUND:
1031 case ISD::FP_EXTEND:
1032 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001033 case ISD::FP_TO_SINT:
1034 return getConstant((int64_t)C->getValue(), VT);
1035 case ISD::FP_TO_UINT:
1036 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001037 case ISD::BIT_CONVERT:
1038 if (VT == MVT::i32) {
1039 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1040 return getConstant(FloatToBits(C->getValue()), VT);
1041 } else if (VT == MVT::i64) {
1042 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1043 return getConstant(DoubleToBits(C->getValue()), VT);
1044 }
1045 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001046 }
1047
1048 unsigned OpOpcode = Operand.Val->getOpcode();
1049 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001050 case ISD::TokenFactor:
1051 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 case ISD::SIGN_EXTEND:
1053 if (Operand.getValueType() == VT) return Operand; // noop extension
1054 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1055 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1056 break;
1057 case ISD::ZERO_EXTEND:
1058 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001059 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001060 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001062 case ISD::ANY_EXTEND:
1063 if (Operand.getValueType() == VT) return Operand; // noop extension
1064 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1065 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1066 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1067 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001068 case ISD::TRUNCATE:
1069 if (Operand.getValueType() == VT) return Operand; // noop truncate
1070 if (OpOpcode == ISD::TRUNCATE)
1071 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001072 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1073 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001074 // If the source is smaller than the dest, we still need an extend.
1075 if (Operand.Val->getOperand(0).getValueType() < VT)
1076 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1077 else if (Operand.Val->getOperand(0).getValueType() > VT)
1078 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1079 else
1080 return Operand.Val->getOperand(0);
1081 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 break;
Chris Lattner35481892005-12-23 00:16:34 +00001083 case ISD::BIT_CONVERT:
1084 // Basic sanity checking.
1085 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1086 "Cannot BIT_CONVERT between two different types!");
1087 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001088 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1089 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001090 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001091 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001092 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1093 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001094 Operand.Val->getOperand(0));
1095 if (OpOpcode == ISD::FNEG) // --X -> X
1096 return Operand.Val->getOperand(0);
1097 break;
1098 case ISD::FABS:
1099 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1100 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1101 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 }
1103
Chris Lattner43247a12005-08-25 19:12:10 +00001104 SDNode *N;
1105 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1106 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001107 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001108 E = N = new SDNode(Opcode, Operand);
1109 } else {
1110 N = new SDNode(Opcode, Operand);
1111 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001112 N->setValueTypes(VT);
1113 AllNodes.push_back(N);
1114 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001115}
1116
Chris Lattner36019aa2005-04-18 03:48:41 +00001117
1118
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1120 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001121#ifndef NDEBUG
1122 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001123 case ISD::TokenFactor:
1124 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1125 N2.getValueType() == MVT::Other && "Invalid token factor!");
1126 break;
Chris Lattner76365122005-01-16 02:23:22 +00001127 case ISD::AND:
1128 case ISD::OR:
1129 case ISD::XOR:
1130 case ISD::UDIV:
1131 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001132 case ISD::MULHU:
1133 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001134 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1135 // fall through
1136 case ISD::ADD:
1137 case ISD::SUB:
1138 case ISD::MUL:
1139 case ISD::SDIV:
1140 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001141 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1142 // fall through.
1143 case ISD::FADD:
1144 case ISD::FSUB:
1145 case ISD::FMUL:
1146 case ISD::FDIV:
1147 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001148 assert(N1.getValueType() == N2.getValueType() &&
1149 N1.getValueType() == VT && "Binary operator types must match!");
1150 break;
1151
1152 case ISD::SHL:
1153 case ISD::SRA:
1154 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001155 case ISD::ROTL:
1156 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001157 assert(VT == N1.getValueType() &&
1158 "Shift operators return type must be the same as their first arg");
1159 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001160 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001161 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001162 case ISD::FP_ROUND_INREG: {
1163 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1164 assert(VT == N1.getValueType() && "Not an inreg round!");
1165 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1166 "Cannot FP_ROUND_INREG integer types");
1167 assert(EVT <= VT && "Not rounding down!");
1168 break;
1169 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001170 case ISD::AssertSext:
1171 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001172 case ISD::SIGN_EXTEND_INREG: {
1173 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1174 assert(VT == N1.getValueType() && "Not an inreg extend!");
1175 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1176 "Cannot *_EXTEND_INREG FP types");
1177 assert(EVT <= VT && "Not extending!");
1178 }
1179
Chris Lattner76365122005-01-16 02:23:22 +00001180 default: break;
1181 }
1182#endif
1183
Chris Lattnerc3aae252005-01-07 07:46:32 +00001184 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1185 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1186 if (N1C) {
1187 if (N2C) {
1188 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1189 switch (Opcode) {
1190 case ISD::ADD: return getConstant(C1 + C2, VT);
1191 case ISD::SUB: return getConstant(C1 - C2, VT);
1192 case ISD::MUL: return getConstant(C1 * C2, VT);
1193 case ISD::UDIV:
1194 if (C2) return getConstant(C1 / C2, VT);
1195 break;
1196 case ISD::UREM :
1197 if (C2) return getConstant(C1 % C2, VT);
1198 break;
1199 case ISD::SDIV :
1200 if (C2) return getConstant(N1C->getSignExtended() /
1201 N2C->getSignExtended(), VT);
1202 break;
1203 case ISD::SREM :
1204 if (C2) return getConstant(N1C->getSignExtended() %
1205 N2C->getSignExtended(), VT);
1206 break;
1207 case ISD::AND : return getConstant(C1 & C2, VT);
1208 case ISD::OR : return getConstant(C1 | C2, VT);
1209 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001210 case ISD::SHL : return getConstant(C1 << C2, VT);
1211 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001212 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001213 case ISD::ROTL :
1214 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1215 VT);
1216 case ISD::ROTR :
1217 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1218 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 default: break;
1220 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001221 } else { // Cannonicalize constant to RHS if commutative
1222 if (isCommutativeBinOp(Opcode)) {
1223 std::swap(N1C, N2C);
1224 std::swap(N1, N2);
1225 }
1226 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 }
1228
1229 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1230 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001231 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001232 if (N2CFP) {
1233 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1234 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001235 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1236 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1237 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1238 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 if (C2) return getConstantFP(C1 / C2, VT);
1240 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001241 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 if (C2) return getConstantFP(fmod(C1, C2), VT);
1243 break;
1244 default: break;
1245 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 } else { // Cannonicalize constant to RHS if commutative
1247 if (isCommutativeBinOp(Opcode)) {
1248 std::swap(N1CFP, N2CFP);
1249 std::swap(N1, N2);
1250 }
1251 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001252 }
1253
Chris Lattnerc3aae252005-01-07 07:46:32 +00001254 // Finally, fold operations that do not require constants.
1255 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001256 case ISD::FP_ROUND_INREG:
1257 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1258 break;
1259 case ISD::SIGN_EXTEND_INREG: {
1260 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1261 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 break;
1263 }
1264
Nate Begemaneea805e2005-04-13 21:23:31 +00001265 // FIXME: figure out how to safely handle things like
1266 // int foo(int x) { return 1 << (x & 255); }
1267 // int bar() { return foo(256); }
1268#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001269 case ISD::SHL:
1270 case ISD::SRL:
1271 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001272 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001273 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001274 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001275 else if (N2.getOpcode() == ISD::AND)
1276 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1277 // If the and is only masking out bits that cannot effect the shift,
1278 // eliminate the and.
1279 unsigned NumBits = MVT::getSizeInBits(VT);
1280 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1281 return getNode(Opcode, VT, N1, N2.getOperand(0));
1282 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001283 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001284#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001285 }
1286
Chris Lattner27e9b412005-05-11 18:57:39 +00001287 // Memoize this node if possible.
1288 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001289 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001290 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1291 if (BON) return SDOperand(BON, 0);
1292
1293 BON = N = new SDNode(Opcode, N1, N2);
1294 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001295 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001296 }
1297
Chris Lattner3e011362005-05-14 07:45:46 +00001298 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001299 AllNodes.push_back(N);
1300 return SDOperand(N, 0);
1301}
1302
Chris Lattnerc3aae252005-01-07 07:46:32 +00001303SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1304 SDOperand N1, SDOperand N2, SDOperand N3) {
1305 // Perform various simplifications.
1306 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1307 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1308 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1309 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001310 case ISD::SETCC: {
1311 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001312 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001313 if (Simp.Val) return Simp;
1314 break;
1315 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001316 case ISD::SELECT:
1317 if (N1C)
1318 if (N1C->getValue())
1319 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001320 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 return N3; // select false, X, Y -> Y
1322
1323 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001324 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001325 case ISD::BRCOND:
1326 if (N2C)
1327 if (N2C->getValue()) // Unconditional branch
1328 return getNode(ISD::BR, MVT::Other, N1, N3);
1329 else
1330 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001331 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 }
1333
Chris Lattner385328c2005-05-14 07:42:29 +00001334 std::vector<SDOperand> Ops;
1335 Ops.reserve(3);
1336 Ops.push_back(N1);
1337 Ops.push_back(N2);
1338 Ops.push_back(N3);
1339
Chris Lattner43247a12005-08-25 19:12:10 +00001340 // Memoize node if it doesn't produce a flag.
1341 SDNode *N;
1342 if (VT != MVT::Flag) {
1343 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1344 if (E) return SDOperand(E, 0);
1345 E = N = new SDNode(Opcode, N1, N2, N3);
1346 } else {
1347 N = new SDNode(Opcode, N1, N2, N3);
1348 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001349 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 AllNodes.push_back(N);
1351 return SDOperand(N, 0);
1352}
1353
1354SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001355 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001356 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001357 std::vector<SDOperand> Ops;
1358 Ops.reserve(4);
1359 Ops.push_back(N1);
1360 Ops.push_back(N2);
1361 Ops.push_back(N3);
1362 Ops.push_back(N4);
1363 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001364}
1365
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001366SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1367 SDOperand N1, SDOperand N2, SDOperand N3,
1368 SDOperand N4, SDOperand N5) {
1369 std::vector<SDOperand> Ops;
1370 Ops.reserve(5);
1371 Ops.push_back(N1);
1372 Ops.push_back(N2);
1373 Ops.push_back(N3);
1374 Ops.push_back(N4);
1375 Ops.push_back(N5);
1376 return getNode(Opcode, VT, Ops);
1377}
1378
Evan Cheng7038daf2005-12-10 00:37:58 +00001379SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1380 SDOperand Chain, SDOperand Ptr,
1381 SDOperand SV) {
1382 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1383 if (N) return SDOperand(N, 0);
1384 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1385
1386 // Loads have a token chain.
1387 setNodeValueTypes(N, VT, MVT::Other);
1388 AllNodes.push_back(N);
1389 return SDOperand(N, 0);
1390}
1391
1392SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1393 SDOperand Chain, SDOperand Ptr,
1394 SDOperand SV) {
1395 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1396 if (N) return SDOperand(N, 0);
1397 std::vector<SDOperand> Ops;
1398 Ops.reserve(5);
1399 Ops.push_back(Chain);
1400 Ops.push_back(Ptr);
1401 Ops.push_back(getConstant(Count, MVT::i32));
1402 Ops.push_back(getValueType(EVT));
1403 Ops.push_back(SV);
1404 std::vector<MVT::ValueType> VTs;
1405 VTs.reserve(2);
1406 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1407 return getNode(ISD::VLOAD, VTs, Ops);
1408}
1409
1410SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1411 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1412 MVT::ValueType EVT) {
1413 std::vector<SDOperand> Ops;
1414 Ops.reserve(4);
1415 Ops.push_back(Chain);
1416 Ops.push_back(Ptr);
1417 Ops.push_back(SV);
1418 Ops.push_back(getValueType(EVT));
1419 std::vector<MVT::ValueType> VTs;
1420 VTs.reserve(2);
1421 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1422 return getNode(Opcode, VTs, Ops);
1423}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001424
Chris Lattner0437cdd2005-05-09 04:14:13 +00001425SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001426 assert((!V || isa<PointerType>(V->getType())) &&
1427 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001428 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1429 if (N) return SDOperand(N, 0);
1430
1431 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001432 AllNodes.push_back(N);
1433 return SDOperand(N, 0);
1434}
1435
Nate Begemanacc398c2006-01-25 18:21:52 +00001436SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1437 SDOperand Chain, SDOperand Ptr,
1438 SDOperand SV) {
1439 std::vector<SDOperand> Ops;
1440 Ops.reserve(3);
1441 Ops.push_back(Chain);
1442 Ops.push_back(Ptr);
1443 Ops.push_back(SV);
1444 std::vector<MVT::ValueType> VTs;
1445 VTs.reserve(2);
1446 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1447 return getNode(ISD::VAARG, VTs, Ops);
1448}
1449
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001450SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001451 std::vector<SDOperand> &Ops) {
1452 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001454 case 1: return getNode(Opcode, VT, Ops[0]);
1455 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1456 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001457 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001458 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001459
Chris Lattner89c34632005-05-14 06:20:26 +00001460 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001461 switch (Opcode) {
1462 default: break;
1463 case ISD::BRCONDTWOWAY:
1464 if (N1C)
1465 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001466 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001467 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001468 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001469 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001470 case ISD::BRTWOWAY_CC:
1471 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1472 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1473 "LHS and RHS of comparison must have same type!");
1474 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001475 case ISD::TRUNCSTORE: {
1476 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1477 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1478#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1479 // If this is a truncating store of a constant, convert to the desired type
1480 // and store it instead.
1481 if (isa<Constant>(Ops[0])) {
1482 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1483 if (isa<Constant>(Op))
1484 N1 = Op;
1485 }
1486 // Also for ConstantFP?
1487#endif
1488 if (Ops[0].getValueType() == EVT) // Normal store?
1489 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1490 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1491 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1492 "Can't do FP-INT conversion!");
1493 break;
1494 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001495 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001496 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001497 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1498 "LHS and RHS of condition must have same type!");
1499 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1500 "True and False arms of SelectCC must have same type!");
1501 assert(Ops[2].getValueType() == VT &&
1502 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001503 break;
1504 }
1505 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001506 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001507 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1508 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001509 break;
1510 }
Chris Lattneref847df2005-04-09 03:27:28 +00001511 }
1512
Chris Lattner385328c2005-05-14 07:42:29 +00001513 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001514 SDNode *N;
1515 if (VT != MVT::Flag) {
1516 SDNode *&E =
1517 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1518 if (E) return SDOperand(E, 0);
1519 E = N = new SDNode(Opcode, Ops);
1520 } else {
1521 N = new SDNode(Opcode, Ops);
1522 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001523 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001524 AllNodes.push_back(N);
1525 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001526}
1527
Chris Lattner89c34632005-05-14 06:20:26 +00001528SDOperand SelectionDAG::getNode(unsigned Opcode,
1529 std::vector<MVT::ValueType> &ResultTys,
1530 std::vector<SDOperand> &Ops) {
1531 if (ResultTys.size() == 1)
1532 return getNode(Opcode, ResultTys[0], Ops);
1533
Chris Lattner5f056bf2005-07-10 01:55:33 +00001534 switch (Opcode) {
1535 case ISD::EXTLOAD:
1536 case ISD::SEXTLOAD:
1537 case ISD::ZEXTLOAD: {
1538 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1539 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1540 // If they are asking for an extending load from/to the same thing, return a
1541 // normal load.
1542 if (ResultTys[0] == EVT)
1543 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1544 assert(EVT < ResultTys[0] &&
1545 "Should only be an extending load, not truncating!");
1546 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1547 "Cannot sign/zero extend a FP load!");
1548 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1549 "Cannot convert from FP to Int or Int -> FP!");
1550 break;
1551 }
1552
Chris Lattnere89083a2005-05-14 07:25:05 +00001553 // FIXME: figure out how to safely handle things like
1554 // int foo(int x) { return 1 << (x & 255); }
1555 // int bar() { return foo(256); }
1556#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001557 case ISD::SRA_PARTS:
1558 case ISD::SRL_PARTS:
1559 case ISD::SHL_PARTS:
1560 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001561 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001562 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1563 else if (N3.getOpcode() == ISD::AND)
1564 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1565 // If the and is only masking out bits that cannot effect the shift,
1566 // eliminate the and.
1567 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1568 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1569 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1570 }
1571 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001572#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001573 }
Chris Lattner89c34632005-05-14 06:20:26 +00001574
Chris Lattner43247a12005-08-25 19:12:10 +00001575 // Memoize the node unless it returns a flag.
1576 SDNode *N;
1577 if (ResultTys.back() != MVT::Flag) {
1578 SDNode *&E =
1579 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1580 if (E) return SDOperand(E, 0);
1581 E = N = new SDNode(Opcode, Ops);
1582 } else {
1583 N = new SDNode(Opcode, Ops);
1584 }
Chris Lattnera3255112005-11-08 23:30:28 +00001585 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001586 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001587 return SDOperand(N, 0);
1588}
1589
Chris Lattnera3255112005-11-08 23:30:28 +00001590void SelectionDAG::setNodeValueTypes(SDNode *N,
1591 std::vector<MVT::ValueType> &RetVals) {
1592 switch (RetVals.size()) {
1593 case 0: return;
1594 case 1: N->setValueTypes(RetVals[0]); return;
1595 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1596 default: break;
1597 }
1598
1599 std::list<std::vector<MVT::ValueType> >::iterator I =
1600 std::find(VTList.begin(), VTList.end(), RetVals);
1601 if (I == VTList.end()) {
1602 VTList.push_front(RetVals);
1603 I = VTList.begin();
1604 }
1605
1606 N->setValueTypes(&(*I)[0], I->size());
1607}
1608
1609void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1610 MVT::ValueType VT2) {
1611 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1612 E = VTList.end(); I != E; ++I) {
1613 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1614 N->setValueTypes(&(*I)[0], 2);
1615 return;
1616 }
1617 }
1618 std::vector<MVT::ValueType> V;
1619 V.push_back(VT1);
1620 V.push_back(VT2);
1621 VTList.push_front(V);
1622 N->setValueTypes(&(*VTList.begin())[0], 2);
1623}
1624
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001625/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1626/// specified operands. If the resultant node already exists in the DAG,
1627/// this does not modify the specified node, instead it returns the node that
1628/// already exists. If the resultant node does not exist in the DAG, the
1629/// input node is returned. As a degenerate case, if you specify the same
1630/// input operands as the node already has, the input node is returned.
1631SDOperand SelectionDAG::
1632UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1633 SDNode *N = InN.Val;
1634 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1635
1636 // Check to see if there is no change.
1637 if (Op == N->getOperand(0)) return InN;
1638
1639 // See if the modified node already exists.
1640 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1641 if (NewSlot && *NewSlot)
1642 return SDOperand(*NewSlot, InN.ResNo);
1643
1644 // Nope it doesn't. Remove the node from it's current place in the maps.
1645 if (NewSlot)
1646 RemoveNodeFromCSEMaps(N);
1647
1648 // Now we update the operands.
1649 N->OperandList[0].Val->removeUser(N);
1650 Op.Val->addUser(N);
1651 N->OperandList[0] = Op;
1652
1653 // If this gets put into a CSE map, add it.
1654 if (NewSlot) *NewSlot = N;
1655 return InN;
1656}
1657
1658SDOperand SelectionDAG::
1659UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1660 SDNode *N = InN.Val;
1661 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1662
1663 // Check to see if there is no change.
1664 bool AnyChange = false;
1665 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1666 return InN; // No operands changed, just return the input node.
1667
1668 // See if the modified node already exists.
1669 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1670 if (NewSlot && *NewSlot)
1671 return SDOperand(*NewSlot, InN.ResNo);
1672
1673 // Nope it doesn't. Remove the node from it's current place in the maps.
1674 if (NewSlot)
1675 RemoveNodeFromCSEMaps(N);
1676
1677 // Now we update the operands.
1678 if (N->OperandList[0] != Op1) {
1679 N->OperandList[0].Val->removeUser(N);
1680 Op1.Val->addUser(N);
1681 N->OperandList[0] = Op1;
1682 }
1683 if (N->OperandList[1] != Op2) {
1684 N->OperandList[1].Val->removeUser(N);
1685 Op2.Val->addUser(N);
1686 N->OperandList[1] = Op2;
1687 }
1688
1689 // If this gets put into a CSE map, add it.
1690 if (NewSlot) *NewSlot = N;
1691 return InN;
1692}
1693
1694SDOperand SelectionDAG::
1695UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1696 std::vector<SDOperand> Ops;
1697 Ops.push_back(Op1);
1698 Ops.push_back(Op2);
1699 Ops.push_back(Op3);
1700 return UpdateNodeOperands(N, Ops);
1701}
1702
1703SDOperand SelectionDAG::
1704UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1705 SDOperand Op3, SDOperand Op4) {
1706 std::vector<SDOperand> Ops;
1707 Ops.push_back(Op1);
1708 Ops.push_back(Op2);
1709 Ops.push_back(Op3);
1710 Ops.push_back(Op4);
1711 return UpdateNodeOperands(N, Ops);
1712}
1713
1714SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001715UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1716 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1717 std::vector<SDOperand> Ops;
1718 Ops.push_back(Op1);
1719 Ops.push_back(Op2);
1720 Ops.push_back(Op3);
1721 Ops.push_back(Op4);
1722 Ops.push_back(Op5);
1723 return UpdateNodeOperands(N, Ops);
1724}
1725
1726
1727SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001728UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1729 SDNode *N = InN.Val;
1730 assert(N->getNumOperands() == Ops.size() &&
1731 "Update with wrong number of operands");
1732
1733 // Check to see if there is no change.
1734 unsigned NumOps = Ops.size();
1735 bool AnyChange = false;
1736 for (unsigned i = 0; i != NumOps; ++i) {
1737 if (Ops[i] != N->getOperand(i)) {
1738 AnyChange = true;
1739 break;
1740 }
1741 }
1742
1743 // No operands changed, just return the input node.
1744 if (!AnyChange) return InN;
1745
1746 // See if the modified node already exists.
1747 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1748 if (NewSlot && *NewSlot)
1749 return SDOperand(*NewSlot, InN.ResNo);
1750
1751 // Nope it doesn't. Remove the node from it's current place in the maps.
1752 if (NewSlot)
1753 RemoveNodeFromCSEMaps(N);
1754
1755 // Now we update the operands.
1756 for (unsigned i = 0; i != NumOps; ++i) {
1757 if (N->OperandList[i] != Ops[i]) {
1758 N->OperandList[i].Val->removeUser(N);
1759 Ops[i].Val->addUser(N);
1760 N->OperandList[i] = Ops[i];
1761 }
1762 }
1763
1764 // If this gets put into a CSE map, add it.
1765 if (NewSlot) *NewSlot = N;
1766 return InN;
1767}
1768
1769
1770
Chris Lattner149c58c2005-08-16 18:17:10 +00001771
1772/// SelectNodeTo - These are used for target selectors to *mutate* the
1773/// specified node to have the specified return type, Target opcode, and
1774/// operands. Note that target opcodes are stored as
1775/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001776///
1777/// Note that SelectNodeTo returns the resultant node. If there is already a
1778/// node of the specified opcode and operands, it returns that node instead of
1779/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001780SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1781 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001782 // If an identical node already exists, use it.
1783 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1784 if (ON) return SDOperand(ON, 0);
1785
Chris Lattner7651fa42005-08-24 23:00:29 +00001786 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001787
Chris Lattner7651fa42005-08-24 23:00:29 +00001788 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1789 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001790
1791 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001792 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001793}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001794
Chris Lattnereb19e402005-11-30 22:45:14 +00001795SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1796 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001797 // If an identical node already exists, use it.
1798 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1799 std::make_pair(Op1, VT))];
1800 if (ON) return SDOperand(ON, 0);
1801
Chris Lattner149c58c2005-08-16 18:17:10 +00001802 RemoveNodeFromCSEMaps(N);
1803 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1804 N->setValueTypes(VT);
1805 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001806
1807 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001808 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001809}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001810
Chris Lattnereb19e402005-11-30 22:45:14 +00001811SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1812 MVT::ValueType VT, SDOperand Op1,
1813 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001814 // If an identical node already exists, use it.
1815 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1816 std::make_pair(Op1, Op2))];
1817 if (ON) return SDOperand(ON, 0);
1818
Chris Lattner149c58c2005-08-16 18:17:10 +00001819 RemoveNodeFromCSEMaps(N);
1820 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1821 N->setValueTypes(VT);
1822 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001823
1824 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001825 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001826}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001827
Chris Lattnereb19e402005-11-30 22:45:14 +00001828SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1829 MVT::ValueType VT, SDOperand Op1,
1830 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001831 // If an identical node already exists, use it.
1832 std::vector<SDOperand> OpList;
1833 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1834 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1835 std::make_pair(VT, OpList))];
1836 if (ON) return SDOperand(ON, 0);
1837
Chris Lattner149c58c2005-08-16 18:17:10 +00001838 RemoveNodeFromCSEMaps(N);
1839 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1840 N->setValueTypes(VT);
1841 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001842
1843 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001844 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001845}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001846
Chris Lattnereb19e402005-11-30 22:45:14 +00001847SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1848 MVT::ValueType VT, SDOperand Op1,
1849 SDOperand Op2, SDOperand Op3,
1850 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001851 // If an identical node already exists, use it.
1852 std::vector<SDOperand> OpList;
1853 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1854 OpList.push_back(Op4);
1855 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1856 std::make_pair(VT, OpList))];
1857 if (ON) return SDOperand(ON, 0);
1858
Nate Begeman294a0a12005-08-18 07:30:15 +00001859 RemoveNodeFromCSEMaps(N);
1860 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1861 N->setValueTypes(VT);
1862 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001863
1864 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001865 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001866}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001867
Chris Lattnereb19e402005-11-30 22:45:14 +00001868SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1869 MVT::ValueType VT, SDOperand Op1,
1870 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1871 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001872 // If an identical node already exists, use it.
1873 std::vector<SDOperand> OpList;
1874 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1875 OpList.push_back(Op4); OpList.push_back(Op5);
1876 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1877 std::make_pair(VT, OpList))];
1878 if (ON) return SDOperand(ON, 0);
1879
Chris Lattner6b09a292005-08-21 18:49:33 +00001880 RemoveNodeFromCSEMaps(N);
1881 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1882 N->setValueTypes(VT);
1883 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001884
1885 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001886 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001887}
Chris Lattner149c58c2005-08-16 18:17:10 +00001888
Chris Lattnereb19e402005-11-30 22:45:14 +00001889SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1890 MVT::ValueType VT, SDOperand Op1,
1891 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1892 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001893 // If an identical node already exists, use it.
1894 std::vector<SDOperand> OpList;
1895 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1896 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1897 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1898 std::make_pair(VT, OpList))];
1899 if (ON) return SDOperand(ON, 0);
1900
Evan Cheng61ca74b2005-11-30 02:04:11 +00001901 RemoveNodeFromCSEMaps(N);
1902 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1903 N->setValueTypes(VT);
1904 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001905
1906 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001907 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001908}
1909
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001910SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1911 MVT::ValueType VT, SDOperand Op1,
1912 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1913 SDOperand Op5, SDOperand Op6,
1914 SDOperand Op7) {
1915 // If an identical node already exists, use it.
1916 std::vector<SDOperand> OpList;
1917 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1918 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1919 OpList.push_back(Op7);
1920 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1921 std::make_pair(VT, OpList))];
1922 if (ON) return SDOperand(ON, 0);
1923
1924 RemoveNodeFromCSEMaps(N);
1925 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1926 N->setValueTypes(VT);
1927 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1928
1929 ON = N; // Memoize the new node.
1930 return SDOperand(N, 0);
1931}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001932SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1933 MVT::ValueType VT, SDOperand Op1,
1934 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1935 SDOperand Op5, SDOperand Op6,
1936 SDOperand Op7, SDOperand Op8) {
1937 // If an identical node already exists, use it.
1938 std::vector<SDOperand> OpList;
1939 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1940 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1941 OpList.push_back(Op7); OpList.push_back(Op8);
1942 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1943 std::make_pair(VT, OpList))];
1944 if (ON) return SDOperand(ON, 0);
1945
1946 RemoveNodeFromCSEMaps(N);
1947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1948 N->setValueTypes(VT);
1949 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1950
1951 ON = N; // Memoize the new node.
1952 return SDOperand(N, 0);
1953}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001954
Chris Lattnereb19e402005-11-30 22:45:14 +00001955SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1956 MVT::ValueType VT1, MVT::ValueType VT2,
1957 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001958 // If an identical node already exists, use it.
1959 std::vector<SDOperand> OpList;
1960 OpList.push_back(Op1); OpList.push_back(Op2);
1961 std::vector<MVT::ValueType> VTList;
1962 VTList.push_back(VT1); VTList.push_back(VT2);
1963 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1964 std::make_pair(VTList, OpList))];
1965 if (ON) return SDOperand(ON, 0);
1966
Chris Lattner0fb094f2005-11-19 01:44:53 +00001967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1969 setNodeValueTypes(N, VT1, VT2);
1970 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001971
1972 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001973 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001974}
1975
Chris Lattnereb19e402005-11-30 22:45:14 +00001976SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1977 MVT::ValueType VT1, MVT::ValueType VT2,
1978 SDOperand Op1, SDOperand Op2,
1979 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001980 // If an identical node already exists, use it.
1981 std::vector<SDOperand> OpList;
1982 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1983 std::vector<MVT::ValueType> VTList;
1984 VTList.push_back(VT1); VTList.push_back(VT2);
1985 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1986 std::make_pair(VTList, OpList))];
1987 if (ON) return SDOperand(ON, 0);
1988
Chris Lattner0fb094f2005-11-19 01:44:53 +00001989 RemoveNodeFromCSEMaps(N);
1990 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1991 setNodeValueTypes(N, VT1, VT2);
1992 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001993
1994 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001995 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001996}
1997
Chris Lattnereb19e402005-11-30 22:45:14 +00001998SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1999 MVT::ValueType VT1, MVT::ValueType VT2,
2000 SDOperand Op1, SDOperand Op2,
2001 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002002 // If an identical node already exists, use it.
2003 std::vector<SDOperand> OpList;
2004 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2005 OpList.push_back(Op4);
2006 std::vector<MVT::ValueType> VTList;
2007 VTList.push_back(VT1); VTList.push_back(VT2);
2008 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2009 std::make_pair(VTList, OpList))];
2010 if (ON) return SDOperand(ON, 0);
2011
Chris Lattner0fb094f2005-11-19 01:44:53 +00002012 RemoveNodeFromCSEMaps(N);
2013 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2014 setNodeValueTypes(N, VT1, VT2);
2015 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002016
2017 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002018 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002019}
2020
Chris Lattnereb19e402005-11-30 22:45:14 +00002021SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2022 MVT::ValueType VT1, MVT::ValueType VT2,
2023 SDOperand Op1, SDOperand Op2,
2024 SDOperand Op3, SDOperand Op4,
2025 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026 // If an identical node already exists, use it.
2027 std::vector<SDOperand> OpList;
2028 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2029 OpList.push_back(Op4); OpList.push_back(Op5);
2030 std::vector<MVT::ValueType> VTList;
2031 VTList.push_back(VT1); VTList.push_back(VT2);
2032 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2033 std::make_pair(VTList, OpList))];
2034 if (ON) return SDOperand(ON, 0);
2035
Chris Lattner0fb094f2005-11-19 01:44:53 +00002036 RemoveNodeFromCSEMaps(N);
2037 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2038 setNodeValueTypes(N, VT1, VT2);
2039 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002040
2041 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002042 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002043}
2044
2045// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002046/// This can cause recursive merging of nodes in the DAG.
2047///
Chris Lattner8b52f212005-08-26 18:36:28 +00002048/// This version assumes From/To have a single result value.
2049///
Chris Lattner1e111c72005-09-07 05:37:01 +00002050void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2051 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002052 SDNode *From = FromN.Val, *To = ToN.Val;
2053 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2054 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002055 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002056
Chris Lattner8b8749f2005-08-17 19:00:20 +00002057 while (!From->use_empty()) {
2058 // Process users until they are all gone.
2059 SDNode *U = *From->use_begin();
2060
2061 // This node is about to morph, remove its old self from the CSE maps.
2062 RemoveNodeFromCSEMaps(U);
2063
Chris Lattner65113b22005-11-08 22:07:03 +00002064 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2065 I != E; ++I)
2066 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002067 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002068 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002069 To->addUser(U);
2070 }
2071
2072 // Now that we have modified U, add it back to the CSE maps. If it already
2073 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002074 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2075 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002076 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002077 if (Deleted) Deleted->push_back(U);
2078 DeleteNodeNotInCSEMaps(U);
2079 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002080 }
2081}
2082
2083/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2084/// This can cause recursive merging of nodes in the DAG.
2085///
2086/// This version assumes From/To have matching types and numbers of result
2087/// values.
2088///
Chris Lattner1e111c72005-09-07 05:37:01 +00002089void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2090 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002091 assert(From != To && "Cannot replace uses of with self");
2092 assert(From->getNumValues() == To->getNumValues() &&
2093 "Cannot use this version of ReplaceAllUsesWith!");
2094 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002095 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002096 return;
2097 }
2098
2099 while (!From->use_empty()) {
2100 // Process users until they are all gone.
2101 SDNode *U = *From->use_begin();
2102
2103 // This node is about to morph, remove its old self from the CSE maps.
2104 RemoveNodeFromCSEMaps(U);
2105
Chris Lattner65113b22005-11-08 22:07:03 +00002106 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2107 I != E; ++I)
2108 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002109 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002110 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002111 To->addUser(U);
2112 }
2113
2114 // Now that we have modified U, add it back to the CSE maps. If it already
2115 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002116 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2117 ReplaceAllUsesWith(U, Existing, Deleted);
2118 // U is now dead.
2119 if (Deleted) Deleted->push_back(U);
2120 DeleteNodeNotInCSEMaps(U);
2121 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002122 }
2123}
2124
Chris Lattner8b52f212005-08-26 18:36:28 +00002125/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2126/// This can cause recursive merging of nodes in the DAG.
2127///
2128/// This version can replace From with any result values. To must match the
2129/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002130void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002131 const std::vector<SDOperand> &To,
2132 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002133 assert(From->getNumValues() == To.size() &&
2134 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002135 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002136 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002137 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002138 return;
2139 }
2140
2141 while (!From->use_empty()) {
2142 // Process users until they are all gone.
2143 SDNode *U = *From->use_begin();
2144
2145 // This node is about to morph, remove its old self from the CSE maps.
2146 RemoveNodeFromCSEMaps(U);
2147
Chris Lattner65113b22005-11-08 22:07:03 +00002148 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2149 I != E; ++I)
2150 if (I->Val == From) {
2151 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002152 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002153 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002154 ToOp.Val->addUser(U);
2155 }
2156
2157 // Now that we have modified U, add it back to the CSE maps. If it already
2158 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002159 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2160 ReplaceAllUsesWith(U, Existing, Deleted);
2161 // U is now dead.
2162 if (Deleted) Deleted->push_back(U);
2163 DeleteNodeNotInCSEMaps(U);
2164 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002165 }
2166}
2167
2168
Jim Laskey58b968b2005-08-17 20:08:02 +00002169//===----------------------------------------------------------------------===//
2170// SDNode Class
2171//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002172
Chris Lattnera3255112005-11-08 23:30:28 +00002173
2174/// getValueTypeList - Return a pointer to the specified value type.
2175///
2176MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2177 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2178 VTs[VT] = VT;
2179 return &VTs[VT];
2180}
2181
Chris Lattner5c884562005-01-12 18:37:47 +00002182/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2183/// indicated value. This method ignores uses of other values defined by this
2184/// operation.
2185bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2186 assert(Value < getNumValues() && "Bad value!");
2187
2188 // If there is only one value, this is easy.
2189 if (getNumValues() == 1)
2190 return use_size() == NUses;
2191 if (Uses.size() < NUses) return false;
2192
2193 SDOperand TheValue(this, Value);
2194
2195 std::set<SDNode*> UsersHandled;
2196
2197 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2198 UI != E; ++UI) {
2199 SDNode *User = *UI;
2200 if (User->getNumOperands() == 1 ||
2201 UsersHandled.insert(User).second) // First time we've seen this?
2202 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2203 if (User->getOperand(i) == TheValue) {
2204 if (NUses == 0)
2205 return false; // too many uses
2206 --NUses;
2207 }
2208 }
2209
2210 // Found exactly the right number of uses?
2211 return NUses == 0;
2212}
2213
2214
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002215const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002216 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002217 default:
2218 if (getOpcode() < ISD::BUILTIN_OP_END)
2219 return "<<Unknown DAG Node>>";
2220 else {
Evan Cheng72261582005-12-20 06:22:03 +00002221 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002222 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002223 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2224 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002225
Evan Cheng72261582005-12-20 06:22:03 +00002226 TargetLowering &TLI = G->getTargetLoweringInfo();
2227 const char *Name =
2228 TLI.getTargetNodeName(getOpcode());
2229 if (Name) return Name;
2230 }
2231
2232 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002233 }
2234
Andrew Lenharth95762122005-03-31 21:24:06 +00002235 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002236 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002237 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002238 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002239 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002240 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002241 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002242 case ISD::AssertSext: return "AssertSext";
2243 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002244 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002245 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002246 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002247 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002248 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002249 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002250 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002251 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002252 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002253 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002254 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002255 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002256 case ISD::ConstantPool: return "ConstantPool";
2257 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002258 case ISD::CopyToReg: return "CopyToReg";
2259 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002260 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002261 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002262 case ISD::INLINEASM: return "inlineasm";
2263
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002264 // Unary operators
2265 case ISD::FABS: return "fabs";
2266 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002267 case ISD::FSQRT: return "fsqrt";
2268 case ISD::FSIN: return "fsin";
2269 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002270
2271 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002272 case ISD::ADD: return "add";
2273 case ISD::SUB: return "sub";
2274 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002275 case ISD::MULHU: return "mulhu";
2276 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002277 case ISD::SDIV: return "sdiv";
2278 case ISD::UDIV: return "udiv";
2279 case ISD::SREM: return "srem";
2280 case ISD::UREM: return "urem";
2281 case ISD::AND: return "and";
2282 case ISD::OR: return "or";
2283 case ISD::XOR: return "xor";
2284 case ISD::SHL: return "shl";
2285 case ISD::SRA: return "sra";
2286 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002287 case ISD::ROTL: return "rotl";
2288 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002289 case ISD::FADD: return "fadd";
2290 case ISD::FSUB: return "fsub";
2291 case ISD::FMUL: return "fmul";
2292 case ISD::FDIV: return "fdiv";
2293 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002294 case ISD::VADD: return "vadd";
2295 case ISD::VSUB: return "vsub";
2296 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002297
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002298 case ISD::SETCC: return "setcc";
2299 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002300 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002301 case ISD::ADD_PARTS: return "add_parts";
2302 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002303 case ISD::SHL_PARTS: return "shl_parts";
2304 case ISD::SRA_PARTS: return "sra_parts";
2305 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002306
Chris Lattner7f644642005-04-28 21:44:03 +00002307 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002308 case ISD::SIGN_EXTEND: return "sign_extend";
2309 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002310 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002311 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002312 case ISD::TRUNCATE: return "truncate";
2313 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002314 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002315 case ISD::FP_EXTEND: return "fp_extend";
2316
2317 case ISD::SINT_TO_FP: return "sint_to_fp";
2318 case ISD::UINT_TO_FP: return "uint_to_fp";
2319 case ISD::FP_TO_SINT: return "fp_to_sint";
2320 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002321 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002322
2323 // Control flow instructions
2324 case ISD::BR: return "br";
2325 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002326 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002327 case ISD::BR_CC: return "br_cc";
2328 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002329 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002330 case ISD::CALLSEQ_START: return "callseq_start";
2331 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002332
2333 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002334 case ISD::LOAD: return "load";
2335 case ISD::STORE: return "store";
2336 case ISD::VLOAD: return "vload";
2337 case ISD::EXTLOAD: return "extload";
2338 case ISD::SEXTLOAD: return "sextload";
2339 case ISD::ZEXTLOAD: return "zextload";
2340 case ISD::TRUNCSTORE: return "truncstore";
2341 case ISD::VAARG: return "vaarg";
2342 case ISD::VACOPY: return "vacopy";
2343 case ISD::VAEND: return "vaend";
2344 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002345 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002346 case ISD::EXTRACT_ELEMENT: return "extract_element";
2347 case ISD::BUILD_PAIR: return "build_pair";
2348 case ISD::STACKSAVE: return "stacksave";
2349 case ISD::STACKRESTORE: return "stackrestore";
2350
2351 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002352 case ISD::MEMSET: return "memset";
2353 case ISD::MEMCPY: return "memcpy";
2354 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002355
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002356 // Bit manipulation
2357 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002358 case ISD::CTPOP: return "ctpop";
2359 case ISD::CTTZ: return "cttz";
2360 case ISD::CTLZ: return "ctlz";
2361
2362 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002363 case ISD::READPORT: return "readport";
2364 case ISD::WRITEPORT: return "writeport";
2365 case ISD::READIO: return "readio";
2366 case ISD::WRITEIO: return "writeio";
2367
Chris Lattner36ce6912005-11-29 06:21:05 +00002368 // Debug info
2369 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002370 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002371 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002372
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002373 case ISD::CONDCODE:
2374 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002375 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002376 case ISD::SETOEQ: return "setoeq";
2377 case ISD::SETOGT: return "setogt";
2378 case ISD::SETOGE: return "setoge";
2379 case ISD::SETOLT: return "setolt";
2380 case ISD::SETOLE: return "setole";
2381 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002382
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002383 case ISD::SETO: return "seto";
2384 case ISD::SETUO: return "setuo";
2385 case ISD::SETUEQ: return "setue";
2386 case ISD::SETUGT: return "setugt";
2387 case ISD::SETUGE: return "setuge";
2388 case ISD::SETULT: return "setult";
2389 case ISD::SETULE: return "setule";
2390 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002391
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002392 case ISD::SETEQ: return "seteq";
2393 case ISD::SETGT: return "setgt";
2394 case ISD::SETGE: return "setge";
2395 case ISD::SETLT: return "setlt";
2396 case ISD::SETLE: return "setle";
2397 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002398 }
2399 }
2400}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002401
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002402void SDNode::dump() const { dump(0); }
2403void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404 std::cerr << (void*)this << ": ";
2405
2406 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2407 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002408 if (getValueType(i) == MVT::Other)
2409 std::cerr << "ch";
2410 else
2411 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002412 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002413 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002414
2415 std::cerr << " ";
2416 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2417 if (i) std::cerr << ", ";
2418 std::cerr << (void*)getOperand(i).Val;
2419 if (unsigned RN = getOperand(i).ResNo)
2420 std::cerr << ":" << RN;
2421 }
2422
2423 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2424 std::cerr << "<" << CSDN->getValue() << ">";
2425 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2426 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002427 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002428 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002429 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002430 std::cerr << "<";
2431 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002432 if (offset > 0)
2433 std::cerr << " + " << offset;
2434 else
2435 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002436 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002437 std::cerr << "<" << FIDN->getIndex() << ">";
2438 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002439 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002440 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002441 std::cerr << "<";
2442 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2443 if (LBB)
2444 std::cerr << LBB->getName() << " ";
2445 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002446 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002447 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002448 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2449 } else {
2450 std::cerr << " #" << R->getReg();
2451 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002452 } else if (const ExternalSymbolSDNode *ES =
2453 dyn_cast<ExternalSymbolSDNode>(this)) {
2454 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002455 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2456 if (M->getValue())
2457 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2458 else
2459 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002460 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2461 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002462 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002463}
2464
Chris Lattnerde202b32005-11-09 23:47:37 +00002465static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002466 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2467 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002468 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002469 else
2470 std::cerr << "\n" << std::string(indent+2, ' ')
2471 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002472
Chris Lattnerea946cd2005-01-09 20:38:33 +00002473
2474 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002475 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002476}
2477
Chris Lattnerc3aae252005-01-07 07:46:32 +00002478void SelectionDAG::dump() const {
2479 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002480 std::vector<const SDNode*> Nodes;
2481 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2482 I != E; ++I)
2483 Nodes.push_back(I);
2484
Chris Lattner49d24712005-01-09 20:26:36 +00002485 std::sort(Nodes.begin(), Nodes.end());
2486
2487 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002488 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002489 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002490 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002491
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002492 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002493
Chris Lattnerc3aae252005-01-07 07:46:32 +00002494 std::cerr << "\n\n";
2495}
2496