blob: b510f1cd959ee7bcbc5123fad824d18e571b5743 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Evan Cheng115c0362005-12-19 23:11:49 +000024#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000026#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000027#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000028#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner5cdcc582005-01-09 20:52:51 +000031static bool isCommutativeBinOp(unsigned Opcode) {
32 switch (Opcode) {
33 case ISD::ADD:
34 case ISD::MUL:
Chris Lattner01b3d732005-09-28 22:28:18 +000035 case ISD::FADD:
36 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000037 case ISD::AND:
38 case ISD::OR:
39 case ISD::XOR: return true;
40 default: return false; // FIXME: Need commutative info for user ops!
41 }
42}
43
44static bool isAssociativeBinOp(unsigned Opcode) {
45 switch (Opcode) {
46 case ISD::ADD:
47 case ISD::MUL:
48 case ISD::AND:
49 case ISD::OR:
50 case ISD::XOR: return true;
51 default: return false; // FIXME: Need associative info for user ops!
52 }
53}
54
Chris Lattner5cdcc582005-01-09 20:52:51 +000055// isInvertibleForFree - Return true if there is no cost to emitting the logical
56// inverse of this node.
57static bool isInvertibleForFree(SDOperand N) {
58 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000059 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000060 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000061 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000062}
63
Jim Laskey58b968b2005-08-17 20:08:02 +000064//===----------------------------------------------------------------------===//
65// ConstantFPSDNode Class
66//===----------------------------------------------------------------------===//
67
68/// isExactlyValue - We don't rely on operator== working on double values, as
69/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
70/// As such, this method can be used to do an exact bit-for-bit comparison of
71/// two floating point values.
72bool ConstantFPSDNode::isExactlyValue(double V) const {
73 return DoubleToBits(V) == DoubleToBits(Value);
74}
75
76//===----------------------------------------------------------------------===//
77// ISD Class
78//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000079
Chris Lattnerc3aae252005-01-07 07:46:32 +000080/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
81/// when given the operation for (X op Y).
82ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
83 // To perform this operation, we just need to swap the L and G bits of the
84 // operation.
85 unsigned OldL = (Operation >> 2) & 1;
86 unsigned OldG = (Operation >> 1) & 1;
87 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
88 (OldL << 1) | // New G bit
89 (OldG << 2)); // New L bit.
90}
91
92/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
93/// 'op' is a valid SetCC operation.
94ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
95 unsigned Operation = Op;
96 if (isInteger)
97 Operation ^= 7; // Flip L, G, E bits, but not U.
98 else
99 Operation ^= 15; // Flip all of the condition bits.
100 if (Operation > ISD::SETTRUE2)
101 Operation &= ~8; // Don't let N and U bits get set.
102 return ISD::CondCode(Operation);
103}
104
105
106/// isSignedOp - For an integer comparison, return 1 if the comparison is a
107/// signed operation and 2 if the result is an unsigned comparison. Return zero
108/// if the operation does not depend on the sign of the input (setne and seteq).
109static int isSignedOp(ISD::CondCode Opcode) {
110 switch (Opcode) {
111 default: assert(0 && "Illegal integer setcc operation!");
112 case ISD::SETEQ:
113 case ISD::SETNE: return 0;
114 case ISD::SETLT:
115 case ISD::SETLE:
116 case ISD::SETGT:
117 case ISD::SETGE: return 1;
118 case ISD::SETULT:
119 case ISD::SETULE:
120 case ISD::SETUGT:
121 case ISD::SETUGE: return 2;
122 }
123}
124
125/// getSetCCOrOperation - Return the result of a logical OR between different
126/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
127/// returns SETCC_INVALID if it is not possible to represent the resultant
128/// comparison.
129ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
130 bool isInteger) {
131 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
132 // Cannot fold a signed integer setcc with an unsigned integer setcc.
133 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000134
Chris Lattnerc3aae252005-01-07 07:46:32 +0000135 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnerc3aae252005-01-07 07:46:32 +0000137 // If the N and U bits get set then the resultant comparison DOES suddenly
138 // care about orderedness, and is true when ordered.
139 if (Op > ISD::SETTRUE2)
140 Op &= ~16; // Clear the N bit.
141 return ISD::CondCode(Op);
142}
143
144/// getSetCCAndOperation - Return the result of a logical AND between different
145/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
146/// function returns zero if it is not possible to represent the resultant
147/// comparison.
148ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
149 bool isInteger) {
150 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
151 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000152 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000153
154 // Combine all of the condition bits.
155 return ISD::CondCode(Op1 & Op2);
156}
157
Chris Lattnerb48da392005-01-23 04:39:44 +0000158const TargetMachine &SelectionDAG::getTarget() const {
159 return TLI.getTargetMachine();
160}
161
Jim Laskey58b968b2005-08-17 20:08:02 +0000162//===----------------------------------------------------------------------===//
163// SelectionDAG Class
164//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000165
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000166/// RemoveDeadNodes - This method deletes all unreachable nodes in the
167/// SelectionDAG, including nodes (like loads) that have uses of their token
168/// chain but no other uses and no side effect. If a node is passed in as an
169/// argument, it is used as the seed for node deletion.
170void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000171 // Create a dummy node (which is not added to allnodes), that adds a reference
172 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000173 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000174
Chris Lattnerf469cb62005-11-08 18:52:27 +0000175 bool MadeChange = false;
176
Chris Lattner8b8749f2005-08-17 19:00:20 +0000177 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000178 if (N && N->use_empty()) {
179 DestroyDeadNode(N);
180 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000181 }
182
Chris Lattnerde202b32005-11-09 23:47:37 +0000183 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
184 if (I->use_empty() && I->getOpcode() != 65535) {
185 // Node is dead, recursively delete newly dead uses.
186 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000187 MadeChange = true;
188 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000189
190 // Walk the nodes list, removing the nodes we've marked as dead.
191 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000192 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
193 SDNode *N = I++;
194 if (N->use_empty())
195 AllNodes.erase(N);
196 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000197 }
198
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000199 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000200 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000201}
202
Chris Lattnerf469cb62005-11-08 18:52:27 +0000203/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
204/// graph. If it is the last user of any of its operands, recursively process
205/// them the same way.
206///
207void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000208 // Okay, we really are going to delete this node. First take this out of the
209 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000210 RemoveNodeFromCSEMaps(N);
211
212 // Next, brutally remove the operand list. This is safe to do, as there are
213 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000214 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
215 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000216 O->removeUser(N);
217
218 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000219 if (O->use_empty())
220 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000221 }
Chris Lattner65113b22005-11-08 22:07:03 +0000222 delete[] N->OperandList;
223 N->OperandList = 0;
224 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000225
226 // Mark the node as dead.
227 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000228}
229
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000230void SelectionDAG::DeleteNode(SDNode *N) {
231 assert(N->use_empty() && "Cannot delete a node that is not dead!");
232
233 // First take this out of the appropriate CSE map.
234 RemoveNodeFromCSEMaps(N);
235
Chris Lattner1e111c72005-09-07 05:37:01 +0000236 // Finally, remove uses due to operands of this node, remove from the
237 // AllNodes list, and delete the node.
238 DeleteNodeNotInCSEMaps(N);
239}
240
241void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
242
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000243 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000244 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000245
246 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000247 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
248 I->Val->removeUser(N);
249 delete[] N->OperandList;
250 N->OperandList = 0;
251 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000252
253 delete N;
254}
255
Chris Lattner149c58c2005-08-16 18:17:10 +0000256/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
257/// correspond to it. This is useful when we're about to delete or repurpose
258/// the node. We don't want future request for structurally identical nodes
259/// to return N anymore.
260void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000261 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000262 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000263 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000265 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
266 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000267 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000268 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000269 Erased = TargetConstants.erase(std::make_pair(
270 cast<ConstantSDNode>(N)->getValue(),
271 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000272 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000273 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000274 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000275 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000276 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000277 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000278 case ISD::STRING:
279 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
280 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000281 case ISD::CONDCODE:
282 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
283 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000284 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000285 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
286 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000287 case ISD::GlobalAddress: {
288 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
289 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
290 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000291 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000292 }
293 case ISD::TargetGlobalAddress: {
294 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
295 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
296 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000297 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000298 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000300 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000302 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000303 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000304 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000305 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000306 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000308 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000309 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000310 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000311 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000312 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 break;
314 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000315 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000316 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000317 case ISD::TargetExternalSymbol:
318 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
319 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000320 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000321 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000322 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
323 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000324 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000325 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
326 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000327 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000328 case ISD::SRCVALUE: {
329 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000330 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000331 break;
332 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000333 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 Erased = Loads.erase(std::make_pair(N->getOperand(1),
335 std::make_pair(N->getOperand(0),
336 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000337 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000338 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000339 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000340 if (N->getNumOperands() == 0) {
341 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
342 N->getValueType(0)));
343 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000344 Erased =
345 UnaryOps.erase(std::make_pair(N->getOpcode(),
346 std::make_pair(N->getOperand(0),
347 N->getValueType(0))));
348 } else if (N->getNumOperands() == 2) {
349 Erased =
350 BinaryOps.erase(std::make_pair(N->getOpcode(),
351 std::make_pair(N->getOperand(0),
352 N->getOperand(1))));
353 } else {
354 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
355 Erased =
356 OneResultNodes.erase(std::make_pair(N->getOpcode(),
357 std::make_pair(N->getValueType(0),
358 Ops)));
359 }
Chris Lattner385328c2005-05-14 07:42:29 +0000360 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000361 // Remove the node from the ArbitraryNodes map.
362 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
363 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000364 Erased =
365 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
366 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000367 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000368 break;
369 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000370#ifndef NDEBUG
371 // Verify that the node was actually in one of the CSE maps, unless it has a
372 // flag result (which cannot be CSE'd) or is one of the special cases that are
373 // not subject to CSE.
374 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
375 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000376 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000377
378 N->dump();
379 assert(0 && "Node is not in map!");
380 }
381#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000382}
383
Chris Lattner8b8749f2005-08-17 19:00:20 +0000384/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
385/// has been taken out and modified in some way. If the specified node already
386/// exists in the CSE maps, do not modify the maps, but return the existing node
387/// instead. If it doesn't exist, add it and return null.
388///
389SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
390 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000391 if (N->getOpcode() == ISD::CALLSEQ_START ||
Chris Lattnerfe14b342005-12-01 23:14:50 +0000392 N->getOpcode() == ISD::CALLSEQ_END ||
Chris Lattner9f8cc692005-12-19 22:21:21 +0000393 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(),
417 std::make_pair(N->getValueType(0), Ops))];
418 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
441
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000442
Chris Lattner78ec3112003-08-11 14:57:33 +0000443SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000444 while (!AllNodes.empty()) {
445 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000446 delete [] N->OperandList;
447 N->OperandList = 0;
448 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000449 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000450 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000451}
452
Chris Lattner0f2287b2005-04-13 02:38:18 +0000453SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000454 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000455 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000456 return getNode(ISD::AND, Op.getValueType(), Op,
457 getConstant(Imm, Op.getValueType()));
458}
459
Chris Lattnerc3aae252005-01-07 07:46:32 +0000460SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
461 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
462 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000463 if (VT != MVT::i64)
464 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000465
Chris Lattnerc3aae252005-01-07 07:46:32 +0000466 SDNode *&N = Constants[std::make_pair(Val, VT)];
467 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000468 N = new ConstantSDNode(false, Val, VT);
469 AllNodes.push_back(N);
470 return SDOperand(N, 0);
471}
472
Chris Lattner36ce6912005-11-29 06:21:05 +0000473SDOperand SelectionDAG::getString(const std::string &Val) {
474 StringSDNode *&N = StringNodes[Val];
475 if (!N) {
476 N = new StringSDNode(Val);
477 AllNodes.push_back(N);
478 }
479 return SDOperand(N, 0);
480}
481
Chris Lattner37bfbb42005-08-17 00:34:06 +0000482SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
483 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
484 // Mask out any bits that are not valid for this constant.
485 if (VT != MVT::i64)
486 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
487
488 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
489 if (N) return SDOperand(N, 0);
490 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000491 AllNodes.push_back(N);
492 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000493}
494
Chris Lattnerc3aae252005-01-07 07:46:32 +0000495SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
496 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
497 if (VT == MVT::f32)
498 Val = (float)Val; // Mask out extra precision.
499
Chris Lattnerd8658612005-02-17 20:17:32 +0000500 // Do the map lookup using the actual bit pattern for the floating point
501 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
502 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000503 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000504 if (N) return SDOperand(N, 0);
505 N = new ConstantFPSDNode(Val, VT);
506 AllNodes.push_back(N);
507 return SDOperand(N, 0);
508}
509
Chris Lattnerc3aae252005-01-07 07:46:32 +0000510SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000511 MVT::ValueType VT, int offset) {
512 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000513 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000514 N = new GlobalAddressSDNode(false, GV, VT);
515 AllNodes.push_back(N);
516 return SDOperand(N, 0);
517}
518
519SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000520 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000521 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000522 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000523 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000524 AllNodes.push_back(N);
525 return SDOperand(N, 0);
526}
527
528SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
529 SDNode *&N = FrameIndices[FI];
530 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000531 N = new FrameIndexSDNode(FI, VT, false);
532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
534}
535
536SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
537 SDNode *&N = TargetFrameIndices[FI];
538 if (N) return SDOperand(N, 0);
539 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
542}
543
Chris Lattner5839bf22005-08-26 17:15:30 +0000544SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
545 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000546 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000547 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
550}
551
Chris Lattner5839bf22005-08-26 17:15:30 +0000552SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
553 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000554 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000555 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000556 AllNodes.push_back(N);
557 return SDOperand(N, 0);
558}
559
560SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
561 SDNode *&N = BBNodes[MBB];
562 if (N) return SDOperand(N, 0);
563 N = new BasicBlockSDNode(MBB);
564 AllNodes.push_back(N);
565 return SDOperand(N, 0);
566}
567
Chris Lattner15e4b012005-07-10 00:07:11 +0000568SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
569 if ((unsigned)VT >= ValueTypeNodes.size())
570 ValueTypeNodes.resize(VT+1);
571 if (ValueTypeNodes[VT] == 0) {
572 ValueTypeNodes[VT] = new VTSDNode(VT);
573 AllNodes.push_back(ValueTypeNodes[VT]);
574 }
575
576 return SDOperand(ValueTypeNodes[VT], 0);
577}
578
Chris Lattnerc3aae252005-01-07 07:46:32 +0000579SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
580 SDNode *&N = ExternalSymbols[Sym];
581 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000582 N = new ExternalSymbolSDNode(false, Sym, VT);
583 AllNodes.push_back(N);
584 return SDOperand(N, 0);
585}
586
587SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
588 SDNode *&N = TargetExternalSymbols[Sym];
589 if (N) return SDOperand(N, 0);
590 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000591 AllNodes.push_back(N);
592 return SDOperand(N, 0);
593}
594
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000595SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
596 if ((unsigned)Cond >= CondCodeNodes.size())
597 CondCodeNodes.resize(Cond+1);
598
Chris Lattner079a27a2005-08-09 20:40:02 +0000599 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000600 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000601 AllNodes.push_back(CondCodeNodes[Cond]);
602 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000603 return SDOperand(CondCodeNodes[Cond], 0);
604}
605
Chris Lattner0fdd7682005-08-30 22:38:38 +0000606SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
607 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
608 if (!Reg) {
609 Reg = new RegisterSDNode(RegNo, VT);
610 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000611 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000612 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000613}
614
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000615SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
616 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617 // These setcc operations always fold.
618 switch (Cond) {
619 default: break;
620 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000621 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000622 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000623 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000624 }
625
Chris Lattner67255a12005-04-07 18:14:58 +0000626 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
627 uint64_t C2 = N2C->getValue();
628 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
629 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000630
Chris Lattnerc3aae252005-01-07 07:46:32 +0000631 // Sign extend the operands if required
632 if (ISD::isSignedIntSetCC(Cond)) {
633 C1 = N1C->getSignExtended();
634 C2 = N2C->getSignExtended();
635 }
636
637 switch (Cond) {
638 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000639 case ISD::SETEQ: return getConstant(C1 == C2, VT);
640 case ISD::SETNE: return getConstant(C1 != C2, VT);
641 case ISD::SETULT: return getConstant(C1 < C2, VT);
642 case ISD::SETUGT: return getConstant(C1 > C2, VT);
643 case ISD::SETULE: return getConstant(C1 <= C2, VT);
644 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
645 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
646 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
647 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
648 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000649 }
Chris Lattner24673922005-04-07 18:58:54 +0000650 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000651 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000652 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
653 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
654
655 // If the comparison constant has bits in the upper part, the
656 // zero-extended value could never match.
657 if (C2 & (~0ULL << InSize)) {
658 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
659 switch (Cond) {
660 case ISD::SETUGT:
661 case ISD::SETUGE:
662 case ISD::SETEQ: return getConstant(0, VT);
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETNE: return getConstant(1, VT);
666 case ISD::SETGT:
667 case ISD::SETGE:
668 // True if the sign bit of C2 is set.
669 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
670 case ISD::SETLT:
671 case ISD::SETLE:
672 // True if the sign bit of C2 isn't set.
673 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
674 default:
675 break;
676 }
677 }
678
679 // Otherwise, we can perform the comparison with the low bits.
680 switch (Cond) {
681 case ISD::SETEQ:
682 case ISD::SETNE:
683 case ISD::SETUGT:
684 case ISD::SETUGE:
685 case ISD::SETULT:
686 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000687 return getSetCC(VT, N1.getOperand(0),
688 getConstant(C2, N1.getOperand(0).getValueType()),
689 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000690 default:
691 break; // todo, be more careful with signed comparisons
692 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000693 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
694 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
695 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
696 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
697 MVT::ValueType ExtDstTy = N1.getValueType();
698 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000699
Chris Lattner7b2880c2005-08-24 22:44:39 +0000700 // If the extended part has any inconsistent bits, it cannot ever
701 // compare equal. In other words, they have to be all ones or all
702 // zeros.
703 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000704 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000705 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
706 return getConstant(Cond == ISD::SETNE, VT);
707
708 // Otherwise, make this a use of a zext.
709 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000710 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000711 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000712 }
713
Chris Lattner67255a12005-04-07 18:14:58 +0000714 uint64_t MinVal, MaxVal;
715 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
716 if (ISD::isSignedIntSetCC(Cond)) {
717 MinVal = 1ULL << (OperandBitSize-1);
718 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
719 MaxVal = ~0ULL >> (65-OperandBitSize);
720 else
721 MaxVal = 0;
722 } else {
723 MinVal = 0;
724 MaxVal = ~0ULL >> (64-OperandBitSize);
725 }
726
727 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
728 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
729 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
730 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000731 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
732 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000733 }
734
735 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
736 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
737 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000738 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
739 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000740 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000741
Nate Begeman72ea2812005-04-14 08:56:52 +0000742 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
743 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000744
Nate Begeman72ea2812005-04-14 08:56:52 +0000745 // Canonicalize setgt X, Min --> setne X, Min
746 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000747 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000748
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000749 // If we have setult X, 1, turn it into seteq X, 0
750 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000751 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
752 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000753 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000754 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000755 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
756 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000757
758 // If we have "setcc X, C1", check to see if we can shrink the immediate
759 // by changing cc.
760
761 // SETUGT X, SINTMAX -> SETLT X, 0
762 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
763 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000764 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000765
766 // FIXME: Implement the rest of these.
767
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000768
769 // Fold bit comparisons when we can.
770 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
771 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
772 if (ConstantSDNode *AndRHS =
773 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
774 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
775 // Perform the xform if the AND RHS is a single bit.
776 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
777 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000778 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000779 TLI.getShiftAmountTy()));
780 }
781 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
782 // (X & 8) == 8 --> (X & 8) >> 3
783 // Perform the xform if C2 is a single bit.
784 if ((C2 & (C2-1)) == 0) {
785 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000786 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000787 }
788 }
789 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000790 }
Chris Lattner67255a12005-04-07 18:14:58 +0000791 } else if (isa<ConstantSDNode>(N1.Val)) {
792 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000793 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000794 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000795
796 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
797 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
798 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000799
Chris Lattnerc3aae252005-01-07 07:46:32 +0000800 switch (Cond) {
801 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000802 case ISD::SETEQ: return getConstant(C1 == C2, VT);
803 case ISD::SETNE: return getConstant(C1 != C2, VT);
804 case ISD::SETLT: return getConstant(C1 < C2, VT);
805 case ISD::SETGT: return getConstant(C1 > C2, VT);
806 case ISD::SETLE: return getConstant(C1 <= C2, VT);
807 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000808 }
809 } else {
810 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000811 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812 }
813
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000814 // Could not fold it.
815 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000816}
817
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000819///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000821 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
822 if (!N) {
823 N = new SDNode(Opcode, VT);
824 AllNodes.push_back(N);
825 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826 return SDOperand(N, 0);
827}
828
Chris Lattnerc3aae252005-01-07 07:46:32 +0000829SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
830 SDOperand Operand) {
Chris Lattner94683772005-12-23 05:30:37 +0000831 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
833 uint64_t Val = C->getValue();
834 switch (Opcode) {
835 default: break;
836 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000837 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000838 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
839 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000840 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
841 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000842 case ISD::BIT_CONVERT:
843 if (VT == MVT::f32) {
844 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
845 return getConstantFP(BitsToFloat(Val), VT);
846 } else if (VT == MVT::f64) {
847 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
848 return getConstantFP(BitsToDouble(Val), VT);
849 }
850 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000851 }
852 }
853
Chris Lattner94683772005-12-23 05:30:37 +0000854 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000855 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
856 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000857 case ISD::FNEG:
858 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000859 case ISD::FABS:
860 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000861 case ISD::FP_ROUND:
862 case ISD::FP_EXTEND:
863 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000864 case ISD::FP_TO_SINT:
865 return getConstant((int64_t)C->getValue(), VT);
866 case ISD::FP_TO_UINT:
867 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000868 case ISD::BIT_CONVERT:
869 if (VT == MVT::i32) {
870 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
871 return getConstant(FloatToBits(C->getValue()), VT);
872 } else if (VT == MVT::i64) {
873 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
874 return getConstant(DoubleToBits(C->getValue()), VT);
875 }
876 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000877 }
878
879 unsigned OpOpcode = Operand.Val->getOpcode();
880 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000881 case ISD::TokenFactor:
882 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000883 case ISD::SIGN_EXTEND:
884 if (Operand.getValueType() == VT) return Operand; // noop extension
885 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
886 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
887 break;
888 case ISD::ZERO_EXTEND:
889 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000890 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000891 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000893 case ISD::ANY_EXTEND:
894 if (Operand.getValueType() == VT) return Operand; // noop extension
895 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
896 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
897 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
898 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899 case ISD::TRUNCATE:
900 if (Operand.getValueType() == VT) return Operand; // noop truncate
901 if (OpOpcode == ISD::TRUNCATE)
902 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000903 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
904 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000905 // If the source is smaller than the dest, we still need an extend.
906 if (Operand.Val->getOperand(0).getValueType() < VT)
907 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
908 else if (Operand.Val->getOperand(0).getValueType() > VT)
909 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
910 else
911 return Operand.Val->getOperand(0);
912 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000913 break;
Chris Lattner35481892005-12-23 00:16:34 +0000914 case ISD::BIT_CONVERT:
915 // Basic sanity checking.
916 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
917 "Cannot BIT_CONVERT between two different types!");
918 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +0000919 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
920 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +0000921 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000922 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000923 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
924 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000925 Operand.Val->getOperand(0));
926 if (OpOpcode == ISD::FNEG) // --X -> X
927 return Operand.Val->getOperand(0);
928 break;
929 case ISD::FABS:
930 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
931 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
932 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000933 }
934
Chris Lattner43247a12005-08-25 19:12:10 +0000935 SDNode *N;
936 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
937 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000938 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000939 E = N = new SDNode(Opcode, Operand);
940 } else {
941 N = new SDNode(Opcode, Operand);
942 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000943 N->setValueTypes(VT);
944 AllNodes.push_back(N);
945 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000946}
947
Chris Lattner36019aa2005-04-18 03:48:41 +0000948
949
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
951 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000952#ifndef NDEBUG
953 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000954 case ISD::TokenFactor:
955 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
956 N2.getValueType() == MVT::Other && "Invalid token factor!");
957 break;
Chris Lattner76365122005-01-16 02:23:22 +0000958 case ISD::AND:
959 case ISD::OR:
960 case ISD::XOR:
961 case ISD::UDIV:
962 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000963 case ISD::MULHU:
964 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000965 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
966 // fall through
967 case ISD::ADD:
968 case ISD::SUB:
969 case ISD::MUL:
970 case ISD::SDIV:
971 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000972 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
973 // fall through.
974 case ISD::FADD:
975 case ISD::FSUB:
976 case ISD::FMUL:
977 case ISD::FDIV:
978 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000979 assert(N1.getValueType() == N2.getValueType() &&
980 N1.getValueType() == VT && "Binary operator types must match!");
981 break;
982
983 case ISD::SHL:
984 case ISD::SRA:
985 case ISD::SRL:
986 assert(VT == N1.getValueType() &&
987 "Shift operators return type must be the same as their first arg");
988 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000989 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000990 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000991 case ISD::FP_ROUND_INREG: {
992 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
993 assert(VT == N1.getValueType() && "Not an inreg round!");
994 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
995 "Cannot FP_ROUND_INREG integer types");
996 assert(EVT <= VT && "Not rounding down!");
997 break;
998 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000999 case ISD::AssertSext:
1000 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001001 case ISD::SIGN_EXTEND_INREG: {
1002 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1003 assert(VT == N1.getValueType() && "Not an inreg extend!");
1004 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1005 "Cannot *_EXTEND_INREG FP types");
1006 assert(EVT <= VT && "Not extending!");
1007 }
1008
Chris Lattner76365122005-01-16 02:23:22 +00001009 default: break;
1010 }
1011#endif
1012
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1014 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1015 if (N1C) {
1016 if (N2C) {
1017 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1018 switch (Opcode) {
1019 case ISD::ADD: return getConstant(C1 + C2, VT);
1020 case ISD::SUB: return getConstant(C1 - C2, VT);
1021 case ISD::MUL: return getConstant(C1 * C2, VT);
1022 case ISD::UDIV:
1023 if (C2) return getConstant(C1 / C2, VT);
1024 break;
1025 case ISD::UREM :
1026 if (C2) return getConstant(C1 % C2, VT);
1027 break;
1028 case ISD::SDIV :
1029 if (C2) return getConstant(N1C->getSignExtended() /
1030 N2C->getSignExtended(), VT);
1031 break;
1032 case ISD::SREM :
1033 if (C2) return getConstant(N1C->getSignExtended() %
1034 N2C->getSignExtended(), VT);
1035 break;
1036 case ISD::AND : return getConstant(C1 & C2, VT);
1037 case ISD::OR : return getConstant(C1 | C2, VT);
1038 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001039 case ISD::SHL : return getConstant(C1 << C2, VT);
1040 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001041 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001042 default: break;
1043 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044 } else { // Cannonicalize constant to RHS if commutative
1045 if (isCommutativeBinOp(Opcode)) {
1046 std::swap(N1C, N2C);
1047 std::swap(N1, N2);
1048 }
1049 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001050 }
1051
1052 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1053 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001054 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055 if (N2CFP) {
1056 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1057 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001058 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1059 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1060 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1061 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 if (C2) return getConstantFP(C1 / C2, VT);
1063 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001064 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001065 if (C2) return getConstantFP(fmod(C1, C2), VT);
1066 break;
1067 default: break;
1068 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001069 } else { // Cannonicalize constant to RHS if commutative
1070 if (isCommutativeBinOp(Opcode)) {
1071 std::swap(N1CFP, N2CFP);
1072 std::swap(N1, N2);
1073 }
1074 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001075 }
1076
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 // Finally, fold operations that do not require constants.
1078 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001079 case ISD::FP_ROUND_INREG:
1080 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1081 break;
1082 case ISD::SIGN_EXTEND_INREG: {
1083 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1084 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001085 break;
1086 }
1087
Nate Begemaneea805e2005-04-13 21:23:31 +00001088 // FIXME: figure out how to safely handle things like
1089 // int foo(int x) { return 1 << (x & 255); }
1090 // int bar() { return foo(256); }
1091#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001092 case ISD::SHL:
1093 case ISD::SRL:
1094 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001095 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001096 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001097 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001098 else if (N2.getOpcode() == ISD::AND)
1099 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1100 // If the and is only masking out bits that cannot effect the shift,
1101 // eliminate the and.
1102 unsigned NumBits = MVT::getSizeInBits(VT);
1103 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1104 return getNode(Opcode, VT, N1, N2.getOperand(0));
1105 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001106 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001107#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001108 }
1109
Chris Lattner27e9b412005-05-11 18:57:39 +00001110 // Memoize this node if possible.
1111 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001112 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1113 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001114 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1115 if (BON) return SDOperand(BON, 0);
1116
1117 BON = N = new SDNode(Opcode, N1, N2);
1118 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001119 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001120 }
1121
Chris Lattner3e011362005-05-14 07:45:46 +00001122 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001123 AllNodes.push_back(N);
1124 return SDOperand(N, 0);
1125}
1126
Chris Lattnerc3aae252005-01-07 07:46:32 +00001127SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1128 SDOperand N1, SDOperand N2, SDOperand N3) {
1129 // Perform various simplifications.
1130 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1131 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1132 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1133 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001134 case ISD::SETCC: {
1135 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001136 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001137 if (Simp.Val) return Simp;
1138 break;
1139 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001140 case ISD::SELECT:
1141 if (N1C)
1142 if (N1C->getValue())
1143 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001144 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 return N3; // select false, X, Y -> Y
1146
1147 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001148 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001149 case ISD::BRCOND:
1150 if (N2C)
1151 if (N2C->getValue()) // Unconditional branch
1152 return getNode(ISD::BR, MVT::Other, N1, N3);
1153 else
1154 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001155 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001156 }
1157
Chris Lattner385328c2005-05-14 07:42:29 +00001158 std::vector<SDOperand> Ops;
1159 Ops.reserve(3);
1160 Ops.push_back(N1);
1161 Ops.push_back(N2);
1162 Ops.push_back(N3);
1163
Chris Lattner43247a12005-08-25 19:12:10 +00001164 // Memoize node if it doesn't produce a flag.
1165 SDNode *N;
1166 if (VT != MVT::Flag) {
1167 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1168 if (E) return SDOperand(E, 0);
1169 E = N = new SDNode(Opcode, N1, N2, N3);
1170 } else {
1171 N = new SDNode(Opcode, N1, N2, N3);
1172 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001173 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001174 AllNodes.push_back(N);
1175 return SDOperand(N, 0);
1176}
1177
1178SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001179 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001180 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001181 std::vector<SDOperand> Ops;
1182 Ops.reserve(4);
1183 Ops.push_back(N1);
1184 Ops.push_back(N2);
1185 Ops.push_back(N3);
1186 Ops.push_back(N4);
1187 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001188}
1189
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001190SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1191 SDOperand N1, SDOperand N2, SDOperand N3,
1192 SDOperand N4, SDOperand N5) {
1193 std::vector<SDOperand> Ops;
1194 Ops.reserve(5);
1195 Ops.push_back(N1);
1196 Ops.push_back(N2);
1197 Ops.push_back(N3);
1198 Ops.push_back(N4);
1199 Ops.push_back(N5);
1200 return getNode(Opcode, VT, Ops);
1201}
1202
Evan Cheng7038daf2005-12-10 00:37:58 +00001203SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1204 SDOperand N1, SDOperand N2, SDOperand N3,
1205 SDOperand N4, SDOperand N5, SDOperand N6) {
1206 std::vector<SDOperand> Ops;
1207 Ops.reserve(6);
1208 Ops.push_back(N1);
1209 Ops.push_back(N2);
1210 Ops.push_back(N3);
1211 Ops.push_back(N4);
1212 Ops.push_back(N5);
1213 Ops.push_back(N6);
1214 return getNode(Opcode, VT, Ops);
1215}
1216
1217// setAdjCallChain - This method changes the token chain of an
1218// CALLSEQ_START/END node to be the specified operand.
1219void SDNode::setAdjCallChain(SDOperand N) {
1220 assert(N.getValueType() == MVT::Other);
1221 assert((getOpcode() == ISD::CALLSEQ_START ||
1222 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
1223
1224 OperandList[0].Val->removeUser(this);
1225 OperandList[0] = N;
1226 OperandList[0].Val->Uses.push_back(this);
1227}
1228
1229
1230
1231SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1232 SDOperand Chain, SDOperand Ptr,
1233 SDOperand SV) {
1234 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1235 if (N) return SDOperand(N, 0);
1236 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1237
1238 // Loads have a token chain.
1239 setNodeValueTypes(N, VT, MVT::Other);
1240 AllNodes.push_back(N);
1241 return SDOperand(N, 0);
1242}
1243
1244SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1245 SDOperand Chain, SDOperand Ptr,
1246 SDOperand SV) {
1247 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1248 if (N) return SDOperand(N, 0);
1249 std::vector<SDOperand> Ops;
1250 Ops.reserve(5);
1251 Ops.push_back(Chain);
1252 Ops.push_back(Ptr);
1253 Ops.push_back(getConstant(Count, MVT::i32));
1254 Ops.push_back(getValueType(EVT));
1255 Ops.push_back(SV);
1256 std::vector<MVT::ValueType> VTs;
1257 VTs.reserve(2);
1258 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1259 return getNode(ISD::VLOAD, VTs, Ops);
1260}
1261
1262SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1263 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1264 MVT::ValueType EVT) {
1265 std::vector<SDOperand> Ops;
1266 Ops.reserve(4);
1267 Ops.push_back(Chain);
1268 Ops.push_back(Ptr);
1269 Ops.push_back(SV);
1270 Ops.push_back(getValueType(EVT));
1271 std::vector<MVT::ValueType> VTs;
1272 VTs.reserve(2);
1273 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1274 return getNode(Opcode, VTs, Ops);
1275}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001276
Chris Lattner0437cdd2005-05-09 04:14:13 +00001277SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001278 assert((!V || isa<PointerType>(V->getType())) &&
1279 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001280 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1281 if (N) return SDOperand(N, 0);
1282
1283 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001284 AllNodes.push_back(N);
1285 return SDOperand(N, 0);
1286}
1287
1288SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001289 std::vector<SDOperand> &Ops) {
1290 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001291 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001292 case 1: return getNode(Opcode, VT, Ops[0]);
1293 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1294 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001295 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001296 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001297
Chris Lattner89c34632005-05-14 06:20:26 +00001298 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001299 switch (Opcode) {
1300 default: break;
1301 case ISD::BRCONDTWOWAY:
1302 if (N1C)
1303 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001304 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001305 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001306 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001307 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001308 case ISD::BRTWOWAY_CC:
1309 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1310 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1311 "LHS and RHS of comparison must have same type!");
1312 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001313 case ISD::TRUNCSTORE: {
1314 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1315 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1316#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1317 // If this is a truncating store of a constant, convert to the desired type
1318 // and store it instead.
1319 if (isa<Constant>(Ops[0])) {
1320 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1321 if (isa<Constant>(Op))
1322 N1 = Op;
1323 }
1324 // Also for ConstantFP?
1325#endif
1326 if (Ops[0].getValueType() == EVT) // Normal store?
1327 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1328 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1329 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1330 "Can't do FP-INT conversion!");
1331 break;
1332 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001333 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001334 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001335 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1336 "LHS and RHS of condition must have same type!");
1337 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1338 "True and False arms of SelectCC must have same type!");
1339 assert(Ops[2].getValueType() == VT &&
1340 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001341 break;
1342 }
1343 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001344 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001345 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1346 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001347 break;
1348 }
Chris Lattneref847df2005-04-09 03:27:28 +00001349 }
1350
Chris Lattner385328c2005-05-14 07:42:29 +00001351 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001352 SDNode *N;
1353 if (VT != MVT::Flag) {
1354 SDNode *&E =
1355 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1356 if (E) return SDOperand(E, 0);
1357 E = N = new SDNode(Opcode, Ops);
1358 } else {
1359 N = new SDNode(Opcode, Ops);
1360 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001361 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001362 AllNodes.push_back(N);
1363 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001364}
1365
Chris Lattner89c34632005-05-14 06:20:26 +00001366SDOperand SelectionDAG::getNode(unsigned Opcode,
1367 std::vector<MVT::ValueType> &ResultTys,
1368 std::vector<SDOperand> &Ops) {
1369 if (ResultTys.size() == 1)
1370 return getNode(Opcode, ResultTys[0], Ops);
1371
Chris Lattner5f056bf2005-07-10 01:55:33 +00001372 switch (Opcode) {
1373 case ISD::EXTLOAD:
1374 case ISD::SEXTLOAD:
1375 case ISD::ZEXTLOAD: {
1376 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1377 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1378 // If they are asking for an extending load from/to the same thing, return a
1379 // normal load.
1380 if (ResultTys[0] == EVT)
1381 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1382 assert(EVT < ResultTys[0] &&
1383 "Should only be an extending load, not truncating!");
1384 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1385 "Cannot sign/zero extend a FP load!");
1386 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1387 "Cannot convert from FP to Int or Int -> FP!");
1388 break;
1389 }
1390
Chris Lattnere89083a2005-05-14 07:25:05 +00001391 // FIXME: figure out how to safely handle things like
1392 // int foo(int x) { return 1 << (x & 255); }
1393 // int bar() { return foo(256); }
1394#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001395 case ISD::SRA_PARTS:
1396 case ISD::SRL_PARTS:
1397 case ISD::SHL_PARTS:
1398 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001399 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001400 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1401 else if (N3.getOpcode() == ISD::AND)
1402 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1403 // If the and is only masking out bits that cannot effect the shift,
1404 // eliminate the and.
1405 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1406 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1407 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1408 }
1409 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001410#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001411 }
Chris Lattner89c34632005-05-14 06:20:26 +00001412
Chris Lattner43247a12005-08-25 19:12:10 +00001413 // Memoize the node unless it returns a flag.
1414 SDNode *N;
1415 if (ResultTys.back() != MVT::Flag) {
1416 SDNode *&E =
1417 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1418 if (E) return SDOperand(E, 0);
1419 E = N = new SDNode(Opcode, Ops);
1420 } else {
1421 N = new SDNode(Opcode, Ops);
1422 }
Chris Lattnera3255112005-11-08 23:30:28 +00001423 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001424 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001425 return SDOperand(N, 0);
1426}
1427
Chris Lattnera3255112005-11-08 23:30:28 +00001428void SelectionDAG::setNodeValueTypes(SDNode *N,
1429 std::vector<MVT::ValueType> &RetVals) {
1430 switch (RetVals.size()) {
1431 case 0: return;
1432 case 1: N->setValueTypes(RetVals[0]); return;
1433 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1434 default: break;
1435 }
1436
1437 std::list<std::vector<MVT::ValueType> >::iterator I =
1438 std::find(VTList.begin(), VTList.end(), RetVals);
1439 if (I == VTList.end()) {
1440 VTList.push_front(RetVals);
1441 I = VTList.begin();
1442 }
1443
1444 N->setValueTypes(&(*I)[0], I->size());
1445}
1446
1447void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1448 MVT::ValueType VT2) {
1449 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1450 E = VTList.end(); I != E; ++I) {
1451 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1452 N->setValueTypes(&(*I)[0], 2);
1453 return;
1454 }
1455 }
1456 std::vector<MVT::ValueType> V;
1457 V.push_back(VT1);
1458 V.push_back(VT2);
1459 VTList.push_front(V);
1460 N->setValueTypes(&(*VTList.begin())[0], 2);
1461}
1462
Chris Lattner149c58c2005-08-16 18:17:10 +00001463
1464/// SelectNodeTo - These are used for target selectors to *mutate* the
1465/// specified node to have the specified return type, Target opcode, and
1466/// operands. Note that target opcodes are stored as
1467/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001468///
1469/// Note that SelectNodeTo returns the resultant node. If there is already a
1470/// node of the specified opcode and operands, it returns that node instead of
1471/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001472SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1473 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001474 // If an identical node already exists, use it.
1475 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1476 if (ON) return SDOperand(ON, 0);
1477
Chris Lattner7651fa42005-08-24 23:00:29 +00001478 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001479
Chris Lattner7651fa42005-08-24 23:00:29 +00001480 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1481 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001482
1483 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001484 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001485}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001486
Chris Lattnereb19e402005-11-30 22:45:14 +00001487SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1488 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001489 // If an identical node already exists, use it.
1490 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1491 std::make_pair(Op1, VT))];
1492 if (ON) return SDOperand(ON, 0);
1493
Chris Lattner149c58c2005-08-16 18:17:10 +00001494 RemoveNodeFromCSEMaps(N);
1495 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1496 N->setValueTypes(VT);
1497 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001498
1499 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001500 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001501}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001502
Chris Lattnereb19e402005-11-30 22:45:14 +00001503SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1504 MVT::ValueType VT, SDOperand Op1,
1505 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001506 // If an identical node already exists, use it.
1507 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1508 std::make_pair(Op1, Op2))];
1509 if (ON) return SDOperand(ON, 0);
1510
Chris Lattner149c58c2005-08-16 18:17:10 +00001511 RemoveNodeFromCSEMaps(N);
1512 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1513 N->setValueTypes(VT);
1514 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001515
1516 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001517 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001518}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001519
Chris Lattnereb19e402005-11-30 22:45:14 +00001520SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1521 MVT::ValueType VT, SDOperand Op1,
1522 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001523 // If an identical node already exists, use it.
1524 std::vector<SDOperand> OpList;
1525 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1526 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1527 std::make_pair(VT, OpList))];
1528 if (ON) return SDOperand(ON, 0);
1529
Chris Lattner149c58c2005-08-16 18:17:10 +00001530 RemoveNodeFromCSEMaps(N);
1531 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1532 N->setValueTypes(VT);
1533 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001534
1535 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001536 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001537}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001538
Chris Lattnereb19e402005-11-30 22:45:14 +00001539SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1540 MVT::ValueType VT, SDOperand Op1,
1541 SDOperand Op2, SDOperand Op3,
1542 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001543 // If an identical node already exists, use it.
1544 std::vector<SDOperand> OpList;
1545 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1546 OpList.push_back(Op4);
1547 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1548 std::make_pair(VT, OpList))];
1549 if (ON) return SDOperand(ON, 0);
1550
Nate Begeman294a0a12005-08-18 07:30:15 +00001551 RemoveNodeFromCSEMaps(N);
1552 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1553 N->setValueTypes(VT);
1554 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001555
1556 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001557 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001558}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001559
Chris Lattnereb19e402005-11-30 22:45:14 +00001560SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1561 MVT::ValueType VT, SDOperand Op1,
1562 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1563 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001564 // If an identical node already exists, use it.
1565 std::vector<SDOperand> OpList;
1566 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1567 OpList.push_back(Op4); OpList.push_back(Op5);
1568 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1569 std::make_pair(VT, OpList))];
1570 if (ON) return SDOperand(ON, 0);
1571
Chris Lattner6b09a292005-08-21 18:49:33 +00001572 RemoveNodeFromCSEMaps(N);
1573 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1574 N->setValueTypes(VT);
1575 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001576
1577 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001578 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001579}
Chris Lattner149c58c2005-08-16 18:17:10 +00001580
Chris Lattnereb19e402005-11-30 22:45:14 +00001581SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1582 MVT::ValueType VT, SDOperand Op1,
1583 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1584 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001585 // If an identical node already exists, use it.
1586 std::vector<SDOperand> OpList;
1587 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1588 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1589 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1590 std::make_pair(VT, OpList))];
1591 if (ON) return SDOperand(ON, 0);
1592
Evan Cheng61ca74b2005-11-30 02:04:11 +00001593 RemoveNodeFromCSEMaps(N);
1594 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1595 N->setValueTypes(VT);
1596 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001597
1598 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001599 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001600}
1601
Chris Lattnereb19e402005-11-30 22:45:14 +00001602SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1603 MVT::ValueType VT1, MVT::ValueType VT2,
1604 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001605 // If an identical node already exists, use it.
1606 std::vector<SDOperand> OpList;
1607 OpList.push_back(Op1); OpList.push_back(Op2);
1608 std::vector<MVT::ValueType> VTList;
1609 VTList.push_back(VT1); VTList.push_back(VT2);
1610 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1611 std::make_pair(VTList, OpList))];
1612 if (ON) return SDOperand(ON, 0);
1613
Chris Lattner0fb094f2005-11-19 01:44:53 +00001614 RemoveNodeFromCSEMaps(N);
1615 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1616 setNodeValueTypes(N, VT1, VT2);
1617 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001618
1619 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001620 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001621}
1622
Chris Lattnereb19e402005-11-30 22:45:14 +00001623SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1624 MVT::ValueType VT1, MVT::ValueType VT2,
1625 SDOperand Op1, SDOperand Op2,
1626 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001627 // If an identical node already exists, use it.
1628 std::vector<SDOperand> OpList;
1629 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1630 std::vector<MVT::ValueType> VTList;
1631 VTList.push_back(VT1); VTList.push_back(VT2);
1632 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1633 std::make_pair(VTList, OpList))];
1634 if (ON) return SDOperand(ON, 0);
1635
Chris Lattner0fb094f2005-11-19 01:44:53 +00001636 RemoveNodeFromCSEMaps(N);
1637 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1638 setNodeValueTypes(N, VT1, VT2);
1639 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001640
1641 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001642 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001643}
1644
Chris Lattnereb19e402005-11-30 22:45:14 +00001645SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1646 MVT::ValueType VT1, MVT::ValueType VT2,
1647 SDOperand Op1, SDOperand Op2,
1648 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001649 // If an identical node already exists, use it.
1650 std::vector<SDOperand> OpList;
1651 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1652 OpList.push_back(Op4);
1653 std::vector<MVT::ValueType> VTList;
1654 VTList.push_back(VT1); VTList.push_back(VT2);
1655 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1656 std::make_pair(VTList, OpList))];
1657 if (ON) return SDOperand(ON, 0);
1658
Chris Lattner0fb094f2005-11-19 01:44:53 +00001659 RemoveNodeFromCSEMaps(N);
1660 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1661 setNodeValueTypes(N, VT1, VT2);
1662 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001663
1664 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001665 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001666}
1667
Chris Lattnereb19e402005-11-30 22:45:14 +00001668SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1669 MVT::ValueType VT1, MVT::ValueType VT2,
1670 SDOperand Op1, SDOperand Op2,
1671 SDOperand Op3, SDOperand Op4,
1672 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001673 // If an identical node already exists, use it.
1674 std::vector<SDOperand> OpList;
1675 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1676 OpList.push_back(Op4); OpList.push_back(Op5);
1677 std::vector<MVT::ValueType> VTList;
1678 VTList.push_back(VT1); VTList.push_back(VT2);
1679 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1680 std::make_pair(VTList, OpList))];
1681 if (ON) return SDOperand(ON, 0);
1682
Chris Lattner0fb094f2005-11-19 01:44:53 +00001683 RemoveNodeFromCSEMaps(N);
1684 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1685 setNodeValueTypes(N, VT1, VT2);
1686 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001687
1688 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001689 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001690}
1691
1692// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00001693/// This can cause recursive merging of nodes in the DAG.
1694///
Chris Lattner8b52f212005-08-26 18:36:28 +00001695/// This version assumes From/To have a single result value.
1696///
Chris Lattner1e111c72005-09-07 05:37:01 +00001697void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1698 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001699 SDNode *From = FromN.Val, *To = ToN.Val;
1700 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1701 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001702 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001703
Chris Lattner8b8749f2005-08-17 19:00:20 +00001704 while (!From->use_empty()) {
1705 // Process users until they are all gone.
1706 SDNode *U = *From->use_begin();
1707
1708 // This node is about to morph, remove its old self from the CSE maps.
1709 RemoveNodeFromCSEMaps(U);
1710
Chris Lattner65113b22005-11-08 22:07:03 +00001711 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1712 I != E; ++I)
1713 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001714 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001715 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001716 To->addUser(U);
1717 }
1718
1719 // Now that we have modified U, add it back to the CSE maps. If it already
1720 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001721 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1722 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001723 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001724 if (Deleted) Deleted->push_back(U);
1725 DeleteNodeNotInCSEMaps(U);
1726 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001727 }
1728}
1729
1730/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1731/// This can cause recursive merging of nodes in the DAG.
1732///
1733/// This version assumes From/To have matching types and numbers of result
1734/// values.
1735///
Chris Lattner1e111c72005-09-07 05:37:01 +00001736void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1737 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001738 assert(From != To && "Cannot replace uses of with self");
1739 assert(From->getNumValues() == To->getNumValues() &&
1740 "Cannot use this version of ReplaceAllUsesWith!");
1741 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001742 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001743 return;
1744 }
1745
1746 while (!From->use_empty()) {
1747 // Process users until they are all gone.
1748 SDNode *U = *From->use_begin();
1749
1750 // This node is about to morph, remove its old self from the CSE maps.
1751 RemoveNodeFromCSEMaps(U);
1752
Chris Lattner65113b22005-11-08 22:07:03 +00001753 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1754 I != E; ++I)
1755 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001756 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001757 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001758 To->addUser(U);
1759 }
1760
1761 // Now that we have modified U, add it back to the CSE maps. If it already
1762 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001763 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1764 ReplaceAllUsesWith(U, Existing, Deleted);
1765 // U is now dead.
1766 if (Deleted) Deleted->push_back(U);
1767 DeleteNodeNotInCSEMaps(U);
1768 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001769 }
1770}
1771
Chris Lattner8b52f212005-08-26 18:36:28 +00001772/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1773/// This can cause recursive merging of nodes in the DAG.
1774///
1775/// This version can replace From with any result values. To must match the
1776/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001777void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001778 const std::vector<SDOperand> &To,
1779 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001780 assert(From->getNumValues() == To.size() &&
1781 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001782 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001783 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001784 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001785 return;
1786 }
1787
1788 while (!From->use_empty()) {
1789 // Process users until they are all gone.
1790 SDNode *U = *From->use_begin();
1791
1792 // This node is about to morph, remove its old self from the CSE maps.
1793 RemoveNodeFromCSEMaps(U);
1794
Chris Lattner65113b22005-11-08 22:07:03 +00001795 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1796 I != E; ++I)
1797 if (I->Val == From) {
1798 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001799 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001800 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001801 ToOp.Val->addUser(U);
1802 }
1803
1804 // Now that we have modified U, add it back to the CSE maps. If it already
1805 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001806 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1807 ReplaceAllUsesWith(U, Existing, Deleted);
1808 // U is now dead.
1809 if (Deleted) Deleted->push_back(U);
1810 DeleteNodeNotInCSEMaps(U);
1811 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001812 }
1813}
1814
1815
Jim Laskey58b968b2005-08-17 20:08:02 +00001816//===----------------------------------------------------------------------===//
1817// SDNode Class
1818//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001819
Chris Lattnera3255112005-11-08 23:30:28 +00001820
1821/// getValueTypeList - Return a pointer to the specified value type.
1822///
1823MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
1824 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
1825 VTs[VT] = VT;
1826 return &VTs[VT];
1827}
1828
Chris Lattner5c884562005-01-12 18:37:47 +00001829/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1830/// indicated value. This method ignores uses of other values defined by this
1831/// operation.
1832bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1833 assert(Value < getNumValues() && "Bad value!");
1834
1835 // If there is only one value, this is easy.
1836 if (getNumValues() == 1)
1837 return use_size() == NUses;
1838 if (Uses.size() < NUses) return false;
1839
1840 SDOperand TheValue(this, Value);
1841
1842 std::set<SDNode*> UsersHandled;
1843
1844 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1845 UI != E; ++UI) {
1846 SDNode *User = *UI;
1847 if (User->getNumOperands() == 1 ||
1848 UsersHandled.insert(User).second) // First time we've seen this?
1849 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1850 if (User->getOperand(i) == TheValue) {
1851 if (NUses == 0)
1852 return false; // too many uses
1853 --NUses;
1854 }
1855 }
1856
1857 // Found exactly the right number of uses?
1858 return NUses == 0;
1859}
1860
1861
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001862const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001863 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001864 default:
1865 if (getOpcode() < ISD::BUILTIN_OP_END)
1866 return "<<Unknown DAG Node>>";
1867 else {
Evan Cheng72261582005-12-20 06:22:03 +00001868 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001869 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001870 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1871 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00001872
Evan Cheng72261582005-12-20 06:22:03 +00001873 TargetLowering &TLI = G->getTargetLoweringInfo();
1874 const char *Name =
1875 TLI.getTargetNodeName(getOpcode());
1876 if (Name) return Name;
1877 }
1878
1879 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001880 }
1881
Andrew Lenharth95762122005-03-31 21:24:06 +00001882 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001883 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001884 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001885 case ISD::VALUETYPE: return "ValueType";
Chris Lattner36ce6912005-11-29 06:21:05 +00001886 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001887 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001888 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001889 case ISD::AssertSext: return "AssertSext";
1890 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001891 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001892 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001893 case ISD::ConstantFP: return "ConstantFP";
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001894 case ISD::ConstantVec: return "ConstantVec";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001895 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001896 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001897 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001898 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001899 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001900 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001901 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001902 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001903 case ISD::ConstantPool: return "ConstantPool";
1904 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001905 case ISD::CopyToReg: return "CopyToReg";
1906 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001907 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001908
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001909 // Unary operators
1910 case ISD::FABS: return "fabs";
1911 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001912 case ISD::FSQRT: return "fsqrt";
1913 case ISD::FSIN: return "fsin";
1914 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001915
1916 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001917 case ISD::ADD: return "add";
1918 case ISD::SUB: return "sub";
1919 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001920 case ISD::MULHU: return "mulhu";
1921 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001922 case ISD::SDIV: return "sdiv";
1923 case ISD::UDIV: return "udiv";
1924 case ISD::SREM: return "srem";
1925 case ISD::UREM: return "urem";
1926 case ISD::AND: return "and";
1927 case ISD::OR: return "or";
1928 case ISD::XOR: return "xor";
1929 case ISD::SHL: return "shl";
1930 case ISD::SRA: return "sra";
1931 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001932 case ISD::FADD: return "fadd";
1933 case ISD::FSUB: return "fsub";
1934 case ISD::FMUL: return "fmul";
1935 case ISD::FDIV: return "fdiv";
1936 case ISD::FREM: return "frem";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001937 case ISD::VADD: return "vadd";
1938 case ISD::VSUB: return "vsub";
1939 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00001940
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001941 case ISD::SETCC: return "setcc";
1942 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001943 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001944 case ISD::ADD_PARTS: return "add_parts";
1945 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001946 case ISD::SHL_PARTS: return "shl_parts";
1947 case ISD::SRA_PARTS: return "sra_parts";
1948 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001949
Chris Lattner7f644642005-04-28 21:44:03 +00001950 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001951 case ISD::SIGN_EXTEND: return "sign_extend";
1952 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001953 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001954 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001955 case ISD::TRUNCATE: return "truncate";
1956 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001957 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001958 case ISD::FP_EXTEND: return "fp_extend";
1959
1960 case ISD::SINT_TO_FP: return "sint_to_fp";
1961 case ISD::UINT_TO_FP: return "uint_to_fp";
1962 case ISD::FP_TO_SINT: return "fp_to_sint";
1963 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00001964 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001965
1966 // Control flow instructions
1967 case ISD::BR: return "br";
1968 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001969 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001970 case ISD::BR_CC: return "br_cc";
1971 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001972 case ISD::RET: return "ret";
1973 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001974 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001975 case ISD::CALLSEQ_START: return "callseq_start";
1976 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001977
1978 // Other operators
1979 case ISD::LOAD: return "load";
1980 case ISD::STORE: return "store";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001981 case ISD::VLOAD: return "vload";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001982 case ISD::EXTLOAD: return "extload";
1983 case ISD::SEXTLOAD: return "sextload";
1984 case ISD::ZEXTLOAD: return "zextload";
1985 case ISD::TRUNCSTORE: return "truncstore";
1986
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001987 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1988 case ISD::EXTRACT_ELEMENT: return "extract_element";
1989 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001990 case ISD::MEMSET: return "memset";
1991 case ISD::MEMCPY: return "memcpy";
1992 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001993
Chris Lattner276260b2005-05-11 04:50:30 +00001994 // Bit counting
1995 case ISD::CTPOP: return "ctpop";
1996 case ISD::CTTZ: return "cttz";
1997 case ISD::CTLZ: return "ctlz";
1998
1999 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002000 case ISD::READPORT: return "readport";
2001 case ISD::WRITEPORT: return "writeport";
2002 case ISD::READIO: return "readio";
2003 case ISD::WRITEIO: return "writeio";
2004
Chris Lattner36ce6912005-11-29 06:21:05 +00002005 // Debug info
2006 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002007 case ISD::DEBUG_LOC: return "debug_loc";
Chris Lattner36ce6912005-11-29 06:21:05 +00002008
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002009 case ISD::CONDCODE:
2010 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002011 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002012 case ISD::SETOEQ: return "setoeq";
2013 case ISD::SETOGT: return "setogt";
2014 case ISD::SETOGE: return "setoge";
2015 case ISD::SETOLT: return "setolt";
2016 case ISD::SETOLE: return "setole";
2017 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002018
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002019 case ISD::SETO: return "seto";
2020 case ISD::SETUO: return "setuo";
2021 case ISD::SETUEQ: return "setue";
2022 case ISD::SETUGT: return "setugt";
2023 case ISD::SETUGE: return "setuge";
2024 case ISD::SETULT: return "setult";
2025 case ISD::SETULE: return "setule";
2026 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002027
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002028 case ISD::SETEQ: return "seteq";
2029 case ISD::SETGT: return "setgt";
2030 case ISD::SETGE: return "setge";
2031 case ISD::SETLT: return "setlt";
2032 case ISD::SETLE: return "setle";
2033 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002034 }
2035 }
2036}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002037
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002038void SDNode::dump() const { dump(0); }
2039void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002040 std::cerr << (void*)this << ": ";
2041
2042 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2043 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002044 if (getValueType(i) == MVT::Other)
2045 std::cerr << "ch";
2046 else
2047 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002048 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002049 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002050
2051 std::cerr << " ";
2052 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2053 if (i) std::cerr << ", ";
2054 std::cerr << (void*)getOperand(i).Val;
2055 if (unsigned RN = getOperand(i).ResNo)
2056 std::cerr << ":" << RN;
2057 }
2058
2059 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2060 std::cerr << "<" << CSDN->getValue() << ">";
2061 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2062 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002063 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002064 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002065 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002066 std::cerr << "<";
2067 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002068 if (offset > 0)
2069 std::cerr << " + " << offset;
2070 else
2071 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002072 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002073 std::cerr << "<" << FIDN->getIndex() << ">";
2074 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002075 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002076 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002077 std::cerr << "<";
2078 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2079 if (LBB)
2080 std::cerr << LBB->getName() << " ";
2081 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002082 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002083 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2084 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2085 } else {
2086 std::cerr << " #" << R->getReg();
2087 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002088 } else if (const ExternalSymbolSDNode *ES =
2089 dyn_cast<ExternalSymbolSDNode>(this)) {
2090 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002091 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2092 if (M->getValue())
2093 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2094 else
2095 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002096 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2097 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002098 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002099}
2100
Chris Lattnerde202b32005-11-09 23:47:37 +00002101static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002102 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2103 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002104 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002105 else
2106 std::cerr << "\n" << std::string(indent+2, ' ')
2107 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002108
Chris Lattnerea946cd2005-01-09 20:38:33 +00002109
2110 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002111 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002112}
2113
Chris Lattnerc3aae252005-01-07 07:46:32 +00002114void SelectionDAG::dump() const {
2115 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002116 std::vector<const SDNode*> Nodes;
2117 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2118 I != E; ++I)
2119 Nodes.push_back(I);
2120
Chris Lattner49d24712005-01-09 20:26:36 +00002121 std::sort(Nodes.begin(), Nodes.end());
2122
2123 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002124 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002125 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002126 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002127
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002128 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002129
Chris Lattnerc3aae252005-01-07 07:46:32 +00002130 std::cerr << "\n\n";
2131}
2132