blob: 48d1d5c806d9ff46e66fd1c4aa2788cc6c8e03f2 [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 Lattner36ce6912005-11-29 06:21:05 +0000280 case ISD::STRING:
281 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
282 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000283 case ISD::CONDCODE:
284 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
285 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000286 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000287 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
288 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000289 case ISD::GlobalAddress: {
290 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
292 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 }
295 case ISD::TargetGlobalAddress: {
296 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
297 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
298 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000299 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000300 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000302 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000304 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000305 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000306 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000308 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000310 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000311 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000312 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000314 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 break;
316 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000317 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000319 case ISD::TargetExternalSymbol:
320 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
321 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000322 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000324 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
325 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000326 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000327 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
328 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000329 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000330 case ISD::SRCVALUE: {
331 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000332 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000333 break;
334 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000335 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000336 Erased = Loads.erase(std::make_pair(N->getOperand(1),
337 std::make_pair(N->getOperand(0),
338 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000340 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000342 if (N->getNumOperands() == 0) {
343 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
344 N->getValueType(0)));
345 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased =
347 UnaryOps.erase(std::make_pair(N->getOpcode(),
348 std::make_pair(N->getOperand(0),
349 N->getValueType(0))));
350 } else if (N->getNumOperands() == 2) {
351 Erased =
352 BinaryOps.erase(std::make_pair(N->getOpcode(),
353 std::make_pair(N->getOperand(0),
354 N->getOperand(1))));
355 } else {
356 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
357 Erased =
358 OneResultNodes.erase(std::make_pair(N->getOpcode(),
359 std::make_pair(N->getValueType(0),
360 Ops)));
361 }
Chris Lattner385328c2005-05-14 07:42:29 +0000362 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000363 // Remove the node from the ArbitraryNodes map.
364 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
365 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000366 Erased =
367 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
368 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000369 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 break;
371 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372#ifndef NDEBUG
373 // Verify that the node was actually in one of the CSE maps, unless it has a
374 // flag result (which cannot be CSE'd) or is one of the special cases that are
375 // not subject to CSE.
376 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
377 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000378 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000379
380 N->dump();
381 assert(0 && "Node is not in map!");
382 }
383#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384}
385
Chris Lattner8b8749f2005-08-17 19:00:20 +0000386/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
387/// has been taken out and modified in some way. If the specified node already
388/// exists in the CSE maps, do not modify the maps, but return the existing node
389/// instead. If it doesn't exist, add it and return null.
390///
391SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000393 if (N->getOpcode() == ISD::CALLSEQ_START ||
Chris Lattnerfe14b342005-12-01 23:14:50 +0000394 N->getOpcode() == ISD::CALLSEQ_END ||
Chris Lattner9f8cc692005-12-19 22:21:21 +0000395 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000396 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000397
Chris Lattner9f8cc692005-12-19 22:21:21 +0000398 // Check that remaining values produced are not flags.
399 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
400 if (N->getValueType(i) == MVT::Flag)
401 return 0; // Never CSE anything that produces a flag.
402
Chris Lattnerfe14b342005-12-01 23:14:50 +0000403 if (N->getNumValues() == 1) {
404 if (N->getNumOperands() == 1) {
405 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
406 std::make_pair(N->getOperand(0),
407 N->getValueType(0)))];
408 if (U) return U;
409 U = N;
410 } else if (N->getNumOperands() == 2) {
411 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getOperand(0),
413 N->getOperand(1)))];
414 if (B) return B;
415 B = N;
416 } else {
417 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
418 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
419 std::make_pair(N->getValueType(0), Ops))];
420 if (ORN) return ORN;
421 ORN = N;
422 }
423 } else {
424 if (N->getOpcode() == ISD::LOAD) {
425 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
426 std::make_pair(N->getOperand(0),
427 N->getValueType(0)))];
428 if (L) return L;
429 L = N;
430 } else {
431 // Remove the node from the ArbitraryNodes map.
432 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
433 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
434 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
435 std::make_pair(RV, Ops))];
436 if (AN) return AN;
437 AN = N;
438 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000439 }
440 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000441}
442
443
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000444
Chris Lattner78ec3112003-08-11 14:57:33 +0000445SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000446 while (!AllNodes.empty()) {
447 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000448 delete [] N->OperandList;
449 N->OperandList = 0;
450 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000451 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000452 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000453}
454
Chris Lattner0f2287b2005-04-13 02:38:18 +0000455SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000456 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000457 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000458 return getNode(ISD::AND, Op.getValueType(), Op,
459 getConstant(Imm, Op.getValueType()));
460}
461
Chris Lattnerc3aae252005-01-07 07:46:32 +0000462SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
463 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
464 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000465 if (VT != MVT::i64)
466 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000467
Chris Lattnerc3aae252005-01-07 07:46:32 +0000468 SDNode *&N = Constants[std::make_pair(Val, VT)];
469 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000470 N = new ConstantSDNode(false, Val, VT);
471 AllNodes.push_back(N);
472 return SDOperand(N, 0);
473}
474
Chris Lattner36ce6912005-11-29 06:21:05 +0000475SDOperand SelectionDAG::getString(const std::string &Val) {
476 StringSDNode *&N = StringNodes[Val];
477 if (!N) {
478 N = new StringSDNode(Val);
479 AllNodes.push_back(N);
480 }
481 return SDOperand(N, 0);
482}
483
Chris Lattner37bfbb42005-08-17 00:34:06 +0000484SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
485 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
486 // Mask out any bits that are not valid for this constant.
487 if (VT != MVT::i64)
488 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
489
490 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
491 if (N) return SDOperand(N, 0);
492 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000493 AllNodes.push_back(N);
494 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000495}
496
Chris Lattnerc3aae252005-01-07 07:46:32 +0000497SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
498 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
499 if (VT == MVT::f32)
500 Val = (float)Val; // Mask out extra precision.
501
Chris Lattnerd8658612005-02-17 20:17:32 +0000502 // Do the map lookup using the actual bit pattern for the floating point
503 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
504 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000505 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000506 if (N) return SDOperand(N, 0);
507 N = new ConstantFPSDNode(Val, VT);
508 AllNodes.push_back(N);
509 return SDOperand(N, 0);
510}
511
Chris Lattnerc3aae252005-01-07 07:46:32 +0000512SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000513 MVT::ValueType VT, int offset) {
514 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000515 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000516 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000517 AllNodes.push_back(N);
518 return SDOperand(N, 0);
519}
520
521SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000522 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000523 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000524 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000525 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000526 AllNodes.push_back(N);
527 return SDOperand(N, 0);
528}
529
530SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
531 SDNode *&N = FrameIndices[FI];
532 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000533 N = new FrameIndexSDNode(FI, VT, false);
534 AllNodes.push_back(N);
535 return SDOperand(N, 0);
536}
537
538SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
539 SDNode *&N = TargetFrameIndices[FI];
540 if (N) return SDOperand(N, 0);
541 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000542 AllNodes.push_back(N);
543 return SDOperand(N, 0);
544}
545
Chris Lattner5839bf22005-08-26 17:15:30 +0000546SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
547 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000548 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000549 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000550 AllNodes.push_back(N);
551 return SDOperand(N, 0);
552}
553
Chris Lattner5839bf22005-08-26 17:15:30 +0000554SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
555 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000556 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000557 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000558 AllNodes.push_back(N);
559 return SDOperand(N, 0);
560}
561
562SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
563 SDNode *&N = BBNodes[MBB];
564 if (N) return SDOperand(N, 0);
565 N = new BasicBlockSDNode(MBB);
566 AllNodes.push_back(N);
567 return SDOperand(N, 0);
568}
569
Chris Lattner15e4b012005-07-10 00:07:11 +0000570SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
571 if ((unsigned)VT >= ValueTypeNodes.size())
572 ValueTypeNodes.resize(VT+1);
573 if (ValueTypeNodes[VT] == 0) {
574 ValueTypeNodes[VT] = new VTSDNode(VT);
575 AllNodes.push_back(ValueTypeNodes[VT]);
576 }
577
578 return SDOperand(ValueTypeNodes[VT], 0);
579}
580
Chris Lattnerc3aae252005-01-07 07:46:32 +0000581SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
582 SDNode *&N = ExternalSymbols[Sym];
583 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000584 N = new ExternalSymbolSDNode(false, Sym, VT);
585 AllNodes.push_back(N);
586 return SDOperand(N, 0);
587}
588
589SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
590 SDNode *&N = TargetExternalSymbols[Sym];
591 if (N) return SDOperand(N, 0);
592 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000593 AllNodes.push_back(N);
594 return SDOperand(N, 0);
595}
596
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000597SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
598 if ((unsigned)Cond >= CondCodeNodes.size())
599 CondCodeNodes.resize(Cond+1);
600
Chris Lattner079a27a2005-08-09 20:40:02 +0000601 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000602 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000603 AllNodes.push_back(CondCodeNodes[Cond]);
604 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000605 return SDOperand(CondCodeNodes[Cond], 0);
606}
607
Chris Lattner0fdd7682005-08-30 22:38:38 +0000608SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
609 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
610 if (!Reg) {
611 Reg = new RegisterSDNode(RegNo, VT);
612 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000613 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000614 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000615}
616
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000617SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
618 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000619 // These setcc operations always fold.
620 switch (Cond) {
621 default: break;
622 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000623 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000625 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000626 }
627
Chris Lattner67255a12005-04-07 18:14:58 +0000628 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
629 uint64_t C2 = N2C->getValue();
630 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
631 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000632
Chris Lattnerc3aae252005-01-07 07:46:32 +0000633 // Sign extend the operands if required
634 if (ISD::isSignedIntSetCC(Cond)) {
635 C1 = N1C->getSignExtended();
636 C2 = N2C->getSignExtended();
637 }
638
639 switch (Cond) {
640 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000641 case ISD::SETEQ: return getConstant(C1 == C2, VT);
642 case ISD::SETNE: return getConstant(C1 != C2, VT);
643 case ISD::SETULT: return getConstant(C1 < C2, VT);
644 case ISD::SETUGT: return getConstant(C1 > C2, VT);
645 case ISD::SETULE: return getConstant(C1 <= C2, VT);
646 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
647 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
648 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
649 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
650 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651 }
Chris Lattner24673922005-04-07 18:58:54 +0000652 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000653 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000654 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
655 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
656
657 // If the comparison constant has bits in the upper part, the
658 // zero-extended value could never match.
659 if (C2 & (~0ULL << InSize)) {
660 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
661 switch (Cond) {
662 case ISD::SETUGT:
663 case ISD::SETUGE:
664 case ISD::SETEQ: return getConstant(0, VT);
665 case ISD::SETULT:
666 case ISD::SETULE:
667 case ISD::SETNE: return getConstant(1, VT);
668 case ISD::SETGT:
669 case ISD::SETGE:
670 // True if the sign bit of C2 is set.
671 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
672 case ISD::SETLT:
673 case ISD::SETLE:
674 // True if the sign bit of C2 isn't set.
675 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
676 default:
677 break;
678 }
679 }
680
681 // Otherwise, we can perform the comparison with the low bits.
682 switch (Cond) {
683 case ISD::SETEQ:
684 case ISD::SETNE:
685 case ISD::SETUGT:
686 case ISD::SETUGE:
687 case ISD::SETULT:
688 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000689 return getSetCC(VT, N1.getOperand(0),
690 getConstant(C2, N1.getOperand(0).getValueType()),
691 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000692 default:
693 break; // todo, be more careful with signed comparisons
694 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000695 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
696 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
697 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
698 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
699 MVT::ValueType ExtDstTy = N1.getValueType();
700 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000701
Chris Lattner7b2880c2005-08-24 22:44:39 +0000702 // If the extended part has any inconsistent bits, it cannot ever
703 // compare equal. In other words, they have to be all ones or all
704 // zeros.
705 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000706 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000707 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
708 return getConstant(Cond == ISD::SETNE, VT);
709
710 // Otherwise, make this a use of a zext.
711 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000712 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000713 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000714 }
715
Chris Lattner67255a12005-04-07 18:14:58 +0000716 uint64_t MinVal, MaxVal;
717 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
718 if (ISD::isSignedIntSetCC(Cond)) {
719 MinVal = 1ULL << (OperandBitSize-1);
720 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
721 MaxVal = ~0ULL >> (65-OperandBitSize);
722 else
723 MaxVal = 0;
724 } else {
725 MinVal = 0;
726 MaxVal = ~0ULL >> (64-OperandBitSize);
727 }
728
729 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
730 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
731 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
732 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000733 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
734 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000735 }
736
737 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
738 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
739 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000740 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
741 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000742 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000743
Nate Begeman72ea2812005-04-14 08:56:52 +0000744 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
745 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000746
Nate Begeman72ea2812005-04-14 08:56:52 +0000747 // Canonicalize setgt X, Min --> setne X, Min
748 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000749 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000750
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000751 // If we have setult X, 1, turn it into seteq X, 0
752 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000753 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
754 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000755 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000756 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000757 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
758 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000759
760 // If we have "setcc X, C1", check to see if we can shrink the immediate
761 // by changing cc.
762
763 // SETUGT X, SINTMAX -> SETLT X, 0
764 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
765 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000766 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000767
768 // FIXME: Implement the rest of these.
769
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000770
771 // Fold bit comparisons when we can.
772 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
773 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
774 if (ConstantSDNode *AndRHS =
775 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
776 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
777 // Perform the xform if the AND RHS is a single bit.
778 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
779 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000780 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000781 TLI.getShiftAmountTy()));
782 }
783 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
784 // (X & 8) == 8 --> (X & 8) >> 3
785 // Perform the xform if C2 is a single bit.
786 if ((C2 & (C2-1)) == 0) {
787 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000788 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000789 }
790 }
791 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000792 }
Chris Lattner67255a12005-04-07 18:14:58 +0000793 } else if (isa<ConstantSDNode>(N1.Val)) {
794 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000795 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000796 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000797
798 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
799 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
800 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000801
Chris Lattnerc3aae252005-01-07 07:46:32 +0000802 switch (Cond) {
803 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000804 case ISD::SETEQ: return getConstant(C1 == C2, VT);
805 case ISD::SETNE: return getConstant(C1 != C2, VT);
806 case ISD::SETLT: return getConstant(C1 < C2, VT);
807 case ISD::SETGT: return getConstant(C1 > C2, VT);
808 case ISD::SETLE: return getConstant(C1 <= C2, VT);
809 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810 }
811 } else {
812 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000813 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814 }
815
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000816 // Could not fold it.
817 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818}
819
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000821///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000823 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
824 if (!N) {
825 N = new SDNode(Opcode, VT);
826 AllNodes.push_back(N);
827 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000828 return SDOperand(N, 0);
829}
830
Chris Lattnerc3aae252005-01-07 07:46:32 +0000831SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
832 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000833 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000834 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
836 uint64_t Val = C->getValue();
837 switch (Opcode) {
838 default: break;
839 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000840 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
842 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000843 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
844 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000845 case ISD::BIT_CONVERT:
846 if (VT == MVT::f32) {
847 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
848 return getConstantFP(BitsToFloat(Val), VT);
849 } else if (VT == MVT::f64) {
850 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
851 return getConstantFP(BitsToDouble(Val), VT);
852 }
853 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000854 case ISD::BSWAP:
855 switch(VT) {
856 default: assert(0 && "Invalid bswap!"); break;
857 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
858 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
859 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
860 }
861 break;
862 case ISD::CTPOP:
863 switch(VT) {
864 default: assert(0 && "Invalid ctpop!"); break;
865 case MVT::i1: return getConstant(Val != 0, VT);
866 case MVT::i8:
867 Tmp1 = (unsigned)Val & 0xFF;
868 return getConstant(CountPopulation_32(Tmp1), VT);
869 case MVT::i16:
870 Tmp1 = (unsigned)Val & 0xFFFF;
871 return getConstant(CountPopulation_32(Tmp1), VT);
872 case MVT::i32:
873 return getConstant(CountPopulation_32((unsigned)Val), VT);
874 case MVT::i64:
875 return getConstant(CountPopulation_64(Val), VT);
876 }
877 case ISD::CTLZ:
878 switch(VT) {
879 default: assert(0 && "Invalid ctlz!"); break;
880 case MVT::i1: return getConstant(Val == 0, VT);
881 case MVT::i8:
882 Tmp1 = (unsigned)Val & 0xFF;
883 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
884 case MVT::i16:
885 Tmp1 = (unsigned)Val & 0xFFFF;
886 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
887 case MVT::i32:
888 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
889 case MVT::i64:
890 return getConstant(CountLeadingZeros_64(Val), VT);
891 }
892 case ISD::CTTZ:
893 switch(VT) {
894 default: assert(0 && "Invalid cttz!"); break;
895 case MVT::i1: return getConstant(Val == 0, VT);
896 case MVT::i8:
897 Tmp1 = (unsigned)Val | 0x100;
898 return getConstant(CountTrailingZeros_32(Tmp1), VT);
899 case MVT::i16:
900 Tmp1 = (unsigned)Val | 0x10000;
901 return getConstant(CountTrailingZeros_32(Tmp1), VT);
902 case MVT::i32:
903 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
904 case MVT::i64:
905 return getConstant(CountTrailingZeros_64(Val), VT);
906 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907 }
908 }
909
Chris Lattner94683772005-12-23 05:30:37 +0000910 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
912 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000913 case ISD::FNEG:
914 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000915 case ISD::FABS:
916 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000917 case ISD::FP_ROUND:
918 case ISD::FP_EXTEND:
919 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000920 case ISD::FP_TO_SINT:
921 return getConstant((int64_t)C->getValue(), VT);
922 case ISD::FP_TO_UINT:
923 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000924 case ISD::BIT_CONVERT:
925 if (VT == MVT::i32) {
926 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
927 return getConstant(FloatToBits(C->getValue()), VT);
928 } else if (VT == MVT::i64) {
929 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
930 return getConstant(DoubleToBits(C->getValue()), VT);
931 }
932 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 }
934
935 unsigned OpOpcode = Operand.Val->getOpcode();
936 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000937 case ISD::TokenFactor:
938 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939 case ISD::SIGN_EXTEND:
940 if (Operand.getValueType() == VT) return Operand; // noop extension
941 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
942 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
943 break;
944 case ISD::ZERO_EXTEND:
945 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000946 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000947 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000949 case ISD::ANY_EXTEND:
950 if (Operand.getValueType() == VT) return Operand; // noop extension
951 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
952 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
953 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
954 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 case ISD::TRUNCATE:
956 if (Operand.getValueType() == VT) return Operand; // noop truncate
957 if (OpOpcode == ISD::TRUNCATE)
958 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000959 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
960 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000961 // If the source is smaller than the dest, we still need an extend.
962 if (Operand.Val->getOperand(0).getValueType() < VT)
963 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
964 else if (Operand.Val->getOperand(0).getValueType() > VT)
965 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
966 else
967 return Operand.Val->getOperand(0);
968 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969 break;
Chris Lattner35481892005-12-23 00:16:34 +0000970 case ISD::BIT_CONVERT:
971 // Basic sanity checking.
972 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
973 "Cannot BIT_CONVERT between two different types!");
974 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +0000975 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
976 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +0000977 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000978 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000979 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
980 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000981 Operand.Val->getOperand(0));
982 if (OpOpcode == ISD::FNEG) // --X -> X
983 return Operand.Val->getOperand(0);
984 break;
985 case ISD::FABS:
986 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
987 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
988 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 }
990
Chris Lattner43247a12005-08-25 19:12:10 +0000991 SDNode *N;
992 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
993 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000994 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000995 E = N = new SDNode(Opcode, Operand);
996 } else {
997 N = new SDNode(Opcode, Operand);
998 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 N->setValueTypes(VT);
1000 AllNodes.push_back(N);
1001 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001002}
1003
Chris Lattner36019aa2005-04-18 03:48:41 +00001004
1005
Chris Lattnerc3aae252005-01-07 07:46:32 +00001006SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1007 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001008#ifndef NDEBUG
1009 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001010 case ISD::TokenFactor:
1011 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1012 N2.getValueType() == MVT::Other && "Invalid token factor!");
1013 break;
Chris Lattner76365122005-01-16 02:23:22 +00001014 case ISD::AND:
1015 case ISD::OR:
1016 case ISD::XOR:
1017 case ISD::UDIV:
1018 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001019 case ISD::MULHU:
1020 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001021 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1022 // fall through
1023 case ISD::ADD:
1024 case ISD::SUB:
1025 case ISD::MUL:
1026 case ISD::SDIV:
1027 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001028 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1029 // fall through.
1030 case ISD::FADD:
1031 case ISD::FSUB:
1032 case ISD::FMUL:
1033 case ISD::FDIV:
1034 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001035 assert(N1.getValueType() == N2.getValueType() &&
1036 N1.getValueType() == VT && "Binary operator types must match!");
1037 break;
1038
1039 case ISD::SHL:
1040 case ISD::SRA:
1041 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001042 case ISD::ROTL:
1043 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001044 assert(VT == N1.getValueType() &&
1045 "Shift operators return type must be the same as their first arg");
1046 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001047 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001048 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001049 case ISD::FP_ROUND_INREG: {
1050 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1051 assert(VT == N1.getValueType() && "Not an inreg round!");
1052 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1053 "Cannot FP_ROUND_INREG integer types");
1054 assert(EVT <= VT && "Not rounding down!");
1055 break;
1056 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001057 case ISD::AssertSext:
1058 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001059 case ISD::SIGN_EXTEND_INREG: {
1060 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1061 assert(VT == N1.getValueType() && "Not an inreg extend!");
1062 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1063 "Cannot *_EXTEND_INREG FP types");
1064 assert(EVT <= VT && "Not extending!");
1065 }
1066
Chris Lattner76365122005-01-16 02:23:22 +00001067 default: break;
1068 }
1069#endif
1070
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1072 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1073 if (N1C) {
1074 if (N2C) {
1075 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1076 switch (Opcode) {
1077 case ISD::ADD: return getConstant(C1 + C2, VT);
1078 case ISD::SUB: return getConstant(C1 - C2, VT);
1079 case ISD::MUL: return getConstant(C1 * C2, VT);
1080 case ISD::UDIV:
1081 if (C2) return getConstant(C1 / C2, VT);
1082 break;
1083 case ISD::UREM :
1084 if (C2) return getConstant(C1 % C2, VT);
1085 break;
1086 case ISD::SDIV :
1087 if (C2) return getConstant(N1C->getSignExtended() /
1088 N2C->getSignExtended(), VT);
1089 break;
1090 case ISD::SREM :
1091 if (C2) return getConstant(N1C->getSignExtended() %
1092 N2C->getSignExtended(), VT);
1093 break;
1094 case ISD::AND : return getConstant(C1 & C2, VT);
1095 case ISD::OR : return getConstant(C1 | C2, VT);
1096 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001097 case ISD::SHL : return getConstant(C1 << C2, VT);
1098 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001099 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001100 case ISD::ROTL :
1101 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1102 VT);
1103 case ISD::ROTR :
1104 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1105 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 default: break;
1107 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 } else { // Cannonicalize constant to RHS if commutative
1109 if (isCommutativeBinOp(Opcode)) {
1110 std::swap(N1C, N2C);
1111 std::swap(N1, N2);
1112 }
1113 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114 }
1115
1116 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1117 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001118 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119 if (N2CFP) {
1120 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1121 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001122 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1123 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1124 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1125 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001126 if (C2) return getConstantFP(C1 / C2, VT);
1127 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001128 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001129 if (C2) return getConstantFP(fmod(C1, C2), VT);
1130 break;
1131 default: break;
1132 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 } else { // Cannonicalize constant to RHS if commutative
1134 if (isCommutativeBinOp(Opcode)) {
1135 std::swap(N1CFP, N2CFP);
1136 std::swap(N1, N2);
1137 }
1138 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001139 }
1140
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 // Finally, fold operations that do not require constants.
1142 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001143 case ISD::FP_ROUND_INREG:
1144 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1145 break;
1146 case ISD::SIGN_EXTEND_INREG: {
1147 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1148 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001149 break;
1150 }
1151
Nate Begemaneea805e2005-04-13 21:23:31 +00001152 // FIXME: figure out how to safely handle things like
1153 // int foo(int x) { return 1 << (x & 255); }
1154 // int bar() { return foo(256); }
1155#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001156 case ISD::SHL:
1157 case ISD::SRL:
1158 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001159 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001160 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001161 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001162 else if (N2.getOpcode() == ISD::AND)
1163 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1164 // If the and is only masking out bits that cannot effect the shift,
1165 // eliminate the and.
1166 unsigned NumBits = MVT::getSizeInBits(VT);
1167 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1168 return getNode(Opcode, VT, N1, N2.getOperand(0));
1169 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001170 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001171#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 }
1173
Chris Lattner27e9b412005-05-11 18:57:39 +00001174 // Memoize this node if possible.
1175 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001176 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1177 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001178 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1179 if (BON) return SDOperand(BON, 0);
1180
1181 BON = N = new SDNode(Opcode, N1, N2);
1182 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001183 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001184 }
1185
Chris Lattner3e011362005-05-14 07:45:46 +00001186 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001187 AllNodes.push_back(N);
1188 return SDOperand(N, 0);
1189}
1190
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1192 SDOperand N1, SDOperand N2, SDOperand N3) {
1193 // Perform various simplifications.
1194 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1195 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1196 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1197 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001198 case ISD::SETCC: {
1199 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001200 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001201 if (Simp.Val) return Simp;
1202 break;
1203 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001204 case ISD::SELECT:
1205 if (N1C)
1206 if (N1C->getValue())
1207 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001208 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001209 return N3; // select false, X, Y -> Y
1210
1211 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001212 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001213 case ISD::BRCOND:
1214 if (N2C)
1215 if (N2C->getValue()) // Unconditional branch
1216 return getNode(ISD::BR, MVT::Other, N1, N3);
1217 else
1218 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001219 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 }
1221
Chris Lattner385328c2005-05-14 07:42:29 +00001222 std::vector<SDOperand> Ops;
1223 Ops.reserve(3);
1224 Ops.push_back(N1);
1225 Ops.push_back(N2);
1226 Ops.push_back(N3);
1227
Chris Lattner43247a12005-08-25 19:12:10 +00001228 // Memoize node if it doesn't produce a flag.
1229 SDNode *N;
1230 if (VT != MVT::Flag) {
1231 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1232 if (E) return SDOperand(E, 0);
1233 E = N = new SDNode(Opcode, N1, N2, N3);
1234 } else {
1235 N = new SDNode(Opcode, N1, N2, N3);
1236 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001237 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 AllNodes.push_back(N);
1239 return SDOperand(N, 0);
1240}
1241
1242SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001243 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001244 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001245 std::vector<SDOperand> Ops;
1246 Ops.reserve(4);
1247 Ops.push_back(N1);
1248 Ops.push_back(N2);
1249 Ops.push_back(N3);
1250 Ops.push_back(N4);
1251 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001252}
1253
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001254SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1255 SDOperand N1, SDOperand N2, SDOperand N3,
1256 SDOperand N4, SDOperand N5) {
1257 std::vector<SDOperand> Ops;
1258 Ops.reserve(5);
1259 Ops.push_back(N1);
1260 Ops.push_back(N2);
1261 Ops.push_back(N3);
1262 Ops.push_back(N4);
1263 Ops.push_back(N5);
1264 return getNode(Opcode, VT, Ops);
1265}
1266
Evan Cheng7038daf2005-12-10 00:37:58 +00001267// setAdjCallChain - This method changes the token chain of an
1268// CALLSEQ_START/END node to be the specified operand.
1269void SDNode::setAdjCallChain(SDOperand N) {
1270 assert(N.getValueType() == MVT::Other);
1271 assert((getOpcode() == ISD::CALLSEQ_START ||
1272 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1273
1274 OperandList[0].Val->removeUser(this);
1275 OperandList[0] = N;
1276 OperandList[0].Val->Uses.push_back(this);
1277}
1278
1279
1280
1281SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1282 SDOperand Chain, SDOperand Ptr,
1283 SDOperand SV) {
1284 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1285 if (N) return SDOperand(N, 0);
1286 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1287
1288 // Loads have a token chain.
1289 setNodeValueTypes(N, VT, MVT::Other);
1290 AllNodes.push_back(N);
1291 return SDOperand(N, 0);
1292}
1293
1294SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1295 SDOperand Chain, SDOperand Ptr,
1296 SDOperand SV) {
1297 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1298 if (N) return SDOperand(N, 0);
1299 std::vector<SDOperand> Ops;
1300 Ops.reserve(5);
1301 Ops.push_back(Chain);
1302 Ops.push_back(Ptr);
1303 Ops.push_back(getConstant(Count, MVT::i32));
1304 Ops.push_back(getValueType(EVT));
1305 Ops.push_back(SV);
1306 std::vector<MVT::ValueType> VTs;
1307 VTs.reserve(2);
1308 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1309 return getNode(ISD::VLOAD, VTs, Ops);
1310}
1311
1312SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1313 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1314 MVT::ValueType EVT) {
1315 std::vector<SDOperand> Ops;
1316 Ops.reserve(4);
1317 Ops.push_back(Chain);
1318 Ops.push_back(Ptr);
1319 Ops.push_back(SV);
1320 Ops.push_back(getValueType(EVT));
1321 std::vector<MVT::ValueType> VTs;
1322 VTs.reserve(2);
1323 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1324 return getNode(Opcode, VTs, Ops);
1325}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001326
Chris Lattner0437cdd2005-05-09 04:14:13 +00001327SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001328 assert((!V || isa<PointerType>(V->getType())) &&
1329 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001330 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1331 if (N) return SDOperand(N, 0);
1332
1333 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001334 AllNodes.push_back(N);
1335 return SDOperand(N, 0);
1336}
1337
1338SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001339 std::vector<SDOperand> &Ops) {
1340 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001341 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001342 case 1: return getNode(Opcode, VT, Ops[0]);
1343 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1344 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001345 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001346 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001347
Chris Lattner89c34632005-05-14 06:20:26 +00001348 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001349 switch (Opcode) {
1350 default: break;
1351 case ISD::BRCONDTWOWAY:
1352 if (N1C)
1353 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001354 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001355 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001356 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001357 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001358 case ISD::BRTWOWAY_CC:
1359 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1360 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1361 "LHS and RHS of comparison must have same type!");
1362 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001363 case ISD::TRUNCSTORE: {
1364 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1365 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1366#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1367 // If this is a truncating store of a constant, convert to the desired type
1368 // and store it instead.
1369 if (isa<Constant>(Ops[0])) {
1370 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1371 if (isa<Constant>(Op))
1372 N1 = Op;
1373 }
1374 // Also for ConstantFP?
1375#endif
1376 if (Ops[0].getValueType() == EVT) // Normal store?
1377 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1378 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1379 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1380 "Can't do FP-INT conversion!");
1381 break;
1382 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001383 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001384 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001385 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1386 "LHS and RHS of condition must have same type!");
1387 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1388 "True and False arms of SelectCC must have same type!");
1389 assert(Ops[2].getValueType() == VT &&
1390 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001391 break;
1392 }
1393 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001394 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001395 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1396 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001397 break;
1398 }
Chris Lattneref847df2005-04-09 03:27:28 +00001399 }
1400
Chris Lattner385328c2005-05-14 07:42:29 +00001401 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001402 SDNode *N;
1403 if (VT != MVT::Flag) {
1404 SDNode *&E =
1405 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1406 if (E) return SDOperand(E, 0);
1407 E = N = new SDNode(Opcode, Ops);
1408 } else {
1409 N = new SDNode(Opcode, Ops);
1410 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001411 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001412 AllNodes.push_back(N);
1413 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001414}
1415
Chris Lattner89c34632005-05-14 06:20:26 +00001416SDOperand SelectionDAG::getNode(unsigned Opcode,
1417 std::vector<MVT::ValueType> &ResultTys,
1418 std::vector<SDOperand> &Ops) {
1419 if (ResultTys.size() == 1)
1420 return getNode(Opcode, ResultTys[0], Ops);
1421
Chris Lattner5f056bf2005-07-10 01:55:33 +00001422 switch (Opcode) {
1423 case ISD::EXTLOAD:
1424 case ISD::SEXTLOAD:
1425 case ISD::ZEXTLOAD: {
1426 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1427 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1428 // If they are asking for an extending load from/to the same thing, return a
1429 // normal load.
1430 if (ResultTys[0] == EVT)
1431 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1432 assert(EVT < ResultTys[0] &&
1433 "Should only be an extending load, not truncating!");
1434 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1435 "Cannot sign/zero extend a FP load!");
1436 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1437 "Cannot convert from FP to Int or Int -> FP!");
1438 break;
1439 }
1440
Chris Lattnere89083a2005-05-14 07:25:05 +00001441 // FIXME: figure out how to safely handle things like
1442 // int foo(int x) { return 1 << (x & 255); }
1443 // int bar() { return foo(256); }
1444#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001445 case ISD::SRA_PARTS:
1446 case ISD::SRL_PARTS:
1447 case ISD::SHL_PARTS:
1448 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001449 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001450 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1451 else if (N3.getOpcode() == ISD::AND)
1452 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1453 // If the and is only masking out bits that cannot effect the shift,
1454 // eliminate the and.
1455 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1456 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1457 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1458 }
1459 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001460#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001461 }
Chris Lattner89c34632005-05-14 06:20:26 +00001462
Chris Lattner43247a12005-08-25 19:12:10 +00001463 // Memoize the node unless it returns a flag.
1464 SDNode *N;
1465 if (ResultTys.back() != MVT::Flag) {
1466 SDNode *&E =
1467 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1468 if (E) return SDOperand(E, 0);
1469 E = N = new SDNode(Opcode, Ops);
1470 } else {
1471 N = new SDNode(Opcode, Ops);
1472 }
Chris Lattnera3255112005-11-08 23:30:28 +00001473 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001474 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001475 return SDOperand(N, 0);
1476}
1477
Chris Lattnera3255112005-11-08 23:30:28 +00001478void SelectionDAG::setNodeValueTypes(SDNode *N,
1479 std::vector<MVT::ValueType> &RetVals) {
1480 switch (RetVals.size()) {
1481 case 0: return;
1482 case 1: N->setValueTypes(RetVals[0]); return;
1483 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1484 default: break;
1485 }
1486
1487 std::list<std::vector<MVT::ValueType> >::iterator I =
1488 std::find(VTList.begin(), VTList.end(), RetVals);
1489 if (I == VTList.end()) {
1490 VTList.push_front(RetVals);
1491 I = VTList.begin();
1492 }
1493
1494 N->setValueTypes(&(*I)[0], I->size());
1495}
1496
1497void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1498 MVT::ValueType VT2) {
1499 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1500 E = VTList.end(); I != E; ++I) {
1501 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1502 N->setValueTypes(&(*I)[0], 2);
1503 return;
1504 }
1505 }
1506 std::vector<MVT::ValueType> V;
1507 V.push_back(VT1);
1508 V.push_back(VT2);
1509 VTList.push_front(V);
1510 N->setValueTypes(&(*VTList.begin())[0], 2);
1511}
1512
Chris Lattner149c58c2005-08-16 18:17:10 +00001513
1514/// SelectNodeTo - These are used for target selectors to *mutate* the
1515/// specified node to have the specified return type, Target opcode, and
1516/// operands. Note that target opcodes are stored as
1517/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001518///
1519/// Note that SelectNodeTo returns the resultant node. If there is already a
1520/// node of the specified opcode and operands, it returns that node instead of
1521/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001522SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1523 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001524 // If an identical node already exists, use it.
1525 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1526 if (ON) return SDOperand(ON, 0);
1527
Chris Lattner7651fa42005-08-24 23:00:29 +00001528 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001529
Chris Lattner7651fa42005-08-24 23:00:29 +00001530 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1531 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001532
1533 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001534 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001535}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001536
Chris Lattnereb19e402005-11-30 22:45:14 +00001537SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1538 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001539 // If an identical node already exists, use it.
1540 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1541 std::make_pair(Op1, VT))];
1542 if (ON) return SDOperand(ON, 0);
1543
Chris Lattner149c58c2005-08-16 18:17:10 +00001544 RemoveNodeFromCSEMaps(N);
1545 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1546 N->setValueTypes(VT);
1547 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001548
1549 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001550 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001551}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001552
Chris Lattnereb19e402005-11-30 22:45:14 +00001553SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1554 MVT::ValueType VT, SDOperand Op1,
1555 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001556 // If an identical node already exists, use it.
1557 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1558 std::make_pair(Op1, Op2))];
1559 if (ON) return SDOperand(ON, 0);
1560
Chris Lattner149c58c2005-08-16 18:17:10 +00001561 RemoveNodeFromCSEMaps(N);
1562 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1563 N->setValueTypes(VT);
1564 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001565
1566 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001567 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001568}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001569
Chris Lattnereb19e402005-11-30 22:45:14 +00001570SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1571 MVT::ValueType VT, SDOperand Op1,
1572 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001573 // If an identical node already exists, use it.
1574 std::vector<SDOperand> OpList;
1575 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1576 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1577 std::make_pair(VT, OpList))];
1578 if (ON) return SDOperand(ON, 0);
1579
Chris Lattner149c58c2005-08-16 18:17:10 +00001580 RemoveNodeFromCSEMaps(N);
1581 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1582 N->setValueTypes(VT);
1583 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001584
1585 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001586 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001587}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001588
Chris Lattnereb19e402005-11-30 22:45:14 +00001589SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1590 MVT::ValueType VT, SDOperand Op1,
1591 SDOperand Op2, SDOperand Op3,
1592 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001593 // If an identical node already exists, use it.
1594 std::vector<SDOperand> OpList;
1595 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1596 OpList.push_back(Op4);
1597 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1598 std::make_pair(VT, OpList))];
1599 if (ON) return SDOperand(ON, 0);
1600
Nate Begeman294a0a12005-08-18 07:30:15 +00001601 RemoveNodeFromCSEMaps(N);
1602 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1603 N->setValueTypes(VT);
1604 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001605
1606 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001607 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001608}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001609
Chris Lattnereb19e402005-11-30 22:45:14 +00001610SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1611 MVT::ValueType VT, SDOperand Op1,
1612 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1613 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001614 // If an identical node already exists, use it.
1615 std::vector<SDOperand> OpList;
1616 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1617 OpList.push_back(Op4); OpList.push_back(Op5);
1618 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1619 std::make_pair(VT, OpList))];
1620 if (ON) return SDOperand(ON, 0);
1621
Chris Lattner6b09a292005-08-21 18:49:33 +00001622 RemoveNodeFromCSEMaps(N);
1623 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1624 N->setValueTypes(VT);
1625 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001626
1627 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001628 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001629}
Chris Lattner149c58c2005-08-16 18:17:10 +00001630
Chris Lattnereb19e402005-11-30 22:45:14 +00001631SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1632 MVT::ValueType VT, SDOperand Op1,
1633 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1634 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001635 // If an identical node already exists, use it.
1636 std::vector<SDOperand> OpList;
1637 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1638 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1639 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1640 std::make_pair(VT, OpList))];
1641 if (ON) return SDOperand(ON, 0);
1642
Evan Cheng61ca74b2005-11-30 02:04:11 +00001643 RemoveNodeFromCSEMaps(N);
1644 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1645 N->setValueTypes(VT);
1646 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001647
1648 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001649 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001650}
1651
Chris Lattnereb19e402005-11-30 22:45:14 +00001652SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1653 MVT::ValueType VT1, MVT::ValueType VT2,
1654 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001655 // If an identical node already exists, use it.
1656 std::vector<SDOperand> OpList;
1657 OpList.push_back(Op1); OpList.push_back(Op2);
1658 std::vector<MVT::ValueType> VTList;
1659 VTList.push_back(VT1); VTList.push_back(VT2);
1660 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1661 std::make_pair(VTList, OpList))];
1662 if (ON) return SDOperand(ON, 0);
1663
Chris Lattner0fb094f2005-11-19 01:44:53 +00001664 RemoveNodeFromCSEMaps(N);
1665 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1666 setNodeValueTypes(N, VT1, VT2);
1667 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001668
1669 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001670 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001671}
1672
Chris Lattnereb19e402005-11-30 22:45:14 +00001673SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1674 MVT::ValueType VT1, MVT::ValueType VT2,
1675 SDOperand Op1, SDOperand Op2,
1676 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001677 // If an identical node already exists, use it.
1678 std::vector<SDOperand> OpList;
1679 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1680 std::vector<MVT::ValueType> VTList;
1681 VTList.push_back(VT1); VTList.push_back(VT2);
1682 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1683 std::make_pair(VTList, OpList))];
1684 if (ON) return SDOperand(ON, 0);
1685
Chris Lattner0fb094f2005-11-19 01:44:53 +00001686 RemoveNodeFromCSEMaps(N);
1687 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1688 setNodeValueTypes(N, VT1, VT2);
1689 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001690
1691 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001692 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001693}
1694
Chris Lattnereb19e402005-11-30 22:45:14 +00001695SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1696 MVT::ValueType VT1, MVT::ValueType VT2,
1697 SDOperand Op1, SDOperand Op2,
1698 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001699 // If an identical node already exists, use it.
1700 std::vector<SDOperand> OpList;
1701 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1702 OpList.push_back(Op4);
1703 std::vector<MVT::ValueType> VTList;
1704 VTList.push_back(VT1); VTList.push_back(VT2);
1705 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1706 std::make_pair(VTList, OpList))];
1707 if (ON) return SDOperand(ON, 0);
1708
Chris Lattner0fb094f2005-11-19 01:44:53 +00001709 RemoveNodeFromCSEMaps(N);
1710 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1711 setNodeValueTypes(N, VT1, VT2);
1712 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001713
1714 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001715 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001716}
1717
Chris Lattnereb19e402005-11-30 22:45:14 +00001718SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1719 MVT::ValueType VT1, MVT::ValueType VT2,
1720 SDOperand Op1, SDOperand Op2,
1721 SDOperand Op3, SDOperand Op4,
1722 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001723 // If an identical node already exists, use it.
1724 std::vector<SDOperand> OpList;
1725 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1726 OpList.push_back(Op4); OpList.push_back(Op5);
1727 std::vector<MVT::ValueType> VTList;
1728 VTList.push_back(VT1); VTList.push_back(VT2);
1729 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1730 std::make_pair(VTList, OpList))];
1731 if (ON) return SDOperand(ON, 0);
1732
Chris Lattner0fb094f2005-11-19 01:44:53 +00001733 RemoveNodeFromCSEMaps(N);
1734 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1735 setNodeValueTypes(N, VT1, VT2);
1736 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001737
1738 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001739 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001740}
1741
1742// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00001743/// This can cause recursive merging of nodes in the DAG.
1744///
Chris Lattner8b52f212005-08-26 18:36:28 +00001745/// This version assumes From/To have a single result value.
1746///
Chris Lattner1e111c72005-09-07 05:37:01 +00001747void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1748 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001749 SDNode *From = FromN.Val, *To = ToN.Val;
1750 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1751 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001752 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001753
Chris Lattner8b8749f2005-08-17 19:00:20 +00001754 while (!From->use_empty()) {
1755 // Process users until they are all gone.
1756 SDNode *U = *From->use_begin();
1757
1758 // This node is about to morph, remove its old self from the CSE maps.
1759 RemoveNodeFromCSEMaps(U);
1760
Chris Lattner65113b22005-11-08 22:07:03 +00001761 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1762 I != E; ++I)
1763 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001764 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001765 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001766 To->addUser(U);
1767 }
1768
1769 // Now that we have modified U, add it back to the CSE maps. If it already
1770 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001771 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1772 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001773 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001774 if (Deleted) Deleted->push_back(U);
1775 DeleteNodeNotInCSEMaps(U);
1776 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001777 }
1778}
1779
1780/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1781/// This can cause recursive merging of nodes in the DAG.
1782///
1783/// This version assumes From/To have matching types and numbers of result
1784/// values.
1785///
Chris Lattner1e111c72005-09-07 05:37:01 +00001786void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1787 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001788 assert(From != To && "Cannot replace uses of with self");
1789 assert(From->getNumValues() == To->getNumValues() &&
1790 "Cannot use this version of ReplaceAllUsesWith!");
1791 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001792 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001793 return;
1794 }
1795
1796 while (!From->use_empty()) {
1797 // Process users until they are all gone.
1798 SDNode *U = *From->use_begin();
1799
1800 // This node is about to morph, remove its old self from the CSE maps.
1801 RemoveNodeFromCSEMaps(U);
1802
Chris Lattner65113b22005-11-08 22:07:03 +00001803 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1804 I != E; ++I)
1805 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001806 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001807 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001808 To->addUser(U);
1809 }
1810
1811 // Now that we have modified U, add it back to the CSE maps. If it already
1812 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001813 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1814 ReplaceAllUsesWith(U, Existing, Deleted);
1815 // U is now dead.
1816 if (Deleted) Deleted->push_back(U);
1817 DeleteNodeNotInCSEMaps(U);
1818 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001819 }
1820}
1821
Chris Lattner8b52f212005-08-26 18:36:28 +00001822/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1823/// This can cause recursive merging of nodes in the DAG.
1824///
1825/// This version can replace From with any result values. To must match the
1826/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001827void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001828 const std::vector<SDOperand> &To,
1829 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001830 assert(From->getNumValues() == To.size() &&
1831 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001832 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001833 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001834 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001835 return;
1836 }
1837
1838 while (!From->use_empty()) {
1839 // Process users until they are all gone.
1840 SDNode *U = *From->use_begin();
1841
1842 // This node is about to morph, remove its old self from the CSE maps.
1843 RemoveNodeFromCSEMaps(U);
1844
Chris Lattner65113b22005-11-08 22:07:03 +00001845 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1846 I != E; ++I)
1847 if (I->Val == From) {
1848 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001849 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001850 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001851 ToOp.Val->addUser(U);
1852 }
1853
1854 // Now that we have modified U, add it back to the CSE maps. If it already
1855 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001856 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1857 ReplaceAllUsesWith(U, Existing, Deleted);
1858 // U is now dead.
1859 if (Deleted) Deleted->push_back(U);
1860 DeleteNodeNotInCSEMaps(U);
1861 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001862 }
1863}
1864
1865
Jim Laskey58b968b2005-08-17 20:08:02 +00001866//===----------------------------------------------------------------------===//
1867// SDNode Class
1868//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001869
Chris Lattnera3255112005-11-08 23:30:28 +00001870
1871/// getValueTypeList - Return a pointer to the specified value type.
1872///
1873MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1874 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1875 VTs[VT] = VT;
1876 return &VTs[VT];
1877}
1878
Chris Lattner5c884562005-01-12 18:37:47 +00001879/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1880/// indicated value. This method ignores uses of other values defined by this
1881/// operation.
1882bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1883 assert(Value < getNumValues() && "Bad value!");
1884
1885 // If there is only one value, this is easy.
1886 if (getNumValues() == 1)
1887 return use_size() == NUses;
1888 if (Uses.size() < NUses) return false;
1889
1890 SDOperand TheValue(this, Value);
1891
1892 std::set<SDNode*> UsersHandled;
1893
1894 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1895 UI != E; ++UI) {
1896 SDNode *User = *UI;
1897 if (User->getNumOperands() == 1 ||
1898 UsersHandled.insert(User).second) // First time we've seen this?
1899 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1900 if (User->getOperand(i) == TheValue) {
1901 if (NUses == 0)
1902 return false; // too many uses
1903 --NUses;
1904 }
1905 }
1906
1907 // Found exactly the right number of uses?
1908 return NUses == 0;
1909}
1910
1911
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001912const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001913 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001914 default:
1915 if (getOpcode() < ISD::BUILTIN_OP_END)
1916 return "<<Unknown DAG Node>>";
1917 else {
Evan Cheng72261582005-12-20 06:22:03 +00001918 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001919 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001920 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1921 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00001922
Evan Cheng72261582005-12-20 06:22:03 +00001923 TargetLowering &TLI = G->getTargetLoweringInfo();
1924 const char *Name =
1925 TLI.getTargetNodeName(getOpcode());
1926 if (Name) return Name;
1927 }
1928
1929 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001930 }
1931
Andrew Lenharth95762122005-03-31 21:24:06 +00001932 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001933 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001934 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001935 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00001936 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001937 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001938 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001939 case ISD::AssertSext: return "AssertSext";
1940 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001941 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001942 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001943 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001944 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001945 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001946 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001947 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001948 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001949 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001950 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001951 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001952 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001953 case ISD::ConstantPool: return "ConstantPool";
1954 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001955 case ISD::CopyToReg: return "CopyToReg";
1956 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001957 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00001958 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001959
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001960 // Unary operators
1961 case ISD::FABS: return "fabs";
1962 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001963 case ISD::FSQRT: return "fsqrt";
1964 case ISD::FSIN: return "fsin";
1965 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001966
1967 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001968 case ISD::ADD: return "add";
1969 case ISD::SUB: return "sub";
1970 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001971 case ISD::MULHU: return "mulhu";
1972 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001973 case ISD::SDIV: return "sdiv";
1974 case ISD::UDIV: return "udiv";
1975 case ISD::SREM: return "srem";
1976 case ISD::UREM: return "urem";
1977 case ISD::AND: return "and";
1978 case ISD::OR: return "or";
1979 case ISD::XOR: return "xor";
1980 case ISD::SHL: return "shl";
1981 case ISD::SRA: return "sra";
1982 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00001983 case ISD::ROTL: return "rotl";
1984 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00001985 case ISD::FADD: return "fadd";
1986 case ISD::FSUB: return "fsub";
1987 case ISD::FMUL: return "fmul";
1988 case ISD::FDIV: return "fdiv";
1989 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001990 case ISD::VADD: return "vadd";
1991 case ISD::VSUB: return "vsub";
1992 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00001993
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001994 case ISD::SETCC: return "setcc";
1995 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001996 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001997 case ISD::ADD_PARTS: return "add_parts";
1998 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001999 case ISD::SHL_PARTS: return "shl_parts";
2000 case ISD::SRA_PARTS: return "sra_parts";
2001 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002002
Chris Lattner7f644642005-04-28 21:44:03 +00002003 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002004 case ISD::SIGN_EXTEND: return "sign_extend";
2005 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002006 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002007 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002008 case ISD::TRUNCATE: return "truncate";
2009 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002010 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002011 case ISD::FP_EXTEND: return "fp_extend";
2012
2013 case ISD::SINT_TO_FP: return "sint_to_fp";
2014 case ISD::UINT_TO_FP: return "uint_to_fp";
2015 case ISD::FP_TO_SINT: return "fp_to_sint";
2016 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002017 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002018
2019 // Control flow instructions
2020 case ISD::BR: return "br";
2021 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002022 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002023 case ISD::BR_CC: return "br_cc";
2024 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002025 case ISD::RET: return "ret";
2026 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002027 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002028 case ISD::CALLSEQ_START: return "callseq_start";
2029 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002030
2031 // Other operators
2032 case ISD::LOAD: return "load";
2033 case ISD::STORE: return "store";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002034 case ISD::VLOAD: return "vload";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002035 case ISD::EXTLOAD: return "extload";
2036 case ISD::SEXTLOAD: return "sextload";
2037 case ISD::ZEXTLOAD: return "zextload";
2038 case ISD::TRUNCSTORE: return "truncstore";
2039
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002040 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002041 case ISD::EXTRACT_ELEMENT: return "extract_element";
2042 case ISD::BUILD_PAIR: return "build_pair";
2043 case ISD::STACKSAVE: return "stacksave";
2044 case ISD::STACKRESTORE: return "stackrestore";
2045
2046 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002047 case ISD::MEMSET: return "memset";
2048 case ISD::MEMCPY: return "memcpy";
2049 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002050
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002051 // Bit manipulation
2052 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002053 case ISD::CTPOP: return "ctpop";
2054 case ISD::CTTZ: return "cttz";
2055 case ISD::CTLZ: return "ctlz";
2056
2057 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002058 case ISD::READPORT: return "readport";
2059 case ISD::WRITEPORT: return "writeport";
2060 case ISD::READIO: return "readio";
2061 case ISD::WRITEIO: return "writeio";
2062
Chris Lattner36ce6912005-11-29 06:21:05 +00002063 // Debug info
2064 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002065 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002066 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002067
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002068 case ISD::CONDCODE:
2069 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002070 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002071 case ISD::SETOEQ: return "setoeq";
2072 case ISD::SETOGT: return "setogt";
2073 case ISD::SETOGE: return "setoge";
2074 case ISD::SETOLT: return "setolt";
2075 case ISD::SETOLE: return "setole";
2076 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002077
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002078 case ISD::SETO: return "seto";
2079 case ISD::SETUO: return "setuo";
2080 case ISD::SETUEQ: return "setue";
2081 case ISD::SETUGT: return "setugt";
2082 case ISD::SETUGE: return "setuge";
2083 case ISD::SETULT: return "setult";
2084 case ISD::SETULE: return "setule";
2085 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002086
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002087 case ISD::SETEQ: return "seteq";
2088 case ISD::SETGT: return "setgt";
2089 case ISD::SETGE: return "setge";
2090 case ISD::SETLT: return "setlt";
2091 case ISD::SETLE: return "setle";
2092 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002093 }
2094 }
2095}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002096
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002097void SDNode::dump() const { dump(0); }
2098void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002099 std::cerr << (void*)this << ": ";
2100
2101 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2102 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002103 if (getValueType(i) == MVT::Other)
2104 std::cerr << "ch";
2105 else
2106 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002107 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002108 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109
2110 std::cerr << " ";
2111 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2112 if (i) std::cerr << ", ";
2113 std::cerr << (void*)getOperand(i).Val;
2114 if (unsigned RN = getOperand(i).ResNo)
2115 std::cerr << ":" << RN;
2116 }
2117
2118 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2119 std::cerr << "<" << CSDN->getValue() << ">";
2120 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2121 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002122 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002123 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002124 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002125 std::cerr << "<";
2126 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002127 if (offset > 0)
2128 std::cerr << " + " << offset;
2129 else
2130 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002131 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002132 std::cerr << "<" << FIDN->getIndex() << ">";
2133 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002134 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002135 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002136 std::cerr << "<";
2137 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2138 if (LBB)
2139 std::cerr << LBB->getName() << " ";
2140 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002141 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002142 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002143 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2144 } else {
2145 std::cerr << " #" << R->getReg();
2146 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002147 } else if (const ExternalSymbolSDNode *ES =
2148 dyn_cast<ExternalSymbolSDNode>(this)) {
2149 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002150 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2151 if (M->getValue())
2152 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2153 else
2154 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002155 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2156 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002157 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002158}
2159
Chris Lattnerde202b32005-11-09 23:47:37 +00002160static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002161 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2162 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002163 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002164 else
2165 std::cerr << "\n" << std::string(indent+2, ' ')
2166 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002167
Chris Lattnerea946cd2005-01-09 20:38:33 +00002168
2169 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002170 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002171}
2172
Chris Lattnerc3aae252005-01-07 07:46:32 +00002173void SelectionDAG::dump() const {
2174 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002175 std::vector<const SDNode*> Nodes;
2176 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2177 I != E; ++I)
2178 Nodes.push_back(I);
2179
Chris Lattner49d24712005-01-09 20:26:36 +00002180 std::sort(Nodes.begin(), Nodes.end());
2181
2182 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002183 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002184 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002185 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002186
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002187 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002188
Chris Lattnerc3aae252005-01-07 07:46:32 +00002189 std::cerr << "\n\n";
2190}
2191