blob: 7d3cddb656839b3ffa975c41acf81d2a1ab80869 [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;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000315 case ISD::TargetExternalSymbol:
316 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
317 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000318 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000319 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000320 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
321 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000322 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
324 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000325 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000326 case ISD::SRCVALUE: {
327 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000328 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000329 break;
330 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000331 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000332 Erased = Loads.erase(std::make_pair(N->getOperand(1),
333 std::make_pair(N->getOperand(0),
334 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000335 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000336 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000337 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000338 if (N->getNumOperands() == 0) {
339 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
340 N->getValueType(0)));
341 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000342 Erased =
343 UnaryOps.erase(std::make_pair(N->getOpcode(),
344 std::make_pair(N->getOperand(0),
345 N->getValueType(0))));
346 } else if (N->getNumOperands() == 2) {
347 Erased =
348 BinaryOps.erase(std::make_pair(N->getOpcode(),
349 std::make_pair(N->getOperand(0),
350 N->getOperand(1))));
351 } else {
352 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
353 Erased =
354 OneResultNodes.erase(std::make_pair(N->getOpcode(),
355 std::make_pair(N->getValueType(0),
356 Ops)));
357 }
Chris Lattner385328c2005-05-14 07:42:29 +0000358 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000359 // Remove the node from the ArbitraryNodes map.
360 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
361 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000362 Erased =
363 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
364 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000365 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000366 break;
367 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000368#ifndef NDEBUG
369 // Verify that the node was actually in one of the CSE maps, unless it has a
370 // flag result (which cannot be CSE'd) or is one of the special cases that are
371 // not subject to CSE.
372 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
373 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000374 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000375
376 N->dump();
377 assert(0 && "Node is not in map!");
378 }
379#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000380}
381
Chris Lattner8b8749f2005-08-17 19:00:20 +0000382/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
383/// has been taken out and modified in some way. If the specified node already
384/// exists in the CSE maps, do not modify the maps, but return the existing node
385/// instead. If it doesn't exist, add it and return null.
386///
387SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
388 assert(N->getNumOperands() && "This is a leaf node!");
389 if (N->getOpcode() == ISD::LOAD) {
390 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
391 std::make_pair(N->getOperand(0),
392 N->getValueType(0)))];
393 if (L) return L;
394 L = N;
Chris Lattner95038592005-10-05 06:35:28 +0000395 } else if (N->getOpcode() == ISD::HANDLENODE) {
396 return 0; // never add it.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000397 } else if (N->getNumOperands() == 1) {
398 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
399 std::make_pair(N->getOperand(0),
400 N->getValueType(0)))];
401 if (U) return U;
402 U = N;
403 } else if (N->getNumOperands() == 2) {
404 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
405 std::make_pair(N->getOperand(0),
406 N->getOperand(1)))];
407 if (B) return B;
408 B = N;
409 } else if (N->getNumValues() == 1) {
410 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
411 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getValueType(0), Ops))];
413 if (ORN) return ORN;
414 ORN = N;
415 } else {
416 // Remove the node from the ArbitraryNodes map.
417 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
418 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
419 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
420 std::make_pair(RV, Ops))];
421 if (AN) return AN;
422 AN = N;
423 }
424 return 0;
425
426}
427
428
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000429
Chris Lattner78ec3112003-08-11 14:57:33 +0000430SelectionDAG::~SelectionDAG() {
431 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
432 delete AllNodes[i];
433}
434
Chris Lattner0f2287b2005-04-13 02:38:18 +0000435SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000436 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000437 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000438 return getNode(ISD::AND, Op.getValueType(), Op,
439 getConstant(Imm, Op.getValueType()));
440}
441
Chris Lattnerc3aae252005-01-07 07:46:32 +0000442SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
443 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
444 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000445 if (VT != MVT::i64)
446 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000447
Chris Lattnerc3aae252005-01-07 07:46:32 +0000448 SDNode *&N = Constants[std::make_pair(Val, VT)];
449 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000450 N = new ConstantSDNode(false, Val, VT);
451 AllNodes.push_back(N);
452 return SDOperand(N, 0);
453}
454
455SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
456 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
457 // Mask out any bits that are not valid for this constant.
458 if (VT != MVT::i64)
459 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
460
461 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
462 if (N) return SDOperand(N, 0);
463 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000464 AllNodes.push_back(N);
465 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000466}
467
Chris Lattnerc3aae252005-01-07 07:46:32 +0000468SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
469 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
470 if (VT == MVT::f32)
471 Val = (float)Val; // Mask out extra precision.
472
Chris Lattnerd8658612005-02-17 20:17:32 +0000473 // Do the map lookup using the actual bit pattern for the floating point
474 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
475 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000476 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000477 if (N) return SDOperand(N, 0);
478 N = new ConstantFPSDNode(Val, VT);
479 AllNodes.push_back(N);
480 return SDOperand(N, 0);
481}
482
483
484
485SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
486 MVT::ValueType VT) {
487 SDNode *&N = GlobalValues[GV];
488 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000489 N = new GlobalAddressSDNode(false, GV, VT);
490 AllNodes.push_back(N);
491 return SDOperand(N, 0);
492}
493
494SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
495 MVT::ValueType VT) {
496 SDNode *&N = TargetGlobalValues[GV];
497 if (N) return SDOperand(N, 0);
498 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000499 AllNodes.push_back(N);
500 return SDOperand(N, 0);
501}
502
503SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
504 SDNode *&N = FrameIndices[FI];
505 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000506 N = new FrameIndexSDNode(FI, VT, false);
507 AllNodes.push_back(N);
508 return SDOperand(N, 0);
509}
510
511SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
512 SDNode *&N = TargetFrameIndices[FI];
513 if (N) return SDOperand(N, 0);
514 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000515 AllNodes.push_back(N);
516 return SDOperand(N, 0);
517}
518
Chris Lattner5839bf22005-08-26 17:15:30 +0000519SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
520 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000521 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000522 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000523 AllNodes.push_back(N);
524 return SDOperand(N, 0);
525}
526
Chris Lattner5839bf22005-08-26 17:15:30 +0000527SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
528 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000529 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000530 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000531 AllNodes.push_back(N);
532 return SDOperand(N, 0);
533}
534
535SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
536 SDNode *&N = BBNodes[MBB];
537 if (N) return SDOperand(N, 0);
538 N = new BasicBlockSDNode(MBB);
539 AllNodes.push_back(N);
540 return SDOperand(N, 0);
541}
542
Chris Lattner15e4b012005-07-10 00:07:11 +0000543SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
544 if ((unsigned)VT >= ValueTypeNodes.size())
545 ValueTypeNodes.resize(VT+1);
546 if (ValueTypeNodes[VT] == 0) {
547 ValueTypeNodes[VT] = new VTSDNode(VT);
548 AllNodes.push_back(ValueTypeNodes[VT]);
549 }
550
551 return SDOperand(ValueTypeNodes[VT], 0);
552}
553
Chris Lattnerc3aae252005-01-07 07:46:32 +0000554SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
555 SDNode *&N = ExternalSymbols[Sym];
556 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000557 N = new ExternalSymbolSDNode(false, Sym, VT);
558 AllNodes.push_back(N);
559 return SDOperand(N, 0);
560}
561
562SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
563 SDNode *&N = TargetExternalSymbols[Sym];
564 if (N) return SDOperand(N, 0);
565 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000566 AllNodes.push_back(N);
567 return SDOperand(N, 0);
568}
569
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000570SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
571 if ((unsigned)Cond >= CondCodeNodes.size())
572 CondCodeNodes.resize(Cond+1);
573
Chris Lattner079a27a2005-08-09 20:40:02 +0000574 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000575 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000576 AllNodes.push_back(CondCodeNodes[Cond]);
577 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000578 return SDOperand(CondCodeNodes[Cond], 0);
579}
580
Chris Lattner0fdd7682005-08-30 22:38:38 +0000581SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
582 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
583 if (!Reg) {
584 Reg = new RegisterSDNode(RegNo, VT);
585 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000586 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000587 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000588}
589
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000590SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
591 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 // These setcc operations always fold.
593 switch (Cond) {
594 default: break;
595 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000596 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000597 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000598 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000599 }
600
Chris Lattner67255a12005-04-07 18:14:58 +0000601 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
602 uint64_t C2 = N2C->getValue();
603 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
604 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000605
Chris Lattnerc3aae252005-01-07 07:46:32 +0000606 // Sign extend the operands if required
607 if (ISD::isSignedIntSetCC(Cond)) {
608 C1 = N1C->getSignExtended();
609 C2 = N2C->getSignExtended();
610 }
611
612 switch (Cond) {
613 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000614 case ISD::SETEQ: return getConstant(C1 == C2, VT);
615 case ISD::SETNE: return getConstant(C1 != C2, VT);
616 case ISD::SETULT: return getConstant(C1 < C2, VT);
617 case ISD::SETUGT: return getConstant(C1 > C2, VT);
618 case ISD::SETULE: return getConstant(C1 <= C2, VT);
619 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
620 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
621 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
622 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
623 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 }
Chris Lattner24673922005-04-07 18:58:54 +0000625 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000626 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000627 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
628 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
629
630 // If the comparison constant has bits in the upper part, the
631 // zero-extended value could never match.
632 if (C2 & (~0ULL << InSize)) {
633 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
634 switch (Cond) {
635 case ISD::SETUGT:
636 case ISD::SETUGE:
637 case ISD::SETEQ: return getConstant(0, VT);
638 case ISD::SETULT:
639 case ISD::SETULE:
640 case ISD::SETNE: return getConstant(1, VT);
641 case ISD::SETGT:
642 case ISD::SETGE:
643 // True if the sign bit of C2 is set.
644 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
645 case ISD::SETLT:
646 case ISD::SETLE:
647 // True if the sign bit of C2 isn't set.
648 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
649 default:
650 break;
651 }
652 }
653
654 // Otherwise, we can perform the comparison with the low bits.
655 switch (Cond) {
656 case ISD::SETEQ:
657 case ISD::SETNE:
658 case ISD::SETUGT:
659 case ISD::SETUGE:
660 case ISD::SETULT:
661 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000662 return getSetCC(VT, N1.getOperand(0),
663 getConstant(C2, N1.getOperand(0).getValueType()),
664 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000665 default:
666 break; // todo, be more careful with signed comparisons
667 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000668 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
669 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
670 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
671 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
672 MVT::ValueType ExtDstTy = N1.getValueType();
673 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000674
Chris Lattner7b2880c2005-08-24 22:44:39 +0000675 // If the extended part has any inconsistent bits, it cannot ever
676 // compare equal. In other words, they have to be all ones or all
677 // zeros.
678 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000679 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000680 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
681 return getConstant(Cond == ISD::SETNE, VT);
682
683 // Otherwise, make this a use of a zext.
684 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000685 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000686 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000687 }
688
Chris Lattner67255a12005-04-07 18:14:58 +0000689 uint64_t MinVal, MaxVal;
690 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
691 if (ISD::isSignedIntSetCC(Cond)) {
692 MinVal = 1ULL << (OperandBitSize-1);
693 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
694 MaxVal = ~0ULL >> (65-OperandBitSize);
695 else
696 MaxVal = 0;
697 } else {
698 MinVal = 0;
699 MaxVal = ~0ULL >> (64-OperandBitSize);
700 }
701
702 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
703 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
704 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
705 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000706 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
707 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000708 }
709
710 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
711 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
712 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000713 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
714 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000715 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000716
Nate Begeman72ea2812005-04-14 08:56:52 +0000717 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
718 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000719
Nate Begeman72ea2812005-04-14 08:56:52 +0000720 // Canonicalize setgt X, Min --> setne X, Min
721 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000722 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000723
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000724 // If we have setult X, 1, turn it into seteq X, 0
725 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000726 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
727 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000728 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000729 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000730 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
731 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000732
733 // If we have "setcc X, C1", check to see if we can shrink the immediate
734 // by changing cc.
735
736 // SETUGT X, SINTMAX -> SETLT X, 0
737 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
738 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000739 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000740
741 // FIXME: Implement the rest of these.
742
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000743
744 // Fold bit comparisons when we can.
745 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
746 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
747 if (ConstantSDNode *AndRHS =
748 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
749 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
750 // Perform the xform if the AND RHS is a single bit.
751 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
752 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000753 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000754 TLI.getShiftAmountTy()));
755 }
756 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
757 // (X & 8) == 8 --> (X & 8) >> 3
758 // Perform the xform if C2 is a single bit.
759 if ((C2 & (C2-1)) == 0) {
760 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000761 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000762 }
763 }
764 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000765 }
Chris Lattner67255a12005-04-07 18:14:58 +0000766 } else if (isa<ConstantSDNode>(N1.Val)) {
767 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000768 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000769 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000770
771 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
772 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
773 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000774
Chris Lattnerc3aae252005-01-07 07:46:32 +0000775 switch (Cond) {
776 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000777 case ISD::SETEQ: return getConstant(C1 == C2, VT);
778 case ISD::SETNE: return getConstant(C1 != C2, VT);
779 case ISD::SETLT: return getConstant(C1 < C2, VT);
780 case ISD::SETGT: return getConstant(C1 > C2, VT);
781 case ISD::SETLE: return getConstant(C1 <= C2, VT);
782 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783 }
784 } else {
785 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000786 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000787 }
788
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000789 // Could not fold it.
790 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000791}
792
Chris Lattnerc3aae252005-01-07 07:46:32 +0000793/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000794///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000795SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000796 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
797 if (!N) {
798 N = new SDNode(Opcode, VT);
799 AllNodes.push_back(N);
800 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000801 return SDOperand(N, 0);
802}
803
Chris Lattnerc3aae252005-01-07 07:46:32 +0000804SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
805 SDOperand Operand) {
806 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
807 uint64_t Val = C->getValue();
808 switch (Opcode) {
809 default: break;
810 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000811 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
813 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000814 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
815 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000816 }
817 }
818
819 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
820 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000821 case ISD::FNEG:
822 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823 case ISD::FP_ROUND:
824 case ISD::FP_EXTEND:
825 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000826 case ISD::FP_TO_SINT:
827 return getConstant((int64_t)C->getValue(), VT);
828 case ISD::FP_TO_UINT:
829 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000830 }
831
832 unsigned OpOpcode = Operand.Val->getOpcode();
833 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000834 case ISD::TokenFactor:
835 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836 case ISD::SIGN_EXTEND:
837 if (Operand.getValueType() == VT) return Operand; // noop extension
838 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
839 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
840 break;
841 case ISD::ZERO_EXTEND:
842 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000843 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000844 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000845 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000846 case ISD::ANY_EXTEND:
847 if (Operand.getValueType() == VT) return Operand; // noop extension
848 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
849 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
850 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
851 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000852 case ISD::TRUNCATE:
853 if (Operand.getValueType() == VT) return Operand; // noop truncate
854 if (OpOpcode == ISD::TRUNCATE)
855 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000856 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
857 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000858 // If the source is smaller than the dest, we still need an extend.
859 if (Operand.Val->getOperand(0).getValueType() < VT)
860 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
861 else if (Operand.Val->getOperand(0).getValueType() > VT)
862 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
863 else
864 return Operand.Val->getOperand(0);
865 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000866 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000867 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000868 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
869 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000870 Operand.Val->getOperand(0));
871 if (OpOpcode == ISD::FNEG) // --X -> X
872 return Operand.Val->getOperand(0);
873 break;
874 case ISD::FABS:
875 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
876 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
877 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000878 }
879
Chris Lattner43247a12005-08-25 19:12:10 +0000880 SDNode *N;
881 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
882 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000883 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000884 E = N = new SDNode(Opcode, Operand);
885 } else {
886 N = new SDNode(Opcode, Operand);
887 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000888 N->setValueTypes(VT);
889 AllNodes.push_back(N);
890 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000891}
892
Chris Lattner36019aa2005-04-18 03:48:41 +0000893
894
Chris Lattnerc3aae252005-01-07 07:46:32 +0000895SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
896 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000897#ifndef NDEBUG
898 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000899 case ISD::TokenFactor:
900 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
901 N2.getValueType() == MVT::Other && "Invalid token factor!");
902 break;
Chris Lattner76365122005-01-16 02:23:22 +0000903 case ISD::AND:
904 case ISD::OR:
905 case ISD::XOR:
906 case ISD::UDIV:
907 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000908 case ISD::MULHU:
909 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000910 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
911 // fall through
912 case ISD::ADD:
913 case ISD::SUB:
914 case ISD::MUL:
915 case ISD::SDIV:
916 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000917 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
918 // fall through.
919 case ISD::FADD:
920 case ISD::FSUB:
921 case ISD::FMUL:
922 case ISD::FDIV:
923 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000924 assert(N1.getValueType() == N2.getValueType() &&
925 N1.getValueType() == VT && "Binary operator types must match!");
926 break;
927
928 case ISD::SHL:
929 case ISD::SRA:
930 case ISD::SRL:
931 assert(VT == N1.getValueType() &&
932 "Shift operators return type must be the same as their first arg");
933 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000934 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000935 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000936 case ISD::FP_ROUND_INREG: {
937 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
938 assert(VT == N1.getValueType() && "Not an inreg round!");
939 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
940 "Cannot FP_ROUND_INREG integer types");
941 assert(EVT <= VT && "Not rounding down!");
942 break;
943 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000944 case ISD::AssertSext:
945 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +0000946 case ISD::SIGN_EXTEND_INREG: {
947 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
948 assert(VT == N1.getValueType() && "Not an inreg extend!");
949 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
950 "Cannot *_EXTEND_INREG FP types");
951 assert(EVT <= VT && "Not extending!");
952 }
953
Chris Lattner76365122005-01-16 02:23:22 +0000954 default: break;
955 }
956#endif
957
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
959 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
960 if (N1C) {
961 if (N2C) {
962 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
963 switch (Opcode) {
964 case ISD::ADD: return getConstant(C1 + C2, VT);
965 case ISD::SUB: return getConstant(C1 - C2, VT);
966 case ISD::MUL: return getConstant(C1 * C2, VT);
967 case ISD::UDIV:
968 if (C2) return getConstant(C1 / C2, VT);
969 break;
970 case ISD::UREM :
971 if (C2) return getConstant(C1 % C2, VT);
972 break;
973 case ISD::SDIV :
974 if (C2) return getConstant(N1C->getSignExtended() /
975 N2C->getSignExtended(), VT);
976 break;
977 case ISD::SREM :
978 if (C2) return getConstant(N1C->getSignExtended() %
979 N2C->getSignExtended(), VT);
980 break;
981 case ISD::AND : return getConstant(C1 & C2, VT);
982 case ISD::OR : return getConstant(C1 | C2, VT);
983 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +0000984 case ISD::SHL : return getConstant(C1 << C2, VT);
985 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000986 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 default: break;
988 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000989 } else { // Cannonicalize constant to RHS if commutative
990 if (isCommutativeBinOp(Opcode)) {
991 std::swap(N1C, N2C);
992 std::swap(N1, N2);
993 }
994 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000995 }
996
997 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
998 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +0000999 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000 if (N2CFP) {
1001 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1002 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001003 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1004 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1005 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1006 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001007 if (C2) return getConstantFP(C1 / C2, VT);
1008 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001009 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 if (C2) return getConstantFP(fmod(C1, C2), VT);
1011 break;
1012 default: break;
1013 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001014 } else { // Cannonicalize constant to RHS if commutative
1015 if (isCommutativeBinOp(Opcode)) {
1016 std::swap(N1CFP, N2CFP);
1017 std::swap(N1, N2);
1018 }
1019 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001020 }
1021
Chris Lattnerc3aae252005-01-07 07:46:32 +00001022 // Finally, fold operations that do not require constants.
1023 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001024 case ISD::FP_ROUND_INREG:
1025 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1026 break;
1027 case ISD::SIGN_EXTEND_INREG: {
1028 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1029 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001030 break;
1031 }
1032
Nate Begemaneea805e2005-04-13 21:23:31 +00001033 // FIXME: figure out how to safely handle things like
1034 // int foo(int x) { return 1 << (x & 255); }
1035 // int bar() { return foo(256); }
1036#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001037 case ISD::SHL:
1038 case ISD::SRL:
1039 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001040 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001041 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001042 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001043 else if (N2.getOpcode() == ISD::AND)
1044 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1045 // If the and is only masking out bits that cannot effect the shift,
1046 // eliminate the and.
1047 unsigned NumBits = MVT::getSizeInBits(VT);
1048 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1049 return getNode(Opcode, VT, N1, N2.getOperand(0));
1050 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001051 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001052#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001053 }
1054
Chris Lattner27e9b412005-05-11 18:57:39 +00001055 // Memoize this node if possible.
1056 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001057 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1058 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001059 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1060 if (BON) return SDOperand(BON, 0);
1061
1062 BON = N = new SDNode(Opcode, N1, N2);
1063 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001064 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001065 }
1066
Chris Lattner3e011362005-05-14 07:45:46 +00001067 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001068 AllNodes.push_back(N);
1069 return SDOperand(N, 0);
1070}
1071
Chris Lattner88de6e72005-05-12 00:17:04 +00001072// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001073// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001074void SDNode::setAdjCallChain(SDOperand N) {
1075 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001076 assert((getOpcode() == ISD::CALLSEQ_START ||
1077 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001078
1079 Operands[0].Val->removeUser(this);
1080 Operands[0] = N;
1081 N.Val->Uses.push_back(this);
1082}
1083
1084
1085
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001087 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001088 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001089 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1090 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001091 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001092
1093 // Loads have a token chain.
1094 N->setValueTypes(VT, MVT::Other);
1095 AllNodes.push_back(N);
1096 return SDOperand(N, 0);
1097}
1098
Chris Lattner5f056bf2005-07-10 01:55:33 +00001099
1100SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1101 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1102 MVT::ValueType EVT) {
1103 std::vector<SDOperand> Ops;
1104 Ops.reserve(4);
1105 Ops.push_back(Chain);
1106 Ops.push_back(Ptr);
1107 Ops.push_back(SV);
1108 Ops.push_back(getValueType(EVT));
1109 std::vector<MVT::ValueType> VTs;
1110 VTs.reserve(2);
1111 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1112 return getNode(Opcode, VTs, Ops);
1113}
1114
Chris Lattnerc3aae252005-01-07 07:46:32 +00001115SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1116 SDOperand N1, SDOperand N2, SDOperand N3) {
1117 // Perform various simplifications.
1118 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1119 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1120 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1121 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001122 case ISD::SETCC: {
1123 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001124 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001125 if (Simp.Val) return Simp;
1126 break;
1127 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128 case ISD::SELECT:
1129 if (N1C)
1130 if (N1C->getValue())
1131 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001132 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001133 return N3; // select false, X, Y -> Y
1134
1135 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001136 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001137 case ISD::BRCOND:
1138 if (N2C)
1139 if (N2C->getValue()) // Unconditional branch
1140 return getNode(ISD::BR, MVT::Other, N1, N3);
1141 else
1142 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001143 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001144 }
1145
Chris Lattner385328c2005-05-14 07:42:29 +00001146 std::vector<SDOperand> Ops;
1147 Ops.reserve(3);
1148 Ops.push_back(N1);
1149 Ops.push_back(N2);
1150 Ops.push_back(N3);
1151
Chris Lattner43247a12005-08-25 19:12:10 +00001152 // Memoize node if it doesn't produce a flag.
1153 SDNode *N;
1154 if (VT != MVT::Flag) {
1155 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1156 if (E) return SDOperand(E, 0);
1157 E = N = new SDNode(Opcode, N1, N2, N3);
1158 } else {
1159 N = new SDNode(Opcode, N1, N2, N3);
1160 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001161 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 AllNodes.push_back(N);
1163 return SDOperand(N, 0);
1164}
1165
1166SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001167 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001168 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001169 std::vector<SDOperand> Ops;
1170 Ops.reserve(4);
1171 Ops.push_back(N1);
1172 Ops.push_back(N2);
1173 Ops.push_back(N3);
1174 Ops.push_back(N4);
1175 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001176}
1177
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001178SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1179 SDOperand N1, SDOperand N2, SDOperand N3,
1180 SDOperand N4, SDOperand N5) {
1181 std::vector<SDOperand> Ops;
1182 Ops.reserve(5);
1183 Ops.push_back(N1);
1184 Ops.push_back(N2);
1185 Ops.push_back(N3);
1186 Ops.push_back(N4);
1187 Ops.push_back(N5);
1188 return getNode(Opcode, VT, Ops);
1189}
1190
1191
Chris Lattner0437cdd2005-05-09 04:14:13 +00001192SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001193 assert((!V || isa<PointerType>(V->getType())) &&
1194 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001195 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1196 if (N) return SDOperand(N, 0);
1197
1198 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001199 AllNodes.push_back(N);
1200 return SDOperand(N, 0);
1201}
1202
1203SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001204 std::vector<SDOperand> &Ops) {
1205 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001206 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001207 case 1: return getNode(Opcode, VT, Ops[0]);
1208 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1209 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001210 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001211 }
Chris Lattneref847df2005-04-09 03:27:28 +00001212
Chris Lattner89c34632005-05-14 06:20:26 +00001213 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001214 switch (Opcode) {
1215 default: break;
1216 case ISD::BRCONDTWOWAY:
1217 if (N1C)
1218 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001219 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001220 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001221 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001222 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001223 case ISD::BRTWOWAY_CC:
1224 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1225 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1226 "LHS and RHS of comparison must have same type!");
1227 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001228 case ISD::TRUNCSTORE: {
1229 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1230 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1231#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1232 // If this is a truncating store of a constant, convert to the desired type
1233 // and store it instead.
1234 if (isa<Constant>(Ops[0])) {
1235 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1236 if (isa<Constant>(Op))
1237 N1 = Op;
1238 }
1239 // Also for ConstantFP?
1240#endif
1241 if (Ops[0].getValueType() == EVT) // Normal store?
1242 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1243 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1244 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1245 "Can't do FP-INT conversion!");
1246 break;
1247 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001248 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001249 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001250 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1251 "LHS and RHS of condition must have same type!");
1252 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1253 "True and False arms of SelectCC must have same type!");
1254 assert(Ops[2].getValueType() == VT &&
1255 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001256 break;
1257 }
1258 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001259 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001260 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1261 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001262 break;
1263 }
Chris Lattneref847df2005-04-09 03:27:28 +00001264 }
1265
Chris Lattner385328c2005-05-14 07:42:29 +00001266 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001267 SDNode *N;
1268 if (VT != MVT::Flag) {
1269 SDNode *&E =
1270 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1271 if (E) return SDOperand(E, 0);
1272 E = N = new SDNode(Opcode, Ops);
1273 } else {
1274 N = new SDNode(Opcode, Ops);
1275 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001276 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001277 AllNodes.push_back(N);
1278 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001279}
1280
Chris Lattner89c34632005-05-14 06:20:26 +00001281SDOperand SelectionDAG::getNode(unsigned Opcode,
1282 std::vector<MVT::ValueType> &ResultTys,
1283 std::vector<SDOperand> &Ops) {
1284 if (ResultTys.size() == 1)
1285 return getNode(Opcode, ResultTys[0], Ops);
1286
Chris Lattner5f056bf2005-07-10 01:55:33 +00001287 switch (Opcode) {
1288 case ISD::EXTLOAD:
1289 case ISD::SEXTLOAD:
1290 case ISD::ZEXTLOAD: {
1291 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1292 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1293 // If they are asking for an extending load from/to the same thing, return a
1294 // normal load.
1295 if (ResultTys[0] == EVT)
1296 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1297 assert(EVT < ResultTys[0] &&
1298 "Should only be an extending load, not truncating!");
1299 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1300 "Cannot sign/zero extend a FP load!");
1301 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1302 "Cannot convert from FP to Int or Int -> FP!");
1303 break;
1304 }
1305
Chris Lattnere89083a2005-05-14 07:25:05 +00001306 // FIXME: figure out how to safely handle things like
1307 // int foo(int x) { return 1 << (x & 255); }
1308 // int bar() { return foo(256); }
1309#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001310 case ISD::SRA_PARTS:
1311 case ISD::SRL_PARTS:
1312 case ISD::SHL_PARTS:
1313 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001314 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001315 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1316 else if (N3.getOpcode() == ISD::AND)
1317 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1318 // If the and is only masking out bits that cannot effect the shift,
1319 // eliminate the and.
1320 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1321 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1322 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1323 }
1324 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001325#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001326 }
Chris Lattner89c34632005-05-14 06:20:26 +00001327
Chris Lattner43247a12005-08-25 19:12:10 +00001328 // Memoize the node unless it returns a flag.
1329 SDNode *N;
1330 if (ResultTys.back() != MVT::Flag) {
1331 SDNode *&E =
1332 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1333 if (E) return SDOperand(E, 0);
1334 E = N = new SDNode(Opcode, Ops);
1335 } else {
1336 N = new SDNode(Opcode, Ops);
1337 }
Chris Lattner89c34632005-05-14 06:20:26 +00001338 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001339 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001340 return SDOperand(N, 0);
1341}
1342
Chris Lattner149c58c2005-08-16 18:17:10 +00001343
1344/// SelectNodeTo - These are used for target selectors to *mutate* the
1345/// specified node to have the specified return type, Target opcode, and
1346/// operands. Note that target opcodes are stored as
1347/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001348void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1349 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00001350 RemoveNodeFromCSEMaps(N);
1351 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1352 N->setValueTypes(VT);
1353}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001354void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1355 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00001356 RemoveNodeFromCSEMaps(N);
1357 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1358 N->setValueTypes(VT);
1359 N->setOperands(Op1);
1360}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001361void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1362 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001363 SDOperand Op2) {
1364 RemoveNodeFromCSEMaps(N);
1365 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1366 N->setValueTypes(VT);
1367 N->setOperands(Op1, Op2);
1368}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001369void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00001370 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001371 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00001372 RemoveNodeFromCSEMaps(N);
1373 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1374 N->setValueTypes(VT1, VT2);
1375 N->setOperands(Op1, Op2);
1376}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001377void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1378 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001379 SDOperand Op2, SDOperand Op3) {
1380 RemoveNodeFromCSEMaps(N);
1381 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1382 N->setValueTypes(VT);
1383 N->setOperands(Op1, Op2, Op3);
1384}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001385void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1386 MVT::ValueType VT1, MVT::ValueType VT2,
1387 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001388 RemoveNodeFromCSEMaps(N);
1389 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1390 N->setValueTypes(VT1, VT2);
1391 N->setOperands(Op1, Op2, Op3);
1392}
1393
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001394void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1395 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00001396 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1397 RemoveNodeFromCSEMaps(N);
1398 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1399 N->setValueTypes(VT);
1400 N->setOperands(Op1, Op2, Op3, Op4);
1401}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001402void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1403 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00001404 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1405 SDOperand Op5) {
1406 RemoveNodeFromCSEMaps(N);
1407 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1408 N->setValueTypes(VT);
1409 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1410}
Chris Lattner149c58c2005-08-16 18:17:10 +00001411
Chris Lattner8b8749f2005-08-17 19:00:20 +00001412/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1413/// This can cause recursive merging of nodes in the DAG.
1414///
Chris Lattner8b52f212005-08-26 18:36:28 +00001415/// This version assumes From/To have a single result value.
1416///
Chris Lattner1e111c72005-09-07 05:37:01 +00001417void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1418 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001419 SDNode *From = FromN.Val, *To = ToN.Val;
1420 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1421 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001422 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001423
Chris Lattner8b8749f2005-08-17 19:00:20 +00001424 while (!From->use_empty()) {
1425 // Process users until they are all gone.
1426 SDNode *U = *From->use_begin();
1427
1428 // This node is about to morph, remove its old self from the CSE maps.
1429 RemoveNodeFromCSEMaps(U);
1430
1431 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1432 if (U->getOperand(i).Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001433 From->removeUser(U);
1434 U->Operands[i].Val = To;
1435 To->addUser(U);
1436 }
1437
1438 // Now that we have modified U, add it back to the CSE maps. If it already
1439 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001440 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1441 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001442 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001443 if (Deleted) Deleted->push_back(U);
1444 DeleteNodeNotInCSEMaps(U);
1445 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001446 }
1447}
1448
1449/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1450/// This can cause recursive merging of nodes in the DAG.
1451///
1452/// This version assumes From/To have matching types and numbers of result
1453/// values.
1454///
Chris Lattner1e111c72005-09-07 05:37:01 +00001455void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1456 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001457 assert(From != To && "Cannot replace uses of with self");
1458 assert(From->getNumValues() == To->getNumValues() &&
1459 "Cannot use this version of ReplaceAllUsesWith!");
1460 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001461 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001462 return;
1463 }
1464
1465 while (!From->use_empty()) {
1466 // Process users until they are all gone.
1467 SDNode *U = *From->use_begin();
1468
1469 // This node is about to morph, remove its old self from the CSE maps.
1470 RemoveNodeFromCSEMaps(U);
1471
1472 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1473 if (U->getOperand(i).Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001474 From->removeUser(U);
1475 U->Operands[i].Val = To;
1476 To->addUser(U);
1477 }
1478
1479 // Now that we have modified U, add it back to the CSE maps. If it already
1480 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001481 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1482 ReplaceAllUsesWith(U, Existing, Deleted);
1483 // U is now dead.
1484 if (Deleted) Deleted->push_back(U);
1485 DeleteNodeNotInCSEMaps(U);
1486 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001487 }
1488}
1489
Chris Lattner8b52f212005-08-26 18:36:28 +00001490/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1491/// This can cause recursive merging of nodes in the DAG.
1492///
1493/// This version can replace From with any result values. To must match the
1494/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001495void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001496 const std::vector<SDOperand> &To,
1497 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001498 assert(From->getNumValues() == To.size() &&
1499 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001500 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001501 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001502 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001503 return;
1504 }
1505
1506 while (!From->use_empty()) {
1507 // Process users until they are all gone.
1508 SDNode *U = *From->use_begin();
1509
1510 // This node is about to morph, remove its old self from the CSE maps.
1511 RemoveNodeFromCSEMaps(U);
1512
1513 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1514 if (U->getOperand(i).Val == From) {
1515 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001516 From->removeUser(U);
1517 U->Operands[i] = ToOp;
1518 ToOp.Val->addUser(U);
1519 }
1520
1521 // Now that we have modified U, add it back to the CSE maps. If it already
1522 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001523 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1524 ReplaceAllUsesWith(U, Existing, Deleted);
1525 // U is now dead.
1526 if (Deleted) Deleted->push_back(U);
1527 DeleteNodeNotInCSEMaps(U);
1528 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001529 }
1530}
1531
1532
Jim Laskey58b968b2005-08-17 20:08:02 +00001533//===----------------------------------------------------------------------===//
1534// SDNode Class
1535//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001536
Chris Lattner5c884562005-01-12 18:37:47 +00001537/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1538/// indicated value. This method ignores uses of other values defined by this
1539/// operation.
1540bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1541 assert(Value < getNumValues() && "Bad value!");
1542
1543 // If there is only one value, this is easy.
1544 if (getNumValues() == 1)
1545 return use_size() == NUses;
1546 if (Uses.size() < NUses) return false;
1547
1548 SDOperand TheValue(this, Value);
1549
1550 std::set<SDNode*> UsersHandled;
1551
1552 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1553 UI != E; ++UI) {
1554 SDNode *User = *UI;
1555 if (User->getNumOperands() == 1 ||
1556 UsersHandled.insert(User).second) // First time we've seen this?
1557 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1558 if (User->getOperand(i) == TheValue) {
1559 if (NUses == 0)
1560 return false; // too many uses
1561 --NUses;
1562 }
1563 }
1564
1565 // Found exactly the right number of uses?
1566 return NUses == 0;
1567}
1568
1569
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001570const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001571 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001572 default:
1573 if (getOpcode() < ISD::BUILTIN_OP_END)
1574 return "<<Unknown DAG Node>>";
1575 else {
1576 if (G)
1577 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001578 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1579 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001580 return "<<Unknown Target Node>>";
1581 }
1582
Andrew Lenharth95762122005-03-31 21:24:06 +00001583 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001584 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001585 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001586 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001587 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001588 case ISD::AssertSext: return "AssertSext";
1589 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001590 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001591 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001592 case ISD::ConstantFP: return "ConstantFP";
1593 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001594 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001595 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001596 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001597 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001598 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001599 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001600 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001601 case ISD::ConstantPool: return "ConstantPool";
1602 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001603 case ISD::CopyToReg: return "CopyToReg";
1604 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001605 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001606 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001607
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001608 // Unary operators
1609 case ISD::FABS: return "fabs";
1610 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001611 case ISD::FSQRT: return "fsqrt";
1612 case ISD::FSIN: return "fsin";
1613 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001614
1615 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001616 case ISD::ADD: return "add";
1617 case ISD::SUB: return "sub";
1618 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001619 case ISD::MULHU: return "mulhu";
1620 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001621 case ISD::SDIV: return "sdiv";
1622 case ISD::UDIV: return "udiv";
1623 case ISD::SREM: return "srem";
1624 case ISD::UREM: return "urem";
1625 case ISD::AND: return "and";
1626 case ISD::OR: return "or";
1627 case ISD::XOR: return "xor";
1628 case ISD::SHL: return "shl";
1629 case ISD::SRA: return "sra";
1630 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001631 case ISD::FADD: return "fadd";
1632 case ISD::FSUB: return "fsub";
1633 case ISD::FMUL: return "fmul";
1634 case ISD::FDIV: return "fdiv";
1635 case ISD::FREM: return "frem";
1636
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001637 case ISD::SETCC: return "setcc";
1638 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001639 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001640 case ISD::ADD_PARTS: return "add_parts";
1641 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001642 case ISD::SHL_PARTS: return "shl_parts";
1643 case ISD::SRA_PARTS: return "sra_parts";
1644 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001645
Chris Lattner7f644642005-04-28 21:44:03 +00001646 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001647 case ISD::SIGN_EXTEND: return "sign_extend";
1648 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001649 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001650 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001651 case ISD::TRUNCATE: return "truncate";
1652 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001653 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001654 case ISD::FP_EXTEND: return "fp_extend";
1655
1656 case ISD::SINT_TO_FP: return "sint_to_fp";
1657 case ISD::UINT_TO_FP: return "uint_to_fp";
1658 case ISD::FP_TO_SINT: return "fp_to_sint";
1659 case ISD::FP_TO_UINT: return "fp_to_uint";
1660
1661 // Control flow instructions
1662 case ISD::BR: return "br";
1663 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001664 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001665 case ISD::BR_CC: return "br_cc";
1666 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001667 case ISD::RET: return "ret";
1668 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001669 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001670 case ISD::CALLSEQ_START: return "callseq_start";
1671 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001672
1673 // Other operators
1674 case ISD::LOAD: return "load";
1675 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001676 case ISD::EXTLOAD: return "extload";
1677 case ISD::SEXTLOAD: return "sextload";
1678 case ISD::ZEXTLOAD: return "zextload";
1679 case ISD::TRUNCSTORE: return "truncstore";
1680
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001681 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1682 case ISD::EXTRACT_ELEMENT: return "extract_element";
1683 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001684 case ISD::MEMSET: return "memset";
1685 case ISD::MEMCPY: return "memcpy";
1686 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001687
Chris Lattner276260b2005-05-11 04:50:30 +00001688 // Bit counting
1689 case ISD::CTPOP: return "ctpop";
1690 case ISD::CTTZ: return "cttz";
1691 case ISD::CTLZ: return "ctlz";
1692
1693 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001694 case ISD::READPORT: return "readport";
1695 case ISD::WRITEPORT: return "writeport";
1696 case ISD::READIO: return "readio";
1697 case ISD::WRITEIO: return "writeio";
1698
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001699 case ISD::CONDCODE:
1700 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001701 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001702 case ISD::SETOEQ: return "setoeq";
1703 case ISD::SETOGT: return "setogt";
1704 case ISD::SETOGE: return "setoge";
1705 case ISD::SETOLT: return "setolt";
1706 case ISD::SETOLE: return "setole";
1707 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001708
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001709 case ISD::SETO: return "seto";
1710 case ISD::SETUO: return "setuo";
1711 case ISD::SETUEQ: return "setue";
1712 case ISD::SETUGT: return "setugt";
1713 case ISD::SETUGE: return "setuge";
1714 case ISD::SETULT: return "setult";
1715 case ISD::SETULE: return "setule";
1716 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001717
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001718 case ISD::SETEQ: return "seteq";
1719 case ISD::SETGT: return "setgt";
1720 case ISD::SETGE: return "setge";
1721 case ISD::SETLT: return "setlt";
1722 case ISD::SETLE: return "setle";
1723 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001724 }
1725 }
1726}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001727
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001728void SDNode::dump() const { dump(0); }
1729void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001730 std::cerr << (void*)this << ": ";
1731
1732 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1733 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001734 if (getValueType(i) == MVT::Other)
1735 std::cerr << "ch";
1736 else
1737 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001738 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001739 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001740
1741 std::cerr << " ";
1742 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1743 if (i) std::cerr << ", ";
1744 std::cerr << (void*)getOperand(i).Val;
1745 if (unsigned RN = getOperand(i).ResNo)
1746 std::cerr << ":" << RN;
1747 }
1748
1749 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1750 std::cerr << "<" << CSDN->getValue() << ">";
1751 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1752 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001753 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001754 dyn_cast<GlobalAddressSDNode>(this)) {
1755 std::cerr << "<";
1756 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001757 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 std::cerr << "<" << FIDN->getIndex() << ">";
1759 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00001760 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001761 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001762 std::cerr << "<";
1763 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1764 if (LBB)
1765 std::cerr << LBB->getName() << " ";
1766 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00001767 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00001768 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
1769 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
1770 } else {
1771 std::cerr << " #" << R->getReg();
1772 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001773 } else if (const ExternalSymbolSDNode *ES =
1774 dyn_cast<ExternalSymbolSDNode>(this)) {
1775 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001776 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1777 if (M->getValue())
1778 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1779 else
1780 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00001781 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
1782 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001783 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001784}
1785
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001786static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001787 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1788 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001789 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001790 else
1791 std::cerr << "\n" << std::string(indent+2, ' ')
1792 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001793
Chris Lattnerea946cd2005-01-09 20:38:33 +00001794
1795 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001796 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001797}
1798
Chris Lattnerc3aae252005-01-07 07:46:32 +00001799void SelectionDAG::dump() const {
1800 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001801 std::vector<SDNode*> Nodes(AllNodes);
1802 std::sort(Nodes.begin(), Nodes.end());
1803
1804 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001805 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001806 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001807 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001808
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001809 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001810
Chris Lattnerc3aae252005-01-07 07:46:32 +00001811 std::cerr << "\n\n";
1812}
1813