blob: 753364ecb3cdb2888c66a2c8b086111c42e8e352 [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"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner5cdcc582005-01-09 20:52:51 +000030static bool isCommutativeBinOp(unsigned Opcode) {
31 switch (Opcode) {
32 case ISD::ADD:
33 case ISD::MUL:
Chris Lattner01b3d732005-09-28 22:28:18 +000034 case ISD::FADD:
35 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000036 case ISD::AND:
37 case ISD::OR:
38 case ISD::XOR: return true;
39 default: return false; // FIXME: Need commutative info for user ops!
40 }
41}
42
43static bool isAssociativeBinOp(unsigned Opcode) {
44 switch (Opcode) {
45 case ISD::ADD:
46 case ISD::MUL:
47 case ISD::AND:
48 case ISD::OR:
49 case ISD::XOR: return true;
50 default: return false; // FIXME: Need associative info for user ops!
51 }
52}
53
Chris Lattner5cdcc582005-01-09 20:52:51 +000054// isInvertibleForFree - Return true if there is no cost to emitting the logical
55// inverse of this node.
56static bool isInvertibleForFree(SDOperand N) {
57 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000058 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000059 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000060 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000061}
62
Jim Laskey58b968b2005-08-17 20:08:02 +000063//===----------------------------------------------------------------------===//
64// ConstantFPSDNode Class
65//===----------------------------------------------------------------------===//
66
67/// isExactlyValue - We don't rely on operator== working on double values, as
68/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
69/// As such, this method can be used to do an exact bit-for-bit comparison of
70/// two floating point values.
71bool ConstantFPSDNode::isExactlyValue(double V) const {
72 return DoubleToBits(V) == DoubleToBits(Value);
73}
74
75//===----------------------------------------------------------------------===//
76// ISD Class
77//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000078
Chris Lattnerc3aae252005-01-07 07:46:32 +000079/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
80/// when given the operation for (X op Y).
81ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
82 // To perform this operation, we just need to swap the L and G bits of the
83 // operation.
84 unsigned OldL = (Operation >> 2) & 1;
85 unsigned OldG = (Operation >> 1) & 1;
86 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
87 (OldL << 1) | // New G bit
88 (OldG << 2)); // New L bit.
89}
90
91/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
92/// 'op' is a valid SetCC operation.
93ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
94 unsigned Operation = Op;
95 if (isInteger)
96 Operation ^= 7; // Flip L, G, E bits, but not U.
97 else
98 Operation ^= 15; // Flip all of the condition bits.
99 if (Operation > ISD::SETTRUE2)
100 Operation &= ~8; // Don't let N and U bits get set.
101 return ISD::CondCode(Operation);
102}
103
104
105/// isSignedOp - For an integer comparison, return 1 if the comparison is a
106/// signed operation and 2 if the result is an unsigned comparison. Return zero
107/// if the operation does not depend on the sign of the input (setne and seteq).
108static int isSignedOp(ISD::CondCode Opcode) {
109 switch (Opcode) {
110 default: assert(0 && "Illegal integer setcc operation!");
111 case ISD::SETEQ:
112 case ISD::SETNE: return 0;
113 case ISD::SETLT:
114 case ISD::SETLE:
115 case ISD::SETGT:
116 case ISD::SETGE: return 1;
117 case ISD::SETULT:
118 case ISD::SETULE:
119 case ISD::SETUGT:
120 case ISD::SETUGE: return 2;
121 }
122}
123
124/// getSetCCOrOperation - Return the result of a logical OR between different
125/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
126/// returns SETCC_INVALID if it is not possible to represent the resultant
127/// comparison.
128ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
129 bool isInteger) {
130 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
131 // Cannot fold a signed integer setcc with an unsigned integer setcc.
132 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000135
Chris Lattnerc3aae252005-01-07 07:46:32 +0000136 // If the N and U bits get set then the resultant comparison DOES suddenly
137 // care about orderedness, and is true when ordered.
138 if (Op > ISD::SETTRUE2)
139 Op &= ~16; // Clear the N bit.
140 return ISD::CondCode(Op);
141}
142
143/// getSetCCAndOperation - Return the result of a logical AND between different
144/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
145/// function returns zero if it is not possible to represent the resultant
146/// comparison.
147ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
148 bool isInteger) {
149 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
150 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000151 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000152
153 // Combine all of the condition bits.
154 return ISD::CondCode(Op1 & Op2);
155}
156
Chris Lattnerb48da392005-01-23 04:39:44 +0000157const TargetMachine &SelectionDAG::getTarget() const {
158 return TLI.getTargetMachine();
159}
160
Jim Laskey58b968b2005-08-17 20:08:02 +0000161//===----------------------------------------------------------------------===//
162// SelectionDAG Class
163//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000164
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000165/// RemoveDeadNodes - This method deletes all unreachable nodes in the
166/// SelectionDAG, including nodes (like loads) that have uses of their token
167/// chain but no other uses and no side effect. If a node is passed in as an
168/// argument, it is used as the seed for node deletion.
169void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000172 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000173
Chris Lattnerf469cb62005-11-08 18:52:27 +0000174 bool MadeChange = false;
175
Chris Lattner8b8749f2005-08-17 19:00:20 +0000176 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000177 if (N && N->use_empty()) {
178 DestroyDeadNode(N);
179 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000180 }
181
Chris Lattnerf469cb62005-11-08 18:52:27 +0000182 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i) {
183 // Try to delete this node.
184 SDNode *N = AllNodes[i];
185 if (N->use_empty() && N->getOpcode() != 65535) {
186 DestroyDeadNode(N);
187 MadeChange = true;
188 }
189 }
190
191 // Walk the nodes list, removing the nodes we've marked as dead.
192 if (MadeChange) {
193 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
194 if (AllNodes[i]->use_empty()) {
195 delete AllNodes[i];
196 AllNodes[i] = AllNodes.back();
197 AllNodes.pop_back();
198 --i; --e;
199 }
200 }
201
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000202 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000203 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000204}
205
Chris Lattnerf469cb62005-11-08 18:52:27 +0000206/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
207/// graph. If it is the last user of any of its operands, recursively process
208/// them the same way.
209///
210void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000211 // Okay, we really are going to delete this node. First take this out of the
212 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000213 RemoveNodeFromCSEMaps(N);
214
215 // Next, brutally remove the operand list. This is safe to do, as there are
216 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000217 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
218 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000219 O->removeUser(N);
220
221 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000222 if (O->use_empty())
223 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000224 }
Chris Lattner65113b22005-11-08 22:07:03 +0000225 delete[] N->OperandList;
226 N->OperandList = 0;
227 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000228
229 // Mark the node as dead.
230 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000231}
232
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000233void SelectionDAG::DeleteNode(SDNode *N) {
234 assert(N->use_empty() && "Cannot delete a node that is not dead!");
235
236 // First take this out of the appropriate CSE map.
237 RemoveNodeFromCSEMaps(N);
238
Chris Lattner1e111c72005-09-07 05:37:01 +0000239 // Finally, remove uses due to operands of this node, remove from the
240 // AllNodes list, and delete the node.
241 DeleteNodeNotInCSEMaps(N);
242}
243
244void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
245
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000246 // Remove it from the AllNodes list.
247 for (std::vector<SDNode*>::iterator I = AllNodes.begin(); ; ++I) {
248 assert(I != AllNodes.end() && "Node not in AllNodes list??");
249 if (*I == N) {
250 // Erase from the vector, which is not ordered.
251 std::swap(*I, AllNodes.back());
252 AllNodes.pop_back();
253 break;
254 }
255 }
256
257 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000258 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
259 I->Val->removeUser(N);
260 delete[] N->OperandList;
261 N->OperandList = 0;
262 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000263
264 delete N;
265}
266
Chris Lattner149c58c2005-08-16 18:17:10 +0000267/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
268/// correspond to it. This is useful when we're about to delete or repurpose
269/// the node. We don't want future request for structurally identical nodes
270/// to return N anymore.
271void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000272 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000273 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000274 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000276 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
277 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000278 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000279 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000280 Erased = TargetConstants.erase(std::make_pair(
281 cast<ConstantSDNode>(N)->getValue(),
282 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000283 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000284 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000285 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000286 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000287 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000288 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000289 case ISD::CONDCODE:
290 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
291 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000292 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000293 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
294 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000295 case ISD::GlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000296 Erased = GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000297 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000298 case ISD::TargetGlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000299 Erased =TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000300 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000302 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000304 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000305 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000306 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000308 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000310 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000311 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000312 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000314 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 break;
316 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000317 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000319 case ISD::TargetExternalSymbol:
320 Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
321 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000322 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000324 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
325 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000326 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000327 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
328 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000329 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000330 case ISD::SRCVALUE: {
331 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000332 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000333 break;
334 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000335 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000336 Erased = Loads.erase(std::make_pair(N->getOperand(1),
337 std::make_pair(N->getOperand(0),
338 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000340 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000342 if (N->getNumOperands() == 0) {
343 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
344 N->getValueType(0)));
345 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000346 Erased =
347 UnaryOps.erase(std::make_pair(N->getOpcode(),
348 std::make_pair(N->getOperand(0),
349 N->getValueType(0))));
350 } else if (N->getNumOperands() == 2) {
351 Erased =
352 BinaryOps.erase(std::make_pair(N->getOpcode(),
353 std::make_pair(N->getOperand(0),
354 N->getOperand(1))));
355 } else {
356 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
357 Erased =
358 OneResultNodes.erase(std::make_pair(N->getOpcode(),
359 std::make_pair(N->getValueType(0),
360 Ops)));
361 }
Chris Lattner385328c2005-05-14 07:42:29 +0000362 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000363 // Remove the node from the ArbitraryNodes map.
364 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
365 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000366 Erased =
367 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
368 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000369 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000370 break;
371 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000372#ifndef NDEBUG
373 // Verify that the node was actually in one of the CSE maps, unless it has a
374 // flag result (which cannot be CSE'd) or is one of the special cases that are
375 // not subject to CSE.
376 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
377 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000378 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000379
380 N->dump();
381 assert(0 && "Node is not in map!");
382 }
383#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384}
385
Chris Lattner8b8749f2005-08-17 19:00:20 +0000386/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
387/// has been taken out and modified in some way. If the specified node already
388/// exists in the CSE maps, do not modify the maps, but return the existing node
389/// instead. If it doesn't exist, add it and return null.
390///
391SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
392 assert(N->getNumOperands() && "This is a leaf node!");
393 if (N->getOpcode() == ISD::LOAD) {
394 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
395 std::make_pair(N->getOperand(0),
396 N->getValueType(0)))];
397 if (L) return L;
398 L = N;
Chris Lattner95038592005-10-05 06:35:28 +0000399 } else if (N->getOpcode() == ISD::HANDLENODE) {
400 return 0; // never add it.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000401 } else if (N->getNumOperands() == 1) {
402 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
403 std::make_pair(N->getOperand(0),
404 N->getValueType(0)))];
405 if (U) return U;
406 U = N;
407 } else if (N->getNumOperands() == 2) {
408 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
409 std::make_pair(N->getOperand(0),
410 N->getOperand(1)))];
411 if (B) return B;
412 B = N;
413 } else if (N->getNumValues() == 1) {
414 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
415 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
416 std::make_pair(N->getValueType(0), Ops))];
417 if (ORN) return ORN;
418 ORN = N;
419 } else {
420 // Remove the node from the ArbitraryNodes map.
421 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
422 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
423 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
424 std::make_pair(RV, Ops))];
425 if (AN) return AN;
426 AN = N;
427 }
428 return 0;
429
430}
431
432
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000433
Chris Lattner78ec3112003-08-11 14:57:33 +0000434SelectionDAG::~SelectionDAG() {
Chris Lattner65113b22005-11-08 22:07:03 +0000435 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i) {
436 SDNode *N = AllNodes[i];
437 delete [] N->OperandList;
438 N->OperandList = 0;
439 N->NumOperands = 0;
440 delete N;
441 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000442}
443
Chris Lattner0f2287b2005-04-13 02:38:18 +0000444SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000445 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000446 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000447 return getNode(ISD::AND, Op.getValueType(), Op,
448 getConstant(Imm, Op.getValueType()));
449}
450
Chris Lattnerc3aae252005-01-07 07:46:32 +0000451SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
452 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
453 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000454 if (VT != MVT::i64)
455 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000456
Chris Lattnerc3aae252005-01-07 07:46:32 +0000457 SDNode *&N = Constants[std::make_pair(Val, VT)];
458 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000459 N = new ConstantSDNode(false, Val, VT);
460 AllNodes.push_back(N);
461 return SDOperand(N, 0);
462}
463
464SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
465 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
466 // Mask out any bits that are not valid for this constant.
467 if (VT != MVT::i64)
468 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
469
470 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
471 if (N) return SDOperand(N, 0);
472 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000473 AllNodes.push_back(N);
474 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000475}
476
Chris Lattnerc3aae252005-01-07 07:46:32 +0000477SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
478 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
479 if (VT == MVT::f32)
480 Val = (float)Val; // Mask out extra precision.
481
Chris Lattnerd8658612005-02-17 20:17:32 +0000482 // Do the map lookup using the actual bit pattern for the floating point
483 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
484 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000485 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000486 if (N) return SDOperand(N, 0);
487 N = new ConstantFPSDNode(Val, VT);
488 AllNodes.push_back(N);
489 return SDOperand(N, 0);
490}
491
492
493
494SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
495 MVT::ValueType VT) {
496 SDNode *&N = GlobalValues[GV];
497 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000498 N = new GlobalAddressSDNode(false, GV, VT);
499 AllNodes.push_back(N);
500 return SDOperand(N, 0);
501}
502
503SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
504 MVT::ValueType VT) {
505 SDNode *&N = TargetGlobalValues[GV];
506 if (N) return SDOperand(N, 0);
507 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000508 AllNodes.push_back(N);
509 return SDOperand(N, 0);
510}
511
512SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
513 SDNode *&N = FrameIndices[FI];
514 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000515 N = new FrameIndexSDNode(FI, VT, false);
516 AllNodes.push_back(N);
517 return SDOperand(N, 0);
518}
519
520SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
521 SDNode *&N = TargetFrameIndices[FI];
522 if (N) return SDOperand(N, 0);
523 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000524 AllNodes.push_back(N);
525 return SDOperand(N, 0);
526}
527
Chris Lattner5839bf22005-08-26 17:15:30 +0000528SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
529 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000530 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000531 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
534}
535
Chris Lattner5839bf22005-08-26 17:15:30 +0000536SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
537 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000538 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000539 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
542}
543
544SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
545 SDNode *&N = BBNodes[MBB];
546 if (N) return SDOperand(N, 0);
547 N = new BasicBlockSDNode(MBB);
548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
550}
551
Chris Lattner15e4b012005-07-10 00:07:11 +0000552SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
553 if ((unsigned)VT >= ValueTypeNodes.size())
554 ValueTypeNodes.resize(VT+1);
555 if (ValueTypeNodes[VT] == 0) {
556 ValueTypeNodes[VT] = new VTSDNode(VT);
557 AllNodes.push_back(ValueTypeNodes[VT]);
558 }
559
560 return SDOperand(ValueTypeNodes[VT], 0);
561}
562
Chris Lattnerc3aae252005-01-07 07:46:32 +0000563SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
564 SDNode *&N = ExternalSymbols[Sym];
565 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000566 N = new ExternalSymbolSDNode(false, Sym, VT);
567 AllNodes.push_back(N);
568 return SDOperand(N, 0);
569}
570
571SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) {
572 SDNode *&N = TargetExternalSymbols[Sym];
573 if (N) return SDOperand(N, 0);
574 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000575 AllNodes.push_back(N);
576 return SDOperand(N, 0);
577}
578
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000579SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
580 if ((unsigned)Cond >= CondCodeNodes.size())
581 CondCodeNodes.resize(Cond+1);
582
Chris Lattner079a27a2005-08-09 20:40:02 +0000583 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000584 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000585 AllNodes.push_back(CondCodeNodes[Cond]);
586 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000587 return SDOperand(CondCodeNodes[Cond], 0);
588}
589
Chris Lattner0fdd7682005-08-30 22:38:38 +0000590SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
591 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
592 if (!Reg) {
593 Reg = new RegisterSDNode(RegNo, VT);
594 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000595 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000596 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000597}
598
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000599SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
600 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000601 // These setcc operations always fold.
602 switch (Cond) {
603 default: break;
604 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000605 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000606 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000607 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000608 }
609
Chris Lattner67255a12005-04-07 18:14:58 +0000610 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
611 uint64_t C2 = N2C->getValue();
612 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
613 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000614
Chris Lattnerc3aae252005-01-07 07:46:32 +0000615 // Sign extend the operands if required
616 if (ISD::isSignedIntSetCC(Cond)) {
617 C1 = N1C->getSignExtended();
618 C2 = N2C->getSignExtended();
619 }
620
621 switch (Cond) {
622 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000623 case ISD::SETEQ: return getConstant(C1 == C2, VT);
624 case ISD::SETNE: return getConstant(C1 != C2, VT);
625 case ISD::SETULT: return getConstant(C1 < C2, VT);
626 case ISD::SETUGT: return getConstant(C1 > C2, VT);
627 case ISD::SETULE: return getConstant(C1 <= C2, VT);
628 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
629 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
630 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
631 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
632 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000633 }
Chris Lattner24673922005-04-07 18:58:54 +0000634 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000635 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000636 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
637 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
638
639 // If the comparison constant has bits in the upper part, the
640 // zero-extended value could never match.
641 if (C2 & (~0ULL << InSize)) {
642 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
643 switch (Cond) {
644 case ISD::SETUGT:
645 case ISD::SETUGE:
646 case ISD::SETEQ: return getConstant(0, VT);
647 case ISD::SETULT:
648 case ISD::SETULE:
649 case ISD::SETNE: return getConstant(1, VT);
650 case ISD::SETGT:
651 case ISD::SETGE:
652 // True if the sign bit of C2 is set.
653 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
654 case ISD::SETLT:
655 case ISD::SETLE:
656 // True if the sign bit of C2 isn't set.
657 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
658 default:
659 break;
660 }
661 }
662
663 // Otherwise, we can perform the comparison with the low bits.
664 switch (Cond) {
665 case ISD::SETEQ:
666 case ISD::SETNE:
667 case ISD::SETUGT:
668 case ISD::SETUGE:
669 case ISD::SETULT:
670 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000671 return getSetCC(VT, N1.getOperand(0),
672 getConstant(C2, N1.getOperand(0).getValueType()),
673 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000674 default:
675 break; // todo, be more careful with signed comparisons
676 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000677 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
678 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
679 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
680 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
681 MVT::ValueType ExtDstTy = N1.getValueType();
682 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000683
Chris Lattner7b2880c2005-08-24 22:44:39 +0000684 // If the extended part has any inconsistent bits, it cannot ever
685 // compare equal. In other words, they have to be all ones or all
686 // zeros.
687 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000688 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000689 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
690 return getConstant(Cond == ISD::SETNE, VT);
691
692 // Otherwise, make this a use of a zext.
693 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000694 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000695 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000696 }
697
Chris Lattner67255a12005-04-07 18:14:58 +0000698 uint64_t MinVal, MaxVal;
699 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
700 if (ISD::isSignedIntSetCC(Cond)) {
701 MinVal = 1ULL << (OperandBitSize-1);
702 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
703 MaxVal = ~0ULL >> (65-OperandBitSize);
704 else
705 MaxVal = 0;
706 } else {
707 MinVal = 0;
708 MaxVal = ~0ULL >> (64-OperandBitSize);
709 }
710
711 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
712 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
713 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
714 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000715 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
716 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000717 }
718
719 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
720 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
721 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000722 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
723 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000724 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000725
Nate Begeman72ea2812005-04-14 08:56:52 +0000726 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
727 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000728
Nate Begeman72ea2812005-04-14 08:56:52 +0000729 // Canonicalize setgt X, Min --> setne X, Min
730 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000731 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000732
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000733 // If we have setult X, 1, turn it into seteq X, 0
734 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000735 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
736 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000737 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000738 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000739 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
740 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000741
742 // If we have "setcc X, C1", check to see if we can shrink the immediate
743 // by changing cc.
744
745 // SETUGT X, SINTMAX -> SETLT X, 0
746 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
747 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000748 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000749
750 // FIXME: Implement the rest of these.
751
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000752
753 // Fold bit comparisons when we can.
754 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
755 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
756 if (ConstantSDNode *AndRHS =
757 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
758 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
759 // Perform the xform if the AND RHS is a single bit.
760 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
761 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000762 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000763 TLI.getShiftAmountTy()));
764 }
765 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
766 // (X & 8) == 8 --> (X & 8) >> 3
767 // Perform the xform if C2 is a single bit.
768 if ((C2 & (C2-1)) == 0) {
769 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000770 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000771 }
772 }
773 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000774 }
Chris Lattner67255a12005-04-07 18:14:58 +0000775 } else if (isa<ConstantSDNode>(N1.Val)) {
776 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000777 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000778 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779
780 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
781 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
782 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000783
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784 switch (Cond) {
785 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000786 case ISD::SETEQ: return getConstant(C1 == C2, VT);
787 case ISD::SETNE: return getConstant(C1 != C2, VT);
788 case ISD::SETLT: return getConstant(C1 < C2, VT);
789 case ISD::SETGT: return getConstant(C1 > C2, VT);
790 case ISD::SETLE: return getConstant(C1 <= C2, VT);
791 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000792 }
793 } else {
794 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000795 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000796 }
797
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000798 // Could not fold it.
799 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000800}
801
Chris Lattnerc3aae252005-01-07 07:46:32 +0000802/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000803///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000804SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000805 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
806 if (!N) {
807 N = new SDNode(Opcode, VT);
808 AllNodes.push_back(N);
809 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810 return SDOperand(N, 0);
811}
812
Chris Lattnerc3aae252005-01-07 07:46:32 +0000813SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
814 SDOperand Operand) {
815 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
816 uint64_t Val = C->getValue();
817 switch (Opcode) {
818 default: break;
819 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000820 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
822 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000823 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
824 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000825 }
826 }
827
828 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
829 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000830 case ISD::FNEG:
831 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000832 case ISD::FP_ROUND:
833 case ISD::FP_EXTEND:
834 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000835 case ISD::FP_TO_SINT:
836 return getConstant((int64_t)C->getValue(), VT);
837 case ISD::FP_TO_UINT:
838 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000839 }
840
841 unsigned OpOpcode = Operand.Val->getOpcode();
842 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000843 case ISD::TokenFactor:
844 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000845 case ISD::SIGN_EXTEND:
846 if (Operand.getValueType() == VT) return Operand; // noop extension
847 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
848 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
849 break;
850 case ISD::ZERO_EXTEND:
851 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000852 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000853 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +0000855 case ISD::ANY_EXTEND:
856 if (Operand.getValueType() == VT) return Operand; // noop extension
857 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
858 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
859 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
860 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000861 case ISD::TRUNCATE:
862 if (Operand.getValueType() == VT) return Operand; // noop truncate
863 if (OpOpcode == ISD::TRUNCATE)
864 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +0000865 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
866 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000867 // If the source is smaller than the dest, we still need an extend.
868 if (Operand.Val->getOperand(0).getValueType() < VT)
869 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
870 else if (Operand.Val->getOperand(0).getValueType() > VT)
871 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
872 else
873 return Operand.Val->getOperand(0);
874 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000875 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000876 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +0000877 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
878 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +0000879 Operand.Val->getOperand(0));
880 if (OpOpcode == ISD::FNEG) // --X -> X
881 return Operand.Val->getOperand(0);
882 break;
883 case ISD::FABS:
884 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
885 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
886 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000887 }
888
Chris Lattner43247a12005-08-25 19:12:10 +0000889 SDNode *N;
890 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
891 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +0000892 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +0000893 E = N = new SDNode(Opcode, Operand);
894 } else {
895 N = new SDNode(Opcode, Operand);
896 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000897 N->setValueTypes(VT);
898 AllNodes.push_back(N);
899 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000900}
901
Chris Lattner36019aa2005-04-18 03:48:41 +0000902
903
Chris Lattnerc3aae252005-01-07 07:46:32 +0000904SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
905 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000906#ifndef NDEBUG
907 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000908 case ISD::TokenFactor:
909 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
910 N2.getValueType() == MVT::Other && "Invalid token factor!");
911 break;
Chris Lattner76365122005-01-16 02:23:22 +0000912 case ISD::AND:
913 case ISD::OR:
914 case ISD::XOR:
915 case ISD::UDIV:
916 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000917 case ISD::MULHU:
918 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000919 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
920 // fall through
921 case ISD::ADD:
922 case ISD::SUB:
923 case ISD::MUL:
924 case ISD::SDIV:
925 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +0000926 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
927 // fall through.
928 case ISD::FADD:
929 case ISD::FSUB:
930 case ISD::FMUL:
931 case ISD::FDIV:
932 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +0000933 assert(N1.getValueType() == N2.getValueType() &&
934 N1.getValueType() == VT && "Binary operator types must match!");
935 break;
936
937 case ISD::SHL:
938 case ISD::SRA:
939 case ISD::SRL:
940 assert(VT == N1.getValueType() &&
941 "Shift operators return type must be the same as their first arg");
942 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000943 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000944 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000945 case ISD::FP_ROUND_INREG: {
946 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
947 assert(VT == N1.getValueType() && "Not an inreg round!");
948 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
949 "Cannot FP_ROUND_INREG integer types");
950 assert(EVT <= VT && "Not rounding down!");
951 break;
952 }
Nate Begeman56eb8682005-08-30 02:44:00 +0000953 case ISD::AssertSext:
954 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +0000955 case ISD::SIGN_EXTEND_INREG: {
956 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
957 assert(VT == N1.getValueType() && "Not an inreg extend!");
958 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
959 "Cannot *_EXTEND_INREG FP types");
960 assert(EVT <= VT && "Not extending!");
961 }
962
Chris Lattner76365122005-01-16 02:23:22 +0000963 default: break;
964 }
965#endif
966
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
968 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
969 if (N1C) {
970 if (N2C) {
971 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
972 switch (Opcode) {
973 case ISD::ADD: return getConstant(C1 + C2, VT);
974 case ISD::SUB: return getConstant(C1 - C2, VT);
975 case ISD::MUL: return getConstant(C1 * C2, VT);
976 case ISD::UDIV:
977 if (C2) return getConstant(C1 / C2, VT);
978 break;
979 case ISD::UREM :
980 if (C2) return getConstant(C1 % C2, VT);
981 break;
982 case ISD::SDIV :
983 if (C2) return getConstant(N1C->getSignExtended() /
984 N2C->getSignExtended(), VT);
985 break;
986 case ISD::SREM :
987 if (C2) return getConstant(N1C->getSignExtended() %
988 N2C->getSignExtended(), VT);
989 break;
990 case ISD::AND : return getConstant(C1 & C2, VT);
991 case ISD::OR : return getConstant(C1 | C2, VT);
992 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +0000993 case ISD::SHL : return getConstant(C1 << C2, VT);
994 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000995 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996 default: break;
997 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000998 } else { // Cannonicalize constant to RHS if commutative
999 if (isCommutativeBinOp(Opcode)) {
1000 std::swap(N1C, N2C);
1001 std::swap(N1, N2);
1002 }
1003 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 }
1005
1006 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1007 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001008 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001009 if (N2CFP) {
1010 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1011 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001012 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1013 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1014 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1015 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016 if (C2) return getConstantFP(C1 / C2, VT);
1017 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001018 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001019 if (C2) return getConstantFP(fmod(C1, C2), VT);
1020 break;
1021 default: break;
1022 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023 } else { // Cannonicalize constant to RHS if commutative
1024 if (isCommutativeBinOp(Opcode)) {
1025 std::swap(N1CFP, N2CFP);
1026 std::swap(N1, N2);
1027 }
1028 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001029 }
1030
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 // Finally, fold operations that do not require constants.
1032 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001033 case ISD::FP_ROUND_INREG:
1034 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1035 break;
1036 case ISD::SIGN_EXTEND_INREG: {
1037 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1038 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001039 break;
1040 }
1041
Nate Begemaneea805e2005-04-13 21:23:31 +00001042 // FIXME: figure out how to safely handle things like
1043 // int foo(int x) { return 1 << (x & 255); }
1044 // int bar() { return foo(256); }
1045#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001046 case ISD::SHL:
1047 case ISD::SRL:
1048 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001049 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001050 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001051 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001052 else if (N2.getOpcode() == ISD::AND)
1053 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1054 // If the and is only masking out bits that cannot effect the shift,
1055 // eliminate the and.
1056 unsigned NumBits = MVT::getSizeInBits(VT);
1057 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1058 return getNode(Opcode, VT, N1, N2.getOperand(0));
1059 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001060 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001061#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 }
1063
Chris Lattner27e9b412005-05-11 18:57:39 +00001064 // Memoize this node if possible.
1065 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001066 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1067 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001068 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1069 if (BON) return SDOperand(BON, 0);
1070
1071 BON = N = new SDNode(Opcode, N1, N2);
1072 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001073 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001074 }
1075
Chris Lattner3e011362005-05-14 07:45:46 +00001076 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 AllNodes.push_back(N);
1078 return SDOperand(N, 0);
1079}
1080
Chris Lattner88de6e72005-05-12 00:17:04 +00001081// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001082// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001083void SDNode::setAdjCallChain(SDOperand N) {
1084 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001085 assert((getOpcode() == ISD::CALLSEQ_START ||
1086 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001087
Chris Lattner65113b22005-11-08 22:07:03 +00001088 OperandList[0].Val->removeUser(this);
1089 OperandList[0] = N;
1090 OperandList[0].Val->Uses.push_back(this);
Chris Lattner27e9b412005-05-11 18:57:39 +00001091}
1092
1093
1094
Chris Lattnerc3aae252005-01-07 07:46:32 +00001095SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001096 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001097 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1099 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001100 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001101
1102 // Loads have a token chain.
1103 N->setValueTypes(VT, MVT::Other);
1104 AllNodes.push_back(N);
1105 return SDOperand(N, 0);
1106}
1107
Chris Lattner5f056bf2005-07-10 01:55:33 +00001108
1109SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1110 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1111 MVT::ValueType EVT) {
1112 std::vector<SDOperand> Ops;
1113 Ops.reserve(4);
1114 Ops.push_back(Chain);
1115 Ops.push_back(Ptr);
1116 Ops.push_back(SV);
1117 Ops.push_back(getValueType(EVT));
1118 std::vector<MVT::ValueType> VTs;
1119 VTs.reserve(2);
1120 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1121 return getNode(Opcode, VTs, Ops);
1122}
1123
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1125 SDOperand N1, SDOperand N2, SDOperand N3) {
1126 // Perform various simplifications.
1127 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1128 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1129 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1130 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001131 case ISD::SETCC: {
1132 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001133 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001134 if (Simp.Val) return Simp;
1135 break;
1136 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 case ISD::SELECT:
1138 if (N1C)
1139 if (N1C->getValue())
1140 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001141 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001142 return N3; // select false, X, Y -> Y
1143
1144 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001146 case ISD::BRCOND:
1147 if (N2C)
1148 if (N2C->getValue()) // Unconditional branch
1149 return getNode(ISD::BR, MVT::Other, N1, N3);
1150 else
1151 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001152 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 }
1154
Chris Lattner385328c2005-05-14 07:42:29 +00001155 std::vector<SDOperand> Ops;
1156 Ops.reserve(3);
1157 Ops.push_back(N1);
1158 Ops.push_back(N2);
1159 Ops.push_back(N3);
1160
Chris Lattner43247a12005-08-25 19:12:10 +00001161 // Memoize node if it doesn't produce a flag.
1162 SDNode *N;
1163 if (VT != MVT::Flag) {
1164 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1165 if (E) return SDOperand(E, 0);
1166 E = N = new SDNode(Opcode, N1, N2, N3);
1167 } else {
1168 N = new SDNode(Opcode, N1, N2, N3);
1169 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001170 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 AllNodes.push_back(N);
1172 return SDOperand(N, 0);
1173}
1174
1175SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001176 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001177 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001178 std::vector<SDOperand> Ops;
1179 Ops.reserve(4);
1180 Ops.push_back(N1);
1181 Ops.push_back(N2);
1182 Ops.push_back(N3);
1183 Ops.push_back(N4);
1184 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001185}
1186
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001187SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1188 SDOperand N1, SDOperand N2, SDOperand N3,
1189 SDOperand N4, SDOperand N5) {
1190 std::vector<SDOperand> Ops;
1191 Ops.reserve(5);
1192 Ops.push_back(N1);
1193 Ops.push_back(N2);
1194 Ops.push_back(N3);
1195 Ops.push_back(N4);
1196 Ops.push_back(N5);
1197 return getNode(Opcode, VT, Ops);
1198}
1199
1200
Chris Lattner0437cdd2005-05-09 04:14:13 +00001201SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001202 assert((!V || isa<PointerType>(V->getType())) &&
1203 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001204 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1205 if (N) return SDOperand(N, 0);
1206
1207 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001208 AllNodes.push_back(N);
1209 return SDOperand(N, 0);
1210}
1211
1212SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001213 std::vector<SDOperand> &Ops) {
1214 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001215 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001216 case 1: return getNode(Opcode, VT, Ops[0]);
1217 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1218 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001219 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 }
Chris Lattneref847df2005-04-09 03:27:28 +00001221
Chris Lattner89c34632005-05-14 06:20:26 +00001222 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001223 switch (Opcode) {
1224 default: break;
1225 case ISD::BRCONDTWOWAY:
1226 if (N1C)
1227 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001228 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001229 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001230 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001231 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001232 case ISD::BRTWOWAY_CC:
1233 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1234 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1235 "LHS and RHS of comparison must have same type!");
1236 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001237 case ISD::TRUNCSTORE: {
1238 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1239 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1240#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1241 // If this is a truncating store of a constant, convert to the desired type
1242 // and store it instead.
1243 if (isa<Constant>(Ops[0])) {
1244 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1245 if (isa<Constant>(Op))
1246 N1 = Op;
1247 }
1248 // Also for ConstantFP?
1249#endif
1250 if (Ops[0].getValueType() == EVT) // Normal store?
1251 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1252 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1253 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1254 "Can't do FP-INT conversion!");
1255 break;
1256 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001257 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001258 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001259 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1260 "LHS and RHS of condition must have same type!");
1261 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1262 "True and False arms of SelectCC must have same type!");
1263 assert(Ops[2].getValueType() == VT &&
1264 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001265 break;
1266 }
1267 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001268 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001269 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1270 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001271 break;
1272 }
Chris Lattneref847df2005-04-09 03:27:28 +00001273 }
1274
Chris Lattner385328c2005-05-14 07:42:29 +00001275 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001276 SDNode *N;
1277 if (VT != MVT::Flag) {
1278 SDNode *&E =
1279 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1280 if (E) return SDOperand(E, 0);
1281 E = N = new SDNode(Opcode, Ops);
1282 } else {
1283 N = new SDNode(Opcode, Ops);
1284 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001285 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001286 AllNodes.push_back(N);
1287 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001288}
1289
Chris Lattner89c34632005-05-14 06:20:26 +00001290SDOperand SelectionDAG::getNode(unsigned Opcode,
1291 std::vector<MVT::ValueType> &ResultTys,
1292 std::vector<SDOperand> &Ops) {
1293 if (ResultTys.size() == 1)
1294 return getNode(Opcode, ResultTys[0], Ops);
1295
Chris Lattner5f056bf2005-07-10 01:55:33 +00001296 switch (Opcode) {
1297 case ISD::EXTLOAD:
1298 case ISD::SEXTLOAD:
1299 case ISD::ZEXTLOAD: {
1300 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1301 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1302 // If they are asking for an extending load from/to the same thing, return a
1303 // normal load.
1304 if (ResultTys[0] == EVT)
1305 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1306 assert(EVT < ResultTys[0] &&
1307 "Should only be an extending load, not truncating!");
1308 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1309 "Cannot sign/zero extend a FP load!");
1310 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1311 "Cannot convert from FP to Int or Int -> FP!");
1312 break;
1313 }
1314
Chris Lattnere89083a2005-05-14 07:25:05 +00001315 // FIXME: figure out how to safely handle things like
1316 // int foo(int x) { return 1 << (x & 255); }
1317 // int bar() { return foo(256); }
1318#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001319 case ISD::SRA_PARTS:
1320 case ISD::SRL_PARTS:
1321 case ISD::SHL_PARTS:
1322 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001323 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001324 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1325 else if (N3.getOpcode() == ISD::AND)
1326 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1327 // If the and is only masking out bits that cannot effect the shift,
1328 // eliminate the and.
1329 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1330 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1331 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1332 }
1333 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001334#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001335 }
Chris Lattner89c34632005-05-14 06:20:26 +00001336
Chris Lattner43247a12005-08-25 19:12:10 +00001337 // Memoize the node unless it returns a flag.
1338 SDNode *N;
1339 if (ResultTys.back() != MVT::Flag) {
1340 SDNode *&E =
1341 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1342 if (E) return SDOperand(E, 0);
1343 E = N = new SDNode(Opcode, Ops);
1344 } else {
1345 N = new SDNode(Opcode, Ops);
1346 }
Chris Lattner89c34632005-05-14 06:20:26 +00001347 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001348 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001349 return SDOperand(N, 0);
1350}
1351
Chris Lattner149c58c2005-08-16 18:17:10 +00001352
1353/// SelectNodeTo - These are used for target selectors to *mutate* the
1354/// specified node to have the specified return type, Target opcode, and
1355/// operands. Note that target opcodes are stored as
1356/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001357void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1358 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00001359 RemoveNodeFromCSEMaps(N);
1360 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1361 N->setValueTypes(VT);
1362}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001363void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1364 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00001365 RemoveNodeFromCSEMaps(N);
1366 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1367 N->setValueTypes(VT);
1368 N->setOperands(Op1);
1369}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001370void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1371 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001372 SDOperand Op2) {
1373 RemoveNodeFromCSEMaps(N);
1374 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1375 N->setValueTypes(VT);
1376 N->setOperands(Op1, Op2);
1377}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001378void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00001379 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001380 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00001381 RemoveNodeFromCSEMaps(N);
1382 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1383 N->setValueTypes(VT1, VT2);
1384 N->setOperands(Op1, Op2);
1385}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001386void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1387 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001388 SDOperand Op2, SDOperand Op3) {
1389 RemoveNodeFromCSEMaps(N);
1390 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1391 N->setValueTypes(VT);
1392 N->setOperands(Op1, Op2, Op3);
1393}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001394void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1395 MVT::ValueType VT1, MVT::ValueType VT2,
1396 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001397 RemoveNodeFromCSEMaps(N);
1398 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1399 N->setValueTypes(VT1, VT2);
1400 N->setOperands(Op1, Op2, Op3);
1401}
1402
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001403void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1404 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00001405 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1406 RemoveNodeFromCSEMaps(N);
1407 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1408 N->setValueTypes(VT);
1409 N->setOperands(Op1, Op2, Op3, Op4);
1410}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001411void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1412 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00001413 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1414 SDOperand Op5) {
1415 RemoveNodeFromCSEMaps(N);
1416 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1417 N->setValueTypes(VT);
1418 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1419}
Chris Lattner149c58c2005-08-16 18:17:10 +00001420
Chris Lattner8b8749f2005-08-17 19:00:20 +00001421/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1422/// This can cause recursive merging of nodes in the DAG.
1423///
Chris Lattner8b52f212005-08-26 18:36:28 +00001424/// This version assumes From/To have a single result value.
1425///
Chris Lattner1e111c72005-09-07 05:37:01 +00001426void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
1427 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001428 SDNode *From = FromN.Val, *To = ToN.Val;
1429 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
1430 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00001431 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00001432
Chris Lattner8b8749f2005-08-17 19:00:20 +00001433 while (!From->use_empty()) {
1434 // Process users until they are all gone.
1435 SDNode *U = *From->use_begin();
1436
1437 // This node is about to morph, remove its old self from the CSE maps.
1438 RemoveNodeFromCSEMaps(U);
1439
Chris Lattner65113b22005-11-08 22:07:03 +00001440 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1441 I != E; ++I)
1442 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001443 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001444 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00001445 To->addUser(U);
1446 }
1447
1448 // Now that we have modified U, add it back to the CSE maps. If it already
1449 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001450 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1451 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001452 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00001453 if (Deleted) Deleted->push_back(U);
1454 DeleteNodeNotInCSEMaps(U);
1455 }
Chris Lattner8b52f212005-08-26 18:36:28 +00001456 }
1457}
1458
1459/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1460/// This can cause recursive merging of nodes in the DAG.
1461///
1462/// This version assumes From/To have matching types and numbers of result
1463/// values.
1464///
Chris Lattner1e111c72005-09-07 05:37:01 +00001465void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
1466 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00001467 assert(From != To && "Cannot replace uses of with self");
1468 assert(From->getNumValues() == To->getNumValues() &&
1469 "Cannot use this version of ReplaceAllUsesWith!");
1470 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00001471 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00001472 return;
1473 }
1474
1475 while (!From->use_empty()) {
1476 // Process users until they are all gone.
1477 SDNode *U = *From->use_begin();
1478
1479 // This node is about to morph, remove its old self from the CSE maps.
1480 RemoveNodeFromCSEMaps(U);
1481
Chris Lattner65113b22005-11-08 22:07:03 +00001482 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1483 I != E; ++I)
1484 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00001485 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001486 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00001487 To->addUser(U);
1488 }
1489
1490 // Now that we have modified U, add it back to the CSE maps. If it already
1491 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001492 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1493 ReplaceAllUsesWith(U, Existing, Deleted);
1494 // U is now dead.
1495 if (Deleted) Deleted->push_back(U);
1496 DeleteNodeNotInCSEMaps(U);
1497 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00001498 }
1499}
1500
Chris Lattner8b52f212005-08-26 18:36:28 +00001501/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1502/// This can cause recursive merging of nodes in the DAG.
1503///
1504/// This version can replace From with any result values. To must match the
1505/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00001506void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00001507 const std::vector<SDOperand> &To,
1508 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001509 assert(From->getNumValues() == To.size() &&
1510 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00001511 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001512 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00001513 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00001514 return;
1515 }
1516
1517 while (!From->use_empty()) {
1518 // Process users until they are all gone.
1519 SDNode *U = *From->use_begin();
1520
1521 // This node is about to morph, remove its old self from the CSE maps.
1522 RemoveNodeFromCSEMaps(U);
1523
Chris Lattner65113b22005-11-08 22:07:03 +00001524 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
1525 I != E; ++I)
1526 if (I->Val == From) {
1527 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00001528 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00001529 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00001530 ToOp.Val->addUser(U);
1531 }
1532
1533 // Now that we have modified U, add it back to the CSE maps. If it already
1534 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00001535 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
1536 ReplaceAllUsesWith(U, Existing, Deleted);
1537 // U is now dead.
1538 if (Deleted) Deleted->push_back(U);
1539 DeleteNodeNotInCSEMaps(U);
1540 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001541 }
1542}
1543
1544
Jim Laskey58b968b2005-08-17 20:08:02 +00001545//===----------------------------------------------------------------------===//
1546// SDNode Class
1547//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001548
Chris Lattner5c884562005-01-12 18:37:47 +00001549/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1550/// indicated value. This method ignores uses of other values defined by this
1551/// operation.
1552bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1553 assert(Value < getNumValues() && "Bad value!");
1554
1555 // If there is only one value, this is easy.
1556 if (getNumValues() == 1)
1557 return use_size() == NUses;
1558 if (Uses.size() < NUses) return false;
1559
1560 SDOperand TheValue(this, Value);
1561
1562 std::set<SDNode*> UsersHandled;
1563
1564 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1565 UI != E; ++UI) {
1566 SDNode *User = *UI;
1567 if (User->getNumOperands() == 1 ||
1568 UsersHandled.insert(User).second) // First time we've seen this?
1569 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1570 if (User->getOperand(i) == TheValue) {
1571 if (NUses == 0)
1572 return false; // too many uses
1573 --NUses;
1574 }
1575 }
1576
1577 // Found exactly the right number of uses?
1578 return NUses == 0;
1579}
1580
1581
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001582const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001583 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001584 default:
1585 if (getOpcode() < ISD::BUILTIN_OP_END)
1586 return "<<Unknown DAG Node>>";
1587 else {
1588 if (G)
1589 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00001590 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
1591 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001592 return "<<Unknown Target Node>>";
1593 }
1594
Andrew Lenharth95762122005-03-31 21:24:06 +00001595 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001596 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001597 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001598 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001599 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00001600 case ISD::AssertSext: return "AssertSext";
1601 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001602 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001603 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001604 case ISD::ConstantFP: return "ConstantFP";
1605 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001606 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001607 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00001608 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001609 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001610 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001611 case ISD::ExternalSymbol: return "ExternalSymbol";
Andrew Lenharth2a2de662005-10-23 03:40:17 +00001612 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00001613 case ISD::ConstantPool: return "ConstantPool";
1614 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001615 case ISD::CopyToReg: return "CopyToReg";
1616 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001617 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001618 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001619
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001620 // Unary operators
1621 case ISD::FABS: return "fabs";
1622 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001623 case ISD::FSQRT: return "fsqrt";
1624 case ISD::FSIN: return "fsin";
1625 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001626
1627 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001628 case ISD::ADD: return "add";
1629 case ISD::SUB: return "sub";
1630 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001631 case ISD::MULHU: return "mulhu";
1632 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001633 case ISD::SDIV: return "sdiv";
1634 case ISD::UDIV: return "udiv";
1635 case ISD::SREM: return "srem";
1636 case ISD::UREM: return "urem";
1637 case ISD::AND: return "and";
1638 case ISD::OR: return "or";
1639 case ISD::XOR: return "xor";
1640 case ISD::SHL: return "shl";
1641 case ISD::SRA: return "sra";
1642 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00001643 case ISD::FADD: return "fadd";
1644 case ISD::FSUB: return "fsub";
1645 case ISD::FMUL: return "fmul";
1646 case ISD::FDIV: return "fdiv";
1647 case ISD::FREM: return "frem";
1648
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001649 case ISD::SETCC: return "setcc";
1650 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001651 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001652 case ISD::ADD_PARTS: return "add_parts";
1653 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001654 case ISD::SHL_PARTS: return "shl_parts";
1655 case ISD::SRA_PARTS: return "sra_parts";
1656 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001657
Chris Lattner7f644642005-04-28 21:44:03 +00001658 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001659 case ISD::SIGN_EXTEND: return "sign_extend";
1660 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00001661 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001662 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001663 case ISD::TRUNCATE: return "truncate";
1664 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001665 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001666 case ISD::FP_EXTEND: return "fp_extend";
1667
1668 case ISD::SINT_TO_FP: return "sint_to_fp";
1669 case ISD::UINT_TO_FP: return "uint_to_fp";
1670 case ISD::FP_TO_SINT: return "fp_to_sint";
1671 case ISD::FP_TO_UINT: return "fp_to_uint";
1672
1673 // Control flow instructions
1674 case ISD::BR: return "br";
1675 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001676 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001677 case ISD::BR_CC: return "br_cc";
1678 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001679 case ISD::RET: return "ret";
1680 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001681 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001682 case ISD::CALLSEQ_START: return "callseq_start";
1683 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001684
1685 // Other operators
1686 case ISD::LOAD: return "load";
1687 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001688 case ISD::EXTLOAD: return "extload";
1689 case ISD::SEXTLOAD: return "sextload";
1690 case ISD::ZEXTLOAD: return "zextload";
1691 case ISD::TRUNCSTORE: return "truncstore";
1692
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001693 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1694 case ISD::EXTRACT_ELEMENT: return "extract_element";
1695 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001696 case ISD::MEMSET: return "memset";
1697 case ISD::MEMCPY: return "memcpy";
1698 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001699
Chris Lattner276260b2005-05-11 04:50:30 +00001700 // Bit counting
1701 case ISD::CTPOP: return "ctpop";
1702 case ISD::CTTZ: return "cttz";
1703 case ISD::CTLZ: return "ctlz";
1704
1705 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001706 case ISD::READPORT: return "readport";
1707 case ISD::WRITEPORT: return "writeport";
1708 case ISD::READIO: return "readio";
1709 case ISD::WRITEIO: return "writeio";
1710
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001711 case ISD::CONDCODE:
1712 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001713 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001714 case ISD::SETOEQ: return "setoeq";
1715 case ISD::SETOGT: return "setogt";
1716 case ISD::SETOGE: return "setoge";
1717 case ISD::SETOLT: return "setolt";
1718 case ISD::SETOLE: return "setole";
1719 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001720
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001721 case ISD::SETO: return "seto";
1722 case ISD::SETUO: return "setuo";
1723 case ISD::SETUEQ: return "setue";
1724 case ISD::SETUGT: return "setugt";
1725 case ISD::SETUGE: return "setuge";
1726 case ISD::SETULT: return "setult";
1727 case ISD::SETULE: return "setule";
1728 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001729
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001730 case ISD::SETEQ: return "seteq";
1731 case ISD::SETGT: return "setgt";
1732 case ISD::SETGE: return "setge";
1733 case ISD::SETLT: return "setlt";
1734 case ISD::SETLE: return "setle";
1735 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001736 }
1737 }
1738}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001739
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001740void SDNode::dump() const { dump(0); }
1741void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001742 std::cerr << (void*)this << ": ";
1743
1744 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1745 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001746 if (getValueType(i) == MVT::Other)
1747 std::cerr << "ch";
1748 else
1749 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001750 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001751 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001752
1753 std::cerr << " ";
1754 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1755 if (i) std::cerr << ", ";
1756 std::cerr << (void*)getOperand(i).Val;
1757 if (unsigned RN = getOperand(i).ResNo)
1758 std::cerr << ":" << RN;
1759 }
1760
1761 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1762 std::cerr << "<" << CSDN->getValue() << ">";
1763 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1764 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001765 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001766 dyn_cast<GlobalAddressSDNode>(this)) {
1767 std::cerr << "<";
1768 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001769 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001770 std::cerr << "<" << FIDN->getIndex() << ">";
1771 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00001772 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001773 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001774 std::cerr << "<";
1775 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1776 if (LBB)
1777 std::cerr << LBB->getName() << " ";
1778 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00001779 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00001780 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
1781 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
1782 } else {
1783 std::cerr << " #" << R->getReg();
1784 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001785 } else if (const ExternalSymbolSDNode *ES =
1786 dyn_cast<ExternalSymbolSDNode>(this)) {
1787 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001788 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1789 if (M->getValue())
1790 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1791 else
1792 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00001793 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
1794 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001795 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001796}
1797
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001798static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001799 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1800 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001801 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001802 else
1803 std::cerr << "\n" << std::string(indent+2, ' ')
1804 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001805
Chris Lattnerea946cd2005-01-09 20:38:33 +00001806
1807 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001808 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001809}
1810
Chris Lattnerc3aae252005-01-07 07:46:32 +00001811void SelectionDAG::dump() const {
1812 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001813 std::vector<SDNode*> Nodes(AllNodes);
1814 std::sort(Nodes.begin(), Nodes.end());
1815
1816 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001817 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001818 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001819 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001820
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001821 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001822
Chris Lattnerc3aae252005-01-07 07:46:32 +00001823 std::cerr << "\n\n";
1824}
1825