blob: a8489d2965683ebf24bed53a5258833e00d9ed73 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner5cdcc582005-01-09 20:52:51 +000030static bool isCommutativeBinOp(unsigned Opcode) {
31 switch (Opcode) {
32 case ISD::ADD:
33 case ISD::MUL:
Chris Lattner01b3d732005-09-28 22:28:18 +000034 case ISD::FADD:
35 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000036 case ISD::AND:
37 case ISD::OR:
38 case ISD::XOR: return true;
39 default: return false; // FIXME: Need commutative info for user ops!
40 }
41}
42
43static bool isAssociativeBinOp(unsigned Opcode) {
44 switch (Opcode) {
45 case ISD::ADD:
46 case ISD::MUL:
47 case ISD::AND:
48 case ISD::OR:
49 case ISD::XOR: return true;
50 default: return false; // FIXME: Need associative info for user ops!
51 }
52}
53
Chris Lattner5cdcc582005-01-09 20:52:51 +000054// isInvertibleForFree - Return true if there is no cost to emitting the logical
55// inverse of this node.
56static bool isInvertibleForFree(SDOperand N) {
57 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000058 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000059 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000060 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000061}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
71bool ConstantFPSDNode::isExactlyValue(double V) const {
72 return DoubleToBits(V) == DoubleToBits(Value);
73}
74
75//===----------------------------------------------------------------------===//
76// ISD Class
77//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000078
Chris Lattnerc3aae252005-01-07 07:46:32 +000079/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
80/// when given the operation for (X op Y).
81ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
82 // To perform this operation, we just need to swap the L and G bits of the
83 // operation.
84 unsigned OldL = (Operation >> 2) & 1;
85 unsigned OldG = (Operation >> 1) & 1;
86 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
87 (OldL << 1) | // New G bit
88 (OldG << 2)); // New L bit.
89}
90
91/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
92/// 'op' is a valid SetCC operation.
93ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
94 unsigned Operation = Op;
95 if (isInteger)
96 Operation ^= 7; // Flip L, G, E bits, but not U.
97 else
98 Operation ^= 15; // Flip all of the condition bits.
99 if (Operation > ISD::SETTRUE2)
100 Operation &= ~8; // Don't let N and U bits get set.
101 return ISD::CondCode(Operation);
102}
103
104
105/// isSignedOp - For an integer comparison, return 1 if the comparison is a
106/// signed operation and 2 if the result is an unsigned comparison. Return zero
107/// if the operation does not depend on the sign of the input (setne and seteq).
108static int isSignedOp(ISD::CondCode Opcode) {
109 switch (Opcode) {
110 default: assert(0 && "Illegal integer setcc operation!");
111 case ISD::SETEQ:
112 case ISD::SETNE: return 0;
113 case ISD::SETLT:
114 case ISD::SETLE:
115 case ISD::SETGT:
116 case ISD::SETGE: return 1;
117 case ISD::SETULT:
118 case ISD::SETULE:
119 case ISD::SETUGT:
120 case ISD::SETUGE: return 2;
121 }
122}
123
124/// getSetCCOrOperation - Return the result of a logical OR between different
125/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
126/// returns SETCC_INVALID if it is not possible to represent the resultant
127/// comparison.
128ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
129 bool isInteger) {
130 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
131 // Cannot fold a signed integer setcc with an unsigned integer setcc.
132 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000135
Chris Lattnerc3aae252005-01-07 07:46:32 +0000136 // If the N and U bits get set then the resultant comparison DOES suddenly
137 // care about orderedness, and is true when ordered.
138 if (Op > ISD::SETTRUE2)
139 Op &= ~16; // Clear the N bit.
140 return ISD::CondCode(Op);
141}
142
143/// getSetCCAndOperation - Return the result of a logical AND between different
144/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
145/// function returns zero if it is not possible to represent the resultant
146/// comparison.
147ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
148 bool isInteger) {
149 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
150 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000151 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000152
153 // Combine all of the condition bits.
154 return ISD::CondCode(Op1 & Op2);
155}
156
Chris Lattnerb48da392005-01-23 04:39:44 +0000157const TargetMachine &SelectionDAG::getTarget() const {
158 return TLI.getTargetMachine();
159}
160
Jim Laskey58b968b2005-08-17 20:08:02 +0000161//===----------------------------------------------------------------------===//
162// SelectionDAG Class
163//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000164
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000165/// RemoveDeadNodes - This method deletes all unreachable nodes in the
166/// SelectionDAG, including nodes (like loads) that have uses of their token
167/// chain but no other uses and no side effect. If a node is passed in as an
168/// argument, it is used as the seed for node deletion.
169void SelectionDAG::RemoveDeadNodes(SDNode *N) {
170 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
171
172 // Create a dummy node (which is not added to allnodes), that adds a reference
173 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000174 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000175
Chris Lattner8b8749f2005-08-17 19:00:20 +0000176 // If we have a hint to start from, use it.
177 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000178
179 Restart:
180 unsigned NumNodes = AllNodeSet.size();
181 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
182 I != E; ++I) {
183 // Try to delete this node.
184 DeleteNodeIfDead(*I, &AllNodeSet);
185
186 // If we actually deleted any nodes, do not use invalid iterators in
187 // AllNodeSet.
188 if (AllNodeSet.size() != NumNodes)
189 goto Restart;
190 }
191
192 // Restore AllNodes.
193 if (AllNodes.size() != NumNodes)
194 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
195
196 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000197 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000198}
199
Chris Lattner149c58c2005-08-16 18:17:10 +0000200
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000201void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
202 if (!N->use_empty())
203 return;
204
205 // Okay, we really are going to delete this node. First take this out of the
206 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000207 RemoveNodeFromCSEMaps(N);
208
209 // Next, brutally remove the operand list. This is safe to do, as there are
210 // no cycles in the graph.
211 while (!N->Operands.empty()) {
212 SDNode *O = N->Operands.back().Val;
213 N->Operands.pop_back();
214 O->removeUser(N);
215
216 // Now that we removed this operand, see if there are no uses of it left.
217 DeleteNodeIfDead(O, NodeSet);
218 }
219
220 // Remove the node from the nodes set and delete it.
221 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
222 AllNodeSet.erase(N);
223
224 // Now that the node is gone, check to see if any of the operands of this node
225 // are dead now.
226 delete N;
227}
228
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000229void SelectionDAG::DeleteNode(SDNode *N) {
230 assert(N->use_empty() && "Cannot delete a node that is not dead!");
231
232 // First take this out of the appropriate CSE map.
233 RemoveNodeFromCSEMaps(N);
234
Chris Lattner1e111c72005-09-07 05:37:01 +0000235 // Finally, remove uses due to operands of this node, remove from the
236 // AllNodes list, and delete the node.
237 DeleteNodeNotInCSEMaps(N);
238}
239
240void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
241
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000242 // Remove it from the AllNodes list.
243 for (std::vector<SDNode*>::iterator I = AllNodes.begin(); ; ++I) {
244 assert(I != AllNodes.end() && "Node not in AllNodes list??");
245 if (*I == N) {
246 // Erase from the vector, which is not ordered.
247 std::swap(*I, AllNodes.back());
248 AllNodes.pop_back();
249 break;
250 }
251 }
252
253 // Drop all of the operands and decrement used nodes use counts.
254 while (!N->Operands.empty()) {
255 SDNode *O = N->Operands.back().Val;
256 N->Operands.pop_back();
257 O->removeUser(N);
258 }
259
260 delete N;
261}
262
Chris Lattner149c58c2005-08-16 18:17:10 +0000263/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
264/// correspond to it. This is useful when we're about to delete or repurpose
265/// the node. We don't want future request for structurally identical nodes
266/// to return N anymore.
267void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000268 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000270 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000272 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
273 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000274 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000275 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000276 Erased = TargetConstants.erase(std::make_pair(
277 cast<ConstantSDNode>(N)->getValue(),
278 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000279 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000280 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000281 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000282 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000283 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000284 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000285 case ISD::CONDCODE:
286 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
287 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000288 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000289 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
290 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000291 case ISD::GlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000292 Erased = GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000293 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000294 case ISD::TargetGlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000295 Erased =TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000296 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000297 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000298 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000300 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000301 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000302 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000304 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000305 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000306 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000307 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000308 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000310 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000311 break;
312 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000313 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000314 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000315 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000316 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000317 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
318 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000319 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000320 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
321 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000322 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000323 case ISD::SRCVALUE: {
324 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000325 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000326 break;
327 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000328 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000329 Erased = Loads.erase(std::make_pair(N->getOperand(1),
330 std::make_pair(N->getOperand(0),
331 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000332 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000333 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000335 if (N->getNumOperands() == 0) {
336 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
337 N->getValueType(0)));
338 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000339 Erased =
340 UnaryOps.erase(std::make_pair(N->getOpcode(),
341 std::make_pair(N->getOperand(0),
342 N->getValueType(0))));
343 } else if (N->getNumOperands() == 2) {
344 Erased =
345 BinaryOps.erase(std::make_pair(N->getOpcode(),
346 std::make_pair(N->getOperand(0),
347 N->getOperand(1))));
348 } else {
349 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
350 Erased =
351 OneResultNodes.erase(std::make_pair(N->getOpcode(),
352 std::make_pair(N->getValueType(0),
353 Ops)));
354 }
Chris Lattner385328c2005-05-14 07:42:29 +0000355 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000356 // Remove the node from the ArbitraryNodes map.
357 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
358 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000359 Erased =
360 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
361 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000362 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000363 break;
364 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000365#ifndef NDEBUG
366 // Verify that the node was actually in one of the CSE maps, unless it has a
367 // flag result (which cannot be CSE'd) or is one of the special cases that are
368 // not subject to CSE.
369 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
370 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000371 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372
373 N->dump();
374 assert(0 && "Node is not in map!");
375 }
376#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000377}
378
Chris Lattner8b8749f2005-08-17 19:00:20 +0000379/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
380/// has been taken out and modified in some way. If the specified node already
381/// exists in the CSE maps, do not modify the maps, but return the existing node
382/// instead. If it doesn't exist, add it and return null.
383///
384SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
385 assert(N->getNumOperands() && "This is a leaf node!");
386 if (N->getOpcode() == ISD::LOAD) {
387 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
388 std::make_pair(N->getOperand(0),
389 N->getValueType(0)))];
390 if (L) return L;
391 L = N;
Chris Lattner95038592005-10-05 06:35:28 +0000392 } else if (N->getOpcode() == ISD::HANDLENODE) {
393 return 0; // never add it.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000394 } else if (N->getNumOperands() == 1) {
395 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
396 std::make_pair(N->getOperand(0),
397 N->getValueType(0)))];
398 if (U) return U;
399 U = N;
400 } else if (N->getNumOperands() == 2) {
401 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
402 std::make_pair(N->getOperand(0),
403 N->getOperand(1)))];
404 if (B) return B;
405 B = N;
406 } else if (N->getNumValues() == 1) {
407 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
408 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
409 std::make_pair(N->getValueType(0), Ops))];
410 if (ORN) return ORN;
411 ORN = N;
412 } else {
413 // Remove the node from the ArbitraryNodes map.
414 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
415 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
416 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
417 std::make_pair(RV, Ops))];
418 if (AN) return AN;
419 AN = N;
420 }
421 return 0;
422
423}
424
425
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000426
Chris Lattner78ec3112003-08-11 14:57:33 +0000427SelectionDAG::~SelectionDAG() {
428 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
429 delete AllNodes[i];
430}
431
Chris Lattner0f2287b2005-04-13 02:38:18 +0000432SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000433 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000434 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000435 return getNode(ISD::AND, Op.getValueType(), Op,
436 getConstant(Imm, Op.getValueType()));
437}
438
Chris Lattnerc3aae252005-01-07 07:46:32 +0000439SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
440 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
441 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000442 if (VT != MVT::i64)
443 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000444
Chris Lattnerc3aae252005-01-07 07:46:32 +0000445 SDNode *&N = Constants[std::make_pair(Val, VT)];
446 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000447 N = new ConstantSDNode(false, Val, VT);
448 AllNodes.push_back(N);
449 return SDOperand(N, 0);
450}
451
452SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
453 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
454 // Mask out any bits that are not valid for this constant.
455 if (VT != MVT::i64)
456 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
457
458 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
459 if (N) return SDOperand(N, 0);
460 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000461 AllNodes.push_back(N);
462 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000463}
464
Chris Lattnerc3aae252005-01-07 07:46:32 +0000465SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
466 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
467 if (VT == MVT::f32)
468 Val = (float)Val; // Mask out extra precision.
469
Chris Lattnerd8658612005-02-17 20:17:32 +0000470 // Do the map lookup using the actual bit pattern for the floating point
471 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
472 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000473 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000474 if (N) return SDOperand(N, 0);
475 N = new ConstantFPSDNode(Val, VT);
476 AllNodes.push_back(N);
477 return SDOperand(N, 0);
478}
479
480
481
482SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
483 MVT::ValueType VT) {
484 SDNode *&N = GlobalValues[GV];
485 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000486 N = new GlobalAddressSDNode(false, GV, VT);
487 AllNodes.push_back(N);
488 return SDOperand(N, 0);
489}
490
491SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
492 MVT::ValueType VT) {
493 SDNode *&N = TargetGlobalValues[GV];
494 if (N) return SDOperand(N, 0);
495 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000496 AllNodes.push_back(N);
497 return SDOperand(N, 0);
498}
499
500SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
501 SDNode *&N = FrameIndices[FI];
502 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000503 N = new FrameIndexSDNode(FI, VT, false);
504 AllNodes.push_back(N);
505 return SDOperand(N, 0);
506}
507
508SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
509 SDNode *&N = TargetFrameIndices[FI];
510 if (N) return SDOperand(N, 0);
511 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000512 AllNodes.push_back(N);
513 return SDOperand(N, 0);
514}
515
Chris Lattner5839bf22005-08-26 17:15:30 +0000516SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
517 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000518 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000519 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000520 AllNodes.push_back(N);
521 return SDOperand(N, 0);
522}
523
Chris Lattner5839bf22005-08-26 17:15:30 +0000524SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
525 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000526 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000527 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000528 AllNodes.push_back(N);
529 return SDOperand(N, 0);
530}
531
532SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
533 SDNode *&N = BBNodes[MBB];
534 if (N) return SDOperand(N, 0);
535 N = new BasicBlockSDNode(MBB);
536 AllNodes.push_back(N);
537 return SDOperand(N, 0);
538}
539
Chris Lattner15e4b012005-07-10 00:07:11 +0000540SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
541 if ((unsigned)VT >= ValueTypeNodes.size())
542 ValueTypeNodes.resize(VT+1);
543 if (ValueTypeNodes[VT] == 0) {
544 ValueTypeNodes[VT] = new VTSDNode(VT);
545 AllNodes.push_back(ValueTypeNodes[VT]);
546 }
547
548 return SDOperand(ValueTypeNodes[VT], 0);
549}
550
Chris Lattnerc3aae252005-01-07 07:46:32 +0000551SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
552 SDNode *&N = ExternalSymbols[Sym];
553 if (N) return SDOperand(N, 0);
554 N = new ExternalSymbolSDNode(Sym, VT);
555 AllNodes.push_back(N);
556 return SDOperand(N, 0);
557}
558
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000559SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
560 if ((unsigned)Cond >= CondCodeNodes.size())
561 CondCodeNodes.resize(Cond+1);
562
Chris Lattner079a27a2005-08-09 20:40:02 +0000563 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000564 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000565 AllNodes.push_back(CondCodeNodes[Cond]);
566 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000567 return SDOperand(CondCodeNodes[Cond], 0);
568}
569
Chris Lattner0fdd7682005-08-30 22:38:38 +0000570SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
571 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
572 if (!Reg) {
573 Reg = new RegisterSDNode(RegNo, VT);
574 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000575 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000576 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000577}
578
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000579SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
580 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000581 // These setcc operations always fold.
582 switch (Cond) {
583 default: break;
584 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000585 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000586 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000587 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000588 }
589
Chris Lattner67255a12005-04-07 18:14:58 +0000590 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
591 uint64_t C2 = N2C->getValue();
592 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
593 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000594
Chris Lattnerc3aae252005-01-07 07:46:32 +0000595 // Sign extend the operands if required
596 if (ISD::isSignedIntSetCC(Cond)) {
597 C1 = N1C->getSignExtended();
598 C2 = N2C->getSignExtended();
599 }
600
601 switch (Cond) {
602 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000603 case ISD::SETEQ: return getConstant(C1 == C2, VT);
604 case ISD::SETNE: return getConstant(C1 != C2, VT);
605 case ISD::SETULT: return getConstant(C1 < C2, VT);
606 case ISD::SETUGT: return getConstant(C1 > C2, VT);
607 case ISD::SETULE: return getConstant(C1 <= C2, VT);
608 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
609 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
610 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
611 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
612 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000613 }
Chris Lattner24673922005-04-07 18:58:54 +0000614 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000615 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000616 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
617 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
618
619 // If the comparison constant has bits in the upper part, the
620 // zero-extended value could never match.
621 if (C2 & (~0ULL << InSize)) {
622 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
623 switch (Cond) {
624 case ISD::SETUGT:
625 case ISD::SETUGE:
626 case ISD::SETEQ: return getConstant(0, VT);
627 case ISD::SETULT:
628 case ISD::SETULE:
629 case ISD::SETNE: return getConstant(1, VT);
630 case ISD::SETGT:
631 case ISD::SETGE:
632 // True if the sign bit of C2 is set.
633 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
634 case ISD::SETLT:
635 case ISD::SETLE:
636 // True if the sign bit of C2 isn't set.
637 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
638 default:
639 break;
640 }
641 }
642
643 // Otherwise, we can perform the comparison with the low bits.
644 switch (Cond) {
645 case ISD::SETEQ:
646 case ISD::SETNE:
647 case ISD::SETUGT:
648 case ISD::SETUGE:
649 case ISD::SETULT:
650 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000651 return getSetCC(VT, N1.getOperand(0),
652 getConstant(C2, N1.getOperand(0).getValueType()),
653 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000654 default:
655 break; // todo, be more careful with signed comparisons
656 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000657 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
658 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
659 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
660 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
661 MVT::ValueType ExtDstTy = N1.getValueType();
662 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000663
Chris Lattner7b2880c2005-08-24 22:44:39 +0000664 // If the extended part has any inconsistent bits, it cannot ever
665 // compare equal. In other words, they have to be all ones or all
666 // zeros.
667 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000668 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000669 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
670 return getConstant(Cond == ISD::SETNE, VT);
671
672 // Otherwise, make this a use of a zext.
673 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000674 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000675 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000676 }
677
Chris Lattner67255a12005-04-07 18:14:58 +0000678 uint64_t MinVal, MaxVal;
679 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
680 if (ISD::isSignedIntSetCC(Cond)) {
681 MinVal = 1ULL << (OperandBitSize-1);
682 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
683 MaxVal = ~0ULL >> (65-OperandBitSize);
684 else
685 MaxVal = 0;
686 } else {
687 MinVal = 0;
688 MaxVal = ~0ULL >> (64-OperandBitSize);
689 }
690
691 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
692 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
693 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
694 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000695 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
696 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000697 }
698
699 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
700 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
701 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000702 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
703 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000704 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000705
Nate Begeman72ea2812005-04-14 08:56:52 +0000706 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
707 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000708
Nate Begeman72ea2812005-04-14 08:56:52 +0000709 // Canonicalize setgt X, Min --> setne X, Min
710 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000711 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000712
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000713 // If we have setult X, 1, turn it into seteq X, 0
714 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000715 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
716 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000717 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000718 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000719 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
720 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000721
722 // If we have "setcc X, C1", check to see if we can shrink the immediate
723 // by changing cc.
724
725 // SETUGT X, SINTMAX -> SETLT X, 0
726 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
727 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000728 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000729
730 // FIXME: Implement the rest of these.
731
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000732
733 // Fold bit comparisons when we can.
734 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
735 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
736 if (ConstantSDNode *AndRHS =
737 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
738 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
739 // Perform the xform if the AND RHS is a single bit.
740 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
741 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000742 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000743 TLI.getShiftAmountTy()));
744 }
745 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
746 // (X & 8) == 8 --> (X & 8) >> 3
747 // Perform the xform if C2 is a single bit.
748 if ((C2 & (C2-1)) == 0) {
749 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000750 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000751 }
752 }
753 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000754 }
Chris Lattner67255a12005-04-07 18:14:58 +0000755 } else if (isa<ConstantSDNode>(N1.Val)) {
756 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000757 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000758 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000759
760 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
761 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
762 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000763
Chris Lattnerc3aae252005-01-07 07:46:32 +0000764 switch (Cond) {
765 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000766 case ISD::SETEQ: return getConstant(C1 == C2, VT);
767 case ISD::SETNE: return getConstant(C1 != C2, VT);
768 case ISD::SETLT: return getConstant(C1 < C2, VT);
769 case ISD::SETGT: return getConstant(C1 > C2, VT);
770 case ISD::SETLE: return getConstant(C1 <= C2, VT);
771 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000772 }
773 } else {
774 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000775 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000776 }
777
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000778 // Could not fold it.
779 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000780}
781
Chris Lattnerc3aae252005-01-07 07:46:32 +0000782/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000783///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000785 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
786 if (!N) {
787 N = new SDNode(Opcode, VT);
788 AllNodes.push_back(N);
789 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000790 return SDOperand(N, 0);
791}
792
Chris Lattnerc3aae252005-01-07 07:46:32 +0000793SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
794 SDOperand Operand) {
795 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
796 uint64_t Val = C->getValue();
797 switch (Opcode) {
798 default: break;
799 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000800 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000801 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
802 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000803 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
804 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000805 }
806 }
807
808 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
809 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000810 case ISD::FNEG:
811 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812 case ISD::FP_ROUND:
813 case ISD::FP_EXTEND:
814 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000815 case ISD::FP_TO_SINT:
816 return getConstant((int64_t)C->getValue(), VT);
817 case ISD::FP_TO_UINT:
818 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819 }
820
821 unsigned OpOpcode = Operand.Val->getOpcode();
822 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000823 case ISD::TokenFactor:
824 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000825 case ISD::SIGN_EXTEND:
826 if (Operand.getValueType() == VT) return Operand; // noop extension
827 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
828 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
829 break;
830 case ISD::ZERO_EXTEND:
831 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000832 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000833 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000834 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000835 case ISD::ANY_EXTEND:
836 if (Operand.getValueType() == VT) return Operand; // noop extension
837 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
838 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
839 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
840 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841 case ISD::TRUNCATE:
842 if (Operand.getValueType() == VT) return Operand; // noop truncate
843 if (OpOpcode == ISD::TRUNCATE)
844 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000845 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
846 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000847 // If the source is smaller than the dest, we still need an extend.
848 if (Operand.Val->getOperand(0).getValueType() < VT)
849 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
850 else if (Operand.Val->getOperand(0).getValueType() > VT)
851 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
852 else
853 return Operand.Val->getOperand(0);
854 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000855 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000856 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000857 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
858 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000859 Operand.Val->getOperand(0));
860 if (OpOpcode == ISD::FNEG) // --X -> X
861 return Operand.Val->getOperand(0);
862 break;
863 case ISD::FABS:
864 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
865 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
866 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000867 }
868
Chris Lattner43247a12005-08-25 19:12:10 +0000869 SDNode *N;
870 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
871 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000872 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000873 E = N = new SDNode(Opcode, Operand);
874 } else {
875 N = new SDNode(Opcode, Operand);
876 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000877 N->setValueTypes(VT);
878 AllNodes.push_back(N);
879 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000880}
881
Chris Lattner36019aa2005-04-18 03:48:41 +0000882
883
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
885 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000886#ifndef NDEBUG
887 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000888 case ISD::TokenFactor:
889 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
890 N2.getValueType() == MVT::Other && "Invalid token factor!");
891 break;
Chris Lattner76365122005-01-16 02:23:22 +0000892 case ISD::AND:
893 case ISD::OR:
894 case ISD::XOR:
895 case ISD::UDIV:
896 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000897 case ISD::MULHU:
898 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000899 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
900 // fall through
901 case ISD::ADD:
902 case ISD::SUB:
903 case ISD::MUL:
904 case ISD::SDIV:
905 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000906 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
907 // fall through.
908 case ISD::FADD:
909 case ISD::FSUB:
910 case ISD::FMUL:
911 case ISD::FDIV:
912 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000913 assert(N1.getValueType() == N2.getValueType() &&
914 N1.getValueType() == VT && "Binary operator types must match!");
915 break;
916
917 case ISD::SHL:
918 case ISD::SRA:
919 case ISD::SRL:
920 assert(VT == N1.getValueType() &&
921 "Shift operators return type must be the same as their first arg");
922 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000923 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000924 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000925 case ISD::FP_ROUND_INREG: {
926 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
927 assert(VT == N1.getValueType() && "Not an inreg round!");
928 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
929 "Cannot FP_ROUND_INREG integer types");
930 assert(EVT <= VT && "Not rounding down!");
931 break;
932 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000933 case ISD::AssertSext:
934 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +0000935 case ISD::SIGN_EXTEND_INREG: {
936 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
937 assert(VT == N1.getValueType() && "Not an inreg extend!");
938 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
939 "Cannot *_EXTEND_INREG FP types");
940 assert(EVT <= VT && "Not extending!");
941 }
942
Chris Lattner76365122005-01-16 02:23:22 +0000943 default: break;
944 }
945#endif
946
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
948 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
949 if (N1C) {
950 if (N2C) {
951 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
952 switch (Opcode) {
953 case ISD::ADD: return getConstant(C1 + C2, VT);
954 case ISD::SUB: return getConstant(C1 - C2, VT);
955 case ISD::MUL: return getConstant(C1 * C2, VT);
956 case ISD::UDIV:
957 if (C2) return getConstant(C1 / C2, VT);
958 break;
959 case ISD::UREM :
960 if (C2) return getConstant(C1 % C2, VT);
961 break;
962 case ISD::SDIV :
963 if (C2) return getConstant(N1C->getSignExtended() /
964 N2C->getSignExtended(), VT);
965 break;
966 case ISD::SREM :
967 if (C2) return getConstant(N1C->getSignExtended() %
968 N2C->getSignExtended(), VT);
969 break;
970 case ISD::AND : return getConstant(C1 & C2, VT);
971 case ISD::OR : return getConstant(C1 | C2, VT);
972 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +0000973 case ISD::SHL : return getConstant(C1 << C2, VT);
974 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000975 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 default: break;
977 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 } else { // Cannonicalize constant to RHS if commutative
979 if (isCommutativeBinOp(Opcode)) {
980 std::swap(N1C, N2C);
981 std::swap(N1, N2);
982 }
983 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000984 }
985
986 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
987 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +0000988 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 if (N2CFP) {
990 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
991 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +0000992 case ISD::FADD: return getConstantFP(C1 + C2, VT);
993 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
994 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
995 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996 if (C2) return getConstantFP(C1 / C2, VT);
997 break;
Chris Lattner01b3d732005-09-28 22:28:18 +0000998 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 if (C2) return getConstantFP(fmod(C1, C2), VT);
1000 break;
1001 default: break;
1002 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001003 } else { // Cannonicalize constant to RHS if commutative
1004 if (isCommutativeBinOp(Opcode)) {
1005 std::swap(N1CFP, N2CFP);
1006 std::swap(N1, N2);
1007 }
1008 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001009 }
1010
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 // Finally, fold operations that do not require constants.
1012 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001013 case ISD::FP_ROUND_INREG:
1014 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1015 break;
1016 case ISD::SIGN_EXTEND_INREG: {
1017 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1018 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001019 break;
1020 }
1021
Nate Begemaneea805e2005-04-13 21:23:31 +00001022 // FIXME: figure out how to safely handle things like
1023 // int foo(int x) { return 1 << (x & 255); }
1024 // int bar() { return foo(256); }
1025#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001026 case ISD::SHL:
1027 case ISD::SRL:
1028 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001029 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001030 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001031 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001032 else if (N2.getOpcode() == ISD::AND)
1033 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1034 // If the and is only masking out bits that cannot effect the shift,
1035 // eliminate the and.
1036 unsigned NumBits = MVT::getSizeInBits(VT);
1037 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1038 return getNode(Opcode, VT, N1, N2.getOperand(0));
1039 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001040 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001041#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001042 }
1043
Chris Lattner27e9b412005-05-11 18:57:39 +00001044 // Memoize this node if possible.
1045 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001046 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1047 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001048 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1049 if (BON) return SDOperand(BON, 0);
1050
1051 BON = N = new SDNode(Opcode, N1, N2);
1052 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001053 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001054 }
1055
Chris Lattner3e011362005-05-14 07:45:46 +00001056 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057 AllNodes.push_back(N);
1058 return SDOperand(N, 0);
1059}
1060
Chris Lattner88de6e72005-05-12 00:17:04 +00001061// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001062// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001063void SDNode::setAdjCallChain(SDOperand N) {
1064 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001065 assert((getOpcode() == ISD::CALLSEQ_START ||
1066 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001067
1068 Operands[0].Val->removeUser(this);
1069 Operands[0] = N;
1070 N.Val->Uses.push_back(this);
1071}
1072
1073
1074
Chris Lattnerc3aae252005-01-07 07:46:32 +00001075SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001076 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001077 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1079 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001080 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081
1082 // Loads have a token chain.
1083 N->setValueTypes(VT, MVT::Other);
1084 AllNodes.push_back(N);
1085 return SDOperand(N, 0);
1086}
1087
Chris Lattner5f056bf2005-07-10 01:55:33 +00001088
1089SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1090 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1091 MVT::ValueType EVT) {
1092 std::vector<SDOperand> Ops;
1093 Ops.reserve(4);
1094 Ops.push_back(Chain);
1095 Ops.push_back(Ptr);
1096 Ops.push_back(SV);
1097 Ops.push_back(getValueType(EVT));
1098 std::vector<MVT::ValueType> VTs;
1099 VTs.reserve(2);
1100 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1101 return getNode(Opcode, VTs, Ops);
1102}
1103
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1105 SDOperand N1, SDOperand N2, SDOperand N3) {
1106 // Perform various simplifications.
1107 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1108 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1109 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1110 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001111 case ISD::SETCC: {
1112 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001113 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001114 if (Simp.Val) return Simp;
1115 break;
1116 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001117 case ISD::SELECT:
1118 if (N1C)
1119 if (N1C->getValue())
1120 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001121 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 return N3; // select false, X, Y -> Y
1123
1124 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001125 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001126 case ISD::BRCOND:
1127 if (N2C)
1128 if (N2C->getValue()) // Unconditional branch
1129 return getNode(ISD::BR, MVT::Other, N1, N3);
1130 else
1131 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001132 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 }
1134
Chris Lattner385328c2005-05-14 07:42:29 +00001135 std::vector<SDOperand> Ops;
1136 Ops.reserve(3);
1137 Ops.push_back(N1);
1138 Ops.push_back(N2);
1139 Ops.push_back(N3);
1140
Chris Lattner43247a12005-08-25 19:12:10 +00001141 // Memoize node if it doesn't produce a flag.
1142 SDNode *N;
1143 if (VT != MVT::Flag) {
1144 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1145 if (E) return SDOperand(E, 0);
1146 E = N = new SDNode(Opcode, N1, N2, N3);
1147 } else {
1148 N = new SDNode(Opcode, N1, N2, N3);
1149 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001150 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001151 AllNodes.push_back(N);
1152 return SDOperand(N, 0);
1153}
1154
1155SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001156 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001157 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001158 std::vector<SDOperand> Ops;
1159 Ops.reserve(4);
1160 Ops.push_back(N1);
1161 Ops.push_back(N2);
1162 Ops.push_back(N3);
1163 Ops.push_back(N4);
1164 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001165}
1166
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001167SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1168 SDOperand N1, SDOperand N2, SDOperand N3,
1169 SDOperand N4, SDOperand N5) {
1170 std::vector<SDOperand> Ops;
1171 Ops.reserve(5);
1172 Ops.push_back(N1);
1173 Ops.push_back(N2);
1174 Ops.push_back(N3);
1175 Ops.push_back(N4);
1176 Ops.push_back(N5);
1177 return getNode(Opcode, VT, Ops);
1178}
1179
1180
Chris Lattner0437cdd2005-05-09 04:14:13 +00001181SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001182 assert((!V || isa<PointerType>(V->getType())) &&
1183 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001184 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1185 if (N) return SDOperand(N, 0);
1186
1187 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001188 AllNodes.push_back(N);
1189 return SDOperand(N, 0);
1190}
1191
1192SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001193 std::vector<SDOperand> &Ops) {
1194 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001196 case 1: return getNode(Opcode, VT, Ops[0]);
1197 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1198 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001199 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001200 }
Chris Lattneref847df2005-04-09 03:27:28 +00001201
Chris Lattner89c34632005-05-14 06:20:26 +00001202 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001203 switch (Opcode) {
1204 default: break;
1205 case ISD::BRCONDTWOWAY:
1206 if (N1C)
1207 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001208 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001209 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001210 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001211 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001212 case ISD::BRTWOWAY_CC:
1213 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1214 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1215 "LHS and RHS of comparison must have same type!");
1216 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001217 case ISD::TRUNCSTORE: {
1218 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1219 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1220#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1221 // If this is a truncating store of a constant, convert to the desired type
1222 // and store it instead.
1223 if (isa<Constant>(Ops[0])) {
1224 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1225 if (isa<Constant>(Op))
1226 N1 = Op;
1227 }
1228 // Also for ConstantFP?
1229#endif
1230 if (Ops[0].getValueType() == EVT) // Normal store?
1231 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1232 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1233 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1234 "Can't do FP-INT conversion!");
1235 break;
1236 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001237 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001238 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001239 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1240 "LHS and RHS of condition must have same type!");
1241 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1242 "True and False arms of SelectCC must have same type!");
1243 assert(Ops[2].getValueType() == VT &&
1244 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001245 break;
1246 }
1247 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001248 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001249 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1250 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001251 break;
1252 }
Chris Lattneref847df2005-04-09 03:27:28 +00001253 }
1254
Chris Lattner385328c2005-05-14 07:42:29 +00001255 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001256 SDNode *N;
1257 if (VT != MVT::Flag) {
1258 SDNode *&E =
1259 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1260 if (E) return SDOperand(E, 0);
1261 E = N = new SDNode(Opcode, Ops);
1262 } else {
1263 N = new SDNode(Opcode, Ops);
1264 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001265 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001266 AllNodes.push_back(N);
1267 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001268}
1269
Chris Lattner89c34632005-05-14 06:20:26 +00001270SDOperand SelectionDAG::getNode(unsigned Opcode,
1271 std::vector<MVT::ValueType> &ResultTys,
1272 std::vector<SDOperand> &Ops) {
1273 if (ResultTys.size() == 1)
1274 return getNode(Opcode, ResultTys[0], Ops);
1275
Chris Lattner5f056bf2005-07-10 01:55:33 +00001276 switch (Opcode) {
1277 case ISD::EXTLOAD:
1278 case ISD::SEXTLOAD:
1279 case ISD::ZEXTLOAD: {
1280 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1281 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1282 // If they are asking for an extending load from/to the same thing, return a
1283 // normal load.
1284 if (ResultTys[0] == EVT)
1285 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1286 assert(EVT < ResultTys[0] &&
1287 "Should only be an extending load, not truncating!");
1288 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1289 "Cannot sign/zero extend a FP load!");
1290 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1291 "Cannot convert from FP to Int or Int -> FP!");
1292 break;
1293 }
1294
Chris Lattnere89083a2005-05-14 07:25:05 +00001295 // FIXME: figure out how to safely handle things like
1296 // int foo(int x) { return 1 << (x & 255); }
1297 // int bar() { return foo(256); }
1298#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001299 case ISD::SRA_PARTS:
1300 case ISD::SRL_PARTS:
1301 case ISD::SHL_PARTS:
1302 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001303 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001304 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1305 else if (N3.getOpcode() == ISD::AND)
1306 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1307 // If the and is only masking out bits that cannot effect the shift,
1308 // eliminate the and.
1309 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1310 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1311 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1312 }
1313 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001314#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001315 }
Chris Lattner89c34632005-05-14 06:20:26 +00001316
Chris Lattner43247a12005-08-25 19:12:10 +00001317 // Memoize the node unless it returns a flag.
1318 SDNode *N;
1319 if (ResultTys.back() != MVT::Flag) {
1320 SDNode *&E =
1321 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1322 if (E) return SDOperand(E, 0);
1323 E = N = new SDNode(Opcode, Ops);
1324 } else {
1325 N = new SDNode(Opcode, Ops);
1326 }
Chris Lattner89c34632005-05-14 06:20:26 +00001327 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001328 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001329 return SDOperand(N, 0);
1330}
1331
Chris Lattner149c58c2005-08-16 18:17:10 +00001332
1333/// SelectNodeTo - These are used for target selectors to *mutate* the
1334/// specified node to have the specified return type, Target opcode, and
1335/// operands. Note that target opcodes are stored as
1336/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001337void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1338 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00001339 RemoveNodeFromCSEMaps(N);
1340 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1341 N->setValueTypes(VT);
1342}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001343void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1344 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00001345 RemoveNodeFromCSEMaps(N);
1346 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1347 N->setValueTypes(VT);
1348 N->setOperands(Op1);
1349}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001350void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1351 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001352 SDOperand Op2) {
1353 RemoveNodeFromCSEMaps(N);
1354 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1355 N->setValueTypes(VT);
1356 N->setOperands(Op1, Op2);
1357}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001358void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00001359 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001360 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00001361 RemoveNodeFromCSEMaps(N);
1362 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1363 N->setValueTypes(VT1, VT2);
1364 N->setOperands(Op1, Op2);
1365}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001366void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1367 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001368 SDOperand Op2, SDOperand Op3) {
1369 RemoveNodeFromCSEMaps(N);
1370 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1371 N->setValueTypes(VT);
1372 N->setOperands(Op1, Op2, Op3);
1373}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001374void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1375 MVT::ValueType VT1, MVT::ValueType VT2,
1376 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001377 RemoveNodeFromCSEMaps(N);
1378 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1379 N->setValueTypes(VT1, VT2);
1380 N->setOperands(Op1, Op2, Op3);
1381}
1382
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001383void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1384 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00001385 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1386 RemoveNodeFromCSEMaps(N);
1387 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1388 N->setValueTypes(VT);
1389 N->setOperands(Op1, Op2, Op3, Op4);
1390}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001391void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1392 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00001393 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1394 SDOperand Op5) {
1395 RemoveNodeFromCSEMaps(N);
1396 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1397 N->setValueTypes(VT);
1398 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1399}
Chris Lattner149c58c2005-08-16 18:17:10 +00001400
Chris Lattner8b8749f2005-08-17 19:00:20 +00001401/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1402/// This can cause recursive merging of nodes in the DAG.
1403///
Chris Lattner8b52f212005-08-26 18:36:28 +00001404/// This version assumes From/To have a single result value.
1405///
Chris Lattner1e111c72005-09-07 05:37:01 +00001406void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1407 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001408 SDNode *From = FromN.Val, *To = ToN.Val;
1409 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1410 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001411 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001412
Chris Lattner8b8749f2005-08-17 19:00:20 +00001413 while (!From->use_empty()) {
1414 // Process users until they are all gone.
1415 SDNode *U = *From->use_begin();
1416
1417 // This node is about to morph, remove its old self from the CSE maps.
1418 RemoveNodeFromCSEMaps(U);
1419
1420 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1421 if (U->getOperand(i).Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001422 From->removeUser(U);
1423 U->Operands[i].Val = To;
1424 To->addUser(U);
1425 }
1426
1427 // Now that we have modified U, add it back to the CSE maps. If it already
1428 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001429 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1430 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001431 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001432 if (Deleted) Deleted->push_back(U);
1433 DeleteNodeNotInCSEMaps(U);
1434 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001435 }
1436}
1437
1438/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1439/// This can cause recursive merging of nodes in the DAG.
1440///
1441/// This version assumes From/To have matching types and numbers of result
1442/// values.
1443///
Chris Lattner1e111c72005-09-07 05:37:01 +00001444void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1445 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001446 assert(From != To && "Cannot replace uses of with self");
1447 assert(From->getNumValues() == To->getNumValues() &&
1448 "Cannot use this version of ReplaceAllUsesWith!");
1449 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001450 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001451 return;
1452 }
1453
1454 while (!From->use_empty()) {
1455 // Process users until they are all gone.
1456 SDNode *U = *From->use_begin();
1457
1458 // This node is about to morph, remove its old self from the CSE maps.
1459 RemoveNodeFromCSEMaps(U);
1460
1461 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1462 if (U->getOperand(i).Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001463 From->removeUser(U);
1464 U->Operands[i].Val = To;
1465 To->addUser(U);
1466 }
1467
1468 // Now that we have modified U, add it back to the CSE maps. If it already
1469 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001470 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1471 ReplaceAllUsesWith(U, Existing, Deleted);
1472 // U is now dead.
1473 if (Deleted) Deleted->push_back(U);
1474 DeleteNodeNotInCSEMaps(U);
1475 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001476 }
1477}
1478
Chris Lattner8b52f212005-08-26 18:36:28 +00001479/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1480/// This can cause recursive merging of nodes in the DAG.
1481///
1482/// This version can replace From with any result values. To must match the
1483/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001484void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001485 const std::vector<SDOperand> &To,
1486 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001487 assert(From->getNumValues() == To.size() &&
1488 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001489 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001490 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001491 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001492 return;
1493 }
1494
1495 while (!From->use_empty()) {
1496 // Process users until they are all gone.
1497 SDNode *U = *From->use_begin();
1498
1499 // This node is about to morph, remove its old self from the CSE maps.
1500 RemoveNodeFromCSEMaps(U);
1501
1502 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1503 if (U->getOperand(i).Val == From) {
1504 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001505 From->removeUser(U);
1506 U->Operands[i] = ToOp;
1507 ToOp.Val->addUser(U);
1508 }
1509
1510 // Now that we have modified U, add it back to the CSE maps. If it already
1511 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001512 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1513 ReplaceAllUsesWith(U, Existing, Deleted);
1514 // U is now dead.
1515 if (Deleted) Deleted->push_back(U);
1516 DeleteNodeNotInCSEMaps(U);
1517 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001518 }
1519}
1520
1521
Jim Laskey58b968b2005-08-17 20:08:02 +00001522//===----------------------------------------------------------------------===//
1523// SDNode Class
1524//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001525
Chris Lattner5c884562005-01-12 18:37:47 +00001526/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1527/// indicated value. This method ignores uses of other values defined by this
1528/// operation.
1529bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1530 assert(Value < getNumValues() && "Bad value!");
1531
1532 // If there is only one value, this is easy.
1533 if (getNumValues() == 1)
1534 return use_size() == NUses;
1535 if (Uses.size() < NUses) return false;
1536
1537 SDOperand TheValue(this, Value);
1538
1539 std::set<SDNode*> UsersHandled;
1540
1541 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1542 UI != E; ++UI) {
1543 SDNode *User = *UI;
1544 if (User->getNumOperands() == 1 ||
1545 UsersHandled.insert(User).second) // First time we've seen this?
1546 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1547 if (User->getOperand(i) == TheValue) {
1548 if (NUses == 0)
1549 return false; // too many uses
1550 --NUses;
1551 }
1552 }
1553
1554 // Found exactly the right number of uses?
1555 return NUses == 0;
1556}
1557
1558
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001559const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001560 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001561 default:
1562 if (getOpcode() < ISD::BUILTIN_OP_END)
1563 return "<<Unknown DAG Node>>";
1564 else {
1565 if (G)
1566 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001567 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1568 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001569 return "<<Unknown Target Node>>";
1570 }
1571
Andrew Lenharth95762122005-03-31 21:24:06 +00001572 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001573 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001574 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001575 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001576 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001577 case ISD::AssertSext: return "AssertSext";
1578 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001579 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001580 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001581 case ISD::ConstantFP: return "ConstantFP";
1582 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001583 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001584 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001585 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001586 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001587 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001588 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001589 case ISD::ConstantPool: return "ConstantPool";
1590 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001591 case ISD::CopyToReg: return "CopyToReg";
1592 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001593 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001594 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001595
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001596 // Unary operators
1597 case ISD::FABS: return "fabs";
1598 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001599 case ISD::FSQRT: return "fsqrt";
1600 case ISD::FSIN: return "fsin";
1601 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001602
1603 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001604 case ISD::ADD: return "add";
1605 case ISD::SUB: return "sub";
1606 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001607 case ISD::MULHU: return "mulhu";
1608 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001609 case ISD::SDIV: return "sdiv";
1610 case ISD::UDIV: return "udiv";
1611 case ISD::SREM: return "srem";
1612 case ISD::UREM: return "urem";
1613 case ISD::AND: return "and";
1614 case ISD::OR: return "or";
1615 case ISD::XOR: return "xor";
1616 case ISD::SHL: return "shl";
1617 case ISD::SRA: return "sra";
1618 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001619 case ISD::FADD: return "fadd";
1620 case ISD::FSUB: return "fsub";
1621 case ISD::FMUL: return "fmul";
1622 case ISD::FDIV: return "fdiv";
1623 case ISD::FREM: return "frem";
1624
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001625 case ISD::SETCC: return "setcc";
1626 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001627 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001628 case ISD::ADD_PARTS: return "add_parts";
1629 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001630 case ISD::SHL_PARTS: return "shl_parts";
1631 case ISD::SRA_PARTS: return "sra_parts";
1632 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001633
Chris Lattner7f644642005-04-28 21:44:03 +00001634 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001635 case ISD::SIGN_EXTEND: return "sign_extend";
1636 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001637 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001638 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001639 case ISD::TRUNCATE: return "truncate";
1640 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001641 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001642 case ISD::FP_EXTEND: return "fp_extend";
1643
1644 case ISD::SINT_TO_FP: return "sint_to_fp";
1645 case ISD::UINT_TO_FP: return "uint_to_fp";
1646 case ISD::FP_TO_SINT: return "fp_to_sint";
1647 case ISD::FP_TO_UINT: return "fp_to_uint";
1648
1649 // Control flow instructions
1650 case ISD::BR: return "br";
1651 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001652 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001653 case ISD::BR_CC: return "br_cc";
1654 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001655 case ISD::RET: return "ret";
1656 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001657 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001658 case ISD::CALLSEQ_START: return "callseq_start";
1659 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001660
1661 // Other operators
1662 case ISD::LOAD: return "load";
1663 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001664 case ISD::EXTLOAD: return "extload";
1665 case ISD::SEXTLOAD: return "sextload";
1666 case ISD::ZEXTLOAD: return "zextload";
1667 case ISD::TRUNCSTORE: return "truncstore";
1668
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001669 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1670 case ISD::EXTRACT_ELEMENT: return "extract_element";
1671 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001672 case ISD::MEMSET: return "memset";
1673 case ISD::MEMCPY: return "memcpy";
1674 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001675
Chris Lattner276260b2005-05-11 04:50:30 +00001676 // Bit counting
1677 case ISD::CTPOP: return "ctpop";
1678 case ISD::CTTZ: return "cttz";
1679 case ISD::CTLZ: return "ctlz";
1680
1681 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001682 case ISD::READPORT: return "readport";
1683 case ISD::WRITEPORT: return "writeport";
1684 case ISD::READIO: return "readio";
1685 case ISD::WRITEIO: return "writeio";
1686
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001687 case ISD::CONDCODE:
1688 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001689 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001690 case ISD::SETOEQ: return "setoeq";
1691 case ISD::SETOGT: return "setogt";
1692 case ISD::SETOGE: return "setoge";
1693 case ISD::SETOLT: return "setolt";
1694 case ISD::SETOLE: return "setole";
1695 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001696
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001697 case ISD::SETO: return "seto";
1698 case ISD::SETUO: return "setuo";
1699 case ISD::SETUEQ: return "setue";
1700 case ISD::SETUGT: return "setugt";
1701 case ISD::SETUGE: return "setuge";
1702 case ISD::SETULT: return "setult";
1703 case ISD::SETULE: return "setule";
1704 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001705
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001706 case ISD::SETEQ: return "seteq";
1707 case ISD::SETGT: return "setgt";
1708 case ISD::SETGE: return "setge";
1709 case ISD::SETLT: return "setlt";
1710 case ISD::SETLE: return "setle";
1711 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001712 }
1713 }
1714}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001715
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001716void SDNode::dump() const { dump(0); }
1717void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001718 std::cerr << (void*)this << ": ";
1719
1720 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1721 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001722 if (getValueType(i) == MVT::Other)
1723 std::cerr << "ch";
1724 else
1725 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001726 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001727 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001728
1729 std::cerr << " ";
1730 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1731 if (i) std::cerr << ", ";
1732 std::cerr << (void*)getOperand(i).Val;
1733 if (unsigned RN = getOperand(i).ResNo)
1734 std::cerr << ":" << RN;
1735 }
1736
1737 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1738 std::cerr << "<" << CSDN->getValue() << ">";
1739 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1740 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001741 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001742 dyn_cast<GlobalAddressSDNode>(this)) {
1743 std::cerr << "<";
1744 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001745 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001746 std::cerr << "<" << FIDN->getIndex() << ">";
1747 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00001748 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001749 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750 std::cerr << "<";
1751 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1752 if (LBB)
1753 std::cerr << LBB->getName() << " ";
1754 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00001755 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00001756 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
1757 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
1758 } else {
1759 std::cerr << " #" << R->getReg();
1760 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001761 } else if (const ExternalSymbolSDNode *ES =
1762 dyn_cast<ExternalSymbolSDNode>(this)) {
1763 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001764 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1765 if (M->getValue())
1766 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1767 else
1768 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00001769 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
1770 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001771 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001772}
1773
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001774static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001775 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1776 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001777 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001778 else
1779 std::cerr << "\n" << std::string(indent+2, ' ')
1780 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001781
Chris Lattnerea946cd2005-01-09 20:38:33 +00001782
1783 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001784 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001785}
1786
Chris Lattnerc3aae252005-01-07 07:46:32 +00001787void SelectionDAG::dump() const {
1788 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001789 std::vector<SDNode*> Nodes(AllNodes);
1790 std::sort(Nodes.begin(), Nodes.end());
1791
1792 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001793 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001794 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001795 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001796
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001797 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001798
Chris Lattnerc3aae252005-01-07 07:46:32 +00001799 std::cerr << "\n\n";
1800}
1801