blob: 6f1a263048d715c9a3d697ef8b227d2b3cbda3c1 [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"
Evan Cheng115c0362005-12-19 23:11:49 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000026#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000027#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000028#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner5cdcc582005-01-09 20:52:51 +000031static bool isCommutativeBinOp(unsigned Opcode) {
32 switch (Opcode) {
33 case ISD::ADD:
34 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000035 case ISD::MULHU:
36 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000037 case ISD::FADD:
38 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000039 case ISD::AND:
40 case ISD::OR:
41 case ISD::XOR: return true;
42 default: return false; // FIXME: Need commutative info for user ops!
43 }
44}
45
46static bool isAssociativeBinOp(unsigned Opcode) {
47 switch (Opcode) {
48 case ISD::ADD:
49 case ISD::MUL:
50 case ISD::AND:
51 case ISD::OR:
52 case ISD::XOR: return true;
53 default: return false; // FIXME: Need associative info for user ops!
54 }
55}
56
Chris Lattner5cdcc582005-01-09 20:52:51 +000057// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000061 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000062 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000063 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000064}
65
Jim Laskey58b968b2005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
74bool ConstantFPSDNode::isExactlyValue(double V) const {
75 return DoubleToBits(V) == DoubleToBits(Value);
76}
77
78//===----------------------------------------------------------------------===//
79// ISD Class
80//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000081
Chris Lattnerc3aae252005-01-07 07:46:32 +000082/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
83/// when given the operation for (X op Y).
84ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
85 // To perform this operation, we just need to swap the L and G bits of the
86 // operation.
87 unsigned OldL = (Operation >> 2) & 1;
88 unsigned OldG = (Operation >> 1) & 1;
89 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
90 (OldL << 1) | // New G bit
91 (OldG << 2)); // New L bit.
92}
93
94/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
95/// 'op' is a valid SetCC operation.
96ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
97 unsigned Operation = Op;
98 if (isInteger)
99 Operation ^= 7; // Flip L, G, E bits, but not U.
100 else
101 Operation ^= 15; // Flip all of the condition bits.
102 if (Operation > ISD::SETTRUE2)
103 Operation &= ~8; // Don't let N and U bits get set.
104 return ISD::CondCode(Operation);
105}
106
107
108/// isSignedOp - For an integer comparison, return 1 if the comparison is a
109/// signed operation and 2 if the result is an unsigned comparison. Return zero
110/// if the operation does not depend on the sign of the input (setne and seteq).
111static int isSignedOp(ISD::CondCode Opcode) {
112 switch (Opcode) {
113 default: assert(0 && "Illegal integer setcc operation!");
114 case ISD::SETEQ:
115 case ISD::SETNE: return 0;
116 case ISD::SETLT:
117 case ISD::SETLE:
118 case ISD::SETGT:
119 case ISD::SETGE: return 1;
120 case ISD::SETULT:
121 case ISD::SETULE:
122 case ISD::SETUGT:
123 case ISD::SETUGE: return 2;
124 }
125}
126
127/// getSetCCOrOperation - Return the result of a logical OR between different
128/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
129/// returns SETCC_INVALID if it is not possible to represent the resultant
130/// comparison.
131ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
132 bool isInteger) {
133 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
134 // Cannot fold a signed integer setcc with an unsigned integer setcc.
135 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnerc3aae252005-01-07 07:46:32 +0000137 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000138
Chris Lattnerc3aae252005-01-07 07:46:32 +0000139 // If the N and U bits get set then the resultant comparison DOES suddenly
140 // care about orderedness, and is true when ordered.
141 if (Op > ISD::SETTRUE2)
142 Op &= ~16; // Clear the N bit.
143 return ISD::CondCode(Op);
144}
145
146/// getSetCCAndOperation - Return the result of a logical AND between different
147/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
148/// function returns zero if it is not possible to represent the resultant
149/// comparison.
150ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
151 bool isInteger) {
152 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
153 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000154 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000155
156 // Combine all of the condition bits.
157 return ISD::CondCode(Op1 & Op2);
158}
159
Chris Lattnerb48da392005-01-23 04:39:44 +0000160const TargetMachine &SelectionDAG::getTarget() const {
161 return TLI.getTargetMachine();
162}
163
Jim Laskey58b968b2005-08-17 20:08:02 +0000164//===----------------------------------------------------------------------===//
165// SelectionDAG Class
166//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000167
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000168/// RemoveDeadNodes - This method deletes all unreachable nodes in the
169/// SelectionDAG, including nodes (like loads) that have uses of their token
170/// chain but no other uses and no side effect. If a node is passed in as an
171/// argument, it is used as the seed for node deletion.
172void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173 // Create a dummy node (which is not added to allnodes), that adds a reference
174 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000175 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000176
Chris Lattnerf469cb62005-11-08 18:52:27 +0000177 bool MadeChange = false;
178
Chris Lattner8b8749f2005-08-17 19:00:20 +0000179 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000180 if (N && N->use_empty()) {
181 DestroyDeadNode(N);
182 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000183 }
184
Chris Lattnerde202b32005-11-09 23:47:37 +0000185 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
186 if (I->use_empty() && I->getOpcode() != 65535) {
187 // Node is dead, recursively delete newly dead uses.
188 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000189 MadeChange = true;
190 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000191
192 // Walk the nodes list, removing the nodes we've marked as dead.
193 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000194 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
195 SDNode *N = I++;
196 if (N->use_empty())
197 AllNodes.erase(N);
198 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000199 }
200
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000201 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000202 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203}
204
Chris Lattnerf469cb62005-11-08 18:52:27 +0000205/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
206/// graph. If it is the last user of any of its operands, recursively process
207/// them the same way.
208///
209void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000210 // Okay, we really are going to delete this node. First take this out of the
211 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000212 RemoveNodeFromCSEMaps(N);
213
214 // Next, brutally remove the operand list. This is safe to do, as there are
215 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000216 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
217 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000218 O->removeUser(N);
219
220 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000221 if (O->use_empty())
222 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000223 }
Chris Lattner65113b22005-11-08 22:07:03 +0000224 delete[] N->OperandList;
225 N->OperandList = 0;
226 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000227
228 // Mark the node as dead.
229 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000230}
231
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000232void SelectionDAG::DeleteNode(SDNode *N) {
233 assert(N->use_empty() && "Cannot delete a node that is not dead!");
234
235 // First take this out of the appropriate CSE map.
236 RemoveNodeFromCSEMaps(N);
237
Chris Lattner1e111c72005-09-07 05:37:01 +0000238 // Finally, remove uses due to operands of this node, remove from the
239 // AllNodes list, and delete the node.
240 DeleteNodeNotInCSEMaps(N);
241}
242
243void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
244
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000245 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000246 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000247
248 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000249 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
250 I->Val->removeUser(N);
251 delete[] N->OperandList;
252 N->OperandList = 0;
253 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000254
255 delete N;
256}
257
Chris Lattner149c58c2005-08-16 18:17:10 +0000258/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259/// correspond to it. This is useful when we're about to delete or repurpose
260/// the node. We don't want future request for structurally identical nodes
261/// to return N anymore.
262void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000263 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000265 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000267 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
268 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000270 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000271 Erased = TargetConstants.erase(std::make_pair(
272 cast<ConstantSDNode>(N)->getValue(),
273 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000274 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000275 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000276 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000277 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000278 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000279 }
Chris Lattner3181a772006-01-29 06:26:56 +0000280 case ISD::TargetConstantFP: {
281 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
282 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
283 break;
284 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000285 case ISD::STRING:
286 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
287 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000288 case ISD::CONDCODE:
289 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
290 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000291 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000292 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 case ISD::GlobalAddress: {
295 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
296 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
297 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000299 }
300 case ISD::TargetGlobalAddress: {
301 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
302 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
303 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000304 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000305 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000307 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000309 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000310 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000311 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000313 Erased = ConstantPoolIndices.
314 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
315 cast<ConstantPoolSDNode>(N)->getAlignment()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000316 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000317 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000318 Erased = TargetConstantPoolIndices.
319 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
320 cast<ConstantPoolSDNode>(N)->getAlignment()));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000321 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000322 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000324 break;
325 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000326 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000327 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000328 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000329 Erased =
330 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000331 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000332 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000333 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000334 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
335 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000336 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000337 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
338 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000339 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000340 case ISD::SRCVALUE: {
341 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000342 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000343 break;
344 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000345 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased = Loads.erase(std::make_pair(N->getOperand(1),
347 std::make_pair(N->getOperand(0),
348 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000349 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000350 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000351 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000352 if (N->getNumOperands() == 0) {
353 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
354 N->getValueType(0)));
355 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000356 Erased =
357 UnaryOps.erase(std::make_pair(N->getOpcode(),
358 std::make_pair(N->getOperand(0),
359 N->getValueType(0))));
360 } else if (N->getNumOperands() == 2) {
361 Erased =
362 BinaryOps.erase(std::make_pair(N->getOpcode(),
363 std::make_pair(N->getOperand(0),
364 N->getOperand(1))));
365 } else {
366 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
367 Erased =
368 OneResultNodes.erase(std::make_pair(N->getOpcode(),
369 std::make_pair(N->getValueType(0),
370 Ops)));
371 }
Chris Lattner385328c2005-05-14 07:42:29 +0000372 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000373 // Remove the node from the ArbitraryNodes map.
374 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
375 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000376 Erased =
377 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
378 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000379 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000380 break;
381 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000382#ifndef NDEBUG
383 // Verify that the node was actually in one of the CSE maps, unless it has a
384 // flag result (which cannot be CSE'd) or is one of the special cases that are
385 // not subject to CSE.
386 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000387 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000388 N->dump();
389 assert(0 && "Node is not in map!");
390 }
391#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000392}
393
Chris Lattner8b8749f2005-08-17 19:00:20 +0000394/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
395/// has been taken out and modified in some way. If the specified node already
396/// exists in the CSE maps, do not modify the maps, but return the existing node
397/// instead. If it doesn't exist, add it and return null.
398///
399SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
400 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000401 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000402 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000403
Chris Lattner9f8cc692005-12-19 22:21:21 +0000404 // Check that remaining values produced are not flags.
405 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
406 if (N->getValueType(i) == MVT::Flag)
407 return 0; // Never CSE anything that produces a flag.
408
Chris Lattnerfe14b342005-12-01 23:14:50 +0000409 if (N->getNumValues() == 1) {
410 if (N->getNumOperands() == 1) {
411 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getOperand(0),
413 N->getValueType(0)))];
414 if (U) return U;
415 U = N;
416 } else if (N->getNumOperands() == 2) {
417 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
418 std::make_pair(N->getOperand(0),
419 N->getOperand(1)))];
420 if (B) return B;
421 B = N;
422 } else {
423 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
424 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000425 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000426 if (ORN) return ORN;
427 ORN = N;
428 }
429 } else {
430 if (N->getOpcode() == ISD::LOAD) {
431 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
432 std::make_pair(N->getOperand(0),
433 N->getValueType(0)))];
434 if (L) return L;
435 L = N;
436 } else {
437 // Remove the node from the ArbitraryNodes map.
438 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
439 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
440 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
441 std::make_pair(RV, Ops))];
442 if (AN) return AN;
443 AN = N;
444 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000445 }
446 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000447}
448
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000449/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
450/// were replaced with those specified. If this node is never memoized,
451/// return null, otherwise return a pointer to the slot it would take. If a
452/// node already exists with these operands, the slot will be non-null.
453SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000454 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000455 return 0; // Never add these nodes.
456
457 // Check that remaining values produced are not flags.
458 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
459 if (N->getValueType(i) == MVT::Flag)
460 return 0; // Never CSE anything that produces a flag.
461
462 if (N->getNumValues() == 1) {
463 return &UnaryOps[std::make_pair(N->getOpcode(),
464 std::make_pair(Op, N->getValueType(0)))];
465 } else {
466 // Remove the node from the ArbitraryNodes map.
467 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
468 std::vector<SDOperand> Ops;
469 Ops.push_back(Op);
470 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
471 std::make_pair(RV, Ops))];
472 }
473 return 0;
474}
475
476/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
477/// were replaced with those specified. If this node is never memoized,
478/// return null, otherwise return a pointer to the slot it would take. If a
479/// node already exists with these operands, the slot will be non-null.
480SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
481 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000482 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000483 return 0; // Never add these nodes.
484
485 // Check that remaining values produced are not flags.
486 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
487 if (N->getValueType(i) == MVT::Flag)
488 return 0; // Never CSE anything that produces a flag.
489
490 if (N->getNumValues() == 1) {
491 return &BinaryOps[std::make_pair(N->getOpcode(),
492 std::make_pair(Op1, Op2))];
493 } else {
494 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
495 std::vector<SDOperand> Ops;
496 Ops.push_back(Op1);
497 Ops.push_back(Op2);
498 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
499 std::make_pair(RV, Ops))];
500 }
501 return 0;
502}
503
504
505/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
506/// were replaced with those specified. If this node is never memoized,
507/// return null, otherwise return a pointer to the slot it would take. If a
508/// node already exists with these operands, the slot will be non-null.
509SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
510 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000511 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000512 return 0; // Never add these nodes.
513
514 // Check that remaining values produced are not flags.
515 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
516 if (N->getValueType(i) == MVT::Flag)
517 return 0; // Never CSE anything that produces a flag.
518
519 if (N->getNumValues() == 1) {
520 if (N->getNumOperands() == 1) {
521 return &UnaryOps[std::make_pair(N->getOpcode(),
522 std::make_pair(Ops[0],
523 N->getValueType(0)))];
524 } else if (N->getNumOperands() == 2) {
525 return &BinaryOps[std::make_pair(N->getOpcode(),
526 std::make_pair(Ops[0], Ops[1]))];
527 } else {
528 return &OneResultNodes[std::make_pair(N->getOpcode(),
529 std::make_pair(N->getValueType(0),
530 Ops))];
531 }
532 } else {
533 if (N->getOpcode() == ISD::LOAD) {
534 return &Loads[std::make_pair(Ops[1],
535 std::make_pair(Ops[0], N->getValueType(0)))];
536 } else {
537 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
538 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
539 std::make_pair(RV, Ops))];
540 }
541 }
542 return 0;
543}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000544
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000545
Chris Lattner78ec3112003-08-11 14:57:33 +0000546SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000547 while (!AllNodes.empty()) {
548 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000549 delete [] N->OperandList;
550 N->OperandList = 0;
551 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000552 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000553 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000554}
555
Chris Lattner0f2287b2005-04-13 02:38:18 +0000556SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000557 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000558 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000559 return getNode(ISD::AND, Op.getValueType(), Op,
560 getConstant(Imm, Op.getValueType()));
561}
562
Chris Lattnerc3aae252005-01-07 07:46:32 +0000563SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
564 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
565 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000566 if (VT != MVT::i64)
567 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000568
Chris Lattnerc3aae252005-01-07 07:46:32 +0000569 SDNode *&N = Constants[std::make_pair(Val, VT)];
570 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000571 N = new ConstantSDNode(false, Val, VT);
572 AllNodes.push_back(N);
573 return SDOperand(N, 0);
574}
575
Chris Lattner36ce6912005-11-29 06:21:05 +0000576SDOperand SelectionDAG::getString(const std::string &Val) {
577 StringSDNode *&N = StringNodes[Val];
578 if (!N) {
579 N = new StringSDNode(Val);
580 AllNodes.push_back(N);
581 }
582 return SDOperand(N, 0);
583}
584
Chris Lattner37bfbb42005-08-17 00:34:06 +0000585SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
586 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
587 // Mask out any bits that are not valid for this constant.
588 if (VT != MVT::i64)
589 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
590
591 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
592 if (N) return SDOperand(N, 0);
593 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000594 AllNodes.push_back(N);
595 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000596}
597
Chris Lattnerc3aae252005-01-07 07:46:32 +0000598SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
599 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
600 if (VT == MVT::f32)
601 Val = (float)Val; // Mask out extra precision.
602
Chris Lattnerd8658612005-02-17 20:17:32 +0000603 // Do the map lookup using the actual bit pattern for the floating point
604 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
605 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000606 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000607 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000608 N = new ConstantFPSDNode(false, Val, VT);
609 AllNodes.push_back(N);
610 return SDOperand(N, 0);
611}
612
613SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
614 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
615 if (VT == MVT::f32)
616 Val = (float)Val; // Mask out extra precision.
617
618 // Do the map lookup using the actual bit pattern for the floating point
619 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
620 // we don't have issues with SNANs.
621 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
622 if (N) return SDOperand(N, 0);
623 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 AllNodes.push_back(N);
625 return SDOperand(N, 0);
626}
627
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000629 MVT::ValueType VT, int offset) {
630 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000631 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000632 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000633 AllNodes.push_back(N);
634 return SDOperand(N, 0);
635}
636
637SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000638 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000639 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000640 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000641 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000642 AllNodes.push_back(N);
643 return SDOperand(N, 0);
644}
645
646SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
647 SDNode *&N = FrameIndices[FI];
648 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000649 N = new FrameIndexSDNode(FI, VT, false);
650 AllNodes.push_back(N);
651 return SDOperand(N, 0);
652}
653
654SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
655 SDNode *&N = TargetFrameIndices[FI];
656 if (N) return SDOperand(N, 0);
657 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000658 AllNodes.push_back(N);
659 return SDOperand(N, 0);
660}
661
Evan Chengb8973bd2006-01-31 22:23:14 +0000662SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
663 unsigned Alignment) {
664 SDNode *&N = ConstantPoolIndices[std::make_pair(C, Alignment)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000665 if (N) return SDOperand(N, 0);
Evan Chengb8973bd2006-01-31 22:23:14 +0000666 N = new ConstantPoolSDNode(C, VT, Alignment, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000667 AllNodes.push_back(N);
668 return SDOperand(N, 0);
669}
670
Evan Chengb8973bd2006-01-31 22:23:14 +0000671SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
672 unsigned Alignment) {
673 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C, Alignment)];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000674 if (N) return SDOperand(N, 0);
Evan Chengb8973bd2006-01-31 22:23:14 +0000675 N = new ConstantPoolSDNode(C, VT, Alignment, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676 AllNodes.push_back(N);
677 return SDOperand(N, 0);
678}
679
680SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
681 SDNode *&N = BBNodes[MBB];
682 if (N) return SDOperand(N, 0);
683 N = new BasicBlockSDNode(MBB);
684 AllNodes.push_back(N);
685 return SDOperand(N, 0);
686}
687
Chris Lattner15e4b012005-07-10 00:07:11 +0000688SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
689 if ((unsigned)VT >= ValueTypeNodes.size())
690 ValueTypeNodes.resize(VT+1);
691 if (ValueTypeNodes[VT] == 0) {
692 ValueTypeNodes[VT] = new VTSDNode(VT);
693 AllNodes.push_back(ValueTypeNodes[VT]);
694 }
695
696 return SDOperand(ValueTypeNodes[VT], 0);
697}
698
Chris Lattnerc3aae252005-01-07 07:46:32 +0000699SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
700 SDNode *&N = ExternalSymbols[Sym];
701 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000702 N = new ExternalSymbolSDNode(false, Sym, VT);
703 AllNodes.push_back(N);
704 return SDOperand(N, 0);
705}
706
Chris Lattner809ec112006-01-28 10:09:25 +0000707SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
708 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000709 SDNode *&N = TargetExternalSymbols[Sym];
710 if (N) return SDOperand(N, 0);
711 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000712 AllNodes.push_back(N);
713 return SDOperand(N, 0);
714}
715
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000716SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
717 if ((unsigned)Cond >= CondCodeNodes.size())
718 CondCodeNodes.resize(Cond+1);
719
Chris Lattner079a27a2005-08-09 20:40:02 +0000720 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000721 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000722 AllNodes.push_back(CondCodeNodes[Cond]);
723 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000724 return SDOperand(CondCodeNodes[Cond], 0);
725}
726
Chris Lattner0fdd7682005-08-30 22:38:38 +0000727SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
728 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
729 if (!Reg) {
730 Reg = new RegisterSDNode(RegNo, VT);
731 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000732 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000733 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000734}
735
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000736SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
737 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000738 // These setcc operations always fold.
739 switch (Cond) {
740 default: break;
741 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000742 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000743 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000744 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745 }
746
Chris Lattner67255a12005-04-07 18:14:58 +0000747 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
748 uint64_t C2 = N2C->getValue();
749 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
750 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000751
Chris Lattnerc3aae252005-01-07 07:46:32 +0000752 // Sign extend the operands if required
753 if (ISD::isSignedIntSetCC(Cond)) {
754 C1 = N1C->getSignExtended();
755 C2 = N2C->getSignExtended();
756 }
757
758 switch (Cond) {
759 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000760 case ISD::SETEQ: return getConstant(C1 == C2, VT);
761 case ISD::SETNE: return getConstant(C1 != C2, VT);
762 case ISD::SETULT: return getConstant(C1 < C2, VT);
763 case ISD::SETUGT: return getConstant(C1 > C2, VT);
764 case ISD::SETULE: return getConstant(C1 <= C2, VT);
765 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
766 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
767 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
768 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
769 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000770 }
Chris Lattner24673922005-04-07 18:58:54 +0000771 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000772 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000773 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
774 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
775
776 // If the comparison constant has bits in the upper part, the
777 // zero-extended value could never match.
778 if (C2 & (~0ULL << InSize)) {
779 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
780 switch (Cond) {
781 case ISD::SETUGT:
782 case ISD::SETUGE:
783 case ISD::SETEQ: return getConstant(0, VT);
784 case ISD::SETULT:
785 case ISD::SETULE:
786 case ISD::SETNE: return getConstant(1, VT);
787 case ISD::SETGT:
788 case ISD::SETGE:
789 // True if the sign bit of C2 is set.
790 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
791 case ISD::SETLT:
792 case ISD::SETLE:
793 // True if the sign bit of C2 isn't set.
794 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
795 default:
796 break;
797 }
798 }
799
800 // Otherwise, we can perform the comparison with the low bits.
801 switch (Cond) {
802 case ISD::SETEQ:
803 case ISD::SETNE:
804 case ISD::SETUGT:
805 case ISD::SETUGE:
806 case ISD::SETULT:
807 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000808 return getSetCC(VT, N1.getOperand(0),
809 getConstant(C2, N1.getOperand(0).getValueType()),
810 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000811 default:
812 break; // todo, be more careful with signed comparisons
813 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000814 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
815 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
816 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
817 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
818 MVT::ValueType ExtDstTy = N1.getValueType();
819 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000820
Chris Lattner7b2880c2005-08-24 22:44:39 +0000821 // If the extended part has any inconsistent bits, it cannot ever
822 // compare equal. In other words, they have to be all ones or all
823 // zeros.
824 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000825 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000826 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
827 return getConstant(Cond == ISD::SETNE, VT);
828
829 // Otherwise, make this a use of a zext.
830 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000831 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000832 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000833 }
834
Chris Lattner67255a12005-04-07 18:14:58 +0000835 uint64_t MinVal, MaxVal;
836 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
837 if (ISD::isSignedIntSetCC(Cond)) {
838 MinVal = 1ULL << (OperandBitSize-1);
839 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
840 MaxVal = ~0ULL >> (65-OperandBitSize);
841 else
842 MaxVal = 0;
843 } else {
844 MinVal = 0;
845 MaxVal = ~0ULL >> (64-OperandBitSize);
846 }
847
848 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
849 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
850 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
851 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000852 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
853 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000854 }
855
856 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
857 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
858 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000859 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
860 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000861 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000862
Nate Begeman72ea2812005-04-14 08:56:52 +0000863 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
864 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000865
Nate Begeman72ea2812005-04-14 08:56:52 +0000866 // Canonicalize setgt X, Min --> setne X, Min
867 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000868 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000869
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000870 // If we have setult X, 1, turn it into seteq X, 0
871 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000872 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
873 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000874 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000875 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000876 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
877 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000878
879 // If we have "setcc X, C1", check to see if we can shrink the immediate
880 // by changing cc.
881
882 // SETUGT X, SINTMAX -> SETLT X, 0
883 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
884 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000885 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000886
887 // FIXME: Implement the rest of these.
888
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000889
890 // Fold bit comparisons when we can.
891 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
892 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
893 if (ConstantSDNode *AndRHS =
894 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
895 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
896 // Perform the xform if the AND RHS is a single bit.
897 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
898 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000899 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000900 TLI.getShiftAmountTy()));
901 }
902 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
903 // (X & 8) == 8 --> (X & 8) >> 3
904 // Perform the xform if C2 is a single bit.
905 if ((C2 & (C2-1)) == 0) {
906 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000907 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000908 }
909 }
910 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 }
Chris Lattner67255a12005-04-07 18:14:58 +0000912 } else if (isa<ConstantSDNode>(N1.Val)) {
913 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000914 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000915 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916
917 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
918 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
919 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000920
Chris Lattnerc3aae252005-01-07 07:46:32 +0000921 switch (Cond) {
922 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000923 case ISD::SETEQ: return getConstant(C1 == C2, VT);
924 case ISD::SETNE: return getConstant(C1 != C2, VT);
925 case ISD::SETLT: return getConstant(C1 < C2, VT);
926 case ISD::SETGT: return getConstant(C1 > C2, VT);
927 case ISD::SETLE: return getConstant(C1 <= C2, VT);
928 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 }
930 } else {
931 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000932 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 }
934
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000935 // Could not fold it.
936 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937}
938
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000940///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000942 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
943 if (!N) {
944 N = new SDNode(Opcode, VT);
945 AllNodes.push_back(N);
946 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947 return SDOperand(N, 0);
948}
949
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
951 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000952 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000953 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
955 uint64_t Val = C->getValue();
956 switch (Opcode) {
957 default: break;
958 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000959 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000960 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
961 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000962 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
963 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000964 case ISD::BIT_CONVERT:
965 if (VT == MVT::f32) {
966 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
967 return getConstantFP(BitsToFloat(Val), VT);
968 } else if (VT == MVT::f64) {
969 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
970 return getConstantFP(BitsToDouble(Val), VT);
971 }
972 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000973 case ISD::BSWAP:
974 switch(VT) {
975 default: assert(0 && "Invalid bswap!"); break;
976 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
977 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
978 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
979 }
980 break;
981 case ISD::CTPOP:
982 switch(VT) {
983 default: assert(0 && "Invalid ctpop!"); break;
984 case MVT::i1: return getConstant(Val != 0, VT);
985 case MVT::i8:
986 Tmp1 = (unsigned)Val & 0xFF;
987 return getConstant(CountPopulation_32(Tmp1), VT);
988 case MVT::i16:
989 Tmp1 = (unsigned)Val & 0xFFFF;
990 return getConstant(CountPopulation_32(Tmp1), VT);
991 case MVT::i32:
992 return getConstant(CountPopulation_32((unsigned)Val), VT);
993 case MVT::i64:
994 return getConstant(CountPopulation_64(Val), VT);
995 }
996 case ISD::CTLZ:
997 switch(VT) {
998 default: assert(0 && "Invalid ctlz!"); break;
999 case MVT::i1: return getConstant(Val == 0, VT);
1000 case MVT::i8:
1001 Tmp1 = (unsigned)Val & 0xFF;
1002 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1003 case MVT::i16:
1004 Tmp1 = (unsigned)Val & 0xFFFF;
1005 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1006 case MVT::i32:
1007 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1008 case MVT::i64:
1009 return getConstant(CountLeadingZeros_64(Val), VT);
1010 }
1011 case ISD::CTTZ:
1012 switch(VT) {
1013 default: assert(0 && "Invalid cttz!"); break;
1014 case MVT::i1: return getConstant(Val == 0, VT);
1015 case MVT::i8:
1016 Tmp1 = (unsigned)Val | 0x100;
1017 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1018 case MVT::i16:
1019 Tmp1 = (unsigned)Val | 0x10000;
1020 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1021 case MVT::i32:
1022 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1023 case MVT::i64:
1024 return getConstant(CountTrailingZeros_64(Val), VT);
1025 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 }
1027 }
1028
Chris Lattner94683772005-12-23 05:30:37 +00001029 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1031 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001032 case ISD::FNEG:
1033 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001034 case ISD::FABS:
1035 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036 case ISD::FP_ROUND:
1037 case ISD::FP_EXTEND:
1038 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001039 case ISD::FP_TO_SINT:
1040 return getConstant((int64_t)C->getValue(), VT);
1041 case ISD::FP_TO_UINT:
1042 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001043 case ISD::BIT_CONVERT:
1044 if (VT == MVT::i32) {
1045 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1046 return getConstant(FloatToBits(C->getValue()), VT);
1047 } else if (VT == MVT::i64) {
1048 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1049 return getConstant(DoubleToBits(C->getValue()), VT);
1050 }
1051 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 }
1053
1054 unsigned OpOpcode = Operand.Val->getOpcode();
1055 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001056 case ISD::TokenFactor:
1057 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058 case ISD::SIGN_EXTEND:
1059 if (Operand.getValueType() == VT) return Operand; // noop extension
1060 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1061 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1062 break;
1063 case ISD::ZERO_EXTEND:
1064 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001065 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001066 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001068 case ISD::ANY_EXTEND:
1069 if (Operand.getValueType() == VT) return Operand; // noop extension
1070 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1071 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1072 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1073 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 case ISD::TRUNCATE:
1075 if (Operand.getValueType() == VT) return Operand; // noop truncate
1076 if (OpOpcode == ISD::TRUNCATE)
1077 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001078 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1079 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001080 // If the source is smaller than the dest, we still need an extend.
1081 if (Operand.Val->getOperand(0).getValueType() < VT)
1082 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1083 else if (Operand.Val->getOperand(0).getValueType() > VT)
1084 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1085 else
1086 return Operand.Val->getOperand(0);
1087 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088 break;
Chris Lattner35481892005-12-23 00:16:34 +00001089 case ISD::BIT_CONVERT:
1090 // Basic sanity checking.
1091 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1092 "Cannot BIT_CONVERT between two different types!");
1093 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001094 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1095 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001096 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001097 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001098 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1099 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001100 Operand.Val->getOperand(0));
1101 if (OpOpcode == ISD::FNEG) // --X -> X
1102 return Operand.Val->getOperand(0);
1103 break;
1104 case ISD::FABS:
1105 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1106 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1107 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 }
1109
Chris Lattner43247a12005-08-25 19:12:10 +00001110 SDNode *N;
1111 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1112 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001113 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001114 E = N = new SDNode(Opcode, Operand);
1115 } else {
1116 N = new SDNode(Opcode, Operand);
1117 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001118 N->setValueTypes(VT);
1119 AllNodes.push_back(N);
1120 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001121}
1122
Chris Lattner36019aa2005-04-18 03:48:41 +00001123
1124
Chris Lattnerc3aae252005-01-07 07:46:32 +00001125SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1126 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001127#ifndef NDEBUG
1128 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001129 case ISD::TokenFactor:
1130 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1131 N2.getValueType() == MVT::Other && "Invalid token factor!");
1132 break;
Chris Lattner76365122005-01-16 02:23:22 +00001133 case ISD::AND:
1134 case ISD::OR:
1135 case ISD::XOR:
1136 case ISD::UDIV:
1137 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001138 case ISD::MULHU:
1139 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001140 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1141 // fall through
1142 case ISD::ADD:
1143 case ISD::SUB:
1144 case ISD::MUL:
1145 case ISD::SDIV:
1146 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001147 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1148 // fall through.
1149 case ISD::FADD:
1150 case ISD::FSUB:
1151 case ISD::FMUL:
1152 case ISD::FDIV:
1153 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001154 assert(N1.getValueType() == N2.getValueType() &&
1155 N1.getValueType() == VT && "Binary operator types must match!");
1156 break;
1157
1158 case ISD::SHL:
1159 case ISD::SRA:
1160 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001161 case ISD::ROTL:
1162 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001163 assert(VT == N1.getValueType() &&
1164 "Shift operators return type must be the same as their first arg");
1165 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001166 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001167 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001168 case ISD::FP_ROUND_INREG: {
1169 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1170 assert(VT == N1.getValueType() && "Not an inreg round!");
1171 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1172 "Cannot FP_ROUND_INREG integer types");
1173 assert(EVT <= VT && "Not rounding down!");
1174 break;
1175 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001176 case ISD::AssertSext:
1177 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001178 case ISD::SIGN_EXTEND_INREG: {
1179 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1180 assert(VT == N1.getValueType() && "Not an inreg extend!");
1181 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1182 "Cannot *_EXTEND_INREG FP types");
1183 assert(EVT <= VT && "Not extending!");
1184 }
1185
Chris Lattner76365122005-01-16 02:23:22 +00001186 default: break;
1187 }
1188#endif
1189
Chris Lattnerc3aae252005-01-07 07:46:32 +00001190 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1191 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1192 if (N1C) {
1193 if (N2C) {
1194 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1195 switch (Opcode) {
1196 case ISD::ADD: return getConstant(C1 + C2, VT);
1197 case ISD::SUB: return getConstant(C1 - C2, VT);
1198 case ISD::MUL: return getConstant(C1 * C2, VT);
1199 case ISD::UDIV:
1200 if (C2) return getConstant(C1 / C2, VT);
1201 break;
1202 case ISD::UREM :
1203 if (C2) return getConstant(C1 % C2, VT);
1204 break;
1205 case ISD::SDIV :
1206 if (C2) return getConstant(N1C->getSignExtended() /
1207 N2C->getSignExtended(), VT);
1208 break;
1209 case ISD::SREM :
1210 if (C2) return getConstant(N1C->getSignExtended() %
1211 N2C->getSignExtended(), VT);
1212 break;
1213 case ISD::AND : return getConstant(C1 & C2, VT);
1214 case ISD::OR : return getConstant(C1 | C2, VT);
1215 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001216 case ISD::SHL : return getConstant(C1 << C2, VT);
1217 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001218 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001219 case ISD::ROTL :
1220 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1221 VT);
1222 case ISD::ROTR :
1223 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1224 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 default: break;
1226 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 } else { // Cannonicalize constant to RHS if commutative
1228 if (isCommutativeBinOp(Opcode)) {
1229 std::swap(N1C, N2C);
1230 std::swap(N1, N2);
1231 }
1232 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001233 }
1234
1235 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1236 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001237 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 if (N2CFP) {
1239 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1240 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001241 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1242 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1243 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1244 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001245 if (C2) return getConstantFP(C1 / C2, VT);
1246 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001247 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001248 if (C2) return getConstantFP(fmod(C1, C2), VT);
1249 break;
1250 default: break;
1251 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001252 } else { // Cannonicalize constant to RHS if commutative
1253 if (isCommutativeBinOp(Opcode)) {
1254 std::swap(N1CFP, N2CFP);
1255 std::swap(N1, N2);
1256 }
1257 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001258 }
1259
Chris Lattnerc3aae252005-01-07 07:46:32 +00001260 // Finally, fold operations that do not require constants.
1261 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 case ISD::FP_ROUND_INREG:
1263 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1264 break;
1265 case ISD::SIGN_EXTEND_INREG: {
1266 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1267 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001268 break;
1269 }
1270
Nate Begemaneea805e2005-04-13 21:23:31 +00001271 // FIXME: figure out how to safely handle things like
1272 // int foo(int x) { return 1 << (x & 255); }
1273 // int bar() { return foo(256); }
1274#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001275 case ISD::SHL:
1276 case ISD::SRL:
1277 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001278 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001279 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001280 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001281 else if (N2.getOpcode() == ISD::AND)
1282 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1283 // If the and is only masking out bits that cannot effect the shift,
1284 // eliminate the and.
1285 unsigned NumBits = MVT::getSizeInBits(VT);
1286 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1287 return getNode(Opcode, VT, N1, N2.getOperand(0));
1288 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001289 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001290#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001291 }
1292
Chris Lattner27e9b412005-05-11 18:57:39 +00001293 // Memoize this node if possible.
1294 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001295 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001296 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1297 if (BON) return SDOperand(BON, 0);
1298
1299 BON = N = new SDNode(Opcode, N1, N2);
1300 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001301 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001302 }
1303
Chris Lattner3e011362005-05-14 07:45:46 +00001304 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001305 AllNodes.push_back(N);
1306 return SDOperand(N, 0);
1307}
1308
Chris Lattnerc3aae252005-01-07 07:46:32 +00001309SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1310 SDOperand N1, SDOperand N2, SDOperand N3) {
1311 // Perform various simplifications.
1312 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1313 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1314 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1315 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001316 case ISD::SETCC: {
1317 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001318 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001319 if (Simp.Val) return Simp;
1320 break;
1321 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001322 case ISD::SELECT:
1323 if (N1C)
1324 if (N1C->getValue())
1325 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001326 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001327 return N3; // select false, X, Y -> Y
1328
1329 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001330 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001331 case ISD::BRCOND:
1332 if (N2C)
1333 if (N2C->getValue()) // Unconditional branch
1334 return getNode(ISD::BR, MVT::Other, N1, N3);
1335 else
1336 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001337 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001338 }
1339
Chris Lattner385328c2005-05-14 07:42:29 +00001340 std::vector<SDOperand> Ops;
1341 Ops.reserve(3);
1342 Ops.push_back(N1);
1343 Ops.push_back(N2);
1344 Ops.push_back(N3);
1345
Chris Lattner43247a12005-08-25 19:12:10 +00001346 // Memoize node if it doesn't produce a flag.
1347 SDNode *N;
1348 if (VT != MVT::Flag) {
1349 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1350 if (E) return SDOperand(E, 0);
1351 E = N = new SDNode(Opcode, N1, N2, N3);
1352 } else {
1353 N = new SDNode(Opcode, N1, N2, N3);
1354 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001355 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001356 AllNodes.push_back(N);
1357 return SDOperand(N, 0);
1358}
1359
1360SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001361 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001362 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001363 std::vector<SDOperand> Ops;
1364 Ops.reserve(4);
1365 Ops.push_back(N1);
1366 Ops.push_back(N2);
1367 Ops.push_back(N3);
1368 Ops.push_back(N4);
1369 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001370}
1371
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001372SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1373 SDOperand N1, SDOperand N2, SDOperand N3,
1374 SDOperand N4, SDOperand N5) {
1375 std::vector<SDOperand> Ops;
1376 Ops.reserve(5);
1377 Ops.push_back(N1);
1378 Ops.push_back(N2);
1379 Ops.push_back(N3);
1380 Ops.push_back(N4);
1381 Ops.push_back(N5);
1382 return getNode(Opcode, VT, Ops);
1383}
1384
Evan Cheng7038daf2005-12-10 00:37:58 +00001385SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1386 SDOperand Chain, SDOperand Ptr,
1387 SDOperand SV) {
1388 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1389 if (N) return SDOperand(N, 0);
1390 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1391
1392 // Loads have a token chain.
1393 setNodeValueTypes(N, VT, MVT::Other);
1394 AllNodes.push_back(N);
1395 return SDOperand(N, 0);
1396}
1397
1398SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1399 SDOperand Chain, SDOperand Ptr,
1400 SDOperand SV) {
1401 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1402 if (N) return SDOperand(N, 0);
1403 std::vector<SDOperand> Ops;
1404 Ops.reserve(5);
1405 Ops.push_back(Chain);
1406 Ops.push_back(Ptr);
1407 Ops.push_back(getConstant(Count, MVT::i32));
1408 Ops.push_back(getValueType(EVT));
1409 Ops.push_back(SV);
1410 std::vector<MVT::ValueType> VTs;
1411 VTs.reserve(2);
1412 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1413 return getNode(ISD::VLOAD, VTs, Ops);
1414}
1415
1416SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1417 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1418 MVT::ValueType EVT) {
1419 std::vector<SDOperand> Ops;
1420 Ops.reserve(4);
1421 Ops.push_back(Chain);
1422 Ops.push_back(Ptr);
1423 Ops.push_back(SV);
1424 Ops.push_back(getValueType(EVT));
1425 std::vector<MVT::ValueType> VTs;
1426 VTs.reserve(2);
1427 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1428 return getNode(Opcode, VTs, Ops);
1429}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001430
Chris Lattner0437cdd2005-05-09 04:14:13 +00001431SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001432 assert((!V || isa<PointerType>(V->getType())) &&
1433 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001434 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1435 if (N) return SDOperand(N, 0);
1436
1437 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001438 AllNodes.push_back(N);
1439 return SDOperand(N, 0);
1440}
1441
Nate Begemanacc398c2006-01-25 18:21:52 +00001442SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1443 SDOperand Chain, SDOperand Ptr,
1444 SDOperand SV) {
1445 std::vector<SDOperand> Ops;
1446 Ops.reserve(3);
1447 Ops.push_back(Chain);
1448 Ops.push_back(Ptr);
1449 Ops.push_back(SV);
1450 std::vector<MVT::ValueType> VTs;
1451 VTs.reserve(2);
1452 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1453 return getNode(ISD::VAARG, VTs, Ops);
1454}
1455
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001456SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001457 std::vector<SDOperand> &Ops) {
1458 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001459 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001460 case 1: return getNode(Opcode, VT, Ops[0]);
1461 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1462 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001463 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001464 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001465
Chris Lattner89c34632005-05-14 06:20:26 +00001466 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001467 switch (Opcode) {
1468 default: break;
1469 case ISD::BRCONDTWOWAY:
1470 if (N1C)
1471 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001472 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001473 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001474 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001475 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001476 case ISD::BRTWOWAY_CC:
1477 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1478 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1479 "LHS and RHS of comparison must have same type!");
1480 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001481 case ISD::TRUNCSTORE: {
1482 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1483 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1484#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1485 // If this is a truncating store of a constant, convert to the desired type
1486 // and store it instead.
1487 if (isa<Constant>(Ops[0])) {
1488 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1489 if (isa<Constant>(Op))
1490 N1 = Op;
1491 }
1492 // Also for ConstantFP?
1493#endif
1494 if (Ops[0].getValueType() == EVT) // Normal store?
1495 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1496 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1497 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1498 "Can't do FP-INT conversion!");
1499 break;
1500 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001501 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001502 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001503 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1504 "LHS and RHS of condition must have same type!");
1505 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1506 "True and False arms of SelectCC must have same type!");
1507 assert(Ops[2].getValueType() == VT &&
1508 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001509 break;
1510 }
1511 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001512 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001513 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1514 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001515 break;
1516 }
Chris Lattneref847df2005-04-09 03:27:28 +00001517 }
1518
Chris Lattner385328c2005-05-14 07:42:29 +00001519 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001520 SDNode *N;
1521 if (VT != MVT::Flag) {
1522 SDNode *&E =
1523 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1524 if (E) return SDOperand(E, 0);
1525 E = N = new SDNode(Opcode, Ops);
1526 } else {
1527 N = new SDNode(Opcode, Ops);
1528 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001529 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001530 AllNodes.push_back(N);
1531 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001532}
1533
Chris Lattner89c34632005-05-14 06:20:26 +00001534SDOperand SelectionDAG::getNode(unsigned Opcode,
1535 std::vector<MVT::ValueType> &ResultTys,
1536 std::vector<SDOperand> &Ops) {
1537 if (ResultTys.size() == 1)
1538 return getNode(Opcode, ResultTys[0], Ops);
1539
Chris Lattner5f056bf2005-07-10 01:55:33 +00001540 switch (Opcode) {
1541 case ISD::EXTLOAD:
1542 case ISD::SEXTLOAD:
1543 case ISD::ZEXTLOAD: {
1544 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1545 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1546 // If they are asking for an extending load from/to the same thing, return a
1547 // normal load.
1548 if (ResultTys[0] == EVT)
1549 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1550 assert(EVT < ResultTys[0] &&
1551 "Should only be an extending load, not truncating!");
1552 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1553 "Cannot sign/zero extend a FP load!");
1554 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1555 "Cannot convert from FP to Int or Int -> FP!");
1556 break;
1557 }
1558
Chris Lattnere89083a2005-05-14 07:25:05 +00001559 // FIXME: figure out how to safely handle things like
1560 // int foo(int x) { return 1 << (x & 255); }
1561 // int bar() { return foo(256); }
1562#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001563 case ISD::SRA_PARTS:
1564 case ISD::SRL_PARTS:
1565 case ISD::SHL_PARTS:
1566 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001567 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001568 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1569 else if (N3.getOpcode() == ISD::AND)
1570 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1571 // If the and is only masking out bits that cannot effect the shift,
1572 // eliminate the and.
1573 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1574 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1575 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1576 }
1577 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001578#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001579 }
Chris Lattner89c34632005-05-14 06:20:26 +00001580
Chris Lattner43247a12005-08-25 19:12:10 +00001581 // Memoize the node unless it returns a flag.
1582 SDNode *N;
1583 if (ResultTys.back() != MVT::Flag) {
1584 SDNode *&E =
1585 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1586 if (E) return SDOperand(E, 0);
1587 E = N = new SDNode(Opcode, Ops);
1588 } else {
1589 N = new SDNode(Opcode, Ops);
1590 }
Chris Lattnera3255112005-11-08 23:30:28 +00001591 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001592 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001593 return SDOperand(N, 0);
1594}
1595
Chris Lattnera3255112005-11-08 23:30:28 +00001596void SelectionDAG::setNodeValueTypes(SDNode *N,
1597 std::vector<MVT::ValueType> &RetVals) {
1598 switch (RetVals.size()) {
1599 case 0: return;
1600 case 1: N->setValueTypes(RetVals[0]); return;
1601 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1602 default: break;
1603 }
1604
1605 std::list<std::vector<MVT::ValueType> >::iterator I =
1606 std::find(VTList.begin(), VTList.end(), RetVals);
1607 if (I == VTList.end()) {
1608 VTList.push_front(RetVals);
1609 I = VTList.begin();
1610 }
1611
1612 N->setValueTypes(&(*I)[0], I->size());
1613}
1614
1615void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1616 MVT::ValueType VT2) {
1617 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1618 E = VTList.end(); I != E; ++I) {
1619 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1620 N->setValueTypes(&(*I)[0], 2);
1621 return;
1622 }
1623 }
1624 std::vector<MVT::ValueType> V;
1625 V.push_back(VT1);
1626 V.push_back(VT2);
1627 VTList.push_front(V);
1628 N->setValueTypes(&(*VTList.begin())[0], 2);
1629}
1630
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001631/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1632/// specified operands. If the resultant node already exists in the DAG,
1633/// this does not modify the specified node, instead it returns the node that
1634/// already exists. If the resultant node does not exist in the DAG, the
1635/// input node is returned. As a degenerate case, if you specify the same
1636/// input operands as the node already has, the input node is returned.
1637SDOperand SelectionDAG::
1638UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1639 SDNode *N = InN.Val;
1640 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1641
1642 // Check to see if there is no change.
1643 if (Op == N->getOperand(0)) return InN;
1644
1645 // See if the modified node already exists.
1646 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1647 if (NewSlot && *NewSlot)
1648 return SDOperand(*NewSlot, InN.ResNo);
1649
1650 // Nope it doesn't. Remove the node from it's current place in the maps.
1651 if (NewSlot)
1652 RemoveNodeFromCSEMaps(N);
1653
1654 // Now we update the operands.
1655 N->OperandList[0].Val->removeUser(N);
1656 Op.Val->addUser(N);
1657 N->OperandList[0] = Op;
1658
1659 // If this gets put into a CSE map, add it.
1660 if (NewSlot) *NewSlot = N;
1661 return InN;
1662}
1663
1664SDOperand SelectionDAG::
1665UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1666 SDNode *N = InN.Val;
1667 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1668
1669 // Check to see if there is no change.
1670 bool AnyChange = false;
1671 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1672 return InN; // No operands changed, just return the input node.
1673
1674 // See if the modified node already exists.
1675 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1676 if (NewSlot && *NewSlot)
1677 return SDOperand(*NewSlot, InN.ResNo);
1678
1679 // Nope it doesn't. Remove the node from it's current place in the maps.
1680 if (NewSlot)
1681 RemoveNodeFromCSEMaps(N);
1682
1683 // Now we update the operands.
1684 if (N->OperandList[0] != Op1) {
1685 N->OperandList[0].Val->removeUser(N);
1686 Op1.Val->addUser(N);
1687 N->OperandList[0] = Op1;
1688 }
1689 if (N->OperandList[1] != Op2) {
1690 N->OperandList[1].Val->removeUser(N);
1691 Op2.Val->addUser(N);
1692 N->OperandList[1] = Op2;
1693 }
1694
1695 // If this gets put into a CSE map, add it.
1696 if (NewSlot) *NewSlot = N;
1697 return InN;
1698}
1699
1700SDOperand SelectionDAG::
1701UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1702 std::vector<SDOperand> Ops;
1703 Ops.push_back(Op1);
1704 Ops.push_back(Op2);
1705 Ops.push_back(Op3);
1706 return UpdateNodeOperands(N, Ops);
1707}
1708
1709SDOperand SelectionDAG::
1710UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1711 SDOperand Op3, SDOperand Op4) {
1712 std::vector<SDOperand> Ops;
1713 Ops.push_back(Op1);
1714 Ops.push_back(Op2);
1715 Ops.push_back(Op3);
1716 Ops.push_back(Op4);
1717 return UpdateNodeOperands(N, Ops);
1718}
1719
1720SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001721UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1722 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1723 std::vector<SDOperand> Ops;
1724 Ops.push_back(Op1);
1725 Ops.push_back(Op2);
1726 Ops.push_back(Op3);
1727 Ops.push_back(Op4);
1728 Ops.push_back(Op5);
1729 return UpdateNodeOperands(N, Ops);
1730}
1731
1732
1733SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001734UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1735 SDNode *N = InN.Val;
1736 assert(N->getNumOperands() == Ops.size() &&
1737 "Update with wrong number of operands");
1738
1739 // Check to see if there is no change.
1740 unsigned NumOps = Ops.size();
1741 bool AnyChange = false;
1742 for (unsigned i = 0; i != NumOps; ++i) {
1743 if (Ops[i] != N->getOperand(i)) {
1744 AnyChange = true;
1745 break;
1746 }
1747 }
1748
1749 // No operands changed, just return the input node.
1750 if (!AnyChange) return InN;
1751
1752 // See if the modified node already exists.
1753 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1754 if (NewSlot && *NewSlot)
1755 return SDOperand(*NewSlot, InN.ResNo);
1756
1757 // Nope it doesn't. Remove the node from it's current place in the maps.
1758 if (NewSlot)
1759 RemoveNodeFromCSEMaps(N);
1760
1761 // Now we update the operands.
1762 for (unsigned i = 0; i != NumOps; ++i) {
1763 if (N->OperandList[i] != Ops[i]) {
1764 N->OperandList[i].Val->removeUser(N);
1765 Ops[i].Val->addUser(N);
1766 N->OperandList[i] = Ops[i];
1767 }
1768 }
1769
1770 // If this gets put into a CSE map, add it.
1771 if (NewSlot) *NewSlot = N;
1772 return InN;
1773}
1774
1775
1776
Chris Lattner149c58c2005-08-16 18:17:10 +00001777
1778/// SelectNodeTo - These are used for target selectors to *mutate* the
1779/// specified node to have the specified return type, Target opcode, and
1780/// operands. Note that target opcodes are stored as
1781/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001782///
1783/// Note that SelectNodeTo returns the resultant node. If there is already a
1784/// node of the specified opcode and operands, it returns that node instead of
1785/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001786SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1787 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001788 // If an identical node already exists, use it.
1789 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1790 if (ON) return SDOperand(ON, 0);
1791
Chris Lattner7651fa42005-08-24 23:00:29 +00001792 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001793
Chris Lattner7651fa42005-08-24 23:00:29 +00001794 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1795 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001796
1797 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001798 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001799}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001800
Chris Lattnereb19e402005-11-30 22:45:14 +00001801SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1802 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001803 // If an identical node already exists, use it.
1804 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1805 std::make_pair(Op1, VT))];
1806 if (ON) return SDOperand(ON, 0);
1807
Chris Lattner149c58c2005-08-16 18:17:10 +00001808 RemoveNodeFromCSEMaps(N);
1809 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1810 N->setValueTypes(VT);
1811 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001812
1813 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001814 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001815}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001816
Chris Lattnereb19e402005-11-30 22:45:14 +00001817SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1818 MVT::ValueType VT, SDOperand Op1,
1819 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001820 // If an identical node already exists, use it.
1821 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1822 std::make_pair(Op1, Op2))];
1823 if (ON) return SDOperand(ON, 0);
1824
Chris Lattner149c58c2005-08-16 18:17:10 +00001825 RemoveNodeFromCSEMaps(N);
1826 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1827 N->setValueTypes(VT);
1828 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001829
1830 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001831 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001832}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001833
Chris Lattnereb19e402005-11-30 22:45:14 +00001834SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1835 MVT::ValueType VT, SDOperand Op1,
1836 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001837 // If an identical node already exists, use it.
1838 std::vector<SDOperand> OpList;
1839 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1840 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1841 std::make_pair(VT, OpList))];
1842 if (ON) return SDOperand(ON, 0);
1843
Chris Lattner149c58c2005-08-16 18:17:10 +00001844 RemoveNodeFromCSEMaps(N);
1845 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1846 N->setValueTypes(VT);
1847 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001848
1849 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001850 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001851}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001852
Chris Lattnereb19e402005-11-30 22:45:14 +00001853SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1854 MVT::ValueType VT, SDOperand Op1,
1855 SDOperand Op2, SDOperand Op3,
1856 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001857 // If an identical node already exists, use it.
1858 std::vector<SDOperand> OpList;
1859 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1860 OpList.push_back(Op4);
1861 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1862 std::make_pair(VT, OpList))];
1863 if (ON) return SDOperand(ON, 0);
1864
Nate Begeman294a0a12005-08-18 07:30:15 +00001865 RemoveNodeFromCSEMaps(N);
1866 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1867 N->setValueTypes(VT);
1868 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001869
1870 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001871 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001872}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001873
Chris Lattnereb19e402005-11-30 22:45:14 +00001874SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1875 MVT::ValueType VT, SDOperand Op1,
1876 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1877 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001878 // If an identical node already exists, use it.
1879 std::vector<SDOperand> OpList;
1880 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1881 OpList.push_back(Op4); OpList.push_back(Op5);
1882 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1883 std::make_pair(VT, OpList))];
1884 if (ON) return SDOperand(ON, 0);
1885
Chris Lattner6b09a292005-08-21 18:49:33 +00001886 RemoveNodeFromCSEMaps(N);
1887 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1888 N->setValueTypes(VT);
1889 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001890
1891 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001892 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001893}
Chris Lattner149c58c2005-08-16 18:17:10 +00001894
Chris Lattnereb19e402005-11-30 22:45:14 +00001895SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1896 MVT::ValueType VT, SDOperand Op1,
1897 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1898 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001899 // If an identical node already exists, use it.
1900 std::vector<SDOperand> OpList;
1901 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1902 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1903 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1904 std::make_pair(VT, OpList))];
1905 if (ON) return SDOperand(ON, 0);
1906
Evan Cheng61ca74b2005-11-30 02:04:11 +00001907 RemoveNodeFromCSEMaps(N);
1908 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1909 N->setValueTypes(VT);
1910 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001911
1912 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001913 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001914}
1915
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001916SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1917 MVT::ValueType VT, SDOperand Op1,
1918 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1919 SDOperand Op5, SDOperand Op6,
1920 SDOperand Op7) {
1921 // If an identical node already exists, use it.
1922 std::vector<SDOperand> OpList;
1923 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1924 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1925 OpList.push_back(Op7);
1926 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1927 std::make_pair(VT, OpList))];
1928 if (ON) return SDOperand(ON, 0);
1929
1930 RemoveNodeFromCSEMaps(N);
1931 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1932 N->setValueTypes(VT);
1933 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1934
1935 ON = N; // Memoize the new node.
1936 return SDOperand(N, 0);
1937}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001938SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1939 MVT::ValueType VT, SDOperand Op1,
1940 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1941 SDOperand Op5, SDOperand Op6,
1942 SDOperand Op7, SDOperand Op8) {
1943 // If an identical node already exists, use it.
1944 std::vector<SDOperand> OpList;
1945 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1946 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1947 OpList.push_back(Op7); OpList.push_back(Op8);
1948 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1949 std::make_pair(VT, OpList))];
1950 if (ON) return SDOperand(ON, 0);
1951
1952 RemoveNodeFromCSEMaps(N);
1953 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1954 N->setValueTypes(VT);
1955 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1956
1957 ON = N; // Memoize the new node.
1958 return SDOperand(N, 0);
1959}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001960
Chris Lattnereb19e402005-11-30 22:45:14 +00001961SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1962 MVT::ValueType VT1, MVT::ValueType VT2,
1963 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001964 // If an identical node already exists, use it.
1965 std::vector<SDOperand> OpList;
1966 OpList.push_back(Op1); OpList.push_back(Op2);
1967 std::vector<MVT::ValueType> VTList;
1968 VTList.push_back(VT1); VTList.push_back(VT2);
1969 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1970 std::make_pair(VTList, OpList))];
1971 if (ON) return SDOperand(ON, 0);
1972
Chris Lattner0fb094f2005-11-19 01:44:53 +00001973 RemoveNodeFromCSEMaps(N);
1974 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1975 setNodeValueTypes(N, VT1, VT2);
1976 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001977
1978 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001979 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001980}
1981
Chris Lattnereb19e402005-11-30 22:45:14 +00001982SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1983 MVT::ValueType VT1, MVT::ValueType VT2,
1984 SDOperand Op1, SDOperand Op2,
1985 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001986 // If an identical node already exists, use it.
1987 std::vector<SDOperand> OpList;
1988 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1989 std::vector<MVT::ValueType> VTList;
1990 VTList.push_back(VT1); VTList.push_back(VT2);
1991 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1992 std::make_pair(VTList, OpList))];
1993 if (ON) return SDOperand(ON, 0);
1994
Chris Lattner0fb094f2005-11-19 01:44:53 +00001995 RemoveNodeFromCSEMaps(N);
1996 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1997 setNodeValueTypes(N, VT1, VT2);
1998 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001999
2000 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002001 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002002}
2003
Chris Lattnereb19e402005-11-30 22:45:14 +00002004SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2005 MVT::ValueType VT1, MVT::ValueType VT2,
2006 SDOperand Op1, SDOperand Op2,
2007 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002008 // If an identical node already exists, use it.
2009 std::vector<SDOperand> OpList;
2010 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2011 OpList.push_back(Op4);
2012 std::vector<MVT::ValueType> VTList;
2013 VTList.push_back(VT1); VTList.push_back(VT2);
2014 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2015 std::make_pair(VTList, OpList))];
2016 if (ON) return SDOperand(ON, 0);
2017
Chris Lattner0fb094f2005-11-19 01:44:53 +00002018 RemoveNodeFromCSEMaps(N);
2019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2020 setNodeValueTypes(N, VT1, VT2);
2021 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002022
2023 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002024 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002025}
2026
Chris Lattnereb19e402005-11-30 22:45:14 +00002027SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2028 MVT::ValueType VT1, MVT::ValueType VT2,
2029 SDOperand Op1, SDOperand Op2,
2030 SDOperand Op3, SDOperand Op4,
2031 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002032 // If an identical node already exists, use it.
2033 std::vector<SDOperand> OpList;
2034 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2035 OpList.push_back(Op4); OpList.push_back(Op5);
2036 std::vector<MVT::ValueType> VTList;
2037 VTList.push_back(VT1); VTList.push_back(VT2);
2038 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2039 std::make_pair(VTList, OpList))];
2040 if (ON) return SDOperand(ON, 0);
2041
Chris Lattner0fb094f2005-11-19 01:44:53 +00002042 RemoveNodeFromCSEMaps(N);
2043 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2044 setNodeValueTypes(N, VT1, VT2);
2045 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002046
2047 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002048 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002049}
2050
2051// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002052/// This can cause recursive merging of nodes in the DAG.
2053///
Chris Lattner8b52f212005-08-26 18:36:28 +00002054/// This version assumes From/To have a single result value.
2055///
Chris Lattner1e111c72005-09-07 05:37:01 +00002056void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2057 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002058 SDNode *From = FromN.Val, *To = ToN.Val;
2059 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2060 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002061 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002062
Chris Lattner8b8749f2005-08-17 19:00:20 +00002063 while (!From->use_empty()) {
2064 // Process users until they are all gone.
2065 SDNode *U = *From->use_begin();
2066
2067 // This node is about to morph, remove its old self from the CSE maps.
2068 RemoveNodeFromCSEMaps(U);
2069
Chris Lattner65113b22005-11-08 22:07:03 +00002070 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2071 I != E; ++I)
2072 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002073 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002074 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002075 To->addUser(U);
2076 }
2077
2078 // Now that we have modified U, add it back to the CSE maps. If it already
2079 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002080 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2081 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002082 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002083 if (Deleted) Deleted->push_back(U);
2084 DeleteNodeNotInCSEMaps(U);
2085 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002086 }
2087}
2088
2089/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2090/// This can cause recursive merging of nodes in the DAG.
2091///
2092/// This version assumes From/To have matching types and numbers of result
2093/// values.
2094///
Chris Lattner1e111c72005-09-07 05:37:01 +00002095void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2096 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002097 assert(From != To && "Cannot replace uses of with self");
2098 assert(From->getNumValues() == To->getNumValues() &&
2099 "Cannot use this version of ReplaceAllUsesWith!");
2100 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002101 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002102 return;
2103 }
2104
2105 while (!From->use_empty()) {
2106 // Process users until they are all gone.
2107 SDNode *U = *From->use_begin();
2108
2109 // This node is about to morph, remove its old self from the CSE maps.
2110 RemoveNodeFromCSEMaps(U);
2111
Chris Lattner65113b22005-11-08 22:07:03 +00002112 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2113 I != E; ++I)
2114 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002115 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002116 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002117 To->addUser(U);
2118 }
2119
2120 // Now that we have modified U, add it back to the CSE maps. If it already
2121 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002122 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2123 ReplaceAllUsesWith(U, Existing, Deleted);
2124 // U is now dead.
2125 if (Deleted) Deleted->push_back(U);
2126 DeleteNodeNotInCSEMaps(U);
2127 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002128 }
2129}
2130
Chris Lattner8b52f212005-08-26 18:36:28 +00002131/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2132/// This can cause recursive merging of nodes in the DAG.
2133///
2134/// This version can replace From with any result values. To must match the
2135/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002136void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002137 const std::vector<SDOperand> &To,
2138 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002139 assert(From->getNumValues() == To.size() &&
2140 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002141 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002142 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002143 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002144 return;
2145 }
2146
2147 while (!From->use_empty()) {
2148 // Process users until they are all gone.
2149 SDNode *U = *From->use_begin();
2150
2151 // This node is about to morph, remove its old self from the CSE maps.
2152 RemoveNodeFromCSEMaps(U);
2153
Chris Lattner65113b22005-11-08 22:07:03 +00002154 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2155 I != E; ++I)
2156 if (I->Val == From) {
2157 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002158 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002159 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002160 ToOp.Val->addUser(U);
2161 }
2162
2163 // Now that we have modified U, add it back to the CSE maps. If it already
2164 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002165 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2166 ReplaceAllUsesWith(U, Existing, Deleted);
2167 // U is now dead.
2168 if (Deleted) Deleted->push_back(U);
2169 DeleteNodeNotInCSEMaps(U);
2170 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002171 }
2172}
2173
2174
Jim Laskey58b968b2005-08-17 20:08:02 +00002175//===----------------------------------------------------------------------===//
2176// SDNode Class
2177//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002178
Chris Lattnera3255112005-11-08 23:30:28 +00002179
2180/// getValueTypeList - Return a pointer to the specified value type.
2181///
2182MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2183 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2184 VTs[VT] = VT;
2185 return &VTs[VT];
2186}
2187
Chris Lattner5c884562005-01-12 18:37:47 +00002188/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2189/// indicated value. This method ignores uses of other values defined by this
2190/// operation.
2191bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2192 assert(Value < getNumValues() && "Bad value!");
2193
2194 // If there is only one value, this is easy.
2195 if (getNumValues() == 1)
2196 return use_size() == NUses;
2197 if (Uses.size() < NUses) return false;
2198
2199 SDOperand TheValue(this, Value);
2200
2201 std::set<SDNode*> UsersHandled;
2202
2203 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2204 UI != E; ++UI) {
2205 SDNode *User = *UI;
2206 if (User->getNumOperands() == 1 ||
2207 UsersHandled.insert(User).second) // First time we've seen this?
2208 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2209 if (User->getOperand(i) == TheValue) {
2210 if (NUses == 0)
2211 return false; // too many uses
2212 --NUses;
2213 }
2214 }
2215
2216 // Found exactly the right number of uses?
2217 return NUses == 0;
2218}
2219
2220
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002221const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002222 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002223 default:
2224 if (getOpcode() < ISD::BUILTIN_OP_END)
2225 return "<<Unknown DAG Node>>";
2226 else {
Evan Cheng72261582005-12-20 06:22:03 +00002227 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002228 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002229 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2230 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002231
Evan Cheng72261582005-12-20 06:22:03 +00002232 TargetLowering &TLI = G->getTargetLoweringInfo();
2233 const char *Name =
2234 TLI.getTargetNodeName(getOpcode());
2235 if (Name) return Name;
2236 }
2237
2238 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002239 }
2240
Andrew Lenharth95762122005-03-31 21:24:06 +00002241 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002242 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002243 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002244 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002245 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002246 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002247 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002248 case ISD::AssertSext: return "AssertSext";
2249 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002250 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002251 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002252 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002253 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002254 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002255 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002256 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002257 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002258 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002259 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002260 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002261 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002262 case ISD::ConstantPool: return "ConstantPool";
2263 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002264 case ISD::CopyToReg: return "CopyToReg";
2265 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002266 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002267 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002268 case ISD::INLINEASM: return "inlineasm";
2269
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002270 // Unary operators
2271 case ISD::FABS: return "fabs";
2272 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002273 case ISD::FSQRT: return "fsqrt";
2274 case ISD::FSIN: return "fsin";
2275 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002276
2277 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002278 case ISD::ADD: return "add";
2279 case ISD::SUB: return "sub";
2280 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002281 case ISD::MULHU: return "mulhu";
2282 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002283 case ISD::SDIV: return "sdiv";
2284 case ISD::UDIV: return "udiv";
2285 case ISD::SREM: return "srem";
2286 case ISD::UREM: return "urem";
2287 case ISD::AND: return "and";
2288 case ISD::OR: return "or";
2289 case ISD::XOR: return "xor";
2290 case ISD::SHL: return "shl";
2291 case ISD::SRA: return "sra";
2292 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002293 case ISD::ROTL: return "rotl";
2294 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002295 case ISD::FADD: return "fadd";
2296 case ISD::FSUB: return "fsub";
2297 case ISD::FMUL: return "fmul";
2298 case ISD::FDIV: return "fdiv";
2299 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002300 case ISD::VADD: return "vadd";
2301 case ISD::VSUB: return "vsub";
2302 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002303
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002304 case ISD::SETCC: return "setcc";
2305 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002306 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002307 case ISD::ADD_PARTS: return "add_parts";
2308 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002309 case ISD::SHL_PARTS: return "shl_parts";
2310 case ISD::SRA_PARTS: return "sra_parts";
2311 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002312
Chris Lattner7f644642005-04-28 21:44:03 +00002313 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002314 case ISD::SIGN_EXTEND: return "sign_extend";
2315 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002316 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002317 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002318 case ISD::TRUNCATE: return "truncate";
2319 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002320 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002321 case ISD::FP_EXTEND: return "fp_extend";
2322
2323 case ISD::SINT_TO_FP: return "sint_to_fp";
2324 case ISD::UINT_TO_FP: return "uint_to_fp";
2325 case ISD::FP_TO_SINT: return "fp_to_sint";
2326 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002327 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002328
2329 // Control flow instructions
2330 case ISD::BR: return "br";
2331 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002332 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002333 case ISD::BR_CC: return "br_cc";
2334 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002335 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002336 case ISD::CALLSEQ_START: return "callseq_start";
2337 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002338
2339 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002340 case ISD::LOAD: return "load";
2341 case ISD::STORE: return "store";
2342 case ISD::VLOAD: return "vload";
2343 case ISD::EXTLOAD: return "extload";
2344 case ISD::SEXTLOAD: return "sextload";
2345 case ISD::ZEXTLOAD: return "zextload";
2346 case ISD::TRUNCSTORE: return "truncstore";
2347 case ISD::VAARG: return "vaarg";
2348 case ISD::VACOPY: return "vacopy";
2349 case ISD::VAEND: return "vaend";
2350 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002351 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002352 case ISD::EXTRACT_ELEMENT: return "extract_element";
2353 case ISD::BUILD_PAIR: return "build_pair";
2354 case ISD::STACKSAVE: return "stacksave";
2355 case ISD::STACKRESTORE: return "stackrestore";
2356
2357 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002358 case ISD::MEMSET: return "memset";
2359 case ISD::MEMCPY: return "memcpy";
2360 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002361
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002362 // Bit manipulation
2363 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002364 case ISD::CTPOP: return "ctpop";
2365 case ISD::CTTZ: return "cttz";
2366 case ISD::CTLZ: return "ctlz";
2367
2368 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002369 case ISD::READPORT: return "readport";
2370 case ISD::WRITEPORT: return "writeport";
2371 case ISD::READIO: return "readio";
2372 case ISD::WRITEIO: return "writeio";
2373
Chris Lattner36ce6912005-11-29 06:21:05 +00002374 // Debug info
2375 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002376 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002377 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002378
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002379 case ISD::CONDCODE:
2380 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002381 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002382 case ISD::SETOEQ: return "setoeq";
2383 case ISD::SETOGT: return "setogt";
2384 case ISD::SETOGE: return "setoge";
2385 case ISD::SETOLT: return "setolt";
2386 case ISD::SETOLE: return "setole";
2387 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002388
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002389 case ISD::SETO: return "seto";
2390 case ISD::SETUO: return "setuo";
2391 case ISD::SETUEQ: return "setue";
2392 case ISD::SETUGT: return "setugt";
2393 case ISD::SETUGE: return "setuge";
2394 case ISD::SETULT: return "setult";
2395 case ISD::SETULE: return "setule";
2396 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002397
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002398 case ISD::SETEQ: return "seteq";
2399 case ISD::SETGT: return "setgt";
2400 case ISD::SETGE: return "setge";
2401 case ISD::SETLT: return "setlt";
2402 case ISD::SETLE: return "setle";
2403 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002404 }
2405 }
2406}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002407
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002408void SDNode::dump() const { dump(0); }
2409void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002410 std::cerr << (void*)this << ": ";
2411
2412 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2413 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002414 if (getValueType(i) == MVT::Other)
2415 std::cerr << "ch";
2416 else
2417 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002418 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002419 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002420
2421 std::cerr << " ";
2422 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2423 if (i) std::cerr << ", ";
2424 std::cerr << (void*)getOperand(i).Val;
2425 if (unsigned RN = getOperand(i).ResNo)
2426 std::cerr << ":" << RN;
2427 }
2428
2429 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2430 std::cerr << "<" << CSDN->getValue() << ">";
2431 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2432 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002433 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002434 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002435 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002436 std::cerr << "<";
2437 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002438 if (offset > 0)
2439 std::cerr << " + " << offset;
2440 else
2441 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002442 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002443 std::cerr << "<" << FIDN->getIndex() << ">";
2444 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002445 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002446 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002447 std::cerr << "<";
2448 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2449 if (LBB)
2450 std::cerr << LBB->getName() << " ";
2451 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002452 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002453 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002454 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2455 } else {
2456 std::cerr << " #" << R->getReg();
2457 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002458 } else if (const ExternalSymbolSDNode *ES =
2459 dyn_cast<ExternalSymbolSDNode>(this)) {
2460 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002461 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2462 if (M->getValue())
2463 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2464 else
2465 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002466 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2467 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002468 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002469}
2470
Chris Lattnerde202b32005-11-09 23:47:37 +00002471static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002472 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2473 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002474 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002475 else
2476 std::cerr << "\n" << std::string(indent+2, ' ')
2477 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002478
Chris Lattnerea946cd2005-01-09 20:38:33 +00002479
2480 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002481 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002482}
2483
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484void SelectionDAG::dump() const {
2485 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002486 std::vector<const SDNode*> Nodes;
2487 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2488 I != E; ++I)
2489 Nodes.push_back(I);
2490
Chris Lattner49d24712005-01-09 20:26:36 +00002491 std::sort(Nodes.begin(), Nodes.end());
2492
2493 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002494 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002495 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002496 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002497
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002498 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002499
Chris Lattnerc3aae252005-01-07 07:46:32 +00002500 std::cerr << "\n\n";
2501}
2502