blob: f9ea84f3010caefe4d255c2c8cd6a01944b9581d [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 ||
Chris Lattnerfe14b342005-12-01 23:14:50 +0000391 N->getOpcode() == ISD::CALLSEQ_END ||
392 N->getOpcode() == ISD::HANDLENODE)
393 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000394
Chris Lattnerfe14b342005-12-01 23:14:50 +0000395 if (N->getNumValues() == 1) {
396 if (N->getNumOperands() == 1) {
397 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
398 std::make_pair(N->getOperand(0),
399 N->getValueType(0)))];
400 if (U) return U;
401 U = N;
402 } else if (N->getNumOperands() == 2) {
403 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
405 N->getOperand(1)))];
406 if (B) return B;
407 B = N;
408 } else {
409 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
410 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
411 std::make_pair(N->getValueType(0), Ops))];
412 if (ORN) return ORN;
413 ORN = N;
414 }
415 } else {
416 if (N->getOpcode() == ISD::LOAD) {
417 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
418 std::make_pair(N->getOperand(0),
419 N->getValueType(0)))];
420 if (L) return L;
421 L = N;
422 } else {
423 // Remove the node from the ArbitraryNodes map.
424 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
425 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
426 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
427 std::make_pair(RV, Ops))];
428 if (AN) return AN;
429 AN = N;
430 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000431 }
432 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000433}
434
435
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000436
Chris Lattner78ec3112003-08-11 14:57:33 +0000437SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000438 while (!AllNodes.empty()) {
439 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000440 delete [] N->OperandList;
441 N->OperandList = 0;
442 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000443 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000444 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000445}
446
Chris Lattner0f2287b2005-04-13 02:38:18 +0000447SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000448 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000449 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000450 return getNode(ISD::AND, Op.getValueType(), Op,
451 getConstant(Imm, Op.getValueType()));
452}
453
Chris Lattnerc3aae252005-01-07 07:46:32 +0000454SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
455 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
456 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000457 if (VT != MVT::i64)
458 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000459
Chris Lattnerc3aae252005-01-07 07:46:32 +0000460 SDNode *&N = Constants[std::make_pair(Val, VT)];
461 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000462 N = new ConstantSDNode(false, Val, VT);
463 AllNodes.push_back(N);
464 return SDOperand(N, 0);
465}
466
Chris Lattner36ce6912005-11-29 06:21:05 +0000467SDOperand SelectionDAG::getString(const std::string &Val) {
468 StringSDNode *&N = StringNodes[Val];
469 if (!N) {
470 N = new StringSDNode(Val);
471 AllNodes.push_back(N);
472 }
473 return SDOperand(N, 0);
474}
475
Chris Lattner37bfbb42005-08-17 00:34:06 +0000476SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
477 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
478 // Mask out any bits that are not valid for this constant.
479 if (VT != MVT::i64)
480 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
481
482 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
483 if (N) return SDOperand(N, 0);
484 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000485 AllNodes.push_back(N);
486 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000487}
488
Chris Lattnerc3aae252005-01-07 07:46:32 +0000489SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
490 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
491 if (VT == MVT::f32)
492 Val = (float)Val; // Mask out extra precision.
493
Chris Lattnerd8658612005-02-17 20:17:32 +0000494 // Do the map lookup using the actual bit pattern for the floating point
495 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
496 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000497 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000498 if (N) return SDOperand(N, 0);
499 N = new ConstantFPSDNode(Val, VT);
500 AllNodes.push_back(N);
501 return SDOperand(N, 0);
502}
503
Chris Lattnerc3aae252005-01-07 07:46:32 +0000504SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000505 MVT::ValueType VT, int offset) {
506 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000507 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000508 N = new GlobalAddressSDNode(false, GV, VT);
509 AllNodes.push_back(N);
510 return SDOperand(N, 0);
511}
512
513SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000514 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000515 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000516 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000517 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000518 AllNodes.push_back(N);
519 return SDOperand(N, 0);
520}
521
522SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
523 SDNode *&N = FrameIndices[FI];
524 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000525 N = new FrameIndexSDNode(FI, VT, false);
526 AllNodes.push_back(N);
527 return SDOperand(N, 0);
528}
529
530SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
531 SDNode *&N = TargetFrameIndices[FI];
532 if (N) return SDOperand(N, 0);
533 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000534 AllNodes.push_back(N);
535 return SDOperand(N, 0);
536}
537
Chris Lattner5839bf22005-08-26 17:15:30 +0000538SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
539 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000540 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000541 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000542 AllNodes.push_back(N);
543 return SDOperand(N, 0);
544}
545
Chris Lattner5839bf22005-08-26 17:15:30 +0000546SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
547 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000548 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000549 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000550 AllNodes.push_back(N);
551 return SDOperand(N, 0);
552}
553
554SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
555 SDNode *&N = BBNodes[MBB];
556 if (N) return SDOperand(N, 0);
557 N = new BasicBlockSDNode(MBB);
558 AllNodes.push_back(N);
559 return SDOperand(N, 0);
560}
561
Chris Lattner15e4b012005-07-10 00:07:11 +0000562SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
563 if ((unsigned)VT >= ValueTypeNodes.size())
564 ValueTypeNodes.resize(VT+1);
565 if (ValueTypeNodes[VT] == 0) {
566 ValueTypeNodes[VT] = new VTSDNode(VT);
567 AllNodes.push_back(ValueTypeNodes[VT]);
568 }
569
570 return SDOperand(ValueTypeNodes[VT], 0);
571}
572
Chris Lattnerc3aae252005-01-07 07:46:32 +0000573SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
574 SDNode *&N = ExternalSymbols[Sym];
575 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000576 N = new ExternalSymbolSDNode(false, Sym, VT);
577 AllNodes.push_back(N);
578 return SDOperand(N, 0);
579}
580
581SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
582 SDNode *&N = TargetExternalSymbols[Sym];
583 if (N) return SDOperand(N, 0);
584 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000585 AllNodes.push_back(N);
586 return SDOperand(N, 0);
587}
588
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000589SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
590 if ((unsigned)Cond >= CondCodeNodes.size())
591 CondCodeNodes.resize(Cond+1);
592
Chris Lattner079a27a2005-08-09 20:40:02 +0000593 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000594 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000595 AllNodes.push_back(CondCodeNodes[Cond]);
596 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000597 return SDOperand(CondCodeNodes[Cond], 0);
598}
599
Chris Lattner0fdd7682005-08-30 22:38:38 +0000600SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
601 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
602 if (!Reg) {
603 Reg = new RegisterSDNode(RegNo, VT);
604 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000605 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000606 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000607}
608
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000609SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
610 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000611 // These setcc operations always fold.
612 switch (Cond) {
613 default: break;
614 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000615 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000616 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000617 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000618 }
619
Chris Lattner67255a12005-04-07 18:14:58 +0000620 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
621 uint64_t C2 = N2C->getValue();
622 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
623 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000624
Chris Lattnerc3aae252005-01-07 07:46:32 +0000625 // Sign extend the operands if required
626 if (ISD::isSignedIntSetCC(Cond)) {
627 C1 = N1C->getSignExtended();
628 C2 = N2C->getSignExtended();
629 }
630
631 switch (Cond) {
632 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000633 case ISD::SETEQ: return getConstant(C1 == C2, VT);
634 case ISD::SETNE: return getConstant(C1 != C2, VT);
635 case ISD::SETULT: return getConstant(C1 < C2, VT);
636 case ISD::SETUGT: return getConstant(C1 > C2, VT);
637 case ISD::SETULE: return getConstant(C1 <= C2, VT);
638 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
639 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
640 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
641 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
642 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000643 }
Chris Lattner24673922005-04-07 18:58:54 +0000644 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000645 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000646 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
647 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
648
649 // If the comparison constant has bits in the upper part, the
650 // zero-extended value could never match.
651 if (C2 & (~0ULL << InSize)) {
652 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
653 switch (Cond) {
654 case ISD::SETUGT:
655 case ISD::SETUGE:
656 case ISD::SETEQ: return getConstant(0, VT);
657 case ISD::SETULT:
658 case ISD::SETULE:
659 case ISD::SETNE: return getConstant(1, VT);
660 case ISD::SETGT:
661 case ISD::SETGE:
662 // True if the sign bit of C2 is set.
663 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
664 case ISD::SETLT:
665 case ISD::SETLE:
666 // True if the sign bit of C2 isn't set.
667 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
668 default:
669 break;
670 }
671 }
672
673 // Otherwise, we can perform the comparison with the low bits.
674 switch (Cond) {
675 case ISD::SETEQ:
676 case ISD::SETNE:
677 case ISD::SETUGT:
678 case ISD::SETUGE:
679 case ISD::SETULT:
680 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000681 return getSetCC(VT, N1.getOperand(0),
682 getConstant(C2, N1.getOperand(0).getValueType()),
683 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000684 default:
685 break; // todo, be more careful with signed comparisons
686 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000687 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
688 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
689 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
690 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
691 MVT::ValueType ExtDstTy = N1.getValueType();
692 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000693
Chris Lattner7b2880c2005-08-24 22:44:39 +0000694 // If the extended part has any inconsistent bits, it cannot ever
695 // compare equal. In other words, they have to be all ones or all
696 // zeros.
697 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000698 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000699 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
700 return getConstant(Cond == ISD::SETNE, VT);
701
702 // Otherwise, make this a use of a zext.
703 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000704 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000705 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000706 }
707
Chris Lattner67255a12005-04-07 18:14:58 +0000708 uint64_t MinVal, MaxVal;
709 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
710 if (ISD::isSignedIntSetCC(Cond)) {
711 MinVal = 1ULL << (OperandBitSize-1);
712 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
713 MaxVal = ~0ULL >> (65-OperandBitSize);
714 else
715 MaxVal = 0;
716 } else {
717 MinVal = 0;
718 MaxVal = ~0ULL >> (64-OperandBitSize);
719 }
720
721 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
722 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
723 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
724 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000725 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
726 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000727 }
728
729 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
730 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
731 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000732 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
733 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000734 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000735
Nate Begeman72ea2812005-04-14 08:56:52 +0000736 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
737 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000738
Nate Begeman72ea2812005-04-14 08:56:52 +0000739 // Canonicalize setgt X, Min --> setne X, Min
740 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000741 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000742
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000743 // If we have setult X, 1, turn it into seteq X, 0
744 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000745 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
746 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000747 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000748 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000749 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
750 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000751
752 // If we have "setcc X, C1", check to see if we can shrink the immediate
753 // by changing cc.
754
755 // SETUGT X, SINTMAX -> SETLT X, 0
756 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
757 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000758 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000759
760 // FIXME: Implement the rest of these.
761
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000762
763 // Fold bit comparisons when we can.
764 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
765 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
766 if (ConstantSDNode *AndRHS =
767 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
768 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
769 // Perform the xform if the AND RHS is a single bit.
770 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
771 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000772 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000773 TLI.getShiftAmountTy()));
774 }
775 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
776 // (X & 8) == 8 --> (X & 8) >> 3
777 // Perform the xform if C2 is a single bit.
778 if ((C2 & (C2-1)) == 0) {
779 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000780 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000781 }
782 }
783 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784 }
Chris Lattner67255a12005-04-07 18:14:58 +0000785 } else if (isa<ConstantSDNode>(N1.Val)) {
786 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000787 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000788 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000789
790 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
791 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
792 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000793
Chris Lattnerc3aae252005-01-07 07:46:32 +0000794 switch (Cond) {
795 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000796 case ISD::SETEQ: return getConstant(C1 == C2, VT);
797 case ISD::SETNE: return getConstant(C1 != C2, VT);
798 case ISD::SETLT: return getConstant(C1 < C2, VT);
799 case ISD::SETGT: return getConstant(C1 > C2, VT);
800 case ISD::SETLE: return getConstant(C1 <= C2, VT);
801 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000802 }
803 } else {
804 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000805 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000806 }
807
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000808 // Could not fold it.
809 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810}
811
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000813///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000814SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000815 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
816 if (!N) {
817 N = new SDNode(Opcode, VT);
818 AllNodes.push_back(N);
819 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820 return SDOperand(N, 0);
821}
822
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
824 SDOperand Operand) {
825 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
826 uint64_t Val = C->getValue();
827 switch (Opcode) {
828 default: break;
829 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000830 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000831 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
832 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000833 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
834 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000835 }
836 }
837
838 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
839 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000840 case ISD::FNEG:
841 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000842 case ISD::FP_ROUND:
843 case ISD::FP_EXTEND:
844 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000845 case ISD::FP_TO_SINT:
846 return getConstant((int64_t)C->getValue(), VT);
847 case ISD::FP_TO_UINT:
848 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849 }
850
851 unsigned OpOpcode = Operand.Val->getOpcode();
852 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000853 case ISD::TokenFactor:
854 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000855 case ISD::SIGN_EXTEND:
856 if (Operand.getValueType() == VT) return Operand; // noop extension
857 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
858 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
859 break;
860 case ISD::ZERO_EXTEND:
861 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000862 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000863 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000864 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000865 case ISD::ANY_EXTEND:
866 if (Operand.getValueType() == VT) return Operand; // noop extension
867 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
868 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
869 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
870 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000871 case ISD::TRUNCATE:
872 if (Operand.getValueType() == VT) return Operand; // noop truncate
873 if (OpOpcode == ISD::TRUNCATE)
874 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000875 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
876 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000877 // If the source is smaller than the dest, we still need an extend.
878 if (Operand.Val->getOperand(0).getValueType() < VT)
879 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
880 else if (Operand.Val->getOperand(0).getValueType() > VT)
881 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
882 else
883 return Operand.Val->getOperand(0);
884 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000885 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000886 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000887 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
888 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000889 Operand.Val->getOperand(0));
890 if (OpOpcode == ISD::FNEG) // --X -> X
891 return Operand.Val->getOperand(0);
892 break;
893 case ISD::FABS:
894 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
895 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
896 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000897 }
898
Chris Lattner43247a12005-08-25 19:12:10 +0000899 SDNode *N;
900 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
901 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000902 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000903 E = N = new SDNode(Opcode, Operand);
904 } else {
905 N = new SDNode(Opcode, Operand);
906 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000907 N->setValueTypes(VT);
908 AllNodes.push_back(N);
909 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000910}
911
Chris Lattner36019aa2005-04-18 03:48:41 +0000912
913
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
915 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000916#ifndef NDEBUG
917 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000918 case ISD::TokenFactor:
919 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
920 N2.getValueType() == MVT::Other && "Invalid token factor!");
921 break;
Chris Lattner76365122005-01-16 02:23:22 +0000922 case ISD::AND:
923 case ISD::OR:
924 case ISD::XOR:
925 case ISD::UDIV:
926 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000927 case ISD::MULHU:
928 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000929 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
930 // fall through
931 case ISD::ADD:
932 case ISD::SUB:
933 case ISD::MUL:
934 case ISD::SDIV:
935 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000936 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
937 // fall through.
938 case ISD::FADD:
939 case ISD::FSUB:
940 case ISD::FMUL:
941 case ISD::FDIV:
942 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000943 assert(N1.getValueType() == N2.getValueType() &&
944 N1.getValueType() == VT && "Binary operator types must match!");
945 break;
946
947 case ISD::SHL:
948 case ISD::SRA:
949 case ISD::SRL:
950 assert(VT == N1.getValueType() &&
951 "Shift operators return type must be the same as their first arg");
952 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000953 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000954 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000955 case ISD::FP_ROUND_INREG: {
956 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
957 assert(VT == N1.getValueType() && "Not an inreg round!");
958 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
959 "Cannot FP_ROUND_INREG integer types");
960 assert(EVT <= VT && "Not rounding down!");
961 break;
962 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000963 case ISD::AssertSext:
964 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +0000965 case ISD::SIGN_EXTEND_INREG: {
966 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
967 assert(VT == N1.getValueType() && "Not an inreg extend!");
968 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
969 "Cannot *_EXTEND_INREG FP types");
970 assert(EVT <= VT && "Not extending!");
971 }
972
Chris Lattner76365122005-01-16 02:23:22 +0000973 default: break;
974 }
975#endif
976
Chris Lattnerc3aae252005-01-07 07:46:32 +0000977 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
978 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
979 if (N1C) {
980 if (N2C) {
981 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
982 switch (Opcode) {
983 case ISD::ADD: return getConstant(C1 + C2, VT);
984 case ISD::SUB: return getConstant(C1 - C2, VT);
985 case ISD::MUL: return getConstant(C1 * C2, VT);
986 case ISD::UDIV:
987 if (C2) return getConstant(C1 / C2, VT);
988 break;
989 case ISD::UREM :
990 if (C2) return getConstant(C1 % C2, VT);
991 break;
992 case ISD::SDIV :
993 if (C2) return getConstant(N1C->getSignExtended() /
994 N2C->getSignExtended(), VT);
995 break;
996 case ISD::SREM :
997 if (C2) return getConstant(N1C->getSignExtended() %
998 N2C->getSignExtended(), VT);
999 break;
1000 case ISD::AND : return getConstant(C1 & C2, VT);
1001 case ISD::OR : return getConstant(C1 | C2, VT);
1002 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001003 case ISD::SHL : return getConstant(C1 << C2, VT);
1004 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001005 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001006 default: break;
1007 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 } else { // Cannonicalize constant to RHS if commutative
1009 if (isCommutativeBinOp(Opcode)) {
1010 std::swap(N1C, N2C);
1011 std::swap(N1, N2);
1012 }
1013 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001014 }
1015
1016 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1017 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001018 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001019 if (N2CFP) {
1020 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1021 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001022 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1023 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1024 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1025 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 if (C2) return getConstantFP(C1 / C2, VT);
1027 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001028 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029 if (C2) return getConstantFP(fmod(C1, C2), VT);
1030 break;
1031 default: break;
1032 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001033 } else { // Cannonicalize constant to RHS if commutative
1034 if (isCommutativeBinOp(Opcode)) {
1035 std::swap(N1CFP, N2CFP);
1036 std::swap(N1, N2);
1037 }
1038 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001039 }
1040
Chris Lattnerc3aae252005-01-07 07:46:32 +00001041 // Finally, fold operations that do not require constants.
1042 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001043 case ISD::FP_ROUND_INREG:
1044 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1045 break;
1046 case ISD::SIGN_EXTEND_INREG: {
1047 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1048 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001049 break;
1050 }
1051
Nate Begemaneea805e2005-04-13 21:23:31 +00001052 // FIXME: figure out how to safely handle things like
1053 // int foo(int x) { return 1 << (x & 255); }
1054 // int bar() { return foo(256); }
1055#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001056 case ISD::SHL:
1057 case ISD::SRL:
1058 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001059 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001060 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001061 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001062 else if (N2.getOpcode() == ISD::AND)
1063 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1064 // If the and is only masking out bits that cannot effect the shift,
1065 // eliminate the and.
1066 unsigned NumBits = MVT::getSizeInBits(VT);
1067 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1068 return getNode(Opcode, VT, N1, N2.getOperand(0));
1069 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001070 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001071#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001072 }
1073
Chris Lattner27e9b412005-05-11 18:57:39 +00001074 // Memoize this node if possible.
1075 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001076 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1077 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001078 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1079 if (BON) return SDOperand(BON, 0);
1080
1081 BON = N = new SDNode(Opcode, N1, N2);
1082 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001083 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001084 }
1085
Chris Lattner3e011362005-05-14 07:45:46 +00001086 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001087 AllNodes.push_back(N);
1088 return SDOperand(N, 0);
1089}
1090
Chris Lattner88de6e72005-05-12 00:17:04 +00001091// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001092// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001093void SDNode::setAdjCallChain(SDOperand N) {
1094 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001095 assert((getOpcode() == ISD::CALLSEQ_START ||
1096 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001097
Chris Lattner65113b22005-11-08 22:07:03 +00001098 OperandList[0].Val->removeUser(this);
1099 OperandList[0] = N;
1100 OperandList[0].Val->Uses.push_back(this);
Chris Lattner27e9b412005-05-11 18:57:39 +00001101}
1102
1103
1104
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001106 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001107 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1109 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001110 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111
1112 // Loads have a token chain.
Chris Lattnera3255112005-11-08 23:30:28 +00001113 setNodeValueTypes(N, VT, MVT::Other);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114 AllNodes.push_back(N);
1115 return SDOperand(N, 0);
1116}
1117
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001118SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1119 SDOperand Chain, SDOperand Ptr,
1120 SDOperand SV) {
1121 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1122 if (N) return SDOperand(N, 0);
1123 std::vector<SDOperand> Ops;
1124 Ops.reserve(5);
1125 Ops.push_back(Chain);
1126 Ops.push_back(Ptr);
1127 Ops.push_back(getConstant(Count, MVT::i32));
1128 Ops.push_back(getValueType(EVT));
1129 Ops.push_back(SV);
1130 std::vector<MVT::ValueType> VTs;
1131 VTs.reserve(2);
Nate Begemanab48be32005-11-22 18:16:00 +00001132 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001133 return getNode(ISD::VLOAD, VTs, Ops);
1134}
Chris Lattner5f056bf2005-07-10 01:55:33 +00001135
1136SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1137 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1138 MVT::ValueType EVT) {
1139 std::vector<SDOperand> Ops;
1140 Ops.reserve(4);
1141 Ops.push_back(Chain);
1142 Ops.push_back(Ptr);
1143 Ops.push_back(SV);
1144 Ops.push_back(getValueType(EVT));
1145 std::vector<MVT::ValueType> VTs;
1146 VTs.reserve(2);
1147 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1148 return getNode(Opcode, VTs, Ops);
1149}
1150
Chris Lattnerc3aae252005-01-07 07:46:32 +00001151SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1152 SDOperand N1, SDOperand N2, SDOperand N3) {
1153 // Perform various simplifications.
1154 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1155 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1156 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1157 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001158 case ISD::SETCC: {
1159 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001160 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001161 if (Simp.Val) return Simp;
1162 break;
1163 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001164 case ISD::SELECT:
1165 if (N1C)
1166 if (N1C->getValue())
1167 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001168 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001169 return N3; // select false, X, Y -> Y
1170
1171 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001173 case ISD::BRCOND:
1174 if (N2C)
1175 if (N2C->getValue()) // Unconditional branch
1176 return getNode(ISD::BR, MVT::Other, N1, N3);
1177 else
1178 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001179 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001180 }
1181
Chris Lattner385328c2005-05-14 07:42:29 +00001182 std::vector<SDOperand> Ops;
1183 Ops.reserve(3);
1184 Ops.push_back(N1);
1185 Ops.push_back(N2);
1186 Ops.push_back(N3);
1187
Chris Lattner43247a12005-08-25 19:12:10 +00001188 // Memoize node if it doesn't produce a flag.
1189 SDNode *N;
1190 if (VT != MVT::Flag) {
1191 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1192 if (E) return SDOperand(E, 0);
1193 E = N = new SDNode(Opcode, N1, N2, N3);
1194 } else {
1195 N = new SDNode(Opcode, N1, N2, N3);
1196 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001197 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001198 AllNodes.push_back(N);
1199 return SDOperand(N, 0);
1200}
1201
1202SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001203 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001204 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001205 std::vector<SDOperand> Ops;
1206 Ops.reserve(4);
1207 Ops.push_back(N1);
1208 Ops.push_back(N2);
1209 Ops.push_back(N3);
1210 Ops.push_back(N4);
1211 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001212}
1213
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001214SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1215 SDOperand N1, SDOperand N2, SDOperand N3,
1216 SDOperand N4, SDOperand N5) {
1217 std::vector<SDOperand> Ops;
1218 Ops.reserve(5);
1219 Ops.push_back(N1);
1220 Ops.push_back(N2);
1221 Ops.push_back(N3);
1222 Ops.push_back(N4);
1223 Ops.push_back(N5);
1224 return getNode(Opcode, VT, Ops);
1225}
1226
1227
Chris Lattner0437cdd2005-05-09 04:14:13 +00001228SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001229 assert((!V || isa<PointerType>(V->getType())) &&
1230 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001231 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1232 if (N) return SDOperand(N, 0);
1233
1234 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001235 AllNodes.push_back(N);
1236 return SDOperand(N, 0);
1237}
1238
1239SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001240 std::vector<SDOperand> &Ops) {
1241 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001243 case 1: return getNode(Opcode, VT, Ops[0]);
1244 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1245 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001246 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001247 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001248
Chris Lattner89c34632005-05-14 06:20:26 +00001249 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001250 switch (Opcode) {
1251 default: break;
1252 case ISD::BRCONDTWOWAY:
1253 if (N1C)
1254 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001255 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001256 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001257 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001258 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001259 case ISD::BRTWOWAY_CC:
1260 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1261 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1262 "LHS and RHS of comparison must have same type!");
1263 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001264 case ISD::TRUNCSTORE: {
1265 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1266 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1267#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1268 // If this is a truncating store of a constant, convert to the desired type
1269 // and store it instead.
1270 if (isa<Constant>(Ops[0])) {
1271 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1272 if (isa<Constant>(Op))
1273 N1 = Op;
1274 }
1275 // Also for ConstantFP?
1276#endif
1277 if (Ops[0].getValueType() == EVT) // Normal store?
1278 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1279 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1280 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1281 "Can't do FP-INT conversion!");
1282 break;
1283 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001284 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001285 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001286 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1287 "LHS and RHS of condition must have same type!");
1288 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1289 "True and False arms of SelectCC must have same type!");
1290 assert(Ops[2].getValueType() == VT &&
1291 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001292 break;
1293 }
1294 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001295 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001296 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1297 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001298 break;
1299 }
Chris Lattneref847df2005-04-09 03:27:28 +00001300 }
1301
Chris Lattner385328c2005-05-14 07:42:29 +00001302 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001303 SDNode *N;
1304 if (VT != MVT::Flag) {
1305 SDNode *&E =
1306 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1307 if (E) return SDOperand(E, 0);
1308 E = N = new SDNode(Opcode, Ops);
1309 } else {
1310 N = new SDNode(Opcode, Ops);
1311 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001312 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001313 AllNodes.push_back(N);
1314 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001315}
1316
Chris Lattner89c34632005-05-14 06:20:26 +00001317SDOperand SelectionDAG::getNode(unsigned Opcode,
1318 std::vector<MVT::ValueType> &ResultTys,
1319 std::vector<SDOperand> &Ops) {
1320 if (ResultTys.size() == 1)
1321 return getNode(Opcode, ResultTys[0], Ops);
1322
Chris Lattner5f056bf2005-07-10 01:55:33 +00001323 switch (Opcode) {
1324 case ISD::EXTLOAD:
1325 case ISD::SEXTLOAD:
1326 case ISD::ZEXTLOAD: {
1327 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1328 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1329 // If they are asking for an extending load from/to the same thing, return a
1330 // normal load.
1331 if (ResultTys[0] == EVT)
1332 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1333 assert(EVT < ResultTys[0] &&
1334 "Should only be an extending load, not truncating!");
1335 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1336 "Cannot sign/zero extend a FP load!");
1337 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1338 "Cannot convert from FP to Int or Int -> FP!");
1339 break;
1340 }
1341
Chris Lattnere89083a2005-05-14 07:25:05 +00001342 // FIXME: figure out how to safely handle things like
1343 // int foo(int x) { return 1 << (x & 255); }
1344 // int bar() { return foo(256); }
1345#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001346 case ISD::SRA_PARTS:
1347 case ISD::SRL_PARTS:
1348 case ISD::SHL_PARTS:
1349 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001350 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001351 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1352 else if (N3.getOpcode() == ISD::AND)
1353 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1354 // If the and is only masking out bits that cannot effect the shift,
1355 // eliminate the and.
1356 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1357 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1358 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1359 }
1360 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001361#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001362 }
Chris Lattner89c34632005-05-14 06:20:26 +00001363
Chris Lattner43247a12005-08-25 19:12:10 +00001364 // Memoize the node unless it returns a flag.
1365 SDNode *N;
1366 if (ResultTys.back() != MVT::Flag) {
1367 SDNode *&E =
1368 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1369 if (E) return SDOperand(E, 0);
1370 E = N = new SDNode(Opcode, Ops);
1371 } else {
1372 N = new SDNode(Opcode, Ops);
1373 }
Chris Lattnera3255112005-11-08 23:30:28 +00001374 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001375 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001376 return SDOperand(N, 0);
1377}
1378
Chris Lattnera3255112005-11-08 23:30:28 +00001379void SelectionDAG::setNodeValueTypes(SDNode *N,
1380 std::vector<MVT::ValueType> &RetVals) {
1381 switch (RetVals.size()) {
1382 case 0: return;
1383 case 1: N->setValueTypes(RetVals[0]); return;
1384 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1385 default: break;
1386 }
1387
1388 std::list<std::vector<MVT::ValueType> >::iterator I =
1389 std::find(VTList.begin(), VTList.end(), RetVals);
1390 if (I == VTList.end()) {
1391 VTList.push_front(RetVals);
1392 I = VTList.begin();
1393 }
1394
1395 N->setValueTypes(&(*I)[0], I->size());
1396}
1397
1398void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1399 MVT::ValueType VT2) {
1400 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1401 E = VTList.end(); I != E; ++I) {
1402 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1403 N->setValueTypes(&(*I)[0], 2);
1404 return;
1405 }
1406 }
1407 std::vector<MVT::ValueType> V;
1408 V.push_back(VT1);
1409 V.push_back(VT2);
1410 VTList.push_front(V);
1411 N->setValueTypes(&(*VTList.begin())[0], 2);
1412}
1413
Chris Lattner149c58c2005-08-16 18:17:10 +00001414
1415/// SelectNodeTo - These are used for target selectors to *mutate* the
1416/// specified node to have the specified return type, Target opcode, and
1417/// operands. Note that target opcodes are stored as
1418/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001419///
1420/// Note that SelectNodeTo returns the resultant node. If there is already a
1421/// node of the specified opcode and operands, it returns that node instead of
1422/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001423SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1424 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001425 // If an identical node already exists, use it.
1426 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1427 if (ON) return SDOperand(ON, 0);
1428
Chris Lattner7651fa42005-08-24 23:00:29 +00001429 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001430
Chris Lattner7651fa42005-08-24 23:00:29 +00001431 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1432 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001433
1434 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001435 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001436}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001437
Chris Lattnereb19e402005-11-30 22:45:14 +00001438SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1439 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001440 // If an identical node already exists, use it.
1441 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1442 std::make_pair(Op1, VT))];
1443 if (ON) return SDOperand(ON, 0);
1444
Chris Lattner149c58c2005-08-16 18:17:10 +00001445 RemoveNodeFromCSEMaps(N);
1446 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1447 N->setValueTypes(VT);
1448 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001449
1450 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001451 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001452}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001453
Chris Lattnereb19e402005-11-30 22:45:14 +00001454SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1455 MVT::ValueType VT, SDOperand Op1,
1456 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001457 // If an identical node already exists, use it.
1458 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1459 std::make_pair(Op1, Op2))];
1460 if (ON) return SDOperand(ON, 0);
1461
Chris Lattner149c58c2005-08-16 18:17:10 +00001462 RemoveNodeFromCSEMaps(N);
1463 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1464 N->setValueTypes(VT);
1465 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001466
1467 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001468 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001469}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001470
Chris Lattnereb19e402005-11-30 22:45:14 +00001471SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1472 MVT::ValueType VT, SDOperand Op1,
1473 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001474 // If an identical node already exists, use it.
1475 std::vector<SDOperand> OpList;
1476 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1477 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1478 std::make_pair(VT, OpList))];
1479 if (ON) return SDOperand(ON, 0);
1480
Chris Lattner149c58c2005-08-16 18:17:10 +00001481 RemoveNodeFromCSEMaps(N);
1482 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1483 N->setValueTypes(VT);
1484 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001485
1486 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001487 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001488}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001489
Chris Lattnereb19e402005-11-30 22:45:14 +00001490SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1491 MVT::ValueType VT, SDOperand Op1,
1492 SDOperand Op2, SDOperand Op3,
1493 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001494 // If an identical node already exists, use it.
1495 std::vector<SDOperand> OpList;
1496 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1497 OpList.push_back(Op4);
1498 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1499 std::make_pair(VT, OpList))];
1500 if (ON) return SDOperand(ON, 0);
1501
Nate Begeman294a0a12005-08-18 07:30:15 +00001502 RemoveNodeFromCSEMaps(N);
1503 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1504 N->setValueTypes(VT);
1505 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001506
1507 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001508 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001509}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001510
Chris Lattnereb19e402005-11-30 22:45:14 +00001511SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1512 MVT::ValueType VT, SDOperand Op1,
1513 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1514 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001515 // If an identical node already exists, use it.
1516 std::vector<SDOperand> OpList;
1517 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1518 OpList.push_back(Op4); OpList.push_back(Op5);
1519 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1520 std::make_pair(VT, OpList))];
1521 if (ON) return SDOperand(ON, 0);
1522
Chris Lattner6b09a292005-08-21 18:49:33 +00001523 RemoveNodeFromCSEMaps(N);
1524 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1525 N->setValueTypes(VT);
1526 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001527
1528 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001529 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001530}
Chris Lattner149c58c2005-08-16 18:17:10 +00001531
Chris Lattnereb19e402005-11-30 22:45:14 +00001532SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1533 MVT::ValueType VT, SDOperand Op1,
1534 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1535 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001536 // If an identical node already exists, use it.
1537 std::vector<SDOperand> OpList;
1538 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1539 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1540 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1541 std::make_pair(VT, OpList))];
1542 if (ON) return SDOperand(ON, 0);
1543
Evan Cheng61ca74b2005-11-30 02:04:11 +00001544 RemoveNodeFromCSEMaps(N);
1545 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1546 N->setValueTypes(VT);
1547 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001548
1549 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001550 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001551}
1552
Chris Lattnereb19e402005-11-30 22:45:14 +00001553SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1554 MVT::ValueType VT1, MVT::ValueType VT2,
1555 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001556 // If an identical node already exists, use it.
1557 std::vector<SDOperand> OpList;
1558 OpList.push_back(Op1); OpList.push_back(Op2);
1559 std::vector<MVT::ValueType> VTList;
1560 VTList.push_back(VT1); VTList.push_back(VT2);
1561 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1562 std::make_pair(VTList, OpList))];
1563 if (ON) return SDOperand(ON, 0);
1564
Chris Lattner0fb094f2005-11-19 01:44:53 +00001565 RemoveNodeFromCSEMaps(N);
1566 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1567 setNodeValueTypes(N, VT1, VT2);
1568 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001569
1570 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001571 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001572}
1573
Chris Lattnereb19e402005-11-30 22:45:14 +00001574SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1575 MVT::ValueType VT1, MVT::ValueType VT2,
1576 SDOperand Op1, SDOperand Op2,
1577 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001578 // If an identical node already exists, use it.
1579 std::vector<SDOperand> OpList;
1580 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1581 std::vector<MVT::ValueType> VTList;
1582 VTList.push_back(VT1); VTList.push_back(VT2);
1583 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1584 std::make_pair(VTList, OpList))];
1585 if (ON) return SDOperand(ON, 0);
1586
Chris Lattner0fb094f2005-11-19 01:44:53 +00001587 RemoveNodeFromCSEMaps(N);
1588 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1589 setNodeValueTypes(N, VT1, VT2);
1590 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001591
1592 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001593 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001594}
1595
Chris Lattnereb19e402005-11-30 22:45:14 +00001596SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1597 MVT::ValueType VT1, MVT::ValueType VT2,
1598 SDOperand Op1, SDOperand Op2,
1599 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001600 // If an identical node already exists, use it.
1601 std::vector<SDOperand> OpList;
1602 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1603 OpList.push_back(Op4);
1604 std::vector<MVT::ValueType> VTList;
1605 VTList.push_back(VT1); VTList.push_back(VT2);
1606 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1607 std::make_pair(VTList, OpList))];
1608 if (ON) return SDOperand(ON, 0);
1609
Chris Lattner0fb094f2005-11-19 01:44:53 +00001610 RemoveNodeFromCSEMaps(N);
1611 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1612 setNodeValueTypes(N, VT1, VT2);
1613 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001614
1615 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001616 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001617}
1618
Chris Lattnereb19e402005-11-30 22:45:14 +00001619SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1620 MVT::ValueType VT1, MVT::ValueType VT2,
1621 SDOperand Op1, SDOperand Op2,
1622 SDOperand Op3, SDOperand Op4,
1623 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001624 // If an identical node already exists, use it.
1625 std::vector<SDOperand> OpList;
1626 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1627 OpList.push_back(Op4); OpList.push_back(Op5);
1628 std::vector<MVT::ValueType> VTList;
1629 VTList.push_back(VT1); VTList.push_back(VT2);
1630 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1631 std::make_pair(VTList, OpList))];
1632 if (ON) return SDOperand(ON, 0);
1633
Chris Lattner0fb094f2005-11-19 01:44:53 +00001634 RemoveNodeFromCSEMaps(N);
1635 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1636 setNodeValueTypes(N, VT1, VT2);
1637 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001638
1639 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001640 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001641}
1642
1643// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00001644/// This can cause recursive merging of nodes in the DAG.
1645///
Chris Lattner8b52f212005-08-26 18:36:28 +00001646/// This version assumes From/To have a single result value.
1647///
Chris Lattner1e111c72005-09-07 05:37:01 +00001648void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1649 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001650 SDNode *From = FromN.Val, *To = ToN.Val;
1651 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1652 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001653 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001654
Chris Lattner8b8749f2005-08-17 19:00:20 +00001655 while (!From->use_empty()) {
1656 // Process users until they are all gone.
1657 SDNode *U = *From->use_begin();
1658
1659 // This node is about to morph, remove its old self from the CSE maps.
1660 RemoveNodeFromCSEMaps(U);
1661
Chris Lattner65113b22005-11-08 22:07:03 +00001662 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1663 I != E; ++I)
1664 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001665 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001666 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001667 To->addUser(U);
1668 }
1669
1670 // Now that we have modified U, add it back to the CSE maps. If it already
1671 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001672 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1673 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001674 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001675 if (Deleted) Deleted->push_back(U);
1676 DeleteNodeNotInCSEMaps(U);
1677 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001678 }
1679}
1680
1681/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1682/// This can cause recursive merging of nodes in the DAG.
1683///
1684/// This version assumes From/To have matching types and numbers of result
1685/// values.
1686///
Chris Lattner1e111c72005-09-07 05:37:01 +00001687void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1688 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001689 assert(From != To && "Cannot replace uses of with self");
1690 assert(From->getNumValues() == To->getNumValues() &&
1691 "Cannot use this version of ReplaceAllUsesWith!");
1692 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001693 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001694 return;
1695 }
1696
1697 while (!From->use_empty()) {
1698 // Process users until they are all gone.
1699 SDNode *U = *From->use_begin();
1700
1701 // This node is about to morph, remove its old self from the CSE maps.
1702 RemoveNodeFromCSEMaps(U);
1703
Chris Lattner65113b22005-11-08 22:07:03 +00001704 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1705 I != E; ++I)
1706 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001707 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001708 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001709 To->addUser(U);
1710 }
1711
1712 // Now that we have modified U, add it back to the CSE maps. If it already
1713 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001714 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1715 ReplaceAllUsesWith(U, Existing, Deleted);
1716 // U is now dead.
1717 if (Deleted) Deleted->push_back(U);
1718 DeleteNodeNotInCSEMaps(U);
1719 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001720 }
1721}
1722
Chris Lattner8b52f212005-08-26 18:36:28 +00001723/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1724/// This can cause recursive merging of nodes in the DAG.
1725///
1726/// This version can replace From with any result values. To must match the
1727/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001728void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001729 const std::vector<SDOperand> &To,
1730 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001731 assert(From->getNumValues() == To.size() &&
1732 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001733 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001734 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001735 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001736 return;
1737 }
1738
1739 while (!From->use_empty()) {
1740 // Process users until they are all gone.
1741 SDNode *U = *From->use_begin();
1742
1743 // This node is about to morph, remove its old self from the CSE maps.
1744 RemoveNodeFromCSEMaps(U);
1745
Chris Lattner65113b22005-11-08 22:07:03 +00001746 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1747 I != E; ++I)
1748 if (I->Val == From) {
1749 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001750 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001751 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001752 ToOp.Val->addUser(U);
1753 }
1754
1755 // Now that we have modified U, add it back to the CSE maps. If it already
1756 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001757 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1758 ReplaceAllUsesWith(U, Existing, Deleted);
1759 // U is now dead.
1760 if (Deleted) Deleted->push_back(U);
1761 DeleteNodeNotInCSEMaps(U);
1762 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001763 }
1764}
1765
1766
Jim Laskey58b968b2005-08-17 20:08:02 +00001767//===----------------------------------------------------------------------===//
1768// SDNode Class
1769//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001770
Chris Lattnera3255112005-11-08 23:30:28 +00001771
1772/// getValueTypeList - Return a pointer to the specified value type.
1773///
1774MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1775 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1776 VTs[VT] = VT;
1777 return &VTs[VT];
1778}
1779
Chris Lattner5c884562005-01-12 18:37:47 +00001780/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1781/// indicated value. This method ignores uses of other values defined by this
1782/// operation.
1783bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1784 assert(Value < getNumValues() && "Bad value!");
1785
1786 // If there is only one value, this is easy.
1787 if (getNumValues() == 1)
1788 return use_size() == NUses;
1789 if (Uses.size() < NUses) return false;
1790
1791 SDOperand TheValue(this, Value);
1792
1793 std::set<SDNode*> UsersHandled;
1794
1795 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1796 UI != E; ++UI) {
1797 SDNode *User = *UI;
1798 if (User->getNumOperands() == 1 ||
1799 UsersHandled.insert(User).second) // First time we've seen this?
1800 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1801 if (User->getOperand(i) == TheValue) {
1802 if (NUses == 0)
1803 return false; // too many uses
1804 --NUses;
1805 }
1806 }
1807
1808 // Found exactly the right number of uses?
1809 return NUses == 0;
1810}
1811
1812
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001813const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001814 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001815 default:
1816 if (getOpcode() < ISD::BUILTIN_OP_END)
1817 return "<<Unknown DAG Node>>";
1818 else {
1819 if (G)
1820 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001821 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1822 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001823 return "<<Unknown Target Node>>";
1824 }
1825
Andrew Lenharth95762122005-03-31 21:24:06 +00001826 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001827 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001828 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001829 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00001830 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001831 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001832 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001833 case ISD::AssertSext: return "AssertSext";
1834 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001835 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001836 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001837 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001838 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001839 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001840 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001841 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001842 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001843 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001844 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001845 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001846 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001847 case ISD::ConstantPool: return "ConstantPool";
1848 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001849 case ISD::CopyToReg: return "CopyToReg";
1850 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001851 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001852 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001853
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001854 // Unary operators
1855 case ISD::FABS: return "fabs";
1856 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001857 case ISD::FSQRT: return "fsqrt";
1858 case ISD::FSIN: return "fsin";
1859 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001860
1861 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001862 case ISD::ADD: return "add";
1863 case ISD::SUB: return "sub";
1864 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001865 case ISD::MULHU: return "mulhu";
1866 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001867 case ISD::SDIV: return "sdiv";
1868 case ISD::UDIV: return "udiv";
1869 case ISD::SREM: return "srem";
1870 case ISD::UREM: return "urem";
1871 case ISD::AND: return "and";
1872 case ISD::OR: return "or";
1873 case ISD::XOR: return "xor";
1874 case ISD::SHL: return "shl";
1875 case ISD::SRA: return "sra";
1876 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001877 case ISD::FADD: return "fadd";
1878 case ISD::FSUB: return "fsub";
1879 case ISD::FMUL: return "fmul";
1880 case ISD::FDIV: return "fdiv";
1881 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001882 case ISD::VADD: return "vadd";
1883 case ISD::VSUB: return "vsub";
1884 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00001885
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001886 case ISD::SETCC: return "setcc";
1887 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001888 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001889 case ISD::ADD_PARTS: return "add_parts";
1890 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001891 case ISD::SHL_PARTS: return "shl_parts";
1892 case ISD::SRA_PARTS: return "sra_parts";
1893 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001894
Chris Lattner7f644642005-04-28 21:44:03 +00001895 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001896 case ISD::SIGN_EXTEND: return "sign_extend";
1897 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001898 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001899 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001900 case ISD::TRUNCATE: return "truncate";
1901 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001902 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001903 case ISD::FP_EXTEND: return "fp_extend";
1904
1905 case ISD::SINT_TO_FP: return "sint_to_fp";
1906 case ISD::UINT_TO_FP: return "uint_to_fp";
1907 case ISD::FP_TO_SINT: return "fp_to_sint";
1908 case ISD::FP_TO_UINT: return "fp_to_uint";
1909
1910 // Control flow instructions
1911 case ISD::BR: return "br";
1912 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001913 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001914 case ISD::BR_CC: return "br_cc";
1915 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001916 case ISD::RET: return "ret";
1917 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001918 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001919 case ISD::CALLSEQ_START: return "callseq_start";
1920 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001921
1922 // Other operators
1923 case ISD::LOAD: return "load";
1924 case ISD::STORE: return "store";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001925 case ISD::VLOAD: return "vload";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001926 case ISD::EXTLOAD: return "extload";
1927 case ISD::SEXTLOAD: return "sextload";
1928 case ISD::ZEXTLOAD: return "zextload";
1929 case ISD::TRUNCSTORE: return "truncstore";
1930
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001931 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1932 case ISD::EXTRACT_ELEMENT: return "extract_element";
1933 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001934 case ISD::MEMSET: return "memset";
1935 case ISD::MEMCPY: return "memcpy";
1936 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001937
Chris Lattner276260b2005-05-11 04:50:30 +00001938 // Bit counting
1939 case ISD::CTPOP: return "ctpop";
1940 case ISD::CTTZ: return "cttz";
1941 case ISD::CTLZ: return "ctlz";
1942
1943 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001944 case ISD::READPORT: return "readport";
1945 case ISD::WRITEPORT: return "writeport";
1946 case ISD::READIO: return "readio";
1947 case ISD::WRITEIO: return "writeio";
1948
Chris Lattner36ce6912005-11-29 06:21:05 +00001949 // Debug info
1950 case ISD::LOCATION: return "location";
1951
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001952 case ISD::CONDCODE:
1953 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001954 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001955 case ISD::SETOEQ: return "setoeq";
1956 case ISD::SETOGT: return "setogt";
1957 case ISD::SETOGE: return "setoge";
1958 case ISD::SETOLT: return "setolt";
1959 case ISD::SETOLE: return "setole";
1960 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001961
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001962 case ISD::SETO: return "seto";
1963 case ISD::SETUO: return "setuo";
1964 case ISD::SETUEQ: return "setue";
1965 case ISD::SETUGT: return "setugt";
1966 case ISD::SETUGE: return "setuge";
1967 case ISD::SETULT: return "setult";
1968 case ISD::SETULE: return "setule";
1969 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001970
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001971 case ISD::SETEQ: return "seteq";
1972 case ISD::SETGT: return "setgt";
1973 case ISD::SETGE: return "setge";
1974 case ISD::SETLT: return "setlt";
1975 case ISD::SETLE: return "setle";
1976 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001977 }
1978 }
1979}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001980
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001981void SDNode::dump() const { dump(0); }
1982void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001983 std::cerr << (void*)this << ": ";
1984
1985 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1986 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001987 if (getValueType(i) == MVT::Other)
1988 std::cerr << "ch";
1989 else
1990 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001991 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001992 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001993
1994 std::cerr << " ";
1995 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1996 if (i) std::cerr << ", ";
1997 std::cerr << (void*)getOperand(i).Val;
1998 if (unsigned RN = getOperand(i).ResNo)
1999 std::cerr << ":" << RN;
2000 }
2001
2002 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2003 std::cerr << "<" << CSDN->getValue() << ">";
2004 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2005 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002006 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002007 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002008 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002009 std::cerr << "<";
2010 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002011 if (offset > 0)
2012 std::cerr << " + " << offset;
2013 else
2014 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002015 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002016 std::cerr << "<" << FIDN->getIndex() << ">";
2017 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002018 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002019 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002020 std::cerr << "<";
2021 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2022 if (LBB)
2023 std::cerr << LBB->getName() << " ";
2024 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002025 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002026 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2027 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2028 } else {
2029 std::cerr << " #" << R->getReg();
2030 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002031 } else if (const ExternalSymbolSDNode *ES =
2032 dyn_cast<ExternalSymbolSDNode>(this)) {
2033 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002034 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2035 if (M->getValue())
2036 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2037 else
2038 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002039 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2040 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002041 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002042}
2043
Chris Lattnerde202b32005-11-09 23:47:37 +00002044static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002045 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2046 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002047 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002048 else
2049 std::cerr << "\n" << std::string(indent+2, ' ')
2050 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002051
Chris Lattnerea946cd2005-01-09 20:38:33 +00002052
2053 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002054 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002055}
2056
Chris Lattnerc3aae252005-01-07 07:46:32 +00002057void SelectionDAG::dump() const {
2058 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002059 std::vector<const SDNode*> Nodes;
2060 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2061 I != E; ++I)
2062 Nodes.push_back(I);
2063
Chris Lattner49d24712005-01-09 20:26:36 +00002064 std::sort(Nodes.begin(), Nodes.end());
2065
2066 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002067 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002068 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002069 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002070
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002071 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002072
Chris Lattnerc3aae252005-01-07 07:46:32 +00002073 std::cerr << "\n\n";
2074}
2075