blob: 8b1deb34700103089ed4df20b109f013bfcd25b6 [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) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000172 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173
Chris Lattnerf469cb62005-11-08 18:52:27 +0000174 bool MadeChange = false;
175
Chris Lattner8b8749f2005-08-17 19:00:20 +0000176 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000177 if (N && N->use_empty()) {
178 DestroyDeadNode(N);
179 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000180 }
181
Chris Lattnerde202b32005-11-09 23:47:37 +0000182 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
183 if (I->use_empty() && I->getOpcode() != 65535) {
184 // Node is dead, recursively delete newly dead uses.
185 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000186 MadeChange = true;
187 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000188
189 // Walk the nodes list, removing the nodes we've marked as dead.
190 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000191 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
192 SDNode *N = I++;
193 if (N->use_empty())
194 AllNodes.erase(N);
195 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000196 }
197
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000198 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000199 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000200}
201
Chris Lattnerf469cb62005-11-08 18:52:27 +0000202/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
203/// graph. If it is the last user of any of its operands, recursively process
204/// them the same way.
205///
206void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000207 // Okay, we really are going to delete this node. First take this out of the
208 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000209 RemoveNodeFromCSEMaps(N);
210
211 // Next, brutally remove the operand list. This is safe to do, as there are
212 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000213 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
214 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000215 O->removeUser(N);
216
217 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000218 if (O->use_empty())
219 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000220 }
Chris Lattner65113b22005-11-08 22:07:03 +0000221 delete[] N->OperandList;
222 N->OperandList = 0;
223 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000224
225 // Mark the node as dead.
226 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000227}
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.
Chris Lattnerde202b32005-11-09 23:47:37 +0000243 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000244
245 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000246 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
247 I->Val->removeUser(N);
248 delete[] N->OperandList;
249 N->OperandList = 0;
250 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000251
252 delete N;
253}
254
Chris Lattner149c58c2005-08-16 18:17:10 +0000255/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
256/// correspond to it. This is useful when we're about to delete or repurpose
257/// the node. We don't want future request for structurally identical nodes
258/// to return N anymore.
259void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000260 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000262 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000263 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000264 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
265 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000267 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000268 Erased = TargetConstants.erase(std::make_pair(
269 cast<ConstantSDNode>(N)->getValue(),
270 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000271 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000272 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000273 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000274 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000276 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000277 case ISD::STRING:
278 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
279 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000280 case ISD::CONDCODE:
281 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
282 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000283 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000284 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
285 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000286 case ISD::GlobalAddress: {
287 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
288 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
289 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000290 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000291 }
292 case ISD::TargetGlobalAddress: {
293 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
294 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
295 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000296 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000297 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000299 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000300 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000301 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000302 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000303 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000304 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000305 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000307 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000308 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000309 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000310 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000311 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312 break;
313 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000314 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000316 case ISD::TargetExternalSymbol:
317 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
318 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000319 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000320 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000321 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
322 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000323 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000324 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
325 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000326 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000327 case ISD::SRCVALUE: {
328 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000329 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000330 break;
331 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000332 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000333 Erased = Loads.erase(std::make_pair(N->getOperand(1),
334 std::make_pair(N->getOperand(0),
335 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000336 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000337 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000338 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000339 if (N->getNumOperands() == 0) {
340 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
341 N->getValueType(0)));
342 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000343 Erased =
344 UnaryOps.erase(std::make_pair(N->getOpcode(),
345 std::make_pair(N->getOperand(0),
346 N->getValueType(0))));
347 } else if (N->getNumOperands() == 2) {
348 Erased =
349 BinaryOps.erase(std::make_pair(N->getOpcode(),
350 std::make_pair(N->getOperand(0),
351 N->getOperand(1))));
352 } else {
353 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
354 Erased =
355 OneResultNodes.erase(std::make_pair(N->getOpcode(),
356 std::make_pair(N->getValueType(0),
357 Ops)));
358 }
Chris Lattner385328c2005-05-14 07:42:29 +0000359 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000360 // Remove the node from the ArbitraryNodes map.
361 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
362 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000363 Erased =
364 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
365 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000366 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000367 break;
368 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369#ifndef NDEBUG
370 // Verify that the node was actually in one of the CSE maps, unless it has a
371 // flag result (which cannot be CSE'd) or is one of the special cases that are
372 // not subject to CSE.
373 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
374 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000375 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000376
377 N->dump();
378 assert(0 && "Node is not in map!");
379 }
380#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000381}
382
Chris Lattner8b8749f2005-08-17 19:00:20 +0000383/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
384/// has been taken out and modified in some way. If the specified node already
385/// exists in the CSE maps, do not modify the maps, but return the existing node
386/// instead. If it doesn't exist, add it and return null.
387///
388SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
389 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000390 if (N->getOpcode() == ISD::CALLSEQ_START ||
391 N->getOpcode() == ISD::CALLSEQ_END)
392 return 0;
393
Chris Lattner8b8749f2005-08-17 19:00:20 +0000394 if (N->getOpcode() == ISD::LOAD) {
395 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
396 std::make_pair(N->getOperand(0),
397 N->getValueType(0)))];
398 if (L) return L;
399 L = N;
Chris Lattner95038592005-10-05 06:35:28 +0000400 } else if (N->getOpcode() == ISD::HANDLENODE) {
401 return 0; // never add it.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000402 } else if (N->getNumOperands() == 1) {
403 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
405 N->getValueType(0)))];
406 if (U) return U;
407 U = N;
408 } else if (N->getNumOperands() == 2) {
409 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
410 std::make_pair(N->getOperand(0),
411 N->getOperand(1)))];
412 if (B) return B;
413 B = N;
414 } else if (N->getNumValues() == 1) {
415 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
416 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
417 std::make_pair(N->getValueType(0), Ops))];
418 if (ORN) return ORN;
419 ORN = N;
420 } else {
421 // Remove the node from the ArbitraryNodes map.
422 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
423 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
424 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
425 std::make_pair(RV, Ops))];
426 if (AN) return AN;
427 AN = N;
428 }
429 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000430}
431
432
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000433
Chris Lattner78ec3112003-08-11 14:57:33 +0000434SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000435 while (!AllNodes.empty()) {
436 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000437 delete [] N->OperandList;
438 N->OperandList = 0;
439 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000440 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000441 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000442}
443
Chris Lattner0f2287b2005-04-13 02:38:18 +0000444SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000445 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000446 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000447 return getNode(ISD::AND, Op.getValueType(), Op,
448 getConstant(Imm, Op.getValueType()));
449}
450
Chris Lattnerc3aae252005-01-07 07:46:32 +0000451SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
452 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
453 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000454 if (VT != MVT::i64)
455 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000456
Chris Lattnerc3aae252005-01-07 07:46:32 +0000457 SDNode *&N = Constants[std::make_pair(Val, VT)];
458 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000459 N = new ConstantSDNode(false, Val, VT);
460 AllNodes.push_back(N);
461 return SDOperand(N, 0);
462}
463
Chris Lattner36ce6912005-11-29 06:21:05 +0000464SDOperand SelectionDAG::getString(const std::string &Val) {
465 StringSDNode *&N = StringNodes[Val];
466 if (!N) {
467 N = new StringSDNode(Val);
468 AllNodes.push_back(N);
469 }
470 return SDOperand(N, 0);
471}
472
Chris Lattner37bfbb42005-08-17 00:34:06 +0000473SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
474 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
475 // Mask out any bits that are not valid for this constant.
476 if (VT != MVT::i64)
477 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
478
479 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
480 if (N) return SDOperand(N, 0);
481 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000482 AllNodes.push_back(N);
483 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000484}
485
Chris Lattnerc3aae252005-01-07 07:46:32 +0000486SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
487 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
488 if (VT == MVT::f32)
489 Val = (float)Val; // Mask out extra precision.
490
Chris Lattnerd8658612005-02-17 20:17:32 +0000491 // Do the map lookup using the actual bit pattern for the floating point
492 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
493 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000494 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000495 if (N) return SDOperand(N, 0);
496 N = new ConstantFPSDNode(Val, VT);
497 AllNodes.push_back(N);
498 return SDOperand(N, 0);
499}
500
501
502
503SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000504 MVT::ValueType VT, int offset) {
505 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000506 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000507 N = new GlobalAddressSDNode(false, GV, VT);
508 AllNodes.push_back(N);
509 return SDOperand(N, 0);
510}
511
512SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000513 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000514 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000515 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000516 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000517 AllNodes.push_back(N);
518 return SDOperand(N, 0);
519}
520
521SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
522 SDNode *&N = FrameIndices[FI];
523 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000524 N = new FrameIndexSDNode(FI, VT, false);
525 AllNodes.push_back(N);
526 return SDOperand(N, 0);
527}
528
529SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
530 SDNode *&N = TargetFrameIndices[FI];
531 if (N) return SDOperand(N, 0);
532 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000533 AllNodes.push_back(N);
534 return SDOperand(N, 0);
535}
536
Chris Lattner5839bf22005-08-26 17:15:30 +0000537SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
538 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000539 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000540 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000541 AllNodes.push_back(N);
542 return SDOperand(N, 0);
543}
544
Chris Lattner5839bf22005-08-26 17:15:30 +0000545SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
546 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000547 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000548 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000549 AllNodes.push_back(N);
550 return SDOperand(N, 0);
551}
552
553SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
554 SDNode *&N = BBNodes[MBB];
555 if (N) return SDOperand(N, 0);
556 N = new BasicBlockSDNode(MBB);
557 AllNodes.push_back(N);
558 return SDOperand(N, 0);
559}
560
Chris Lattner15e4b012005-07-10 00:07:11 +0000561SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
562 if ((unsigned)VT >= ValueTypeNodes.size())
563 ValueTypeNodes.resize(VT+1);
564 if (ValueTypeNodes[VT] == 0) {
565 ValueTypeNodes[VT] = new VTSDNode(VT);
566 AllNodes.push_back(ValueTypeNodes[VT]);
567 }
568
569 return SDOperand(ValueTypeNodes[VT], 0);
570}
571
Chris Lattnerc3aae252005-01-07 07:46:32 +0000572SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
573 SDNode *&N = ExternalSymbols[Sym];
574 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000575 N = new ExternalSymbolSDNode(false, Sym, VT);
576 AllNodes.push_back(N);
577 return SDOperand(N, 0);
578}
579
580SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
581 SDNode *&N = TargetExternalSymbols[Sym];
582 if (N) return SDOperand(N, 0);
583 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000584 AllNodes.push_back(N);
585 return SDOperand(N, 0);
586}
587
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000588SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
589 if ((unsigned)Cond >= CondCodeNodes.size())
590 CondCodeNodes.resize(Cond+1);
591
Chris Lattner079a27a2005-08-09 20:40:02 +0000592 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000593 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000594 AllNodes.push_back(CondCodeNodes[Cond]);
595 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000596 return SDOperand(CondCodeNodes[Cond], 0);
597}
598
Chris Lattner0fdd7682005-08-30 22:38:38 +0000599SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
600 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
601 if (!Reg) {
602 Reg = new RegisterSDNode(RegNo, VT);
603 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000604 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000605 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000606}
607
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000608SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
609 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000610 // These setcc operations always fold.
611 switch (Cond) {
612 default: break;
613 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000614 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000615 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000616 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617 }
618
Chris Lattner67255a12005-04-07 18:14:58 +0000619 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
620 uint64_t C2 = N2C->getValue();
621 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
622 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000623
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 // Sign extend the operands if required
625 if (ISD::isSignedIntSetCC(Cond)) {
626 C1 = N1C->getSignExtended();
627 C2 = N2C->getSignExtended();
628 }
629
630 switch (Cond) {
631 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000632 case ISD::SETEQ: return getConstant(C1 == C2, VT);
633 case ISD::SETNE: return getConstant(C1 != C2, VT);
634 case ISD::SETULT: return getConstant(C1 < C2, VT);
635 case ISD::SETUGT: return getConstant(C1 > C2, VT);
636 case ISD::SETULE: return getConstant(C1 <= C2, VT);
637 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
638 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
639 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
640 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
641 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000642 }
Chris Lattner24673922005-04-07 18:58:54 +0000643 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000644 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000645 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
646 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
647
648 // If the comparison constant has bits in the upper part, the
649 // zero-extended value could never match.
650 if (C2 & (~0ULL << InSize)) {
651 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
652 switch (Cond) {
653 case ISD::SETUGT:
654 case ISD::SETUGE:
655 case ISD::SETEQ: return getConstant(0, VT);
656 case ISD::SETULT:
657 case ISD::SETULE:
658 case ISD::SETNE: return getConstant(1, VT);
659 case ISD::SETGT:
660 case ISD::SETGE:
661 // True if the sign bit of C2 is set.
662 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
663 case ISD::SETLT:
664 case ISD::SETLE:
665 // True if the sign bit of C2 isn't set.
666 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
667 default:
668 break;
669 }
670 }
671
672 // Otherwise, we can perform the comparison with the low bits.
673 switch (Cond) {
674 case ISD::SETEQ:
675 case ISD::SETNE:
676 case ISD::SETUGT:
677 case ISD::SETUGE:
678 case ISD::SETULT:
679 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000680 return getSetCC(VT, N1.getOperand(0),
681 getConstant(C2, N1.getOperand(0).getValueType()),
682 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000683 default:
684 break; // todo, be more careful with signed comparisons
685 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000686 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
687 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
688 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
689 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
690 MVT::ValueType ExtDstTy = N1.getValueType();
691 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000692
Chris Lattner7b2880c2005-08-24 22:44:39 +0000693 // If the extended part has any inconsistent bits, it cannot ever
694 // compare equal. In other words, they have to be all ones or all
695 // zeros.
696 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000697 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000698 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
699 return getConstant(Cond == ISD::SETNE, VT);
700
701 // Otherwise, make this a use of a zext.
702 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000703 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000704 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000705 }
706
Chris Lattner67255a12005-04-07 18:14:58 +0000707 uint64_t MinVal, MaxVal;
708 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
709 if (ISD::isSignedIntSetCC(Cond)) {
710 MinVal = 1ULL << (OperandBitSize-1);
711 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
712 MaxVal = ~0ULL >> (65-OperandBitSize);
713 else
714 MaxVal = 0;
715 } else {
716 MinVal = 0;
717 MaxVal = ~0ULL >> (64-OperandBitSize);
718 }
719
720 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
721 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
722 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
723 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000724 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
725 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000726 }
727
728 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
729 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
730 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000731 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
732 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000733 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000734
Nate Begeman72ea2812005-04-14 08:56:52 +0000735 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
736 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000737
Nate Begeman72ea2812005-04-14 08:56:52 +0000738 // Canonicalize setgt X, Min --> setne X, Min
739 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000740 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000741
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000742 // If we have setult X, 1, turn it into seteq X, 0
743 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000744 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
745 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000746 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000747 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000748 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
749 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000750
751 // If we have "setcc X, C1", check to see if we can shrink the immediate
752 // by changing cc.
753
754 // SETUGT X, SINTMAX -> SETLT X, 0
755 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
756 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000757 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000758
759 // FIXME: Implement the rest of these.
760
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000761
762 // Fold bit comparisons when we can.
763 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
764 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
765 if (ConstantSDNode *AndRHS =
766 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
767 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
768 // Perform the xform if the AND RHS is a single bit.
769 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
770 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000771 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000772 TLI.getShiftAmountTy()));
773 }
774 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
775 // (X & 8) == 8 --> (X & 8) >> 3
776 // Perform the xform if C2 is a single bit.
777 if ((C2 & (C2-1)) == 0) {
778 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000779 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000780 }
781 }
782 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783 }
Chris Lattner67255a12005-04-07 18:14:58 +0000784 } else if (isa<ConstantSDNode>(N1.Val)) {
785 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000786 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000787 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000788
789 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
790 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
791 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000792
Chris Lattnerc3aae252005-01-07 07:46:32 +0000793 switch (Cond) {
794 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000795 case ISD::SETEQ: return getConstant(C1 == C2, VT);
796 case ISD::SETNE: return getConstant(C1 != C2, VT);
797 case ISD::SETLT: return getConstant(C1 < C2, VT);
798 case ISD::SETGT: return getConstant(C1 > C2, VT);
799 case ISD::SETLE: return getConstant(C1 <= C2, VT);
800 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000801 }
802 } else {
803 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000804 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000805 }
806
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000807 // Could not fold it.
808 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000809}
810
Chris Lattnerc3aae252005-01-07 07:46:32 +0000811/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000812///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000813SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000814 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
815 if (!N) {
816 N = new SDNode(Opcode, VT);
817 AllNodes.push_back(N);
818 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819 return SDOperand(N, 0);
820}
821
Chris Lattnerc3aae252005-01-07 07:46:32 +0000822SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
823 SDOperand Operand) {
824 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
825 uint64_t Val = C->getValue();
826 switch (Opcode) {
827 default: break;
828 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000829 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000830 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
831 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000832 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
833 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000834 }
835 }
836
837 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
838 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000839 case ISD::FNEG:
840 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000841 case ISD::FP_ROUND:
842 case ISD::FP_EXTEND:
843 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000844 case ISD::FP_TO_SINT:
845 return getConstant((int64_t)C->getValue(), VT);
846 case ISD::FP_TO_UINT:
847 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000848 }
849
850 unsigned OpOpcode = Operand.Val->getOpcode();
851 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000852 case ISD::TokenFactor:
853 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854 case ISD::SIGN_EXTEND:
855 if (Operand.getValueType() == VT) return Operand; // noop extension
856 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
857 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
858 break;
859 case ISD::ZERO_EXTEND:
860 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000861 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000862 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000863 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000864 case ISD::ANY_EXTEND:
865 if (Operand.getValueType() == VT) return Operand; // noop extension
866 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
867 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
868 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
869 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000870 case ISD::TRUNCATE:
871 if (Operand.getValueType() == VT) return Operand; // noop truncate
872 if (OpOpcode == ISD::TRUNCATE)
873 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000874 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
875 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000876 // If the source is smaller than the dest, we still need an extend.
877 if (Operand.Val->getOperand(0).getValueType() < VT)
878 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
879 else if (Operand.Val->getOperand(0).getValueType() > VT)
880 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
881 else
882 return Operand.Val->getOperand(0);
883 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000885 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000886 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
887 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000888 Operand.Val->getOperand(0));
889 if (OpOpcode == ISD::FNEG) // --X -> X
890 return Operand.Val->getOperand(0);
891 break;
892 case ISD::FABS:
893 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
894 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
895 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000896 }
897
Chris Lattner43247a12005-08-25 19:12:10 +0000898 SDNode *N;
899 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
900 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000901 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000902 E = N = new SDNode(Opcode, Operand);
903 } else {
904 N = new SDNode(Opcode, Operand);
905 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000906 N->setValueTypes(VT);
907 AllNodes.push_back(N);
908 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000909}
910
Chris Lattner36019aa2005-04-18 03:48:41 +0000911
912
Chris Lattnerc3aae252005-01-07 07:46:32 +0000913SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
914 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000915#ifndef NDEBUG
916 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000917 case ISD::TokenFactor:
918 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
919 N2.getValueType() == MVT::Other && "Invalid token factor!");
920 break;
Chris Lattner76365122005-01-16 02:23:22 +0000921 case ISD::AND:
922 case ISD::OR:
923 case ISD::XOR:
924 case ISD::UDIV:
925 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000926 case ISD::MULHU:
927 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000928 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
929 // fall through
930 case ISD::ADD:
931 case ISD::SUB:
932 case ISD::MUL:
933 case ISD::SDIV:
934 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000935 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
936 // fall through.
937 case ISD::FADD:
938 case ISD::FSUB:
939 case ISD::FMUL:
940 case ISD::FDIV:
941 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000942 assert(N1.getValueType() == N2.getValueType() &&
943 N1.getValueType() == VT && "Binary operator types must match!");
944 break;
945
946 case ISD::SHL:
947 case ISD::SRA:
948 case ISD::SRL:
949 assert(VT == N1.getValueType() &&
950 "Shift operators return type must be the same as their first arg");
951 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000952 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000953 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000954 case ISD::FP_ROUND_INREG: {
955 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
956 assert(VT == N1.getValueType() && "Not an inreg round!");
957 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
958 "Cannot FP_ROUND_INREG integer types");
959 assert(EVT <= VT && "Not rounding down!");
960 break;
961 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000962 case ISD::AssertSext:
963 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +0000964 case ISD::SIGN_EXTEND_INREG: {
965 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
966 assert(VT == N1.getValueType() && "Not an inreg extend!");
967 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
968 "Cannot *_EXTEND_INREG FP types");
969 assert(EVT <= VT && "Not extending!");
970 }
971
Chris Lattner76365122005-01-16 02:23:22 +0000972 default: break;
973 }
974#endif
975
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
977 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
978 if (N1C) {
979 if (N2C) {
980 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
981 switch (Opcode) {
982 case ISD::ADD: return getConstant(C1 + C2, VT);
983 case ISD::SUB: return getConstant(C1 - C2, VT);
984 case ISD::MUL: return getConstant(C1 * C2, VT);
985 case ISD::UDIV:
986 if (C2) return getConstant(C1 / C2, VT);
987 break;
988 case ISD::UREM :
989 if (C2) return getConstant(C1 % C2, VT);
990 break;
991 case ISD::SDIV :
992 if (C2) return getConstant(N1C->getSignExtended() /
993 N2C->getSignExtended(), VT);
994 break;
995 case ISD::SREM :
996 if (C2) return getConstant(N1C->getSignExtended() %
997 N2C->getSignExtended(), VT);
998 break;
999 case ISD::AND : return getConstant(C1 & C2, VT);
1000 case ISD::OR : return getConstant(C1 | C2, VT);
1001 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001002 case ISD::SHL : return getConstant(C1 << C2, VT);
1003 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001004 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 default: break;
1006 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001007 } else { // Cannonicalize constant to RHS if commutative
1008 if (isCommutativeBinOp(Opcode)) {
1009 std::swap(N1C, N2C);
1010 std::swap(N1, N2);
1011 }
1012 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 }
1014
1015 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1016 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001017 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 if (N2CFP) {
1019 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1020 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001021 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1022 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1023 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1024 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001025 if (C2) return getConstantFP(C1 / C2, VT);
1026 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001027 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028 if (C2) return getConstantFP(fmod(C1, C2), VT);
1029 break;
1030 default: break;
1031 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032 } else { // Cannonicalize constant to RHS if commutative
1033 if (isCommutativeBinOp(Opcode)) {
1034 std::swap(N1CFP, N2CFP);
1035 std::swap(N1, N2);
1036 }
1037 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001038 }
1039
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 // Finally, fold operations that do not require constants.
1041 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001042 case ISD::FP_ROUND_INREG:
1043 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1044 break;
1045 case ISD::SIGN_EXTEND_INREG: {
1046 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1047 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001048 break;
1049 }
1050
Nate Begemaneea805e2005-04-13 21:23:31 +00001051 // FIXME: figure out how to safely handle things like
1052 // int foo(int x) { return 1 << (x & 255); }
1053 // int bar() { return foo(256); }
1054#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001055 case ISD::SHL:
1056 case ISD::SRL:
1057 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001058 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001059 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001060 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001061 else if (N2.getOpcode() == ISD::AND)
1062 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1063 // If the and is only masking out bits that cannot effect the shift,
1064 // eliminate the and.
1065 unsigned NumBits = MVT::getSizeInBits(VT);
1066 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1067 return getNode(Opcode, VT, N1, N2.getOperand(0));
1068 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001069 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001070#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 }
1072
Chris Lattner27e9b412005-05-11 18:57:39 +00001073 // Memoize this node if possible.
1074 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001075 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1076 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001077 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1078 if (BON) return SDOperand(BON, 0);
1079
1080 BON = N = new SDNode(Opcode, N1, N2);
1081 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001082 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001083 }
1084
Chris Lattner3e011362005-05-14 07:45:46 +00001085 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 AllNodes.push_back(N);
1087 return SDOperand(N, 0);
1088}
1089
Chris Lattner88de6e72005-05-12 00:17:04 +00001090// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001091// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001092void SDNode::setAdjCallChain(SDOperand N) {
1093 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001094 assert((getOpcode() == ISD::CALLSEQ_START ||
1095 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001096
Chris Lattner65113b22005-11-08 22:07:03 +00001097 OperandList[0].Val->removeUser(this);
1098 OperandList[0] = N;
1099 OperandList[0].Val->Uses.push_back(this);
Chris Lattner27e9b412005-05-11 18:57:39 +00001100}
1101
1102
1103
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001105 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001106 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001107 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1108 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001109 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001110
1111 // Loads have a token chain.
Chris Lattnera3255112005-11-08 23:30:28 +00001112 setNodeValueTypes(N, VT, MVT::Other);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113 AllNodes.push_back(N);
1114 return SDOperand(N, 0);
1115}
1116
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001117SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1118 SDOperand Chain, SDOperand Ptr,
1119 SDOperand SV) {
1120 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1121 if (N) return SDOperand(N, 0);
1122 std::vector<SDOperand> Ops;
1123 Ops.reserve(5);
1124 Ops.push_back(Chain);
1125 Ops.push_back(Ptr);
1126 Ops.push_back(getConstant(Count, MVT::i32));
1127 Ops.push_back(getValueType(EVT));
1128 Ops.push_back(SV);
1129 std::vector<MVT::ValueType> VTs;
1130 VTs.reserve(2);
Nate Begemanab48be32005-11-22 18:16:00 +00001131 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001132 return getNode(ISD::VLOAD, VTs, Ops);
1133}
Chris Lattner5f056bf2005-07-10 01:55:33 +00001134
1135SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1136 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1137 MVT::ValueType EVT) {
1138 std::vector<SDOperand> Ops;
1139 Ops.reserve(4);
1140 Ops.push_back(Chain);
1141 Ops.push_back(Ptr);
1142 Ops.push_back(SV);
1143 Ops.push_back(getValueType(EVT));
1144 std::vector<MVT::ValueType> VTs;
1145 VTs.reserve(2);
1146 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1147 return getNode(Opcode, VTs, Ops);
1148}
1149
Chris Lattnerc3aae252005-01-07 07:46:32 +00001150SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1151 SDOperand N1, SDOperand N2, SDOperand N3) {
1152 // Perform various simplifications.
1153 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1154 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1155 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1156 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001157 case ISD::SETCC: {
1158 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001159 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001160 if (Simp.Val) return Simp;
1161 break;
1162 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 case ISD::SELECT:
1164 if (N1C)
1165 if (N1C->getValue())
1166 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001167 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001168 return N3; // select false, X, Y -> Y
1169
1170 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001172 case ISD::BRCOND:
1173 if (N2C)
1174 if (N2C->getValue()) // Unconditional branch
1175 return getNode(ISD::BR, MVT::Other, N1, N3);
1176 else
1177 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001178 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001179 }
1180
Chris Lattner385328c2005-05-14 07:42:29 +00001181 std::vector<SDOperand> Ops;
1182 Ops.reserve(3);
1183 Ops.push_back(N1);
1184 Ops.push_back(N2);
1185 Ops.push_back(N3);
1186
Chris Lattner43247a12005-08-25 19:12:10 +00001187 // Memoize node if it doesn't produce a flag.
1188 SDNode *N;
1189 if (VT != MVT::Flag) {
1190 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1191 if (E) return SDOperand(E, 0);
1192 E = N = new SDNode(Opcode, N1, N2, N3);
1193 } else {
1194 N = new SDNode(Opcode, N1, N2, N3);
1195 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001196 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001197 AllNodes.push_back(N);
1198 return SDOperand(N, 0);
1199}
1200
1201SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001202 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001203 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001204 std::vector<SDOperand> Ops;
1205 Ops.reserve(4);
1206 Ops.push_back(N1);
1207 Ops.push_back(N2);
1208 Ops.push_back(N3);
1209 Ops.push_back(N4);
1210 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001211}
1212
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001213SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1214 SDOperand N1, SDOperand N2, SDOperand N3,
1215 SDOperand N4, SDOperand N5) {
1216 std::vector<SDOperand> Ops;
1217 Ops.reserve(5);
1218 Ops.push_back(N1);
1219 Ops.push_back(N2);
1220 Ops.push_back(N3);
1221 Ops.push_back(N4);
1222 Ops.push_back(N5);
1223 return getNode(Opcode, VT, Ops);
1224}
1225
1226
Chris Lattner0437cdd2005-05-09 04:14:13 +00001227SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001228 assert((!V || isa<PointerType>(V->getType())) &&
1229 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001230 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1231 if (N) return SDOperand(N, 0);
1232
1233 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001234 AllNodes.push_back(N);
1235 return SDOperand(N, 0);
1236}
1237
1238SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001239 std::vector<SDOperand> &Ops) {
1240 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001242 case 1: return getNode(Opcode, VT, Ops[0]);
1243 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1244 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001245 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001247
Chris Lattner89c34632005-05-14 06:20:26 +00001248 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001249 switch (Opcode) {
1250 default: break;
1251 case ISD::BRCONDTWOWAY:
1252 if (N1C)
1253 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001254 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001255 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001256 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001257 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001258 case ISD::BRTWOWAY_CC:
1259 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1260 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1261 "LHS and RHS of comparison must have same type!");
1262 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001263 case ISD::TRUNCSTORE: {
1264 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1265 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1266#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1267 // If this is a truncating store of a constant, convert to the desired type
1268 // and store it instead.
1269 if (isa<Constant>(Ops[0])) {
1270 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1271 if (isa<Constant>(Op))
1272 N1 = Op;
1273 }
1274 // Also for ConstantFP?
1275#endif
1276 if (Ops[0].getValueType() == EVT) // Normal store?
1277 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1278 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1279 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1280 "Can't do FP-INT conversion!");
1281 break;
1282 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001283 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001284 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001285 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1286 "LHS and RHS of condition must have same type!");
1287 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1288 "True and False arms of SelectCC must have same type!");
1289 assert(Ops[2].getValueType() == VT &&
1290 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001291 break;
1292 }
1293 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001294 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001295 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1296 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001297 break;
1298 }
Chris Lattneref847df2005-04-09 03:27:28 +00001299 }
1300
Chris Lattner385328c2005-05-14 07:42:29 +00001301 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001302 SDNode *N;
1303 if (VT != MVT::Flag) {
1304 SDNode *&E =
1305 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1306 if (E) return SDOperand(E, 0);
1307 E = N = new SDNode(Opcode, Ops);
1308 } else {
1309 N = new SDNode(Opcode, Ops);
1310 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001311 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001312 AllNodes.push_back(N);
1313 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001314}
1315
Chris Lattner89c34632005-05-14 06:20:26 +00001316SDOperand SelectionDAG::getNode(unsigned Opcode,
1317 std::vector<MVT::ValueType> &ResultTys,
1318 std::vector<SDOperand> &Ops) {
1319 if (ResultTys.size() == 1)
1320 return getNode(Opcode, ResultTys[0], Ops);
1321
Chris Lattner5f056bf2005-07-10 01:55:33 +00001322 switch (Opcode) {
1323 case ISD::EXTLOAD:
1324 case ISD::SEXTLOAD:
1325 case ISD::ZEXTLOAD: {
1326 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1327 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1328 // If they are asking for an extending load from/to the same thing, return a
1329 // normal load.
1330 if (ResultTys[0] == EVT)
1331 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1332 assert(EVT < ResultTys[0] &&
1333 "Should only be an extending load, not truncating!");
1334 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1335 "Cannot sign/zero extend a FP load!");
1336 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1337 "Cannot convert from FP to Int or Int -> FP!");
1338 break;
1339 }
1340
Chris Lattnere89083a2005-05-14 07:25:05 +00001341 // FIXME: figure out how to safely handle things like
1342 // int foo(int x) { return 1 << (x & 255); }
1343 // int bar() { return foo(256); }
1344#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001345 case ISD::SRA_PARTS:
1346 case ISD::SRL_PARTS:
1347 case ISD::SHL_PARTS:
1348 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001349 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001350 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1351 else if (N3.getOpcode() == ISD::AND)
1352 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1353 // If the and is only masking out bits that cannot effect the shift,
1354 // eliminate the and.
1355 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1356 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1357 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1358 }
1359 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001360#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001361 }
Chris Lattner89c34632005-05-14 06:20:26 +00001362
Chris Lattner43247a12005-08-25 19:12:10 +00001363 // Memoize the node unless it returns a flag.
1364 SDNode *N;
1365 if (ResultTys.back() != MVT::Flag) {
1366 SDNode *&E =
1367 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1368 if (E) return SDOperand(E, 0);
1369 E = N = new SDNode(Opcode, Ops);
1370 } else {
1371 N = new SDNode(Opcode, Ops);
1372 }
Chris Lattnera3255112005-11-08 23:30:28 +00001373 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001374 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001375 return SDOperand(N, 0);
1376}
1377
Chris Lattnera3255112005-11-08 23:30:28 +00001378void SelectionDAG::setNodeValueTypes(SDNode *N,
1379 std::vector<MVT::ValueType> &RetVals) {
1380 switch (RetVals.size()) {
1381 case 0: return;
1382 case 1: N->setValueTypes(RetVals[0]); return;
1383 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1384 default: break;
1385 }
1386
1387 std::list<std::vector<MVT::ValueType> >::iterator I =
1388 std::find(VTList.begin(), VTList.end(), RetVals);
1389 if (I == VTList.end()) {
1390 VTList.push_front(RetVals);
1391 I = VTList.begin();
1392 }
1393
1394 N->setValueTypes(&(*I)[0], I->size());
1395}
1396
1397void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1398 MVT::ValueType VT2) {
1399 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1400 E = VTList.end(); I != E; ++I) {
1401 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1402 N->setValueTypes(&(*I)[0], 2);
1403 return;
1404 }
1405 }
1406 std::vector<MVT::ValueType> V;
1407 V.push_back(VT1);
1408 V.push_back(VT2);
1409 VTList.push_front(V);
1410 N->setValueTypes(&(*VTList.begin())[0], 2);
1411}
1412
Chris Lattner149c58c2005-08-16 18:17:10 +00001413
1414/// SelectNodeTo - These are used for target selectors to *mutate* the
1415/// specified node to have the specified return type, Target opcode, and
1416/// operands. Note that target opcodes are stored as
1417/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001418///
1419/// Note that SelectNodeTo returns the resultant node. If there is already a
1420/// node of the specified opcode and operands, it returns that node instead of
1421/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001422SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1423 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001424 // If an identical node already exists, use it.
1425 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1426 if (ON) return SDOperand(ON, 0);
1427
Chris Lattner7651fa42005-08-24 23:00:29 +00001428 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001429
Chris Lattner7651fa42005-08-24 23:00:29 +00001430 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1431 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001432
1433 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001434 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001435}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001436
Chris Lattnereb19e402005-11-30 22:45:14 +00001437SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1438 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001439 // If an identical node already exists, use it.
1440 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1441 std::make_pair(Op1, VT))];
1442 if (ON) return SDOperand(ON, 0);
1443
Chris Lattner149c58c2005-08-16 18:17:10 +00001444 RemoveNodeFromCSEMaps(N);
1445 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1446 N->setValueTypes(VT);
1447 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001448
1449 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001450 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001451}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001452
Chris Lattnereb19e402005-11-30 22:45:14 +00001453SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1454 MVT::ValueType VT, SDOperand Op1,
1455 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001456 // If an identical node already exists, use it.
1457 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1458 std::make_pair(Op1, Op2))];
1459 if (ON) return SDOperand(ON, 0);
1460
Chris Lattner149c58c2005-08-16 18:17:10 +00001461 RemoveNodeFromCSEMaps(N);
1462 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1463 N->setValueTypes(VT);
1464 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001465
1466 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001467 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001468}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001469
Chris Lattnereb19e402005-11-30 22:45:14 +00001470SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1471 MVT::ValueType VT, SDOperand Op1,
1472 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001473 // If an identical node already exists, use it.
1474 std::vector<SDOperand> OpList;
1475 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1476 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1477 std::make_pair(VT, OpList))];
1478 if (ON) return SDOperand(ON, 0);
1479
Chris Lattner149c58c2005-08-16 18:17:10 +00001480 RemoveNodeFromCSEMaps(N);
1481 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1482 N->setValueTypes(VT);
1483 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001484
1485 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001486 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001487}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001488
Chris Lattnereb19e402005-11-30 22:45:14 +00001489SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1490 MVT::ValueType VT, SDOperand Op1,
1491 SDOperand Op2, SDOperand Op3,
1492 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001493 // If an identical node already exists, use it.
1494 std::vector<SDOperand> OpList;
1495 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1496 OpList.push_back(Op4);
1497 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1498 std::make_pair(VT, OpList))];
1499 if (ON) return SDOperand(ON, 0);
1500
Nate Begeman294a0a12005-08-18 07:30:15 +00001501 RemoveNodeFromCSEMaps(N);
1502 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1503 N->setValueTypes(VT);
1504 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001505
1506 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001507 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001508}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001509
Chris Lattnereb19e402005-11-30 22:45:14 +00001510SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1511 MVT::ValueType VT, SDOperand Op1,
1512 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1513 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001514 // If an identical node already exists, use it.
1515 std::vector<SDOperand> OpList;
1516 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1517 OpList.push_back(Op4); OpList.push_back(Op5);
1518 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1519 std::make_pair(VT, OpList))];
1520 if (ON) return SDOperand(ON, 0);
1521
Chris Lattner6b09a292005-08-21 18:49:33 +00001522 RemoveNodeFromCSEMaps(N);
1523 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1524 N->setValueTypes(VT);
1525 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001526
1527 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001528 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001529}
Chris Lattner149c58c2005-08-16 18:17:10 +00001530
Chris Lattnereb19e402005-11-30 22:45:14 +00001531SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1532 MVT::ValueType VT, SDOperand Op1,
1533 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1534 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001535 // If an identical node already exists, use it.
1536 std::vector<SDOperand> OpList;
1537 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1538 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1539 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1540 std::make_pair(VT, OpList))];
1541 if (ON) return SDOperand(ON, 0);
1542
Evan Cheng61ca74b2005-11-30 02:04:11 +00001543 RemoveNodeFromCSEMaps(N);
1544 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1545 N->setValueTypes(VT);
1546 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001547
1548 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001549 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001550}
1551
Chris Lattnereb19e402005-11-30 22:45:14 +00001552SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1553 MVT::ValueType VT1, MVT::ValueType VT2,
1554 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001555 // If an identical node already exists, use it.
1556 std::vector<SDOperand> OpList;
1557 OpList.push_back(Op1); OpList.push_back(Op2);
1558 std::vector<MVT::ValueType> VTList;
1559 VTList.push_back(VT1); VTList.push_back(VT2);
1560 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1561 std::make_pair(VTList, OpList))];
1562 if (ON) return SDOperand(ON, 0);
1563
Chris Lattner0fb094f2005-11-19 01:44:53 +00001564 RemoveNodeFromCSEMaps(N);
1565 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1566 setNodeValueTypes(N, VT1, VT2);
1567 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001568
1569 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001570 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001571}
1572
Chris Lattnereb19e402005-11-30 22:45:14 +00001573SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1574 MVT::ValueType VT1, MVT::ValueType VT2,
1575 SDOperand Op1, SDOperand Op2,
1576 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001577 // If an identical node already exists, use it.
1578 std::vector<SDOperand> OpList;
1579 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1580 std::vector<MVT::ValueType> VTList;
1581 VTList.push_back(VT1); VTList.push_back(VT2);
1582 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1583 std::make_pair(VTList, OpList))];
1584 if (ON) return SDOperand(ON, 0);
1585
Chris Lattner0fb094f2005-11-19 01:44:53 +00001586 RemoveNodeFromCSEMaps(N);
1587 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1588 setNodeValueTypes(N, VT1, VT2);
1589 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001590
1591 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001592 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001593}
1594
Chris Lattnereb19e402005-11-30 22:45:14 +00001595SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1596 MVT::ValueType VT1, MVT::ValueType VT2,
1597 SDOperand Op1, SDOperand Op2,
1598 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001599 // If an identical node already exists, use it.
1600 std::vector<SDOperand> OpList;
1601 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1602 OpList.push_back(Op4);
1603 std::vector<MVT::ValueType> VTList;
1604 VTList.push_back(VT1); VTList.push_back(VT2);
1605 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1606 std::make_pair(VTList, OpList))];
1607 if (ON) return SDOperand(ON, 0);
1608
Chris Lattner0fb094f2005-11-19 01:44:53 +00001609 RemoveNodeFromCSEMaps(N);
1610 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1611 setNodeValueTypes(N, VT1, VT2);
1612 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001613
1614 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001615 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001616}
1617
Chris Lattnereb19e402005-11-30 22:45:14 +00001618SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1619 MVT::ValueType VT1, MVT::ValueType VT2,
1620 SDOperand Op1, SDOperand Op2,
1621 SDOperand Op3, SDOperand Op4,
1622 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001623 // If an identical node already exists, use it.
1624 std::vector<SDOperand> OpList;
1625 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1626 OpList.push_back(Op4); OpList.push_back(Op5);
1627 std::vector<MVT::ValueType> VTList;
1628 VTList.push_back(VT1); VTList.push_back(VT2);
1629 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1630 std::make_pair(VTList, OpList))];
1631 if (ON) return SDOperand(ON, 0);
1632
Chris Lattner0fb094f2005-11-19 01:44:53 +00001633 RemoveNodeFromCSEMaps(N);
1634 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1635 setNodeValueTypes(N, VT1, VT2);
1636 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001637
1638 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001639 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001640}
1641
1642// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00001643/// This can cause recursive merging of nodes in the DAG.
1644///
Chris Lattner8b52f212005-08-26 18:36:28 +00001645/// This version assumes From/To have a single result value.
1646///
Chris Lattner1e111c72005-09-07 05:37:01 +00001647void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1648 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001649 SDNode *From = FromN.Val, *To = ToN.Val;
1650 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1651 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001652 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001653
Chris Lattner8b8749f2005-08-17 19:00:20 +00001654 while (!From->use_empty()) {
1655 // Process users until they are all gone.
1656 SDNode *U = *From->use_begin();
1657
1658 // This node is about to morph, remove its old self from the CSE maps.
1659 RemoveNodeFromCSEMaps(U);
1660
Chris Lattner65113b22005-11-08 22:07:03 +00001661 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1662 I != E; ++I)
1663 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001664 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001665 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001666 To->addUser(U);
1667 }
1668
1669 // Now that we have modified U, add it back to the CSE maps. If it already
1670 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001671 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1672 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001673 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001674 if (Deleted) Deleted->push_back(U);
1675 DeleteNodeNotInCSEMaps(U);
1676 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001677 }
1678}
1679
1680/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1681/// This can cause recursive merging of nodes in the DAG.
1682///
1683/// This version assumes From/To have matching types and numbers of result
1684/// values.
1685///
Chris Lattner1e111c72005-09-07 05:37:01 +00001686void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1687 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001688 assert(From != To && "Cannot replace uses of with self");
1689 assert(From->getNumValues() == To->getNumValues() &&
1690 "Cannot use this version of ReplaceAllUsesWith!");
1691 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001692 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001693 return;
1694 }
1695
1696 while (!From->use_empty()) {
1697 // Process users until they are all gone.
1698 SDNode *U = *From->use_begin();
1699
1700 // This node is about to morph, remove its old self from the CSE maps.
1701 RemoveNodeFromCSEMaps(U);
1702
Chris Lattner65113b22005-11-08 22:07:03 +00001703 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1704 I != E; ++I)
1705 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001706 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001707 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001708 To->addUser(U);
1709 }
1710
1711 // Now that we have modified U, add it back to the CSE maps. If it already
1712 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001713 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1714 ReplaceAllUsesWith(U, Existing, Deleted);
1715 // U is now dead.
1716 if (Deleted) Deleted->push_back(U);
1717 DeleteNodeNotInCSEMaps(U);
1718 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001719 }
1720}
1721
Chris Lattner8b52f212005-08-26 18:36:28 +00001722/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1723/// This can cause recursive merging of nodes in the DAG.
1724///
1725/// This version can replace From with any result values. To must match the
1726/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001727void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001728 const std::vector<SDOperand> &To,
1729 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001730 assert(From->getNumValues() == To.size() &&
1731 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001732 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001733 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001734 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001735 return;
1736 }
1737
1738 while (!From->use_empty()) {
1739 // Process users until they are all gone.
1740 SDNode *U = *From->use_begin();
1741
1742 // This node is about to morph, remove its old self from the CSE maps.
1743 RemoveNodeFromCSEMaps(U);
1744
Chris Lattner65113b22005-11-08 22:07:03 +00001745 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1746 I != E; ++I)
1747 if (I->Val == From) {
1748 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001749 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001750 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001751 ToOp.Val->addUser(U);
1752 }
1753
1754 // Now that we have modified U, add it back to the CSE maps. If it already
1755 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001756 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1757 ReplaceAllUsesWith(U, Existing, Deleted);
1758 // U is now dead.
1759 if (Deleted) Deleted->push_back(U);
1760 DeleteNodeNotInCSEMaps(U);
1761 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001762 }
1763}
1764
1765
Jim Laskey58b968b2005-08-17 20:08:02 +00001766//===----------------------------------------------------------------------===//
1767// SDNode Class
1768//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001769
Chris Lattnera3255112005-11-08 23:30:28 +00001770
1771/// getValueTypeList - Return a pointer to the specified value type.
1772///
1773MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1774 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1775 VTs[VT] = VT;
1776 return &VTs[VT];
1777}
1778
Chris Lattner5c884562005-01-12 18:37:47 +00001779/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1780/// indicated value. This method ignores uses of other values defined by this
1781/// operation.
1782bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1783 assert(Value < getNumValues() && "Bad value!");
1784
1785 // If there is only one value, this is easy.
1786 if (getNumValues() == 1)
1787 return use_size() == NUses;
1788 if (Uses.size() < NUses) return false;
1789
1790 SDOperand TheValue(this, Value);
1791
1792 std::set<SDNode*> UsersHandled;
1793
1794 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1795 UI != E; ++UI) {
1796 SDNode *User = *UI;
1797 if (User->getNumOperands() == 1 ||
1798 UsersHandled.insert(User).second) // First time we've seen this?
1799 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1800 if (User->getOperand(i) == TheValue) {
1801 if (NUses == 0)
1802 return false; // too many uses
1803 --NUses;
1804 }
1805 }
1806
1807 // Found exactly the right number of uses?
1808 return NUses == 0;
1809}
1810
1811
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001812const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001813 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001814 default:
1815 if (getOpcode() < ISD::BUILTIN_OP_END)
1816 return "<<Unknown DAG Node>>";
1817 else {
1818 if (G)
1819 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001820 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1821 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001822 return "<<Unknown Target Node>>";
1823 }
1824
Andrew Lenharth95762122005-03-31 21:24:06 +00001825 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001826 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001827 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001828 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00001829 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001830 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001831 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001832 case ISD::AssertSext: return "AssertSext";
1833 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001834 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001835 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001836 case ISD::ConstantFP: return "ConstantFP";
1837 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001838 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001839 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001840 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001841 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001842 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001843 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001844 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001845 case ISD::ConstantPool: return "ConstantPool";
1846 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001847 case ISD::CopyToReg: return "CopyToReg";
1848 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001849 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001850 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001851
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001852 // Unary operators
1853 case ISD::FABS: return "fabs";
1854 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001855 case ISD::FSQRT: return "fsqrt";
1856 case ISD::FSIN: return "fsin";
1857 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001858
1859 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001860 case ISD::ADD: return "add";
1861 case ISD::SUB: return "sub";
1862 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001863 case ISD::MULHU: return "mulhu";
1864 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001865 case ISD::SDIV: return "sdiv";
1866 case ISD::UDIV: return "udiv";
1867 case ISD::SREM: return "srem";
1868 case ISD::UREM: return "urem";
1869 case ISD::AND: return "and";
1870 case ISD::OR: return "or";
1871 case ISD::XOR: return "xor";
1872 case ISD::SHL: return "shl";
1873 case ISD::SRA: return "sra";
1874 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001875 case ISD::FADD: return "fadd";
1876 case ISD::FSUB: return "fsub";
1877 case ISD::FMUL: return "fmul";
1878 case ISD::FDIV: return "fdiv";
1879 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001880 case ISD::VADD: return "vadd";
1881 case ISD::VSUB: return "vsub";
1882 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00001883
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001884 case ISD::SETCC: return "setcc";
1885 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001886 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001887 case ISD::ADD_PARTS: return "add_parts";
1888 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001889 case ISD::SHL_PARTS: return "shl_parts";
1890 case ISD::SRA_PARTS: return "sra_parts";
1891 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001892
Chris Lattner7f644642005-04-28 21:44:03 +00001893 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001894 case ISD::SIGN_EXTEND: return "sign_extend";
1895 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001896 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001897 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001898 case ISD::TRUNCATE: return "truncate";
1899 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001900 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001901 case ISD::FP_EXTEND: return "fp_extend";
1902
1903 case ISD::SINT_TO_FP: return "sint_to_fp";
1904 case ISD::UINT_TO_FP: return "uint_to_fp";
1905 case ISD::FP_TO_SINT: return "fp_to_sint";
1906 case ISD::FP_TO_UINT: return "fp_to_uint";
1907
1908 // Control flow instructions
1909 case ISD::BR: return "br";
1910 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001911 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001912 case ISD::BR_CC: return "br_cc";
1913 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001914 case ISD::RET: return "ret";
1915 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001916 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001917 case ISD::CALLSEQ_START: return "callseq_start";
1918 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001919
1920 // Other operators
1921 case ISD::LOAD: return "load";
1922 case ISD::STORE: return "store";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001923 case ISD::VLOAD: return "vload";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001924 case ISD::EXTLOAD: return "extload";
1925 case ISD::SEXTLOAD: return "sextload";
1926 case ISD::ZEXTLOAD: return "zextload";
1927 case ISD::TRUNCSTORE: return "truncstore";
1928
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001929 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1930 case ISD::EXTRACT_ELEMENT: return "extract_element";
1931 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001932 case ISD::MEMSET: return "memset";
1933 case ISD::MEMCPY: return "memcpy";
1934 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001935
Chris Lattner276260b2005-05-11 04:50:30 +00001936 // Bit counting
1937 case ISD::CTPOP: return "ctpop";
1938 case ISD::CTTZ: return "cttz";
1939 case ISD::CTLZ: return "ctlz";
1940
1941 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001942 case ISD::READPORT: return "readport";
1943 case ISD::WRITEPORT: return "writeport";
1944 case ISD::READIO: return "readio";
1945 case ISD::WRITEIO: return "writeio";
1946
Chris Lattner36ce6912005-11-29 06:21:05 +00001947 // Debug info
1948 case ISD::LOCATION: return "location";
1949
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001950 case ISD::CONDCODE:
1951 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001952 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001953 case ISD::SETOEQ: return "setoeq";
1954 case ISD::SETOGT: return "setogt";
1955 case ISD::SETOGE: return "setoge";
1956 case ISD::SETOLT: return "setolt";
1957 case ISD::SETOLE: return "setole";
1958 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001959
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001960 case ISD::SETO: return "seto";
1961 case ISD::SETUO: return "setuo";
1962 case ISD::SETUEQ: return "setue";
1963 case ISD::SETUGT: return "setugt";
1964 case ISD::SETUGE: return "setuge";
1965 case ISD::SETULT: return "setult";
1966 case ISD::SETULE: return "setule";
1967 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001968
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001969 case ISD::SETEQ: return "seteq";
1970 case ISD::SETGT: return "setgt";
1971 case ISD::SETGE: return "setge";
1972 case ISD::SETLT: return "setlt";
1973 case ISD::SETLE: return "setle";
1974 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001975 }
1976 }
1977}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001978
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001979void SDNode::dump() const { dump(0); }
1980void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001981 std::cerr << (void*)this << ": ";
1982
1983 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1984 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001985 if (getValueType(i) == MVT::Other)
1986 std::cerr << "ch";
1987 else
1988 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001989 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001990 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001991
1992 std::cerr << " ";
1993 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1994 if (i) std::cerr << ", ";
1995 std::cerr << (void*)getOperand(i).Val;
1996 if (unsigned RN = getOperand(i).ResNo)
1997 std::cerr << ":" << RN;
1998 }
1999
2000 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2001 std::cerr << "<" << CSDN->getValue() << ">";
2002 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2003 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002004 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002005 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002006 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002007 std::cerr << "<";
2008 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002009 if (offset > 0)
2010 std::cerr << " + " << offset;
2011 else
2012 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002013 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002014 std::cerr << "<" << FIDN->getIndex() << ">";
2015 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002016 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002017 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002018 std::cerr << "<";
2019 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2020 if (LBB)
2021 std::cerr << LBB->getName() << " ";
2022 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002023 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002024 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2025 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2026 } else {
2027 std::cerr << " #" << R->getReg();
2028 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002029 } else if (const ExternalSymbolSDNode *ES =
2030 dyn_cast<ExternalSymbolSDNode>(this)) {
2031 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002032 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2033 if (M->getValue())
2034 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2035 else
2036 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002037 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2038 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002039 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002040}
2041
Chris Lattnerde202b32005-11-09 23:47:37 +00002042static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002043 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2044 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002045 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002046 else
2047 std::cerr << "\n" << std::string(indent+2, ' ')
2048 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002049
Chris Lattnerea946cd2005-01-09 20:38:33 +00002050
2051 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002052 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002053}
2054
Chris Lattnerc3aae252005-01-07 07:46:32 +00002055void SelectionDAG::dump() const {
2056 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002057 std::vector<const SDNode*> Nodes;
2058 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2059 I != E; ++I)
2060 Nodes.push_back(I);
2061
Chris Lattner49d24712005-01-09 20:26:36 +00002062 std::sort(Nodes.begin(), Nodes.end());
2063
2064 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002065 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002066 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002067 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002068
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002069 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002070
Chris Lattnerc3aae252005-01-07 07:46:32 +00002071 std::cerr << "\n\n";
2072}
2073