blob: ec75dfee32ae4a437f6f35a10427f3cab54b8cd3 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-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 Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner061a1ea2005-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 Lattner6667bdb2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattner0875d1a2005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattner90b7c132005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerbc892262005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Evan Cheng9fd95412005-12-19 23:11:49 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000025#include <iostream>
Chris Lattner9c667932005-01-07 21:09:16 +000026#include <set>
Chris Lattner061a1ea2005-01-07 07:46:32 +000027#include <cmath>
Jeff Cohen7d1670da2005-01-09 20:41:56 +000028#include <algorithm>
Chris Lattner560b5e42004-06-02 04:28:06 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattnerfde3a212005-01-09 20:52:51 +000031static bool isCommutativeBinOp(unsigned Opcode) {
32 switch (Opcode) {
33 case ISD::ADD:
34 case ISD::MUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +000035 case ISD::FADD:
36 case ISD::FMUL:
Chris Lattnerfde3a212005-01-09 20:52:51 +000037 case ISD::AND:
38 case ISD::OR:
39 case ISD::XOR: return true;
40 default: return false; // FIXME: Need commutative info for user ops!
41 }
42}
43
44static bool isAssociativeBinOp(unsigned Opcode) {
45 switch (Opcode) {
46 case ISD::ADD:
47 case ISD::MUL:
48 case ISD::AND:
49 case ISD::OR:
50 case ISD::XOR: return true;
51 default: return false; // FIXME: Need associative info for user ops!
52 }
53}
54
Chris Lattnerfde3a212005-01-09 20:52:51 +000055// isInvertibleForFree - Return true if there is no cost to emitting the logical
56// inverse of this node.
57static bool isInvertibleForFree(SDOperand N) {
58 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattnerd47675e2005-08-09 20:20:18 +000059 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattnerfde3a212005-01-09 20:52:51 +000060 return true;
Misha Brukman835702a2005-04-21 22:36:52 +000061 return false;
Chris Lattnerfde3a212005-01-09 20:52:51 +000062}
63
Jim Laskeyd66e6162005-08-17 20:08:02 +000064//===----------------------------------------------------------------------===//
65// ConstantFPSDNode Class
66//===----------------------------------------------------------------------===//
67
68/// isExactlyValue - We don't rely on operator== working on double values, as
69/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
70/// As such, this method can be used to do an exact bit-for-bit comparison of
71/// two floating point values.
72bool ConstantFPSDNode::isExactlyValue(double V) const {
73 return DoubleToBits(V) == DoubleToBits(Value);
74}
75
76//===----------------------------------------------------------------------===//
77// ISD Class
78//===----------------------------------------------------------------------===//
Chris Lattnerfde3a212005-01-09 20:52:51 +000079
Chris Lattner061a1ea2005-01-07 07:46:32 +000080/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
81/// when given the operation for (X op Y).
82ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
83 // To perform this operation, we just need to swap the L and G bits of the
84 // operation.
85 unsigned OldL = (Operation >> 2) & 1;
86 unsigned OldG = (Operation >> 1) & 1;
87 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
88 (OldL << 1) | // New G bit
89 (OldG << 2)); // New L bit.
90}
91
92/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
93/// 'op' is a valid SetCC operation.
94ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
95 unsigned Operation = Op;
96 if (isInteger)
97 Operation ^= 7; // Flip L, G, E bits, but not U.
98 else
99 Operation ^= 15; // Flip all of the condition bits.
100 if (Operation > ISD::SETTRUE2)
101 Operation &= ~8; // Don't let N and U bits get set.
102 return ISD::CondCode(Operation);
103}
104
105
106/// isSignedOp - For an integer comparison, return 1 if the comparison is a
107/// signed operation and 2 if the result is an unsigned comparison. Return zero
108/// if the operation does not depend on the sign of the input (setne and seteq).
109static int isSignedOp(ISD::CondCode Opcode) {
110 switch (Opcode) {
111 default: assert(0 && "Illegal integer setcc operation!");
112 case ISD::SETEQ:
113 case ISD::SETNE: return 0;
114 case ISD::SETLT:
115 case ISD::SETLE:
116 case ISD::SETGT:
117 case ISD::SETGE: return 1;
118 case ISD::SETULT:
119 case ISD::SETULE:
120 case ISD::SETUGT:
121 case ISD::SETUGE: return 2;
122 }
123}
124
125/// getSetCCOrOperation - Return the result of a logical OR between different
126/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
127/// returns SETCC_INVALID if it is not possible to represent the resultant
128/// comparison.
129ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
130 bool isInteger) {
131 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
132 // Cannot fold a signed integer setcc with an unsigned integer setcc.
133 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000134
Chris Lattner061a1ea2005-01-07 07:46:32 +0000135 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000136
Chris Lattner061a1ea2005-01-07 07:46:32 +0000137 // If the N and U bits get set then the resultant comparison DOES suddenly
138 // care about orderedness, and is true when ordered.
139 if (Op > ISD::SETTRUE2)
140 Op &= ~16; // Clear the N bit.
141 return ISD::CondCode(Op);
142}
143
144/// getSetCCAndOperation - Return the result of a logical AND between different
145/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
146/// function returns zero if it is not possible to represent the resultant
147/// comparison.
148ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
149 bool isInteger) {
150 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
151 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000152 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000153
154 // Combine all of the condition bits.
155 return ISD::CondCode(Op1 & Op2);
156}
157
Chris Lattner90b7c132005-01-23 04:39:44 +0000158const TargetMachine &SelectionDAG::getTarget() const {
159 return TLI.getTargetMachine();
160}
161
Jim Laskeyd66e6162005-08-17 20:08:02 +0000162//===----------------------------------------------------------------------===//
163// SelectionDAG Class
164//===----------------------------------------------------------------------===//
Chris Lattner90b7c132005-01-23 04:39:44 +0000165
Chris Lattner9c667932005-01-07 21:09:16 +0000166/// RemoveDeadNodes - This method deletes all unreachable nodes in the
167/// SelectionDAG, including nodes (like loads) that have uses of their token
168/// chain but no other uses and no side effect. If a node is passed in as an
169/// argument, it is used as the seed for node deletion.
170void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner9c667932005-01-07 21:09:16 +0000171 // Create a dummy node (which is not added to allnodes), that adds a reference
172 // to the root node, preventing it from being deleted.
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000173 HandleSDNode Dummy(getRoot());
Chris Lattner9c667932005-01-07 21:09:16 +0000174
Chris Lattneraba48dd2005-11-08 18:52:27 +0000175 bool MadeChange = false;
176
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000177 // If we have a hint to start from, use it.
Chris Lattneraba48dd2005-11-08 18:52:27 +0000178 if (N && N->use_empty()) {
179 DestroyDeadNode(N);
180 MadeChange = true;
Chris Lattner9c667932005-01-07 21:09:16 +0000181 }
182
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000183 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
184 if (I->use_empty() && I->getOpcode() != 65535) {
185 // Node is dead, recursively delete newly dead uses.
186 DestroyDeadNode(I);
Chris Lattneraba48dd2005-11-08 18:52:27 +0000187 MadeChange = true;
188 }
Chris Lattneraba48dd2005-11-08 18:52:27 +0000189
190 // Walk the nodes list, removing the nodes we've marked as dead.
191 if (MadeChange) {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000192 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
193 SDNode *N = I++;
194 if (N->use_empty())
195 AllNodes.erase(N);
196 }
Chris Lattneraba48dd2005-11-08 18:52:27 +0000197 }
198
Chris Lattner9c667932005-01-07 21:09:16 +0000199 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000200 setRoot(Dummy.getValue());
Chris Lattner9c667932005-01-07 21:09:16 +0000201}
202
Chris Lattneraba48dd2005-11-08 18:52:27 +0000203/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
204/// graph. If it is the last user of any of its operands, recursively process
205/// them the same way.
206///
207void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner9c667932005-01-07 21:09:16 +0000208 // Okay, we really are going to delete this node. First take this out of the
209 // appropriate CSE map.
Chris Lattner19732782005-08-16 18:17:10 +0000210 RemoveNodeFromCSEMaps(N);
211
212 // Next, brutally remove the operand list. This is safe to do, as there are
213 // no cycles in the graph.
Chris Lattner7e4b5d32005-11-08 22:07:03 +0000214 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
215 SDNode *O = I->Val;
Chris Lattner19732782005-08-16 18:17:10 +0000216 O->removeUser(N);
217
218 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattneraba48dd2005-11-08 18:52:27 +0000219 if (O->use_empty())
220 DestroyDeadNode(O);
Chris Lattner19732782005-08-16 18:17:10 +0000221 }
Chris Lattner7e4b5d32005-11-08 22:07:03 +0000222 delete[] N->OperandList;
223 N->OperandList = 0;
224 N->NumOperands = 0;
Chris Lattneraba48dd2005-11-08 18:52:27 +0000225
226 // Mark the node as dead.
227 N->MorphNodeTo(65535);
Chris Lattner19732782005-08-16 18:17:10 +0000228}
229
Chris Lattnerc738d002005-08-29 21:59:31 +0000230void SelectionDAG::DeleteNode(SDNode *N) {
231 assert(N->use_empty() && "Cannot delete a node that is not dead!");
232
233 // First take this out of the appropriate CSE map.
234 RemoveNodeFromCSEMaps(N);
235
Chris Lattnerfe883ad2005-09-07 05:37:01 +0000236 // Finally, remove uses due to operands of this node, remove from the
237 // AllNodes list, and delete the node.
238 DeleteNodeNotInCSEMaps(N);
239}
240
241void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
242
Chris Lattnerc738d002005-08-29 21:59:31 +0000243 // Remove it from the AllNodes list.
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000244 AllNodes.remove(N);
Chris Lattnerc738d002005-08-29 21:59:31 +0000245
246 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner7e4b5d32005-11-08 22:07:03 +0000247 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
248 I->Val->removeUser(N);
249 delete[] N->OperandList;
250 N->OperandList = 0;
251 N->NumOperands = 0;
Chris Lattnerc738d002005-08-29 21:59:31 +0000252
253 delete N;
254}
255
Chris Lattner19732782005-08-16 18:17:10 +0000256/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
257/// correspond to it. This is useful when we're about to delete or repurpose
258/// the node. We don't want future request for structurally identical nodes
259/// to return N anymore.
260void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000261 bool Erased = false;
Chris Lattner9c667932005-01-07 21:09:16 +0000262 switch (N->getOpcode()) {
Chris Lattner06f1d0f2005-10-05 06:35:28 +0000263 case ISD::HANDLENODE: return; // noop.
Chris Lattner9c667932005-01-07 21:09:16 +0000264 case ISD::Constant:
Chris Lattner1e89e362005-09-02 19:15:44 +0000265 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
266 N->getValueType(0)));
Chris Lattner9c667932005-01-07 21:09:16 +0000267 break;
Chris Lattner0d2456e2005-08-17 00:34:06 +0000268 case ISD::TargetConstant:
Chris Lattner1e89e362005-09-02 19:15:44 +0000269 Erased = TargetConstants.erase(std::make_pair(
270 cast<ConstantSDNode>(N)->getValue(),
271 N->getValueType(0)));
Chris Lattner0d2456e2005-08-17 00:34:06 +0000272 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000273 case ISD::ConstantFP: {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000274 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner1e89e362005-09-02 19:15:44 +0000275 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner9c667932005-01-07 21:09:16 +0000276 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000277 }
Chris Lattner435b4022005-11-29 06:21:05 +0000278 case ISD::STRING:
279 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
280 break;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000281 case ISD::CONDCODE:
282 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
283 "Cond code doesn't exist!");
Chris Lattner1e89e362005-09-02 19:15:44 +0000284 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000285 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
286 break;
Evan Cheng11d61612005-11-30 02:49:21 +0000287 case ISD::GlobalAddress: {
288 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
289 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
290 GN->getOffset()));
Chris Lattner9c667932005-01-07 21:09:16 +0000291 break;
Evan Cheng11d61612005-11-30 02:49:21 +0000292 }
293 case ISD::TargetGlobalAddress: {
294 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
295 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
296 GN->getOffset()));
Chris Lattner1be7edd2005-08-19 22:31:04 +0000297 break;
Evan Cheng11d61612005-11-30 02:49:21 +0000298 }
Chris Lattner9c667932005-01-07 21:09:16 +0000299 case ISD::FrameIndex:
Chris Lattner1e89e362005-09-02 19:15:44 +0000300 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner9c667932005-01-07 21:09:16 +0000301 break;
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +0000302 case ISD::TargetFrameIndex:
Chris Lattner1e89e362005-09-02 19:15:44 +0000303 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +0000304 break;
Chris Lattner9c667932005-01-07 21:09:16 +0000305 case ISD::ConstantPool:
Chris Lattner1e89e362005-09-02 19:15:44 +0000306 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner9c667932005-01-07 21:09:16 +0000307 break;
Chris Lattner407c6412005-08-25 05:03:06 +0000308 case ISD::TargetConstantPool:
Chris Lattner1e89e362005-09-02 19:15:44 +0000309 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner407c6412005-08-25 05:03:06 +0000310 break;
Chris Lattner9c667932005-01-07 21:09:16 +0000311 case ISD::BasicBlock:
Chris Lattner1e89e362005-09-02 19:15:44 +0000312 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner9c667932005-01-07 21:09:16 +0000313 break;
314 case ISD::ExternalSymbol:
Chris Lattner1e89e362005-09-02 19:15:44 +0000315 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner9c667932005-01-07 21:09:16 +0000316 break;
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000317 case ISD::TargetExternalSymbol:
318 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
319 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000320 case ISD::VALUETYPE:
Chris Lattner1e89e362005-09-02 19:15:44 +0000321 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000322 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
323 break;
Chris Lattner33182322005-08-16 21:55:35 +0000324 case ISD::Register:
Chris Lattner1e89e362005-09-02 19:15:44 +0000325 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
326 N->getValueType(0)));
Chris Lattner33182322005-08-16 21:55:35 +0000327 break;
Chris Lattner1095dc92005-08-05 16:55:31 +0000328 case ISD::SRCVALUE: {
329 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner1e89e362005-09-02 19:15:44 +0000330 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattner1095dc92005-08-05 16:55:31 +0000331 break;
332 }
Chris Lattner9c667932005-01-07 21:09:16 +0000333 case ISD::LOAD:
Chris Lattner1e89e362005-09-02 19:15:44 +0000334 Erased = Loads.erase(std::make_pair(N->getOperand(1),
335 std::make_pair(N->getOperand(0),
336 N->getValueType(0))));
Chris Lattner9c667932005-01-07 21:09:16 +0000337 break;
Chris Lattner9c667932005-01-07 21:09:16 +0000338 default:
Chris Lattner1e89e362005-09-02 19:15:44 +0000339 if (N->getNumValues() == 1) {
Chris Lattnerb95b2802005-09-02 19:36:17 +0000340 if (N->getNumOperands() == 0) {
341 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
342 N->getValueType(0)));
343 } else if (N->getNumOperands() == 1) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000344 Erased =
345 UnaryOps.erase(std::make_pair(N->getOpcode(),
346 std::make_pair(N->getOperand(0),
347 N->getValueType(0))));
348 } else if (N->getNumOperands() == 2) {
349 Erased =
350 BinaryOps.erase(std::make_pair(N->getOpcode(),
351 std::make_pair(N->getOperand(0),
352 N->getOperand(1))));
353 } else {
354 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
355 Erased =
356 OneResultNodes.erase(std::make_pair(N->getOpcode(),
357 std::make_pair(N->getValueType(0),
358 Ops)));
359 }
Chris Lattner566307f2005-05-14 07:42:29 +0000360 } else {
Chris Lattnerd5531332005-05-14 06:20:26 +0000361 // Remove the node from the ArbitraryNodes map.
362 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
363 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner1e89e362005-09-02 19:15:44 +0000364 Erased =
365 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
366 std::make_pair(RV, Ops)));
Chris Lattnerd5531332005-05-14 06:20:26 +0000367 }
Chris Lattner9c667932005-01-07 21:09:16 +0000368 break;
369 }
Chris Lattner1e89e362005-09-02 19:15:44 +0000370#ifndef NDEBUG
371 // Verify that the node was actually in one of the CSE maps, unless it has a
372 // flag result (which cannot be CSE'd) or is one of the special cases that are
373 // not subject to CSE.
374 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
375 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner821628f2005-09-03 01:04:40 +0000376 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner1e89e362005-09-02 19:15:44 +0000377
378 N->dump();
379 assert(0 && "Node is not in map!");
380 }
381#endif
Chris Lattner9c667932005-01-07 21:09:16 +0000382}
383
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000384/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
385/// has been taken out and modified in some way. If the specified node already
386/// exists in the CSE maps, do not modify the maps, but return the existing node
387/// instead. If it doesn't exist, add it and return null.
388///
389SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
390 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc1740482005-11-30 18:20:52 +0000391 if (N->getOpcode() == ISD::CALLSEQ_START ||
Chris Lattner0142afd2005-12-01 23:14:50 +0000392 N->getOpcode() == ISD::CALLSEQ_END ||
Chris Lattner50b2d302005-12-19 22:21:21 +0000393 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattner0142afd2005-12-01 23:14:50 +0000394 return 0; // Never add these nodes.
Chris Lattnerc1740482005-11-30 18:20:52 +0000395
Chris Lattner50b2d302005-12-19 22:21:21 +0000396 // Check that remaining values produced are not flags.
397 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
398 if (N->getValueType(i) == MVT::Flag)
399 return 0; // Never CSE anything that produces a flag.
400
Chris Lattner0142afd2005-12-01 23:14:50 +0000401 if (N->getNumValues() == 1) {
402 if (N->getNumOperands() == 1) {
403 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
405 N->getValueType(0)))];
406 if (U) return U;
407 U = N;
408 } else if (N->getNumOperands() == 2) {
409 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
410 std::make_pair(N->getOperand(0),
411 N->getOperand(1)))];
412 if (B) return B;
413 B = N;
414 } else {
415 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
416 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
417 std::make_pair(N->getValueType(0), Ops))];
418 if (ORN) return ORN;
419 ORN = N;
420 }
421 } else {
422 if (N->getOpcode() == ISD::LOAD) {
423 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
424 std::make_pair(N->getOperand(0),
425 N->getValueType(0)))];
426 if (L) return L;
427 L = N;
428 } else {
429 // Remove the node from the ArbitraryNodes map.
430 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
431 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
432 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
433 std::make_pair(RV, Ops))];
434 if (AN) return AN;
435 AN = N;
436 }
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000437 }
438 return 0;
Chris Lattnerab0de9d2005-08-17 19:00:20 +0000439}
440
441
Chris Lattner9c667932005-01-07 21:09:16 +0000442
Chris Lattner600d3082003-08-11 14:57:33 +0000443SelectionDAG::~SelectionDAG() {
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000444 while (!AllNodes.empty()) {
445 SDNode *N = AllNodes.begin();
Chris Lattner7e4b5d32005-11-08 22:07:03 +0000446 delete [] N->OperandList;
447 N->OperandList = 0;
448 N->NumOperands = 0;
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000449 AllNodes.pop_front();
Chris Lattner7e4b5d32005-11-08 22:07:03 +0000450 }
Chris Lattner600d3082003-08-11 14:57:33 +0000451}
452
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000453SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner8c3d4092005-04-13 19:41:05 +0000454 if (Op.getValueType() == VT) return Op;
Jeff Cohen915594d2005-05-10 02:22:38 +0000455 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000456 return getNode(ISD::AND, Op.getValueType(), Op,
457 getConstant(Imm, Op.getValueType()));
458}
459
Chris Lattner061a1ea2005-01-07 07:46:32 +0000460SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
461 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
462 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000463 if (VT != MVT::i64)
464 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukman835702a2005-04-21 22:36:52 +0000465
Chris Lattner061a1ea2005-01-07 07:46:32 +0000466 SDNode *&N = Constants[std::make_pair(Val, VT)];
467 if (N) return SDOperand(N, 0);
Chris Lattner0d2456e2005-08-17 00:34:06 +0000468 N = new ConstantSDNode(false, Val, VT);
469 AllNodes.push_back(N);
470 return SDOperand(N, 0);
471}
472
Chris Lattner435b4022005-11-29 06:21:05 +0000473SDOperand SelectionDAG::getString(const std::string &Val) {
474 StringSDNode *&N = StringNodes[Val];
475 if (!N) {
476 N = new StringSDNode(Val);
477 AllNodes.push_back(N);
478 }
479 return SDOperand(N, 0);
480}
481
Chris Lattner0d2456e2005-08-17 00:34:06 +0000482SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
483 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
484 // Mask out any bits that are not valid for this constant.
485 if (VT != MVT::i64)
486 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
487
488 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
489 if (N) return SDOperand(N, 0);
490 N = new ConstantSDNode(true, Val, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000491 AllNodes.push_back(N);
492 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000493}
494
Chris Lattner061a1ea2005-01-07 07:46:32 +0000495SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
496 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
497 if (VT == MVT::f32)
498 Val = (float)Val; // Mask out extra precision.
499
Chris Lattner381dddc2005-02-17 20:17:32 +0000500 // Do the map lookup using the actual bit pattern for the floating point
501 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
502 // we don't have issues with SNANs.
Jim Laskeyb74c6662005-08-17 19:34:49 +0000503 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000504 if (N) return SDOperand(N, 0);
505 N = new ConstantFPSDNode(Val, VT);
506 AllNodes.push_back(N);
507 return SDOperand(N, 0);
508}
509
Chris Lattner061a1ea2005-01-07 07:46:32 +0000510SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng11d61612005-11-30 02:49:21 +0000511 MVT::ValueType VT, int offset) {
512 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000513 if (N) return SDOperand(N, 0);
Chris Lattner1be7edd2005-08-19 22:31:04 +0000514 N = new GlobalAddressSDNode(false, GV, VT);
515 AllNodes.push_back(N);
516 return SDOperand(N, 0);
517}
518
519SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng0e0de2f2005-11-30 02:04:11 +0000520 MVT::ValueType VT, int offset) {
Evan Cheng11d61612005-11-30 02:49:21 +0000521 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattner1be7edd2005-08-19 22:31:04 +0000522 if (N) return SDOperand(N, 0);
Evan Cheng0e0de2f2005-11-30 02:04:11 +0000523 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000524 AllNodes.push_back(N);
525 return SDOperand(N, 0);
526}
527
528SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
529 SDNode *&N = FrameIndices[FI];
530 if (N) return SDOperand(N, 0);
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +0000531 N = new FrameIndexSDNode(FI, VT, false);
532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
534}
535
536SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
537 SDNode *&N = TargetFrameIndices[FI];
538 if (N) return SDOperand(N, 0);
539 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
542}
543
Chris Lattnerc30405e2005-08-26 17:15:30 +0000544SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
545 SDNode *&N = ConstantPoolIndices[C];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000546 if (N) return SDOperand(N, 0);
Chris Lattnerc30405e2005-08-26 17:15:30 +0000547 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner407c6412005-08-25 05:03:06 +0000548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
550}
551
Chris Lattnerc30405e2005-08-26 17:15:30 +0000552SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
553 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner407c6412005-08-25 05:03:06 +0000554 if (N) return SDOperand(N, 0);
Chris Lattnerc30405e2005-08-26 17:15:30 +0000555 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000556 AllNodes.push_back(N);
557 return SDOperand(N, 0);
558}
559
560SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
561 SDNode *&N = BBNodes[MBB];
562 if (N) return SDOperand(N, 0);
563 N = new BasicBlockSDNode(MBB);
564 AllNodes.push_back(N);
565 return SDOperand(N, 0);
566}
567
Chris Lattner0b6ba902005-07-10 00:07:11 +0000568SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
569 if ((unsigned)VT >= ValueTypeNodes.size())
570 ValueTypeNodes.resize(VT+1);
571 if (ValueTypeNodes[VT] == 0) {
572 ValueTypeNodes[VT] = new VTSDNode(VT);
573 AllNodes.push_back(ValueTypeNodes[VT]);
574 }
575
576 return SDOperand(ValueTypeNodes[VT], 0);
577}
578
Chris Lattner061a1ea2005-01-07 07:46:32 +0000579SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
580 SDNode *&N = ExternalSymbols[Sym];
581 if (N) return SDOperand(N, 0);
Andrew Lenharth4b3932a2005-10-23 03:40:17 +0000582 N = new ExternalSymbolSDNode(false, Sym, VT);
583 AllNodes.push_back(N);
584 return SDOperand(N, 0);
585}
586
587SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
588 SDNode *&N = TargetExternalSymbols[Sym];
589 if (N) return SDOperand(N, 0);
590 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000591 AllNodes.push_back(N);
592 return SDOperand(N, 0);
593}
594
Chris Lattnerd47675e2005-08-09 20:20:18 +0000595SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
596 if ((unsigned)Cond >= CondCodeNodes.size())
597 CondCodeNodes.resize(Cond+1);
598
Chris Lattner14e060f2005-08-09 20:40:02 +0000599 if (CondCodeNodes[Cond] == 0) {
Chris Lattnerd47675e2005-08-09 20:20:18 +0000600 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner14e060f2005-08-09 20:40:02 +0000601 AllNodes.push_back(CondCodeNodes[Cond]);
602 }
Chris Lattnerd47675e2005-08-09 20:20:18 +0000603 return SDOperand(CondCodeNodes[Cond], 0);
604}
605
Chris Lattner5764da422005-08-30 22:38:38 +0000606SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
607 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
608 if (!Reg) {
609 Reg = new RegisterSDNode(RegNo, VT);
610 AllNodes.push_back(Reg);
Chris Lattner33182322005-08-16 21:55:35 +0000611 }
Chris Lattner5764da422005-08-30 22:38:38 +0000612 return SDOperand(Reg, 0);
Chris Lattner33182322005-08-16 21:55:35 +0000613}
614
Chris Lattner679f5b02005-08-09 23:09:05 +0000615SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
616 SDOperand N2, ISD::CondCode Cond) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000617 // These setcc operations always fold.
618 switch (Cond) {
619 default: break;
620 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000621 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000622 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000623 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000624 }
625
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000626 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
627 uint64_t C2 = N2C->getValue();
628 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
629 uint64_t C1 = N1C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000630
Chris Lattner061a1ea2005-01-07 07:46:32 +0000631 // Sign extend the operands if required
632 if (ISD::isSignedIntSetCC(Cond)) {
633 C1 = N1C->getSignExtended();
634 C2 = N2C->getSignExtended();
635 }
636
637 switch (Cond) {
638 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000639 case ISD::SETEQ: return getConstant(C1 == C2, VT);
640 case ISD::SETNE: return getConstant(C1 != C2, VT);
641 case ISD::SETULT: return getConstant(C1 < C2, VT);
642 case ISD::SETUGT: return getConstant(C1 > C2, VT);
643 case ISD::SETULE: return getConstant(C1 <= C2, VT);
644 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
645 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
646 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
647 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
648 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000649 }
Chris Lattnerdfed7352005-04-07 18:58:54 +0000650 } else {
Chris Lattnerd7ee4d82005-08-24 22:44:39 +0000651 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner6d40fd02005-04-18 04:30:45 +0000652 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
653 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
654
655 // If the comparison constant has bits in the upper part, the
656 // zero-extended value could never match.
657 if (C2 & (~0ULL << InSize)) {
658 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
659 switch (Cond) {
660 case ISD::SETUGT:
661 case ISD::SETUGE:
662 case ISD::SETEQ: return getConstant(0, VT);
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETNE: return getConstant(1, VT);
666 case ISD::SETGT:
667 case ISD::SETGE:
668 // True if the sign bit of C2 is set.
669 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
670 case ISD::SETLT:
671 case ISD::SETLE:
672 // True if the sign bit of C2 isn't set.
673 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
674 default:
675 break;
676 }
677 }
678
679 // Otherwise, we can perform the comparison with the low bits.
680 switch (Cond) {
681 case ISD::SETEQ:
682 case ISD::SETNE:
683 case ISD::SETUGT:
684 case ISD::SETUGE:
685 case ISD::SETULT:
686 case ISD::SETULE:
Chris Lattnerd47675e2005-08-09 20:20:18 +0000687 return getSetCC(VT, N1.getOperand(0),
688 getConstant(C2, N1.getOperand(0).getValueType()),
689 Cond);
Chris Lattner6d40fd02005-04-18 04:30:45 +0000690 default:
691 break; // todo, be more careful with signed comparisons
692 }
Chris Lattnerd7ee4d82005-08-24 22:44:39 +0000693 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
694 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
695 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
696 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
697 MVT::ValueType ExtDstTy = N1.getValueType();
698 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman43144a22005-08-30 02:44:00 +0000699
Chris Lattnerd7ee4d82005-08-24 22:44:39 +0000700 // If the extended part has any inconsistent bits, it cannot ever
701 // compare equal. In other words, they have to be all ones or all
702 // zeros.
703 uint64_t ExtBits =
Jeff Cohend8c84e32005-08-31 02:47:06 +0000704 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattnerd7ee4d82005-08-24 22:44:39 +0000705 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
706 return getConstant(Cond == ISD::SETNE, VT);
707
708 // Otherwise, make this a use of a zext.
709 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohend8c84e32005-08-31 02:47:06 +0000710 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattnerd7ee4d82005-08-24 22:44:39 +0000711 Cond);
Chris Lattner6d40fd02005-04-18 04:30:45 +0000712 }
713
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000714 uint64_t MinVal, MaxVal;
715 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
716 if (ISD::isSignedIntSetCC(Cond)) {
717 MinVal = 1ULL << (OperandBitSize-1);
718 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
719 MaxVal = ~0ULL >> (65-OperandBitSize);
720 else
721 MaxVal = 0;
722 } else {
723 MinVal = 0;
724 MaxVal = ~0ULL >> (64-OperandBitSize);
725 }
726
727 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
728 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
729 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
730 --C2; // X >= C1 --> X > (C1-1)
Chris Lattner679f5b02005-08-09 23:09:05 +0000731 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
732 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000733 }
734
735 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
736 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
737 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattner679f5b02005-08-09 23:09:05 +0000738 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
739 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000740 }
Misha Brukman835702a2005-04-21 22:36:52 +0000741
Nate Begeman80c095f2005-04-14 08:56:52 +0000742 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
743 return getConstant(0, VT); // X < MIN --> false
Misha Brukman835702a2005-04-21 22:36:52 +0000744
Nate Begeman80c095f2005-04-14 08:56:52 +0000745 // Canonicalize setgt X, Min --> setne X, Min
746 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000747 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukman835702a2005-04-21 22:36:52 +0000748
Chris Lattner87bd6982005-04-12 00:28:49 +0000749 // If we have setult X, 1, turn it into seteq X, 0
750 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000751 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
752 ISD::SETEQ);
Nate Begeman80c095f2005-04-14 08:56:52 +0000753 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner87bd6982005-04-12 00:28:49 +0000754 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000755 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
756 ISD::SETEQ);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000757
758 // If we have "setcc X, C1", check to see if we can shrink the immediate
759 // by changing cc.
760
761 // SETUGT X, SINTMAX -> SETLT X, 0
762 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
763 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000764 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000765
766 // FIXME: Implement the rest of these.
767
Chris Lattnerab1ed772005-04-21 06:12:41 +0000768
769 // Fold bit comparisons when we can.
770 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
771 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
772 if (ConstantSDNode *AndRHS =
773 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
774 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
775 // Perform the xform if the AND RHS is a single bit.
776 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
777 return getNode(ISD::SRL, VT, N1,
Chris Lattner6667bdb2005-08-02 19:26:06 +0000778 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattnerab1ed772005-04-21 06:12:41 +0000779 TLI.getShiftAmountTy()));
780 }
781 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
782 // (X & 8) == 8 --> (X & 8) >> 3
783 // Perform the xform if C2 is a single bit.
784 if ((C2 & (C2-1)) == 0) {
785 return getNode(ISD::SRL, VT, N1,
Chris Lattner6667bdb2005-08-02 19:26:06 +0000786 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattnerab1ed772005-04-21 06:12:41 +0000787 }
788 }
789 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000790 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000791 } else if (isa<ConstantSDNode>(N1.Val)) {
792 // Ensure that the constant occurs on the RHS.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000793 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000794 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000795
796 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
797 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
798 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000799
Chris Lattner061a1ea2005-01-07 07:46:32 +0000800 switch (Cond) {
801 default: break; // FIXME: Implement the rest of these!
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000802 case ISD::SETEQ: return getConstant(C1 == C2, VT);
803 case ISD::SETNE: return getConstant(C1 != C2, VT);
804 case ISD::SETLT: return getConstant(C1 < C2, VT);
805 case ISD::SETGT: return getConstant(C1 > C2, VT);
806 case ISD::SETLE: return getConstant(C1 <= C2, VT);
807 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000808 }
809 } else {
810 // Ensure that the constant occurs on the RHS.
Chris Lattner679f5b02005-08-09 23:09:05 +0000811 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000812 }
813
Chris Lattnerd47675e2005-08-09 20:20:18 +0000814 // Could not fold it.
815 return SDOperand();
Chris Lattner061a1ea2005-01-07 07:46:32 +0000816}
817
Chris Lattner061a1ea2005-01-07 07:46:32 +0000818/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000819///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000820SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattnerb95b2802005-09-02 19:36:17 +0000821 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
822 if (!N) {
823 N = new SDNode(Opcode, VT);
824 AllNodes.push_back(N);
825 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000826 return SDOperand(N, 0);
827}
828
Chris Lattner061a1ea2005-01-07 07:46:32 +0000829SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
830 SDOperand Operand) {
831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
832 uint64_t Val = C->getValue();
833 switch (Opcode) {
834 default: break;
835 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner8c393c22005-09-02 00:17:32 +0000836 case ISD::ANY_EXTEND:
Chris Lattner061a1ea2005-01-07 07:46:32 +0000837 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
838 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000839 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
840 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000841 }
842 }
843
844 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
845 switch (Opcode) {
Chris Lattner0ea81f92005-04-09 03:02:46 +0000846 case ISD::FNEG:
847 return getConstantFP(-C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000848 case ISD::FP_ROUND:
849 case ISD::FP_EXTEND:
850 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000851 case ISD::FP_TO_SINT:
852 return getConstant((int64_t)C->getValue(), VT);
853 case ISD::FP_TO_UINT:
854 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000855 }
856
857 unsigned OpOpcode = Operand.Val->getOpcode();
858 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000859 case ISD::TokenFactor:
860 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000861 case ISD::SIGN_EXTEND:
862 if (Operand.getValueType() == VT) return Operand; // noop extension
863 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
864 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
865 break;
866 case ISD::ZERO_EXTEND:
867 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattnerb32d9312005-04-07 19:43:53 +0000868 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000869 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000870 break;
Chris Lattner8c393c22005-09-02 00:17:32 +0000871 case ISD::ANY_EXTEND:
872 if (Operand.getValueType() == VT) return Operand; // noop extension
873 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
874 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
875 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
876 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000877 case ISD::TRUNCATE:
878 if (Operand.getValueType() == VT) return Operand; // noop truncate
879 if (OpOpcode == ISD::TRUNCATE)
880 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner8c393c22005-09-02 00:17:32 +0000881 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
882 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattner4d5ba992005-01-07 21:56:24 +0000883 // If the source is smaller than the dest, we still need an extend.
884 if (Operand.Val->getOperand(0).getValueType() < VT)
885 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
886 else if (Operand.Val->getOperand(0).getValueType() > VT)
887 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
888 else
889 return Operand.Val->getOperand(0);
890 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000891 break;
Chris Lattner36e663d2005-12-23 00:16:34 +0000892 case ISD::BIT_CONVERT:
893 // Basic sanity checking.
894 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
895 "Cannot BIT_CONVERT between two different types!");
896 if (VT == Operand.getValueType()) return Operand; // noop conversion.
897 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +0000898 case ISD::FNEG:
Chris Lattner6f3b5772005-09-28 22:28:18 +0000899 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
900 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner0ea81f92005-04-09 03:02:46 +0000901 Operand.Val->getOperand(0));
902 if (OpOpcode == ISD::FNEG) // --X -> X
903 return Operand.Val->getOperand(0);
904 break;
905 case ISD::FABS:
906 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
907 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
908 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000909 }
910
Chris Lattnerf9c19152005-08-25 19:12:10 +0000911 SDNode *N;
912 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
913 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattner1cb550c2005-08-26 00:13:12 +0000914 if (E) return SDOperand(E, 0);
Chris Lattnerf9c19152005-08-25 19:12:10 +0000915 E = N = new SDNode(Opcode, Operand);
916 } else {
917 N = new SDNode(Opcode, Operand);
918 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000919 N->setValueTypes(VT);
920 AllNodes.push_back(N);
921 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000922}
923
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000924
925
Chris Lattner061a1ea2005-01-07 07:46:32 +0000926SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
927 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000928#ifndef NDEBUG
929 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000930 case ISD::TokenFactor:
931 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
932 N2.getValueType() == MVT::Other && "Invalid token factor!");
933 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000934 case ISD::AND:
935 case ISD::OR:
936 case ISD::XOR:
937 case ISD::UDIV:
938 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +0000939 case ISD::MULHU:
940 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +0000941 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
942 // fall through
943 case ISD::ADD:
944 case ISD::SUB:
945 case ISD::MUL:
946 case ISD::SDIV:
947 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +0000948 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
949 // fall through.
950 case ISD::FADD:
951 case ISD::FSUB:
952 case ISD::FMUL:
953 case ISD::FDIV:
954 case ISD::FREM:
Chris Lattner4e550eb2005-01-16 02:23:22 +0000955 assert(N1.getValueType() == N2.getValueType() &&
956 N1.getValueType() == VT && "Binary operator types must match!");
957 break;
958
959 case ISD::SHL:
960 case ISD::SRA:
961 case ISD::SRL:
962 assert(VT == N1.getValueType() &&
963 "Shift operators return type must be the same as their first arg");
964 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000965 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000966 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000967 case ISD::FP_ROUND_INREG: {
968 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
969 assert(VT == N1.getValueType() && "Not an inreg round!");
970 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
971 "Cannot FP_ROUND_INREG integer types");
972 assert(EVT <= VT && "Not rounding down!");
973 break;
974 }
Nate Begeman43144a22005-08-30 02:44:00 +0000975 case ISD::AssertSext:
976 case ISD::AssertZext:
Chris Lattner0b6ba902005-07-10 00:07:11 +0000977 case ISD::SIGN_EXTEND_INREG: {
978 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
979 assert(VT == N1.getValueType() && "Not an inreg extend!");
980 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
981 "Cannot *_EXTEND_INREG FP types");
982 assert(EVT <= VT && "Not extending!");
983 }
984
Chris Lattner4e550eb2005-01-16 02:23:22 +0000985 default: break;
986 }
987#endif
988
Chris Lattner061a1ea2005-01-07 07:46:32 +0000989 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
990 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
991 if (N1C) {
992 if (N2C) {
993 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
994 switch (Opcode) {
995 case ISD::ADD: return getConstant(C1 + C2, VT);
996 case ISD::SUB: return getConstant(C1 - C2, VT);
997 case ISD::MUL: return getConstant(C1 * C2, VT);
998 case ISD::UDIV:
999 if (C2) return getConstant(C1 / C2, VT);
1000 break;
1001 case ISD::UREM :
1002 if (C2) return getConstant(C1 % C2, VT);
1003 break;
1004 case ISD::SDIV :
1005 if (C2) return getConstant(N1C->getSignExtended() /
1006 N2C->getSignExtended(), VT);
1007 break;
1008 case ISD::SREM :
1009 if (C2) return getConstant(N1C->getSignExtended() %
1010 N2C->getSignExtended(), VT);
1011 break;
1012 case ISD::AND : return getConstant(C1 & C2, VT);
1013 case ISD::OR : return getConstant(C1 | C2, VT);
1014 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemane07bc282005-08-31 00:27:53 +00001015 case ISD::SHL : return getConstant(C1 << C2, VT);
1016 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +00001017 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001018 default: break;
1019 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001020 } else { // Cannonicalize constant to RHS if commutative
1021 if (isCommutativeBinOp(Opcode)) {
1022 std::swap(N1C, N2C);
1023 std::swap(N1, N2);
1024 }
1025 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001026 }
1027
1028 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1029 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001030 if (N1CFP) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001031 if (N2CFP) {
1032 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1033 switch (Opcode) {
Chris Lattner6f3b5772005-09-28 22:28:18 +00001034 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1035 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1036 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1037 case ISD::FDIV:
Chris Lattner061a1ea2005-01-07 07:46:32 +00001038 if (C2) return getConstantFP(C1 / C2, VT);
1039 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00001040 case ISD::FREM :
Chris Lattner061a1ea2005-01-07 07:46:32 +00001041 if (C2) return getConstantFP(fmod(C1, C2), VT);
1042 break;
1043 default: break;
1044 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001045 } else { // Cannonicalize constant to RHS if commutative
1046 if (isCommutativeBinOp(Opcode)) {
1047 std::swap(N1CFP, N2CFP);
1048 std::swap(N1, N2);
1049 }
1050 }
Chris Lattner0b6ba902005-07-10 00:07:11 +00001051 }
1052
Chris Lattner061a1ea2005-01-07 07:46:32 +00001053 // Finally, fold operations that do not require constants.
1054 switch (Opcode) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00001055 case ISD::FP_ROUND_INREG:
1056 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1057 break;
1058 case ISD::SIGN_EXTEND_INREG: {
1059 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1060 if (EVT == VT) return N1; // Not actually extending
Chris Lattner0b6ba902005-07-10 00:07:11 +00001061 break;
1062 }
1063
Nate Begeman4ddd8162005-04-13 21:23:31 +00001064 // FIXME: figure out how to safely handle things like
1065 // int foo(int x) { return 1 << (x & 255); }
1066 // int bar() { return foo(256); }
1067#if 0
Nate Begemanca916ba2005-04-12 23:32:28 +00001068 case ISD::SHL:
1069 case ISD::SRL:
1070 case ISD::SRA:
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001071 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00001072 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemanca916ba2005-04-12 23:32:28 +00001073 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001074 else if (N2.getOpcode() == ISD::AND)
1075 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1076 // If the and is only masking out bits that cannot effect the shift,
1077 // eliminate the and.
1078 unsigned NumBits = MVT::getSizeInBits(VT);
1079 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1080 return getNode(Opcode, VT, N1, N2.getOperand(0));
1081 }
Nate Begemanca916ba2005-04-12 23:32:28 +00001082 break;
Nate Begeman4ddd8162005-04-13 21:23:31 +00001083#endif
Chris Lattner061a1ea2005-01-07 07:46:32 +00001084 }
1085
Chris Lattner724f7ee2005-05-11 18:57:39 +00001086 // Memoize this node if possible.
1087 SDNode *N;
Chris Lattnerf9c19152005-08-25 19:12:10 +00001088 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1089 VT != MVT::Flag) {
Chris Lattner724f7ee2005-05-11 18:57:39 +00001090 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1091 if (BON) return SDOperand(BON, 0);
1092
1093 BON = N = new SDNode(Opcode, N1, N2);
1094 } else {
Chris Lattner8005e912005-05-12 00:17:04 +00001095 N = new SDNode(Opcode, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00001096 }
1097
Chris Lattner86535992005-05-14 07:45:46 +00001098 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001099 AllNodes.push_back(N);
1100 return SDOperand(N, 0);
1101}
1102
Chris Lattner061a1ea2005-01-07 07:46:32 +00001103SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1104 SDOperand N1, SDOperand N2, SDOperand N3) {
1105 // Perform various simplifications.
1106 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1107 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1108 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1109 switch (Opcode) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001110 case ISD::SETCC: {
1111 // Use SimplifySetCC to simplify SETCC's.
Chris Lattner679f5b02005-08-09 23:09:05 +00001112 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattnerd47675e2005-08-09 20:20:18 +00001113 if (Simp.Val) return Simp;
1114 break;
1115 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001116 case ISD::SELECT:
1117 if (N1C)
1118 if (N1C->getValue())
1119 return N2; // select true, X, Y -> X
Misha Brukman835702a2005-04-21 22:36:52 +00001120 else
Chris Lattner061a1ea2005-01-07 07:46:32 +00001121 return N3; // select false, X, Y -> Y
1122
1123 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattner061a1ea2005-01-07 07:46:32 +00001124 break;
Chris Lattner5c66e452005-01-07 22:49:57 +00001125 case ISD::BRCOND:
1126 if (N2C)
1127 if (N2C->getValue()) // Unconditional branch
1128 return getNode(ISD::BR, MVT::Other, N1, N3);
1129 else
1130 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +00001131 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001132 }
1133
Chris Lattner566307f2005-05-14 07:42:29 +00001134 std::vector<SDOperand> Ops;
1135 Ops.reserve(3);
1136 Ops.push_back(N1);
1137 Ops.push_back(N2);
1138 Ops.push_back(N3);
1139
Chris Lattnerf9c19152005-08-25 19:12:10 +00001140 // Memoize node if it doesn't produce a flag.
1141 SDNode *N;
1142 if (VT != MVT::Flag) {
1143 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1144 if (E) return SDOperand(E, 0);
1145 E = N = new SDNode(Opcode, N1, N2, N3);
1146 } else {
1147 N = new SDNode(Opcode, N1, N2, N3);
1148 }
Chris Lattner96c262e2005-05-14 07:29:57 +00001149 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001150 AllNodes.push_back(N);
1151 return SDOperand(N, 0);
1152}
1153
1154SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001155 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001156 SDOperand N4) {
Chris Lattner833a4fb2005-05-14 07:32:14 +00001157 std::vector<SDOperand> Ops;
1158 Ops.reserve(4);
1159 Ops.push_back(N1);
1160 Ops.push_back(N2);
1161 Ops.push_back(N3);
1162 Ops.push_back(N4);
1163 return getNode(Opcode, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001164}
1165
Chris Lattner36db1ed2005-07-10 00:29:18 +00001166SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1167 SDOperand N1, SDOperand N2, SDOperand N3,
1168 SDOperand N4, SDOperand N5) {
1169 std::vector<SDOperand> Ops;
1170 Ops.reserve(5);
1171 Ops.push_back(N1);
1172 Ops.push_back(N2);
1173 Ops.push_back(N3);
1174 Ops.push_back(N4);
1175 Ops.push_back(N5);
1176 return getNode(Opcode, VT, Ops);
1177}
1178
Evan Chengdadc1052005-12-10 00:37:58 +00001179SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1180 SDOperand N1, SDOperand N2, SDOperand N3,
1181 SDOperand N4, SDOperand N5, SDOperand N6) {
1182 std::vector<SDOperand> Ops;
1183 Ops.reserve(6);
1184 Ops.push_back(N1);
1185 Ops.push_back(N2);
1186 Ops.push_back(N3);
1187 Ops.push_back(N4);
1188 Ops.push_back(N5);
1189 Ops.push_back(N6);
1190 return getNode(Opcode, VT, Ops);
1191}
1192
1193// setAdjCallChain - This method changes the token chain of an
1194// CALLSEQ_START/END node to be the specified operand.
1195void SDNode::setAdjCallChain(SDOperand N) {
1196 assert(N.getValueType() == MVT::Other);
1197 assert((getOpcode() == ISD::CALLSEQ_START ||
1198 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1199
1200 OperandList[0].Val->removeUser(this);
1201 OperandList[0] = N;
1202 OperandList[0].Val->Uses.push_back(this);
1203}
1204
1205
1206
1207SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1208 SDOperand Chain, SDOperand Ptr,
1209 SDOperand SV) {
1210 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1211 if (N) return SDOperand(N, 0);
1212 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1213
1214 // Loads have a token chain.
1215 setNodeValueTypes(N, VT, MVT::Other);
1216 AllNodes.push_back(N);
1217 return SDOperand(N, 0);
1218}
1219
1220SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1221 SDOperand Chain, SDOperand Ptr,
1222 SDOperand SV) {
1223 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1224 if (N) return SDOperand(N, 0);
1225 std::vector<SDOperand> Ops;
1226 Ops.reserve(5);
1227 Ops.push_back(Chain);
1228 Ops.push_back(Ptr);
1229 Ops.push_back(getConstant(Count, MVT::i32));
1230 Ops.push_back(getValueType(EVT));
1231 Ops.push_back(SV);
1232 std::vector<MVT::ValueType> VTs;
1233 VTs.reserve(2);
1234 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1235 return getNode(ISD::VLOAD, VTs, Ops);
1236}
1237
1238SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1239 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1240 MVT::ValueType EVT) {
1241 std::vector<SDOperand> Ops;
1242 Ops.reserve(4);
1243 Ops.push_back(Chain);
1244 Ops.push_back(Ptr);
1245 Ops.push_back(SV);
1246 Ops.push_back(getValueType(EVT));
1247 std::vector<MVT::ValueType> VTs;
1248 VTs.reserve(2);
1249 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1250 return getNode(Opcode, VTs, Ops);
1251}
Chris Lattner36db1ed2005-07-10 00:29:18 +00001252
Chris Lattnerc14f3542005-05-09 04:14:13 +00001253SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth2edc1882005-06-29 18:54:02 +00001254 assert((!V || isa<PointerType>(V->getType())) &&
1255 "SrcValue is not a pointer?");
Chris Lattnerc14f3542005-05-09 04:14:13 +00001256 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1257 if (N) return SDOperand(N, 0);
1258
1259 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001260 AllNodes.push_back(N);
1261 return SDOperand(N, 0);
1262}
1263
1264SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerd5531332005-05-14 06:20:26 +00001265 std::vector<SDOperand> &Ops) {
1266 switch (Ops.size()) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001267 case 0: return getNode(Opcode, VT);
Chris Lattnerd5531332005-05-14 06:20:26 +00001268 case 1: return getNode(Opcode, VT, Ops[0]);
1269 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1270 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001271 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001272 }
Chris Lattnerbf4f23322005-11-09 23:47:37 +00001273
Chris Lattnerd5531332005-05-14 06:20:26 +00001274 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001275 switch (Opcode) {
1276 default: break;
1277 case ISD::BRCONDTWOWAY:
1278 if (N1C)
1279 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001280 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001281 else // Unconditional branch to false dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001282 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001283 break;
Nate Begeman371e4952005-08-16 19:49:35 +00001284 case ISD::BRTWOWAY_CC:
1285 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1286 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1287 "LHS and RHS of comparison must have same type!");
1288 break;
Chris Lattner36db1ed2005-07-10 00:29:18 +00001289 case ISD::TRUNCSTORE: {
1290 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1291 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1292#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1293 // If this is a truncating store of a constant, convert to the desired type
1294 // and store it instead.
1295 if (isa<Constant>(Ops[0])) {
1296 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1297 if (isa<Constant>(Op))
1298 N1 = Op;
1299 }
1300 // Also for ConstantFP?
1301#endif
1302 if (Ops[0].getValueType() == EVT) // Normal store?
1303 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1304 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1305 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1306 "Can't do FP-INT conversion!");
1307 break;
1308 }
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001309 case ISD::SELECT_CC: {
Chris Lattnerb11d1562005-10-05 06:37:22 +00001310 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001311 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1312 "LHS and RHS of condition must have same type!");
1313 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1314 "True and False arms of SelectCC must have same type!");
1315 assert(Ops[2].getValueType() == VT &&
1316 "select_cc node must be of same type as true and false value!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001317 break;
1318 }
1319 case ISD::BR_CC: {
Chris Lattnerb11d1562005-10-05 06:37:22 +00001320 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001321 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1322 "LHS/RHS of comparison should match types!");
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001323 break;
1324 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001325 }
1326
Chris Lattner566307f2005-05-14 07:42:29 +00001327 // Memoize nodes.
Chris Lattnerf9c19152005-08-25 19:12:10 +00001328 SDNode *N;
1329 if (VT != MVT::Flag) {
1330 SDNode *&E =
1331 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1332 if (E) return SDOperand(E, 0);
1333 E = N = new SDNode(Opcode, Ops);
1334 } else {
1335 N = new SDNode(Opcode, Ops);
1336 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001337 N->setValueTypes(VT);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001338 AllNodes.push_back(N);
1339 return SDOperand(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001340}
1341
Chris Lattnerd5531332005-05-14 06:20:26 +00001342SDOperand SelectionDAG::getNode(unsigned Opcode,
1343 std::vector<MVT::ValueType> &ResultTys,
1344 std::vector<SDOperand> &Ops) {
1345 if (ResultTys.size() == 1)
1346 return getNode(Opcode, ResultTys[0], Ops);
1347
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001348 switch (Opcode) {
1349 case ISD::EXTLOAD:
1350 case ISD::SEXTLOAD:
1351 case ISD::ZEXTLOAD: {
1352 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1353 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1354 // If they are asking for an extending load from/to the same thing, return a
1355 // normal load.
1356 if (ResultTys[0] == EVT)
1357 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1358 assert(EVT < ResultTys[0] &&
1359 "Should only be an extending load, not truncating!");
1360 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1361 "Cannot sign/zero extend a FP load!");
1362 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1363 "Cannot convert from FP to Int or Int -> FP!");
1364 break;
1365 }
1366
Chris Lattner669e8c22005-05-14 07:25:05 +00001367 // FIXME: figure out how to safely handle things like
1368 // int foo(int x) { return 1 << (x & 255); }
1369 // int bar() { return foo(256); }
1370#if 0
Chris Lattner669e8c22005-05-14 07:25:05 +00001371 case ISD::SRA_PARTS:
1372 case ISD::SRL_PARTS:
1373 case ISD::SHL_PARTS:
1374 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00001375 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattner669e8c22005-05-14 07:25:05 +00001376 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1377 else if (N3.getOpcode() == ISD::AND)
1378 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1379 // If the and is only masking out bits that cannot effect the shift,
1380 // eliminate the and.
1381 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1382 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1383 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1384 }
1385 break;
Chris Lattner669e8c22005-05-14 07:25:05 +00001386#endif
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001387 }
Chris Lattnerd5531332005-05-14 06:20:26 +00001388
Chris Lattnerf9c19152005-08-25 19:12:10 +00001389 // Memoize the node unless it returns a flag.
1390 SDNode *N;
1391 if (ResultTys.back() != MVT::Flag) {
1392 SDNode *&E =
1393 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1394 if (E) return SDOperand(E, 0);
1395 E = N = new SDNode(Opcode, Ops);
1396 } else {
1397 N = new SDNode(Opcode, Ops);
1398 }
Chris Lattner88fa11c2005-11-08 23:30:28 +00001399 setNodeValueTypes(N, ResultTys);
Chris Lattner006f56b2005-05-14 06:42:57 +00001400 AllNodes.push_back(N);
Chris Lattnerd5531332005-05-14 06:20:26 +00001401 return SDOperand(N, 0);
1402}
1403
Chris Lattner88fa11c2005-11-08 23:30:28 +00001404void SelectionDAG::setNodeValueTypes(SDNode *N,
1405 std::vector<MVT::ValueType> &RetVals) {
1406 switch (RetVals.size()) {
1407 case 0: return;
1408 case 1: N->setValueTypes(RetVals[0]); return;
1409 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1410 default: break;
1411 }
1412
1413 std::list<std::vector<MVT::ValueType> >::iterator I =
1414 std::find(VTList.begin(), VTList.end(), RetVals);
1415 if (I == VTList.end()) {
1416 VTList.push_front(RetVals);
1417 I = VTList.begin();
1418 }
1419
1420 N->setValueTypes(&(*I)[0], I->size());
1421}
1422
1423void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1424 MVT::ValueType VT2) {
1425 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1426 E = VTList.end(); I != E; ++I) {
1427 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1428 N->setValueTypes(&(*I)[0], 2);
1429 return;
1430 }
1431 }
1432 std::vector<MVT::ValueType> V;
1433 V.push_back(VT1);
1434 V.push_back(VT2);
1435 VTList.push_front(V);
1436 N->setValueTypes(&(*VTList.begin())[0], 2);
1437}
1438
Chris Lattner19732782005-08-16 18:17:10 +00001439
1440/// SelectNodeTo - These are used for target selectors to *mutate* the
1441/// specified node to have the specified return type, Target opcode, and
1442/// operands. Note that target opcodes are stored as
1443/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner9d0d7152005-12-01 18:00:57 +00001444///
1445/// Note that SelectNodeTo returns the resultant node. If there is already a
1446/// node of the specified opcode and operands, it returns that node instead of
1447/// the current one.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001448SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1449 MVT::ValueType VT) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001450 // If an identical node already exists, use it.
1451 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1452 if (ON) return SDOperand(ON, 0);
1453
Chris Lattner45e1ce42005-08-24 23:00:29 +00001454 RemoveNodeFromCSEMaps(N);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001455
Chris Lattner45e1ce42005-08-24 23:00:29 +00001456 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1457 N->setValueTypes(VT);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001458
1459 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001460 return SDOperand(N, 0);
Chris Lattner45e1ce42005-08-24 23:00:29 +00001461}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001462
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001463SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1464 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001465 // If an identical node already exists, use it.
1466 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1467 std::make_pair(Op1, VT))];
1468 if (ON) return SDOperand(ON, 0);
1469
Chris Lattner19732782005-08-16 18:17:10 +00001470 RemoveNodeFromCSEMaps(N);
1471 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1472 N->setValueTypes(VT);
1473 N->setOperands(Op1);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001474
1475 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001476 return SDOperand(N, 0);
Chris Lattner19732782005-08-16 18:17:10 +00001477}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001478
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001479SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1480 MVT::ValueType VT, SDOperand Op1,
1481 SDOperand Op2) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001482 // If an identical node already exists, use it.
1483 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1484 std::make_pair(Op1, Op2))];
1485 if (ON) return SDOperand(ON, 0);
1486
Chris Lattner19732782005-08-16 18:17:10 +00001487 RemoveNodeFromCSEMaps(N);
1488 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1489 N->setValueTypes(VT);
1490 N->setOperands(Op1, Op2);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001491
1492 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001493 return SDOperand(N, 0);
Chris Lattner19732782005-08-16 18:17:10 +00001494}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001495
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001496SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1497 MVT::ValueType VT, SDOperand Op1,
1498 SDOperand Op2, SDOperand Op3) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001499 // If an identical node already exists, use it.
1500 std::vector<SDOperand> OpList;
1501 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1502 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1503 std::make_pair(VT, OpList))];
1504 if (ON) return SDOperand(ON, 0);
1505
Chris Lattner19732782005-08-16 18:17:10 +00001506 RemoveNodeFromCSEMaps(N);
1507 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1508 N->setValueTypes(VT);
1509 N->setOperands(Op1, Op2, Op3);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001510
1511 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001512 return SDOperand(N, 0);
Chris Lattner19732782005-08-16 18:17:10 +00001513}
Chris Lattner466fece2005-08-21 22:30:30 +00001514
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001515SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1516 MVT::ValueType VT, SDOperand Op1,
1517 SDOperand Op2, SDOperand Op3,
1518 SDOperand Op4) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001519 // If an identical node already exists, use it.
1520 std::vector<SDOperand> OpList;
1521 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1522 OpList.push_back(Op4);
1523 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1524 std::make_pair(VT, OpList))];
1525 if (ON) return SDOperand(ON, 0);
1526
Nate Begeman19a271a2005-08-18 07:30:15 +00001527 RemoveNodeFromCSEMaps(N);
1528 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1529 N->setValueTypes(VT);
1530 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001531
1532 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001533 return SDOperand(N, 0);
Nate Begeman19a271a2005-08-18 07:30:15 +00001534}
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001535
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001536SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1537 MVT::ValueType VT, SDOperand Op1,
1538 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1539 SDOperand Op5) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001540 // If an identical node already exists, use it.
1541 std::vector<SDOperand> OpList;
1542 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1543 OpList.push_back(Op4); OpList.push_back(Op5);
1544 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1545 std::make_pair(VT, OpList))];
1546 if (ON) return SDOperand(ON, 0);
1547
Chris Lattner707b39f2005-08-21 18:49:33 +00001548 RemoveNodeFromCSEMaps(N);
1549 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1550 N->setValueTypes(VT);
1551 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001552
1553 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001554 return SDOperand(N, 0);
Chris Lattner707b39f2005-08-21 18:49:33 +00001555}
Chris Lattner19732782005-08-16 18:17:10 +00001556
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001557SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1558 MVT::ValueType VT, SDOperand Op1,
1559 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1560 SDOperand Op5, SDOperand Op6) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001561 // If an identical node already exists, use it.
1562 std::vector<SDOperand> OpList;
1563 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1564 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1565 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1566 std::make_pair(VT, OpList))];
1567 if (ON) return SDOperand(ON, 0);
1568
Evan Cheng0e0de2f2005-11-30 02:04:11 +00001569 RemoveNodeFromCSEMaps(N);
1570 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1571 N->setValueTypes(VT);
1572 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001573
1574 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001575 return SDOperand(N, 0);
Evan Cheng0e0de2f2005-11-30 02:04:11 +00001576}
1577
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001578SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1579 MVT::ValueType VT1, MVT::ValueType VT2,
1580 SDOperand Op1, SDOperand Op2) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001581 // If an identical node already exists, use it.
1582 std::vector<SDOperand> OpList;
1583 OpList.push_back(Op1); OpList.push_back(Op2);
1584 std::vector<MVT::ValueType> VTList;
1585 VTList.push_back(VT1); VTList.push_back(VT2);
1586 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1587 std::make_pair(VTList, OpList))];
1588 if (ON) return SDOperand(ON, 0);
1589
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001590 RemoveNodeFromCSEMaps(N);
1591 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1592 setNodeValueTypes(N, VT1, VT2);
1593 N->setOperands(Op1, Op2);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001594
1595 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001596 return SDOperand(N, 0);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001597}
1598
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001599SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1600 MVT::ValueType VT1, MVT::ValueType VT2,
1601 SDOperand Op1, SDOperand Op2,
1602 SDOperand Op3) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001603 // If an identical node already exists, use it.
1604 std::vector<SDOperand> OpList;
1605 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1606 std::vector<MVT::ValueType> VTList;
1607 VTList.push_back(VT1); VTList.push_back(VT2);
1608 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1609 std::make_pair(VTList, OpList))];
1610 if (ON) return SDOperand(ON, 0);
1611
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001612 RemoveNodeFromCSEMaps(N);
1613 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1614 setNodeValueTypes(N, VT1, VT2);
1615 N->setOperands(Op1, Op2, Op3);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001616
1617 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001618 return SDOperand(N, 0);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001619}
1620
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001621SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1622 MVT::ValueType VT1, MVT::ValueType VT2,
1623 SDOperand Op1, SDOperand Op2,
1624 SDOperand Op3, SDOperand Op4) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001625 // If an identical node already exists, use it.
1626 std::vector<SDOperand> OpList;
1627 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1628 OpList.push_back(Op4);
1629 std::vector<MVT::ValueType> VTList;
1630 VTList.push_back(VT1); VTList.push_back(VT2);
1631 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1632 std::make_pair(VTList, OpList))];
1633 if (ON) return SDOperand(ON, 0);
1634
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001635 RemoveNodeFromCSEMaps(N);
1636 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1637 setNodeValueTypes(N, VT1, VT2);
1638 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001639
1640 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001641 return SDOperand(N, 0);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001642}
1643
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001644SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1645 MVT::ValueType VT1, MVT::ValueType VT2,
1646 SDOperand Op1, SDOperand Op2,
1647 SDOperand Op3, SDOperand Op4,
1648 SDOperand Op5) {
Chris Lattner9d0d7152005-12-01 18:00:57 +00001649 // If an identical node already exists, use it.
1650 std::vector<SDOperand> OpList;
1651 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1652 OpList.push_back(Op4); OpList.push_back(Op5);
1653 std::vector<MVT::ValueType> VTList;
1654 VTList.push_back(VT1); VTList.push_back(VT2);
1655 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1656 std::make_pair(VTList, OpList))];
1657 if (ON) return SDOperand(ON, 0);
1658
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001659 RemoveNodeFromCSEMaps(N);
1660 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1661 setNodeValueTypes(N, VT1, VT2);
1662 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattner9d0d7152005-12-01 18:00:57 +00001663
1664 ON = N; // Memoize the new node.
Chris Lattnerbe5dd5d2005-11-30 22:45:14 +00001665 return SDOperand(N, 0);
Chris Lattnerf090f7e2005-11-19 01:44:53 +00001666}
1667
1668// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattnerab0de9d2005-08-17 19:00:20 +00001669/// This can cause recursive merging of nodes in the DAG.
1670///
Chris Lattner373f0482005-08-26 18:36:28 +00001671/// This version assumes From/To have a single result value.
1672///
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001673void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1674 std::vector<SDNode*> *Deleted) {
Chris Lattner373f0482005-08-26 18:36:28 +00001675 SDNode *From = FromN.Val, *To = ToN.Val;
1676 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1677 "Cannot replace with this method!");
Chris Lattnerab0de9d2005-08-17 19:00:20 +00001678 assert(From != To && "Cannot replace uses of with self");
Chris Lattner373f0482005-08-26 18:36:28 +00001679
Chris Lattnerab0de9d2005-08-17 19:00:20 +00001680 while (!From->use_empty()) {
1681 // Process users until they are all gone.
1682 SDNode *U = *From->use_begin();
1683
1684 // This node is about to morph, remove its old self from the CSE maps.
1685 RemoveNodeFromCSEMaps(U);
1686
Chris Lattner7e4b5d32005-11-08 22:07:03 +00001687 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1688 I != E; ++I)
1689 if (I->Val == From) {
Chris Lattner373f0482005-08-26 18:36:28 +00001690 From->removeUser(U);
Chris Lattner7e4b5d32005-11-08 22:07:03 +00001691 I->Val = To;
Chris Lattner373f0482005-08-26 18:36:28 +00001692 To->addUser(U);
1693 }
1694
1695 // Now that we have modified U, add it back to the CSE maps. If it already
1696 // exists there, recursively merge the results together.
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001697 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1698 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner373f0482005-08-26 18:36:28 +00001699 // U is now dead.
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001700 if (Deleted) Deleted->push_back(U);
1701 DeleteNodeNotInCSEMaps(U);
1702 }
Chris Lattner373f0482005-08-26 18:36:28 +00001703 }
1704}
1705
1706/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1707/// This can cause recursive merging of nodes in the DAG.
1708///
1709/// This version assumes From/To have matching types and numbers of result
1710/// values.
1711///
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001712void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1713 std::vector<SDNode*> *Deleted) {
Chris Lattner373f0482005-08-26 18:36:28 +00001714 assert(From != To && "Cannot replace uses of with self");
1715 assert(From->getNumValues() == To->getNumValues() &&
1716 "Cannot use this version of ReplaceAllUsesWith!");
1717 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001718 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner373f0482005-08-26 18:36:28 +00001719 return;
1720 }
1721
1722 while (!From->use_empty()) {
1723 // Process users until they are all gone.
1724 SDNode *U = *From->use_begin();
1725
1726 // This node is about to morph, remove its old self from the CSE maps.
1727 RemoveNodeFromCSEMaps(U);
1728
Chris Lattner7e4b5d32005-11-08 22:07:03 +00001729 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1730 I != E; ++I)
1731 if (I->Val == From) {
Chris Lattnerab0de9d2005-08-17 19:00:20 +00001732 From->removeUser(U);
Chris Lattner7e4b5d32005-11-08 22:07:03 +00001733 I->Val = To;
Chris Lattnerab0de9d2005-08-17 19:00:20 +00001734 To->addUser(U);
1735 }
1736
1737 // Now that we have modified U, add it back to the CSE maps. If it already
1738 // exists there, recursively merge the results together.
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001739 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1740 ReplaceAllUsesWith(U, Existing, Deleted);
1741 // U is now dead.
1742 if (Deleted) Deleted->push_back(U);
1743 DeleteNodeNotInCSEMaps(U);
1744 }
Chris Lattnerab0de9d2005-08-17 19:00:20 +00001745 }
1746}
1747
Chris Lattner373f0482005-08-26 18:36:28 +00001748/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1749/// This can cause recursive merging of nodes in the DAG.
1750///
1751/// This version can replace From with any result values. To must match the
1752/// number and types of values returned by From.
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001753void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001754 const std::vector<SDOperand> &To,
1755 std::vector<SDNode*> *Deleted) {
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001756 assert(From->getNumValues() == To.size() &&
1757 "Incorrect number of values to replace with!");
Chris Lattner87421c82005-08-28 23:59:36 +00001758 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001759 // Degenerate case handled above.
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001760 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001761 return;
1762 }
1763
1764 while (!From->use_empty()) {
1765 // Process users until they are all gone.
1766 SDNode *U = *From->use_begin();
1767
1768 // This node is about to morph, remove its old self from the CSE maps.
1769 RemoveNodeFromCSEMaps(U);
1770
Chris Lattner7e4b5d32005-11-08 22:07:03 +00001771 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1772 I != E; ++I)
1773 if (I->Val == From) {
1774 const SDOperand &ToOp = To[I->ResNo];
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001775 From->removeUser(U);
Chris Lattner7e4b5d32005-11-08 22:07:03 +00001776 *I = ToOp;
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001777 ToOp.Val->addUser(U);
1778 }
1779
1780 // Now that we have modified U, add it back to the CSE maps. If it already
1781 // exists there, recursively merge the results together.
Chris Lattnerfe883ad2005-09-07 05:37:01 +00001782 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1783 ReplaceAllUsesWith(U, Existing, Deleted);
1784 // U is now dead.
1785 if (Deleted) Deleted->push_back(U);
1786 DeleteNodeNotInCSEMaps(U);
1787 }
Chris Lattnerd7ee4d82005-08-24 22:44:39 +00001788 }
1789}
1790
1791
Jim Laskeyd66e6162005-08-17 20:08:02 +00001792//===----------------------------------------------------------------------===//
1793// SDNode Class
1794//===----------------------------------------------------------------------===//
Chris Lattner19732782005-08-16 18:17:10 +00001795
Chris Lattner88fa11c2005-11-08 23:30:28 +00001796
1797/// getValueTypeList - Return a pointer to the specified value type.
1798///
1799MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1800 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1801 VTs[VT] = VT;
1802 return &VTs[VT];
1803}
1804
Chris Lattner40e79822005-01-12 18:37:47 +00001805/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1806/// indicated value. This method ignores uses of other values defined by this
1807/// operation.
1808bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1809 assert(Value < getNumValues() && "Bad value!");
1810
1811 // If there is only one value, this is easy.
1812 if (getNumValues() == 1)
1813 return use_size() == NUses;
1814 if (Uses.size() < NUses) return false;
1815
1816 SDOperand TheValue(this, Value);
1817
1818 std::set<SDNode*> UsersHandled;
1819
1820 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1821 UI != E; ++UI) {
1822 SDNode *User = *UI;
1823 if (User->getNumOperands() == 1 ||
1824 UsersHandled.insert(User).second) // First time we've seen this?
1825 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1826 if (User->getOperand(i) == TheValue) {
1827 if (NUses == 0)
1828 return false; // too many uses
1829 --NUses;
1830 }
1831 }
1832
1833 // Found exactly the right number of uses?
1834 return NUses == 0;
1835}
1836
1837
Chris Lattnerbc892262005-08-16 18:33:07 +00001838const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattner9e4c7612005-01-10 23:25:25 +00001839 switch (getOpcode()) {
Chris Lattnerbc892262005-08-16 18:33:07 +00001840 default:
1841 if (getOpcode() < ISD::BUILTIN_OP_END)
1842 return "<<Unknown DAG Node>>";
1843 else {
Evan Cheng6af02632005-12-20 06:22:03 +00001844 if (G) {
Chris Lattnerbc892262005-08-16 18:33:07 +00001845 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner16e5cb82005-09-09 22:35:03 +00001846 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1847 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng9fd95412005-12-19 23:11:49 +00001848
Evan Cheng6af02632005-12-20 06:22:03 +00001849 TargetLowering &TLI = G->getTargetLoweringInfo();
1850 const char *Name =
1851 TLI.getTargetNodeName(getOpcode());
1852 if (Name) return Name;
1853 }
1854
1855 return "<<Unknown Target Node>>";
Chris Lattnerbc892262005-08-16 18:33:07 +00001856 }
1857
Andrew Lenharthdec53922005-03-31 21:24:06 +00001858 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001859 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner9440d6e2005-05-09 04:08:27 +00001860 case ISD::SRCVALUE: return "SrcValue";
Chris Lattner802080d2005-08-18 03:31:02 +00001861 case ISD::VALUETYPE: return "ValueType";
Chris Lattner435b4022005-11-29 06:21:05 +00001862 case ISD::STRING: return "String";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001863 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001864 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman43144a22005-08-30 02:44:00 +00001865 case ISD::AssertSext: return "AssertSext";
1866 case ISD::AssertZext: return "AssertZext";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001867 case ISD::Constant: return "Constant";
Chris Lattner0d2456e2005-08-17 00:34:06 +00001868 case ISD::TargetConstant: return "TargetConstant";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001869 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman41b1cdc2005-12-06 06:18:55 +00001870 case ISD::ConstantVec: return "ConstantVec";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001871 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattner1be7edd2005-08-19 22:31:04 +00001872 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001873 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerbbe0e7d2005-08-25 00:43:01 +00001874 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001875 case ISD::BasicBlock: return "BasicBlock";
Chris Lattner33182322005-08-16 21:55:35 +00001876 case ISD::Register: return "Register";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001877 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth4b3932a2005-10-23 03:40:17 +00001878 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattnerc30405e2005-08-26 17:15:30 +00001879 case ISD::ConstantPool: return "ConstantPool";
1880 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001881 case ISD::CopyToReg: return "CopyToReg";
1882 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemancda9aa72005-04-01 22:34:39 +00001883 case ISD::UNDEF: return "undef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001884
Chris Lattnerc4a20462005-04-02 04:58:41 +00001885 // Unary operators
1886 case ISD::FABS: return "fabs";
1887 case ISD::FNEG: return "fneg";
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001888 case ISD::FSQRT: return "fsqrt";
1889 case ISD::FSIN: return "fsin";
1890 case ISD::FCOS: return "fcos";
Chris Lattnerc4a20462005-04-02 04:58:41 +00001891
1892 // Binary operators
Chris Lattner9e4c7612005-01-10 23:25:25 +00001893 case ISD::ADD: return "add";
1894 case ISD::SUB: return "sub";
1895 case ISD::MUL: return "mul";
Nate Begeman55e86252005-04-05 22:36:56 +00001896 case ISD::MULHU: return "mulhu";
1897 case ISD::MULHS: return "mulhs";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001898 case ISD::SDIV: return "sdiv";
1899 case ISD::UDIV: return "udiv";
1900 case ISD::SREM: return "srem";
1901 case ISD::UREM: return "urem";
1902 case ISD::AND: return "and";
1903 case ISD::OR: return "or";
1904 case ISD::XOR: return "xor";
1905 case ISD::SHL: return "shl";
1906 case ISD::SRA: return "sra";
1907 case ISD::SRL: return "srl";
Chris Lattner6f3b5772005-09-28 22:28:18 +00001908 case ISD::FADD: return "fadd";
1909 case ISD::FSUB: return "fsub";
1910 case ISD::FMUL: return "fmul";
1911 case ISD::FDIV: return "fdiv";
1912 case ISD::FREM: return "frem";
Nate Begemanb2e089c2005-11-19 00:36:38 +00001913 case ISD::VADD: return "vadd";
1914 case ISD::VSUB: return "vsub";
1915 case ISD::VMUL: return "vmul";
Chris Lattner6f3b5772005-09-28 22:28:18 +00001916
Chris Lattnerd47675e2005-08-09 20:20:18 +00001917 case ISD::SETCC: return "setcc";
1918 case ISD::SELECT: return "select";
Nate Begemane5b86d72005-08-10 20:51:12 +00001919 case ISD::SELECT_CC: return "select_cc";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001920 case ISD::ADD_PARTS: return "add_parts";
1921 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner5b7bb562005-04-02 03:30:42 +00001922 case ISD::SHL_PARTS: return "shl_parts";
1923 case ISD::SRA_PARTS: return "sra_parts";
1924 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001925
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001926 // Conversion operators.
Chris Lattner9e4c7612005-01-10 23:25:25 +00001927 case ISD::SIGN_EXTEND: return "sign_extend";
1928 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner8c393c22005-09-02 00:17:32 +00001929 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001930 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001931 case ISD::TRUNCATE: return "truncate";
1932 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001933 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001934 case ISD::FP_EXTEND: return "fp_extend";
1935
1936 case ISD::SINT_TO_FP: return "sint_to_fp";
1937 case ISD::UINT_TO_FP: return "uint_to_fp";
1938 case ISD::FP_TO_SINT: return "fp_to_sint";
1939 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner36e663d2005-12-23 00:16:34 +00001940 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001941
1942 // Control flow instructions
1943 case ISD::BR: return "br";
1944 case ISD::BRCOND: return "brcond";
Chris Lattnerb0713c72005-04-09 03:27:28 +00001945 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman371e4952005-08-16 19:49:35 +00001946 case ISD::BR_CC: return "br_cc";
1947 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001948 case ISD::RET: return "ret";
1949 case ISD::CALL: return "call";
Chris Lattnerd0feb642005-05-13 18:43:43 +00001950 case ISD::TAILCALL:return "tailcall";
Chris Lattnere3677d62005-05-12 23:51:40 +00001951 case ISD::CALLSEQ_START: return "callseq_start";
1952 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001953
1954 // Other operators
1955 case ISD::LOAD: return "load";
1956 case ISD::STORE: return "store";
Nate Begemanb2e089c2005-11-19 00:36:38 +00001957 case ISD::VLOAD: return "vload";
Chris Lattner39c67442005-01-14 22:08:15 +00001958 case ISD::EXTLOAD: return "extload";
1959 case ISD::SEXTLOAD: return "sextload";
1960 case ISD::ZEXTLOAD: return "zextload";
1961 case ISD::TRUNCSTORE: return "truncstore";
1962
Chris Lattner9e4c7612005-01-10 23:25:25 +00001963 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1964 case ISD::EXTRACT_ELEMENT: return "extract_element";
1965 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001966 case ISD::MEMSET: return "memset";
1967 case ISD::MEMCPY: return "memcpy";
1968 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001969
Chris Lattner93f4f5f2005-05-11 04:50:30 +00001970 // Bit counting
1971 case ISD::CTPOP: return "ctpop";
1972 case ISD::CTTZ: return "cttz";
1973 case ISD::CTLZ: return "ctlz";
1974
1975 // IO Intrinsics
Chris Lattner67ab9452005-05-09 20:22:17 +00001976 case ISD::READPORT: return "readport";
1977 case ISD::WRITEPORT: return "writeport";
1978 case ISD::READIO: return "readio";
1979 case ISD::WRITEIO: return "writeio";
1980
Chris Lattner435b4022005-11-29 06:21:05 +00001981 // Debug info
1982 case ISD::LOCATION: return "location";
Jim Laskey7c462762005-12-16 22:45:29 +00001983 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner435b4022005-11-29 06:21:05 +00001984
Chris Lattnerd47675e2005-08-09 20:20:18 +00001985 case ISD::CONDCODE:
1986 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattner9e4c7612005-01-10 23:25:25 +00001987 default: assert(0 && "Unknown setcc condition!");
Chris Lattnerd47675e2005-08-09 20:20:18 +00001988 case ISD::SETOEQ: return "setoeq";
1989 case ISD::SETOGT: return "setogt";
1990 case ISD::SETOGE: return "setoge";
1991 case ISD::SETOLT: return "setolt";
1992 case ISD::SETOLE: return "setole";
1993 case ISD::SETONE: return "setone";
Misha Brukman835702a2005-04-21 22:36:52 +00001994
Chris Lattnerd47675e2005-08-09 20:20:18 +00001995 case ISD::SETO: return "seto";
1996 case ISD::SETUO: return "setuo";
1997 case ISD::SETUEQ: return "setue";
1998 case ISD::SETUGT: return "setugt";
1999 case ISD::SETUGE: return "setuge";
2000 case ISD::SETULT: return "setult";
2001 case ISD::SETULE: return "setule";
2002 case ISD::SETUNE: return "setune";
Misha Brukman835702a2005-04-21 22:36:52 +00002003
Chris Lattnerd47675e2005-08-09 20:20:18 +00002004 case ISD::SETEQ: return "seteq";
2005 case ISD::SETGT: return "setgt";
2006 case ISD::SETGE: return "setge";
2007 case ISD::SETLT: return "setlt";
2008 case ISD::SETLE: return "setle";
2009 case ISD::SETNE: return "setne";
Chris Lattner9e4c7612005-01-10 23:25:25 +00002010 }
2011 }
2012}
Chris Lattner061a1ea2005-01-07 07:46:32 +00002013
Chris Lattnerbc892262005-08-16 18:33:07 +00002014void SDNode::dump() const { dump(0); }
2015void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner061a1ea2005-01-07 07:46:32 +00002016 std::cerr << (void*)this << ": ";
2017
2018 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2019 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00002020 if (getValueType(i) == MVT::Other)
2021 std::cerr << "ch";
2022 else
2023 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00002024 }
Chris Lattnerbc892262005-08-16 18:33:07 +00002025 std::cerr << " = " << getOperationName(G);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002026
2027 std::cerr << " ";
2028 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2029 if (i) std::cerr << ", ";
2030 std::cerr << (void*)getOperand(i).Val;
2031 if (unsigned RN = getOperand(i).ResNo)
2032 std::cerr << ":" << RN;
2033 }
2034
2035 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2036 std::cerr << "<" << CSDN->getValue() << ">";
2037 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2038 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukman835702a2005-04-21 22:36:52 +00002039 } else if (const GlobalAddressSDNode *GADN =
Chris Lattner061a1ea2005-01-07 07:46:32 +00002040 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng0e0de2f2005-11-30 02:04:11 +00002041 int offset = GADN->getOffset();
Chris Lattner061a1ea2005-01-07 07:46:32 +00002042 std::cerr << "<";
2043 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng0e0de2f2005-11-30 02:04:11 +00002044 if (offset > 0)
2045 std::cerr << " + " << offset;
2046 else
2047 std::cerr << " " << offset;
Misha Brukman77451162005-04-22 04:01:18 +00002048 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00002049 std::cerr << "<" << FIDN->getIndex() << ">";
2050 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattnerc30405e2005-08-26 17:15:30 +00002051 std::cerr << "<" << *CP->get() << ">";
Misha Brukman77451162005-04-22 04:01:18 +00002052 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00002053 std::cerr << "<";
2054 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2055 if (LBB)
2056 std::cerr << LBB->getName() << " ";
2057 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner0875d1a2005-08-19 21:34:13 +00002058 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner49903352005-08-19 21:21:16 +00002059 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2060 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2061 } else {
2062 std::cerr << " #" << R->getReg();
2063 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002064 } else if (const ExternalSymbolSDNode *ES =
2065 dyn_cast<ExternalSymbolSDNode>(this)) {
2066 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner9440d6e2005-05-09 04:08:27 +00002067 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2068 if (M->getValue())
2069 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2070 else
2071 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattner802080d2005-08-18 03:31:02 +00002072 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2073 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattner061a1ea2005-01-07 07:46:32 +00002074 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00002075}
2076
Chris Lattnerbf4f23322005-11-09 23:47:37 +00002077static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnere6f78822005-01-09 20:38:33 +00002078 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2079 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerbc892262005-08-16 18:33:07 +00002080 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnere6f78822005-01-09 20:38:33 +00002081 else
2082 std::cerr << "\n" << std::string(indent+2, ' ')
2083 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukman835702a2005-04-21 22:36:52 +00002084
Chris Lattnere6f78822005-01-09 20:38:33 +00002085
2086 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerbc892262005-08-16 18:33:07 +00002087 N->dump(G);
Chris Lattnere6f78822005-01-09 20:38:33 +00002088}
2089
Chris Lattner061a1ea2005-01-07 07:46:32 +00002090void SelectionDAG::dump() const {
2091 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerbf4f23322005-11-09 23:47:37 +00002092 std::vector<const SDNode*> Nodes;
2093 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2094 I != E; ++I)
2095 Nodes.push_back(I);
2096
Chris Lattner1270acc2005-01-09 20:26:36 +00002097 std::sort(Nodes.begin(), Nodes.end());
2098
2099 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00002100 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerbc892262005-08-16 18:33:07 +00002101 DumpNodes(Nodes[i], 2, this);
Chris Lattner061a1ea2005-01-07 07:46:32 +00002102 }
Chris Lattnere6f78822005-01-09 20:38:33 +00002103
Chris Lattnerbc892262005-08-16 18:33:07 +00002104 DumpNodes(getRoot().Val, 2, this);
Chris Lattnere6f78822005-01-09 20:38:33 +00002105
Chris Lattner061a1ea2005-01-07 07:46:32 +00002106 std::cerr << "\n\n";
2107}
2108