blob: bb66825cd3db8141c462de718b4b56f8d36610d3 [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
1050 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1051 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1052 break;
1053 case ISD::ZERO_EXTEND:
1054 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001055 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001056 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001058 case ISD::ANY_EXTEND:
1059 if (Operand.getValueType() == VT) return Operand; // noop extension
1060 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1061 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1062 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1063 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064 case ISD::TRUNCATE:
1065 if (Operand.getValueType() == VT) return Operand; // noop truncate
1066 if (OpOpcode == ISD::TRUNCATE)
1067 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001068 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1069 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001070 // If the source is smaller than the dest, we still need an extend.
1071 if (Operand.Val->getOperand(0).getValueType() < VT)
1072 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1073 else if (Operand.Val->getOperand(0).getValueType() > VT)
1074 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1075 else
1076 return Operand.Val->getOperand(0);
1077 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 break;
Chris Lattner35481892005-12-23 00:16:34 +00001079 case ISD::BIT_CONVERT:
1080 // Basic sanity checking.
1081 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1082 "Cannot BIT_CONVERT between two different types!");
1083 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001084 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1085 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001086 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001087 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001088 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1089 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001090 Operand.Val->getOperand(0));
1091 if (OpOpcode == ISD::FNEG) // --X -> X
1092 return Operand.Val->getOperand(0);
1093 break;
1094 case ISD::FABS:
1095 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1096 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1097 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 }
1099
Chris Lattner43247a12005-08-25 19:12:10 +00001100 SDNode *N;
1101 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1102 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001103 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001104 E = N = new SDNode(Opcode, Operand);
1105 } else {
1106 N = new SDNode(Opcode, Operand);
1107 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 N->setValueTypes(VT);
1109 AllNodes.push_back(N);
1110 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001111}
1112
Chris Lattner36019aa2005-04-18 03:48:41 +00001113
1114
Chris Lattnerc3aae252005-01-07 07:46:32 +00001115SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1116 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001117#ifndef NDEBUG
1118 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001119 case ISD::TokenFactor:
1120 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1121 N2.getValueType() == MVT::Other && "Invalid token factor!");
1122 break;
Chris Lattner76365122005-01-16 02:23:22 +00001123 case ISD::AND:
1124 case ISD::OR:
1125 case ISD::XOR:
1126 case ISD::UDIV:
1127 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001128 case ISD::MULHU:
1129 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001130 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1131 // fall through
1132 case ISD::ADD:
1133 case ISD::SUB:
1134 case ISD::MUL:
1135 case ISD::SDIV:
1136 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001137 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1138 // fall through.
1139 case ISD::FADD:
1140 case ISD::FSUB:
1141 case ISD::FMUL:
1142 case ISD::FDIV:
1143 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001144 assert(N1.getValueType() == N2.getValueType() &&
1145 N1.getValueType() == VT && "Binary operator types must match!");
1146 break;
1147
1148 case ISD::SHL:
1149 case ISD::SRA:
1150 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001151 case ISD::ROTL:
1152 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001153 assert(VT == N1.getValueType() &&
1154 "Shift operators return type must be the same as their first arg");
1155 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001156 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001157 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001158 case ISD::FP_ROUND_INREG: {
1159 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1160 assert(VT == N1.getValueType() && "Not an inreg round!");
1161 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1162 "Cannot FP_ROUND_INREG integer types");
1163 assert(EVT <= VT && "Not rounding down!");
1164 break;
1165 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001166 case ISD::AssertSext:
1167 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001168 case ISD::SIGN_EXTEND_INREG: {
1169 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1170 assert(VT == N1.getValueType() && "Not an inreg extend!");
1171 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1172 "Cannot *_EXTEND_INREG FP types");
1173 assert(EVT <= VT && "Not extending!");
1174 }
1175
Chris Lattner76365122005-01-16 02:23:22 +00001176 default: break;
1177 }
1178#endif
1179
Chris Lattnerc3aae252005-01-07 07:46:32 +00001180 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1181 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1182 if (N1C) {
1183 if (N2C) {
1184 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1185 switch (Opcode) {
1186 case ISD::ADD: return getConstant(C1 + C2, VT);
1187 case ISD::SUB: return getConstant(C1 - C2, VT);
1188 case ISD::MUL: return getConstant(C1 * C2, VT);
1189 case ISD::UDIV:
1190 if (C2) return getConstant(C1 / C2, VT);
1191 break;
1192 case ISD::UREM :
1193 if (C2) return getConstant(C1 % C2, VT);
1194 break;
1195 case ISD::SDIV :
1196 if (C2) return getConstant(N1C->getSignExtended() /
1197 N2C->getSignExtended(), VT);
1198 break;
1199 case ISD::SREM :
1200 if (C2) return getConstant(N1C->getSignExtended() %
1201 N2C->getSignExtended(), VT);
1202 break;
1203 case ISD::AND : return getConstant(C1 & C2, VT);
1204 case ISD::OR : return getConstant(C1 | C2, VT);
1205 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001206 case ISD::SHL : return getConstant(C1 << C2, VT);
1207 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001208 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001209 case ISD::ROTL :
1210 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1211 VT);
1212 case ISD::ROTR :
1213 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1214 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001215 default: break;
1216 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001217 } else { // Cannonicalize constant to RHS if commutative
1218 if (isCommutativeBinOp(Opcode)) {
1219 std::swap(N1C, N2C);
1220 std::swap(N1, N2);
1221 }
1222 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001223 }
1224
1225 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1226 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001227 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228 if (N2CFP) {
1229 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1230 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001231 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1232 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1233 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1234 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 if (C2) return getConstantFP(C1 / C2, VT);
1236 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001237 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 if (C2) return getConstantFP(fmod(C1, C2), VT);
1239 break;
1240 default: break;
1241 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001242 } else { // Cannonicalize constant to RHS if commutative
1243 if (isCommutativeBinOp(Opcode)) {
1244 std::swap(N1CFP, N2CFP);
1245 std::swap(N1, N2);
1246 }
1247 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001248 }
1249
Chris Lattnerc3aae252005-01-07 07:46:32 +00001250 // Finally, fold operations that do not require constants.
1251 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001252 case ISD::FP_ROUND_INREG:
1253 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1254 break;
1255 case ISD::SIGN_EXTEND_INREG: {
1256 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1257 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001258 break;
1259 }
1260
Nate Begemaneea805e2005-04-13 21:23:31 +00001261 // FIXME: figure out how to safely handle things like
1262 // int foo(int x) { return 1 << (x & 255); }
1263 // int bar() { return foo(256); }
1264#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001265 case ISD::SHL:
1266 case ISD::SRL:
1267 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001268 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001269 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001270 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001271 else if (N2.getOpcode() == ISD::AND)
1272 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1273 // If the and is only masking out bits that cannot effect the shift,
1274 // eliminate the and.
1275 unsigned NumBits = MVT::getSizeInBits(VT);
1276 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1277 return getNode(Opcode, VT, N1, N2.getOperand(0));
1278 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001279 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001280#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001281 }
1282
Chris Lattner27e9b412005-05-11 18:57:39 +00001283 // Memoize this node if possible.
1284 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001285 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001286 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1287 if (BON) return SDOperand(BON, 0);
1288
1289 BON = N = new SDNode(Opcode, N1, N2);
1290 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001291 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001292 }
1293
Chris Lattner3e011362005-05-14 07:45:46 +00001294 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001295 AllNodes.push_back(N);
1296 return SDOperand(N, 0);
1297}
1298
Chris Lattnerc3aae252005-01-07 07:46:32 +00001299SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1300 SDOperand N1, SDOperand N2, SDOperand N3) {
1301 // Perform various simplifications.
1302 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1303 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1304 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1305 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001306 case ISD::SETCC: {
1307 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001308 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001309 if (Simp.Val) return Simp;
1310 break;
1311 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001312 case ISD::SELECT:
1313 if (N1C)
1314 if (N1C->getValue())
1315 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001316 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317 return N3; // select false, X, Y -> Y
1318
1319 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001320 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001321 case ISD::BRCOND:
1322 if (N2C)
1323 if (N2C->getValue()) // Unconditional branch
1324 return getNode(ISD::BR, MVT::Other, N1, N3);
1325 else
1326 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001327 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 }
1329
Chris Lattner385328c2005-05-14 07:42:29 +00001330 std::vector<SDOperand> Ops;
1331 Ops.reserve(3);
1332 Ops.push_back(N1);
1333 Ops.push_back(N2);
1334 Ops.push_back(N3);
1335
Chris Lattner43247a12005-08-25 19:12:10 +00001336 // Memoize node if it doesn't produce a flag.
1337 SDNode *N;
1338 if (VT != MVT::Flag) {
1339 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1340 if (E) return SDOperand(E, 0);
1341 E = N = new SDNode(Opcode, N1, N2, N3);
1342 } else {
1343 N = new SDNode(Opcode, N1, N2, N3);
1344 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001345 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001346 AllNodes.push_back(N);
1347 return SDOperand(N, 0);
1348}
1349
1350SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001351 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001352 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001353 std::vector<SDOperand> Ops;
1354 Ops.reserve(4);
1355 Ops.push_back(N1);
1356 Ops.push_back(N2);
1357 Ops.push_back(N3);
1358 Ops.push_back(N4);
1359 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001360}
1361
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001362SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1363 SDOperand N1, SDOperand N2, SDOperand N3,
1364 SDOperand N4, SDOperand N5) {
1365 std::vector<SDOperand> Ops;
1366 Ops.reserve(5);
1367 Ops.push_back(N1);
1368 Ops.push_back(N2);
1369 Ops.push_back(N3);
1370 Ops.push_back(N4);
1371 Ops.push_back(N5);
1372 return getNode(Opcode, VT, Ops);
1373}
1374
Evan Cheng7038daf2005-12-10 00:37:58 +00001375SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1376 SDOperand Chain, SDOperand Ptr,
1377 SDOperand SV) {
1378 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1379 if (N) return SDOperand(N, 0);
1380 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1381
1382 // Loads have a token chain.
1383 setNodeValueTypes(N, VT, MVT::Other);
1384 AllNodes.push_back(N);
1385 return SDOperand(N, 0);
1386}
1387
1388SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1389 SDOperand Chain, SDOperand Ptr,
1390 SDOperand SV) {
1391 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1392 if (N) return SDOperand(N, 0);
1393 std::vector<SDOperand> Ops;
1394 Ops.reserve(5);
1395 Ops.push_back(Chain);
1396 Ops.push_back(Ptr);
1397 Ops.push_back(getConstant(Count, MVT::i32));
1398 Ops.push_back(getValueType(EVT));
1399 Ops.push_back(SV);
1400 std::vector<MVT::ValueType> VTs;
1401 VTs.reserve(2);
1402 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1403 return getNode(ISD::VLOAD, VTs, Ops);
1404}
1405
1406SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1407 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1408 MVT::ValueType EVT) {
1409 std::vector<SDOperand> Ops;
1410 Ops.reserve(4);
1411 Ops.push_back(Chain);
1412 Ops.push_back(Ptr);
1413 Ops.push_back(SV);
1414 Ops.push_back(getValueType(EVT));
1415 std::vector<MVT::ValueType> VTs;
1416 VTs.reserve(2);
1417 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1418 return getNode(Opcode, VTs, Ops);
1419}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001420
Chris Lattner0437cdd2005-05-09 04:14:13 +00001421SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001422 assert((!V || isa<PointerType>(V->getType())) &&
1423 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001424 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1425 if (N) return SDOperand(N, 0);
1426
1427 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001428 AllNodes.push_back(N);
1429 return SDOperand(N, 0);
1430}
1431
Nate Begemanacc398c2006-01-25 18:21:52 +00001432SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1433 SDOperand Chain, SDOperand Ptr,
1434 SDOperand SV) {
1435 std::vector<SDOperand> Ops;
1436 Ops.reserve(3);
1437 Ops.push_back(Chain);
1438 Ops.push_back(Ptr);
1439 Ops.push_back(SV);
1440 std::vector<MVT::ValueType> VTs;
1441 VTs.reserve(2);
1442 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1443 return getNode(ISD::VAARG, VTs, Ops);
1444}
1445
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001446SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001447 std::vector<SDOperand> &Ops) {
1448 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001449 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001450 case 1: return getNode(Opcode, VT, Ops[0]);
1451 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1452 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001453 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001454 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001455
Chris Lattner89c34632005-05-14 06:20:26 +00001456 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001457 switch (Opcode) {
1458 default: break;
1459 case ISD::BRCONDTWOWAY:
1460 if (N1C)
1461 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001462 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001463 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001464 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001465 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001466 case ISD::BRTWOWAY_CC:
1467 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1468 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1469 "LHS and RHS of comparison must have same type!");
1470 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001471 case ISD::TRUNCSTORE: {
1472 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1473 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1474#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1475 // If this is a truncating store of a constant, convert to the desired type
1476 // and store it instead.
1477 if (isa<Constant>(Ops[0])) {
1478 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1479 if (isa<Constant>(Op))
1480 N1 = Op;
1481 }
1482 // Also for ConstantFP?
1483#endif
1484 if (Ops[0].getValueType() == EVT) // Normal store?
1485 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1486 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1487 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1488 "Can't do FP-INT conversion!");
1489 break;
1490 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001491 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001492 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001493 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1494 "LHS and RHS of condition must have same type!");
1495 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1496 "True and False arms of SelectCC must have same type!");
1497 assert(Ops[2].getValueType() == VT &&
1498 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001499 break;
1500 }
1501 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001502 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001503 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1504 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001505 break;
1506 }
Chris Lattneref847df2005-04-09 03:27:28 +00001507 }
1508
Chris Lattner385328c2005-05-14 07:42:29 +00001509 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001510 SDNode *N;
1511 if (VT != MVT::Flag) {
1512 SDNode *&E =
1513 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1514 if (E) return SDOperand(E, 0);
1515 E = N = new SDNode(Opcode, Ops);
1516 } else {
1517 N = new SDNode(Opcode, Ops);
1518 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001519 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001520 AllNodes.push_back(N);
1521 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001522}
1523
Chris Lattner89c34632005-05-14 06:20:26 +00001524SDOperand SelectionDAG::getNode(unsigned Opcode,
1525 std::vector<MVT::ValueType> &ResultTys,
1526 std::vector<SDOperand> &Ops) {
1527 if (ResultTys.size() == 1)
1528 return getNode(Opcode, ResultTys[0], Ops);
1529
Chris Lattner5f056bf2005-07-10 01:55:33 +00001530 switch (Opcode) {
1531 case ISD::EXTLOAD:
1532 case ISD::SEXTLOAD:
1533 case ISD::ZEXTLOAD: {
1534 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1535 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1536 // If they are asking for an extending load from/to the same thing, return a
1537 // normal load.
1538 if (ResultTys[0] == EVT)
1539 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1540 assert(EVT < ResultTys[0] &&
1541 "Should only be an extending load, not truncating!");
1542 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1543 "Cannot sign/zero extend a FP load!");
1544 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1545 "Cannot convert from FP to Int or Int -> FP!");
1546 break;
1547 }
1548
Chris Lattnere89083a2005-05-14 07:25:05 +00001549 // FIXME: figure out how to safely handle things like
1550 // int foo(int x) { return 1 << (x & 255); }
1551 // int bar() { return foo(256); }
1552#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001553 case ISD::SRA_PARTS:
1554 case ISD::SRL_PARTS:
1555 case ISD::SHL_PARTS:
1556 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001557 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001558 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1559 else if (N3.getOpcode() == ISD::AND)
1560 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1561 // If the and is only masking out bits that cannot effect the shift,
1562 // eliminate the and.
1563 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1564 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1565 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1566 }
1567 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001568#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001569 }
Chris Lattner89c34632005-05-14 06:20:26 +00001570
Chris Lattner43247a12005-08-25 19:12:10 +00001571 // Memoize the node unless it returns a flag.
1572 SDNode *N;
1573 if (ResultTys.back() != MVT::Flag) {
1574 SDNode *&E =
1575 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1576 if (E) return SDOperand(E, 0);
1577 E = N = new SDNode(Opcode, Ops);
1578 } else {
1579 N = new SDNode(Opcode, Ops);
1580 }
Chris Lattnera3255112005-11-08 23:30:28 +00001581 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001582 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001583 return SDOperand(N, 0);
1584}
1585
Chris Lattnera3255112005-11-08 23:30:28 +00001586void SelectionDAG::setNodeValueTypes(SDNode *N,
1587 std::vector<MVT::ValueType> &RetVals) {
1588 switch (RetVals.size()) {
1589 case 0: return;
1590 case 1: N->setValueTypes(RetVals[0]); return;
1591 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1592 default: break;
1593 }
1594
1595 std::list<std::vector<MVT::ValueType> >::iterator I =
1596 std::find(VTList.begin(), VTList.end(), RetVals);
1597 if (I == VTList.end()) {
1598 VTList.push_front(RetVals);
1599 I = VTList.begin();
1600 }
1601
1602 N->setValueTypes(&(*I)[0], I->size());
1603}
1604
1605void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1606 MVT::ValueType VT2) {
1607 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1608 E = VTList.end(); I != E; ++I) {
1609 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1610 N->setValueTypes(&(*I)[0], 2);
1611 return;
1612 }
1613 }
1614 std::vector<MVT::ValueType> V;
1615 V.push_back(VT1);
1616 V.push_back(VT2);
1617 VTList.push_front(V);
1618 N->setValueTypes(&(*VTList.begin())[0], 2);
1619}
1620
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001621/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1622/// specified operands. If the resultant node already exists in the DAG,
1623/// this does not modify the specified node, instead it returns the node that
1624/// already exists. If the resultant node does not exist in the DAG, the
1625/// input node is returned. As a degenerate case, if you specify the same
1626/// input operands as the node already has, the input node is returned.
1627SDOperand SelectionDAG::
1628UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1629 SDNode *N = InN.Val;
1630 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1631
1632 // Check to see if there is no change.
1633 if (Op == N->getOperand(0)) return InN;
1634
1635 // See if the modified node already exists.
1636 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1637 if (NewSlot && *NewSlot)
1638 return SDOperand(*NewSlot, InN.ResNo);
1639
1640 // Nope it doesn't. Remove the node from it's current place in the maps.
1641 if (NewSlot)
1642 RemoveNodeFromCSEMaps(N);
1643
1644 // Now we update the operands.
1645 N->OperandList[0].Val->removeUser(N);
1646 Op.Val->addUser(N);
1647 N->OperandList[0] = Op;
1648
1649 // If this gets put into a CSE map, add it.
1650 if (NewSlot) *NewSlot = N;
1651 return InN;
1652}
1653
1654SDOperand SelectionDAG::
1655UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1656 SDNode *N = InN.Val;
1657 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1658
1659 // Check to see if there is no change.
1660 bool AnyChange = false;
1661 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1662 return InN; // No operands changed, just return the input node.
1663
1664 // See if the modified node already exists.
1665 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1666 if (NewSlot && *NewSlot)
1667 return SDOperand(*NewSlot, InN.ResNo);
1668
1669 // Nope it doesn't. Remove the node from it's current place in the maps.
1670 if (NewSlot)
1671 RemoveNodeFromCSEMaps(N);
1672
1673 // Now we update the operands.
1674 if (N->OperandList[0] != Op1) {
1675 N->OperandList[0].Val->removeUser(N);
1676 Op1.Val->addUser(N);
1677 N->OperandList[0] = Op1;
1678 }
1679 if (N->OperandList[1] != Op2) {
1680 N->OperandList[1].Val->removeUser(N);
1681 Op2.Val->addUser(N);
1682 N->OperandList[1] = Op2;
1683 }
1684
1685 // If this gets put into a CSE map, add it.
1686 if (NewSlot) *NewSlot = N;
1687 return InN;
1688}
1689
1690SDOperand SelectionDAG::
1691UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1692 std::vector<SDOperand> Ops;
1693 Ops.push_back(Op1);
1694 Ops.push_back(Op2);
1695 Ops.push_back(Op3);
1696 return UpdateNodeOperands(N, Ops);
1697}
1698
1699SDOperand SelectionDAG::
1700UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1701 SDOperand Op3, SDOperand Op4) {
1702 std::vector<SDOperand> Ops;
1703 Ops.push_back(Op1);
1704 Ops.push_back(Op2);
1705 Ops.push_back(Op3);
1706 Ops.push_back(Op4);
1707 return UpdateNodeOperands(N, Ops);
1708}
1709
1710SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001711UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1712 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1713 std::vector<SDOperand> Ops;
1714 Ops.push_back(Op1);
1715 Ops.push_back(Op2);
1716 Ops.push_back(Op3);
1717 Ops.push_back(Op4);
1718 Ops.push_back(Op5);
1719 return UpdateNodeOperands(N, Ops);
1720}
1721
1722
1723SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001724UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1725 SDNode *N = InN.Val;
1726 assert(N->getNumOperands() == Ops.size() &&
1727 "Update with wrong number of operands");
1728
1729 // Check to see if there is no change.
1730 unsigned NumOps = Ops.size();
1731 bool AnyChange = false;
1732 for (unsigned i = 0; i != NumOps; ++i) {
1733 if (Ops[i] != N->getOperand(i)) {
1734 AnyChange = true;
1735 break;
1736 }
1737 }
1738
1739 // No operands changed, just return the input node.
1740 if (!AnyChange) return InN;
1741
1742 // See if the modified node already exists.
1743 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1744 if (NewSlot && *NewSlot)
1745 return SDOperand(*NewSlot, InN.ResNo);
1746
1747 // Nope it doesn't. Remove the node from it's current place in the maps.
1748 if (NewSlot)
1749 RemoveNodeFromCSEMaps(N);
1750
1751 // Now we update the operands.
1752 for (unsigned i = 0; i != NumOps; ++i) {
1753 if (N->OperandList[i] != Ops[i]) {
1754 N->OperandList[i].Val->removeUser(N);
1755 Ops[i].Val->addUser(N);
1756 N->OperandList[i] = Ops[i];
1757 }
1758 }
1759
1760 // If this gets put into a CSE map, add it.
1761 if (NewSlot) *NewSlot = N;
1762 return InN;
1763}
1764
1765
1766
Chris Lattner149c58c2005-08-16 18:17:10 +00001767
1768/// SelectNodeTo - These are used for target selectors to *mutate* the
1769/// specified node to have the specified return type, Target opcode, and
1770/// operands. Note that target opcodes are stored as
1771/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001772///
1773/// Note that SelectNodeTo returns the resultant node. If there is already a
1774/// node of the specified opcode and operands, it returns that node instead of
1775/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001776SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1777 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001778 // If an identical node already exists, use it.
1779 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1780 if (ON) return SDOperand(ON, 0);
1781
Chris Lattner7651fa42005-08-24 23:00:29 +00001782 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001783
Chris Lattner7651fa42005-08-24 23:00:29 +00001784 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1785 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001786
1787 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001788 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001789}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001790
Chris Lattnereb19e402005-11-30 22:45:14 +00001791SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1792 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001793 // If an identical node already exists, use it.
1794 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1795 std::make_pair(Op1, VT))];
1796 if (ON) return SDOperand(ON, 0);
1797
Chris Lattner149c58c2005-08-16 18:17:10 +00001798 RemoveNodeFromCSEMaps(N);
1799 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1800 N->setValueTypes(VT);
1801 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001802
1803 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001804 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001805}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001806
Chris Lattnereb19e402005-11-30 22:45:14 +00001807SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1808 MVT::ValueType VT, SDOperand Op1,
1809 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001810 // If an identical node already exists, use it.
1811 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1812 std::make_pair(Op1, Op2))];
1813 if (ON) return SDOperand(ON, 0);
1814
Chris Lattner149c58c2005-08-16 18:17:10 +00001815 RemoveNodeFromCSEMaps(N);
1816 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1817 N->setValueTypes(VT);
1818 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001819
1820 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001821 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001822}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001823
Chris Lattnereb19e402005-11-30 22:45:14 +00001824SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1825 MVT::ValueType VT, SDOperand Op1,
1826 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001827 // If an identical node already exists, use it.
1828 std::vector<SDOperand> OpList;
1829 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1830 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1831 std::make_pair(VT, OpList))];
1832 if (ON) return SDOperand(ON, 0);
1833
Chris Lattner149c58c2005-08-16 18:17:10 +00001834 RemoveNodeFromCSEMaps(N);
1835 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1836 N->setValueTypes(VT);
1837 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001838
1839 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001840 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001841}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001842
Chris Lattnereb19e402005-11-30 22:45:14 +00001843SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1844 MVT::ValueType VT, SDOperand Op1,
1845 SDOperand Op2, SDOperand Op3,
1846 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001847 // If an identical node already exists, use it.
1848 std::vector<SDOperand> OpList;
1849 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1850 OpList.push_back(Op4);
1851 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1852 std::make_pair(VT, OpList))];
1853 if (ON) return SDOperand(ON, 0);
1854
Nate Begeman294a0a12005-08-18 07:30:15 +00001855 RemoveNodeFromCSEMaps(N);
1856 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1857 N->setValueTypes(VT);
1858 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001859
1860 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001861 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001862}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001863
Chris Lattnereb19e402005-11-30 22:45:14 +00001864SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1865 MVT::ValueType VT, SDOperand Op1,
1866 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1867 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001868 // If an identical node already exists, use it.
1869 std::vector<SDOperand> OpList;
1870 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1871 OpList.push_back(Op4); OpList.push_back(Op5);
1872 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1873 std::make_pair(VT, OpList))];
1874 if (ON) return SDOperand(ON, 0);
1875
Chris Lattner6b09a292005-08-21 18:49:33 +00001876 RemoveNodeFromCSEMaps(N);
1877 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1878 N->setValueTypes(VT);
1879 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001880
1881 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001882 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001883}
Chris Lattner149c58c2005-08-16 18:17:10 +00001884
Chris Lattnereb19e402005-11-30 22:45:14 +00001885SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1886 MVT::ValueType VT, SDOperand Op1,
1887 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1888 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001889 // If an identical node already exists, use it.
1890 std::vector<SDOperand> OpList;
1891 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1892 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1893 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1894 std::make_pair(VT, OpList))];
1895 if (ON) return SDOperand(ON, 0);
1896
Evan Cheng61ca74b2005-11-30 02:04:11 +00001897 RemoveNodeFromCSEMaps(N);
1898 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1899 N->setValueTypes(VT);
1900 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001901
1902 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001903 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001904}
1905
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001906SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1907 MVT::ValueType VT, SDOperand Op1,
1908 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1909 SDOperand Op5, SDOperand Op6,
1910 SDOperand Op7) {
1911 // If an identical node already exists, use it.
1912 std::vector<SDOperand> OpList;
1913 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1914 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1915 OpList.push_back(Op7);
1916 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1917 std::make_pair(VT, OpList))];
1918 if (ON) return SDOperand(ON, 0);
1919
1920 RemoveNodeFromCSEMaps(N);
1921 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1922 N->setValueTypes(VT);
1923 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1924
1925 ON = N; // Memoize the new node.
1926 return SDOperand(N, 0);
1927}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001928SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1929 MVT::ValueType VT, SDOperand Op1,
1930 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1931 SDOperand Op5, SDOperand Op6,
1932 SDOperand Op7, SDOperand Op8) {
1933 // If an identical node already exists, use it.
1934 std::vector<SDOperand> OpList;
1935 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1936 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1937 OpList.push_back(Op7); OpList.push_back(Op8);
1938 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1939 std::make_pair(VT, OpList))];
1940 if (ON) return SDOperand(ON, 0);
1941
1942 RemoveNodeFromCSEMaps(N);
1943 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1944 N->setValueTypes(VT);
1945 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1946
1947 ON = N; // Memoize the new node.
1948 return SDOperand(N, 0);
1949}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001950
Chris Lattnereb19e402005-11-30 22:45:14 +00001951SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1952 MVT::ValueType VT1, MVT::ValueType VT2,
1953 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001954 // If an identical node already exists, use it.
1955 std::vector<SDOperand> OpList;
1956 OpList.push_back(Op1); OpList.push_back(Op2);
1957 std::vector<MVT::ValueType> VTList;
1958 VTList.push_back(VT1); VTList.push_back(VT2);
1959 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1960 std::make_pair(VTList, OpList))];
1961 if (ON) return SDOperand(ON, 0);
1962
Chris Lattner0fb094f2005-11-19 01:44:53 +00001963 RemoveNodeFromCSEMaps(N);
1964 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1965 setNodeValueTypes(N, VT1, VT2);
1966 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001967
1968 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001969 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001970}
1971
Chris Lattnereb19e402005-11-30 22:45:14 +00001972SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1973 MVT::ValueType VT1, MVT::ValueType VT2,
1974 SDOperand Op1, SDOperand Op2,
1975 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001976 // If an identical node already exists, use it.
1977 std::vector<SDOperand> OpList;
1978 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1979 std::vector<MVT::ValueType> VTList;
1980 VTList.push_back(VT1); VTList.push_back(VT2);
1981 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1982 std::make_pair(VTList, OpList))];
1983 if (ON) return SDOperand(ON, 0);
1984
Chris Lattner0fb094f2005-11-19 01:44:53 +00001985 RemoveNodeFromCSEMaps(N);
1986 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1987 setNodeValueTypes(N, VT1, VT2);
1988 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001989
1990 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001991 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001992}
1993
Chris Lattnereb19e402005-11-30 22:45:14 +00001994SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1995 MVT::ValueType VT1, MVT::ValueType VT2,
1996 SDOperand Op1, SDOperand Op2,
1997 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001998 // If an identical node already exists, use it.
1999 std::vector<SDOperand> OpList;
2000 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2001 OpList.push_back(Op4);
2002 std::vector<MVT::ValueType> VTList;
2003 VTList.push_back(VT1); VTList.push_back(VT2);
2004 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2005 std::make_pair(VTList, OpList))];
2006 if (ON) return SDOperand(ON, 0);
2007
Chris Lattner0fb094f2005-11-19 01:44:53 +00002008 RemoveNodeFromCSEMaps(N);
2009 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2010 setNodeValueTypes(N, VT1, VT2);
2011 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002012
2013 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002014 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002015}
2016
Chris Lattnereb19e402005-11-30 22:45:14 +00002017SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2018 MVT::ValueType VT1, MVT::ValueType VT2,
2019 SDOperand Op1, SDOperand Op2,
2020 SDOperand Op3, SDOperand Op4,
2021 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002022 // If an identical node already exists, use it.
2023 std::vector<SDOperand> OpList;
2024 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2025 OpList.push_back(Op4); OpList.push_back(Op5);
2026 std::vector<MVT::ValueType> VTList;
2027 VTList.push_back(VT1); VTList.push_back(VT2);
2028 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2029 std::make_pair(VTList, OpList))];
2030 if (ON) return SDOperand(ON, 0);
2031
Chris Lattner0fb094f2005-11-19 01:44:53 +00002032 RemoveNodeFromCSEMaps(N);
2033 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2034 setNodeValueTypes(N, VT1, VT2);
2035 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002036
2037 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002038 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002039}
2040
Evan Cheng6ae46c42006-02-09 07:15:23 +00002041/// getTargetNode - These are used for target selectors to create a new node
2042/// with specified return type(s), target opcode, and operands.
2043///
2044/// Note that getTargetNode returns the resultant node. If there is already a
2045/// node of the specified opcode and operands, it returns that node instead of
2046/// the current one.
2047SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2048 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2049}
2050SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2051 SDOperand Op1) {
2052 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2053}
2054SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2055 SDOperand Op1, SDOperand Op2) {
2056 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2057}
2058SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2059 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2060 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2061}
2062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2063 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2064 SDOperand Op4) {
2065 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2066}
2067SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2068 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2069 SDOperand Op4, SDOperand Op5) {
2070 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2071}
2072SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2073 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2074 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2075 std::vector<SDOperand> Ops;
2076 Ops.reserve(6);
2077 Ops.push_back(Op1);
2078 Ops.push_back(Op2);
2079 Ops.push_back(Op3);
2080 Ops.push_back(Op4);
2081 Ops.push_back(Op5);
2082 Ops.push_back(Op6);
2083 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2084}
2085SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2086 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2087 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2088 SDOperand Op7) {
2089 std::vector<SDOperand> Ops;
2090 Ops.reserve(7);
2091 Ops.push_back(Op1);
2092 Ops.push_back(Op2);
2093 Ops.push_back(Op3);
2094 Ops.push_back(Op4);
2095 Ops.push_back(Op5);
2096 Ops.push_back(Op6);
2097 Ops.push_back(Op7);
2098 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2099}
2100SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2101 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2102 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2103 SDOperand Op7, SDOperand Op8) {
2104 std::vector<SDOperand> Ops;
2105 Ops.reserve(8);
2106 Ops.push_back(Op1);
2107 Ops.push_back(Op2);
2108 Ops.push_back(Op3);
2109 Ops.push_back(Op4);
2110 Ops.push_back(Op5);
2111 Ops.push_back(Op6);
2112 Ops.push_back(Op7);
2113 Ops.push_back(Op8);
2114 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2115}
2116SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2117 std::vector<SDOperand> &Ops) {
2118 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2119}
2120SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2121 MVT::ValueType VT2, SDOperand Op1) {
2122 std::vector<MVT::ValueType> ResultTys;
2123 ResultTys.push_back(VT1);
2124 ResultTys.push_back(VT2);
2125 std::vector<SDOperand> Ops;
2126 Ops.push_back(Op1);
2127 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2128}
2129SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2130 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2131 std::vector<MVT::ValueType> ResultTys;
2132 ResultTys.push_back(VT1);
2133 ResultTys.push_back(VT2);
2134 std::vector<SDOperand> Ops;
2135 Ops.push_back(Op1);
2136 Ops.push_back(Op2);
2137 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2138}
2139SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2140 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2141 SDOperand Op3) {
2142 std::vector<MVT::ValueType> ResultTys;
2143 ResultTys.push_back(VT1);
2144 ResultTys.push_back(VT2);
2145 std::vector<SDOperand> Ops;
2146 Ops.push_back(Op1);
2147 Ops.push_back(Op2);
2148 Ops.push_back(Op3);
2149 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2150}
2151SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2152 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2153 SDOperand Op3, SDOperand Op4) {
2154 std::vector<MVT::ValueType> ResultTys;
2155 ResultTys.push_back(VT1);
2156 ResultTys.push_back(VT2);
2157 std::vector<SDOperand> Ops;
2158 Ops.push_back(Op1);
2159 Ops.push_back(Op2);
2160 Ops.push_back(Op3);
2161 Ops.push_back(Op4);
2162 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2163}
2164SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2165 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2166 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2167 std::vector<MVT::ValueType> ResultTys;
2168 ResultTys.push_back(VT1);
2169 ResultTys.push_back(VT2);
2170 std::vector<SDOperand> Ops;
2171 Ops.push_back(Op1);
2172 Ops.push_back(Op2);
2173 Ops.push_back(Op3);
2174 Ops.push_back(Op4);
2175 Ops.push_back(Op5);
2176 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2177}
2178SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2179 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2180 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2181 SDOperand Op6) {
2182 std::vector<MVT::ValueType> ResultTys;
2183 ResultTys.push_back(VT1);
2184 ResultTys.push_back(VT2);
2185 std::vector<SDOperand> Ops;
2186 Ops.push_back(Op1);
2187 Ops.push_back(Op2);
2188 Ops.push_back(Op3);
2189 Ops.push_back(Op4);
2190 Ops.push_back(Op5);
2191 Ops.push_back(Op6);
2192 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2193}
2194SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2195 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2196 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2197 SDOperand Op6, SDOperand Op7) {
2198 std::vector<MVT::ValueType> ResultTys;
2199 ResultTys.push_back(VT1);
2200 ResultTys.push_back(VT2);
2201 std::vector<SDOperand> Ops;
2202 Ops.push_back(Op1);
2203 Ops.push_back(Op2);
2204 Ops.push_back(Op3);
2205 Ops.push_back(Op4);
2206 Ops.push_back(Op5);
2207 Ops.push_back(Op6);
2208 Ops.push_back(Op7);
2209 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2210}
2211SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2212 MVT::ValueType VT2, MVT::ValueType VT3,
2213 SDOperand Op1, SDOperand Op2) {
2214 std::vector<MVT::ValueType> ResultTys;
2215 ResultTys.push_back(VT1);
2216 ResultTys.push_back(VT2);
2217 ResultTys.push_back(VT3);
2218 std::vector<SDOperand> Ops;
2219 Ops.push_back(Op1);
2220 Ops.push_back(Op2);
2221 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2222}
2223SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2224 MVT::ValueType VT2, MVT::ValueType VT3,
2225 SDOperand Op1, SDOperand Op2,
2226 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2227 std::vector<MVT::ValueType> ResultTys;
2228 ResultTys.push_back(VT1);
2229 ResultTys.push_back(VT2);
2230 ResultTys.push_back(VT3);
2231 std::vector<SDOperand> Ops;
2232 Ops.push_back(Op1);
2233 Ops.push_back(Op2);
2234 Ops.push_back(Op3);
2235 Ops.push_back(Op4);
2236 Ops.push_back(Op5);
2237 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2238}
2239SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2240 MVT::ValueType VT2, MVT::ValueType VT3,
2241 SDOperand Op1, SDOperand Op2,
2242 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2243 SDOperand Op6) {
2244 std::vector<MVT::ValueType> ResultTys;
2245 ResultTys.push_back(VT1);
2246 ResultTys.push_back(VT2);
2247 ResultTys.push_back(VT3);
2248 std::vector<SDOperand> Ops;
2249 Ops.push_back(Op1);
2250 Ops.push_back(Op2);
2251 Ops.push_back(Op3);
2252 Ops.push_back(Op4);
2253 Ops.push_back(Op5);
2254 Ops.push_back(Op6);
2255 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2256}
2257SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2258 MVT::ValueType VT2, MVT::ValueType VT3,
2259 SDOperand Op1, SDOperand Op2,
2260 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2261 SDOperand Op6, SDOperand Op7) {
2262 std::vector<MVT::ValueType> ResultTys;
2263 ResultTys.push_back(VT1);
2264 ResultTys.push_back(VT2);
2265 ResultTys.push_back(VT3);
2266 std::vector<SDOperand> Ops;
2267 Ops.push_back(Op1);
2268 Ops.push_back(Op2);
2269 Ops.push_back(Op3);
2270 Ops.push_back(Op4);
2271 Ops.push_back(Op5);
2272 Ops.push_back(Op6);
2273 Ops.push_back(Op7);
2274 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2275}
2276SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2277 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2278 std::vector<MVT::ValueType> ResultTys;
2279 ResultTys.push_back(VT1);
2280 ResultTys.push_back(VT2);
2281 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2282}
2283
Chris Lattner0fb094f2005-11-19 01:44:53 +00002284// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002285/// This can cause recursive merging of nodes in the DAG.
2286///
Chris Lattner8b52f212005-08-26 18:36:28 +00002287/// This version assumes From/To have a single result value.
2288///
Chris Lattner1e111c72005-09-07 05:37:01 +00002289void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2290 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002291 SDNode *From = FromN.Val, *To = ToN.Val;
2292 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2293 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002294 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002295
Chris Lattner8b8749f2005-08-17 19:00:20 +00002296 while (!From->use_empty()) {
2297 // Process users until they are all gone.
2298 SDNode *U = *From->use_begin();
2299
2300 // This node is about to morph, remove its old self from the CSE maps.
2301 RemoveNodeFromCSEMaps(U);
2302
Chris Lattner65113b22005-11-08 22:07:03 +00002303 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2304 I != E; ++I)
2305 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002306 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002307 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002308 To->addUser(U);
2309 }
2310
2311 // Now that we have modified U, add it back to the CSE maps. If it already
2312 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002313 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2314 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002315 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002316 if (Deleted) Deleted->push_back(U);
2317 DeleteNodeNotInCSEMaps(U);
2318 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002319 }
2320}
2321
2322/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2323/// This can cause recursive merging of nodes in the DAG.
2324///
2325/// This version assumes From/To have matching types and numbers of result
2326/// values.
2327///
Chris Lattner1e111c72005-09-07 05:37:01 +00002328void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2329 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002330 assert(From != To && "Cannot replace uses of with self");
2331 assert(From->getNumValues() == To->getNumValues() &&
2332 "Cannot use this version of ReplaceAllUsesWith!");
2333 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002334 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002335 return;
2336 }
2337
2338 while (!From->use_empty()) {
2339 // Process users until they are all gone.
2340 SDNode *U = *From->use_begin();
2341
2342 // This node is about to morph, remove its old self from the CSE maps.
2343 RemoveNodeFromCSEMaps(U);
2344
Chris Lattner65113b22005-11-08 22:07:03 +00002345 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2346 I != E; ++I)
2347 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002348 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002349 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002350 To->addUser(U);
2351 }
2352
2353 // Now that we have modified U, add it back to the CSE maps. If it already
2354 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002355 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2356 ReplaceAllUsesWith(U, Existing, Deleted);
2357 // U is now dead.
2358 if (Deleted) Deleted->push_back(U);
2359 DeleteNodeNotInCSEMaps(U);
2360 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002361 }
2362}
2363
Chris Lattner8b52f212005-08-26 18:36:28 +00002364/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2365/// This can cause recursive merging of nodes in the DAG.
2366///
2367/// This version can replace From with any result values. To must match the
2368/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002369void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002370 const std::vector<SDOperand> &To,
2371 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002372 assert(From->getNumValues() == To.size() &&
2373 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002374 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002375 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002376 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002377 return;
2378 }
2379
2380 while (!From->use_empty()) {
2381 // Process users until they are all gone.
2382 SDNode *U = *From->use_begin();
2383
2384 // This node is about to morph, remove its old self from the CSE maps.
2385 RemoveNodeFromCSEMaps(U);
2386
Chris Lattner65113b22005-11-08 22:07:03 +00002387 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2388 I != E; ++I)
2389 if (I->Val == From) {
2390 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002391 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002392 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002393 ToOp.Val->addUser(U);
2394 }
2395
2396 // Now that we have modified U, add it back to the CSE maps. If it already
2397 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002398 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2399 ReplaceAllUsesWith(U, Existing, Deleted);
2400 // U is now dead.
2401 if (Deleted) Deleted->push_back(U);
2402 DeleteNodeNotInCSEMaps(U);
2403 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002404 }
2405}
2406
Chris Lattner012f2412006-02-17 21:58:01 +00002407/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2408/// uses of other values produced by From.Val alone. The Deleted vector is
2409/// handled the same was as for ReplaceAllUsesWith.
2410void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2411 std::vector<SDNode*> &Deleted) {
2412 assert(From != To && "Cannot replace a value with itself");
2413 // Handle the simple, trivial, case efficiently.
2414 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2415 ReplaceAllUsesWith(From, To, &Deleted);
2416 return;
2417 }
2418
2419 // Get all of the users in a nice, deterministically ordered, uniqued set.
2420 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2421
2422 while (!Users.empty()) {
2423 // We know that this user uses some value of From. If it is the right
2424 // value, update it.
2425 SDNode *User = Users.back();
2426 Users.pop_back();
2427
2428 for (SDOperand *Op = User->OperandList,
2429 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2430 if (*Op == From) {
2431 // Okay, we know this user needs to be updated. Remove its old self
2432 // from the CSE maps.
2433 RemoveNodeFromCSEMaps(User);
2434
2435 // Update all operands that match "From".
2436 for (; Op != E; ++Op) {
2437 if (*Op == From) {
2438 From.Val->removeUser(User);
2439 *Op = To;
2440 To.Val->addUser(User);
2441 }
2442 }
2443
2444 // Now that we have modified User, add it back to the CSE maps. If it
2445 // already exists there, recursively merge the results together.
2446 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2447 unsigned NumDeleted = Deleted.size();
2448 ReplaceAllUsesWith(User, Existing, &Deleted);
2449
2450 // User is now dead.
2451 Deleted.push_back(User);
2452 DeleteNodeNotInCSEMaps(User);
2453
2454 // We have to be careful here, because ReplaceAllUsesWith could have
2455 // deleted a user of From, which means there may be dangling pointers
2456 // in the "Users" setvector. Scan over the deleted node pointers and
2457 // remove them from the setvector.
2458 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2459 Users.remove(Deleted[i]);
2460 }
2461 break; // Exit the operand scanning loop.
2462 }
2463 }
2464 }
2465}
2466
Chris Lattner7b2880c2005-08-24 22:44:39 +00002467
Jim Laskey58b968b2005-08-17 20:08:02 +00002468//===----------------------------------------------------------------------===//
2469// SDNode Class
2470//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002471
Chris Lattnera3255112005-11-08 23:30:28 +00002472
2473/// getValueTypeList - Return a pointer to the specified value type.
2474///
2475MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2476 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2477 VTs[VT] = VT;
2478 return &VTs[VT];
2479}
2480
Chris Lattner5c884562005-01-12 18:37:47 +00002481/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2482/// indicated value. This method ignores uses of other values defined by this
2483/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002484bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002485 assert(Value < getNumValues() && "Bad value!");
2486
2487 // If there is only one value, this is easy.
2488 if (getNumValues() == 1)
2489 return use_size() == NUses;
2490 if (Uses.size() < NUses) return false;
2491
Evan Cheng4ee62112006-02-05 06:29:23 +00002492 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002493
2494 std::set<SDNode*> UsersHandled;
2495
Evan Cheng4ee62112006-02-05 06:29:23 +00002496 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002497 UI != E; ++UI) {
2498 SDNode *User = *UI;
2499 if (User->getNumOperands() == 1 ||
2500 UsersHandled.insert(User).second) // First time we've seen this?
2501 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2502 if (User->getOperand(i) == TheValue) {
2503 if (NUses == 0)
2504 return false; // too many uses
2505 --NUses;
2506 }
2507 }
2508
2509 // Found exactly the right number of uses?
2510 return NUses == 0;
2511}
2512
2513
Evan Cheng4ee62112006-02-05 06:29:23 +00002514// isOnlyUse - Return true if this node is the only use of N.
2515bool SDNode::isOnlyUse(SDNode *N) const {
2516 bool Seen = false;
2517 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2518 SDNode *User = *I;
2519 if (User == this)
2520 Seen = true;
2521 else
2522 return false;
2523 }
2524
2525 return Seen;
2526}
2527
2528
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002529const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002530 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002531 default:
2532 if (getOpcode() < ISD::BUILTIN_OP_END)
2533 return "<<Unknown DAG Node>>";
2534 else {
Evan Cheng72261582005-12-20 06:22:03 +00002535 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002536 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002537 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2538 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002539
Evan Cheng72261582005-12-20 06:22:03 +00002540 TargetLowering &TLI = G->getTargetLoweringInfo();
2541 const char *Name =
2542 TLI.getTargetNodeName(getOpcode());
2543 if (Name) return Name;
2544 }
2545
2546 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002547 }
2548
Andrew Lenharth95762122005-03-31 21:24:06 +00002549 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002550 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002551 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002552 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00002553 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002554 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002555 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002556 case ISD::AssertSext: return "AssertSext";
2557 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002558 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002559 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002560 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00002561 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002562 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002563 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002564 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002565 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002566 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002567 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002568 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00002569 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002570 case ISD::ConstantPool: return "ConstantPool";
2571 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002572 case ISD::CopyToReg: return "CopyToReg";
2573 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002574 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002575 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002576 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002577 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002578
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002579 // Unary operators
2580 case ISD::FABS: return "fabs";
2581 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002582 case ISD::FSQRT: return "fsqrt";
2583 case ISD::FSIN: return "fsin";
2584 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002585
2586 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002587 case ISD::ADD: return "add";
2588 case ISD::SUB: return "sub";
2589 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002590 case ISD::MULHU: return "mulhu";
2591 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002592 case ISD::SDIV: return "sdiv";
2593 case ISD::UDIV: return "udiv";
2594 case ISD::SREM: return "srem";
2595 case ISD::UREM: return "urem";
2596 case ISD::AND: return "and";
2597 case ISD::OR: return "or";
2598 case ISD::XOR: return "xor";
2599 case ISD::SHL: return "shl";
2600 case ISD::SRA: return "sra";
2601 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002602 case ISD::ROTL: return "rotl";
2603 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002604 case ISD::FADD: return "fadd";
2605 case ISD::FSUB: return "fsub";
2606 case ISD::FMUL: return "fmul";
2607 case ISD::FDIV: return "fdiv";
2608 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002609 case ISD::VADD: return "vadd";
2610 case ISD::VSUB: return "vsub";
2611 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002612
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002613 case ISD::SETCC: return "setcc";
2614 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002615 case ISD::SELECT_CC: return "select_cc";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002616 case ISD::ADDC: return "addc";
2617 case ISD::ADDE: return "adde";
2618 case ISD::SUBC: return "subc";
2619 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002620 case ISD::SHL_PARTS: return "shl_parts";
2621 case ISD::SRA_PARTS: return "sra_parts";
2622 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002623
Chris Lattner7f644642005-04-28 21:44:03 +00002624 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002625 case ISD::SIGN_EXTEND: return "sign_extend";
2626 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002627 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002628 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002629 case ISD::TRUNCATE: return "truncate";
2630 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002631 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002632 case ISD::FP_EXTEND: return "fp_extend";
2633
2634 case ISD::SINT_TO_FP: return "sint_to_fp";
2635 case ISD::UINT_TO_FP: return "uint_to_fp";
2636 case ISD::FP_TO_SINT: return "fp_to_sint";
2637 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002638 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002639
2640 // Control flow instructions
2641 case ISD::BR: return "br";
2642 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002643 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002644 case ISD::BR_CC: return "br_cc";
2645 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002646 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002647 case ISD::CALLSEQ_START: return "callseq_start";
2648 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002649
2650 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002651 case ISD::LOAD: return "load";
2652 case ISD::STORE: return "store";
2653 case ISD::VLOAD: return "vload";
2654 case ISD::EXTLOAD: return "extload";
2655 case ISD::SEXTLOAD: return "sextload";
2656 case ISD::ZEXTLOAD: return "zextload";
2657 case ISD::TRUNCSTORE: return "truncstore";
2658 case ISD::VAARG: return "vaarg";
2659 case ISD::VACOPY: return "vacopy";
2660 case ISD::VAEND: return "vaend";
2661 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002662 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002663 case ISD::EXTRACT_ELEMENT: return "extract_element";
2664 case ISD::BUILD_PAIR: return "build_pair";
2665 case ISD::STACKSAVE: return "stacksave";
2666 case ISD::STACKRESTORE: return "stackrestore";
2667
2668 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002669 case ISD::MEMSET: return "memset";
2670 case ISD::MEMCPY: return "memcpy";
2671 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002672
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002673 // Bit manipulation
2674 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002675 case ISD::CTPOP: return "ctpop";
2676 case ISD::CTTZ: return "cttz";
2677 case ISD::CTLZ: return "ctlz";
2678
2679 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002680 case ISD::READPORT: return "readport";
2681 case ISD::WRITEPORT: return "writeport";
2682 case ISD::READIO: return "readio";
2683 case ISD::WRITEIO: return "writeio";
2684
Chris Lattner36ce6912005-11-29 06:21:05 +00002685 // Debug info
2686 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002687 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002688 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002689
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002690 case ISD::CONDCODE:
2691 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002692 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002693 case ISD::SETOEQ: return "setoeq";
2694 case ISD::SETOGT: return "setogt";
2695 case ISD::SETOGE: return "setoge";
2696 case ISD::SETOLT: return "setolt";
2697 case ISD::SETOLE: return "setole";
2698 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002699
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002700 case ISD::SETO: return "seto";
2701 case ISD::SETUO: return "setuo";
2702 case ISD::SETUEQ: return "setue";
2703 case ISD::SETUGT: return "setugt";
2704 case ISD::SETUGE: return "setuge";
2705 case ISD::SETULT: return "setult";
2706 case ISD::SETULE: return "setule";
2707 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002708
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002709 case ISD::SETEQ: return "seteq";
2710 case ISD::SETGT: return "setgt";
2711 case ISD::SETGE: return "setge";
2712 case ISD::SETLT: return "setlt";
2713 case ISD::SETLE: return "setle";
2714 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002715 }
2716 }
2717}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002718
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002719void SDNode::dump() const { dump(0); }
2720void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002721 std::cerr << (void*)this << ": ";
2722
2723 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2724 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002725 if (getValueType(i) == MVT::Other)
2726 std::cerr << "ch";
2727 else
2728 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002729 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002730 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002731
2732 std::cerr << " ";
2733 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2734 if (i) std::cerr << ", ";
2735 std::cerr << (void*)getOperand(i).Val;
2736 if (unsigned RN = getOperand(i).ResNo)
2737 std::cerr << ":" << RN;
2738 }
2739
2740 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2741 std::cerr << "<" << CSDN->getValue() << ">";
2742 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2743 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002744 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002745 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002746 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002747 std::cerr << "<";
2748 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002749 if (offset > 0)
2750 std::cerr << " + " << offset;
2751 else
2752 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002753 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002754 std::cerr << "<" << FIDN->getIndex() << ">";
2755 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002756 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002757 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002758 std::cerr << "<";
2759 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2760 if (LBB)
2761 std::cerr << LBB->getName() << " ";
2762 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002763 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002764 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002765 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2766 } else {
2767 std::cerr << " #" << R->getReg();
2768 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002769 } else if (const ExternalSymbolSDNode *ES =
2770 dyn_cast<ExternalSymbolSDNode>(this)) {
2771 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002772 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2773 if (M->getValue())
2774 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2775 else
2776 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002777 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2778 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002779 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002780}
2781
Chris Lattnerde202b32005-11-09 23:47:37 +00002782static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002783 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2784 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002785 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002786 else
2787 std::cerr << "\n" << std::string(indent+2, ' ')
2788 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002789
Chris Lattnerea946cd2005-01-09 20:38:33 +00002790
2791 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002792 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002793}
2794
Chris Lattnerc3aae252005-01-07 07:46:32 +00002795void SelectionDAG::dump() const {
2796 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002797 std::vector<const SDNode*> Nodes;
2798 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2799 I != E; ++I)
2800 Nodes.push_back(I);
2801
Chris Lattner49d24712005-01-09 20:26:36 +00002802 std::sort(Nodes.begin(), Nodes.end());
2803
2804 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002805 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002806 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002807 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002808
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002809 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002810
Chris Lattnerc3aae252005-01-07 07:46:32 +00002811 std::cerr << "\n\n";
2812}
2813
Evan Chengfae9f1c2006-02-09 22:11:03 +00002814/// InsertISelMapEntry - A helper function to insert a key / element pair
2815/// into a SDOperand to SDOperand map. This is added to avoid the map
2816/// insertion operator from being inlined.
2817void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2818 SDNode *Key, unsigned KeyResNo,
2819 SDNode *Element, unsigned ElementResNo) {
2820 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2821 SDOperand(Element, ElementResNo)));
2822}