blob: 054a5cc2240fe9568e784f676dc9f79f75eabf8c [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 Lattner36ce6912005-11-29 06:21:05 +0000280 case ISD::STRING:
281 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
282 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000283 case ISD::CONDCODE:
284 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
285 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000286 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000287 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
288 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000289 case ISD::GlobalAddress: {
290 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
292 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 }
295 case ISD::TargetGlobalAddress: {
296 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
297 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
298 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000299 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000300 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000302 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000304 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000305 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000306 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000308 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000310 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000311 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000312 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000314 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 break;
316 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000317 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000319 case ISD::TargetExternalSymbol:
320 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
321 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000322 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000324 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
325 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000326 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000327 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
328 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000329 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000330 case ISD::SRCVALUE: {
331 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000332 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000333 break;
334 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000335 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000336 Erased = Loads.erase(std::make_pair(N->getOperand(1),
337 std::make_pair(N->getOperand(0),
338 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000340 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000342 if (N->getNumOperands() == 0) {
343 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
344 N->getValueType(0)));
345 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased =
347 UnaryOps.erase(std::make_pair(N->getOpcode(),
348 std::make_pair(N->getOperand(0),
349 N->getValueType(0))));
350 } else if (N->getNumOperands() == 2) {
351 Erased =
352 BinaryOps.erase(std::make_pair(N->getOpcode(),
353 std::make_pair(N->getOperand(0),
354 N->getOperand(1))));
355 } else {
356 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
357 Erased =
358 OneResultNodes.erase(std::make_pair(N->getOpcode(),
359 std::make_pair(N->getValueType(0),
360 Ops)));
361 }
Chris Lattner385328c2005-05-14 07:42:29 +0000362 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000363 // Remove the node from the ArbitraryNodes map.
364 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
365 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000366 Erased =
367 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
368 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000369 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 break;
371 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372#ifndef NDEBUG
373 // Verify that the node was actually in one of the CSE maps, unless it has a
374 // flag result (which cannot be CSE'd) or is one of the special cases that are
375 // not subject to CSE.
376 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner0ff5c272006-01-28 00:18:58 +0000377 N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000378 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000379
380 N->dump();
381 assert(0 && "Node is not in map!");
382 }
383#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384}
385
Chris Lattner8b8749f2005-08-17 19:00:20 +0000386/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
387/// has been taken out and modified in some way. If the specified node already
388/// exists in the CSE maps, do not modify the maps, but return the existing node
389/// instead. If it doesn't exist, add it and return null.
390///
391SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000393 if (N->getOpcode() == ISD::CALLSEQ_START ||
Chris Lattnerfe14b342005-12-01 23:14:50 +0000394 N->getOpcode() == ISD::CALLSEQ_END ||
Chris Lattner9f8cc692005-12-19 22:21:21 +0000395 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000396 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000397
Chris Lattner9f8cc692005-12-19 22:21:21 +0000398 // Check that remaining values produced are not flags.
399 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
400 if (N->getValueType(i) == MVT::Flag)
401 return 0; // Never CSE anything that produces a flag.
402
Chris Lattnerfe14b342005-12-01 23:14:50 +0000403 if (N->getNumValues() == 1) {
404 if (N->getNumOperands() == 1) {
405 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
406 std::make_pair(N->getOperand(0),
407 N->getValueType(0)))];
408 if (U) return U;
409 U = N;
410 } else if (N->getNumOperands() == 2) {
411 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
412 std::make_pair(N->getOperand(0),
413 N->getOperand(1)))];
414 if (B) return B;
415 B = N;
416 } else {
417 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
418 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
419 std::make_pair(N->getValueType(0), Ops))];
420 if (ORN) return ORN;
421 ORN = N;
422 }
423 } else {
424 if (N->getOpcode() == ISD::LOAD) {
425 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
426 std::make_pair(N->getOperand(0),
427 N->getValueType(0)))];
428 if (L) return L;
429 L = N;
430 } else {
431 // Remove the node from the ArbitraryNodes map.
432 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
433 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
434 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
435 std::make_pair(RV, Ops))];
436 if (AN) return AN;
437 AN = N;
438 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000439 }
440 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000441}
442
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000443/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
444/// were replaced with those specified. If this node is never memoized,
445/// return null, otherwise return a pointer to the slot it would take. If a
446/// node already exists with these operands, the slot will be non-null.
447SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
448 if (N->getOpcode() == ISD::CALLSEQ_START ||
449 N->getOpcode() == ISD::CALLSEQ_END ||
450 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
451 return 0; // Never add these nodes.
452
453 // Check that remaining values produced are not flags.
454 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
455 if (N->getValueType(i) == MVT::Flag)
456 return 0; // Never CSE anything that produces a flag.
457
458 if (N->getNumValues() == 1) {
459 return &UnaryOps[std::make_pair(N->getOpcode(),
460 std::make_pair(Op, N->getValueType(0)))];
461 } else {
462 // Remove the node from the ArbitraryNodes map.
463 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
464 std::vector<SDOperand> Ops;
465 Ops.push_back(Op);
466 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
467 std::make_pair(RV, Ops))];
468 }
469 return 0;
470}
471
472/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
473/// were replaced with those specified. If this node is never memoized,
474/// return null, otherwise return a pointer to the slot it would take. If a
475/// node already exists with these operands, the slot will be non-null.
476SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
477 SDOperand Op1, SDOperand Op2) {
478 if (N->getOpcode() == ISD::CALLSEQ_START ||
479 N->getOpcode() == ISD::CALLSEQ_END ||
480 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
481 return 0; // Never add these nodes.
482
483 // Check that remaining values produced are not flags.
484 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
485 if (N->getValueType(i) == MVT::Flag)
486 return 0; // Never CSE anything that produces a flag.
487
488 if (N->getNumValues() == 1) {
489 return &BinaryOps[std::make_pair(N->getOpcode(),
490 std::make_pair(Op1, Op2))];
491 } else {
492 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
493 std::vector<SDOperand> Ops;
494 Ops.push_back(Op1);
495 Ops.push_back(Op2);
496 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
497 std::make_pair(RV, Ops))];
498 }
499 return 0;
500}
501
502
503/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
504/// were replaced with those specified. If this node is never memoized,
505/// return null, otherwise return a pointer to the slot it would take. If a
506/// node already exists with these operands, the slot will be non-null.
507SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
508 const std::vector<SDOperand> &Ops) {
509 if (N->getOpcode() == ISD::CALLSEQ_START ||
510 N->getOpcode() == ISD::CALLSEQ_END ||
511 N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
512 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);
608 N = new ConstantFPSDNode(Val, VT);
609 AllNodes.push_back(N);
610 return SDOperand(N, 0);
611}
612
Chris Lattnerc3aae252005-01-07 07:46:32 +0000613SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000614 MVT::ValueType VT, int offset) {
615 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000616 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000617 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000618 AllNodes.push_back(N);
619 return SDOperand(N, 0);
620}
621
622SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000623 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000624 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000625 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000626 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000627 AllNodes.push_back(N);
628 return SDOperand(N, 0);
629}
630
631SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
632 SDNode *&N = FrameIndices[FI];
633 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000634 N = new FrameIndexSDNode(FI, VT, false);
635 AllNodes.push_back(N);
636 return SDOperand(N, 0);
637}
638
639SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
640 SDNode *&N = TargetFrameIndices[FI];
641 if (N) return SDOperand(N, 0);
642 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000643 AllNodes.push_back(N);
644 return SDOperand(N, 0);
645}
646
Chris Lattner5839bf22005-08-26 17:15:30 +0000647SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
648 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000649 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000650 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000651 AllNodes.push_back(N);
652 return SDOperand(N, 0);
653}
654
Chris Lattner5839bf22005-08-26 17:15:30 +0000655SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
656 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000657 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000658 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000659 AllNodes.push_back(N);
660 return SDOperand(N, 0);
661}
662
663SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
664 SDNode *&N = BBNodes[MBB];
665 if (N) return SDOperand(N, 0);
666 N = new BasicBlockSDNode(MBB);
667 AllNodes.push_back(N);
668 return SDOperand(N, 0);
669}
670
Chris Lattner15e4b012005-07-10 00:07:11 +0000671SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
672 if ((unsigned)VT >= ValueTypeNodes.size())
673 ValueTypeNodes.resize(VT+1);
674 if (ValueTypeNodes[VT] == 0) {
675 ValueTypeNodes[VT] = new VTSDNode(VT);
676 AllNodes.push_back(ValueTypeNodes[VT]);
677 }
678
679 return SDOperand(ValueTypeNodes[VT], 0);
680}
681
Chris Lattnerc3aae252005-01-07 07:46:32 +0000682SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
683 SDNode *&N = ExternalSymbols[Sym];
684 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000685 N = new ExternalSymbolSDNode(false, Sym, VT);
686 AllNodes.push_back(N);
687 return SDOperand(N, 0);
688}
689
690SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
691 SDNode *&N = TargetExternalSymbols[Sym];
692 if (N) return SDOperand(N, 0);
693 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000694 AllNodes.push_back(N);
695 return SDOperand(N, 0);
696}
697
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000698SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
699 if ((unsigned)Cond >= CondCodeNodes.size())
700 CondCodeNodes.resize(Cond+1);
701
Chris Lattner079a27a2005-08-09 20:40:02 +0000702 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000703 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000704 AllNodes.push_back(CondCodeNodes[Cond]);
705 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000706 return SDOperand(CondCodeNodes[Cond], 0);
707}
708
Chris Lattner0fdd7682005-08-30 22:38:38 +0000709SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
710 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
711 if (!Reg) {
712 Reg = new RegisterSDNode(RegNo, VT);
713 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000714 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000715 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000716}
717
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000718SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
719 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000720 // These setcc operations always fold.
721 switch (Cond) {
722 default: break;
723 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000724 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000725 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000726 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 }
728
Chris Lattner67255a12005-04-07 18:14:58 +0000729 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
730 uint64_t C2 = N2C->getValue();
731 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
732 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000733
Chris Lattnerc3aae252005-01-07 07:46:32 +0000734 // Sign extend the operands if required
735 if (ISD::isSignedIntSetCC(Cond)) {
736 C1 = N1C->getSignExtended();
737 C2 = N2C->getSignExtended();
738 }
739
740 switch (Cond) {
741 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000742 case ISD::SETEQ: return getConstant(C1 == C2, VT);
743 case ISD::SETNE: return getConstant(C1 != C2, VT);
744 case ISD::SETULT: return getConstant(C1 < C2, VT);
745 case ISD::SETUGT: return getConstant(C1 > C2, VT);
746 case ISD::SETULE: return getConstant(C1 <= C2, VT);
747 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
748 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
749 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
750 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
751 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000752 }
Chris Lattner24673922005-04-07 18:58:54 +0000753 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000754 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000755 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
756 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
757
758 // If the comparison constant has bits in the upper part, the
759 // zero-extended value could never match.
760 if (C2 & (~0ULL << InSize)) {
761 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
762 switch (Cond) {
763 case ISD::SETUGT:
764 case ISD::SETUGE:
765 case ISD::SETEQ: return getConstant(0, VT);
766 case ISD::SETULT:
767 case ISD::SETULE:
768 case ISD::SETNE: return getConstant(1, VT);
769 case ISD::SETGT:
770 case ISD::SETGE:
771 // True if the sign bit of C2 is set.
772 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
773 case ISD::SETLT:
774 case ISD::SETLE:
775 // True if the sign bit of C2 isn't set.
776 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
777 default:
778 break;
779 }
780 }
781
782 // Otherwise, we can perform the comparison with the low bits.
783 switch (Cond) {
784 case ISD::SETEQ:
785 case ISD::SETNE:
786 case ISD::SETUGT:
787 case ISD::SETUGE:
788 case ISD::SETULT:
789 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000790 return getSetCC(VT, N1.getOperand(0),
791 getConstant(C2, N1.getOperand(0).getValueType()),
792 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000793 default:
794 break; // todo, be more careful with signed comparisons
795 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000796 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
797 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
798 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
799 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
800 MVT::ValueType ExtDstTy = N1.getValueType();
801 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000802
Chris Lattner7b2880c2005-08-24 22:44:39 +0000803 // If the extended part has any inconsistent bits, it cannot ever
804 // compare equal. In other words, they have to be all ones or all
805 // zeros.
806 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000807 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000808 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
809 return getConstant(Cond == ISD::SETNE, VT);
810
811 // Otherwise, make this a use of a zext.
812 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000813 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000814 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000815 }
816
Chris Lattner67255a12005-04-07 18:14:58 +0000817 uint64_t MinVal, MaxVal;
818 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
819 if (ISD::isSignedIntSetCC(Cond)) {
820 MinVal = 1ULL << (OperandBitSize-1);
821 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
822 MaxVal = ~0ULL >> (65-OperandBitSize);
823 else
824 MaxVal = 0;
825 } else {
826 MinVal = 0;
827 MaxVal = ~0ULL >> (64-OperandBitSize);
828 }
829
830 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
831 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
832 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
833 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000834 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
835 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000836 }
837
838 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
839 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
840 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000841 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
842 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000843 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000844
Nate Begeman72ea2812005-04-14 08:56:52 +0000845 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
846 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000847
Nate Begeman72ea2812005-04-14 08:56:52 +0000848 // Canonicalize setgt X, Min --> setne X, Min
849 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000850 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000851
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000852 // If we have setult X, 1, turn it into seteq X, 0
853 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000854 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
855 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000856 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000857 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000858 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
859 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000860
861 // If we have "setcc X, C1", check to see if we can shrink the immediate
862 // by changing cc.
863
864 // SETUGT X, SINTMAX -> SETLT X, 0
865 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
866 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000867 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000868
869 // FIXME: Implement the rest of these.
870
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000871
872 // Fold bit comparisons when we can.
873 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
874 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
875 if (ConstantSDNode *AndRHS =
876 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
877 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
878 // Perform the xform if the AND RHS is a single bit.
879 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
880 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000881 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000882 TLI.getShiftAmountTy()));
883 }
884 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
885 // (X & 8) == 8 --> (X & 8) >> 3
886 // Perform the xform if C2 is a single bit.
887 if ((C2 & (C2-1)) == 0) {
888 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000889 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000890 }
891 }
892 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000893 }
Chris Lattner67255a12005-04-07 18:14:58 +0000894 } else if (isa<ConstantSDNode>(N1.Val)) {
895 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000896 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000897 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000898
899 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
900 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
901 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000902
Chris Lattnerc3aae252005-01-07 07:46:32 +0000903 switch (Cond) {
904 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000905 case ISD::SETEQ: return getConstant(C1 == C2, VT);
906 case ISD::SETNE: return getConstant(C1 != C2, VT);
907 case ISD::SETLT: return getConstant(C1 < C2, VT);
908 case ISD::SETGT: return getConstant(C1 > C2, VT);
909 case ISD::SETLE: return getConstant(C1 <= C2, VT);
910 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 }
912 } else {
913 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000914 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915 }
916
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000917 // Could not fold it.
918 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919}
920
Chris Lattnerc3aae252005-01-07 07:46:32 +0000921/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000922///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000924 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
925 if (!N) {
926 N = new SDNode(Opcode, VT);
927 AllNodes.push_back(N);
928 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929 return SDOperand(N, 0);
930}
931
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
933 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000934 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000935 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
937 uint64_t Val = C->getValue();
938 switch (Opcode) {
939 default: break;
940 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000941 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000942 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
943 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000944 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
945 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000946 case ISD::BIT_CONVERT:
947 if (VT == MVT::f32) {
948 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
949 return getConstantFP(BitsToFloat(Val), VT);
950 } else if (VT == MVT::f64) {
951 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
952 return getConstantFP(BitsToDouble(Val), VT);
953 }
954 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000955 case ISD::BSWAP:
956 switch(VT) {
957 default: assert(0 && "Invalid bswap!"); break;
958 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
959 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
960 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
961 }
962 break;
963 case ISD::CTPOP:
964 switch(VT) {
965 default: assert(0 && "Invalid ctpop!"); break;
966 case MVT::i1: return getConstant(Val != 0, VT);
967 case MVT::i8:
968 Tmp1 = (unsigned)Val & 0xFF;
969 return getConstant(CountPopulation_32(Tmp1), VT);
970 case MVT::i16:
971 Tmp1 = (unsigned)Val & 0xFFFF;
972 return getConstant(CountPopulation_32(Tmp1), VT);
973 case MVT::i32:
974 return getConstant(CountPopulation_32((unsigned)Val), VT);
975 case MVT::i64:
976 return getConstant(CountPopulation_64(Val), VT);
977 }
978 case ISD::CTLZ:
979 switch(VT) {
980 default: assert(0 && "Invalid ctlz!"); break;
981 case MVT::i1: return getConstant(Val == 0, VT);
982 case MVT::i8:
983 Tmp1 = (unsigned)Val & 0xFF;
984 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
985 case MVT::i16:
986 Tmp1 = (unsigned)Val & 0xFFFF;
987 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
988 case MVT::i32:
989 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
990 case MVT::i64:
991 return getConstant(CountLeadingZeros_64(Val), VT);
992 }
993 case ISD::CTTZ:
994 switch(VT) {
995 default: assert(0 && "Invalid cttz!"); break;
996 case MVT::i1: return getConstant(Val == 0, VT);
997 case MVT::i8:
998 Tmp1 = (unsigned)Val | 0x100;
999 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1000 case MVT::i16:
1001 Tmp1 = (unsigned)Val | 0x10000;
1002 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1003 case MVT::i32:
1004 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1005 case MVT::i64:
1006 return getConstant(CountTrailingZeros_64(Val), VT);
1007 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 }
1009 }
1010
Chris Lattner94683772005-12-23 05:30:37 +00001011 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1013 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001014 case ISD::FNEG:
1015 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001016 case ISD::FABS:
1017 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 case ISD::FP_ROUND:
1019 case ISD::FP_EXTEND:
1020 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001021 case ISD::FP_TO_SINT:
1022 return getConstant((int64_t)C->getValue(), VT);
1023 case ISD::FP_TO_UINT:
1024 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001025 case ISD::BIT_CONVERT:
1026 if (VT == MVT::i32) {
1027 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1028 return getConstant(FloatToBits(C->getValue()), VT);
1029 } else if (VT == MVT::i64) {
1030 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1031 return getConstant(DoubleToBits(C->getValue()), VT);
1032 }
1033 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001034 }
1035
1036 unsigned OpOpcode = Operand.Val->getOpcode();
1037 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001038 case ISD::TokenFactor:
1039 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 case ISD::SIGN_EXTEND:
1041 if (Operand.getValueType() == VT) return Operand; // noop extension
1042 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1043 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1044 break;
1045 case ISD::ZERO_EXTEND:
1046 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001047 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001048 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001050 case ISD::ANY_EXTEND:
1051 if (Operand.getValueType() == VT) return Operand; // noop extension
1052 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1053 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1054 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1055 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056 case ISD::TRUNCATE:
1057 if (Operand.getValueType() == VT) return Operand; // noop truncate
1058 if (OpOpcode == ISD::TRUNCATE)
1059 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001060 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1061 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001062 // If the source is smaller than the dest, we still need an extend.
1063 if (Operand.Val->getOperand(0).getValueType() < VT)
1064 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1065 else if (Operand.Val->getOperand(0).getValueType() > VT)
1066 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1067 else
1068 return Operand.Val->getOperand(0);
1069 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 break;
Chris Lattner35481892005-12-23 00:16:34 +00001071 case ISD::BIT_CONVERT:
1072 // Basic sanity checking.
1073 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1074 "Cannot BIT_CONVERT between two different types!");
1075 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001076 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1077 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001078 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001079 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001080 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1081 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001082 Operand.Val->getOperand(0));
1083 if (OpOpcode == ISD::FNEG) // --X -> X
1084 return Operand.Val->getOperand(0);
1085 break;
1086 case ISD::FABS:
1087 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1088 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1089 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001090 }
1091
Chris Lattner43247a12005-08-25 19:12:10 +00001092 SDNode *N;
1093 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1094 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001095 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001096 E = N = new SDNode(Opcode, Operand);
1097 } else {
1098 N = new SDNode(Opcode, Operand);
1099 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 N->setValueTypes(VT);
1101 AllNodes.push_back(N);
1102 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001103}
1104
Chris Lattner36019aa2005-04-18 03:48:41 +00001105
1106
Chris Lattnerc3aae252005-01-07 07:46:32 +00001107SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1108 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001109#ifndef NDEBUG
1110 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001111 case ISD::TokenFactor:
1112 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1113 N2.getValueType() == MVT::Other && "Invalid token factor!");
1114 break;
Chris Lattner76365122005-01-16 02:23:22 +00001115 case ISD::AND:
1116 case ISD::OR:
1117 case ISD::XOR:
1118 case ISD::UDIV:
1119 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001120 case ISD::MULHU:
1121 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001122 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1123 // fall through
1124 case ISD::ADD:
1125 case ISD::SUB:
1126 case ISD::MUL:
1127 case ISD::SDIV:
1128 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001129 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1130 // fall through.
1131 case ISD::FADD:
1132 case ISD::FSUB:
1133 case ISD::FMUL:
1134 case ISD::FDIV:
1135 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001136 assert(N1.getValueType() == N2.getValueType() &&
1137 N1.getValueType() == VT && "Binary operator types must match!");
1138 break;
1139
1140 case ISD::SHL:
1141 case ISD::SRA:
1142 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001143 case ISD::ROTL:
1144 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001145 assert(VT == N1.getValueType() &&
1146 "Shift operators return type must be the same as their first arg");
1147 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001148 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001149 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001150 case ISD::FP_ROUND_INREG: {
1151 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1152 assert(VT == N1.getValueType() && "Not an inreg round!");
1153 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1154 "Cannot FP_ROUND_INREG integer types");
1155 assert(EVT <= VT && "Not rounding down!");
1156 break;
1157 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001158 case ISD::AssertSext:
1159 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001160 case ISD::SIGN_EXTEND_INREG: {
1161 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1162 assert(VT == N1.getValueType() && "Not an inreg extend!");
1163 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1164 "Cannot *_EXTEND_INREG FP types");
1165 assert(EVT <= VT && "Not extending!");
1166 }
1167
Chris Lattner76365122005-01-16 02:23:22 +00001168 default: break;
1169 }
1170#endif
1171
Chris Lattnerc3aae252005-01-07 07:46:32 +00001172 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1173 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1174 if (N1C) {
1175 if (N2C) {
1176 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1177 switch (Opcode) {
1178 case ISD::ADD: return getConstant(C1 + C2, VT);
1179 case ISD::SUB: return getConstant(C1 - C2, VT);
1180 case ISD::MUL: return getConstant(C1 * C2, VT);
1181 case ISD::UDIV:
1182 if (C2) return getConstant(C1 / C2, VT);
1183 break;
1184 case ISD::UREM :
1185 if (C2) return getConstant(C1 % C2, VT);
1186 break;
1187 case ISD::SDIV :
1188 if (C2) return getConstant(N1C->getSignExtended() /
1189 N2C->getSignExtended(), VT);
1190 break;
1191 case ISD::SREM :
1192 if (C2) return getConstant(N1C->getSignExtended() %
1193 N2C->getSignExtended(), VT);
1194 break;
1195 case ISD::AND : return getConstant(C1 & C2, VT);
1196 case ISD::OR : return getConstant(C1 | C2, VT);
1197 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001198 case ISD::SHL : return getConstant(C1 << C2, VT);
1199 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001200 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001201 case ISD::ROTL :
1202 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1203 VT);
1204 case ISD::ROTR :
1205 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1206 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 default: break;
1208 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001209 } else { // Cannonicalize constant to RHS if commutative
1210 if (isCommutativeBinOp(Opcode)) {
1211 std::swap(N1C, N2C);
1212 std::swap(N1, N2);
1213 }
1214 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001215 }
1216
1217 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1218 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001219 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 if (N2CFP) {
1221 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1222 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001223 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1224 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1225 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1226 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 if (C2) return getConstantFP(C1 / C2, VT);
1228 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001229 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230 if (C2) return getConstantFP(fmod(C1, C2), VT);
1231 break;
1232 default: break;
1233 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001234 } else { // Cannonicalize constant to RHS if commutative
1235 if (isCommutativeBinOp(Opcode)) {
1236 std::swap(N1CFP, N2CFP);
1237 std::swap(N1, N2);
1238 }
1239 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001240 }
1241
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 // Finally, fold operations that do not require constants.
1243 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001244 case ISD::FP_ROUND_INREG:
1245 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1246 break;
1247 case ISD::SIGN_EXTEND_INREG: {
1248 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1249 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001250 break;
1251 }
1252
Nate Begemaneea805e2005-04-13 21:23:31 +00001253 // FIXME: figure out how to safely handle things like
1254 // int foo(int x) { return 1 << (x & 255); }
1255 // int bar() { return foo(256); }
1256#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001257 case ISD::SHL:
1258 case ISD::SRL:
1259 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001260 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001261 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001262 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001263 else if (N2.getOpcode() == ISD::AND)
1264 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1265 // If the and is only masking out bits that cannot effect the shift,
1266 // eliminate the and.
1267 unsigned NumBits = MVT::getSizeInBits(VT);
1268 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1269 return getNode(Opcode, VT, N1, N2.getOperand(0));
1270 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001271 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001272#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001273 }
1274
Chris Lattner27e9b412005-05-11 18:57:39 +00001275 // Memoize this node if possible.
1276 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001277 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1278 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001279 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1280 if (BON) return SDOperand(BON, 0);
1281
1282 BON = N = new SDNode(Opcode, N1, N2);
1283 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001284 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001285 }
1286
Chris Lattner3e011362005-05-14 07:45:46 +00001287 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001288 AllNodes.push_back(N);
1289 return SDOperand(N, 0);
1290}
1291
Chris Lattnerc3aae252005-01-07 07:46:32 +00001292SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1293 SDOperand N1, SDOperand N2, SDOperand N3) {
1294 // Perform various simplifications.
1295 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1296 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1297 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1298 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001299 case ISD::SETCC: {
1300 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001301 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001302 if (Simp.Val) return Simp;
1303 break;
1304 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001305 case ISD::SELECT:
1306 if (N1C)
1307 if (N1C->getValue())
1308 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001309 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310 return N3; // select false, X, Y -> Y
1311
1312 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001313 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001314 case ISD::BRCOND:
1315 if (N2C)
1316 if (N2C->getValue()) // Unconditional branch
1317 return getNode(ISD::BR, MVT::Other, N1, N3);
1318 else
1319 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001320 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 }
1322
Chris Lattner385328c2005-05-14 07:42:29 +00001323 std::vector<SDOperand> Ops;
1324 Ops.reserve(3);
1325 Ops.push_back(N1);
1326 Ops.push_back(N2);
1327 Ops.push_back(N3);
1328
Chris Lattner43247a12005-08-25 19:12:10 +00001329 // Memoize node if it doesn't produce a flag.
1330 SDNode *N;
1331 if (VT != MVT::Flag) {
1332 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1333 if (E) return SDOperand(E, 0);
1334 E = N = new SDNode(Opcode, N1, N2, N3);
1335 } else {
1336 N = new SDNode(Opcode, N1, N2, N3);
1337 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001338 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001339 AllNodes.push_back(N);
1340 return SDOperand(N, 0);
1341}
1342
1343SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001344 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001345 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001346 std::vector<SDOperand> Ops;
1347 Ops.reserve(4);
1348 Ops.push_back(N1);
1349 Ops.push_back(N2);
1350 Ops.push_back(N3);
1351 Ops.push_back(N4);
1352 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001353}
1354
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001355SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1356 SDOperand N1, SDOperand N2, SDOperand N3,
1357 SDOperand N4, SDOperand N5) {
1358 std::vector<SDOperand> Ops;
1359 Ops.reserve(5);
1360 Ops.push_back(N1);
1361 Ops.push_back(N2);
1362 Ops.push_back(N3);
1363 Ops.push_back(N4);
1364 Ops.push_back(N5);
1365 return getNode(Opcode, VT, Ops);
1366}
1367
Evan Cheng7038daf2005-12-10 00:37:58 +00001368// setAdjCallChain - This method changes the token chain of an
1369// CALLSEQ_START/END node to be the specified operand.
1370void SDNode::setAdjCallChain(SDOperand N) {
1371 assert(N.getValueType() == MVT::Other);
1372 assert((getOpcode() == ISD::CALLSEQ_START ||
1373 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1374
1375 OperandList[0].Val->removeUser(this);
1376 OperandList[0] = N;
1377 OperandList[0].Val->Uses.push_back(this);
1378}
1379
Chris Lattner6a542892006-01-24 05:48:21 +00001380// setAdjCallFlag - This method changes the flag input of an
1381// CALLSEQ_START/END node to be the specified operand.
1382void SDNode::setAdjCallFlag(SDOperand N) {
1383 assert(N.getValueType() == MVT::Flag);
1384 assert((getOpcode() == ISD::CALLSEQ_START ||
1385 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1386
1387 SDOperand &FlagOp = OperandList[getNumOperands()-1];
1388 assert(FlagOp.getValueType() == MVT::Flag);
1389 FlagOp.Val->removeUser(this);
1390 FlagOp = N;
1391 FlagOp.Val->Uses.push_back(this);
1392}
Evan Cheng7038daf2005-12-10 00:37:58 +00001393
1394
1395SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1396 SDOperand Chain, SDOperand Ptr,
1397 SDOperand SV) {
1398 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1399 if (N) return SDOperand(N, 0);
1400 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1401
1402 // Loads have a token chain.
1403 setNodeValueTypes(N, VT, MVT::Other);
1404 AllNodes.push_back(N);
1405 return SDOperand(N, 0);
1406}
1407
1408SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1409 SDOperand Chain, SDOperand Ptr,
1410 SDOperand SV) {
1411 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1412 if (N) return SDOperand(N, 0);
1413 std::vector<SDOperand> Ops;
1414 Ops.reserve(5);
1415 Ops.push_back(Chain);
1416 Ops.push_back(Ptr);
1417 Ops.push_back(getConstant(Count, MVT::i32));
1418 Ops.push_back(getValueType(EVT));
1419 Ops.push_back(SV);
1420 std::vector<MVT::ValueType> VTs;
1421 VTs.reserve(2);
1422 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1423 return getNode(ISD::VLOAD, VTs, Ops);
1424}
1425
1426SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1427 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1428 MVT::ValueType EVT) {
1429 std::vector<SDOperand> Ops;
1430 Ops.reserve(4);
1431 Ops.push_back(Chain);
1432 Ops.push_back(Ptr);
1433 Ops.push_back(SV);
1434 Ops.push_back(getValueType(EVT));
1435 std::vector<MVT::ValueType> VTs;
1436 VTs.reserve(2);
1437 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1438 return getNode(Opcode, VTs, Ops);
1439}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001440
Chris Lattner0437cdd2005-05-09 04:14:13 +00001441SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001442 assert((!V || isa<PointerType>(V->getType())) &&
1443 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001444 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1445 if (N) return SDOperand(N, 0);
1446
1447 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001448 AllNodes.push_back(N);
1449 return SDOperand(N, 0);
1450}
1451
Nate Begemanacc398c2006-01-25 18:21:52 +00001452SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1453 SDOperand Chain, SDOperand Ptr,
1454 SDOperand SV) {
1455 std::vector<SDOperand> Ops;
1456 Ops.reserve(3);
1457 Ops.push_back(Chain);
1458 Ops.push_back(Ptr);
1459 Ops.push_back(SV);
1460 std::vector<MVT::ValueType> VTs;
1461 VTs.reserve(2);
1462 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1463 return getNode(ISD::VAARG, VTs, Ops);
1464}
1465
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001466SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001467 std::vector<SDOperand> &Ops) {
1468 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001469 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001470 case 1: return getNode(Opcode, VT, Ops[0]);
1471 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1472 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001473 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001474 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001475
Chris Lattner89c34632005-05-14 06:20:26 +00001476 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001477 switch (Opcode) {
1478 default: break;
1479 case ISD::BRCONDTWOWAY:
1480 if (N1C)
1481 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001482 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001483 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001484 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001485 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001486 case ISD::BRTWOWAY_CC:
1487 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1488 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1489 "LHS and RHS of comparison must have same type!");
1490 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001491 case ISD::TRUNCSTORE: {
1492 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1493 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1494#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1495 // If this is a truncating store of a constant, convert to the desired type
1496 // and store it instead.
1497 if (isa<Constant>(Ops[0])) {
1498 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1499 if (isa<Constant>(Op))
1500 N1 = Op;
1501 }
1502 // Also for ConstantFP?
1503#endif
1504 if (Ops[0].getValueType() == EVT) // Normal store?
1505 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1506 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1507 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1508 "Can't do FP-INT conversion!");
1509 break;
1510 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001511 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001512 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001513 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1514 "LHS and RHS of condition must have same type!");
1515 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1516 "True and False arms of SelectCC must have same type!");
1517 assert(Ops[2].getValueType() == VT &&
1518 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001519 break;
1520 }
1521 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001522 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001523 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1524 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001525 break;
1526 }
Chris Lattneref847df2005-04-09 03:27:28 +00001527 }
1528
Chris Lattner385328c2005-05-14 07:42:29 +00001529 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001530 SDNode *N;
1531 if (VT != MVT::Flag) {
1532 SDNode *&E =
1533 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1534 if (E) return SDOperand(E, 0);
1535 E = N = new SDNode(Opcode, Ops);
1536 } else {
1537 N = new SDNode(Opcode, Ops);
1538 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001539 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001540 AllNodes.push_back(N);
1541 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001542}
1543
Chris Lattner89c34632005-05-14 06:20:26 +00001544SDOperand SelectionDAG::getNode(unsigned Opcode,
1545 std::vector<MVT::ValueType> &ResultTys,
1546 std::vector<SDOperand> &Ops) {
1547 if (ResultTys.size() == 1)
1548 return getNode(Opcode, ResultTys[0], Ops);
1549
Chris Lattner5f056bf2005-07-10 01:55:33 +00001550 switch (Opcode) {
1551 case ISD::EXTLOAD:
1552 case ISD::SEXTLOAD:
1553 case ISD::ZEXTLOAD: {
1554 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1555 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1556 // If they are asking for an extending load from/to the same thing, return a
1557 // normal load.
1558 if (ResultTys[0] == EVT)
1559 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1560 assert(EVT < ResultTys[0] &&
1561 "Should only be an extending load, not truncating!");
1562 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1563 "Cannot sign/zero extend a FP load!");
1564 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1565 "Cannot convert from FP to Int or Int -> FP!");
1566 break;
1567 }
1568
Chris Lattnere89083a2005-05-14 07:25:05 +00001569 // FIXME: figure out how to safely handle things like
1570 // int foo(int x) { return 1 << (x & 255); }
1571 // int bar() { return foo(256); }
1572#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001573 case ISD::SRA_PARTS:
1574 case ISD::SRL_PARTS:
1575 case ISD::SHL_PARTS:
1576 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001577 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001578 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1579 else if (N3.getOpcode() == ISD::AND)
1580 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1581 // If the and is only masking out bits that cannot effect the shift,
1582 // eliminate the and.
1583 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1584 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1585 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1586 }
1587 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001588#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001589 }
Chris Lattner89c34632005-05-14 06:20:26 +00001590
Chris Lattner43247a12005-08-25 19:12:10 +00001591 // Memoize the node unless it returns a flag.
1592 SDNode *N;
1593 if (ResultTys.back() != MVT::Flag) {
1594 SDNode *&E =
1595 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1596 if (E) return SDOperand(E, 0);
1597 E = N = new SDNode(Opcode, Ops);
1598 } else {
1599 N = new SDNode(Opcode, Ops);
1600 }
Chris Lattnera3255112005-11-08 23:30:28 +00001601 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001602 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001603 return SDOperand(N, 0);
1604}
1605
Chris Lattnera3255112005-11-08 23:30:28 +00001606void SelectionDAG::setNodeValueTypes(SDNode *N,
1607 std::vector<MVT::ValueType> &RetVals) {
1608 switch (RetVals.size()) {
1609 case 0: return;
1610 case 1: N->setValueTypes(RetVals[0]); return;
1611 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1612 default: break;
1613 }
1614
1615 std::list<std::vector<MVT::ValueType> >::iterator I =
1616 std::find(VTList.begin(), VTList.end(), RetVals);
1617 if (I == VTList.end()) {
1618 VTList.push_front(RetVals);
1619 I = VTList.begin();
1620 }
1621
1622 N->setValueTypes(&(*I)[0], I->size());
1623}
1624
1625void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1626 MVT::ValueType VT2) {
1627 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1628 E = VTList.end(); I != E; ++I) {
1629 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1630 N->setValueTypes(&(*I)[0], 2);
1631 return;
1632 }
1633 }
1634 std::vector<MVT::ValueType> V;
1635 V.push_back(VT1);
1636 V.push_back(VT2);
1637 VTList.push_front(V);
1638 N->setValueTypes(&(*VTList.begin())[0], 2);
1639}
1640
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001641/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1642/// specified operands. If the resultant node already exists in the DAG,
1643/// this does not modify the specified node, instead it returns the node that
1644/// already exists. If the resultant node does not exist in the DAG, the
1645/// input node is returned. As a degenerate case, if you specify the same
1646/// input operands as the node already has, the input node is returned.
1647SDOperand SelectionDAG::
1648UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1649 SDNode *N = InN.Val;
1650 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1651
1652 // Check to see if there is no change.
1653 if (Op == N->getOperand(0)) return InN;
1654
1655 // See if the modified node already exists.
1656 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1657 if (NewSlot && *NewSlot)
1658 return SDOperand(*NewSlot, InN.ResNo);
1659
1660 // Nope it doesn't. Remove the node from it's current place in the maps.
1661 if (NewSlot)
1662 RemoveNodeFromCSEMaps(N);
1663
1664 // Now we update the operands.
1665 N->OperandList[0].Val->removeUser(N);
1666 Op.Val->addUser(N);
1667 N->OperandList[0] = Op;
1668
1669 // If this gets put into a CSE map, add it.
1670 if (NewSlot) *NewSlot = N;
1671 return InN;
1672}
1673
1674SDOperand SelectionDAG::
1675UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1676 SDNode *N = InN.Val;
1677 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1678
1679 // Check to see if there is no change.
1680 bool AnyChange = false;
1681 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1682 return InN; // No operands changed, just return the input node.
1683
1684 // See if the modified node already exists.
1685 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1686 if (NewSlot && *NewSlot)
1687 return SDOperand(*NewSlot, InN.ResNo);
1688
1689 // Nope it doesn't. Remove the node from it's current place in the maps.
1690 if (NewSlot)
1691 RemoveNodeFromCSEMaps(N);
1692
1693 // Now we update the operands.
1694 if (N->OperandList[0] != Op1) {
1695 N->OperandList[0].Val->removeUser(N);
1696 Op1.Val->addUser(N);
1697 N->OperandList[0] = Op1;
1698 }
1699 if (N->OperandList[1] != Op2) {
1700 N->OperandList[1].Val->removeUser(N);
1701 Op2.Val->addUser(N);
1702 N->OperandList[1] = Op2;
1703 }
1704
1705 // If this gets put into a CSE map, add it.
1706 if (NewSlot) *NewSlot = N;
1707 return InN;
1708}
1709
1710SDOperand SelectionDAG::
1711UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1712 std::vector<SDOperand> Ops;
1713 Ops.push_back(Op1);
1714 Ops.push_back(Op2);
1715 Ops.push_back(Op3);
1716 return UpdateNodeOperands(N, Ops);
1717}
1718
1719SDOperand SelectionDAG::
1720UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1721 SDOperand Op3, SDOperand Op4) {
1722 std::vector<SDOperand> Ops;
1723 Ops.push_back(Op1);
1724 Ops.push_back(Op2);
1725 Ops.push_back(Op3);
1726 Ops.push_back(Op4);
1727 return UpdateNodeOperands(N, Ops);
1728}
1729
1730SDOperand SelectionDAG::
1731UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1732 SDNode *N = InN.Val;
1733 assert(N->getNumOperands() == Ops.size() &&
1734 "Update with wrong number of operands");
1735
1736 // Check to see if there is no change.
1737 unsigned NumOps = Ops.size();
1738 bool AnyChange = false;
1739 for (unsigned i = 0; i != NumOps; ++i) {
1740 if (Ops[i] != N->getOperand(i)) {
1741 AnyChange = true;
1742 break;
1743 }
1744 }
1745
1746 // No operands changed, just return the input node.
1747 if (!AnyChange) return InN;
1748
1749 // See if the modified node already exists.
1750 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1751 if (NewSlot && *NewSlot)
1752 return SDOperand(*NewSlot, InN.ResNo);
1753
1754 // Nope it doesn't. Remove the node from it's current place in the maps.
1755 if (NewSlot)
1756 RemoveNodeFromCSEMaps(N);
1757
1758 // Now we update the operands.
1759 for (unsigned i = 0; i != NumOps; ++i) {
1760 if (N->OperandList[i] != Ops[i]) {
1761 N->OperandList[i].Val->removeUser(N);
1762 Ops[i].Val->addUser(N);
1763 N->OperandList[i] = Ops[i];
1764 }
1765 }
1766
1767 // If this gets put into a CSE map, add it.
1768 if (NewSlot) *NewSlot = N;
1769 return InN;
1770}
1771
1772
1773
Chris Lattner149c58c2005-08-16 18:17:10 +00001774
1775/// SelectNodeTo - These are used for target selectors to *mutate* the
1776/// specified node to have the specified return type, Target opcode, and
1777/// operands. Note that target opcodes are stored as
1778/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001779///
1780/// Note that SelectNodeTo returns the resultant node. If there is already a
1781/// node of the specified opcode and operands, it returns that node instead of
1782/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001783SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1784 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001785 // If an identical node already exists, use it.
1786 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1787 if (ON) return SDOperand(ON, 0);
1788
Chris Lattner7651fa42005-08-24 23:00:29 +00001789 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001790
Chris Lattner7651fa42005-08-24 23:00:29 +00001791 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1792 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001793
1794 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001795 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001796}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001797
Chris Lattnereb19e402005-11-30 22:45:14 +00001798SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1799 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001800 // If an identical node already exists, use it.
1801 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1802 std::make_pair(Op1, VT))];
1803 if (ON) return SDOperand(ON, 0);
1804
Chris Lattner149c58c2005-08-16 18:17:10 +00001805 RemoveNodeFromCSEMaps(N);
1806 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1807 N->setValueTypes(VT);
1808 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001809
1810 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001811 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001812}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001813
Chris Lattnereb19e402005-11-30 22:45:14 +00001814SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1815 MVT::ValueType VT, SDOperand Op1,
1816 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001817 // If an identical node already exists, use it.
1818 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1819 std::make_pair(Op1, Op2))];
1820 if (ON) return SDOperand(ON, 0);
1821
Chris Lattner149c58c2005-08-16 18:17:10 +00001822 RemoveNodeFromCSEMaps(N);
1823 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1824 N->setValueTypes(VT);
1825 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001826
1827 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001828 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001829}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001830
Chris Lattnereb19e402005-11-30 22:45:14 +00001831SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1832 MVT::ValueType VT, SDOperand Op1,
1833 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001834 // If an identical node already exists, use it.
1835 std::vector<SDOperand> OpList;
1836 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1837 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1838 std::make_pair(VT, OpList))];
1839 if (ON) return SDOperand(ON, 0);
1840
Chris Lattner149c58c2005-08-16 18:17:10 +00001841 RemoveNodeFromCSEMaps(N);
1842 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1843 N->setValueTypes(VT);
1844 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001845
1846 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001847 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001848}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001849
Chris Lattnereb19e402005-11-30 22:45:14 +00001850SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1851 MVT::ValueType VT, SDOperand Op1,
1852 SDOperand Op2, SDOperand Op3,
1853 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001854 // If an identical node already exists, use it.
1855 std::vector<SDOperand> OpList;
1856 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1857 OpList.push_back(Op4);
1858 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1859 std::make_pair(VT, OpList))];
1860 if (ON) return SDOperand(ON, 0);
1861
Nate Begeman294a0a12005-08-18 07:30:15 +00001862 RemoveNodeFromCSEMaps(N);
1863 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1864 N->setValueTypes(VT);
1865 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001866
1867 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001868 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001869}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001870
Chris Lattnereb19e402005-11-30 22:45:14 +00001871SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1872 MVT::ValueType VT, SDOperand Op1,
1873 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1874 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001875 // If an identical node already exists, use it.
1876 std::vector<SDOperand> OpList;
1877 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1878 OpList.push_back(Op4); OpList.push_back(Op5);
1879 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1880 std::make_pair(VT, OpList))];
1881 if (ON) return SDOperand(ON, 0);
1882
Chris Lattner6b09a292005-08-21 18:49:33 +00001883 RemoveNodeFromCSEMaps(N);
1884 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1885 N->setValueTypes(VT);
1886 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001887
1888 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001889 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001890}
Chris Lattner149c58c2005-08-16 18:17:10 +00001891
Chris Lattnereb19e402005-11-30 22:45:14 +00001892SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1893 MVT::ValueType VT, SDOperand Op1,
1894 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1895 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001896 // If an identical node already exists, use it.
1897 std::vector<SDOperand> OpList;
1898 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1899 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1900 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1901 std::make_pair(VT, OpList))];
1902 if (ON) return SDOperand(ON, 0);
1903
Evan Cheng61ca74b2005-11-30 02:04:11 +00001904 RemoveNodeFromCSEMaps(N);
1905 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1906 N->setValueTypes(VT);
1907 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001908
1909 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001910 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001911}
1912
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001913SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1914 MVT::ValueType VT, SDOperand Op1,
1915 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1916 SDOperand Op5, SDOperand Op6,
1917 SDOperand Op7) {
1918 // If an identical node already exists, use it.
1919 std::vector<SDOperand> OpList;
1920 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1921 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1922 OpList.push_back(Op7);
1923 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1924 std::make_pair(VT, OpList))];
1925 if (ON) return SDOperand(ON, 0);
1926
1927 RemoveNodeFromCSEMaps(N);
1928 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1929 N->setValueTypes(VT);
1930 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1931
1932 ON = N; // Memoize the new node.
1933 return SDOperand(N, 0);
1934}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001935SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1936 MVT::ValueType VT, SDOperand Op1,
1937 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1938 SDOperand Op5, SDOperand Op6,
1939 SDOperand Op7, SDOperand Op8) {
1940 // If an identical node already exists, use it.
1941 std::vector<SDOperand> OpList;
1942 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1943 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1944 OpList.push_back(Op7); OpList.push_back(Op8);
1945 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1946 std::make_pair(VT, OpList))];
1947 if (ON) return SDOperand(ON, 0);
1948
1949 RemoveNodeFromCSEMaps(N);
1950 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1951 N->setValueTypes(VT);
1952 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1953
1954 ON = N; // Memoize the new node.
1955 return SDOperand(N, 0);
1956}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001957
Chris Lattnereb19e402005-11-30 22:45:14 +00001958SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1959 MVT::ValueType VT1, MVT::ValueType VT2,
1960 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001961 // If an identical node already exists, use it.
1962 std::vector<SDOperand> OpList;
1963 OpList.push_back(Op1); OpList.push_back(Op2);
1964 std::vector<MVT::ValueType> VTList;
1965 VTList.push_back(VT1); VTList.push_back(VT2);
1966 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1967 std::make_pair(VTList, OpList))];
1968 if (ON) return SDOperand(ON, 0);
1969
Chris Lattner0fb094f2005-11-19 01:44:53 +00001970 RemoveNodeFromCSEMaps(N);
1971 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1972 setNodeValueTypes(N, VT1, VT2);
1973 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001974
1975 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001976 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001977}
1978
Chris Lattnereb19e402005-11-30 22:45:14 +00001979SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1980 MVT::ValueType VT1, MVT::ValueType VT2,
1981 SDOperand Op1, SDOperand Op2,
1982 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001983 // If an identical node already exists, use it.
1984 std::vector<SDOperand> OpList;
1985 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1986 std::vector<MVT::ValueType> VTList;
1987 VTList.push_back(VT1); VTList.push_back(VT2);
1988 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1989 std::make_pair(VTList, OpList))];
1990 if (ON) return SDOperand(ON, 0);
1991
Chris Lattner0fb094f2005-11-19 01:44:53 +00001992 RemoveNodeFromCSEMaps(N);
1993 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1994 setNodeValueTypes(N, VT1, VT2);
1995 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001996
1997 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001998 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001999}
2000
Chris Lattnereb19e402005-11-30 22:45:14 +00002001SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2002 MVT::ValueType VT1, MVT::ValueType VT2,
2003 SDOperand Op1, SDOperand Op2,
2004 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002005 // If an identical node already exists, use it.
2006 std::vector<SDOperand> OpList;
2007 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2008 OpList.push_back(Op4);
2009 std::vector<MVT::ValueType> VTList;
2010 VTList.push_back(VT1); VTList.push_back(VT2);
2011 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2012 std::make_pair(VTList, OpList))];
2013 if (ON) return SDOperand(ON, 0);
2014
Chris Lattner0fb094f2005-11-19 01:44:53 +00002015 RemoveNodeFromCSEMaps(N);
2016 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2017 setNodeValueTypes(N, VT1, VT2);
2018 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002019
2020 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002021 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002022}
2023
Chris Lattnereb19e402005-11-30 22:45:14 +00002024SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2025 MVT::ValueType VT1, MVT::ValueType VT2,
2026 SDOperand Op1, SDOperand Op2,
2027 SDOperand Op3, SDOperand Op4,
2028 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002029 // If an identical node already exists, use it.
2030 std::vector<SDOperand> OpList;
2031 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2032 OpList.push_back(Op4); OpList.push_back(Op5);
2033 std::vector<MVT::ValueType> VTList;
2034 VTList.push_back(VT1); VTList.push_back(VT2);
2035 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2036 std::make_pair(VTList, OpList))];
2037 if (ON) return SDOperand(ON, 0);
2038
Chris Lattner0fb094f2005-11-19 01:44:53 +00002039 RemoveNodeFromCSEMaps(N);
2040 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2041 setNodeValueTypes(N, VT1, VT2);
2042 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002043
2044 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002045 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002046}
2047
2048// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002049/// This can cause recursive merging of nodes in the DAG.
2050///
Chris Lattner8b52f212005-08-26 18:36:28 +00002051/// This version assumes From/To have a single result value.
2052///
Chris Lattner1e111c72005-09-07 05:37:01 +00002053void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2054 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002055 SDNode *From = FromN.Val, *To = ToN.Val;
2056 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2057 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002058 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002059
Chris Lattner8b8749f2005-08-17 19:00:20 +00002060 while (!From->use_empty()) {
2061 // Process users until they are all gone.
2062 SDNode *U = *From->use_begin();
2063
2064 // This node is about to morph, remove its old self from the CSE maps.
2065 RemoveNodeFromCSEMaps(U);
2066
Chris Lattner65113b22005-11-08 22:07:03 +00002067 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2068 I != E; ++I)
2069 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002070 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002071 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002072 To->addUser(U);
2073 }
2074
2075 // Now that we have modified U, add it back to the CSE maps. If it already
2076 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002077 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2078 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002079 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002080 if (Deleted) Deleted->push_back(U);
2081 DeleteNodeNotInCSEMaps(U);
2082 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002083 }
2084}
2085
2086/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2087/// This can cause recursive merging of nodes in the DAG.
2088///
2089/// This version assumes From/To have matching types and numbers of result
2090/// values.
2091///
Chris Lattner1e111c72005-09-07 05:37:01 +00002092void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2093 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002094 assert(From != To && "Cannot replace uses of with self");
2095 assert(From->getNumValues() == To->getNumValues() &&
2096 "Cannot use this version of ReplaceAllUsesWith!");
2097 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002098 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002099 return;
2100 }
2101
2102 while (!From->use_empty()) {
2103 // Process users until they are all gone.
2104 SDNode *U = *From->use_begin();
2105
2106 // This node is about to morph, remove its old self from the CSE maps.
2107 RemoveNodeFromCSEMaps(U);
2108
Chris Lattner65113b22005-11-08 22:07:03 +00002109 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2110 I != E; ++I)
2111 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002112 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002113 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002114 To->addUser(U);
2115 }
2116
2117 // Now that we have modified U, add it back to the CSE maps. If it already
2118 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002119 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2120 ReplaceAllUsesWith(U, Existing, Deleted);
2121 // U is now dead.
2122 if (Deleted) Deleted->push_back(U);
2123 DeleteNodeNotInCSEMaps(U);
2124 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002125 }
2126}
2127
Chris Lattner8b52f212005-08-26 18:36:28 +00002128/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2129/// This can cause recursive merging of nodes in the DAG.
2130///
2131/// This version can replace From with any result values. To must match the
2132/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002133void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002134 const std::vector<SDOperand> &To,
2135 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002136 assert(From->getNumValues() == To.size() &&
2137 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002138 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002139 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002140 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002141 return;
2142 }
2143
2144 while (!From->use_empty()) {
2145 // Process users until they are all gone.
2146 SDNode *U = *From->use_begin();
2147
2148 // This node is about to morph, remove its old self from the CSE maps.
2149 RemoveNodeFromCSEMaps(U);
2150
Chris Lattner65113b22005-11-08 22:07:03 +00002151 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2152 I != E; ++I)
2153 if (I->Val == From) {
2154 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002155 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002156 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002157 ToOp.Val->addUser(U);
2158 }
2159
2160 // Now that we have modified U, add it back to the CSE maps. If it already
2161 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002162 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2163 ReplaceAllUsesWith(U, Existing, Deleted);
2164 // U is now dead.
2165 if (Deleted) Deleted->push_back(U);
2166 DeleteNodeNotInCSEMaps(U);
2167 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002168 }
2169}
2170
2171
Jim Laskey58b968b2005-08-17 20:08:02 +00002172//===----------------------------------------------------------------------===//
2173// SDNode Class
2174//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002175
Chris Lattnera3255112005-11-08 23:30:28 +00002176
2177/// getValueTypeList - Return a pointer to the specified value type.
2178///
2179MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2180 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2181 VTs[VT] = VT;
2182 return &VTs[VT];
2183}
2184
Chris Lattner5c884562005-01-12 18:37:47 +00002185/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2186/// indicated value. This method ignores uses of other values defined by this
2187/// operation.
2188bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2189 assert(Value < getNumValues() && "Bad value!");
2190
2191 // If there is only one value, this is easy.
2192 if (getNumValues() == 1)
2193 return use_size() == NUses;
2194 if (Uses.size() < NUses) return false;
2195
2196 SDOperand TheValue(this, Value);
2197
2198 std::set<SDNode*> UsersHandled;
2199
2200 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2201 UI != E; ++UI) {
2202 SDNode *User = *UI;
2203 if (User->getNumOperands() == 1 ||
2204 UsersHandled.insert(User).second) // First time we've seen this?
2205 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2206 if (User->getOperand(i) == TheValue) {
2207 if (NUses == 0)
2208 return false; // too many uses
2209 --NUses;
2210 }
2211 }
2212
2213 // Found exactly the right number of uses?
2214 return NUses == 0;
2215}
2216
2217
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002218const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002219 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002220 default:
2221 if (getOpcode() < ISD::BUILTIN_OP_END)
2222 return "<<Unknown DAG Node>>";
2223 else {
Evan Cheng72261582005-12-20 06:22:03 +00002224 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002225 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002226 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2227 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002228
Evan Cheng72261582005-12-20 06:22:03 +00002229 TargetLowering &TLI = G->getTargetLoweringInfo();
2230 const char *Name =
2231 TLI.getTargetNodeName(getOpcode());
2232 if (Name) return Name;
2233 }
2234
2235 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002236 }
2237
Andrew Lenharth95762122005-03-31 21:24:06 +00002238 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002239 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002240 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002241 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002242 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002243 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002244 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002245 case ISD::AssertSext: return "AssertSext";
2246 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002247 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002248 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002249 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002250 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002251 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002252 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002253 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002254 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002255 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002256 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002257 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002258 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002259 case ISD::ConstantPool: return "ConstantPool";
2260 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002261 case ISD::CopyToReg: return "CopyToReg";
2262 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002263 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002264 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002265 case ISD::INLINEASM: return "inlineasm";
2266
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002267 // Unary operators
2268 case ISD::FABS: return "fabs";
2269 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002270 case ISD::FSQRT: return "fsqrt";
2271 case ISD::FSIN: return "fsin";
2272 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002273
2274 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002275 case ISD::ADD: return "add";
2276 case ISD::SUB: return "sub";
2277 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002278 case ISD::MULHU: return "mulhu";
2279 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002280 case ISD::SDIV: return "sdiv";
2281 case ISD::UDIV: return "udiv";
2282 case ISD::SREM: return "srem";
2283 case ISD::UREM: return "urem";
2284 case ISD::AND: return "and";
2285 case ISD::OR: return "or";
2286 case ISD::XOR: return "xor";
2287 case ISD::SHL: return "shl";
2288 case ISD::SRA: return "sra";
2289 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002290 case ISD::ROTL: return "rotl";
2291 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002292 case ISD::FADD: return "fadd";
2293 case ISD::FSUB: return "fsub";
2294 case ISD::FMUL: return "fmul";
2295 case ISD::FDIV: return "fdiv";
2296 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002297 case ISD::VADD: return "vadd";
2298 case ISD::VSUB: return "vsub";
2299 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002300
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002301 case ISD::SETCC: return "setcc";
2302 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002303 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002304 case ISD::ADD_PARTS: return "add_parts";
2305 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002306 case ISD::SHL_PARTS: return "shl_parts";
2307 case ISD::SRA_PARTS: return "sra_parts";
2308 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002309
Chris Lattner7f644642005-04-28 21:44:03 +00002310 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002311 case ISD::SIGN_EXTEND: return "sign_extend";
2312 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002313 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002314 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002315 case ISD::TRUNCATE: return "truncate";
2316 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002317 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002318 case ISD::FP_EXTEND: return "fp_extend";
2319
2320 case ISD::SINT_TO_FP: return "sint_to_fp";
2321 case ISD::UINT_TO_FP: return "uint_to_fp";
2322 case ISD::FP_TO_SINT: return "fp_to_sint";
2323 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002324 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002325
2326 // Control flow instructions
2327 case ISD::BR: return "br";
2328 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002329 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002330 case ISD::BR_CC: return "br_cc";
2331 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002332 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002333 case ISD::CALLSEQ_START: return "callseq_start";
2334 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002335
2336 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002337 case ISD::LOAD: return "load";
2338 case ISD::STORE: return "store";
2339 case ISD::VLOAD: return "vload";
2340 case ISD::EXTLOAD: return "extload";
2341 case ISD::SEXTLOAD: return "sextload";
2342 case ISD::ZEXTLOAD: return "zextload";
2343 case ISD::TRUNCSTORE: return "truncstore";
2344 case ISD::VAARG: return "vaarg";
2345 case ISD::VACOPY: return "vacopy";
2346 case ISD::VAEND: return "vaend";
2347 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002348 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002349 case ISD::EXTRACT_ELEMENT: return "extract_element";
2350 case ISD::BUILD_PAIR: return "build_pair";
2351 case ISD::STACKSAVE: return "stacksave";
2352 case ISD::STACKRESTORE: return "stackrestore";
2353
2354 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002355 case ISD::MEMSET: return "memset";
2356 case ISD::MEMCPY: return "memcpy";
2357 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002358
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002359 // Bit manipulation
2360 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002361 case ISD::CTPOP: return "ctpop";
2362 case ISD::CTTZ: return "cttz";
2363 case ISD::CTLZ: return "ctlz";
2364
2365 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002366 case ISD::READPORT: return "readport";
2367 case ISD::WRITEPORT: return "writeport";
2368 case ISD::READIO: return "readio";
2369 case ISD::WRITEIO: return "writeio";
2370
Chris Lattner36ce6912005-11-29 06:21:05 +00002371 // Debug info
2372 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002373 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002374 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002375
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002376 case ISD::CONDCODE:
2377 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002378 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002379 case ISD::SETOEQ: return "setoeq";
2380 case ISD::SETOGT: return "setogt";
2381 case ISD::SETOGE: return "setoge";
2382 case ISD::SETOLT: return "setolt";
2383 case ISD::SETOLE: return "setole";
2384 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002385
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002386 case ISD::SETO: return "seto";
2387 case ISD::SETUO: return "setuo";
2388 case ISD::SETUEQ: return "setue";
2389 case ISD::SETUGT: return "setugt";
2390 case ISD::SETUGE: return "setuge";
2391 case ISD::SETULT: return "setult";
2392 case ISD::SETULE: return "setule";
2393 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002394
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002395 case ISD::SETEQ: return "seteq";
2396 case ISD::SETGT: return "setgt";
2397 case ISD::SETGE: return "setge";
2398 case ISD::SETLT: return "setlt";
2399 case ISD::SETLE: return "setle";
2400 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002401 }
2402 }
2403}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002404
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002405void SDNode::dump() const { dump(0); }
2406void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002407 std::cerr << (void*)this << ": ";
2408
2409 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2410 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002411 if (getValueType(i) == MVT::Other)
2412 std::cerr << "ch";
2413 else
2414 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002415 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002416 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002417
2418 std::cerr << " ";
2419 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2420 if (i) std::cerr << ", ";
2421 std::cerr << (void*)getOperand(i).Val;
2422 if (unsigned RN = getOperand(i).ResNo)
2423 std::cerr << ":" << RN;
2424 }
2425
2426 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2427 std::cerr << "<" << CSDN->getValue() << ">";
2428 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2429 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002430 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002431 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002432 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002433 std::cerr << "<";
2434 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002435 if (offset > 0)
2436 std::cerr << " + " << offset;
2437 else
2438 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002439 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002440 std::cerr << "<" << FIDN->getIndex() << ">";
2441 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002442 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002443 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002444 std::cerr << "<";
2445 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2446 if (LBB)
2447 std::cerr << LBB->getName() << " ";
2448 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002449 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002450 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002451 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2452 } else {
2453 std::cerr << " #" << R->getReg();
2454 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002455 } else if (const ExternalSymbolSDNode *ES =
2456 dyn_cast<ExternalSymbolSDNode>(this)) {
2457 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002458 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2459 if (M->getValue())
2460 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2461 else
2462 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002463 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2464 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002465 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002466}
2467
Chris Lattnerde202b32005-11-09 23:47:37 +00002468static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002469 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2470 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002471 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002472 else
2473 std::cerr << "\n" << std::string(indent+2, ' ')
2474 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002475
Chris Lattnerea946cd2005-01-09 20:38:33 +00002476
2477 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002478 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002479}
2480
Chris Lattnerc3aae252005-01-07 07:46:32 +00002481void SelectionDAG::dump() const {
2482 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002483 std::vector<const SDNode*> Nodes;
2484 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2485 I != E; ++I)
2486 Nodes.push_back(I);
2487
Chris Lattner49d24712005-01-09 20:26:36 +00002488 std::sort(Nodes.begin(), Nodes.end());
2489
2490 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002491 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002492 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002493 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002494
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002495 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002496
Chris Lattnerc3aae252005-01-07 07:46:32 +00002497 std::cerr << "\n\n";
2498}
2499