blob: 59449625e6667e2e074e990d86deb4771123e31c [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"
Chris Lattner012f2412006-02-17 21:58:01 +000024#include "llvm/ADT/SetVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000025#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000027#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000028#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000029#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner5cdcc582005-01-09 20:52:51 +000032static bool isCommutativeBinOp(unsigned Opcode) {
33 switch (Opcode) {
34 case ISD::ADD:
35 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000036 case ISD::MULHU:
37 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000038 case ISD::FADD:
39 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000040 case ISD::AND:
41 case ISD::OR:
42 case ISD::XOR: return true;
43 default: return false; // FIXME: Need commutative info for user ops!
44 }
45}
46
Chris Lattner5cdcc582005-01-09 20:52:51 +000047// isInvertibleForFree - Return true if there is no cost to emitting the logical
48// inverse of this node.
49static bool isInvertibleForFree(SDOperand N) {
50 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000051 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000052 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000053 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000054}
55
Jim Laskey58b968b2005-08-17 20:08:02 +000056//===----------------------------------------------------------------------===//
57// ConstantFPSDNode Class
58//===----------------------------------------------------------------------===//
59
60/// isExactlyValue - We don't rely on operator== working on double values, as
61/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
62/// As such, this method can be used to do an exact bit-for-bit comparison of
63/// two floating point values.
64bool ConstantFPSDNode::isExactlyValue(double V) const {
65 return DoubleToBits(V) == DoubleToBits(Value);
66}
67
68//===----------------------------------------------------------------------===//
69// ISD Class
70//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000071
Chris Lattnerc3aae252005-01-07 07:46:32 +000072/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
73/// when given the operation for (X op Y).
74ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
75 // To perform this operation, we just need to swap the L and G bits of the
76 // operation.
77 unsigned OldL = (Operation >> 2) & 1;
78 unsigned OldG = (Operation >> 1) & 1;
79 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
80 (OldL << 1) | // New G bit
81 (OldG << 2)); // New L bit.
82}
83
84/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
85/// 'op' is a valid SetCC operation.
86ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
87 unsigned Operation = Op;
88 if (isInteger)
89 Operation ^= 7; // Flip L, G, E bits, but not U.
90 else
91 Operation ^= 15; // Flip all of the condition bits.
92 if (Operation > ISD::SETTRUE2)
93 Operation &= ~8; // Don't let N and U bits get set.
94 return ISD::CondCode(Operation);
95}
96
97
98/// isSignedOp - For an integer comparison, return 1 if the comparison is a
99/// signed operation and 2 if the result is an unsigned comparison. Return zero
100/// if the operation does not depend on the sign of the input (setne and seteq).
101static int isSignedOp(ISD::CondCode Opcode) {
102 switch (Opcode) {
103 default: assert(0 && "Illegal integer setcc operation!");
104 case ISD::SETEQ:
105 case ISD::SETNE: return 0;
106 case ISD::SETLT:
107 case ISD::SETLE:
108 case ISD::SETGT:
109 case ISD::SETGE: return 1;
110 case ISD::SETULT:
111 case ISD::SETULE:
112 case ISD::SETUGT:
113 case ISD::SETUGE: return 2;
114 }
115}
116
117/// getSetCCOrOperation - Return the result of a logical OR between different
118/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
119/// returns SETCC_INVALID if it is not possible to represent the resultant
120/// comparison.
121ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
122 bool isInteger) {
123 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
124 // Cannot fold a signed integer setcc with an unsigned integer setcc.
125 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000126
Chris Lattnerc3aae252005-01-07 07:46:32 +0000127 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000128
Chris Lattnerc3aae252005-01-07 07:46:32 +0000129 // If the N and U bits get set then the resultant comparison DOES suddenly
130 // care about orderedness, and is true when ordered.
131 if (Op > ISD::SETTRUE2)
132 Op &= ~16; // Clear the N bit.
133 return ISD::CondCode(Op);
134}
135
136/// getSetCCAndOperation - Return the result of a logical AND between different
137/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
138/// function returns zero if it is not possible to represent the resultant
139/// comparison.
140ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
141 bool isInteger) {
142 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
143 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000144 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000145
146 // Combine all of the condition bits.
147 return ISD::CondCode(Op1 & Op2);
148}
149
Chris Lattnerb48da392005-01-23 04:39:44 +0000150const TargetMachine &SelectionDAG::getTarget() const {
151 return TLI.getTargetMachine();
152}
153
Jim Laskey58b968b2005-08-17 20:08:02 +0000154//===----------------------------------------------------------------------===//
155// SelectionDAG Class
156//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000157
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000158/// RemoveDeadNodes - This method deletes all unreachable nodes in the
159/// SelectionDAG, including nodes (like loads) that have uses of their token
160/// chain but no other uses and no side effect. If a node is passed in as an
161/// argument, it is used as the seed for node deletion.
162void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000163 // Create a dummy node (which is not added to allnodes), that adds a reference
164 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000165 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000166
Chris Lattnerf469cb62005-11-08 18:52:27 +0000167 bool MadeChange = false;
168
Chris Lattner8b8749f2005-08-17 19:00:20 +0000169 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000170 if (N && N->use_empty()) {
171 DestroyDeadNode(N);
172 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173 }
174
Chris Lattnerde202b32005-11-09 23:47:37 +0000175 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
176 if (I->use_empty() && I->getOpcode() != 65535) {
177 // Node is dead, recursively delete newly dead uses.
178 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000179 MadeChange = true;
180 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000181
182 // Walk the nodes list, removing the nodes we've marked as dead.
183 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000184 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
185 SDNode *N = I++;
186 if (N->use_empty())
187 AllNodes.erase(N);
188 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000189 }
190
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000191 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000192 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000193}
194
Chris Lattnerf469cb62005-11-08 18:52:27 +0000195/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
196/// graph. If it is the last user of any of its operands, recursively process
197/// them the same way.
198///
199void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000200 // Okay, we really are going to delete this node. First take this out of the
201 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000202 RemoveNodeFromCSEMaps(N);
203
204 // Next, brutally remove the operand list. This is safe to do, as there are
205 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000206 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
207 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000208 O->removeUser(N);
209
210 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000211 if (O->use_empty())
212 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000213 }
Chris Lattner65113b22005-11-08 22:07:03 +0000214 delete[] N->OperandList;
215 N->OperandList = 0;
216 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000217
218 // Mark the node as dead.
219 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000220}
221
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000222void SelectionDAG::DeleteNode(SDNode *N) {
223 assert(N->use_empty() && "Cannot delete a node that is not dead!");
224
225 // First take this out of the appropriate CSE map.
226 RemoveNodeFromCSEMaps(N);
227
Chris Lattner1e111c72005-09-07 05:37:01 +0000228 // Finally, remove uses due to operands of this node, remove from the
229 // AllNodes list, and delete the node.
230 DeleteNodeNotInCSEMaps(N);
231}
232
233void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
234
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000235 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000236 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000237
238 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000239 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
240 I->Val->removeUser(N);
241 delete[] N->OperandList;
242 N->OperandList = 0;
243 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000244
245 delete N;
246}
247
Chris Lattner149c58c2005-08-16 18:17:10 +0000248/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
249/// correspond to it. This is useful when we're about to delete or repurpose
250/// the node. We don't want future request for structurally identical nodes
251/// to return N anymore.
252void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000253 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000255 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000256 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000257 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
258 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000259 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000260 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000261 Erased = TargetConstants.erase(std::make_pair(
262 cast<ConstantSDNode>(N)->getValue(),
263 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000264 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000265 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000266 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000267 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000268 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000269 }
Chris Lattner3181a772006-01-29 06:26:56 +0000270 case ISD::TargetConstantFP: {
271 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
272 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
273 break;
274 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000275 case ISD::STRING:
276 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
277 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000278 case ISD::CONDCODE:
279 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
280 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000281 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000282 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
283 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000284 case ISD::GlobalAddress: {
285 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
286 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
287 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000288 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000289 }
290 case ISD::TargetGlobalAddress: {
291 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
292 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
293 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000294 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000295 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000296 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000297 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000299 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000300 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000301 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000302 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000303 Erased = ConstantPoolIndices.
304 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
305 cast<ConstantPoolSDNode>(N)->getAlignment()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000307 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000308 Erased = TargetConstantPoolIndices.
309 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
310 cast<ConstantPoolSDNode>(N)->getAlignment()));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000311 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000313 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000314 break;
315 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000316 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000317 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000318 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000319 Erased =
320 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000321 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 Lattner70814bc2006-01-29 07:58:15 +0000377 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000378 N->dump();
379 assert(0 && "Node is not in map!");
380 }
381#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000382}
383
Chris Lattner8b8749f2005-08-17 19:00:20 +0000384/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
385/// has been taken out and modified in some way. If the specified node already
386/// exists in the CSE maps, do not modify the maps, but return the existing node
387/// instead. If it doesn't exist, add it and return null.
388///
389SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
390 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000391 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000392 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000393
Chris Lattner9f8cc692005-12-19 22:21:21 +0000394 // Check that remaining values produced are not flags.
395 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
396 if (N->getValueType(i) == MVT::Flag)
397 return 0; // Never CSE anything that produces a flag.
398
Chris Lattnerfe14b342005-12-01 23:14:50 +0000399 if (N->getNumValues() == 1) {
400 if (N->getNumOperands() == 1) {
401 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
402 std::make_pair(N->getOperand(0),
403 N->getValueType(0)))];
404 if (U) return U;
405 U = N;
406 } else if (N->getNumOperands() == 2) {
407 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
408 std::make_pair(N->getOperand(0),
409 N->getOperand(1)))];
410 if (B) return B;
411 B = N;
412 } else {
413 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
414 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000415 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000416 if (ORN) return ORN;
417 ORN = N;
418 }
419 } else {
420 if (N->getOpcode() == ISD::LOAD) {
421 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
422 std::make_pair(N->getOperand(0),
423 N->getValueType(0)))];
424 if (L) return L;
425 L = N;
426 } else {
427 // Remove the node from the ArbitraryNodes map.
428 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
429 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
430 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
431 std::make_pair(RV, Ops))];
432 if (AN) return AN;
433 AN = N;
434 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000435 }
436 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000437}
438
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000439/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
440/// were replaced with those specified. If this node is never memoized,
441/// return null, otherwise return a pointer to the slot it would take. If a
442/// node already exists with these operands, the slot will be non-null.
443SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000444 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000445 return 0; // Never add these nodes.
446
447 // Check that remaining values produced are not flags.
448 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
449 if (N->getValueType(i) == MVT::Flag)
450 return 0; // Never CSE anything that produces a flag.
451
452 if (N->getNumValues() == 1) {
453 return &UnaryOps[std::make_pair(N->getOpcode(),
454 std::make_pair(Op, N->getValueType(0)))];
455 } else {
456 // Remove the node from the ArbitraryNodes map.
457 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
458 std::vector<SDOperand> Ops;
459 Ops.push_back(Op);
460 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
461 std::make_pair(RV, Ops))];
462 }
463 return 0;
464}
465
466/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
467/// were replaced with those specified. If this node is never memoized,
468/// return null, otherwise return a pointer to the slot it would take. If a
469/// node already exists with these operands, the slot will be non-null.
470SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
471 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000472 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000473 return 0; // Never add these nodes.
474
475 // Check that remaining values produced are not flags.
476 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
477 if (N->getValueType(i) == MVT::Flag)
478 return 0; // Never CSE anything that produces a flag.
479
480 if (N->getNumValues() == 1) {
481 return &BinaryOps[std::make_pair(N->getOpcode(),
482 std::make_pair(Op1, Op2))];
483 } else {
484 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
485 std::vector<SDOperand> Ops;
486 Ops.push_back(Op1);
487 Ops.push_back(Op2);
488 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
489 std::make_pair(RV, Ops))];
490 }
491 return 0;
492}
493
494
495/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
496/// were replaced with those specified. If this node is never memoized,
497/// return null, otherwise return a pointer to the slot it would take. If a
498/// node already exists with these operands, the slot will be non-null.
499SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
500 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000501 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000502 return 0; // Never add these nodes.
503
504 // Check that remaining values produced are not flags.
505 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
506 if (N->getValueType(i) == MVT::Flag)
507 return 0; // Never CSE anything that produces a flag.
508
509 if (N->getNumValues() == 1) {
510 if (N->getNumOperands() == 1) {
511 return &UnaryOps[std::make_pair(N->getOpcode(),
512 std::make_pair(Ops[0],
513 N->getValueType(0)))];
514 } else if (N->getNumOperands() == 2) {
515 return &BinaryOps[std::make_pair(N->getOpcode(),
516 std::make_pair(Ops[0], Ops[1]))];
517 } else {
518 return &OneResultNodes[std::make_pair(N->getOpcode(),
519 std::make_pair(N->getValueType(0),
520 Ops))];
521 }
522 } else {
523 if (N->getOpcode() == ISD::LOAD) {
524 return &Loads[std::make_pair(Ops[1],
525 std::make_pair(Ops[0], N->getValueType(0)))];
526 } else {
527 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
528 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
529 std::make_pair(RV, Ops))];
530 }
531 }
532 return 0;
533}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000534
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000535
Chris Lattner78ec3112003-08-11 14:57:33 +0000536SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000537 while (!AllNodes.empty()) {
538 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000539 delete [] N->OperandList;
540 N->OperandList = 0;
541 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000542 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000543 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000544}
545
Chris Lattner0f2287b2005-04-13 02:38:18 +0000546SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000547 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000548 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000549 return getNode(ISD::AND, Op.getValueType(), Op,
550 getConstant(Imm, Op.getValueType()));
551}
552
Chris Lattnerc3aae252005-01-07 07:46:32 +0000553SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
554 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
555 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000556 if (VT != MVT::i64)
557 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000558
Chris Lattnerc3aae252005-01-07 07:46:32 +0000559 SDNode *&N = Constants[std::make_pair(Val, VT)];
560 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000561 N = new ConstantSDNode(false, Val, VT);
562 AllNodes.push_back(N);
563 return SDOperand(N, 0);
564}
565
Chris Lattner36ce6912005-11-29 06:21:05 +0000566SDOperand SelectionDAG::getString(const std::string &Val) {
567 StringSDNode *&N = StringNodes[Val];
568 if (!N) {
569 N = new StringSDNode(Val);
570 AllNodes.push_back(N);
571 }
572 return SDOperand(N, 0);
573}
574
Chris Lattner37bfbb42005-08-17 00:34:06 +0000575SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
576 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
577 // Mask out any bits that are not valid for this constant.
578 if (VT != MVT::i64)
579 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
580
581 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
582 if (N) return SDOperand(N, 0);
583 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000584 AllNodes.push_back(N);
585 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000586}
587
Chris Lattnerc3aae252005-01-07 07:46:32 +0000588SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
589 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
590 if (VT == MVT::f32)
591 Val = (float)Val; // Mask out extra precision.
592
Chris Lattnerd8658612005-02-17 20:17:32 +0000593 // Do the map lookup using the actual bit pattern for the floating point
594 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
595 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000596 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000597 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000598 N = new ConstantFPSDNode(false, Val, VT);
599 AllNodes.push_back(N);
600 return SDOperand(N, 0);
601}
602
603SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
604 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
605 if (VT == MVT::f32)
606 Val = (float)Val; // Mask out extra precision.
607
608 // Do the map lookup using the actual bit pattern for the floating point
609 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
610 // we don't have issues with SNANs.
611 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
612 if (N) return SDOperand(N, 0);
613 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000614 AllNodes.push_back(N);
615 return SDOperand(N, 0);
616}
617
Chris Lattnerc3aae252005-01-07 07:46:32 +0000618SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000619 MVT::ValueType VT, int offset) {
620 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000621 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000622 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000623 AllNodes.push_back(N);
624 return SDOperand(N, 0);
625}
626
627SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000628 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000629 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000630 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000631 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000632 AllNodes.push_back(N);
633 return SDOperand(N, 0);
634}
635
636SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
637 SDNode *&N = FrameIndices[FI];
638 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000639 N = new FrameIndexSDNode(FI, VT, false);
640 AllNodes.push_back(N);
641 return SDOperand(N, 0);
642}
643
644SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
645 SDNode *&N = TargetFrameIndices[FI];
646 if (N) return SDOperand(N, 0);
647 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000648 AllNodes.push_back(N);
649 return SDOperand(N, 0);
650}
651
Evan Chengb8973bd2006-01-31 22:23:14 +0000652SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
653 unsigned Alignment) {
654 SDNode *&N = ConstantPoolIndices[std::make_pair(C, Alignment)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000655 if (N) return SDOperand(N, 0);
Evan Chengb8973bd2006-01-31 22:23:14 +0000656 N = new ConstantPoolSDNode(C, VT, Alignment, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000657 AllNodes.push_back(N);
658 return SDOperand(N, 0);
659}
660
Evan Chengb8973bd2006-01-31 22:23:14 +0000661SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
662 unsigned Alignment) {
663 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C, Alignment)];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000664 if (N) return SDOperand(N, 0);
Evan Chengb8973bd2006-01-31 22:23:14 +0000665 N = new ConstantPoolSDNode(C, VT, Alignment, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000666 AllNodes.push_back(N);
667 return SDOperand(N, 0);
668}
669
670SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
671 SDNode *&N = BBNodes[MBB];
672 if (N) return SDOperand(N, 0);
673 N = new BasicBlockSDNode(MBB);
674 AllNodes.push_back(N);
675 return SDOperand(N, 0);
676}
677
Chris Lattner15e4b012005-07-10 00:07:11 +0000678SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
679 if ((unsigned)VT >= ValueTypeNodes.size())
680 ValueTypeNodes.resize(VT+1);
681 if (ValueTypeNodes[VT] == 0) {
682 ValueTypeNodes[VT] = new VTSDNode(VT);
683 AllNodes.push_back(ValueTypeNodes[VT]);
684 }
685
686 return SDOperand(ValueTypeNodes[VT], 0);
687}
688
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
690 SDNode *&N = ExternalSymbols[Sym];
691 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000692 N = new ExternalSymbolSDNode(false, Sym, VT);
693 AllNodes.push_back(N);
694 return SDOperand(N, 0);
695}
696
Chris Lattner809ec112006-01-28 10:09:25 +0000697SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
698 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000699 SDNode *&N = TargetExternalSymbols[Sym];
700 if (N) return SDOperand(N, 0);
701 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000702 AllNodes.push_back(N);
703 return SDOperand(N, 0);
704}
705
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000706SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
707 if ((unsigned)Cond >= CondCodeNodes.size())
708 CondCodeNodes.resize(Cond+1);
709
Chris Lattner079a27a2005-08-09 20:40:02 +0000710 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000711 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000712 AllNodes.push_back(CondCodeNodes[Cond]);
713 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000714 return SDOperand(CondCodeNodes[Cond], 0);
715}
716
Chris Lattner0fdd7682005-08-30 22:38:38 +0000717SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
718 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
719 if (!Reg) {
720 Reg = new RegisterSDNode(RegNo, VT);
721 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000722 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000723 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000724}
725
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000726SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
727 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000728 // These setcc operations always fold.
729 switch (Cond) {
730 default: break;
731 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000732 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000734 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000735 }
736
Chris Lattner67255a12005-04-07 18:14:58 +0000737 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
738 uint64_t C2 = N2C->getValue();
739 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
740 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000741
Chris Lattnerc3aae252005-01-07 07:46:32 +0000742 // Sign extend the operands if required
743 if (ISD::isSignedIntSetCC(Cond)) {
744 C1 = N1C->getSignExtended();
745 C2 = N2C->getSignExtended();
746 }
747
748 switch (Cond) {
749 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000750 case ISD::SETEQ: return getConstant(C1 == C2, VT);
751 case ISD::SETNE: return getConstant(C1 != C2, VT);
752 case ISD::SETULT: return getConstant(C1 < C2, VT);
753 case ISD::SETUGT: return getConstant(C1 > C2, VT);
754 case ISD::SETULE: return getConstant(C1 <= C2, VT);
755 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
756 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
757 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
758 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
759 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000760 }
Chris Lattner24673922005-04-07 18:58:54 +0000761 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000762 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000763 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
764 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
765
766 // If the comparison constant has bits in the upper part, the
767 // zero-extended value could never match.
768 if (C2 & (~0ULL << InSize)) {
769 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
770 switch (Cond) {
771 case ISD::SETUGT:
772 case ISD::SETUGE:
773 case ISD::SETEQ: return getConstant(0, VT);
774 case ISD::SETULT:
775 case ISD::SETULE:
776 case ISD::SETNE: return getConstant(1, VT);
777 case ISD::SETGT:
778 case ISD::SETGE:
779 // True if the sign bit of C2 is set.
780 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
781 case ISD::SETLT:
782 case ISD::SETLE:
783 // True if the sign bit of C2 isn't set.
784 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
785 default:
786 break;
787 }
788 }
789
790 // Otherwise, we can perform the comparison with the low bits.
791 switch (Cond) {
792 case ISD::SETEQ:
793 case ISD::SETNE:
794 case ISD::SETUGT:
795 case ISD::SETUGE:
796 case ISD::SETULT:
797 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000798 return getSetCC(VT, N1.getOperand(0),
799 getConstant(C2, N1.getOperand(0).getValueType()),
800 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000801 default:
802 break; // todo, be more careful with signed comparisons
803 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000804 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
805 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
806 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
807 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
808 MVT::ValueType ExtDstTy = N1.getValueType();
809 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000810
Chris Lattner7b2880c2005-08-24 22:44:39 +0000811 // If the extended part has any inconsistent bits, it cannot ever
812 // compare equal. In other words, they have to be all ones or all
813 // zeros.
814 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000815 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000816 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
817 return getConstant(Cond == ISD::SETNE, VT);
818
819 // Otherwise, make this a use of a zext.
820 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000821 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000822 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000823 }
824
Chris Lattner67255a12005-04-07 18:14:58 +0000825 uint64_t MinVal, MaxVal;
826 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
827 if (ISD::isSignedIntSetCC(Cond)) {
828 MinVal = 1ULL << (OperandBitSize-1);
829 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
830 MaxVal = ~0ULL >> (65-OperandBitSize);
831 else
832 MaxVal = 0;
833 } else {
834 MinVal = 0;
835 MaxVal = ~0ULL >> (64-OperandBitSize);
836 }
837
838 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
839 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
840 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
841 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000842 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
843 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000844 }
845
846 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
847 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
848 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000849 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
850 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000851 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000852
Nate Begeman72ea2812005-04-14 08:56:52 +0000853 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
854 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000855
Nate Begeman72ea2812005-04-14 08:56:52 +0000856 // Canonicalize setgt X, Min --> setne X, Min
857 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000858 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000859
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000860 // If we have setult X, 1, turn it into seteq X, 0
861 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000862 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
863 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000864 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000865 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000866 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
867 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000868
869 // If we have "setcc X, C1", check to see if we can shrink the immediate
870 // by changing cc.
871
872 // SETUGT X, SINTMAX -> SETLT X, 0
873 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
874 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000875 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000876
877 // FIXME: Implement the rest of these.
878
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000879
880 // Fold bit comparisons when we can.
881 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
882 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
883 if (ConstantSDNode *AndRHS =
884 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
885 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
886 // Perform the xform if the AND RHS is a single bit.
887 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
888 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000889 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000890 TLI.getShiftAmountTy()));
891 }
892 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
893 // (X & 8) == 8 --> (X & 8) >> 3
894 // Perform the xform if C2 is a single bit.
895 if ((C2 & (C2-1)) == 0) {
896 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000897 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000898 }
899 }
900 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000901 }
Chris Lattner67255a12005-04-07 18:14:58 +0000902 } else if (isa<ConstantSDNode>(N1.Val)) {
903 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000904 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000905 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000906
907 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
908 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
909 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000910
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 switch (Cond) {
912 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000913 case ISD::SETEQ: return getConstant(C1 == C2, VT);
914 case ISD::SETNE: return getConstant(C1 != C2, VT);
915 case ISD::SETLT: return getConstant(C1 < C2, VT);
916 case ISD::SETGT: return getConstant(C1 > C2, VT);
917 case ISD::SETLE: return getConstant(C1 <= C2, VT);
918 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000919 }
920 } else {
921 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000922 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 }
924
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000925 // Could not fold it.
926 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000927}
928
Chris Lattnerc3aae252005-01-07 07:46:32 +0000929/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000930///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000931SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000932 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
933 if (!N) {
934 N = new SDNode(Opcode, VT);
935 AllNodes.push_back(N);
936 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937 return SDOperand(N, 0);
938}
939
Chris Lattnerc3aae252005-01-07 07:46:32 +0000940SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
941 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000942 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000943 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000944 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
945 uint64_t Val = C->getValue();
946 switch (Opcode) {
947 default: break;
948 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000949 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
951 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000952 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
953 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000954 case ISD::BIT_CONVERT:
955 if (VT == MVT::f32) {
956 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
957 return getConstantFP(BitsToFloat(Val), VT);
958 } else if (VT == MVT::f64) {
959 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
960 return getConstantFP(BitsToDouble(Val), VT);
961 }
962 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000963 case ISD::BSWAP:
964 switch(VT) {
965 default: assert(0 && "Invalid bswap!"); break;
966 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
967 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
968 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
969 }
970 break;
971 case ISD::CTPOP:
972 switch(VT) {
973 default: assert(0 && "Invalid ctpop!"); break;
974 case MVT::i1: return getConstant(Val != 0, VT);
975 case MVT::i8:
976 Tmp1 = (unsigned)Val & 0xFF;
977 return getConstant(CountPopulation_32(Tmp1), VT);
978 case MVT::i16:
979 Tmp1 = (unsigned)Val & 0xFFFF;
980 return getConstant(CountPopulation_32(Tmp1), VT);
981 case MVT::i32:
982 return getConstant(CountPopulation_32((unsigned)Val), VT);
983 case MVT::i64:
984 return getConstant(CountPopulation_64(Val), VT);
985 }
986 case ISD::CTLZ:
987 switch(VT) {
988 default: assert(0 && "Invalid ctlz!"); break;
989 case MVT::i1: return getConstant(Val == 0, VT);
990 case MVT::i8:
991 Tmp1 = (unsigned)Val & 0xFF;
992 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
993 case MVT::i16:
994 Tmp1 = (unsigned)Val & 0xFFFF;
995 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
996 case MVT::i32:
997 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
998 case MVT::i64:
999 return getConstant(CountLeadingZeros_64(Val), VT);
1000 }
1001 case ISD::CTTZ:
1002 switch(VT) {
1003 default: assert(0 && "Invalid cttz!"); break;
1004 case MVT::i1: return getConstant(Val == 0, VT);
1005 case MVT::i8:
1006 Tmp1 = (unsigned)Val | 0x100;
1007 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1008 case MVT::i16:
1009 Tmp1 = (unsigned)Val | 0x10000;
1010 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1011 case MVT::i32:
1012 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1013 case MVT::i64:
1014 return getConstant(CountTrailingZeros_64(Val), VT);
1015 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016 }
1017 }
1018
Chris Lattner94683772005-12-23 05:30:37 +00001019 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1021 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001022 case ISD::FNEG:
1023 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001024 case ISD::FABS:
1025 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001026 case ISD::FP_ROUND:
1027 case ISD::FP_EXTEND:
1028 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001029 case ISD::FP_TO_SINT:
1030 return getConstant((int64_t)C->getValue(), VT);
1031 case ISD::FP_TO_UINT:
1032 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001033 case ISD::BIT_CONVERT:
1034 if (VT == MVT::i32) {
1035 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1036 return getConstant(FloatToBits(C->getValue()), VT);
1037 } else if (VT == MVT::i64) {
1038 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1039 return getConstant(DoubleToBits(C->getValue()), VT);
1040 }
1041 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001042 }
1043
1044 unsigned OpOpcode = Operand.Val->getOpcode();
1045 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001046 case ISD::TokenFactor:
1047 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048 case ISD::SIGN_EXTEND:
1049 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001050 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1052 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1053 break;
1054 case ISD::ZERO_EXTEND:
1055 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001056 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001057 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001058 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001060 case ISD::ANY_EXTEND:
1061 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001062 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001063 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1064 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1065 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1066 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067 case ISD::TRUNCATE:
1068 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001069 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 if (OpOpcode == ISD::TRUNCATE)
1071 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001072 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1073 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001074 // If the source is smaller than the dest, we still need an extend.
1075 if (Operand.Val->getOperand(0).getValueType() < VT)
1076 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1077 else if (Operand.Val->getOperand(0).getValueType() > VT)
1078 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1079 else
1080 return Operand.Val->getOperand(0);
1081 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 break;
Chris Lattner35481892005-12-23 00:16:34 +00001083 case ISD::BIT_CONVERT:
1084 // Basic sanity checking.
1085 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1086 "Cannot BIT_CONVERT between two different types!");
1087 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001088 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1089 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001090 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001091 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001092 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1093 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001094 Operand.Val->getOperand(0));
1095 if (OpOpcode == ISD::FNEG) // --X -> X
1096 return Operand.Val->getOperand(0);
1097 break;
1098 case ISD::FABS:
1099 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1100 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1101 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 }
1103
Chris Lattner43247a12005-08-25 19:12:10 +00001104 SDNode *N;
1105 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1106 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001107 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001108 E = N = new SDNode(Opcode, Operand);
1109 } else {
1110 N = new SDNode(Opcode, Operand);
1111 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001112 N->setValueTypes(VT);
1113 AllNodes.push_back(N);
1114 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001115}
1116
Chris Lattner36019aa2005-04-18 03:48:41 +00001117
1118
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1120 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001121#ifndef NDEBUG
1122 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001123 case ISD::TokenFactor:
1124 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1125 N2.getValueType() == MVT::Other && "Invalid token factor!");
1126 break;
Chris Lattner76365122005-01-16 02:23:22 +00001127 case ISD::AND:
1128 case ISD::OR:
1129 case ISD::XOR:
1130 case ISD::UDIV:
1131 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001132 case ISD::MULHU:
1133 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001134 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1135 // fall through
1136 case ISD::ADD:
1137 case ISD::SUB:
1138 case ISD::MUL:
1139 case ISD::SDIV:
1140 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001141 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1142 // fall through.
1143 case ISD::FADD:
1144 case ISD::FSUB:
1145 case ISD::FMUL:
1146 case ISD::FDIV:
1147 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001148 assert(N1.getValueType() == N2.getValueType() &&
1149 N1.getValueType() == VT && "Binary operator types must match!");
1150 break;
1151
1152 case ISD::SHL:
1153 case ISD::SRA:
1154 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001155 case ISD::ROTL:
1156 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001157 assert(VT == N1.getValueType() &&
1158 "Shift operators return type must be the same as their first arg");
1159 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001160 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001161 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001162 case ISD::FP_ROUND_INREG: {
1163 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1164 assert(VT == N1.getValueType() && "Not an inreg round!");
1165 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1166 "Cannot FP_ROUND_INREG integer types");
1167 assert(EVT <= VT && "Not rounding down!");
1168 break;
1169 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001170 case ISD::AssertSext:
1171 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001172 case ISD::SIGN_EXTEND_INREG: {
1173 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1174 assert(VT == N1.getValueType() && "Not an inreg extend!");
1175 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1176 "Cannot *_EXTEND_INREG FP types");
1177 assert(EVT <= VT && "Not extending!");
1178 }
1179
Chris Lattner76365122005-01-16 02:23:22 +00001180 default: break;
1181 }
1182#endif
1183
Chris Lattnerc3aae252005-01-07 07:46:32 +00001184 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1185 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1186 if (N1C) {
1187 if (N2C) {
1188 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1189 switch (Opcode) {
1190 case ISD::ADD: return getConstant(C1 + C2, VT);
1191 case ISD::SUB: return getConstant(C1 - C2, VT);
1192 case ISD::MUL: return getConstant(C1 * C2, VT);
1193 case ISD::UDIV:
1194 if (C2) return getConstant(C1 / C2, VT);
1195 break;
1196 case ISD::UREM :
1197 if (C2) return getConstant(C1 % C2, VT);
1198 break;
1199 case ISD::SDIV :
1200 if (C2) return getConstant(N1C->getSignExtended() /
1201 N2C->getSignExtended(), VT);
1202 break;
1203 case ISD::SREM :
1204 if (C2) return getConstant(N1C->getSignExtended() %
1205 N2C->getSignExtended(), VT);
1206 break;
1207 case ISD::AND : return getConstant(C1 & C2, VT);
1208 case ISD::OR : return getConstant(C1 | C2, VT);
1209 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001210 case ISD::SHL : return getConstant(C1 << C2, VT);
1211 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001212 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001213 case ISD::ROTL :
1214 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1215 VT);
1216 case ISD::ROTR :
1217 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1218 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 default: break;
1220 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001221 } else { // Cannonicalize constant to RHS if commutative
1222 if (isCommutativeBinOp(Opcode)) {
1223 std::swap(N1C, N2C);
1224 std::swap(N1, N2);
1225 }
1226 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001227 }
1228
1229 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1230 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001231 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001232 if (N2CFP) {
1233 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1234 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001235 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1236 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1237 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1238 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001239 if (C2) return getConstantFP(C1 / C2, VT);
1240 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001241 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 if (C2) return getConstantFP(fmod(C1, C2), VT);
1243 break;
1244 default: break;
1245 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 } else { // Cannonicalize constant to RHS if commutative
1247 if (isCommutativeBinOp(Opcode)) {
1248 std::swap(N1CFP, N2CFP);
1249 std::swap(N1, N2);
1250 }
1251 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001252 }
1253
Chris Lattnerc3aae252005-01-07 07:46:32 +00001254 // Finally, fold operations that do not require constants.
1255 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001256 case ISD::FP_ROUND_INREG:
1257 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1258 break;
1259 case ISD::SIGN_EXTEND_INREG: {
1260 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1261 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001262 break;
1263 }
1264
Nate Begemaneea805e2005-04-13 21:23:31 +00001265 // FIXME: figure out how to safely handle things like
1266 // int foo(int x) { return 1 << (x & 255); }
1267 // int bar() { return foo(256); }
1268#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001269 case ISD::SHL:
1270 case ISD::SRL:
1271 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001272 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001273 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001274 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001275 else if (N2.getOpcode() == ISD::AND)
1276 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1277 // If the and is only masking out bits that cannot effect the shift,
1278 // eliminate the and.
1279 unsigned NumBits = MVT::getSizeInBits(VT);
1280 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1281 return getNode(Opcode, VT, N1, N2.getOperand(0));
1282 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001283 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001284#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001285 }
1286
Chris Lattner27e9b412005-05-11 18:57:39 +00001287 // Memoize this node if possible.
1288 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001289 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001290 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1291 if (BON) return SDOperand(BON, 0);
1292
1293 BON = N = new SDNode(Opcode, N1, N2);
1294 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001295 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001296 }
1297
Chris Lattner3e011362005-05-14 07:45:46 +00001298 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001299 AllNodes.push_back(N);
1300 return SDOperand(N, 0);
1301}
1302
Chris Lattnerc3aae252005-01-07 07:46:32 +00001303SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1304 SDOperand N1, SDOperand N2, SDOperand N3) {
1305 // Perform various simplifications.
1306 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1307 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1308 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1309 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001310 case ISD::SETCC: {
1311 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001312 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001313 if (Simp.Val) return Simp;
1314 break;
1315 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001316 case ISD::SELECT:
1317 if (N1C)
1318 if (N1C->getValue())
1319 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001320 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 return N3; // select false, X, Y -> Y
1322
1323 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001324 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001325 case ISD::BRCOND:
1326 if (N2C)
1327 if (N2C->getValue()) // Unconditional branch
1328 return getNode(ISD::BR, MVT::Other, N1, N3);
1329 else
1330 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001331 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 }
1333
Chris Lattner385328c2005-05-14 07:42:29 +00001334 std::vector<SDOperand> Ops;
1335 Ops.reserve(3);
1336 Ops.push_back(N1);
1337 Ops.push_back(N2);
1338 Ops.push_back(N3);
1339
Chris Lattner43247a12005-08-25 19:12:10 +00001340 // Memoize node if it doesn't produce a flag.
1341 SDNode *N;
1342 if (VT != MVT::Flag) {
1343 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1344 if (E) return SDOperand(E, 0);
1345 E = N = new SDNode(Opcode, N1, N2, N3);
1346 } else {
1347 N = new SDNode(Opcode, N1, N2, N3);
1348 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001349 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 AllNodes.push_back(N);
1351 return SDOperand(N, 0);
1352}
1353
1354SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001355 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001356 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001357 std::vector<SDOperand> Ops;
1358 Ops.reserve(4);
1359 Ops.push_back(N1);
1360 Ops.push_back(N2);
1361 Ops.push_back(N3);
1362 Ops.push_back(N4);
1363 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001364}
1365
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001366SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1367 SDOperand N1, SDOperand N2, SDOperand N3,
1368 SDOperand N4, SDOperand N5) {
1369 std::vector<SDOperand> Ops;
1370 Ops.reserve(5);
1371 Ops.push_back(N1);
1372 Ops.push_back(N2);
1373 Ops.push_back(N3);
1374 Ops.push_back(N4);
1375 Ops.push_back(N5);
1376 return getNode(Opcode, VT, Ops);
1377}
1378
Evan Cheng7038daf2005-12-10 00:37:58 +00001379SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1380 SDOperand Chain, SDOperand Ptr,
1381 SDOperand SV) {
1382 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1383 if (N) return SDOperand(N, 0);
1384 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1385
1386 // Loads have a token chain.
1387 setNodeValueTypes(N, VT, MVT::Other);
1388 AllNodes.push_back(N);
1389 return SDOperand(N, 0);
1390}
1391
1392SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1393 SDOperand Chain, SDOperand Ptr,
1394 SDOperand SV) {
1395 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1396 if (N) return SDOperand(N, 0);
1397 std::vector<SDOperand> Ops;
1398 Ops.reserve(5);
1399 Ops.push_back(Chain);
1400 Ops.push_back(Ptr);
1401 Ops.push_back(getConstant(Count, MVT::i32));
1402 Ops.push_back(getValueType(EVT));
1403 Ops.push_back(SV);
1404 std::vector<MVT::ValueType> VTs;
1405 VTs.reserve(2);
1406 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1407 return getNode(ISD::VLOAD, VTs, Ops);
1408}
1409
1410SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1411 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1412 MVT::ValueType EVT) {
1413 std::vector<SDOperand> Ops;
1414 Ops.reserve(4);
1415 Ops.push_back(Chain);
1416 Ops.push_back(Ptr);
1417 Ops.push_back(SV);
1418 Ops.push_back(getValueType(EVT));
1419 std::vector<MVT::ValueType> VTs;
1420 VTs.reserve(2);
1421 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1422 return getNode(Opcode, VTs, Ops);
1423}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001424
Chris Lattner0437cdd2005-05-09 04:14:13 +00001425SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001426 assert((!V || isa<PointerType>(V->getType())) &&
1427 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001428 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1429 if (N) return SDOperand(N, 0);
1430
1431 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001432 AllNodes.push_back(N);
1433 return SDOperand(N, 0);
1434}
1435
Nate Begemanacc398c2006-01-25 18:21:52 +00001436SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1437 SDOperand Chain, SDOperand Ptr,
1438 SDOperand SV) {
1439 std::vector<SDOperand> Ops;
1440 Ops.reserve(3);
1441 Ops.push_back(Chain);
1442 Ops.push_back(Ptr);
1443 Ops.push_back(SV);
1444 std::vector<MVT::ValueType> VTs;
1445 VTs.reserve(2);
1446 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1447 return getNode(ISD::VAARG, VTs, Ops);
1448}
1449
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001450SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001451 std::vector<SDOperand> &Ops) {
1452 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001454 case 1: return getNode(Opcode, VT, Ops[0]);
1455 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1456 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001457 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001458 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001459
Chris Lattner89c34632005-05-14 06:20:26 +00001460 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001461 switch (Opcode) {
1462 default: break;
1463 case ISD::BRCONDTWOWAY:
1464 if (N1C)
1465 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001466 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001467 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001468 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001469 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001470 case ISD::BRTWOWAY_CC:
1471 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1472 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1473 "LHS and RHS of comparison must have same type!");
1474 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001475 case ISD::TRUNCSTORE: {
1476 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1477 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1478#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1479 // If this is a truncating store of a constant, convert to the desired type
1480 // and store it instead.
1481 if (isa<Constant>(Ops[0])) {
1482 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1483 if (isa<Constant>(Op))
1484 N1 = Op;
1485 }
1486 // Also for ConstantFP?
1487#endif
1488 if (Ops[0].getValueType() == EVT) // Normal store?
1489 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1490 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1491 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1492 "Can't do FP-INT conversion!");
1493 break;
1494 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001495 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001496 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001497 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1498 "LHS and RHS of condition must have same type!");
1499 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1500 "True and False arms of SelectCC must have same type!");
1501 assert(Ops[2].getValueType() == VT &&
1502 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001503 break;
1504 }
1505 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001506 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001507 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1508 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001509 break;
1510 }
Chris Lattneref847df2005-04-09 03:27:28 +00001511 }
1512
Chris Lattner385328c2005-05-14 07:42:29 +00001513 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001514 SDNode *N;
1515 if (VT != MVT::Flag) {
1516 SDNode *&E =
1517 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1518 if (E) return SDOperand(E, 0);
1519 E = N = new SDNode(Opcode, Ops);
1520 } else {
1521 N = new SDNode(Opcode, Ops);
1522 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001523 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001524 AllNodes.push_back(N);
1525 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001526}
1527
Chris Lattner89c34632005-05-14 06:20:26 +00001528SDOperand SelectionDAG::getNode(unsigned Opcode,
1529 std::vector<MVT::ValueType> &ResultTys,
1530 std::vector<SDOperand> &Ops) {
1531 if (ResultTys.size() == 1)
1532 return getNode(Opcode, ResultTys[0], Ops);
1533
Chris Lattner5f056bf2005-07-10 01:55:33 +00001534 switch (Opcode) {
1535 case ISD::EXTLOAD:
1536 case ISD::SEXTLOAD:
1537 case ISD::ZEXTLOAD: {
1538 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1539 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1540 // If they are asking for an extending load from/to the same thing, return a
1541 // normal load.
1542 if (ResultTys[0] == EVT)
1543 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1544 assert(EVT < ResultTys[0] &&
1545 "Should only be an extending load, not truncating!");
1546 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1547 "Cannot sign/zero extend a FP load!");
1548 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1549 "Cannot convert from FP to Int or Int -> FP!");
1550 break;
1551 }
1552
Chris Lattnere89083a2005-05-14 07:25:05 +00001553 // FIXME: figure out how to safely handle things like
1554 // int foo(int x) { return 1 << (x & 255); }
1555 // int bar() { return foo(256); }
1556#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001557 case ISD::SRA_PARTS:
1558 case ISD::SRL_PARTS:
1559 case ISD::SHL_PARTS:
1560 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001561 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001562 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1563 else if (N3.getOpcode() == ISD::AND)
1564 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1565 // If the and is only masking out bits that cannot effect the shift,
1566 // eliminate the and.
1567 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1568 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1569 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1570 }
1571 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001572#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001573 }
Chris Lattner89c34632005-05-14 06:20:26 +00001574
Chris Lattner43247a12005-08-25 19:12:10 +00001575 // Memoize the node unless it returns a flag.
1576 SDNode *N;
1577 if (ResultTys.back() != MVT::Flag) {
1578 SDNode *&E =
1579 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1580 if (E) return SDOperand(E, 0);
1581 E = N = new SDNode(Opcode, Ops);
1582 } else {
1583 N = new SDNode(Opcode, Ops);
1584 }
Chris Lattnera3255112005-11-08 23:30:28 +00001585 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001586 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001587 return SDOperand(N, 0);
1588}
1589
Chris Lattnera3255112005-11-08 23:30:28 +00001590void SelectionDAG::setNodeValueTypes(SDNode *N,
1591 std::vector<MVT::ValueType> &RetVals) {
1592 switch (RetVals.size()) {
1593 case 0: return;
1594 case 1: N->setValueTypes(RetVals[0]); return;
1595 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1596 default: break;
1597 }
1598
1599 std::list<std::vector<MVT::ValueType> >::iterator I =
1600 std::find(VTList.begin(), VTList.end(), RetVals);
1601 if (I == VTList.end()) {
1602 VTList.push_front(RetVals);
1603 I = VTList.begin();
1604 }
1605
1606 N->setValueTypes(&(*I)[0], I->size());
1607}
1608
1609void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1610 MVT::ValueType VT2) {
1611 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1612 E = VTList.end(); I != E; ++I) {
1613 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1614 N->setValueTypes(&(*I)[0], 2);
1615 return;
1616 }
1617 }
1618 std::vector<MVT::ValueType> V;
1619 V.push_back(VT1);
1620 V.push_back(VT2);
1621 VTList.push_front(V);
1622 N->setValueTypes(&(*VTList.begin())[0], 2);
1623}
1624
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001625/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1626/// specified operands. If the resultant node already exists in the DAG,
1627/// this does not modify the specified node, instead it returns the node that
1628/// already exists. If the resultant node does not exist in the DAG, the
1629/// input node is returned. As a degenerate case, if you specify the same
1630/// input operands as the node already has, the input node is returned.
1631SDOperand SelectionDAG::
1632UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1633 SDNode *N = InN.Val;
1634 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1635
1636 // Check to see if there is no change.
1637 if (Op == N->getOperand(0)) return InN;
1638
1639 // See if the modified node already exists.
1640 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1641 if (NewSlot && *NewSlot)
1642 return SDOperand(*NewSlot, InN.ResNo);
1643
1644 // Nope it doesn't. Remove the node from it's current place in the maps.
1645 if (NewSlot)
1646 RemoveNodeFromCSEMaps(N);
1647
1648 // Now we update the operands.
1649 N->OperandList[0].Val->removeUser(N);
1650 Op.Val->addUser(N);
1651 N->OperandList[0] = Op;
1652
1653 // If this gets put into a CSE map, add it.
1654 if (NewSlot) *NewSlot = N;
1655 return InN;
1656}
1657
1658SDOperand SelectionDAG::
1659UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1660 SDNode *N = InN.Val;
1661 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1662
1663 // Check to see if there is no change.
1664 bool AnyChange = false;
1665 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1666 return InN; // No operands changed, just return the input node.
1667
1668 // See if the modified node already exists.
1669 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1670 if (NewSlot && *NewSlot)
1671 return SDOperand(*NewSlot, InN.ResNo);
1672
1673 // Nope it doesn't. Remove the node from it's current place in the maps.
1674 if (NewSlot)
1675 RemoveNodeFromCSEMaps(N);
1676
1677 // Now we update the operands.
1678 if (N->OperandList[0] != Op1) {
1679 N->OperandList[0].Val->removeUser(N);
1680 Op1.Val->addUser(N);
1681 N->OperandList[0] = Op1;
1682 }
1683 if (N->OperandList[1] != Op2) {
1684 N->OperandList[1].Val->removeUser(N);
1685 Op2.Val->addUser(N);
1686 N->OperandList[1] = Op2;
1687 }
1688
1689 // If this gets put into a CSE map, add it.
1690 if (NewSlot) *NewSlot = N;
1691 return InN;
1692}
1693
1694SDOperand SelectionDAG::
1695UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1696 std::vector<SDOperand> Ops;
1697 Ops.push_back(Op1);
1698 Ops.push_back(Op2);
1699 Ops.push_back(Op3);
1700 return UpdateNodeOperands(N, Ops);
1701}
1702
1703SDOperand SelectionDAG::
1704UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1705 SDOperand Op3, SDOperand Op4) {
1706 std::vector<SDOperand> Ops;
1707 Ops.push_back(Op1);
1708 Ops.push_back(Op2);
1709 Ops.push_back(Op3);
1710 Ops.push_back(Op4);
1711 return UpdateNodeOperands(N, Ops);
1712}
1713
1714SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001715UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1716 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1717 std::vector<SDOperand> Ops;
1718 Ops.push_back(Op1);
1719 Ops.push_back(Op2);
1720 Ops.push_back(Op3);
1721 Ops.push_back(Op4);
1722 Ops.push_back(Op5);
1723 return UpdateNodeOperands(N, Ops);
1724}
1725
1726
1727SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001728UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1729 SDNode *N = InN.Val;
1730 assert(N->getNumOperands() == Ops.size() &&
1731 "Update with wrong number of operands");
1732
1733 // Check to see if there is no change.
1734 unsigned NumOps = Ops.size();
1735 bool AnyChange = false;
1736 for (unsigned i = 0; i != NumOps; ++i) {
1737 if (Ops[i] != N->getOperand(i)) {
1738 AnyChange = true;
1739 break;
1740 }
1741 }
1742
1743 // No operands changed, just return the input node.
1744 if (!AnyChange) return InN;
1745
1746 // See if the modified node already exists.
1747 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1748 if (NewSlot && *NewSlot)
1749 return SDOperand(*NewSlot, InN.ResNo);
1750
1751 // Nope it doesn't. Remove the node from it's current place in the maps.
1752 if (NewSlot)
1753 RemoveNodeFromCSEMaps(N);
1754
1755 // Now we update the operands.
1756 for (unsigned i = 0; i != NumOps; ++i) {
1757 if (N->OperandList[i] != Ops[i]) {
1758 N->OperandList[i].Val->removeUser(N);
1759 Ops[i].Val->addUser(N);
1760 N->OperandList[i] = Ops[i];
1761 }
1762 }
1763
1764 // If this gets put into a CSE map, add it.
1765 if (NewSlot) *NewSlot = N;
1766 return InN;
1767}
1768
1769
1770
Chris Lattner149c58c2005-08-16 18:17:10 +00001771
1772/// SelectNodeTo - These are used for target selectors to *mutate* the
1773/// specified node to have the specified return type, Target opcode, and
1774/// operands. Note that target opcodes are stored as
1775/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001776///
1777/// Note that SelectNodeTo returns the resultant node. If there is already a
1778/// node of the specified opcode and operands, it returns that node instead of
1779/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001780SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1781 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001782 // If an identical node already exists, use it.
1783 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1784 if (ON) return SDOperand(ON, 0);
1785
Chris Lattner7651fa42005-08-24 23:00:29 +00001786 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001787
Chris Lattner7651fa42005-08-24 23:00:29 +00001788 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1789 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001790
1791 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001792 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001793}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001794
Chris Lattnereb19e402005-11-30 22:45:14 +00001795SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1796 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001797 // If an identical node already exists, use it.
1798 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1799 std::make_pair(Op1, VT))];
1800 if (ON) return SDOperand(ON, 0);
1801
Chris Lattner149c58c2005-08-16 18:17:10 +00001802 RemoveNodeFromCSEMaps(N);
1803 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1804 N->setValueTypes(VT);
1805 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001806
1807 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001808 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001809}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001810
Chris Lattnereb19e402005-11-30 22:45:14 +00001811SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1812 MVT::ValueType VT, SDOperand Op1,
1813 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001814 // If an identical node already exists, use it.
1815 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1816 std::make_pair(Op1, Op2))];
1817 if (ON) return SDOperand(ON, 0);
1818
Chris Lattner149c58c2005-08-16 18:17:10 +00001819 RemoveNodeFromCSEMaps(N);
1820 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1821 N->setValueTypes(VT);
1822 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001823
1824 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001825 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001826}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001827
Chris Lattnereb19e402005-11-30 22:45:14 +00001828SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1829 MVT::ValueType VT, SDOperand Op1,
1830 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001831 // If an identical node already exists, use it.
1832 std::vector<SDOperand> OpList;
1833 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1834 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1835 std::make_pair(VT, OpList))];
1836 if (ON) return SDOperand(ON, 0);
1837
Chris Lattner149c58c2005-08-16 18:17:10 +00001838 RemoveNodeFromCSEMaps(N);
1839 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1840 N->setValueTypes(VT);
1841 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001842
1843 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001844 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001845}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001846
Chris Lattnereb19e402005-11-30 22:45:14 +00001847SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1848 MVT::ValueType VT, SDOperand Op1,
1849 SDOperand Op2, SDOperand Op3,
1850 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001851 // If an identical node already exists, use it.
1852 std::vector<SDOperand> OpList;
1853 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1854 OpList.push_back(Op4);
1855 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1856 std::make_pair(VT, OpList))];
1857 if (ON) return SDOperand(ON, 0);
1858
Nate Begeman294a0a12005-08-18 07:30:15 +00001859 RemoveNodeFromCSEMaps(N);
1860 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1861 N->setValueTypes(VT);
1862 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001863
1864 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001865 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001866}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001867
Chris Lattnereb19e402005-11-30 22:45:14 +00001868SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1869 MVT::ValueType VT, SDOperand Op1,
1870 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1871 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001872 // If an identical node already exists, use it.
1873 std::vector<SDOperand> OpList;
1874 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1875 OpList.push_back(Op4); OpList.push_back(Op5);
1876 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1877 std::make_pair(VT, OpList))];
1878 if (ON) return SDOperand(ON, 0);
1879
Chris Lattner6b09a292005-08-21 18:49:33 +00001880 RemoveNodeFromCSEMaps(N);
1881 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1882 N->setValueTypes(VT);
1883 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001884
1885 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001886 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001887}
Chris Lattner149c58c2005-08-16 18:17:10 +00001888
Chris Lattnereb19e402005-11-30 22:45:14 +00001889SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1890 MVT::ValueType VT, SDOperand Op1,
1891 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1892 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001893 // If an identical node already exists, use it.
1894 std::vector<SDOperand> OpList;
1895 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1896 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1897 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1898 std::make_pair(VT, OpList))];
1899 if (ON) return SDOperand(ON, 0);
1900
Evan Cheng61ca74b2005-11-30 02:04:11 +00001901 RemoveNodeFromCSEMaps(N);
1902 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1903 N->setValueTypes(VT);
1904 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001905
1906 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001907 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001908}
1909
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001910SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1911 MVT::ValueType VT, SDOperand Op1,
1912 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1913 SDOperand Op5, SDOperand Op6,
1914 SDOperand Op7) {
1915 // If an identical node already exists, use it.
1916 std::vector<SDOperand> OpList;
1917 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1918 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1919 OpList.push_back(Op7);
1920 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1921 std::make_pair(VT, OpList))];
1922 if (ON) return SDOperand(ON, 0);
1923
1924 RemoveNodeFromCSEMaps(N);
1925 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1926 N->setValueTypes(VT);
1927 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1928
1929 ON = N; // Memoize the new node.
1930 return SDOperand(N, 0);
1931}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001932SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1933 MVT::ValueType VT, SDOperand Op1,
1934 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1935 SDOperand Op5, SDOperand Op6,
1936 SDOperand Op7, SDOperand Op8) {
1937 // If an identical node already exists, use it.
1938 std::vector<SDOperand> OpList;
1939 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1940 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1941 OpList.push_back(Op7); OpList.push_back(Op8);
1942 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1943 std::make_pair(VT, OpList))];
1944 if (ON) return SDOperand(ON, 0);
1945
1946 RemoveNodeFromCSEMaps(N);
1947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1948 N->setValueTypes(VT);
1949 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1950
1951 ON = N; // Memoize the new node.
1952 return SDOperand(N, 0);
1953}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001954
Chris Lattnereb19e402005-11-30 22:45:14 +00001955SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1956 MVT::ValueType VT1, MVT::ValueType VT2,
1957 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001958 // If an identical node already exists, use it.
1959 std::vector<SDOperand> OpList;
1960 OpList.push_back(Op1); OpList.push_back(Op2);
1961 std::vector<MVT::ValueType> VTList;
1962 VTList.push_back(VT1); VTList.push_back(VT2);
1963 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1964 std::make_pair(VTList, OpList))];
1965 if (ON) return SDOperand(ON, 0);
1966
Chris Lattner0fb094f2005-11-19 01:44:53 +00001967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1969 setNodeValueTypes(N, VT1, VT2);
1970 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001971
1972 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001973 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001974}
1975
Chris Lattnereb19e402005-11-30 22:45:14 +00001976SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1977 MVT::ValueType VT1, MVT::ValueType VT2,
1978 SDOperand Op1, SDOperand Op2,
1979 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001980 // If an identical node already exists, use it.
1981 std::vector<SDOperand> OpList;
1982 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1983 std::vector<MVT::ValueType> VTList;
1984 VTList.push_back(VT1); VTList.push_back(VT2);
1985 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1986 std::make_pair(VTList, OpList))];
1987 if (ON) return SDOperand(ON, 0);
1988
Chris Lattner0fb094f2005-11-19 01:44:53 +00001989 RemoveNodeFromCSEMaps(N);
1990 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1991 setNodeValueTypes(N, VT1, VT2);
1992 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001993
1994 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001995 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001996}
1997
Chris Lattnereb19e402005-11-30 22:45:14 +00001998SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1999 MVT::ValueType VT1, MVT::ValueType VT2,
2000 SDOperand Op1, SDOperand Op2,
2001 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002002 // If an identical node already exists, use it.
2003 std::vector<SDOperand> OpList;
2004 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2005 OpList.push_back(Op4);
2006 std::vector<MVT::ValueType> VTList;
2007 VTList.push_back(VT1); VTList.push_back(VT2);
2008 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2009 std::make_pair(VTList, OpList))];
2010 if (ON) return SDOperand(ON, 0);
2011
Chris Lattner0fb094f2005-11-19 01:44:53 +00002012 RemoveNodeFromCSEMaps(N);
2013 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2014 setNodeValueTypes(N, VT1, VT2);
2015 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002016
2017 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002018 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002019}
2020
Chris Lattnereb19e402005-11-30 22:45:14 +00002021SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2022 MVT::ValueType VT1, MVT::ValueType VT2,
2023 SDOperand Op1, SDOperand Op2,
2024 SDOperand Op3, SDOperand Op4,
2025 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026 // If an identical node already exists, use it.
2027 std::vector<SDOperand> OpList;
2028 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2029 OpList.push_back(Op4); OpList.push_back(Op5);
2030 std::vector<MVT::ValueType> VTList;
2031 VTList.push_back(VT1); VTList.push_back(VT2);
2032 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2033 std::make_pair(VTList, OpList))];
2034 if (ON) return SDOperand(ON, 0);
2035
Chris Lattner0fb094f2005-11-19 01:44:53 +00002036 RemoveNodeFromCSEMaps(N);
2037 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2038 setNodeValueTypes(N, VT1, VT2);
2039 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002040
2041 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002042 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002043}
2044
Evan Cheng6ae46c42006-02-09 07:15:23 +00002045/// getTargetNode - These are used for target selectors to create a new node
2046/// with specified return type(s), target opcode, and operands.
2047///
2048/// Note that getTargetNode returns the resultant node. If there is already a
2049/// node of the specified opcode and operands, it returns that node instead of
2050/// the current one.
2051SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2052 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2053}
2054SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2055 SDOperand Op1) {
2056 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2057}
2058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2059 SDOperand Op1, SDOperand Op2) {
2060 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2061}
2062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2063 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2064 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2065}
2066SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2067 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2068 SDOperand Op4) {
2069 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2070}
2071SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2072 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2073 SDOperand Op4, SDOperand Op5) {
2074 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2075}
2076SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2077 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2078 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2079 std::vector<SDOperand> Ops;
2080 Ops.reserve(6);
2081 Ops.push_back(Op1);
2082 Ops.push_back(Op2);
2083 Ops.push_back(Op3);
2084 Ops.push_back(Op4);
2085 Ops.push_back(Op5);
2086 Ops.push_back(Op6);
2087 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2088}
2089SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2090 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2091 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2092 SDOperand Op7) {
2093 std::vector<SDOperand> Ops;
2094 Ops.reserve(7);
2095 Ops.push_back(Op1);
2096 Ops.push_back(Op2);
2097 Ops.push_back(Op3);
2098 Ops.push_back(Op4);
2099 Ops.push_back(Op5);
2100 Ops.push_back(Op6);
2101 Ops.push_back(Op7);
2102 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2103}
2104SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2105 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2106 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2107 SDOperand Op7, SDOperand Op8) {
2108 std::vector<SDOperand> Ops;
2109 Ops.reserve(8);
2110 Ops.push_back(Op1);
2111 Ops.push_back(Op2);
2112 Ops.push_back(Op3);
2113 Ops.push_back(Op4);
2114 Ops.push_back(Op5);
2115 Ops.push_back(Op6);
2116 Ops.push_back(Op7);
2117 Ops.push_back(Op8);
2118 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2119}
2120SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2121 std::vector<SDOperand> &Ops) {
2122 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2123}
2124SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2125 MVT::ValueType VT2, SDOperand Op1) {
2126 std::vector<MVT::ValueType> ResultTys;
2127 ResultTys.push_back(VT1);
2128 ResultTys.push_back(VT2);
2129 std::vector<SDOperand> Ops;
2130 Ops.push_back(Op1);
2131 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2132}
2133SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2134 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2135 std::vector<MVT::ValueType> ResultTys;
2136 ResultTys.push_back(VT1);
2137 ResultTys.push_back(VT2);
2138 std::vector<SDOperand> Ops;
2139 Ops.push_back(Op1);
2140 Ops.push_back(Op2);
2141 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2142}
2143SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2144 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2145 SDOperand Op3) {
2146 std::vector<MVT::ValueType> ResultTys;
2147 ResultTys.push_back(VT1);
2148 ResultTys.push_back(VT2);
2149 std::vector<SDOperand> Ops;
2150 Ops.push_back(Op1);
2151 Ops.push_back(Op2);
2152 Ops.push_back(Op3);
2153 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2154}
2155SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2156 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2157 SDOperand Op3, SDOperand Op4) {
2158 std::vector<MVT::ValueType> ResultTys;
2159 ResultTys.push_back(VT1);
2160 ResultTys.push_back(VT2);
2161 std::vector<SDOperand> Ops;
2162 Ops.push_back(Op1);
2163 Ops.push_back(Op2);
2164 Ops.push_back(Op3);
2165 Ops.push_back(Op4);
2166 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2167}
2168SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2169 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2170 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2171 std::vector<MVT::ValueType> ResultTys;
2172 ResultTys.push_back(VT1);
2173 ResultTys.push_back(VT2);
2174 std::vector<SDOperand> Ops;
2175 Ops.push_back(Op1);
2176 Ops.push_back(Op2);
2177 Ops.push_back(Op3);
2178 Ops.push_back(Op4);
2179 Ops.push_back(Op5);
2180 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2181}
2182SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2183 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2184 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2185 SDOperand Op6) {
2186 std::vector<MVT::ValueType> ResultTys;
2187 ResultTys.push_back(VT1);
2188 ResultTys.push_back(VT2);
2189 std::vector<SDOperand> Ops;
2190 Ops.push_back(Op1);
2191 Ops.push_back(Op2);
2192 Ops.push_back(Op3);
2193 Ops.push_back(Op4);
2194 Ops.push_back(Op5);
2195 Ops.push_back(Op6);
2196 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2197}
2198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2199 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2200 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2201 SDOperand Op6, SDOperand Op7) {
2202 std::vector<MVT::ValueType> ResultTys;
2203 ResultTys.push_back(VT1);
2204 ResultTys.push_back(VT2);
2205 std::vector<SDOperand> Ops;
2206 Ops.push_back(Op1);
2207 Ops.push_back(Op2);
2208 Ops.push_back(Op3);
2209 Ops.push_back(Op4);
2210 Ops.push_back(Op5);
2211 Ops.push_back(Op6);
2212 Ops.push_back(Op7);
2213 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2214}
2215SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2216 MVT::ValueType VT2, MVT::ValueType VT3,
2217 SDOperand Op1, SDOperand Op2) {
2218 std::vector<MVT::ValueType> ResultTys;
2219 ResultTys.push_back(VT1);
2220 ResultTys.push_back(VT2);
2221 ResultTys.push_back(VT3);
2222 std::vector<SDOperand> Ops;
2223 Ops.push_back(Op1);
2224 Ops.push_back(Op2);
2225 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2226}
2227SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2228 MVT::ValueType VT2, MVT::ValueType VT3,
2229 SDOperand Op1, SDOperand Op2,
2230 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2231 std::vector<MVT::ValueType> ResultTys;
2232 ResultTys.push_back(VT1);
2233 ResultTys.push_back(VT2);
2234 ResultTys.push_back(VT3);
2235 std::vector<SDOperand> Ops;
2236 Ops.push_back(Op1);
2237 Ops.push_back(Op2);
2238 Ops.push_back(Op3);
2239 Ops.push_back(Op4);
2240 Ops.push_back(Op5);
2241 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2242}
2243SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2244 MVT::ValueType VT2, MVT::ValueType VT3,
2245 SDOperand Op1, SDOperand Op2,
2246 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2247 SDOperand Op6) {
2248 std::vector<MVT::ValueType> ResultTys;
2249 ResultTys.push_back(VT1);
2250 ResultTys.push_back(VT2);
2251 ResultTys.push_back(VT3);
2252 std::vector<SDOperand> Ops;
2253 Ops.push_back(Op1);
2254 Ops.push_back(Op2);
2255 Ops.push_back(Op3);
2256 Ops.push_back(Op4);
2257 Ops.push_back(Op5);
2258 Ops.push_back(Op6);
2259 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2260}
2261SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2262 MVT::ValueType VT2, MVT::ValueType VT3,
2263 SDOperand Op1, SDOperand Op2,
2264 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2265 SDOperand Op6, SDOperand Op7) {
2266 std::vector<MVT::ValueType> ResultTys;
2267 ResultTys.push_back(VT1);
2268 ResultTys.push_back(VT2);
2269 ResultTys.push_back(VT3);
2270 std::vector<SDOperand> Ops;
2271 Ops.push_back(Op1);
2272 Ops.push_back(Op2);
2273 Ops.push_back(Op3);
2274 Ops.push_back(Op4);
2275 Ops.push_back(Op5);
2276 Ops.push_back(Op6);
2277 Ops.push_back(Op7);
2278 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2279}
2280SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2281 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2282 std::vector<MVT::ValueType> ResultTys;
2283 ResultTys.push_back(VT1);
2284 ResultTys.push_back(VT2);
2285 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2286}
2287
Chris Lattner0fb094f2005-11-19 01:44:53 +00002288// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002289/// This can cause recursive merging of nodes in the DAG.
2290///
Chris Lattner8b52f212005-08-26 18:36:28 +00002291/// This version assumes From/To have a single result value.
2292///
Chris Lattner1e111c72005-09-07 05:37:01 +00002293void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2294 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002295 SDNode *From = FromN.Val, *To = ToN.Val;
2296 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2297 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002298 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002299
Chris Lattner8b8749f2005-08-17 19:00:20 +00002300 while (!From->use_empty()) {
2301 // Process users until they are all gone.
2302 SDNode *U = *From->use_begin();
2303
2304 // This node is about to morph, remove its old self from the CSE maps.
2305 RemoveNodeFromCSEMaps(U);
2306
Chris Lattner65113b22005-11-08 22:07:03 +00002307 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2308 I != E; ++I)
2309 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002310 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002311 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002312 To->addUser(U);
2313 }
2314
2315 // Now that we have modified U, add it back to the CSE maps. If it already
2316 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002317 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2318 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002319 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002320 if (Deleted) Deleted->push_back(U);
2321 DeleteNodeNotInCSEMaps(U);
2322 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002323 }
2324}
2325
2326/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2327/// This can cause recursive merging of nodes in the DAG.
2328///
2329/// This version assumes From/To have matching types and numbers of result
2330/// values.
2331///
Chris Lattner1e111c72005-09-07 05:37:01 +00002332void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2333 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002334 assert(From != To && "Cannot replace uses of with self");
2335 assert(From->getNumValues() == To->getNumValues() &&
2336 "Cannot use this version of ReplaceAllUsesWith!");
2337 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002338 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002339 return;
2340 }
2341
2342 while (!From->use_empty()) {
2343 // Process users until they are all gone.
2344 SDNode *U = *From->use_begin();
2345
2346 // This node is about to morph, remove its old self from the CSE maps.
2347 RemoveNodeFromCSEMaps(U);
2348
Chris Lattner65113b22005-11-08 22:07:03 +00002349 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2350 I != E; ++I)
2351 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002352 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002353 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002354 To->addUser(U);
2355 }
2356
2357 // Now that we have modified U, add it back to the CSE maps. If it already
2358 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002359 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2360 ReplaceAllUsesWith(U, Existing, Deleted);
2361 // U is now dead.
2362 if (Deleted) Deleted->push_back(U);
2363 DeleteNodeNotInCSEMaps(U);
2364 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002365 }
2366}
2367
Chris Lattner8b52f212005-08-26 18:36:28 +00002368/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2369/// This can cause recursive merging of nodes in the DAG.
2370///
2371/// This version can replace From with any result values. To must match the
2372/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002373void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002374 const std::vector<SDOperand> &To,
2375 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002376 assert(From->getNumValues() == To.size() &&
2377 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002378 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002379 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002380 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002381 return;
2382 }
2383
2384 while (!From->use_empty()) {
2385 // Process users until they are all gone.
2386 SDNode *U = *From->use_begin();
2387
2388 // This node is about to morph, remove its old self from the CSE maps.
2389 RemoveNodeFromCSEMaps(U);
2390
Chris Lattner65113b22005-11-08 22:07:03 +00002391 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2392 I != E; ++I)
2393 if (I->Val == From) {
2394 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002395 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002396 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002397 ToOp.Val->addUser(U);
2398 }
2399
2400 // Now that we have modified U, add it back to the CSE maps. If it already
2401 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002402 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2403 ReplaceAllUsesWith(U, Existing, Deleted);
2404 // U is now dead.
2405 if (Deleted) Deleted->push_back(U);
2406 DeleteNodeNotInCSEMaps(U);
2407 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002408 }
2409}
2410
Chris Lattner012f2412006-02-17 21:58:01 +00002411/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2412/// uses of other values produced by From.Val alone. The Deleted vector is
2413/// handled the same was as for ReplaceAllUsesWith.
2414void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2415 std::vector<SDNode*> &Deleted) {
2416 assert(From != To && "Cannot replace a value with itself");
2417 // Handle the simple, trivial, case efficiently.
2418 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2419 ReplaceAllUsesWith(From, To, &Deleted);
2420 return;
2421 }
2422
2423 // Get all of the users in a nice, deterministically ordered, uniqued set.
2424 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2425
2426 while (!Users.empty()) {
2427 // We know that this user uses some value of From. If it is the right
2428 // value, update it.
2429 SDNode *User = Users.back();
2430 Users.pop_back();
2431
2432 for (SDOperand *Op = User->OperandList,
2433 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2434 if (*Op == From) {
2435 // Okay, we know this user needs to be updated. Remove its old self
2436 // from the CSE maps.
2437 RemoveNodeFromCSEMaps(User);
2438
2439 // Update all operands that match "From".
2440 for (; Op != E; ++Op) {
2441 if (*Op == From) {
2442 From.Val->removeUser(User);
2443 *Op = To;
2444 To.Val->addUser(User);
2445 }
2446 }
2447
2448 // Now that we have modified User, add it back to the CSE maps. If it
2449 // already exists there, recursively merge the results together.
2450 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2451 unsigned NumDeleted = Deleted.size();
2452 ReplaceAllUsesWith(User, Existing, &Deleted);
2453
2454 // User is now dead.
2455 Deleted.push_back(User);
2456 DeleteNodeNotInCSEMaps(User);
2457
2458 // We have to be careful here, because ReplaceAllUsesWith could have
2459 // deleted a user of From, which means there may be dangling pointers
2460 // in the "Users" setvector. Scan over the deleted node pointers and
2461 // remove them from the setvector.
2462 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2463 Users.remove(Deleted[i]);
2464 }
2465 break; // Exit the operand scanning loop.
2466 }
2467 }
2468 }
2469}
2470
Chris Lattner7b2880c2005-08-24 22:44:39 +00002471
Jim Laskey58b968b2005-08-17 20:08:02 +00002472//===----------------------------------------------------------------------===//
2473// SDNode Class
2474//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002475
Chris Lattnera3255112005-11-08 23:30:28 +00002476
2477/// getValueTypeList - Return a pointer to the specified value type.
2478///
2479MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2480 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2481 VTs[VT] = VT;
2482 return &VTs[VT];
2483}
2484
Chris Lattner5c884562005-01-12 18:37:47 +00002485/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2486/// indicated value. This method ignores uses of other values defined by this
2487/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002488bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002489 assert(Value < getNumValues() && "Bad value!");
2490
2491 // If there is only one value, this is easy.
2492 if (getNumValues() == 1)
2493 return use_size() == NUses;
2494 if (Uses.size() < NUses) return false;
2495
Evan Cheng4ee62112006-02-05 06:29:23 +00002496 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002497
2498 std::set<SDNode*> UsersHandled;
2499
Evan Cheng4ee62112006-02-05 06:29:23 +00002500 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002501 UI != E; ++UI) {
2502 SDNode *User = *UI;
2503 if (User->getNumOperands() == 1 ||
2504 UsersHandled.insert(User).second) // First time we've seen this?
2505 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2506 if (User->getOperand(i) == TheValue) {
2507 if (NUses == 0)
2508 return false; // too many uses
2509 --NUses;
2510 }
2511 }
2512
2513 // Found exactly the right number of uses?
2514 return NUses == 0;
2515}
2516
2517
Evan Cheng4ee62112006-02-05 06:29:23 +00002518// isOnlyUse - Return true if this node is the only use of N.
2519bool SDNode::isOnlyUse(SDNode *N) const {
2520 bool Seen = false;
2521 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2522 SDNode *User = *I;
2523 if (User == this)
2524 Seen = true;
2525 else
2526 return false;
2527 }
2528
2529 return Seen;
2530}
2531
2532
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002533const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002534 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002535 default:
2536 if (getOpcode() < ISD::BUILTIN_OP_END)
2537 return "<<Unknown DAG Node>>";
2538 else {
Evan Cheng72261582005-12-20 06:22:03 +00002539 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002540 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002541 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2542 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002543
Evan Cheng72261582005-12-20 06:22:03 +00002544 TargetLowering &TLI = G->getTargetLoweringInfo();
2545 const char *Name =
2546 TLI.getTargetNodeName(getOpcode());
2547 if (Name) return Name;
2548 }
2549
2550 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002551 }
2552
Andrew Lenharth95762122005-03-31 21:24:06 +00002553 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002554 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002555 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002556 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002557 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002558 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002559 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002560 case ISD::AssertSext: return "AssertSext";
2561 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002562 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002563 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002564 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002565 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002566 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002567 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002568 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002569 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002570 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002571 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002572 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002573 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002574 case ISD::ConstantPool: return "ConstantPool";
2575 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002576 case ISD::CopyToReg: return "CopyToReg";
2577 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002578 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002579 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002580 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002581 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002582
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002583 // Unary operators
2584 case ISD::FABS: return "fabs";
2585 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002586 case ISD::FSQRT: return "fsqrt";
2587 case ISD::FSIN: return "fsin";
2588 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002589
2590 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002591 case ISD::ADD: return "add";
2592 case ISD::SUB: return "sub";
2593 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002594 case ISD::MULHU: return "mulhu";
2595 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002596 case ISD::SDIV: return "sdiv";
2597 case ISD::UDIV: return "udiv";
2598 case ISD::SREM: return "srem";
2599 case ISD::UREM: return "urem";
2600 case ISD::AND: return "and";
2601 case ISD::OR: return "or";
2602 case ISD::XOR: return "xor";
2603 case ISD::SHL: return "shl";
2604 case ISD::SRA: return "sra";
2605 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002606 case ISD::ROTL: return "rotl";
2607 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002608 case ISD::FADD: return "fadd";
2609 case ISD::FSUB: return "fsub";
2610 case ISD::FMUL: return "fmul";
2611 case ISD::FDIV: return "fdiv";
2612 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002613 case ISD::VADD: return "vadd";
2614 case ISD::VSUB: return "vsub";
2615 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002616
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002617 case ISD::SETCC: return "setcc";
2618 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002619 case ISD::SELECT_CC: return "select_cc";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002620 case ISD::ADDC: return "addc";
2621 case ISD::ADDE: return "adde";
2622 case ISD::SUBC: return "subc";
2623 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002624 case ISD::SHL_PARTS: return "shl_parts";
2625 case ISD::SRA_PARTS: return "sra_parts";
2626 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002627
Chris Lattner7f644642005-04-28 21:44:03 +00002628 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002629 case ISD::SIGN_EXTEND: return "sign_extend";
2630 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002631 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002632 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002633 case ISD::TRUNCATE: return "truncate";
2634 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002635 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002636 case ISD::FP_EXTEND: return "fp_extend";
2637
2638 case ISD::SINT_TO_FP: return "sint_to_fp";
2639 case ISD::UINT_TO_FP: return "uint_to_fp";
2640 case ISD::FP_TO_SINT: return "fp_to_sint";
2641 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002642 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002643
2644 // Control flow instructions
2645 case ISD::BR: return "br";
2646 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002647 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002648 case ISD::BR_CC: return "br_cc";
2649 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002650 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002651 case ISD::CALLSEQ_START: return "callseq_start";
2652 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002653
2654 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002655 case ISD::LOAD: return "load";
2656 case ISD::STORE: return "store";
2657 case ISD::VLOAD: return "vload";
2658 case ISD::EXTLOAD: return "extload";
2659 case ISD::SEXTLOAD: return "sextload";
2660 case ISD::ZEXTLOAD: return "zextload";
2661 case ISD::TRUNCSTORE: return "truncstore";
2662 case ISD::VAARG: return "vaarg";
2663 case ISD::VACOPY: return "vacopy";
2664 case ISD::VAEND: return "vaend";
2665 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002666 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002667 case ISD::EXTRACT_ELEMENT: return "extract_element";
2668 case ISD::BUILD_PAIR: return "build_pair";
2669 case ISD::STACKSAVE: return "stacksave";
2670 case ISD::STACKRESTORE: return "stackrestore";
2671
2672 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002673 case ISD::MEMSET: return "memset";
2674 case ISD::MEMCPY: return "memcpy";
2675 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002676
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002677 // Bit manipulation
2678 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002679 case ISD::CTPOP: return "ctpop";
2680 case ISD::CTTZ: return "cttz";
2681 case ISD::CTLZ: return "ctlz";
2682
2683 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002684 case ISD::READPORT: return "readport";
2685 case ISD::WRITEPORT: return "writeport";
2686 case ISD::READIO: return "readio";
2687 case ISD::WRITEIO: return "writeio";
2688
Chris Lattner36ce6912005-11-29 06:21:05 +00002689 // Debug info
2690 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002691 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002692 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002693
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002694 case ISD::CONDCODE:
2695 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002696 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002697 case ISD::SETOEQ: return "setoeq";
2698 case ISD::SETOGT: return "setogt";
2699 case ISD::SETOGE: return "setoge";
2700 case ISD::SETOLT: return "setolt";
2701 case ISD::SETOLE: return "setole";
2702 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002703
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002704 case ISD::SETO: return "seto";
2705 case ISD::SETUO: return "setuo";
2706 case ISD::SETUEQ: return "setue";
2707 case ISD::SETUGT: return "setugt";
2708 case ISD::SETUGE: return "setuge";
2709 case ISD::SETULT: return "setult";
2710 case ISD::SETULE: return "setule";
2711 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002712
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002713 case ISD::SETEQ: return "seteq";
2714 case ISD::SETGT: return "setgt";
2715 case ISD::SETGE: return "setge";
2716 case ISD::SETLT: return "setlt";
2717 case ISD::SETLE: return "setle";
2718 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002719 }
2720 }
2721}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002722
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002723void SDNode::dump() const { dump(0); }
2724void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002725 std::cerr << (void*)this << ": ";
2726
2727 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2728 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002729 if (getValueType(i) == MVT::Other)
2730 std::cerr << "ch";
2731 else
2732 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002733 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002734 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002735
2736 std::cerr << " ";
2737 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2738 if (i) std::cerr << ", ";
2739 std::cerr << (void*)getOperand(i).Val;
2740 if (unsigned RN = getOperand(i).ResNo)
2741 std::cerr << ":" << RN;
2742 }
2743
2744 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2745 std::cerr << "<" << CSDN->getValue() << ">";
2746 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2747 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002748 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002749 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002750 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002751 std::cerr << "<";
2752 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002753 if (offset > 0)
2754 std::cerr << " + " << offset;
2755 else
2756 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002757 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002758 std::cerr << "<" << FIDN->getIndex() << ">";
2759 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002760 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002761 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002762 std::cerr << "<";
2763 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2764 if (LBB)
2765 std::cerr << LBB->getName() << " ";
2766 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002767 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002768 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002769 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2770 } else {
2771 std::cerr << " #" << R->getReg();
2772 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002773 } else if (const ExternalSymbolSDNode *ES =
2774 dyn_cast<ExternalSymbolSDNode>(this)) {
2775 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002776 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2777 if (M->getValue())
2778 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2779 else
2780 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002781 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2782 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002783 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002784}
2785
Chris Lattnerde202b32005-11-09 23:47:37 +00002786static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002787 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2788 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002789 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002790 else
2791 std::cerr << "\n" << std::string(indent+2, ' ')
2792 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002793
Chris Lattnerea946cd2005-01-09 20:38:33 +00002794
2795 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002796 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002797}
2798
Chris Lattnerc3aae252005-01-07 07:46:32 +00002799void SelectionDAG::dump() const {
2800 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002801 std::vector<const SDNode*> Nodes;
2802 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2803 I != E; ++I)
2804 Nodes.push_back(I);
2805
Chris Lattner49d24712005-01-09 20:26:36 +00002806 std::sort(Nodes.begin(), Nodes.end());
2807
2808 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002809 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002810 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002811 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002812
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002813 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002814
Chris Lattnerc3aae252005-01-07 07:46:32 +00002815 std::cerr << "\n\n";
2816}
2817
Evan Chengfae9f1c2006-02-09 22:11:03 +00002818/// InsertISelMapEntry - A helper function to insert a key / element pair
2819/// into a SDOperand to SDOperand map. This is added to avoid the map
2820/// insertion operator from being inlined.
2821void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2822 SDNode *Key, unsigned KeyResNo,
2823 SDNode *Element, unsigned ElementResNo) {
2824 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2825 SDOperand(Element, ElementResNo)));
2826}