blob: ec778171692afe2c1b34a3b76d1ff8f428397fc8 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000024#include "llvm/ADT/SetVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000025#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000027#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000028#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000029#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Jeff Cohen3d68e152006-03-05 21:43:37 +000032#ifdef _MSC_VER
33#include <float.h>
34#define copysign _copysign
35#endif
36
Chris Lattner5cdcc582005-01-09 20:52:51 +000037static bool isCommutativeBinOp(unsigned Opcode) {
38 switch (Opcode) {
39 case ISD::ADD:
40 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000041 case ISD::MULHU:
42 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000043 case ISD::FADD:
44 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000045 case ISD::AND:
46 case ISD::OR:
47 case ISD::XOR: return true;
48 default: return false; // FIXME: Need commutative info for user ops!
49 }
50}
51
Chris Lattner5cdcc582005-01-09 20:52:51 +000052// isInvertibleForFree - Return true if there is no cost to emitting the logical
53// inverse of this node.
54static bool isInvertibleForFree(SDOperand N) {
55 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000056 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000057 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000058 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000059}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
69bool ConstantFPSDNode::isExactlyValue(double V) const {
70 return DoubleToBits(V) == DoubleToBits(Value);
71}
72
73//===----------------------------------------------------------------------===//
74// ISD Class
75//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000076
Chris Lattnerc3aae252005-01-07 07:46:32 +000077/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
78/// when given the operation for (X op Y).
79ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
80 // To perform this operation, we just need to swap the L and G bits of the
81 // operation.
82 unsigned OldL = (Operation >> 2) & 1;
83 unsigned OldG = (Operation >> 1) & 1;
84 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
85 (OldL << 1) | // New G bit
86 (OldG << 2)); // New L bit.
87}
88
89/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
90/// 'op' is a valid SetCC operation.
91ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
92 unsigned Operation = Op;
93 if (isInteger)
94 Operation ^= 7; // Flip L, G, E bits, but not U.
95 else
96 Operation ^= 15; // Flip all of the condition bits.
97 if (Operation > ISD::SETTRUE2)
98 Operation &= ~8; // Don't let N and U bits get set.
99 return ISD::CondCode(Operation);
100}
101
102
103/// isSignedOp - For an integer comparison, return 1 if the comparison is a
104/// signed operation and 2 if the result is an unsigned comparison. Return zero
105/// if the operation does not depend on the sign of the input (setne and seteq).
106static int isSignedOp(ISD::CondCode Opcode) {
107 switch (Opcode) {
108 default: assert(0 && "Illegal integer setcc operation!");
109 case ISD::SETEQ:
110 case ISD::SETNE: return 0;
111 case ISD::SETLT:
112 case ISD::SETLE:
113 case ISD::SETGT:
114 case ISD::SETGE: return 1;
115 case ISD::SETULT:
116 case ISD::SETULE:
117 case ISD::SETUGT:
118 case ISD::SETUGE: return 2;
119 }
120}
121
122/// getSetCCOrOperation - Return the result of a logical OR between different
123/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
124/// returns SETCC_INVALID if it is not possible to represent the resultant
125/// comparison.
126ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
127 bool isInteger) {
128 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
129 // Cannot fold a signed integer setcc with an unsigned integer setcc.
130 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131
Chris Lattnerc3aae252005-01-07 07:46:32 +0000132 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 // If the N and U bits get set then the resultant comparison DOES suddenly
135 // care about orderedness, and is true when ordered.
136 if (Op > ISD::SETTRUE2)
137 Op &= ~16; // Clear the N bit.
138 return ISD::CondCode(Op);
139}
140
141/// getSetCCAndOperation - Return the result of a logical AND between different
142/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
143/// function returns zero if it is not possible to represent the resultant
144/// comparison.
145ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
146 bool isInteger) {
147 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
148 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000149 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000150
151 // Combine all of the condition bits.
152 return ISD::CondCode(Op1 & Op2);
153}
154
Chris Lattnerb48da392005-01-23 04:39:44 +0000155const TargetMachine &SelectionDAG::getTarget() const {
156 return TLI.getTargetMachine();
157}
158
Jim Laskey58b968b2005-08-17 20:08:02 +0000159//===----------------------------------------------------------------------===//
160// SelectionDAG Class
161//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000162
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000163/// RemoveDeadNodes - This method deletes all unreachable nodes in the
164/// SelectionDAG, including nodes (like loads) that have uses of their token
165/// chain but no other uses and no side effect. If a node is passed in as an
166/// argument, it is used as the seed for node deletion.
167void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000168 // Create a dummy node (which is not added to allnodes), that adds a reference
169 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000170 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000171
Chris Lattnerf469cb62005-11-08 18:52:27 +0000172 bool MadeChange = false;
173
Chris Lattner8b8749f2005-08-17 19:00:20 +0000174 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000175 if (N && N->use_empty()) {
176 DestroyDeadNode(N);
177 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000178 }
179
Chris Lattnerde202b32005-11-09 23:47:37 +0000180 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
181 if (I->use_empty() && I->getOpcode() != 65535) {
182 // Node is dead, recursively delete newly dead uses.
183 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000184 MadeChange = true;
185 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000186
187 // Walk the nodes list, removing the nodes we've marked as dead.
188 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000189 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
190 SDNode *N = I++;
191 if (N->use_empty())
192 AllNodes.erase(N);
193 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000194 }
195
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000196 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000197 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000198}
199
Chris Lattnerf469cb62005-11-08 18:52:27 +0000200/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
201/// graph. If it is the last user of any of its operands, recursively process
202/// them the same way.
203///
204void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000205 // Okay, we really are going to delete this node. First take this out of the
206 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000207 RemoveNodeFromCSEMaps(N);
208
209 // Next, brutally remove the operand list. This is safe to do, as there are
210 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000211 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
212 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000213 O->removeUser(N);
214
215 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000216 if (O->use_empty())
217 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000218 }
Chris Lattner65113b22005-11-08 22:07:03 +0000219 delete[] N->OperandList;
220 N->OperandList = 0;
221 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000222
223 // Mark the node as dead.
224 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000225}
226
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000227void SelectionDAG::DeleteNode(SDNode *N) {
228 assert(N->use_empty() && "Cannot delete a node that is not dead!");
229
230 // First take this out of the appropriate CSE map.
231 RemoveNodeFromCSEMaps(N);
232
Chris Lattner1e111c72005-09-07 05:37:01 +0000233 // Finally, remove uses due to operands of this node, remove from the
234 // AllNodes list, and delete the node.
235 DeleteNodeNotInCSEMaps(N);
236}
237
238void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
239
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000240 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000241 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000242
243 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000244 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
245 I->Val->removeUser(N);
246 delete[] N->OperandList;
247 N->OperandList = 0;
248 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000249
250 delete N;
251}
252
Chris Lattner149c58c2005-08-16 18:17:10 +0000253/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
254/// correspond to it. This is useful when we're about to delete or repurpose
255/// the node. We don't want future request for structurally identical nodes
256/// to return N anymore.
257void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000258 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000259 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000260 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000262 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
263 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000265 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000266 Erased = TargetConstants.erase(std::make_pair(
267 cast<ConstantSDNode>(N)->getValue(),
268 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000269 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000270 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000271 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000272 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000273 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000274 }
Chris Lattner3181a772006-01-29 06:26:56 +0000275 case ISD::TargetConstantFP: {
276 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
277 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
278 break;
279 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000280 case ISD::STRING:
281 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
282 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000283 case ISD::CONDCODE:
284 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
285 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000286 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000287 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
288 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000289 case ISD::GlobalAddress: {
290 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
291 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
292 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000293 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000294 }
295 case ISD::TargetGlobalAddress: {
296 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
297 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
298 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000299 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000300 }
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:
Evan Chengb8973bd2006-01-31 22:23:14 +0000308 Erased = ConstantPoolIndices.
309 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000310 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
311 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000313 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000314 Erased = TargetConstantPoolIndices.
315 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000316 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
317 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000318 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000319 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000320 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000321 break;
322 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000323 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000324 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000325 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000326 Erased =
327 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000328 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000329 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000330 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000331 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
332 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000333 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
335 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000336 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000337 case ISD::SRCVALUE: {
338 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000339 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000340 break;
341 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000342 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000343 Erased = Loads.erase(std::make_pair(N->getOperand(1),
344 std::make_pair(N->getOperand(0),
345 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000346 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000348 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000349 if (N->getNumOperands() == 0) {
350 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
351 N->getValueType(0)));
352 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000353 Erased =
354 UnaryOps.erase(std::make_pair(N->getOpcode(),
355 std::make_pair(N->getOperand(0),
356 N->getValueType(0))));
357 } else if (N->getNumOperands() == 2) {
358 Erased =
359 BinaryOps.erase(std::make_pair(N->getOpcode(),
360 std::make_pair(N->getOperand(0),
361 N->getOperand(1))));
362 } else {
363 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
364 Erased =
365 OneResultNodes.erase(std::make_pair(N->getOpcode(),
366 std::make_pair(N->getValueType(0),
367 Ops)));
368 }
Chris Lattner385328c2005-05-14 07:42:29 +0000369 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000370 // Remove the node from the ArbitraryNodes map.
371 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
372 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000373 Erased =
374 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
375 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000376 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000377 break;
378 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000379#ifndef NDEBUG
380 // Verify that the node was actually in one of the CSE maps, unless it has a
381 // flag result (which cannot be CSE'd) or is one of the special cases that are
382 // not subject to CSE.
383 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000384 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000385 N->dump();
386 assert(0 && "Node is not in map!");
387 }
388#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000389}
390
Chris Lattner8b8749f2005-08-17 19:00:20 +0000391/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
392/// has been taken out and modified in some way. If the specified node already
393/// exists in the CSE maps, do not modify the maps, but return the existing node
394/// instead. If it doesn't exist, add it and return null.
395///
396SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
397 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000398 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000399 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000400
Chris Lattner9f8cc692005-12-19 22:21:21 +0000401 // Check that remaining values produced are not flags.
402 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
403 if (N->getValueType(i) == MVT::Flag)
404 return 0; // Never CSE anything that produces a flag.
405
Chris Lattnerfe14b342005-12-01 23:14:50 +0000406 if (N->getNumValues() == 1) {
407 if (N->getNumOperands() == 1) {
408 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
409 std::make_pair(N->getOperand(0),
410 N->getValueType(0)))];
411 if (U) return U;
412 U = N;
413 } else if (N->getNumOperands() == 2) {
414 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
415 std::make_pair(N->getOperand(0),
416 N->getOperand(1)))];
417 if (B) return B;
418 B = N;
419 } else {
420 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
421 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000422 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000423 if (ORN) return ORN;
424 ORN = N;
425 }
426 } else {
427 if (N->getOpcode() == ISD::LOAD) {
428 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
429 std::make_pair(N->getOperand(0),
430 N->getValueType(0)))];
431 if (L) return L;
432 L = N;
433 } else {
434 // Remove the node from the ArbitraryNodes map.
435 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
436 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
437 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
438 std::make_pair(RV, Ops))];
439 if (AN) return AN;
440 AN = N;
441 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000442 }
443 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000444}
445
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000446/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
447/// were replaced with those specified. If this node is never memoized,
448/// return null, otherwise return a pointer to the slot it would take. If a
449/// node already exists with these operands, the slot will be non-null.
450SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000451 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000452 return 0; // Never add these nodes.
453
454 // Check that remaining values produced are not flags.
455 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
456 if (N->getValueType(i) == MVT::Flag)
457 return 0; // Never CSE anything that produces a flag.
458
459 if (N->getNumValues() == 1) {
460 return &UnaryOps[std::make_pair(N->getOpcode(),
461 std::make_pair(Op, N->getValueType(0)))];
462 } else {
463 // Remove the node from the ArbitraryNodes map.
464 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
465 std::vector<SDOperand> Ops;
466 Ops.push_back(Op);
467 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
468 std::make_pair(RV, Ops))];
469 }
470 return 0;
471}
472
473/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
474/// were replaced with those specified. If this node is never memoized,
475/// return null, otherwise return a pointer to the slot it would take. If a
476/// node already exists with these operands, the slot will be non-null.
477SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
478 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000479 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000480 return 0; // Never add these nodes.
481
482 // Check that remaining values produced are not flags.
483 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
484 if (N->getValueType(i) == MVT::Flag)
485 return 0; // Never CSE anything that produces a flag.
486
487 if (N->getNumValues() == 1) {
488 return &BinaryOps[std::make_pair(N->getOpcode(),
489 std::make_pair(Op1, Op2))];
490 } else {
491 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
492 std::vector<SDOperand> Ops;
493 Ops.push_back(Op1);
494 Ops.push_back(Op2);
495 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
496 std::make_pair(RV, Ops))];
497 }
498 return 0;
499}
500
501
502/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
503/// were replaced with those specified. If this node is never memoized,
504/// return null, otherwise return a pointer to the slot it would take. If a
505/// node already exists with these operands, the slot will be non-null.
506SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
507 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000508 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000509 return 0; // Never add these nodes.
510
511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return 0; // Never CSE anything that produces a flag.
515
516 if (N->getNumValues() == 1) {
517 if (N->getNumOperands() == 1) {
518 return &UnaryOps[std::make_pair(N->getOpcode(),
519 std::make_pair(Ops[0],
520 N->getValueType(0)))];
521 } else if (N->getNumOperands() == 2) {
522 return &BinaryOps[std::make_pair(N->getOpcode(),
523 std::make_pair(Ops[0], Ops[1]))];
524 } else {
525 return &OneResultNodes[std::make_pair(N->getOpcode(),
526 std::make_pair(N->getValueType(0),
527 Ops))];
528 }
529 } else {
530 if (N->getOpcode() == ISD::LOAD) {
531 return &Loads[std::make_pair(Ops[1],
532 std::make_pair(Ops[0], N->getValueType(0)))];
533 } else {
534 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
535 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
536 std::make_pair(RV, Ops))];
537 }
538 }
539 return 0;
540}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000541
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000542
Chris Lattner78ec3112003-08-11 14:57:33 +0000543SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000544 while (!AllNodes.empty()) {
545 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000546 delete [] N->OperandList;
547 N->OperandList = 0;
548 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000549 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000550 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000551}
552
Chris Lattner0f2287b2005-04-13 02:38:18 +0000553SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000554 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000555 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000556 return getNode(ISD::AND, Op.getValueType(), Op,
557 getConstant(Imm, Op.getValueType()));
558}
559
Chris Lattnerc3aae252005-01-07 07:46:32 +0000560SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
561 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
562 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000563 if (VT != MVT::i64)
564 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000565
Chris Lattnerc3aae252005-01-07 07:46:32 +0000566 SDNode *&N = Constants[std::make_pair(Val, VT)];
567 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000568 N = new ConstantSDNode(false, Val, VT);
569 AllNodes.push_back(N);
570 return SDOperand(N, 0);
571}
572
Chris Lattner36ce6912005-11-29 06:21:05 +0000573SDOperand SelectionDAG::getString(const std::string &Val) {
574 StringSDNode *&N = StringNodes[Val];
575 if (!N) {
576 N = new StringSDNode(Val);
577 AllNodes.push_back(N);
578 }
579 return SDOperand(N, 0);
580}
581
Chris Lattner37bfbb42005-08-17 00:34:06 +0000582SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
583 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
584 // Mask out any bits that are not valid for this constant.
585 if (VT != MVT::i64)
586 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
587
588 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
589 if (N) return SDOperand(N, 0);
590 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000591 AllNodes.push_back(N);
592 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000593}
594
Chris Lattnerc3aae252005-01-07 07:46:32 +0000595SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
596 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
597 if (VT == MVT::f32)
598 Val = (float)Val; // Mask out extra precision.
599
Chris Lattnerd8658612005-02-17 20:17:32 +0000600 // Do the map lookup using the actual bit pattern for the floating point
601 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
602 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000603 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000604 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000605 N = new ConstantFPSDNode(false, Val, VT);
606 AllNodes.push_back(N);
607 return SDOperand(N, 0);
608}
609
610SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
611 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
612 if (VT == MVT::f32)
613 Val = (float)Val; // Mask out extra precision.
614
615 // Do the map lookup using the actual bit pattern for the floating point
616 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
617 // we don't have issues with SNANs.
618 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
619 if (N) return SDOperand(N, 0);
620 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000621 AllNodes.push_back(N);
622 return SDOperand(N, 0);
623}
624
Chris Lattnerc3aae252005-01-07 07:46:32 +0000625SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000626 MVT::ValueType VT, int offset) {
627 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000629 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000630 AllNodes.push_back(N);
631 return SDOperand(N, 0);
632}
633
634SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000635 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000636 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000637 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000638 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000639 AllNodes.push_back(N);
640 return SDOperand(N, 0);
641}
642
643SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
644 SDNode *&N = FrameIndices[FI];
645 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000646 N = new FrameIndexSDNode(FI, VT, false);
647 AllNodes.push_back(N);
648 return SDOperand(N, 0);
649}
650
651SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
652 SDNode *&N = TargetFrameIndices[FI];
653 if (N) return SDOperand(N, 0);
654 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000655 AllNodes.push_back(N);
656 return SDOperand(N, 0);
657}
658
Evan Chengb8973bd2006-01-31 22:23:14 +0000659SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000660 unsigned Alignment, int Offset) {
661 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
662 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000663 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000664 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000665 AllNodes.push_back(N);
666 return SDOperand(N, 0);
667}
668
Evan Chengb8973bd2006-01-31 22:23:14 +0000669SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000670 unsigned Alignment, int Offset) {
671 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
672 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000673 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000674 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000675 AllNodes.push_back(N);
676 return SDOperand(N, 0);
677}
678
679SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
680 SDNode *&N = BBNodes[MBB];
681 if (N) return SDOperand(N, 0);
682 N = new BasicBlockSDNode(MBB);
683 AllNodes.push_back(N);
684 return SDOperand(N, 0);
685}
686
Chris Lattner15e4b012005-07-10 00:07:11 +0000687SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
688 if ((unsigned)VT >= ValueTypeNodes.size())
689 ValueTypeNodes.resize(VT+1);
690 if (ValueTypeNodes[VT] == 0) {
691 ValueTypeNodes[VT] = new VTSDNode(VT);
692 AllNodes.push_back(ValueTypeNodes[VT]);
693 }
694
695 return SDOperand(ValueTypeNodes[VT], 0);
696}
697
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
699 SDNode *&N = ExternalSymbols[Sym];
700 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000701 N = new ExternalSymbolSDNode(false, Sym, VT);
702 AllNodes.push_back(N);
703 return SDOperand(N, 0);
704}
705
Chris Lattner809ec112006-01-28 10:09:25 +0000706SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
707 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000708 SDNode *&N = TargetExternalSymbols[Sym];
709 if (N) return SDOperand(N, 0);
710 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000711 AllNodes.push_back(N);
712 return SDOperand(N, 0);
713}
714
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000715SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
716 if ((unsigned)Cond >= CondCodeNodes.size())
717 CondCodeNodes.resize(Cond+1);
718
Chris Lattner079a27a2005-08-09 20:40:02 +0000719 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000720 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000721 AllNodes.push_back(CondCodeNodes[Cond]);
722 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000723 return SDOperand(CondCodeNodes[Cond], 0);
724}
725
Chris Lattner0fdd7682005-08-30 22:38:38 +0000726SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
727 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
728 if (!Reg) {
729 Reg = new RegisterSDNode(RegNo, VT);
730 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000731 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000732 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000733}
734
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000735SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
736 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 // These setcc operations always fold.
738 switch (Cond) {
739 default: break;
740 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000741 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000742 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000743 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000744 }
745
Chris Lattner67255a12005-04-07 18:14:58 +0000746 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
747 uint64_t C2 = N2C->getValue();
748 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
749 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000750
Chris Lattnerc3aae252005-01-07 07:46:32 +0000751 // Sign extend the operands if required
752 if (ISD::isSignedIntSetCC(Cond)) {
753 C1 = N1C->getSignExtended();
754 C2 = N2C->getSignExtended();
755 }
756
757 switch (Cond) {
758 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000759 case ISD::SETEQ: return getConstant(C1 == C2, VT);
760 case ISD::SETNE: return getConstant(C1 != C2, VT);
761 case ISD::SETULT: return getConstant(C1 < C2, VT);
762 case ISD::SETUGT: return getConstant(C1 > C2, VT);
763 case ISD::SETULE: return getConstant(C1 <= C2, VT);
764 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
765 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
766 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
767 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
768 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000769 }
Chris Lattner24673922005-04-07 18:58:54 +0000770 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000771 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000772 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
773 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
774
775 // If the comparison constant has bits in the upper part, the
776 // zero-extended value could never match.
777 if (C2 & (~0ULL << InSize)) {
778 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
779 switch (Cond) {
780 case ISD::SETUGT:
781 case ISD::SETUGE:
782 case ISD::SETEQ: return getConstant(0, VT);
783 case ISD::SETULT:
784 case ISD::SETULE:
785 case ISD::SETNE: return getConstant(1, VT);
786 case ISD::SETGT:
787 case ISD::SETGE:
788 // True if the sign bit of C2 is set.
789 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
790 case ISD::SETLT:
791 case ISD::SETLE:
792 // True if the sign bit of C2 isn't set.
793 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
794 default:
795 break;
796 }
797 }
798
799 // Otherwise, we can perform the comparison with the low bits.
800 switch (Cond) {
801 case ISD::SETEQ:
802 case ISD::SETNE:
803 case ISD::SETUGT:
804 case ISD::SETUGE:
805 case ISD::SETULT:
806 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000807 return getSetCC(VT, N1.getOperand(0),
808 getConstant(C2, N1.getOperand(0).getValueType()),
809 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000810 default:
811 break; // todo, be more careful with signed comparisons
812 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000813 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
814 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
815 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
816 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
817 MVT::ValueType ExtDstTy = N1.getValueType();
818 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000819
Chris Lattner7b2880c2005-08-24 22:44:39 +0000820 // If the extended part has any inconsistent bits, it cannot ever
821 // compare equal. In other words, they have to be all ones or all
822 // zeros.
823 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000824 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000825 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
826 return getConstant(Cond == ISD::SETNE, VT);
827
828 // Otherwise, make this a use of a zext.
829 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000830 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000831 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000832 }
833
Chris Lattner67255a12005-04-07 18:14:58 +0000834 uint64_t MinVal, MaxVal;
835 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
836 if (ISD::isSignedIntSetCC(Cond)) {
837 MinVal = 1ULL << (OperandBitSize-1);
838 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
839 MaxVal = ~0ULL >> (65-OperandBitSize);
840 else
841 MaxVal = 0;
842 } else {
843 MinVal = 0;
844 MaxVal = ~0ULL >> (64-OperandBitSize);
845 }
846
847 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
848 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
849 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
850 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000851 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
852 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000853 }
854
855 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
856 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
857 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000858 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
859 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000860 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000861
Nate Begeman72ea2812005-04-14 08:56:52 +0000862 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
863 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000864
Nate Begeman72ea2812005-04-14 08:56:52 +0000865 // Canonicalize setgt X, Min --> setne X, Min
866 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000867 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000868
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000869 // If we have setult X, 1, turn it into seteq X, 0
870 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000871 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
872 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000873 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000874 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000875 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
876 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000877
878 // If we have "setcc X, C1", check to see if we can shrink the immediate
879 // by changing cc.
880
881 // SETUGT X, SINTMAX -> SETLT X, 0
882 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
883 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000884 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000885
886 // FIXME: Implement the rest of these.
887
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000888
889 // Fold bit comparisons when we can.
890 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
891 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
892 if (ConstantSDNode *AndRHS =
893 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
894 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
895 // Perform the xform if the AND RHS is a single bit.
896 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
897 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000898 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000899 TLI.getShiftAmountTy()));
900 }
901 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
902 // (X & 8) == 8 --> (X & 8) >> 3
903 // Perform the xform if C2 is a single bit.
904 if ((C2 & (C2-1)) == 0) {
905 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000906 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000907 }
908 }
909 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910 }
Chris Lattner67255a12005-04-07 18:14:58 +0000911 } else if (isa<ConstantSDNode>(N1.Val)) {
912 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000913 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000914 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000915
916 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
917 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
918 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000919
Chris Lattnerc3aae252005-01-07 07:46:32 +0000920 switch (Cond) {
921 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000922 case ISD::SETEQ: return getConstant(C1 == C2, VT);
923 case ISD::SETNE: return getConstant(C1 != C2, VT);
924 case ISD::SETLT: return getConstant(C1 < C2, VT);
925 case ISD::SETGT: return getConstant(C1 > C2, VT);
926 case ISD::SETLE: return getConstant(C1 <= C2, VT);
927 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000928 }
929 } else {
930 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000931 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000932 }
933
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000934 // Could not fold it.
935 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000936}
937
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000939///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000940SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000941 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
942 if (!N) {
943 N = new SDNode(Opcode, VT);
944 AllNodes.push_back(N);
945 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000946 return SDOperand(N, 0);
947}
948
Chris Lattnerc3aae252005-01-07 07:46:32 +0000949SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
950 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000951 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +0000952 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000953 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
954 uint64_t Val = C->getValue();
955 switch (Opcode) {
956 default: break;
957 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +0000958 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +0000959 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
960 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000961 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
962 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +0000963 case ISD::BIT_CONVERT:
964 if (VT == MVT::f32) {
965 assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!");
966 return getConstantFP(BitsToFloat(Val), VT);
967 } else if (VT == MVT::f64) {
968 assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!");
969 return getConstantFP(BitsToDouble(Val), VT);
970 }
971 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +0000972 case ISD::BSWAP:
973 switch(VT) {
974 default: assert(0 && "Invalid bswap!"); break;
975 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
976 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
977 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
978 }
979 break;
980 case ISD::CTPOP:
981 switch(VT) {
982 default: assert(0 && "Invalid ctpop!"); break;
983 case MVT::i1: return getConstant(Val != 0, VT);
984 case MVT::i8:
985 Tmp1 = (unsigned)Val & 0xFF;
986 return getConstant(CountPopulation_32(Tmp1), VT);
987 case MVT::i16:
988 Tmp1 = (unsigned)Val & 0xFFFF;
989 return getConstant(CountPopulation_32(Tmp1), VT);
990 case MVT::i32:
991 return getConstant(CountPopulation_32((unsigned)Val), VT);
992 case MVT::i64:
993 return getConstant(CountPopulation_64(Val), VT);
994 }
995 case ISD::CTLZ:
996 switch(VT) {
997 default: assert(0 && "Invalid ctlz!"); break;
998 case MVT::i1: return getConstant(Val == 0, VT);
999 case MVT::i8:
1000 Tmp1 = (unsigned)Val & 0xFF;
1001 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1002 case MVT::i16:
1003 Tmp1 = (unsigned)Val & 0xFFFF;
1004 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1005 case MVT::i32:
1006 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1007 case MVT::i64:
1008 return getConstant(CountLeadingZeros_64(Val), VT);
1009 }
1010 case ISD::CTTZ:
1011 switch(VT) {
1012 default: assert(0 && "Invalid cttz!"); break;
1013 case MVT::i1: return getConstant(Val == 0, VT);
1014 case MVT::i8:
1015 Tmp1 = (unsigned)Val | 0x100;
1016 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1017 case MVT::i16:
1018 Tmp1 = (unsigned)Val | 0x10000;
1019 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1020 case MVT::i32:
1021 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1022 case MVT::i64:
1023 return getConstant(CountTrailingZeros_64(Val), VT);
1024 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001025 }
1026 }
1027
Chris Lattner94683772005-12-23 05:30:37 +00001028 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1030 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001031 case ISD::FNEG:
1032 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001033 case ISD::FABS:
1034 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001035 case ISD::FP_ROUND:
1036 case ISD::FP_EXTEND:
1037 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001038 case ISD::FP_TO_SINT:
1039 return getConstant((int64_t)C->getValue(), VT);
1040 case ISD::FP_TO_UINT:
1041 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001042 case ISD::BIT_CONVERT:
1043 if (VT == MVT::i32) {
1044 assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!");
1045 return getConstant(FloatToBits(C->getValue()), VT);
1046 } else if (VT == MVT::i64) {
1047 assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!");
1048 return getConstant(DoubleToBits(C->getValue()), VT);
1049 }
1050 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 }
1052
1053 unsigned OpOpcode = Operand.Val->getOpcode();
1054 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001055 case ISD::TokenFactor:
1056 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057 case ISD::SIGN_EXTEND:
1058 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001059 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001060 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1061 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1062 break;
1063 case ISD::ZERO_EXTEND:
1064 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001065 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001066 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001067 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001068 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001069 case ISD::ANY_EXTEND:
1070 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001071 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001072 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1073 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1074 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1075 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 case ISD::TRUNCATE:
1077 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001078 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001079 if (OpOpcode == ISD::TRUNCATE)
1080 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001081 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1082 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001083 // If the source is smaller than the dest, we still need an extend.
1084 if (Operand.Val->getOperand(0).getValueType() < VT)
1085 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1086 else if (Operand.Val->getOperand(0).getValueType() > VT)
1087 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1088 else
1089 return Operand.Val->getOperand(0);
1090 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 break;
Chris Lattner35481892005-12-23 00:16:34 +00001092 case ISD::BIT_CONVERT:
1093 // Basic sanity checking.
1094 assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) &&
1095 "Cannot BIT_CONVERT between two different types!");
1096 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001097 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1098 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001099 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001100 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001101 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1102 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001103 Operand.Val->getOperand(0));
1104 if (OpOpcode == ISD::FNEG) // --X -> X
1105 return Operand.Val->getOperand(0);
1106 break;
1107 case ISD::FABS:
1108 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1109 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1110 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001111 }
1112
Chris Lattner43247a12005-08-25 19:12:10 +00001113 SDNode *N;
1114 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1115 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001116 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001117 E = N = new SDNode(Opcode, Operand);
1118 } else {
1119 N = new SDNode(Opcode, Operand);
1120 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001121 N->setValueTypes(VT);
1122 AllNodes.push_back(N);
1123 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001124}
1125
Chris Lattner36019aa2005-04-18 03:48:41 +00001126
1127
Chris Lattnerc3aae252005-01-07 07:46:32 +00001128SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1129 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001130#ifndef NDEBUG
1131 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001132 case ISD::TokenFactor:
1133 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1134 N2.getValueType() == MVT::Other && "Invalid token factor!");
1135 break;
Chris Lattner76365122005-01-16 02:23:22 +00001136 case ISD::AND:
1137 case ISD::OR:
1138 case ISD::XOR:
1139 case ISD::UDIV:
1140 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001141 case ISD::MULHU:
1142 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001143 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1144 // fall through
1145 case ISD::ADD:
1146 case ISD::SUB:
1147 case ISD::MUL:
1148 case ISD::SDIV:
1149 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001150 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1151 // fall through.
1152 case ISD::FADD:
1153 case ISD::FSUB:
1154 case ISD::FMUL:
1155 case ISD::FDIV:
1156 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001157 assert(N1.getValueType() == N2.getValueType() &&
1158 N1.getValueType() == VT && "Binary operator types must match!");
1159 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001160 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1161 assert(N1.getValueType() == VT &&
1162 MVT::isFloatingPoint(N1.getValueType()) &&
1163 MVT::isFloatingPoint(N2.getValueType()) &&
1164 "Invalid FCOPYSIGN!");
1165 break;
Chris Lattner76365122005-01-16 02:23:22 +00001166 case ISD::SHL:
1167 case ISD::SRA:
1168 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001169 case ISD::ROTL:
1170 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001171 assert(VT == N1.getValueType() &&
1172 "Shift operators return type must be the same as their first arg");
1173 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001174 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001175 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001176 case ISD::FP_ROUND_INREG: {
1177 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1178 assert(VT == N1.getValueType() && "Not an inreg round!");
1179 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1180 "Cannot FP_ROUND_INREG integer types");
1181 assert(EVT <= VT && "Not rounding down!");
1182 break;
1183 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001184 case ISD::AssertSext:
1185 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001186 case ISD::SIGN_EXTEND_INREG: {
1187 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1188 assert(VT == N1.getValueType() && "Not an inreg extend!");
1189 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1190 "Cannot *_EXTEND_INREG FP types");
1191 assert(EVT <= VT && "Not extending!");
1192 }
1193
Chris Lattner76365122005-01-16 02:23:22 +00001194 default: break;
1195 }
1196#endif
1197
Chris Lattnerc3aae252005-01-07 07:46:32 +00001198 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1199 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1200 if (N1C) {
1201 if (N2C) {
1202 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1203 switch (Opcode) {
1204 case ISD::ADD: return getConstant(C1 + C2, VT);
1205 case ISD::SUB: return getConstant(C1 - C2, VT);
1206 case ISD::MUL: return getConstant(C1 * C2, VT);
1207 case ISD::UDIV:
1208 if (C2) return getConstant(C1 / C2, VT);
1209 break;
1210 case ISD::UREM :
1211 if (C2) return getConstant(C1 % C2, VT);
1212 break;
1213 case ISD::SDIV :
1214 if (C2) return getConstant(N1C->getSignExtended() /
1215 N2C->getSignExtended(), VT);
1216 break;
1217 case ISD::SREM :
1218 if (C2) return getConstant(N1C->getSignExtended() %
1219 N2C->getSignExtended(), VT);
1220 break;
1221 case ISD::AND : return getConstant(C1 & C2, VT);
1222 case ISD::OR : return getConstant(C1 | C2, VT);
1223 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001224 case ISD::SHL : return getConstant(C1 << C2, VT);
1225 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001226 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001227 case ISD::ROTL :
1228 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1229 VT);
1230 case ISD::ROTR :
1231 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1232 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001233 default: break;
1234 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 } else { // Cannonicalize constant to RHS if commutative
1236 if (isCommutativeBinOp(Opcode)) {
1237 std::swap(N1C, N2C);
1238 std::swap(N1, N2);
1239 }
1240 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 }
1242
1243 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1244 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001245 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001246 if (N2CFP) {
1247 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1248 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001249 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1250 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1251 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1252 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001253 if (C2) return getConstantFP(C1 / C2, VT);
1254 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001255 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001256 if (C2) return getConstantFP(fmod(C1, C2), VT);
1257 break;
Chris Lattner12d83032006-03-05 05:30:57 +00001258 case ISD::FCOPYSIGN:
1259 return getConstantFP(copysign(C1, C2), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001260 default: break;
1261 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001262 } else { // Cannonicalize constant to RHS if commutative
1263 if (isCommutativeBinOp(Opcode)) {
1264 std::swap(N1CFP, N2CFP);
1265 std::swap(N1, N2);
1266 }
1267 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001268 }
1269
Chris Lattnerc3aae252005-01-07 07:46:32 +00001270 // Finally, fold operations that do not require constants.
1271 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001272 case ISD::FP_ROUND_INREG:
1273 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1274 break;
1275 case ISD::SIGN_EXTEND_INREG: {
1276 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1277 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001278 break;
1279 }
1280
Nate Begemaneea805e2005-04-13 21:23:31 +00001281 // FIXME: figure out how to safely handle things like
1282 // int foo(int x) { return 1 << (x & 255); }
1283 // int bar() { return foo(256); }
1284#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001285 case ISD::SHL:
1286 case ISD::SRL:
1287 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001288 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001289 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001290 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001291 else if (N2.getOpcode() == ISD::AND)
1292 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1293 // If the and is only masking out bits that cannot effect the shift,
1294 // eliminate the and.
1295 unsigned NumBits = MVT::getSizeInBits(VT);
1296 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1297 return getNode(Opcode, VT, N1, N2.getOperand(0));
1298 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001299 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001300#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 }
1302
Chris Lattner27e9b412005-05-11 18:57:39 +00001303 // Memoize this node if possible.
1304 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001305 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001306 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1307 if (BON) return SDOperand(BON, 0);
1308
1309 BON = N = new SDNode(Opcode, N1, N2);
1310 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001311 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001312 }
1313
Chris Lattner3e011362005-05-14 07:45:46 +00001314 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001315 AllNodes.push_back(N);
1316 return SDOperand(N, 0);
1317}
1318
Chris Lattnerc3aae252005-01-07 07:46:32 +00001319SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1320 SDOperand N1, SDOperand N2, SDOperand N3) {
1321 // Perform various simplifications.
1322 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1323 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1324 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1325 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001326 case ISD::SETCC: {
1327 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001328 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001329 if (Simp.Val) return Simp;
1330 break;
1331 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 case ISD::SELECT:
1333 if (N1C)
1334 if (N1C->getValue())
1335 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001336 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001337 return N3; // select false, X, Y -> Y
1338
1339 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001340 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001341 case ISD::BRCOND:
1342 if (N2C)
1343 if (N2C->getValue()) // Unconditional branch
1344 return getNode(ISD::BR, MVT::Other, N1, N3);
1345 else
1346 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001347 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001348 }
1349
Chris Lattner385328c2005-05-14 07:42:29 +00001350 std::vector<SDOperand> Ops;
1351 Ops.reserve(3);
1352 Ops.push_back(N1);
1353 Ops.push_back(N2);
1354 Ops.push_back(N3);
1355
Chris Lattner43247a12005-08-25 19:12:10 +00001356 // Memoize node if it doesn't produce a flag.
1357 SDNode *N;
1358 if (VT != MVT::Flag) {
1359 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1360 if (E) return SDOperand(E, 0);
1361 E = N = new SDNode(Opcode, N1, N2, N3);
1362 } else {
1363 N = new SDNode(Opcode, N1, N2, N3);
1364 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001365 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001366 AllNodes.push_back(N);
1367 return SDOperand(N, 0);
1368}
1369
1370SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001371 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001372 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001373 std::vector<SDOperand> Ops;
1374 Ops.reserve(4);
1375 Ops.push_back(N1);
1376 Ops.push_back(N2);
1377 Ops.push_back(N3);
1378 Ops.push_back(N4);
1379 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001380}
1381
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001382SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1383 SDOperand N1, SDOperand N2, SDOperand N3,
1384 SDOperand N4, SDOperand N5) {
1385 std::vector<SDOperand> Ops;
1386 Ops.reserve(5);
1387 Ops.push_back(N1);
1388 Ops.push_back(N2);
1389 Ops.push_back(N3);
1390 Ops.push_back(N4);
1391 Ops.push_back(N5);
1392 return getNode(Opcode, VT, Ops);
1393}
1394
Evan Cheng7038daf2005-12-10 00:37:58 +00001395SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1396 SDOperand Chain, SDOperand Ptr,
1397 SDOperand SV) {
1398 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1399 if (N) return SDOperand(N, 0);
1400 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1401
1402 // Loads have a token chain.
1403 setNodeValueTypes(N, VT, MVT::Other);
1404 AllNodes.push_back(N);
1405 return SDOperand(N, 0);
1406}
1407
1408SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1409 SDOperand Chain, SDOperand Ptr,
1410 SDOperand SV) {
1411 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1412 if (N) return SDOperand(N, 0);
1413 std::vector<SDOperand> Ops;
1414 Ops.reserve(5);
Evan Cheng7038daf2005-12-10 00:37:58 +00001415 Ops.push_back(getConstant(Count, MVT::i32));
1416 Ops.push_back(getValueType(EVT));
Evan Cheng1ab7d852006-03-01 00:51:13 +00001417 Ops.push_back(Chain);
1418 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001419 Ops.push_back(SV);
1420 std::vector<MVT::ValueType> VTs;
1421 VTs.reserve(2);
1422 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1423 return getNode(ISD::VLOAD, VTs, Ops);
1424}
1425
1426SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1427 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1428 MVT::ValueType EVT) {
1429 std::vector<SDOperand> Ops;
1430 Ops.reserve(4);
1431 Ops.push_back(Chain);
1432 Ops.push_back(Ptr);
1433 Ops.push_back(SV);
1434 Ops.push_back(getValueType(EVT));
1435 std::vector<MVT::ValueType> VTs;
1436 VTs.reserve(2);
1437 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1438 return getNode(Opcode, VTs, Ops);
1439}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001440
Chris Lattner0437cdd2005-05-09 04:14:13 +00001441SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001442 assert((!V || isa<PointerType>(V->getType())) &&
1443 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001444 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1445 if (N) return SDOperand(N, 0);
1446
1447 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001448 AllNodes.push_back(N);
1449 return SDOperand(N, 0);
1450}
1451
Nate Begemanacc398c2006-01-25 18:21:52 +00001452SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1453 SDOperand Chain, SDOperand Ptr,
1454 SDOperand SV) {
1455 std::vector<SDOperand> Ops;
1456 Ops.reserve(3);
1457 Ops.push_back(Chain);
1458 Ops.push_back(Ptr);
1459 Ops.push_back(SV);
1460 std::vector<MVT::ValueType> VTs;
1461 VTs.reserve(2);
1462 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1463 return getNode(ISD::VAARG, VTs, Ops);
1464}
1465
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001466SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001467 std::vector<SDOperand> &Ops) {
1468 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001469 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001470 case 1: return getNode(Opcode, VT, Ops[0]);
1471 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1472 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001473 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001474 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001475
Chris Lattner89c34632005-05-14 06:20:26 +00001476 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001477 switch (Opcode) {
1478 default: break;
1479 case ISD::BRCONDTWOWAY:
1480 if (N1C)
1481 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001482 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001483 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001484 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001485 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001486 case ISD::BRTWOWAY_CC:
1487 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1488 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1489 "LHS and RHS of comparison must have same type!");
1490 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001491 case ISD::TRUNCSTORE: {
1492 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1493 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1494#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1495 // If this is a truncating store of a constant, convert to the desired type
1496 // and store it instead.
1497 if (isa<Constant>(Ops[0])) {
1498 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1499 if (isa<Constant>(Op))
1500 N1 = Op;
1501 }
1502 // Also for ConstantFP?
1503#endif
1504 if (Ops[0].getValueType() == EVT) // Normal store?
1505 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1506 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1507 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1508 "Can't do FP-INT conversion!");
1509 break;
1510 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001511 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001512 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001513 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1514 "LHS and RHS of condition must have same type!");
1515 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1516 "True and False arms of SelectCC must have same type!");
1517 assert(Ops[2].getValueType() == VT &&
1518 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001519 break;
1520 }
1521 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001522 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001523 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1524 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001525 break;
1526 }
Chris Lattneref847df2005-04-09 03:27:28 +00001527 }
1528
Chris Lattner385328c2005-05-14 07:42:29 +00001529 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001530 SDNode *N;
1531 if (VT != MVT::Flag) {
1532 SDNode *&E =
1533 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1534 if (E) return SDOperand(E, 0);
1535 E = N = new SDNode(Opcode, Ops);
1536 } else {
1537 N = new SDNode(Opcode, Ops);
1538 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001539 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001540 AllNodes.push_back(N);
1541 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001542}
1543
Chris Lattner89c34632005-05-14 06:20:26 +00001544SDOperand SelectionDAG::getNode(unsigned Opcode,
1545 std::vector<MVT::ValueType> &ResultTys,
1546 std::vector<SDOperand> &Ops) {
1547 if (ResultTys.size() == 1)
1548 return getNode(Opcode, ResultTys[0], Ops);
1549
Chris Lattner5f056bf2005-07-10 01:55:33 +00001550 switch (Opcode) {
1551 case ISD::EXTLOAD:
1552 case ISD::SEXTLOAD:
1553 case ISD::ZEXTLOAD: {
1554 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1555 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1556 // If they are asking for an extending load from/to the same thing, return a
1557 // normal load.
1558 if (ResultTys[0] == EVT)
1559 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1560 assert(EVT < ResultTys[0] &&
1561 "Should only be an extending load, not truncating!");
1562 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1563 "Cannot sign/zero extend a FP load!");
1564 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1565 "Cannot convert from FP to Int or Int -> FP!");
1566 break;
1567 }
1568
Chris Lattnere89083a2005-05-14 07:25:05 +00001569 // FIXME: figure out how to safely handle things like
1570 // int foo(int x) { return 1 << (x & 255); }
1571 // int bar() { return foo(256); }
1572#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001573 case ISD::SRA_PARTS:
1574 case ISD::SRL_PARTS:
1575 case ISD::SHL_PARTS:
1576 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001577 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001578 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1579 else if (N3.getOpcode() == ISD::AND)
1580 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1581 // If the and is only masking out bits that cannot effect the shift,
1582 // eliminate the and.
1583 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1584 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1585 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1586 }
1587 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001588#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001589 }
Chris Lattner89c34632005-05-14 06:20:26 +00001590
Chris Lattner43247a12005-08-25 19:12:10 +00001591 // Memoize the node unless it returns a flag.
1592 SDNode *N;
1593 if (ResultTys.back() != MVT::Flag) {
1594 SDNode *&E =
1595 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1596 if (E) return SDOperand(E, 0);
1597 E = N = new SDNode(Opcode, Ops);
1598 } else {
1599 N = new SDNode(Opcode, Ops);
1600 }
Chris Lattnera3255112005-11-08 23:30:28 +00001601 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001602 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001603 return SDOperand(N, 0);
1604}
1605
Chris Lattnera3255112005-11-08 23:30:28 +00001606void SelectionDAG::setNodeValueTypes(SDNode *N,
1607 std::vector<MVT::ValueType> &RetVals) {
1608 switch (RetVals.size()) {
1609 case 0: return;
1610 case 1: N->setValueTypes(RetVals[0]); return;
1611 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1612 default: break;
1613 }
1614
1615 std::list<std::vector<MVT::ValueType> >::iterator I =
1616 std::find(VTList.begin(), VTList.end(), RetVals);
1617 if (I == VTList.end()) {
1618 VTList.push_front(RetVals);
1619 I = VTList.begin();
1620 }
1621
1622 N->setValueTypes(&(*I)[0], I->size());
1623}
1624
1625void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1626 MVT::ValueType VT2) {
1627 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1628 E = VTList.end(); I != E; ++I) {
1629 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1630 N->setValueTypes(&(*I)[0], 2);
1631 return;
1632 }
1633 }
1634 std::vector<MVT::ValueType> V;
1635 V.push_back(VT1);
1636 V.push_back(VT2);
1637 VTList.push_front(V);
1638 N->setValueTypes(&(*VTList.begin())[0], 2);
1639}
1640
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001641/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1642/// specified operands. If the resultant node already exists in the DAG,
1643/// this does not modify the specified node, instead it returns the node that
1644/// already exists. If the resultant node does not exist in the DAG, the
1645/// input node is returned. As a degenerate case, if you specify the same
1646/// input operands as the node already has, the input node is returned.
1647SDOperand SelectionDAG::
1648UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1649 SDNode *N = InN.Val;
1650 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1651
1652 // Check to see if there is no change.
1653 if (Op == N->getOperand(0)) return InN;
1654
1655 // See if the modified node already exists.
1656 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1657 if (NewSlot && *NewSlot)
1658 return SDOperand(*NewSlot, InN.ResNo);
1659
1660 // Nope it doesn't. Remove the node from it's current place in the maps.
1661 if (NewSlot)
1662 RemoveNodeFromCSEMaps(N);
1663
1664 // Now we update the operands.
1665 N->OperandList[0].Val->removeUser(N);
1666 Op.Val->addUser(N);
1667 N->OperandList[0] = Op;
1668
1669 // If this gets put into a CSE map, add it.
1670 if (NewSlot) *NewSlot = N;
1671 return InN;
1672}
1673
1674SDOperand SelectionDAG::
1675UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1676 SDNode *N = InN.Val;
1677 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1678
1679 // Check to see if there is no change.
1680 bool AnyChange = false;
1681 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1682 return InN; // No operands changed, just return the input node.
1683
1684 // See if the modified node already exists.
1685 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1686 if (NewSlot && *NewSlot)
1687 return SDOperand(*NewSlot, InN.ResNo);
1688
1689 // Nope it doesn't. Remove the node from it's current place in the maps.
1690 if (NewSlot)
1691 RemoveNodeFromCSEMaps(N);
1692
1693 // Now we update the operands.
1694 if (N->OperandList[0] != Op1) {
1695 N->OperandList[0].Val->removeUser(N);
1696 Op1.Val->addUser(N);
1697 N->OperandList[0] = Op1;
1698 }
1699 if (N->OperandList[1] != Op2) {
1700 N->OperandList[1].Val->removeUser(N);
1701 Op2.Val->addUser(N);
1702 N->OperandList[1] = Op2;
1703 }
1704
1705 // If this gets put into a CSE map, add it.
1706 if (NewSlot) *NewSlot = N;
1707 return InN;
1708}
1709
1710SDOperand SelectionDAG::
1711UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1712 std::vector<SDOperand> Ops;
1713 Ops.push_back(Op1);
1714 Ops.push_back(Op2);
1715 Ops.push_back(Op3);
1716 return UpdateNodeOperands(N, Ops);
1717}
1718
1719SDOperand SelectionDAG::
1720UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1721 SDOperand Op3, SDOperand Op4) {
1722 std::vector<SDOperand> Ops;
1723 Ops.push_back(Op1);
1724 Ops.push_back(Op2);
1725 Ops.push_back(Op3);
1726 Ops.push_back(Op4);
1727 return UpdateNodeOperands(N, Ops);
1728}
1729
1730SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001731UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1732 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1733 std::vector<SDOperand> Ops;
1734 Ops.push_back(Op1);
1735 Ops.push_back(Op2);
1736 Ops.push_back(Op3);
1737 Ops.push_back(Op4);
1738 Ops.push_back(Op5);
1739 return UpdateNodeOperands(N, Ops);
1740}
1741
1742
1743SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001744UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1745 SDNode *N = InN.Val;
1746 assert(N->getNumOperands() == Ops.size() &&
1747 "Update with wrong number of operands");
1748
1749 // Check to see if there is no change.
1750 unsigned NumOps = Ops.size();
1751 bool AnyChange = false;
1752 for (unsigned i = 0; i != NumOps; ++i) {
1753 if (Ops[i] != N->getOperand(i)) {
1754 AnyChange = true;
1755 break;
1756 }
1757 }
1758
1759 // No operands changed, just return the input node.
1760 if (!AnyChange) return InN;
1761
1762 // See if the modified node already exists.
1763 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1764 if (NewSlot && *NewSlot)
1765 return SDOperand(*NewSlot, InN.ResNo);
1766
1767 // Nope it doesn't. Remove the node from it's current place in the maps.
1768 if (NewSlot)
1769 RemoveNodeFromCSEMaps(N);
1770
1771 // Now we update the operands.
1772 for (unsigned i = 0; i != NumOps; ++i) {
1773 if (N->OperandList[i] != Ops[i]) {
1774 N->OperandList[i].Val->removeUser(N);
1775 Ops[i].Val->addUser(N);
1776 N->OperandList[i] = Ops[i];
1777 }
1778 }
1779
1780 // If this gets put into a CSE map, add it.
1781 if (NewSlot) *NewSlot = N;
1782 return InN;
1783}
1784
1785
1786
Chris Lattner149c58c2005-08-16 18:17:10 +00001787
1788/// SelectNodeTo - These are used for target selectors to *mutate* the
1789/// specified node to have the specified return type, Target opcode, and
1790/// operands. Note that target opcodes are stored as
1791/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001792///
1793/// Note that SelectNodeTo returns the resultant node. If there is already a
1794/// node of the specified opcode and operands, it returns that node instead of
1795/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001796SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1797 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001798 // If an identical node already exists, use it.
1799 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1800 if (ON) return SDOperand(ON, 0);
1801
Chris Lattner7651fa42005-08-24 23:00:29 +00001802 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001803
Chris Lattner7651fa42005-08-24 23:00:29 +00001804 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1805 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001806
1807 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001808 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001809}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001810
Chris Lattnereb19e402005-11-30 22:45:14 +00001811SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1812 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001813 // If an identical node already exists, use it.
1814 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1815 std::make_pair(Op1, VT))];
1816 if (ON) return SDOperand(ON, 0);
1817
Chris Lattner149c58c2005-08-16 18:17:10 +00001818 RemoveNodeFromCSEMaps(N);
1819 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1820 N->setValueTypes(VT);
1821 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001822
1823 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001824 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001825}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001826
Chris Lattnereb19e402005-11-30 22:45:14 +00001827SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1828 MVT::ValueType VT, SDOperand Op1,
1829 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001830 // If an identical node already exists, use it.
1831 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1832 std::make_pair(Op1, Op2))];
1833 if (ON) return SDOperand(ON, 0);
1834
Chris Lattner149c58c2005-08-16 18:17:10 +00001835 RemoveNodeFromCSEMaps(N);
1836 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1837 N->setValueTypes(VT);
1838 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001839
1840 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001841 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001842}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001843
Chris Lattnereb19e402005-11-30 22:45:14 +00001844SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1845 MVT::ValueType VT, SDOperand Op1,
1846 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001847 // If an identical node already exists, use it.
1848 std::vector<SDOperand> OpList;
1849 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1850 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1851 std::make_pair(VT, OpList))];
1852 if (ON) return SDOperand(ON, 0);
1853
Chris Lattner149c58c2005-08-16 18:17:10 +00001854 RemoveNodeFromCSEMaps(N);
1855 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1856 N->setValueTypes(VT);
1857 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001858
1859 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001860 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001861}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001862
Chris Lattnereb19e402005-11-30 22:45:14 +00001863SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1864 MVT::ValueType VT, SDOperand Op1,
1865 SDOperand Op2, SDOperand Op3,
1866 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001867 // If an identical node already exists, use it.
1868 std::vector<SDOperand> OpList;
1869 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1870 OpList.push_back(Op4);
1871 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1872 std::make_pair(VT, OpList))];
1873 if (ON) return SDOperand(ON, 0);
1874
Nate Begeman294a0a12005-08-18 07:30:15 +00001875 RemoveNodeFromCSEMaps(N);
1876 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1877 N->setValueTypes(VT);
1878 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001879
1880 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001881 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001882}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001883
Chris Lattnereb19e402005-11-30 22:45:14 +00001884SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1885 MVT::ValueType VT, SDOperand Op1,
1886 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1887 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001888 // If an identical node already exists, use it.
1889 std::vector<SDOperand> OpList;
1890 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1891 OpList.push_back(Op4); OpList.push_back(Op5);
1892 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1893 std::make_pair(VT, OpList))];
1894 if (ON) return SDOperand(ON, 0);
1895
Chris Lattner6b09a292005-08-21 18:49:33 +00001896 RemoveNodeFromCSEMaps(N);
1897 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1898 N->setValueTypes(VT);
1899 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001900
1901 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001902 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001903}
Chris Lattner149c58c2005-08-16 18:17:10 +00001904
Chris Lattnereb19e402005-11-30 22:45:14 +00001905SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1906 MVT::ValueType VT, SDOperand Op1,
1907 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1908 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001909 // If an identical node already exists, use it.
1910 std::vector<SDOperand> OpList;
1911 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1912 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1913 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1914 std::make_pair(VT, OpList))];
1915 if (ON) return SDOperand(ON, 0);
1916
Evan Cheng61ca74b2005-11-30 02:04:11 +00001917 RemoveNodeFromCSEMaps(N);
1918 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1919 N->setValueTypes(VT);
1920 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001921
1922 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001923 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00001924}
1925
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001926SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1927 MVT::ValueType VT, SDOperand Op1,
1928 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1929 SDOperand Op5, SDOperand Op6,
1930 SDOperand Op7) {
1931 // If an identical node already exists, use it.
1932 std::vector<SDOperand> OpList;
1933 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1934 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1935 OpList.push_back(Op7);
1936 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1937 std::make_pair(VT, OpList))];
1938 if (ON) return SDOperand(ON, 0);
1939
1940 RemoveNodeFromCSEMaps(N);
1941 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1942 N->setValueTypes(VT);
1943 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
1944
1945 ON = N; // Memoize the new node.
1946 return SDOperand(N, 0);
1947}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00001948SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1949 MVT::ValueType VT, SDOperand Op1,
1950 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1951 SDOperand Op5, SDOperand Op6,
1952 SDOperand Op7, SDOperand Op8) {
1953 // If an identical node already exists, use it.
1954 std::vector<SDOperand> OpList;
1955 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1956 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1957 OpList.push_back(Op7); OpList.push_back(Op8);
1958 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1959 std::make_pair(VT, OpList))];
1960 if (ON) return SDOperand(ON, 0);
1961
1962 RemoveNodeFromCSEMaps(N);
1963 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1964 N->setValueTypes(VT);
1965 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
1966
1967 ON = N; // Memoize the new node.
1968 return SDOperand(N, 0);
1969}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00001970
Chris Lattnereb19e402005-11-30 22:45:14 +00001971SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1972 MVT::ValueType VT1, MVT::ValueType VT2,
1973 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001974 // If an identical node already exists, use it.
1975 std::vector<SDOperand> OpList;
1976 OpList.push_back(Op1); OpList.push_back(Op2);
1977 std::vector<MVT::ValueType> VTList;
1978 VTList.push_back(VT1); VTList.push_back(VT2);
1979 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1980 std::make_pair(VTList, OpList))];
1981 if (ON) return SDOperand(ON, 0);
1982
Chris Lattner0fb094f2005-11-19 01:44:53 +00001983 RemoveNodeFromCSEMaps(N);
1984 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1985 setNodeValueTypes(N, VT1, VT2);
1986 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001987
1988 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001989 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00001990}
1991
Chris Lattnereb19e402005-11-30 22:45:14 +00001992SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1993 MVT::ValueType VT1, MVT::ValueType VT2,
1994 SDOperand Op1, SDOperand Op2,
1995 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001996 // If an identical node already exists, use it.
1997 std::vector<SDOperand> OpList;
1998 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1999 std::vector<MVT::ValueType> VTList;
2000 VTList.push_back(VT1); VTList.push_back(VT2);
2001 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2002 std::make_pair(VTList, OpList))];
2003 if (ON) return SDOperand(ON, 0);
2004
Chris Lattner0fb094f2005-11-19 01:44:53 +00002005 RemoveNodeFromCSEMaps(N);
2006 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2007 setNodeValueTypes(N, VT1, VT2);
2008 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002009
2010 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002011 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002012}
2013
Chris Lattnereb19e402005-11-30 22:45:14 +00002014SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2015 MVT::ValueType VT1, MVT::ValueType VT2,
2016 SDOperand Op1, SDOperand Op2,
2017 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002018 // If an identical node already exists, use it.
2019 std::vector<SDOperand> OpList;
2020 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2021 OpList.push_back(Op4);
2022 std::vector<MVT::ValueType> VTList;
2023 VTList.push_back(VT1); VTList.push_back(VT2);
2024 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2025 std::make_pair(VTList, OpList))];
2026 if (ON) return SDOperand(ON, 0);
2027
Chris Lattner0fb094f2005-11-19 01:44:53 +00002028 RemoveNodeFromCSEMaps(N);
2029 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2030 setNodeValueTypes(N, VT1, VT2);
2031 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002032
2033 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002034 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002035}
2036
Chris Lattnereb19e402005-11-30 22:45:14 +00002037SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2038 MVT::ValueType VT1, MVT::ValueType VT2,
2039 SDOperand Op1, SDOperand Op2,
2040 SDOperand Op3, SDOperand Op4,
2041 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002042 // If an identical node already exists, use it.
2043 std::vector<SDOperand> OpList;
2044 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2045 OpList.push_back(Op4); OpList.push_back(Op5);
2046 std::vector<MVT::ValueType> VTList;
2047 VTList.push_back(VT1); VTList.push_back(VT2);
2048 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2049 std::make_pair(VTList, OpList))];
2050 if (ON) return SDOperand(ON, 0);
2051
Chris Lattner0fb094f2005-11-19 01:44:53 +00002052 RemoveNodeFromCSEMaps(N);
2053 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2054 setNodeValueTypes(N, VT1, VT2);
2055 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002056
2057 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002058 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002059}
2060
Evan Cheng6ae46c42006-02-09 07:15:23 +00002061/// getTargetNode - These are used for target selectors to create a new node
2062/// with specified return type(s), target opcode, and operands.
2063///
2064/// Note that getTargetNode returns the resultant node. If there is already a
2065/// node of the specified opcode and operands, it returns that node instead of
2066/// the current one.
2067SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2068 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2069}
2070SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2071 SDOperand Op1) {
2072 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2073}
2074SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2075 SDOperand Op1, SDOperand Op2) {
2076 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2077}
2078SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2079 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2080 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2081}
2082SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2083 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2084 SDOperand Op4) {
2085 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2086}
2087SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2088 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2089 SDOperand Op4, SDOperand Op5) {
2090 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2091}
2092SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2093 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2094 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2095 std::vector<SDOperand> Ops;
2096 Ops.reserve(6);
2097 Ops.push_back(Op1);
2098 Ops.push_back(Op2);
2099 Ops.push_back(Op3);
2100 Ops.push_back(Op4);
2101 Ops.push_back(Op5);
2102 Ops.push_back(Op6);
2103 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2104}
2105SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2106 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2107 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2108 SDOperand Op7) {
2109 std::vector<SDOperand> Ops;
2110 Ops.reserve(7);
2111 Ops.push_back(Op1);
2112 Ops.push_back(Op2);
2113 Ops.push_back(Op3);
2114 Ops.push_back(Op4);
2115 Ops.push_back(Op5);
2116 Ops.push_back(Op6);
2117 Ops.push_back(Op7);
2118 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2119}
2120SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2121 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2122 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2123 SDOperand Op7, SDOperand Op8) {
2124 std::vector<SDOperand> Ops;
2125 Ops.reserve(8);
2126 Ops.push_back(Op1);
2127 Ops.push_back(Op2);
2128 Ops.push_back(Op3);
2129 Ops.push_back(Op4);
2130 Ops.push_back(Op5);
2131 Ops.push_back(Op6);
2132 Ops.push_back(Op7);
2133 Ops.push_back(Op8);
2134 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2135}
2136SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2137 std::vector<SDOperand> &Ops) {
2138 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2139}
2140SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2141 MVT::ValueType VT2, SDOperand Op1) {
2142 std::vector<MVT::ValueType> ResultTys;
2143 ResultTys.push_back(VT1);
2144 ResultTys.push_back(VT2);
2145 std::vector<SDOperand> Ops;
2146 Ops.push_back(Op1);
2147 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2148}
2149SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2150 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2151 std::vector<MVT::ValueType> ResultTys;
2152 ResultTys.push_back(VT1);
2153 ResultTys.push_back(VT2);
2154 std::vector<SDOperand> Ops;
2155 Ops.push_back(Op1);
2156 Ops.push_back(Op2);
2157 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2158}
2159SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2160 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2161 SDOperand Op3) {
2162 std::vector<MVT::ValueType> ResultTys;
2163 ResultTys.push_back(VT1);
2164 ResultTys.push_back(VT2);
2165 std::vector<SDOperand> Ops;
2166 Ops.push_back(Op1);
2167 Ops.push_back(Op2);
2168 Ops.push_back(Op3);
2169 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2170}
2171SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2172 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2173 SDOperand Op3, SDOperand Op4) {
2174 std::vector<MVT::ValueType> ResultTys;
2175 ResultTys.push_back(VT1);
2176 ResultTys.push_back(VT2);
2177 std::vector<SDOperand> Ops;
2178 Ops.push_back(Op1);
2179 Ops.push_back(Op2);
2180 Ops.push_back(Op3);
2181 Ops.push_back(Op4);
2182 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2183}
2184SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2185 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2186 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2187 std::vector<MVT::ValueType> ResultTys;
2188 ResultTys.push_back(VT1);
2189 ResultTys.push_back(VT2);
2190 std::vector<SDOperand> Ops;
2191 Ops.push_back(Op1);
2192 Ops.push_back(Op2);
2193 Ops.push_back(Op3);
2194 Ops.push_back(Op4);
2195 Ops.push_back(Op5);
2196 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2197}
2198SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2199 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2200 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2201 SDOperand Op6) {
2202 std::vector<MVT::ValueType> ResultTys;
2203 ResultTys.push_back(VT1);
2204 ResultTys.push_back(VT2);
2205 std::vector<SDOperand> Ops;
2206 Ops.push_back(Op1);
2207 Ops.push_back(Op2);
2208 Ops.push_back(Op3);
2209 Ops.push_back(Op4);
2210 Ops.push_back(Op5);
2211 Ops.push_back(Op6);
2212 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2213}
2214SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2215 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2216 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2217 SDOperand Op6, SDOperand Op7) {
2218 std::vector<MVT::ValueType> ResultTys;
2219 ResultTys.push_back(VT1);
2220 ResultTys.push_back(VT2);
2221 std::vector<SDOperand> Ops;
2222 Ops.push_back(Op1);
2223 Ops.push_back(Op2);
2224 Ops.push_back(Op3);
2225 Ops.push_back(Op4);
2226 Ops.push_back(Op5);
2227 Ops.push_back(Op6);
2228 Ops.push_back(Op7);
2229 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2230}
2231SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2232 MVT::ValueType VT2, MVT::ValueType VT3,
2233 SDOperand Op1, SDOperand Op2) {
2234 std::vector<MVT::ValueType> ResultTys;
2235 ResultTys.push_back(VT1);
2236 ResultTys.push_back(VT2);
2237 ResultTys.push_back(VT3);
2238 std::vector<SDOperand> Ops;
2239 Ops.push_back(Op1);
2240 Ops.push_back(Op2);
2241 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2242}
2243SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2244 MVT::ValueType VT2, MVT::ValueType VT3,
2245 SDOperand Op1, SDOperand Op2,
2246 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2247 std::vector<MVT::ValueType> ResultTys;
2248 ResultTys.push_back(VT1);
2249 ResultTys.push_back(VT2);
2250 ResultTys.push_back(VT3);
2251 std::vector<SDOperand> Ops;
2252 Ops.push_back(Op1);
2253 Ops.push_back(Op2);
2254 Ops.push_back(Op3);
2255 Ops.push_back(Op4);
2256 Ops.push_back(Op5);
2257 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2258}
2259SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2260 MVT::ValueType VT2, MVT::ValueType VT3,
2261 SDOperand Op1, SDOperand Op2,
2262 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2263 SDOperand Op6) {
2264 std::vector<MVT::ValueType> ResultTys;
2265 ResultTys.push_back(VT1);
2266 ResultTys.push_back(VT2);
2267 ResultTys.push_back(VT3);
2268 std::vector<SDOperand> Ops;
2269 Ops.push_back(Op1);
2270 Ops.push_back(Op2);
2271 Ops.push_back(Op3);
2272 Ops.push_back(Op4);
2273 Ops.push_back(Op5);
2274 Ops.push_back(Op6);
2275 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2276}
2277SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2278 MVT::ValueType VT2, MVT::ValueType VT3,
2279 SDOperand Op1, SDOperand Op2,
2280 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2281 SDOperand Op6, SDOperand Op7) {
2282 std::vector<MVT::ValueType> ResultTys;
2283 ResultTys.push_back(VT1);
2284 ResultTys.push_back(VT2);
2285 ResultTys.push_back(VT3);
2286 std::vector<SDOperand> Ops;
2287 Ops.push_back(Op1);
2288 Ops.push_back(Op2);
2289 Ops.push_back(Op3);
2290 Ops.push_back(Op4);
2291 Ops.push_back(Op5);
2292 Ops.push_back(Op6);
2293 Ops.push_back(Op7);
2294 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2295}
2296SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2297 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2298 std::vector<MVT::ValueType> ResultTys;
2299 ResultTys.push_back(VT1);
2300 ResultTys.push_back(VT2);
2301 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2302}
2303
Chris Lattner0fb094f2005-11-19 01:44:53 +00002304// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002305/// This can cause recursive merging of nodes in the DAG.
2306///
Chris Lattner8b52f212005-08-26 18:36:28 +00002307/// This version assumes From/To have a single result value.
2308///
Chris Lattner1e111c72005-09-07 05:37:01 +00002309void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2310 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002311 SDNode *From = FromN.Val, *To = ToN.Val;
2312 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2313 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002314 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002315
Chris Lattner8b8749f2005-08-17 19:00:20 +00002316 while (!From->use_empty()) {
2317 // Process users until they are all gone.
2318 SDNode *U = *From->use_begin();
2319
2320 // This node is about to morph, remove its old self from the CSE maps.
2321 RemoveNodeFromCSEMaps(U);
2322
Chris Lattner65113b22005-11-08 22:07:03 +00002323 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2324 I != E; ++I)
2325 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002326 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002327 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002328 To->addUser(U);
2329 }
2330
2331 // Now that we have modified U, add it back to the CSE maps. If it already
2332 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002333 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2334 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002335 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002336 if (Deleted) Deleted->push_back(U);
2337 DeleteNodeNotInCSEMaps(U);
2338 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002339 }
2340}
2341
2342/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2343/// This can cause recursive merging of nodes in the DAG.
2344///
2345/// This version assumes From/To have matching types and numbers of result
2346/// values.
2347///
Chris Lattner1e111c72005-09-07 05:37:01 +00002348void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2349 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002350 assert(From != To && "Cannot replace uses of with self");
2351 assert(From->getNumValues() == To->getNumValues() &&
2352 "Cannot use this version of ReplaceAllUsesWith!");
2353 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002354 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002355 return;
2356 }
2357
2358 while (!From->use_empty()) {
2359 // Process users until they are all gone.
2360 SDNode *U = *From->use_begin();
2361
2362 // This node is about to morph, remove its old self from the CSE maps.
2363 RemoveNodeFromCSEMaps(U);
2364
Chris Lattner65113b22005-11-08 22:07:03 +00002365 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2366 I != E; ++I)
2367 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002368 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002369 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002370 To->addUser(U);
2371 }
2372
2373 // Now that we have modified U, add it back to the CSE maps. If it already
2374 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002375 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2376 ReplaceAllUsesWith(U, Existing, Deleted);
2377 // U is now dead.
2378 if (Deleted) Deleted->push_back(U);
2379 DeleteNodeNotInCSEMaps(U);
2380 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002381 }
2382}
2383
Chris Lattner8b52f212005-08-26 18:36:28 +00002384/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2385/// This can cause recursive merging of nodes in the DAG.
2386///
2387/// This version can replace From with any result values. To must match the
2388/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002389void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002390 const std::vector<SDOperand> &To,
2391 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002392 assert(From->getNumValues() == To.size() &&
2393 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002394 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002395 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002396 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002397 return;
2398 }
2399
2400 while (!From->use_empty()) {
2401 // Process users until they are all gone.
2402 SDNode *U = *From->use_begin();
2403
2404 // This node is about to morph, remove its old self from the CSE maps.
2405 RemoveNodeFromCSEMaps(U);
2406
Chris Lattner65113b22005-11-08 22:07:03 +00002407 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2408 I != E; ++I)
2409 if (I->Val == From) {
2410 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002411 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002412 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002413 ToOp.Val->addUser(U);
2414 }
2415
2416 // Now that we have modified U, add it back to the CSE maps. If it already
2417 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002418 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2419 ReplaceAllUsesWith(U, Existing, Deleted);
2420 // U is now dead.
2421 if (Deleted) Deleted->push_back(U);
2422 DeleteNodeNotInCSEMaps(U);
2423 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002424 }
2425}
2426
Chris Lattner012f2412006-02-17 21:58:01 +00002427/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2428/// uses of other values produced by From.Val alone. The Deleted vector is
2429/// handled the same was as for ReplaceAllUsesWith.
2430void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2431 std::vector<SDNode*> &Deleted) {
2432 assert(From != To && "Cannot replace a value with itself");
2433 // Handle the simple, trivial, case efficiently.
2434 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2435 ReplaceAllUsesWith(From, To, &Deleted);
2436 return;
2437 }
2438
2439 // Get all of the users in a nice, deterministically ordered, uniqued set.
2440 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2441
2442 while (!Users.empty()) {
2443 // We know that this user uses some value of From. If it is the right
2444 // value, update it.
2445 SDNode *User = Users.back();
2446 Users.pop_back();
2447
2448 for (SDOperand *Op = User->OperandList,
2449 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2450 if (*Op == From) {
2451 // Okay, we know this user needs to be updated. Remove its old self
2452 // from the CSE maps.
2453 RemoveNodeFromCSEMaps(User);
2454
2455 // Update all operands that match "From".
2456 for (; Op != E; ++Op) {
2457 if (*Op == From) {
2458 From.Val->removeUser(User);
2459 *Op = To;
2460 To.Val->addUser(User);
2461 }
2462 }
2463
2464 // Now that we have modified User, add it back to the CSE maps. If it
2465 // already exists there, recursively merge the results together.
2466 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2467 unsigned NumDeleted = Deleted.size();
2468 ReplaceAllUsesWith(User, Existing, &Deleted);
2469
2470 // User is now dead.
2471 Deleted.push_back(User);
2472 DeleteNodeNotInCSEMaps(User);
2473
2474 // We have to be careful here, because ReplaceAllUsesWith could have
2475 // deleted a user of From, which means there may be dangling pointers
2476 // in the "Users" setvector. Scan over the deleted node pointers and
2477 // remove them from the setvector.
2478 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2479 Users.remove(Deleted[i]);
2480 }
2481 break; // Exit the operand scanning loop.
2482 }
2483 }
2484 }
2485}
2486
Chris Lattner7b2880c2005-08-24 22:44:39 +00002487
Jim Laskey58b968b2005-08-17 20:08:02 +00002488//===----------------------------------------------------------------------===//
2489// SDNode Class
2490//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002491
Chris Lattnera3255112005-11-08 23:30:28 +00002492
2493/// getValueTypeList - Return a pointer to the specified value type.
2494///
2495MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2496 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2497 VTs[VT] = VT;
2498 return &VTs[VT];
2499}
2500
Chris Lattner5c884562005-01-12 18:37:47 +00002501/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2502/// indicated value. This method ignores uses of other values defined by this
2503/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002504bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002505 assert(Value < getNumValues() && "Bad value!");
2506
2507 // If there is only one value, this is easy.
2508 if (getNumValues() == 1)
2509 return use_size() == NUses;
2510 if (Uses.size() < NUses) return false;
2511
Evan Cheng4ee62112006-02-05 06:29:23 +00002512 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002513
2514 std::set<SDNode*> UsersHandled;
2515
Evan Cheng4ee62112006-02-05 06:29:23 +00002516 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002517 UI != E; ++UI) {
2518 SDNode *User = *UI;
2519 if (User->getNumOperands() == 1 ||
2520 UsersHandled.insert(User).second) // First time we've seen this?
2521 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2522 if (User->getOperand(i) == TheValue) {
2523 if (NUses == 0)
2524 return false; // too many uses
2525 --NUses;
2526 }
2527 }
2528
2529 // Found exactly the right number of uses?
2530 return NUses == 0;
2531}
2532
2533
Evan Cheng4ee62112006-02-05 06:29:23 +00002534// isOnlyUse - Return true if this node is the only use of N.
2535bool SDNode::isOnlyUse(SDNode *N) const {
2536 bool Seen = false;
2537 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2538 SDNode *User = *I;
2539 if (User == this)
2540 Seen = true;
2541 else
2542 return false;
2543 }
2544
2545 return Seen;
2546}
2547
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002548// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002549bool SDOperand::isOperand(SDNode *N) const {
2550 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2551 if (*this == N->getOperand(i))
2552 return true;
2553 return false;
2554}
2555
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002556bool SDNode::isOperand(SDNode *N) const {
2557 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2558 if (this == N->OperandList[i].Val)
2559 return true;
2560 return false;
2561}
Evan Cheng4ee62112006-02-05 06:29:23 +00002562
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002563const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002564 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002565 default:
2566 if (getOpcode() < ISD::BUILTIN_OP_END)
2567 return "<<Unknown DAG Node>>";
2568 else {
Evan Cheng72261582005-12-20 06:22:03 +00002569 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002570 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002571 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2572 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002573
Evan Cheng72261582005-12-20 06:22:03 +00002574 TargetLowering &TLI = G->getTargetLoweringInfo();
2575 const char *Name =
2576 TLI.getTargetNodeName(getOpcode());
2577 if (Name) return Name;
2578 }
2579
2580 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002581 }
2582
Andrew Lenharth95762122005-03-31 21:24:06 +00002583 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002584 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002585 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002586 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002587 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002588 case ISD::AssertSext: return "AssertSext";
2589 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002590
2591 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002592 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002593 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002594 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002595
2596 case ISD::Constant: return "Constant";
2597 case ISD::ConstantFP: return "ConstantFP";
2598 case ISD::GlobalAddress: return "GlobalAddress";
2599 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002600 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002601 case ISD::ExternalSymbol: return "ExternalSymbol";
2602
2603 case ISD::ConstantVec: return "ConstantVec";
2604 case ISD::TargetConstant: return "TargetConstant";
2605 case ISD::TargetConstantFP:return "TargetConstantFP";
2606 case ISD::TargetConstantVec:return "TargetConstantVec";
2607 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2608 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002609 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002610 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
2611 case ISD::VConstant: return "VConstant";
2612
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002613 case ISD::CopyToReg: return "CopyToReg";
2614 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002615 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002616 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002617 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002618 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002619
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002620 // Unary operators
2621 case ISD::FABS: return "fabs";
2622 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002623 case ISD::FSQRT: return "fsqrt";
2624 case ISD::FSIN: return "fsin";
2625 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002626
2627 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002628 case ISD::ADD: return "add";
2629 case ISD::SUB: return "sub";
2630 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002631 case ISD::MULHU: return "mulhu";
2632 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002633 case ISD::SDIV: return "sdiv";
2634 case ISD::UDIV: return "udiv";
2635 case ISD::SREM: return "srem";
2636 case ISD::UREM: return "urem";
2637 case ISD::AND: return "and";
2638 case ISD::OR: return "or";
2639 case ISD::XOR: return "xor";
2640 case ISD::SHL: return "shl";
2641 case ISD::SRA: return "sra";
2642 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002643 case ISD::ROTL: return "rotl";
2644 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002645 case ISD::FADD: return "fadd";
2646 case ISD::FSUB: return "fsub";
2647 case ISD::FMUL: return "fmul";
2648 case ISD::FDIV: return "fdiv";
2649 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002650 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002651 case ISD::VADD: return "vadd";
2652 case ISD::VSUB: return "vsub";
2653 case ISD::VMUL: return "vmul";
Chris Lattner01b3d732005-09-28 22:28:18 +00002654
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002655 case ISD::SETCC: return "setcc";
2656 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002657 case ISD::SELECT_CC: return "select_cc";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002658 case ISD::ADDC: return "addc";
2659 case ISD::ADDE: return "adde";
2660 case ISD::SUBC: return "subc";
2661 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002662 case ISD::SHL_PARTS: return "shl_parts";
2663 case ISD::SRA_PARTS: return "sra_parts";
2664 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002665
Chris Lattner7f644642005-04-28 21:44:03 +00002666 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002667 case ISD::SIGN_EXTEND: return "sign_extend";
2668 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002669 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002670 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002671 case ISD::TRUNCATE: return "truncate";
2672 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002673 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002674 case ISD::FP_EXTEND: return "fp_extend";
2675
2676 case ISD::SINT_TO_FP: return "sint_to_fp";
2677 case ISD::UINT_TO_FP: return "uint_to_fp";
2678 case ISD::FP_TO_SINT: return "fp_to_sint";
2679 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002680 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002681
2682 // Control flow instructions
2683 case ISD::BR: return "br";
2684 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002685 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002686 case ISD::BR_CC: return "br_cc";
2687 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002688 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002689 case ISD::CALLSEQ_START: return "callseq_start";
2690 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002691
2692 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002693 case ISD::LOAD: return "load";
2694 case ISD::STORE: return "store";
2695 case ISD::VLOAD: return "vload";
2696 case ISD::EXTLOAD: return "extload";
2697 case ISD::SEXTLOAD: return "sextload";
2698 case ISD::ZEXTLOAD: return "zextload";
2699 case ISD::TRUNCSTORE: return "truncstore";
2700 case ISD::VAARG: return "vaarg";
2701 case ISD::VACOPY: return "vacopy";
2702 case ISD::VAEND: return "vaend";
2703 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002704 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002705 case ISD::EXTRACT_ELEMENT: return "extract_element";
2706 case ISD::BUILD_PAIR: return "build_pair";
2707 case ISD::STACKSAVE: return "stacksave";
2708 case ISD::STACKRESTORE: return "stackrestore";
2709
2710 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002711 case ISD::MEMSET: return "memset";
2712 case ISD::MEMCPY: return "memcpy";
2713 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002714
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002715 // Bit manipulation
2716 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002717 case ISD::CTPOP: return "ctpop";
2718 case ISD::CTTZ: return "cttz";
2719 case ISD::CTLZ: return "ctlz";
2720
Chris Lattner36ce6912005-11-29 06:21:05 +00002721 // Debug info
2722 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002723 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002724 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002725
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002726 case ISD::CONDCODE:
2727 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002728 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002729 case ISD::SETOEQ: return "setoeq";
2730 case ISD::SETOGT: return "setogt";
2731 case ISD::SETOGE: return "setoge";
2732 case ISD::SETOLT: return "setolt";
2733 case ISD::SETOLE: return "setole";
2734 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002735
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002736 case ISD::SETO: return "seto";
2737 case ISD::SETUO: return "setuo";
2738 case ISD::SETUEQ: return "setue";
2739 case ISD::SETUGT: return "setugt";
2740 case ISD::SETUGE: return "setuge";
2741 case ISD::SETULT: return "setult";
2742 case ISD::SETULE: return "setule";
2743 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002744
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002745 case ISD::SETEQ: return "seteq";
2746 case ISD::SETGT: return "setgt";
2747 case ISD::SETGE: return "setge";
2748 case ISD::SETLT: return "setlt";
2749 case ISD::SETLE: return "setle";
2750 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002751 }
2752 }
2753}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002754
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002755void SDNode::dump() const { dump(0); }
2756void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002757 std::cerr << (void*)this << ": ";
2758
2759 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2760 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002761 if (getValueType(i) == MVT::Other)
2762 std::cerr << "ch";
2763 else
2764 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002765 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002766 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002767
2768 std::cerr << " ";
2769 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2770 if (i) std::cerr << ", ";
2771 std::cerr << (void*)getOperand(i).Val;
2772 if (unsigned RN = getOperand(i).ResNo)
2773 std::cerr << ":" << RN;
2774 }
2775
2776 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2777 std::cerr << "<" << CSDN->getValue() << ">";
2778 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2779 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002780 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002781 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002782 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002783 std::cerr << "<";
2784 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002785 if (offset > 0)
2786 std::cerr << " + " << offset;
2787 else
2788 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002789 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002790 std::cerr << "<" << FIDN->getIndex() << ">";
2791 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002792 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002793 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002794 if (offset > 0)
2795 std::cerr << " + " << offset;
2796 else
2797 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002798 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002799 std::cerr << "<";
2800 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2801 if (LBB)
2802 std::cerr << LBB->getName() << " ";
2803 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002804 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002805 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002806 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2807 } else {
2808 std::cerr << " #" << R->getReg();
2809 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002810 } else if (const ExternalSymbolSDNode *ES =
2811 dyn_cast<ExternalSymbolSDNode>(this)) {
2812 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002813 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2814 if (M->getValue())
2815 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2816 else
2817 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002818 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2819 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002820 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002821}
2822
Chris Lattnerde202b32005-11-09 23:47:37 +00002823static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002824 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2825 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002826 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002827 else
2828 std::cerr << "\n" << std::string(indent+2, ' ')
2829 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002830
Chris Lattnerea946cd2005-01-09 20:38:33 +00002831
2832 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002833 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002834}
2835
Chris Lattnerc3aae252005-01-07 07:46:32 +00002836void SelectionDAG::dump() const {
2837 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002838 std::vector<const SDNode*> Nodes;
2839 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2840 I != E; ++I)
2841 Nodes.push_back(I);
2842
Chris Lattner49d24712005-01-09 20:26:36 +00002843 std::sort(Nodes.begin(), Nodes.end());
2844
2845 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002846 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002847 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002848 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002849
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002850 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002851
Chris Lattnerc3aae252005-01-07 07:46:32 +00002852 std::cerr << "\n\n";
2853}
2854
Evan Chengfae9f1c2006-02-09 22:11:03 +00002855/// InsertISelMapEntry - A helper function to insert a key / element pair
2856/// into a SDOperand to SDOperand map. This is added to avoid the map
2857/// insertion operator from being inlined.
2858void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2859 SDNode *Key, unsigned KeyResNo,
2860 SDNode *Element, unsigned ElementResNo) {
2861 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2862 SDOperand(Element, ElementResNo)));
2863}