blob: 57f10663d03fe7414e600658d137d1c51850c262 [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(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000305 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
306 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000308 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000309 Erased = TargetConstantPoolIndices.
310 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000311 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
312 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000313 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000314 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000315 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000316 break;
317 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000318 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000319 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000320 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000321 Erased =
322 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000323 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000324 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000325 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000326 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
327 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000328 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000329 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
330 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000331 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000332 case ISD::SRCVALUE: {
333 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000335 break;
336 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000337 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000338 Erased = Loads.erase(std::make_pair(N->getOperand(1),
339 std::make_pair(N->getOperand(0),
340 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000341 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000342 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000343 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000344 if (N->getNumOperands() == 0) {
345 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
346 N->getValueType(0)));
347 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000348 Erased =
349 UnaryOps.erase(std::make_pair(N->getOpcode(),
350 std::make_pair(N->getOperand(0),
351 N->getValueType(0))));
352 } else if (N->getNumOperands() == 2) {
353 Erased =
354 BinaryOps.erase(std::make_pair(N->getOpcode(),
355 std::make_pair(N->getOperand(0),
356 N->getOperand(1))));
357 } else {
358 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
359 Erased =
360 OneResultNodes.erase(std::make_pair(N->getOpcode(),
361 std::make_pair(N->getValueType(0),
362 Ops)));
363 }
Chris Lattner385328c2005-05-14 07:42:29 +0000364 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000365 // Remove the node from the ArbitraryNodes map.
366 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
367 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000368 Erased =
369 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
370 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000371 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000372 break;
373 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000374#ifndef NDEBUG
375 // Verify that the node was actually in one of the CSE maps, unless it has a
376 // flag result (which cannot be CSE'd) or is one of the special cases that are
377 // not subject to CSE.
378 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000379 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000380 N->dump();
381 assert(0 && "Node is not in map!");
382 }
383#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384}
385
Chris Lattner8b8749f2005-08-17 19:00:20 +0000386/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
387/// has been taken out and modified in some way. If the specified node already
388/// exists in the CSE maps, do not modify the maps, but return the existing node
389/// instead. If it doesn't exist, add it and return null.
390///
391SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000393 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000394 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000395
Chris Lattner9f8cc692005-12-19 22:21:21 +0000396 // Check that remaining values produced are not flags.
397 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
398 if (N->getValueType(i) == MVT::Flag)
399 return 0; // Never CSE anything that produces a flag.
400
Chris Lattnerfe14b342005-12-01 23:14:50 +0000401 if (N->getNumValues() == 1) {
402 if (N->getNumOperands() == 1) {
403 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
404 std::make_pair(N->getOperand(0),
405 N->getValueType(0)))];
406 if (U) return U;
407 U = N;
408 } else if (N->getNumOperands() == 2) {
409 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
410 std::make_pair(N->getOperand(0),
411 N->getOperand(1)))];
412 if (B) return B;
413 B = N;
414 } else {
415 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
416 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000417 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000418 if (ORN) return ORN;
419 ORN = N;
420 }
421 } else {
422 if (N->getOpcode() == ISD::LOAD) {
423 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
424 std::make_pair(N->getOperand(0),
425 N->getValueType(0)))];
426 if (L) return L;
427 L = N;
428 } else {
429 // Remove the node from the ArbitraryNodes map.
430 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
431 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
432 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
433 std::make_pair(RV, Ops))];
434 if (AN) return AN;
435 AN = N;
436 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000437 }
438 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000439}
440
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000441/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
442/// were replaced with those specified. If this node is never memoized,
443/// return null, otherwise return a pointer to the slot it would take. If a
444/// node already exists with these operands, the slot will be non-null.
445SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000446 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000447 return 0; // Never add these nodes.
448
449 // Check that remaining values produced are not flags.
450 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
451 if (N->getValueType(i) == MVT::Flag)
452 return 0; // Never CSE anything that produces a flag.
453
454 if (N->getNumValues() == 1) {
455 return &UnaryOps[std::make_pair(N->getOpcode(),
456 std::make_pair(Op, N->getValueType(0)))];
457 } else {
458 // Remove the node from the ArbitraryNodes map.
459 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
460 std::vector<SDOperand> Ops;
461 Ops.push_back(Op);
462 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
463 std::make_pair(RV, Ops))];
464 }
465 return 0;
466}
467
468/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
469/// were replaced with those specified. If this node is never memoized,
470/// return null, otherwise return a pointer to the slot it would take. If a
471/// node already exists with these operands, the slot will be non-null.
472SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
473 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000474 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000475 return 0; // Never add these nodes.
476
477 // Check that remaining values produced are not flags.
478 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
479 if (N->getValueType(i) == MVT::Flag)
480 return 0; // Never CSE anything that produces a flag.
481
482 if (N->getNumValues() == 1) {
483 return &BinaryOps[std::make_pair(N->getOpcode(),
484 std::make_pair(Op1, Op2))];
485 } else {
486 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
487 std::vector<SDOperand> Ops;
488 Ops.push_back(Op1);
489 Ops.push_back(Op2);
490 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
491 std::make_pair(RV, Ops))];
492 }
493 return 0;
494}
495
496
497/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
498/// were replaced with those specified. If this node is never memoized,
499/// return null, otherwise return a pointer to the slot it would take. If a
500/// node already exists with these operands, the slot will be non-null.
501SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
502 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000503 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000504 return 0; // Never add these nodes.
505
506 // Check that remaining values produced are not flags.
507 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
508 if (N->getValueType(i) == MVT::Flag)
509 return 0; // Never CSE anything that produces a flag.
510
511 if (N->getNumValues() == 1) {
512 if (N->getNumOperands() == 1) {
513 return &UnaryOps[std::make_pair(N->getOpcode(),
514 std::make_pair(Ops[0],
515 N->getValueType(0)))];
516 } else if (N->getNumOperands() == 2) {
517 return &BinaryOps[std::make_pair(N->getOpcode(),
518 std::make_pair(Ops[0], Ops[1]))];
519 } else {
520 return &OneResultNodes[std::make_pair(N->getOpcode(),
521 std::make_pair(N->getValueType(0),
522 Ops))];
523 }
524 } else {
525 if (N->getOpcode() == ISD::LOAD) {
526 return &Loads[std::make_pair(Ops[1],
527 std::make_pair(Ops[0], N->getValueType(0)))];
528 } else {
529 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
530 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
531 std::make_pair(RV, Ops))];
532 }
533 }
534 return 0;
535}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000536
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000537
Chris Lattner78ec3112003-08-11 14:57:33 +0000538SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000539 while (!AllNodes.empty()) {
540 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000541 delete [] N->OperandList;
542 N->OperandList = 0;
543 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000544 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000545 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000546}
547
Chris Lattner0f2287b2005-04-13 02:38:18 +0000548SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000549 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000550 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000551 return getNode(ISD::AND, Op.getValueType(), Op,
552 getConstant(Imm, Op.getValueType()));
553}
554
Chris Lattnerc3aae252005-01-07 07:46:32 +0000555SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
556 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
557 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000558 if (VT != MVT::i64)
559 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000560
Chris Lattnerc3aae252005-01-07 07:46:32 +0000561 SDNode *&N = Constants[std::make_pair(Val, VT)];
562 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000563 N = new ConstantSDNode(false, Val, VT);
564 AllNodes.push_back(N);
565 return SDOperand(N, 0);
566}
567
Chris Lattner36ce6912005-11-29 06:21:05 +0000568SDOperand SelectionDAG::getString(const std::string &Val) {
569 StringSDNode *&N = StringNodes[Val];
570 if (!N) {
571 N = new StringSDNode(Val);
572 AllNodes.push_back(N);
573 }
574 return SDOperand(N, 0);
575}
576
Chris Lattner37bfbb42005-08-17 00:34:06 +0000577SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
578 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
579 // Mask out any bits that are not valid for this constant.
580 if (VT != MVT::i64)
581 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
582
583 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
584 if (N) return SDOperand(N, 0);
585 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000586 AllNodes.push_back(N);
587 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000588}
589
Chris Lattnerc3aae252005-01-07 07:46:32 +0000590SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
591 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
592 if (VT == MVT::f32)
593 Val = (float)Val; // Mask out extra precision.
594
Chris Lattnerd8658612005-02-17 20:17:32 +0000595 // Do the map lookup using the actual bit pattern for the floating point
596 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
597 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000598 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000599 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000600 N = new ConstantFPSDNode(false, Val, VT);
601 AllNodes.push_back(N);
602 return SDOperand(N, 0);
603}
604
605SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
606 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
607 if (VT == MVT::f32)
608 Val = (float)Val; // Mask out extra precision.
609
610 // Do the map lookup using the actual bit pattern for the floating point
611 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
612 // we don't have issues with SNANs.
613 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
614 if (N) return SDOperand(N, 0);
615 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000616 AllNodes.push_back(N);
617 return SDOperand(N, 0);
618}
619
Chris Lattnerc3aae252005-01-07 07:46:32 +0000620SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000621 MVT::ValueType VT, int offset) {
622 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000623 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000624 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000625 AllNodes.push_back(N);
626 return SDOperand(N, 0);
627}
628
629SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000630 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000631 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000632 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000633 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000634 AllNodes.push_back(N);
635 return SDOperand(N, 0);
636}
637
638SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
639 SDNode *&N = FrameIndices[FI];
640 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000641 N = new FrameIndexSDNode(FI, VT, false);
642 AllNodes.push_back(N);
643 return SDOperand(N, 0);
644}
645
646SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
647 SDNode *&N = TargetFrameIndices[FI];
648 if (N) return SDOperand(N, 0);
649 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000650 AllNodes.push_back(N);
651 return SDOperand(N, 0);
652}
653
Evan Chengb8973bd2006-01-31 22:23:14 +0000654SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000655 unsigned Alignment, int Offset) {
656 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
657 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000658 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000659 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000660 AllNodes.push_back(N);
661 return SDOperand(N, 0);
662}
663
Evan Chengb8973bd2006-01-31 22:23:14 +0000664SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000665 unsigned Alignment, int Offset) {
666 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
667 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000668 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000669 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000670 AllNodes.push_back(N);
671 return SDOperand(N, 0);
672}
673
674SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
675 SDNode *&N = BBNodes[MBB];
676 if (N) return SDOperand(N, 0);
677 N = new BasicBlockSDNode(MBB);
678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
680}
681
Chris Lattner15e4b012005-07-10 00:07:11 +0000682SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
683 if ((unsigned)VT >= ValueTypeNodes.size())
684 ValueTypeNodes.resize(VT+1);
685 if (ValueTypeNodes[VT] == 0) {
686 ValueTypeNodes[VT] = new VTSDNode(VT);
687 AllNodes.push_back(ValueTypeNodes[VT]);
688 }
689
690 return SDOperand(ValueTypeNodes[VT], 0);
691}
692
Chris Lattnerc3aae252005-01-07 07:46:32 +0000693SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
694 SDNode *&N = ExternalSymbols[Sym];
695 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000696 N = new ExternalSymbolSDNode(false, Sym, VT);
697 AllNodes.push_back(N);
698 return SDOperand(N, 0);
699}
700
Chris Lattner809ec112006-01-28 10:09:25 +0000701SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
702 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000703 SDNode *&N = TargetExternalSymbols[Sym];
704 if (N) return SDOperand(N, 0);
705 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000706 AllNodes.push_back(N);
707 return SDOperand(N, 0);
708}
709
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000710SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
711 if ((unsigned)Cond >= CondCodeNodes.size())
712 CondCodeNodes.resize(Cond+1);
713
Chris Lattner079a27a2005-08-09 20:40:02 +0000714 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000715 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000716 AllNodes.push_back(CondCodeNodes[Cond]);
717 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000718 return SDOperand(CondCodeNodes[Cond], 0);
719}
720
Chris Lattner0fdd7682005-08-30 22:38:38 +0000721SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
722 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
723 if (!Reg) {
724 Reg = new RegisterSDNode(RegNo, VT);
725 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000726 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000727 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000728}
729
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000730SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
731 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 // These setcc operations always fold.
733 switch (Cond) {
734 default: break;
735 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000736 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000738 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000739 }
740
Chris Lattner67255a12005-04-07 18:14:58 +0000741 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
742 uint64_t C2 = N2C->getValue();
743 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
744 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000745
Chris Lattnerc3aae252005-01-07 07:46:32 +0000746 // Sign extend the operands if required
747 if (ISD::isSignedIntSetCC(Cond)) {
748 C1 = N1C->getSignExtended();
749 C2 = N2C->getSignExtended();
750 }
751
752 switch (Cond) {
753 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000754 case ISD::SETEQ: return getConstant(C1 == C2, VT);
755 case ISD::SETNE: return getConstant(C1 != C2, VT);
756 case ISD::SETULT: return getConstant(C1 < C2, VT);
757 case ISD::SETUGT: return getConstant(C1 > C2, VT);
758 case ISD::SETULE: return getConstant(C1 <= C2, VT);
759 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
760 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
761 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
762 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
763 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000764 }
Chris Lattner24673922005-04-07 18:58:54 +0000765 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000766 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000767 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
768 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
769
770 // If the comparison constant has bits in the upper part, the
771 // zero-extended value could never match.
772 if (C2 & (~0ULL << InSize)) {
773 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
774 switch (Cond) {
775 case ISD::SETUGT:
776 case ISD::SETUGE:
777 case ISD::SETEQ: return getConstant(0, VT);
778 case ISD::SETULT:
779 case ISD::SETULE:
780 case ISD::SETNE: return getConstant(1, VT);
781 case ISD::SETGT:
782 case ISD::SETGE:
783 // True if the sign bit of C2 is set.
784 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
785 case ISD::SETLT:
786 case ISD::SETLE:
787 // True if the sign bit of C2 isn't set.
788 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
789 default:
790 break;
791 }
792 }
793
794 // Otherwise, we can perform the comparison with the low bits.
795 switch (Cond) {
796 case ISD::SETEQ:
797 case ISD::SETNE:
798 case ISD::SETUGT:
799 case ISD::SETUGE:
800 case ISD::SETULT:
801 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000802 return getSetCC(VT, N1.getOperand(0),
803 getConstant(C2, N1.getOperand(0).getValueType()),
804 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000805 default:
806 break; // todo, be more careful with signed comparisons
807 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000808 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
809 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
810 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
811 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
812 MVT::ValueType ExtDstTy = N1.getValueType();
813 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000814
Chris Lattner7b2880c2005-08-24 22:44:39 +0000815 // If the extended part has any inconsistent bits, it cannot ever
816 // compare equal. In other words, they have to be all ones or all
817 // zeros.
818 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000819 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000820 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
821 return getConstant(Cond == ISD::SETNE, VT);
822
823 // Otherwise, make this a use of a zext.
824 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000825 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000826 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000827 }
828
Chris Lattner67255a12005-04-07 18:14:58 +0000829 uint64_t MinVal, MaxVal;
830 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
831 if (ISD::isSignedIntSetCC(Cond)) {
832 MinVal = 1ULL << (OperandBitSize-1);
833 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
834 MaxVal = ~0ULL >> (65-OperandBitSize);
835 else
836 MaxVal = 0;
837 } else {
838 MinVal = 0;
839 MaxVal = ~0ULL >> (64-OperandBitSize);
840 }
841
842 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
843 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
844 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
845 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000846 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
847 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000848 }
849
850 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
851 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
852 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000853 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
854 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000855 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000856
Nate Begeman72ea2812005-04-14 08:56:52 +0000857 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
858 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000859
Nate Begeman72ea2812005-04-14 08:56:52 +0000860 // Canonicalize setgt X, Min --> setne X, Min
861 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000862 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000863
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000864 // If we have setult X, 1, turn it into seteq X, 0
865 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000866 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
867 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000868 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000869 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000870 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
871 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000872
873 // If we have "setcc X, C1", check to see if we can shrink the immediate
874 // by changing cc.
875
876 // SETUGT X, SINTMAX -> SETLT X, 0
877 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
878 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000879 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000880
881 // FIXME: Implement the rest of these.
882
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000883
884 // Fold bit comparisons when we can.
885 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
886 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
887 if (ConstantSDNode *AndRHS =
888 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
889 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
890 // Perform the xform if the AND RHS is a single bit.
891 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
892 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000893 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000894 TLI.getShiftAmountTy()));
895 }
896 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
897 // (X & 8) == 8 --> (X & 8) >> 3
898 // Perform the xform if C2 is a single bit.
899 if ((C2 & (C2-1)) == 0) {
900 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000901 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000902 }
903 }
904 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000905 }
Chris Lattner67255a12005-04-07 18:14:58 +0000906 } else if (isa<ConstantSDNode>(N1.Val)) {
907 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000908 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000909 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910
911 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
912 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
913 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000914
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915 switch (Cond) {
916 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000917 case ISD::SETEQ: return getConstant(C1 == C2, VT);
918 case ISD::SETNE: return getConstant(C1 != C2, VT);
919 case ISD::SETLT: return getConstant(C1 < C2, VT);
920 case ISD::SETGT: return getConstant(C1 > C2, VT);
921 case ISD::SETLE: return getConstant(C1 <= C2, VT);
922 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 }
924 } else {
925 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000926 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000927 }
928
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000929 // Could not fold it.
930 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000931}
932
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000934///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000935SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000936 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
937 if (!N) {
938 N = new SDNode(Opcode, VT);
939 AllNodes.push_back(N);
940 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941 return SDOperand(N, 0);
942}
943
Chris Lattnerc3aae252005-01-07 07:46:32 +0000944SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
945 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000946 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000947 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
949 uint64_t Val = C->getValue();
950 switch (Opcode) {
951 default: break;
952 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000953 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000954 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
955 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000956 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
957 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000958 case ISD::BIT_CONVERT:
959 if (VT == MVT::f32) {
960 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
961 return getConstantFP(BitsToFloat(Val), VT);
962 } else if (VT == MVT::f64) {
963 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
964 return getConstantFP(BitsToDouble(Val), VT);
965 }
966 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000967 case ISD::BSWAP:
968 switch(VT) {
969 default: assert(0 && "Invalid bswap!"); break;
970 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
971 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
972 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
973 }
974 break;
975 case ISD::CTPOP:
976 switch(VT) {
977 default: assert(0 && "Invalid ctpop!"); break;
978 case MVT::i1: return getConstant(Val != 0, VT);
979 case MVT::i8:
980 Tmp1 = (unsigned)Val & 0xFF;
981 return getConstant(CountPopulation_32(Tmp1), VT);
982 case MVT::i16:
983 Tmp1 = (unsigned)Val & 0xFFFF;
984 return getConstant(CountPopulation_32(Tmp1), VT);
985 case MVT::i32:
986 return getConstant(CountPopulation_32((unsigned)Val), VT);
987 case MVT::i64:
988 return getConstant(CountPopulation_64(Val), VT);
989 }
990 case ISD::CTLZ:
991 switch(VT) {
992 default: assert(0 && "Invalid ctlz!"); break;
993 case MVT::i1: return getConstant(Val == 0, VT);
994 case MVT::i8:
995 Tmp1 = (unsigned)Val & 0xFF;
996 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
997 case MVT::i16:
998 Tmp1 = (unsigned)Val & 0xFFFF;
999 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1000 case MVT::i32:
1001 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1002 case MVT::i64:
1003 return getConstant(CountLeadingZeros_64(Val), VT);
1004 }
1005 case ISD::CTTZ:
1006 switch(VT) {
1007 default: assert(0 && "Invalid cttz!"); break;
1008 case MVT::i1: return getConstant(Val == 0, VT);
1009 case MVT::i8:
1010 Tmp1 = (unsigned)Val | 0x100;
1011 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1012 case MVT::i16:
1013 Tmp1 = (unsigned)Val | 0x10000;
1014 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1015 case MVT::i32:
1016 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1017 case MVT::i64:
1018 return getConstant(CountTrailingZeros_64(Val), VT);
1019 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001020 }
1021 }
1022
Chris Lattner94683772005-12-23 05:30:37 +00001023 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001024 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1025 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001026 case ISD::FNEG:
1027 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001028 case ISD::FABS:
1029 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001030 case ISD::FP_ROUND:
1031 case ISD::FP_EXTEND:
1032 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001033 case ISD::FP_TO_SINT:
1034 return getConstant((int64_t)C->getValue(), VT);
1035 case ISD::FP_TO_UINT:
1036 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001037 case ISD::BIT_CONVERT:
1038 if (VT == MVT::i32) {
1039 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1040 return getConstant(FloatToBits(C->getValue()), VT);
1041 } else if (VT == MVT::i64) {
1042 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1043 return getConstant(DoubleToBits(C->getValue()), VT);
1044 }
1045 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001046 }
1047
1048 unsigned OpOpcode = Operand.Val->getOpcode();
1049 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001050 case ISD::TokenFactor:
1051 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 case ISD::SIGN_EXTEND:
1053 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001054 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1056 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1057 break;
1058 case ISD::ZERO_EXTEND:
1059 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001060 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001061 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001062 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001063 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001064 case ISD::ANY_EXTEND:
1065 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001066 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001067 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1068 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1069 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1070 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001071 case ISD::TRUNCATE:
1072 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001073 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001074 if (OpOpcode == ISD::TRUNCATE)
1075 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001076 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1077 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001078 // If the source is smaller than the dest, we still need an extend.
1079 if (Operand.Val->getOperand(0).getValueType() < VT)
1080 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1081 else if (Operand.Val->getOperand(0).getValueType() > VT)
1082 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1083 else
1084 return Operand.Val->getOperand(0);
1085 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001086 break;
Chris Lattner35481892005-12-23 00:16:34 +00001087 case ISD::BIT_CONVERT:
1088 // Basic sanity checking.
1089 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1090 "Cannot BIT_CONVERT between two different types!");
1091 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001092 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1093 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001094 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001095 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001096 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1097 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001098 Operand.Val->getOperand(0));
1099 if (OpOpcode == ISD::FNEG) // --X -> X
1100 return Operand.Val->getOperand(0);
1101 break;
1102 case ISD::FABS:
1103 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1104 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1105 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001106 }
1107
Chris Lattner43247a12005-08-25 19:12:10 +00001108 SDNode *N;
1109 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1110 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001111 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001112 E = N = new SDNode(Opcode, Operand);
1113 } else {
1114 N = new SDNode(Opcode, Operand);
1115 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001116 N->setValueTypes(VT);
1117 AllNodes.push_back(N);
1118 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001119}
1120
Chris Lattner36019aa2005-04-18 03:48:41 +00001121
1122
Chris Lattnerc3aae252005-01-07 07:46:32 +00001123SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1124 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001125#ifndef NDEBUG
1126 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001127 case ISD::TokenFactor:
1128 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1129 N2.getValueType() == MVT::Other && "Invalid token factor!");
1130 break;
Chris Lattner76365122005-01-16 02:23:22 +00001131 case ISD::AND:
1132 case ISD::OR:
1133 case ISD::XOR:
1134 case ISD::UDIV:
1135 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001136 case ISD::MULHU:
1137 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001138 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1139 // fall through
1140 case ISD::ADD:
1141 case ISD::SUB:
1142 case ISD::MUL:
1143 case ISD::SDIV:
1144 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001145 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1146 // fall through.
1147 case ISD::FADD:
1148 case ISD::FSUB:
1149 case ISD::FMUL:
1150 case ISD::FDIV:
1151 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001152 assert(N1.getValueType() == N2.getValueType() &&
1153 N1.getValueType() == VT && "Binary operator types must match!");
1154 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001155 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1156 assert(N1.getValueType() == VT &&
1157 MVT::isFloatingPoint(N1.getValueType()) &&
1158 MVT::isFloatingPoint(N2.getValueType()) &&
1159 "Invalid FCOPYSIGN!");
1160 break;
Chris Lattner76365122005-01-16 02:23:22 +00001161 case ISD::SHL:
1162 case ISD::SRA:
1163 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001164 case ISD::ROTL:
1165 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001166 assert(VT == N1.getValueType() &&
1167 "Shift operators return type must be the same as their first arg");
1168 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001169 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001170 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001171 case ISD::FP_ROUND_INREG: {
1172 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1173 assert(VT == N1.getValueType() && "Not an inreg round!");
1174 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1175 "Cannot FP_ROUND_INREG integer types");
1176 assert(EVT <= VT && "Not rounding down!");
1177 break;
1178 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001179 case ISD::AssertSext:
1180 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001181 case ISD::SIGN_EXTEND_INREG: {
1182 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1183 assert(VT == N1.getValueType() && "Not an inreg extend!");
1184 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1185 "Cannot *_EXTEND_INREG FP types");
1186 assert(EVT <= VT && "Not extending!");
1187 }
1188
Chris Lattner76365122005-01-16 02:23:22 +00001189 default: break;
1190 }
1191#endif
1192
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1194 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1195 if (N1C) {
1196 if (N2C) {
1197 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1198 switch (Opcode) {
1199 case ISD::ADD: return getConstant(C1 + C2, VT);
1200 case ISD::SUB: return getConstant(C1 - C2, VT);
1201 case ISD::MUL: return getConstant(C1 * C2, VT);
1202 case ISD::UDIV:
1203 if (C2) return getConstant(C1 / C2, VT);
1204 break;
1205 case ISD::UREM :
1206 if (C2) return getConstant(C1 % C2, VT);
1207 break;
1208 case ISD::SDIV :
1209 if (C2) return getConstant(N1C->getSignExtended() /
1210 N2C->getSignExtended(), VT);
1211 break;
1212 case ISD::SREM :
1213 if (C2) return getConstant(N1C->getSignExtended() %
1214 N2C->getSignExtended(), VT);
1215 break;
1216 case ISD::AND : return getConstant(C1 & C2, VT);
1217 case ISD::OR : return getConstant(C1 | C2, VT);
1218 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001219 case ISD::SHL : return getConstant(C1 << C2, VT);
1220 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001221 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001222 case ISD::ROTL :
1223 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1224 VT);
1225 case ISD::ROTR :
1226 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1227 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001228 default: break;
1229 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230 } else { // Cannonicalize constant to RHS if commutative
1231 if (isCommutativeBinOp(Opcode)) {
1232 std::swap(N1C, N2C);
1233 std::swap(N1, N2);
1234 }
1235 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001236 }
1237
1238 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1239 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001240 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 if (N2CFP) {
1242 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1243 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001244 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1245 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1246 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1247 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001248 if (C2) return getConstantFP(C1 / C2, VT);
1249 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001250 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001251 if (C2) return getConstantFP(fmod(C1, C2), VT);
1252 break;
Chris Lattner12d83032006-03-05 05:30:57 +00001253 case ISD::FCOPYSIGN:
1254 return getConstantFP(copysign(C1, C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001255 default: break;
1256 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001257 } else { // Cannonicalize constant to RHS if commutative
1258 if (isCommutativeBinOp(Opcode)) {
1259 std::swap(N1CFP, N2CFP);
1260 std::swap(N1, N2);
1261 }
1262 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001263 }
1264
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265 // Finally, fold operations that do not require constants.
1266 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001267 case ISD::FP_ROUND_INREG:
1268 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1269 break;
1270 case ISD::SIGN_EXTEND_INREG: {
1271 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1272 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001273 break;
1274 }
1275
Nate Begemaneea805e2005-04-13 21:23:31 +00001276 // FIXME: figure out how to safely handle things like
1277 // int foo(int x) { return 1 << (x & 255); }
1278 // int bar() { return foo(256); }
1279#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001280 case ISD::SHL:
1281 case ISD::SRL:
1282 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001283 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001284 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001285 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001286 else if (N2.getOpcode() == ISD::AND)
1287 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1288 // If the and is only masking out bits that cannot effect the shift,
1289 // eliminate the and.
1290 unsigned NumBits = MVT::getSizeInBits(VT);
1291 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1292 return getNode(Opcode, VT, N1, N2.getOperand(0));
1293 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001294 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001295#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001296 }
1297
Chris Lattner27e9b412005-05-11 18:57:39 +00001298 // Memoize this node if possible.
1299 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001300 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001301 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1302 if (BON) return SDOperand(BON, 0);
1303
1304 BON = N = new SDNode(Opcode, N1, N2);
1305 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001306 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001307 }
1308
Chris Lattner3e011362005-05-14 07:45:46 +00001309 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310 AllNodes.push_back(N);
1311 return SDOperand(N, 0);
1312}
1313
Chris Lattnerc3aae252005-01-07 07:46:32 +00001314SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1315 SDOperand N1, SDOperand N2, SDOperand N3) {
1316 // Perform various simplifications.
1317 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1318 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1319 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1320 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001321 case ISD::SETCC: {
1322 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001323 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001324 if (Simp.Val) return Simp;
1325 break;
1326 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001327 case ISD::SELECT:
1328 if (N1C)
1329 if (N1C->getValue())
1330 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001331 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 return N3; // select false, X, Y -> Y
1333
1334 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001335 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001336 case ISD::BRCOND:
1337 if (N2C)
1338 if (N2C->getValue()) // Unconditional branch
1339 return getNode(ISD::BR, MVT::Other, N1, N3);
1340 else
1341 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001342 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001343 }
1344
Chris Lattner385328c2005-05-14 07:42:29 +00001345 std::vector<SDOperand> Ops;
1346 Ops.reserve(3);
1347 Ops.push_back(N1);
1348 Ops.push_back(N2);
1349 Ops.push_back(N3);
1350
Chris Lattner43247a12005-08-25 19:12:10 +00001351 // Memoize node if it doesn't produce a flag.
1352 SDNode *N;
1353 if (VT != MVT::Flag) {
1354 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1355 if (E) return SDOperand(E, 0);
1356 E = N = new SDNode(Opcode, N1, N2, N3);
1357 } else {
1358 N = new SDNode(Opcode, N1, N2, N3);
1359 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001360 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 AllNodes.push_back(N);
1362 return SDOperand(N, 0);
1363}
1364
1365SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001366 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001367 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001368 std::vector<SDOperand> Ops;
1369 Ops.reserve(4);
1370 Ops.push_back(N1);
1371 Ops.push_back(N2);
1372 Ops.push_back(N3);
1373 Ops.push_back(N4);
1374 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001375}
1376
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001377SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1378 SDOperand N1, SDOperand N2, SDOperand N3,
1379 SDOperand N4, SDOperand N5) {
1380 std::vector<SDOperand> Ops;
1381 Ops.reserve(5);
1382 Ops.push_back(N1);
1383 Ops.push_back(N2);
1384 Ops.push_back(N3);
1385 Ops.push_back(N4);
1386 Ops.push_back(N5);
1387 return getNode(Opcode, VT, Ops);
1388}
1389
Evan Cheng7038daf2005-12-10 00:37:58 +00001390SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1391 SDOperand Chain, SDOperand Ptr,
1392 SDOperand SV) {
1393 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1394 if (N) return SDOperand(N, 0);
1395 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1396
1397 // Loads have a token chain.
1398 setNodeValueTypes(N, VT, MVT::Other);
1399 AllNodes.push_back(N);
1400 return SDOperand(N, 0);
1401}
1402
1403SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1404 SDOperand Chain, SDOperand Ptr,
1405 SDOperand SV) {
1406 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1407 if (N) return SDOperand(N, 0);
1408 std::vector<SDOperand> Ops;
1409 Ops.reserve(5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001410 Ops.push_back(getConstant(Count, MVT::i32));
1411 Ops.push_back(getValueType(EVT));
Evan Cheng1ab7d852006-03-01 00:51:13 +00001412 Ops.push_back(Chain);
1413 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001414 Ops.push_back(SV);
1415 std::vector<MVT::ValueType> VTs;
1416 VTs.reserve(2);
1417 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1418 return getNode(ISD::VLOAD, VTs, Ops);
1419}
1420
1421SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1422 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1423 MVT::ValueType EVT) {
1424 std::vector<SDOperand> Ops;
1425 Ops.reserve(4);
1426 Ops.push_back(Chain);
1427 Ops.push_back(Ptr);
1428 Ops.push_back(SV);
1429 Ops.push_back(getValueType(EVT));
1430 std::vector<MVT::ValueType> VTs;
1431 VTs.reserve(2);
1432 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1433 return getNode(Opcode, VTs, Ops);
1434}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001435
Chris Lattner0437cdd2005-05-09 04:14:13 +00001436SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001437 assert((!V || isa<PointerType>(V->getType())) &&
1438 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001439 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1440 if (N) return SDOperand(N, 0);
1441
1442 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001443 AllNodes.push_back(N);
1444 return SDOperand(N, 0);
1445}
1446
Nate Begemanacc398c2006-01-25 18:21:52 +00001447SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1448 SDOperand Chain, SDOperand Ptr,
1449 SDOperand SV) {
1450 std::vector<SDOperand> Ops;
1451 Ops.reserve(3);
1452 Ops.push_back(Chain);
1453 Ops.push_back(Ptr);
1454 Ops.push_back(SV);
1455 std::vector<MVT::ValueType> VTs;
1456 VTs.reserve(2);
1457 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1458 return getNode(ISD::VAARG, VTs, Ops);
1459}
1460
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001461SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001462 std::vector<SDOperand> &Ops) {
1463 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001464 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001465 case 1: return getNode(Opcode, VT, Ops[0]);
1466 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1467 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001468 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001469 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001470
Chris Lattner89c34632005-05-14 06:20:26 +00001471 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001472 switch (Opcode) {
1473 default: break;
1474 case ISD::BRCONDTWOWAY:
1475 if (N1C)
1476 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001477 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001478 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001479 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001480 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001481 case ISD::BRTWOWAY_CC:
1482 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1483 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1484 "LHS and RHS of comparison must have same type!");
1485 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001486 case ISD::TRUNCSTORE: {
1487 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1488 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1489#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1490 // If this is a truncating store of a constant, convert to the desired type
1491 // and store it instead.
1492 if (isa<Constant>(Ops[0])) {
1493 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1494 if (isa<Constant>(Op))
1495 N1 = Op;
1496 }
1497 // Also for ConstantFP?
1498#endif
1499 if (Ops[0].getValueType() == EVT) // Normal store?
1500 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1501 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1502 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1503 "Can't do FP-INT conversion!");
1504 break;
1505 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001506 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001507 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001508 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1509 "LHS and RHS of condition must have same type!");
1510 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1511 "True and False arms of SelectCC must have same type!");
1512 assert(Ops[2].getValueType() == VT &&
1513 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001514 break;
1515 }
1516 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001517 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001518 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1519 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001520 break;
1521 }
Chris Lattneref847df2005-04-09 03:27:28 +00001522 }
1523
Chris Lattner385328c2005-05-14 07:42:29 +00001524 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001525 SDNode *N;
1526 if (VT != MVT::Flag) {
1527 SDNode *&E =
1528 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1529 if (E) return SDOperand(E, 0);
1530 E = N = new SDNode(Opcode, Ops);
1531 } else {
1532 N = new SDNode(Opcode, Ops);
1533 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001534 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001535 AllNodes.push_back(N);
1536 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001537}
1538
Chris Lattner89c34632005-05-14 06:20:26 +00001539SDOperand SelectionDAG::getNode(unsigned Opcode,
1540 std::vector<MVT::ValueType> &ResultTys,
1541 std::vector<SDOperand> &Ops) {
1542 if (ResultTys.size() == 1)
1543 return getNode(Opcode, ResultTys[0], Ops);
1544
Chris Lattner5f056bf2005-07-10 01:55:33 +00001545 switch (Opcode) {
1546 case ISD::EXTLOAD:
1547 case ISD::SEXTLOAD:
1548 case ISD::ZEXTLOAD: {
1549 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1550 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1551 // If they are asking for an extending load from/to the same thing, return a
1552 // normal load.
1553 if (ResultTys[0] == EVT)
1554 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1555 assert(EVT < ResultTys[0] &&
1556 "Should only be an extending load, not truncating!");
1557 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1558 "Cannot sign/zero extend a FP load!");
1559 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1560 "Cannot convert from FP to Int or Int -> FP!");
1561 break;
1562 }
1563
Chris Lattnere89083a2005-05-14 07:25:05 +00001564 // FIXME: figure out how to safely handle things like
1565 // int foo(int x) { return 1 << (x & 255); }
1566 // int bar() { return foo(256); }
1567#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001568 case ISD::SRA_PARTS:
1569 case ISD::SRL_PARTS:
1570 case ISD::SHL_PARTS:
1571 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001572 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001573 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1574 else if (N3.getOpcode() == ISD::AND)
1575 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1576 // If the and is only masking out bits that cannot effect the shift,
1577 // eliminate the and.
1578 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1579 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1580 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1581 }
1582 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001583#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001584 }
Chris Lattner89c34632005-05-14 06:20:26 +00001585
Chris Lattner43247a12005-08-25 19:12:10 +00001586 // Memoize the node unless it returns a flag.
1587 SDNode *N;
1588 if (ResultTys.back() != MVT::Flag) {
1589 SDNode *&E =
1590 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1591 if (E) return SDOperand(E, 0);
1592 E = N = new SDNode(Opcode, Ops);
1593 } else {
1594 N = new SDNode(Opcode, Ops);
1595 }
Chris Lattnera3255112005-11-08 23:30:28 +00001596 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001597 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001598 return SDOperand(N, 0);
1599}
1600
Chris Lattnera3255112005-11-08 23:30:28 +00001601void SelectionDAG::setNodeValueTypes(SDNode *N,
1602 std::vector<MVT::ValueType> &RetVals) {
1603 switch (RetVals.size()) {
1604 case 0: return;
1605 case 1: N->setValueTypes(RetVals[0]); return;
1606 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1607 default: break;
1608 }
1609
1610 std::list<std::vector<MVT::ValueType> >::iterator I =
1611 std::find(VTList.begin(), VTList.end(), RetVals);
1612 if (I == VTList.end()) {
1613 VTList.push_front(RetVals);
1614 I = VTList.begin();
1615 }
1616
1617 N->setValueTypes(&(*I)[0], I->size());
1618}
1619
1620void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1621 MVT::ValueType VT2) {
1622 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1623 E = VTList.end(); I != E; ++I) {
1624 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1625 N->setValueTypes(&(*I)[0], 2);
1626 return;
1627 }
1628 }
1629 std::vector<MVT::ValueType> V;
1630 V.push_back(VT1);
1631 V.push_back(VT2);
1632 VTList.push_front(V);
1633 N->setValueTypes(&(*VTList.begin())[0], 2);
1634}
1635
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001636/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1637/// specified operands. If the resultant node already exists in the DAG,
1638/// this does not modify the specified node, instead it returns the node that
1639/// already exists. If the resultant node does not exist in the DAG, the
1640/// input node is returned. As a degenerate case, if you specify the same
1641/// input operands as the node already has, the input node is returned.
1642SDOperand SelectionDAG::
1643UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1644 SDNode *N = InN.Val;
1645 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1646
1647 // Check to see if there is no change.
1648 if (Op == N->getOperand(0)) return InN;
1649
1650 // See if the modified node already exists.
1651 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1652 if (NewSlot && *NewSlot)
1653 return SDOperand(*NewSlot, InN.ResNo);
1654
1655 // Nope it doesn't. Remove the node from it's current place in the maps.
1656 if (NewSlot)
1657 RemoveNodeFromCSEMaps(N);
1658
1659 // Now we update the operands.
1660 N->OperandList[0].Val->removeUser(N);
1661 Op.Val->addUser(N);
1662 N->OperandList[0] = Op;
1663
1664 // If this gets put into a CSE map, add it.
1665 if (NewSlot) *NewSlot = N;
1666 return InN;
1667}
1668
1669SDOperand SelectionDAG::
1670UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1671 SDNode *N = InN.Val;
1672 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1673
1674 // Check to see if there is no change.
1675 bool AnyChange = false;
1676 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1677 return InN; // No operands changed, just return the input node.
1678
1679 // See if the modified node already exists.
1680 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1681 if (NewSlot && *NewSlot)
1682 return SDOperand(*NewSlot, InN.ResNo);
1683
1684 // Nope it doesn't. Remove the node from it's current place in the maps.
1685 if (NewSlot)
1686 RemoveNodeFromCSEMaps(N);
1687
1688 // Now we update the operands.
1689 if (N->OperandList[0] != Op1) {
1690 N->OperandList[0].Val->removeUser(N);
1691 Op1.Val->addUser(N);
1692 N->OperandList[0] = Op1;
1693 }
1694 if (N->OperandList[1] != Op2) {
1695 N->OperandList[1].Val->removeUser(N);
1696 Op2.Val->addUser(N);
1697 N->OperandList[1] = Op2;
1698 }
1699
1700 // If this gets put into a CSE map, add it.
1701 if (NewSlot) *NewSlot = N;
1702 return InN;
1703}
1704
1705SDOperand SelectionDAG::
1706UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1707 std::vector<SDOperand> Ops;
1708 Ops.push_back(Op1);
1709 Ops.push_back(Op2);
1710 Ops.push_back(Op3);
1711 return UpdateNodeOperands(N, Ops);
1712}
1713
1714SDOperand SelectionDAG::
1715UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1716 SDOperand Op3, SDOperand Op4) {
1717 std::vector<SDOperand> Ops;
1718 Ops.push_back(Op1);
1719 Ops.push_back(Op2);
1720 Ops.push_back(Op3);
1721 Ops.push_back(Op4);
1722 return UpdateNodeOperands(N, Ops);
1723}
1724
1725SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001726UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1727 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1728 std::vector<SDOperand> Ops;
1729 Ops.push_back(Op1);
1730 Ops.push_back(Op2);
1731 Ops.push_back(Op3);
1732 Ops.push_back(Op4);
1733 Ops.push_back(Op5);
1734 return UpdateNodeOperands(N, Ops);
1735}
1736
1737
1738SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001739UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1740 SDNode *N = InN.Val;
1741 assert(N->getNumOperands() == Ops.size() &&
1742 "Update with wrong number of operands");
1743
1744 // Check to see if there is no change.
1745 unsigned NumOps = Ops.size();
1746 bool AnyChange = false;
1747 for (unsigned i = 0; i != NumOps; ++i) {
1748 if (Ops[i] != N->getOperand(i)) {
1749 AnyChange = true;
1750 break;
1751 }
1752 }
1753
1754 // No operands changed, just return the input node.
1755 if (!AnyChange) return InN;
1756
1757 // See if the modified node already exists.
1758 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1759 if (NewSlot && *NewSlot)
1760 return SDOperand(*NewSlot, InN.ResNo);
1761
1762 // Nope it doesn't. Remove the node from it's current place in the maps.
1763 if (NewSlot)
1764 RemoveNodeFromCSEMaps(N);
1765
1766 // Now we update the operands.
1767 for (unsigned i = 0; i != NumOps; ++i) {
1768 if (N->OperandList[i] != Ops[i]) {
1769 N->OperandList[i].Val->removeUser(N);
1770 Ops[i].Val->addUser(N);
1771 N->OperandList[i] = Ops[i];
1772 }
1773 }
1774
1775 // If this gets put into a CSE map, add it.
1776 if (NewSlot) *NewSlot = N;
1777 return InN;
1778}
1779
1780
1781
Chris Lattner149c58c2005-08-16 18:17:10 +00001782
1783/// SelectNodeTo - These are used for target selectors to *mutate* the
1784/// specified node to have the specified return type, Target opcode, and
1785/// operands. Note that target opcodes are stored as
1786/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001787///
1788/// Note that SelectNodeTo returns the resultant node. If there is already a
1789/// node of the specified opcode and operands, it returns that node instead of
1790/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001791SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1792 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001793 // If an identical node already exists, use it.
1794 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1795 if (ON) return SDOperand(ON, 0);
1796
Chris Lattner7651fa42005-08-24 23:00:29 +00001797 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001798
Chris Lattner7651fa42005-08-24 23:00:29 +00001799 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1800 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001801
1802 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001803 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001804}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001805
Chris Lattnereb19e402005-11-30 22:45:14 +00001806SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1807 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001808 // If an identical node already exists, use it.
1809 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1810 std::make_pair(Op1, VT))];
1811 if (ON) return SDOperand(ON, 0);
1812
Chris Lattner149c58c2005-08-16 18:17:10 +00001813 RemoveNodeFromCSEMaps(N);
1814 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1815 N->setValueTypes(VT);
1816 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001817
1818 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001819 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001820}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001821
Chris Lattnereb19e402005-11-30 22:45:14 +00001822SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1823 MVT::ValueType VT, SDOperand Op1,
1824 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001825 // If an identical node already exists, use it.
1826 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1827 std::make_pair(Op1, Op2))];
1828 if (ON) return SDOperand(ON, 0);
1829
Chris Lattner149c58c2005-08-16 18:17:10 +00001830 RemoveNodeFromCSEMaps(N);
1831 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1832 N->setValueTypes(VT);
1833 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001834
1835 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001836 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001837}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001838
Chris Lattnereb19e402005-11-30 22:45:14 +00001839SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1840 MVT::ValueType VT, SDOperand Op1,
1841 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001842 // If an identical node already exists, use it.
1843 std::vector<SDOperand> OpList;
1844 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1845 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1846 std::make_pair(VT, OpList))];
1847 if (ON) return SDOperand(ON, 0);
1848
Chris Lattner149c58c2005-08-16 18:17:10 +00001849 RemoveNodeFromCSEMaps(N);
1850 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1851 N->setValueTypes(VT);
1852 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001853
1854 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001855 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001856}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001857
Chris Lattnereb19e402005-11-30 22:45:14 +00001858SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1859 MVT::ValueType VT, SDOperand Op1,
1860 SDOperand Op2, SDOperand Op3,
1861 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001862 // If an identical node already exists, use it.
1863 std::vector<SDOperand> OpList;
1864 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1865 OpList.push_back(Op4);
1866 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1867 std::make_pair(VT, OpList))];
1868 if (ON) return SDOperand(ON, 0);
1869
Nate Begeman294a0a12005-08-18 07:30:15 +00001870 RemoveNodeFromCSEMaps(N);
1871 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1872 N->setValueTypes(VT);
1873 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001874
1875 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001876 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001877}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001878
Chris Lattnereb19e402005-11-30 22:45:14 +00001879SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1880 MVT::ValueType VT, SDOperand Op1,
1881 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1882 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001883 // If an identical node already exists, use it.
1884 std::vector<SDOperand> OpList;
1885 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1886 OpList.push_back(Op4); OpList.push_back(Op5);
1887 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1888 std::make_pair(VT, OpList))];
1889 if (ON) return SDOperand(ON, 0);
1890
Chris Lattner6b09a292005-08-21 18:49:33 +00001891 RemoveNodeFromCSEMaps(N);
1892 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1893 N->setValueTypes(VT);
1894 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001895
1896 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001897 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001898}
Chris Lattner149c58c2005-08-16 18:17:10 +00001899
Chris Lattnereb19e402005-11-30 22:45:14 +00001900SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1901 MVT::ValueType VT, SDOperand Op1,
1902 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1903 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001904 // If an identical node already exists, use it.
1905 std::vector<SDOperand> OpList;
1906 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1907 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1908 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1909 std::make_pair(VT, OpList))];
1910 if (ON) return SDOperand(ON, 0);
1911
Evan Cheng61ca74b2005-11-30 02:04:11 +00001912 RemoveNodeFromCSEMaps(N);
1913 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1914 N->setValueTypes(VT);
1915 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001916
1917 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001918 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001919}
1920
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001921SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1922 MVT::ValueType VT, SDOperand Op1,
1923 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1924 SDOperand Op5, SDOperand Op6,
1925 SDOperand Op7) {
1926 // If an identical node already exists, use it.
1927 std::vector<SDOperand> OpList;
1928 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1929 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1930 OpList.push_back(Op7);
1931 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1932 std::make_pair(VT, OpList))];
1933 if (ON) return SDOperand(ON, 0);
1934
1935 RemoveNodeFromCSEMaps(N);
1936 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1937 N->setValueTypes(VT);
1938 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1939
1940 ON = N; // Memoize the new node.
1941 return SDOperand(N, 0);
1942}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001943SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1944 MVT::ValueType VT, SDOperand Op1,
1945 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1946 SDOperand Op5, SDOperand Op6,
1947 SDOperand Op7, SDOperand Op8) {
1948 // If an identical node already exists, use it.
1949 std::vector<SDOperand> OpList;
1950 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1951 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1952 OpList.push_back(Op7); OpList.push_back(Op8);
1953 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1954 std::make_pair(VT, OpList))];
1955 if (ON) return SDOperand(ON, 0);
1956
1957 RemoveNodeFromCSEMaps(N);
1958 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1959 N->setValueTypes(VT);
1960 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1961
1962 ON = N; // Memoize the new node.
1963 return SDOperand(N, 0);
1964}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001965
Chris Lattnereb19e402005-11-30 22:45:14 +00001966SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1967 MVT::ValueType VT1, MVT::ValueType VT2,
1968 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001969 // If an identical node already exists, use it.
1970 std::vector<SDOperand> OpList;
1971 OpList.push_back(Op1); OpList.push_back(Op2);
1972 std::vector<MVT::ValueType> VTList;
1973 VTList.push_back(VT1); VTList.push_back(VT2);
1974 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1975 std::make_pair(VTList, OpList))];
1976 if (ON) return SDOperand(ON, 0);
1977
Chris Lattner0fb094f2005-11-19 01:44:53 +00001978 RemoveNodeFromCSEMaps(N);
1979 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1980 setNodeValueTypes(N, VT1, VT2);
1981 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001982
1983 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001984 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001985}
1986
Chris Lattnereb19e402005-11-30 22:45:14 +00001987SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1988 MVT::ValueType VT1, MVT::ValueType VT2,
1989 SDOperand Op1, SDOperand Op2,
1990 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001991 // If an identical node already exists, use it.
1992 std::vector<SDOperand> OpList;
1993 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1994 std::vector<MVT::ValueType> VTList;
1995 VTList.push_back(VT1); VTList.push_back(VT2);
1996 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1997 std::make_pair(VTList, OpList))];
1998 if (ON) return SDOperand(ON, 0);
1999
Chris Lattner0fb094f2005-11-19 01:44:53 +00002000 RemoveNodeFromCSEMaps(N);
2001 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2002 setNodeValueTypes(N, VT1, VT2);
2003 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002004
2005 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002006 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002007}
2008
Chris Lattnereb19e402005-11-30 22:45:14 +00002009SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2010 MVT::ValueType VT1, MVT::ValueType VT2,
2011 SDOperand Op1, SDOperand Op2,
2012 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002013 // If an identical node already exists, use it.
2014 std::vector<SDOperand> OpList;
2015 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2016 OpList.push_back(Op4);
2017 std::vector<MVT::ValueType> VTList;
2018 VTList.push_back(VT1); VTList.push_back(VT2);
2019 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2020 std::make_pair(VTList, OpList))];
2021 if (ON) return SDOperand(ON, 0);
2022
Chris Lattner0fb094f2005-11-19 01:44:53 +00002023 RemoveNodeFromCSEMaps(N);
2024 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2025 setNodeValueTypes(N, VT1, VT2);
2026 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002027
2028 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002029 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002030}
2031
Chris Lattnereb19e402005-11-30 22:45:14 +00002032SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2033 MVT::ValueType VT1, MVT::ValueType VT2,
2034 SDOperand Op1, SDOperand Op2,
2035 SDOperand Op3, SDOperand Op4,
2036 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002037 // If an identical node already exists, use it.
2038 std::vector<SDOperand> OpList;
2039 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2040 OpList.push_back(Op4); OpList.push_back(Op5);
2041 std::vector<MVT::ValueType> VTList;
2042 VTList.push_back(VT1); VTList.push_back(VT2);
2043 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2044 std::make_pair(VTList, OpList))];
2045 if (ON) return SDOperand(ON, 0);
2046
Chris Lattner0fb094f2005-11-19 01:44:53 +00002047 RemoveNodeFromCSEMaps(N);
2048 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2049 setNodeValueTypes(N, VT1, VT2);
2050 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002051
2052 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002053 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002054}
2055
Evan Cheng6ae46c42006-02-09 07:15:23 +00002056/// getTargetNode - These are used for target selectors to create a new node
2057/// with specified return type(s), target opcode, and operands.
2058///
2059/// Note that getTargetNode returns the resultant node. If there is already a
2060/// node of the specified opcode and operands, it returns that node instead of
2061/// the current one.
2062SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2063 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2064}
2065SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2066 SDOperand Op1) {
2067 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2068}
2069SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2070 SDOperand Op1, SDOperand Op2) {
2071 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2072}
2073SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2074 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2075 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2076}
2077SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2078 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2079 SDOperand Op4) {
2080 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2081}
2082SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2083 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2084 SDOperand Op4, SDOperand Op5) {
2085 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2086}
2087SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2088 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2089 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2090 std::vector<SDOperand> Ops;
2091 Ops.reserve(6);
2092 Ops.push_back(Op1);
2093 Ops.push_back(Op2);
2094 Ops.push_back(Op3);
2095 Ops.push_back(Op4);
2096 Ops.push_back(Op5);
2097 Ops.push_back(Op6);
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) {
2104 std::vector<SDOperand> Ops;
2105 Ops.reserve(7);
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 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2114}
2115SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2116 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2117 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2118 SDOperand Op7, SDOperand Op8) {
2119 std::vector<SDOperand> Ops;
2120 Ops.reserve(8);
2121 Ops.push_back(Op1);
2122 Ops.push_back(Op2);
2123 Ops.push_back(Op3);
2124 Ops.push_back(Op4);
2125 Ops.push_back(Op5);
2126 Ops.push_back(Op6);
2127 Ops.push_back(Op7);
2128 Ops.push_back(Op8);
2129 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2130}
2131SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2132 std::vector<SDOperand> &Ops) {
2133 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2134}
2135SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2136 MVT::ValueType VT2, SDOperand Op1) {
2137 std::vector<MVT::ValueType> ResultTys;
2138 ResultTys.push_back(VT1);
2139 ResultTys.push_back(VT2);
2140 std::vector<SDOperand> Ops;
2141 Ops.push_back(Op1);
2142 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2143}
2144SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2145 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2146 std::vector<MVT::ValueType> ResultTys;
2147 ResultTys.push_back(VT1);
2148 ResultTys.push_back(VT2);
2149 std::vector<SDOperand> Ops;
2150 Ops.push_back(Op1);
2151 Ops.push_back(Op2);
2152 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2153}
2154SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2155 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2156 SDOperand Op3) {
2157 std::vector<MVT::ValueType> ResultTys;
2158 ResultTys.push_back(VT1);
2159 ResultTys.push_back(VT2);
2160 std::vector<SDOperand> Ops;
2161 Ops.push_back(Op1);
2162 Ops.push_back(Op2);
2163 Ops.push_back(Op3);
2164 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2165}
2166SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2167 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2168 SDOperand Op3, SDOperand Op4) {
2169 std::vector<MVT::ValueType> ResultTys;
2170 ResultTys.push_back(VT1);
2171 ResultTys.push_back(VT2);
2172 std::vector<SDOperand> Ops;
2173 Ops.push_back(Op1);
2174 Ops.push_back(Op2);
2175 Ops.push_back(Op3);
2176 Ops.push_back(Op4);
2177 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2178}
2179SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2180 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2181 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
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 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2192}
2193SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2194 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2195 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2196 SDOperand Op6) {
2197 std::vector<MVT::ValueType> ResultTys;
2198 ResultTys.push_back(VT1);
2199 ResultTys.push_back(VT2);
2200 std::vector<SDOperand> Ops;
2201 Ops.push_back(Op1);
2202 Ops.push_back(Op2);
2203 Ops.push_back(Op3);
2204 Ops.push_back(Op4);
2205 Ops.push_back(Op5);
2206 Ops.push_back(Op6);
2207 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2208}
2209SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2210 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2211 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2212 SDOperand Op6, SDOperand Op7) {
2213 std::vector<MVT::ValueType> ResultTys;
2214 ResultTys.push_back(VT1);
2215 ResultTys.push_back(VT2);
2216 std::vector<SDOperand> Ops;
2217 Ops.push_back(Op1);
2218 Ops.push_back(Op2);
2219 Ops.push_back(Op3);
2220 Ops.push_back(Op4);
2221 Ops.push_back(Op5);
2222 Ops.push_back(Op6);
2223 Ops.push_back(Op7);
2224 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2225}
2226SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2227 MVT::ValueType VT2, MVT::ValueType VT3,
2228 SDOperand Op1, SDOperand Op2) {
2229 std::vector<MVT::ValueType> ResultTys;
2230 ResultTys.push_back(VT1);
2231 ResultTys.push_back(VT2);
2232 ResultTys.push_back(VT3);
2233 std::vector<SDOperand> Ops;
2234 Ops.push_back(Op1);
2235 Ops.push_back(Op2);
2236 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2237}
2238SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2239 MVT::ValueType VT2, MVT::ValueType VT3,
2240 SDOperand Op1, SDOperand Op2,
2241 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2242 std::vector<MVT::ValueType> ResultTys;
2243 ResultTys.push_back(VT1);
2244 ResultTys.push_back(VT2);
2245 ResultTys.push_back(VT3);
2246 std::vector<SDOperand> Ops;
2247 Ops.push_back(Op1);
2248 Ops.push_back(Op2);
2249 Ops.push_back(Op3);
2250 Ops.push_back(Op4);
2251 Ops.push_back(Op5);
2252 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2253}
2254SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2255 MVT::ValueType VT2, MVT::ValueType VT3,
2256 SDOperand Op1, SDOperand Op2,
2257 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2258 SDOperand Op6) {
2259 std::vector<MVT::ValueType> ResultTys;
2260 ResultTys.push_back(VT1);
2261 ResultTys.push_back(VT2);
2262 ResultTys.push_back(VT3);
2263 std::vector<SDOperand> Ops;
2264 Ops.push_back(Op1);
2265 Ops.push_back(Op2);
2266 Ops.push_back(Op3);
2267 Ops.push_back(Op4);
2268 Ops.push_back(Op5);
2269 Ops.push_back(Op6);
2270 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2271}
2272SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2273 MVT::ValueType VT2, MVT::ValueType VT3,
2274 SDOperand Op1, SDOperand Op2,
2275 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2276 SDOperand Op6, SDOperand Op7) {
2277 std::vector<MVT::ValueType> ResultTys;
2278 ResultTys.push_back(VT1);
2279 ResultTys.push_back(VT2);
2280 ResultTys.push_back(VT3);
2281 std::vector<SDOperand> Ops;
2282 Ops.push_back(Op1);
2283 Ops.push_back(Op2);
2284 Ops.push_back(Op3);
2285 Ops.push_back(Op4);
2286 Ops.push_back(Op5);
2287 Ops.push_back(Op6);
2288 Ops.push_back(Op7);
2289 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2290}
2291SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2292 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2293 std::vector<MVT::ValueType> ResultTys;
2294 ResultTys.push_back(VT1);
2295 ResultTys.push_back(VT2);
2296 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2297}
2298
Chris Lattner0fb094f2005-11-19 01:44:53 +00002299// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002300/// This can cause recursive merging of nodes in the DAG.
2301///
Chris Lattner8b52f212005-08-26 18:36:28 +00002302/// This version assumes From/To have a single result value.
2303///
Chris Lattner1e111c72005-09-07 05:37:01 +00002304void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2305 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002306 SDNode *From = FromN.Val, *To = ToN.Val;
2307 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2308 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002309 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002310
Chris Lattner8b8749f2005-08-17 19:00:20 +00002311 while (!From->use_empty()) {
2312 // Process users until they are all gone.
2313 SDNode *U = *From->use_begin();
2314
2315 // This node is about to morph, remove its old self from the CSE maps.
2316 RemoveNodeFromCSEMaps(U);
2317
Chris Lattner65113b22005-11-08 22:07:03 +00002318 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2319 I != E; ++I)
2320 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002321 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002322 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002323 To->addUser(U);
2324 }
2325
2326 // Now that we have modified U, add it back to the CSE maps. If it already
2327 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002328 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2329 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002330 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002331 if (Deleted) Deleted->push_back(U);
2332 DeleteNodeNotInCSEMaps(U);
2333 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002334 }
2335}
2336
2337/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2338/// This can cause recursive merging of nodes in the DAG.
2339///
2340/// This version assumes From/To have matching types and numbers of result
2341/// values.
2342///
Chris Lattner1e111c72005-09-07 05:37:01 +00002343void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2344 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002345 assert(From != To && "Cannot replace uses of with self");
2346 assert(From->getNumValues() == To->getNumValues() &&
2347 "Cannot use this version of ReplaceAllUsesWith!");
2348 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002349 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002350 return;
2351 }
2352
2353 while (!From->use_empty()) {
2354 // Process users until they are all gone.
2355 SDNode *U = *From->use_begin();
2356
2357 // This node is about to morph, remove its old self from the CSE maps.
2358 RemoveNodeFromCSEMaps(U);
2359
Chris Lattner65113b22005-11-08 22:07:03 +00002360 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2361 I != E; ++I)
2362 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002363 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002364 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002365 To->addUser(U);
2366 }
2367
2368 // Now that we have modified U, add it back to the CSE maps. If it already
2369 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002370 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2371 ReplaceAllUsesWith(U, Existing, Deleted);
2372 // U is now dead.
2373 if (Deleted) Deleted->push_back(U);
2374 DeleteNodeNotInCSEMaps(U);
2375 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002376 }
2377}
2378
Chris Lattner8b52f212005-08-26 18:36:28 +00002379/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2380/// This can cause recursive merging of nodes in the DAG.
2381///
2382/// This version can replace From with any result values. To must match the
2383/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002384void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002385 const std::vector<SDOperand> &To,
2386 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002387 assert(From->getNumValues() == To.size() &&
2388 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002389 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002390 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002391 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002392 return;
2393 }
2394
2395 while (!From->use_empty()) {
2396 // Process users until they are all gone.
2397 SDNode *U = *From->use_begin();
2398
2399 // This node is about to morph, remove its old self from the CSE maps.
2400 RemoveNodeFromCSEMaps(U);
2401
Chris Lattner65113b22005-11-08 22:07:03 +00002402 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2403 I != E; ++I)
2404 if (I->Val == From) {
2405 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002406 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002407 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002408 ToOp.Val->addUser(U);
2409 }
2410
2411 // Now that we have modified U, add it back to the CSE maps. If it already
2412 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002413 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2414 ReplaceAllUsesWith(U, Existing, Deleted);
2415 // U is now dead.
2416 if (Deleted) Deleted->push_back(U);
2417 DeleteNodeNotInCSEMaps(U);
2418 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002419 }
2420}
2421
Chris Lattner012f2412006-02-17 21:58:01 +00002422/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2423/// uses of other values produced by From.Val alone. The Deleted vector is
2424/// handled the same was as for ReplaceAllUsesWith.
2425void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2426 std::vector<SDNode*> &Deleted) {
2427 assert(From != To && "Cannot replace a value with itself");
2428 // Handle the simple, trivial, case efficiently.
2429 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2430 ReplaceAllUsesWith(From, To, &Deleted);
2431 return;
2432 }
2433
2434 // Get all of the users in a nice, deterministically ordered, uniqued set.
2435 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2436
2437 while (!Users.empty()) {
2438 // We know that this user uses some value of From. If it is the right
2439 // value, update it.
2440 SDNode *User = Users.back();
2441 Users.pop_back();
2442
2443 for (SDOperand *Op = User->OperandList,
2444 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2445 if (*Op == From) {
2446 // Okay, we know this user needs to be updated. Remove its old self
2447 // from the CSE maps.
2448 RemoveNodeFromCSEMaps(User);
2449
2450 // Update all operands that match "From".
2451 for (; Op != E; ++Op) {
2452 if (*Op == From) {
2453 From.Val->removeUser(User);
2454 *Op = To;
2455 To.Val->addUser(User);
2456 }
2457 }
2458
2459 // Now that we have modified User, add it back to the CSE maps. If it
2460 // already exists there, recursively merge the results together.
2461 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2462 unsigned NumDeleted = Deleted.size();
2463 ReplaceAllUsesWith(User, Existing, &Deleted);
2464
2465 // User is now dead.
2466 Deleted.push_back(User);
2467 DeleteNodeNotInCSEMaps(User);
2468
2469 // We have to be careful here, because ReplaceAllUsesWith could have
2470 // deleted a user of From, which means there may be dangling pointers
2471 // in the "Users" setvector. Scan over the deleted node pointers and
2472 // remove them from the setvector.
2473 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2474 Users.remove(Deleted[i]);
2475 }
2476 break; // Exit the operand scanning loop.
2477 }
2478 }
2479 }
2480}
2481
Chris Lattner7b2880c2005-08-24 22:44:39 +00002482
Jim Laskey58b968b2005-08-17 20:08:02 +00002483//===----------------------------------------------------------------------===//
2484// SDNode Class
2485//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002486
Chris Lattnera3255112005-11-08 23:30:28 +00002487
2488/// getValueTypeList - Return a pointer to the specified value type.
2489///
2490MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2491 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2492 VTs[VT] = VT;
2493 return &VTs[VT];
2494}
2495
Chris Lattner5c884562005-01-12 18:37:47 +00002496/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2497/// indicated value. This method ignores uses of other values defined by this
2498/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002499bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002500 assert(Value < getNumValues() && "Bad value!");
2501
2502 // If there is only one value, this is easy.
2503 if (getNumValues() == 1)
2504 return use_size() == NUses;
2505 if (Uses.size() < NUses) return false;
2506
Evan Cheng4ee62112006-02-05 06:29:23 +00002507 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002508
2509 std::set<SDNode*> UsersHandled;
2510
Evan Cheng4ee62112006-02-05 06:29:23 +00002511 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002512 UI != E; ++UI) {
2513 SDNode *User = *UI;
2514 if (User->getNumOperands() == 1 ||
2515 UsersHandled.insert(User).second) // First time we've seen this?
2516 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2517 if (User->getOperand(i) == TheValue) {
2518 if (NUses == 0)
2519 return false; // too many uses
2520 --NUses;
2521 }
2522 }
2523
2524 // Found exactly the right number of uses?
2525 return NUses == 0;
2526}
2527
2528
Evan Cheng4ee62112006-02-05 06:29:23 +00002529// isOnlyUse - Return true if this node is the only use of N.
2530bool SDNode::isOnlyUse(SDNode *N) const {
2531 bool Seen = false;
2532 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2533 SDNode *User = *I;
2534 if (User == this)
2535 Seen = true;
2536 else
2537 return false;
2538 }
2539
2540 return Seen;
2541}
2542
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002543// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002544bool SDOperand::isOperand(SDNode *N) const {
2545 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2546 if (*this == N->getOperand(i))
2547 return true;
2548 return false;
2549}
2550
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002551bool SDNode::isOperand(SDNode *N) const {
2552 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2553 if (this == N->OperandList[i].Val)
2554 return true;
2555 return false;
2556}
Evan Cheng4ee62112006-02-05 06:29:23 +00002557
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002558const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002559 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002560 default:
2561 if (getOpcode() < ISD::BUILTIN_OP_END)
2562 return "<<Unknown DAG Node>>";
2563 else {
Evan Cheng72261582005-12-20 06:22:03 +00002564 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002565 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002566 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2567 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002568
Evan Cheng72261582005-12-20 06:22:03 +00002569 TargetLowering &TLI = G->getTargetLoweringInfo();
2570 const char *Name =
2571 TLI.getTargetNodeName(getOpcode());
2572 if (Name) return Name;
2573 }
2574
2575 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002576 }
2577
Andrew Lenharth95762122005-03-31 21:24:06 +00002578 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002579 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002580 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002581 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002582 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002583 case ISD::AssertSext: return "AssertSext";
2584 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002585
2586 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002587 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002588 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002589 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002590
2591 case ISD::Constant: return "Constant";
2592 case ISD::ConstantFP: return "ConstantFP";
2593 case ISD::GlobalAddress: return "GlobalAddress";
2594 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002595 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002596 case ISD::ExternalSymbol: return "ExternalSymbol";
2597
2598 case ISD::ConstantVec: return "ConstantVec";
2599 case ISD::TargetConstant: return "TargetConstant";
2600 case ISD::TargetConstantFP:return "TargetConstantFP";
2601 case ISD::TargetConstantVec:return "TargetConstantVec";
2602 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2603 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002604 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002605 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2606 case ISD::VConstant: return "VConstant";
2607
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002608 case ISD::CopyToReg: return "CopyToReg";
2609 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002610 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002611 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002612 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002613 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002614
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002615 // Unary operators
2616 case ISD::FABS: return "fabs";
2617 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002618 case ISD::FSQRT: return "fsqrt";
2619 case ISD::FSIN: return "fsin";
2620 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002621
2622 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002623 case ISD::ADD: return "add";
2624 case ISD::SUB: return "sub";
2625 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002626 case ISD::MULHU: return "mulhu";
2627 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002628 case ISD::SDIV: return "sdiv";
2629 case ISD::UDIV: return "udiv";
2630 case ISD::SREM: return "srem";
2631 case ISD::UREM: return "urem";
2632 case ISD::AND: return "and";
2633 case ISD::OR: return "or";
2634 case ISD::XOR: return "xor";
2635 case ISD::SHL: return "shl";
2636 case ISD::SRA: return "sra";
2637 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002638 case ISD::ROTL: return "rotl";
2639 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002640 case ISD::FADD: return "fadd";
2641 case ISD::FSUB: return "fsub";
2642 case ISD::FMUL: return "fmul";
2643 case ISD::FDIV: return "fdiv";
2644 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002645 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002646 case ISD::VADD: return "vadd";
2647 case ISD::VSUB: return "vsub";
2648 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002649
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002650 case ISD::SETCC: return "setcc";
2651 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002652 case ISD::SELECT_CC: return "select_cc";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002653 case ISD::ADDC: return "addc";
2654 case ISD::ADDE: return "adde";
2655 case ISD::SUBC: return "subc";
2656 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002657 case ISD::SHL_PARTS: return "shl_parts";
2658 case ISD::SRA_PARTS: return "sra_parts";
2659 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002660
Chris Lattner7f644642005-04-28 21:44:03 +00002661 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002662 case ISD::SIGN_EXTEND: return "sign_extend";
2663 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002664 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002665 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002666 case ISD::TRUNCATE: return "truncate";
2667 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002668 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002669 case ISD::FP_EXTEND: return "fp_extend";
2670
2671 case ISD::SINT_TO_FP: return "sint_to_fp";
2672 case ISD::UINT_TO_FP: return "uint_to_fp";
2673 case ISD::FP_TO_SINT: return "fp_to_sint";
2674 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002675 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002676
2677 // Control flow instructions
2678 case ISD::BR: return "br";
2679 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002680 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002681 case ISD::BR_CC: return "br_cc";
2682 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002683 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002684 case ISD::CALLSEQ_START: return "callseq_start";
2685 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002686
2687 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002688 case ISD::LOAD: return "load";
2689 case ISD::STORE: return "store";
2690 case ISD::VLOAD: return "vload";
2691 case ISD::EXTLOAD: return "extload";
2692 case ISD::SEXTLOAD: return "sextload";
2693 case ISD::ZEXTLOAD: return "zextload";
2694 case ISD::TRUNCSTORE: return "truncstore";
2695 case ISD::VAARG: return "vaarg";
2696 case ISD::VACOPY: return "vacopy";
2697 case ISD::VAEND: return "vaend";
2698 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002699 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002700 case ISD::EXTRACT_ELEMENT: return "extract_element";
2701 case ISD::BUILD_PAIR: return "build_pair";
2702 case ISD::STACKSAVE: return "stacksave";
2703 case ISD::STACKRESTORE: return "stackrestore";
2704
2705 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002706 case ISD::MEMSET: return "memset";
2707 case ISD::MEMCPY: return "memcpy";
2708 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002709
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002710 // Bit manipulation
2711 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002712 case ISD::CTPOP: return "ctpop";
2713 case ISD::CTTZ: return "cttz";
2714 case ISD::CTLZ: return "ctlz";
2715
Chris Lattner36ce6912005-11-29 06:21:05 +00002716 // Debug info
2717 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002718 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002719 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002720
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002721 case ISD::CONDCODE:
2722 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002723 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002724 case ISD::SETOEQ: return "setoeq";
2725 case ISD::SETOGT: return "setogt";
2726 case ISD::SETOGE: return "setoge";
2727 case ISD::SETOLT: return "setolt";
2728 case ISD::SETOLE: return "setole";
2729 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002730
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002731 case ISD::SETO: return "seto";
2732 case ISD::SETUO: return "setuo";
2733 case ISD::SETUEQ: return "setue";
2734 case ISD::SETUGT: return "setugt";
2735 case ISD::SETUGE: return "setuge";
2736 case ISD::SETULT: return "setult";
2737 case ISD::SETULE: return "setule";
2738 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002739
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002740 case ISD::SETEQ: return "seteq";
2741 case ISD::SETGT: return "setgt";
2742 case ISD::SETGE: return "setge";
2743 case ISD::SETLT: return "setlt";
2744 case ISD::SETLE: return "setle";
2745 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002746 }
2747 }
2748}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002749
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002750void SDNode::dump() const { dump(0); }
2751void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002752 std::cerr << (void*)this << ": ";
2753
2754 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2755 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002756 if (getValueType(i) == MVT::Other)
2757 std::cerr << "ch";
2758 else
2759 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002760 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002761 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002762
2763 std::cerr << " ";
2764 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2765 if (i) std::cerr << ", ";
2766 std::cerr << (void*)getOperand(i).Val;
2767 if (unsigned RN = getOperand(i).ResNo)
2768 std::cerr << ":" << RN;
2769 }
2770
2771 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2772 std::cerr << "<" << CSDN->getValue() << ">";
2773 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2774 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002775 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002776 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002777 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002778 std::cerr << "<";
2779 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002780 if (offset > 0)
2781 std::cerr << " + " << offset;
2782 else
2783 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002784 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002785 std::cerr << "<" << FIDN->getIndex() << ">";
2786 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002787 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002788 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002789 if (offset > 0)
2790 std::cerr << " + " << offset;
2791 else
2792 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002793 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002794 std::cerr << "<";
2795 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2796 if (LBB)
2797 std::cerr << LBB->getName() << " ";
2798 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002799 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002800 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002801 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2802 } else {
2803 std::cerr << " #" << R->getReg();
2804 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002805 } else if (const ExternalSymbolSDNode *ES =
2806 dyn_cast<ExternalSymbolSDNode>(this)) {
2807 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002808 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2809 if (M->getValue())
2810 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2811 else
2812 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002813 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2814 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002815 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002816}
2817
Chris Lattnerde202b32005-11-09 23:47:37 +00002818static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002819 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2820 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002821 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002822 else
2823 std::cerr << "\n" << std::string(indent+2, ' ')
2824 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002825
Chris Lattnerea946cd2005-01-09 20:38:33 +00002826
2827 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002828 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002829}
2830
Chris Lattnerc3aae252005-01-07 07:46:32 +00002831void SelectionDAG::dump() const {
2832 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002833 std::vector<const SDNode*> Nodes;
2834 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2835 I != E; ++I)
2836 Nodes.push_back(I);
2837
Chris Lattner49d24712005-01-09 20:26:36 +00002838 std::sort(Nodes.begin(), Nodes.end());
2839
2840 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002841 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002842 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002843 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002844
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002845 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002846
Chris Lattnerc3aae252005-01-07 07:46:32 +00002847 std::cerr << "\n\n";
2848}
2849
Evan Chengfae9f1c2006-02-09 22:11:03 +00002850/// InsertISelMapEntry - A helper function to insert a key / element pair
2851/// into a SDOperand to SDOperand map. This is added to avoid the map
2852/// insertion operator from being inlined.
2853void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2854 SDNode *Key, unsigned KeyResNo,
2855 SDNode *Element, unsigned ElementResNo) {
2856 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2857 SDOperand(Element, ElementResNo)));
2858}