blob: 6e171d1251ba2226ef1758dc51d4a261b2eeef7b [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner1e111c72005-09-07 05:37:01 +000030// Temporary boolean for testing the dag combiner
31namespace llvm {
32 extern bool CombinerEnabled;
33}
34
Chris Lattner5cdcc582005-01-09 20:52:51 +000035static bool isCommutativeBinOp(unsigned Opcode) {
36 switch (Opcode) {
37 case ISD::ADD:
38 case ISD::MUL:
Chris Lattner01b3d732005-09-28 22:28:18 +000039 case ISD::FADD:
40 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000041 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need commutative info for user ops!
45 }
46}
47
48static bool isAssociativeBinOp(unsigned Opcode) {
49 switch (Opcode) {
50 case ISD::ADD:
51 case ISD::MUL:
52 case ISD::AND:
53 case ISD::OR:
54 case ISD::XOR: return true;
55 default: return false; // FIXME: Need associative info for user ops!
56 }
57}
58
Chris Lattner5cdcc582005-01-09 20:52:51 +000059// isInvertibleForFree - Return true if there is no cost to emitting the logical
60// inverse of this node.
61static bool isInvertibleForFree(SDOperand N) {
62 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000063 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000064 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000065 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000066}
67
Jim Laskey58b968b2005-08-17 20:08:02 +000068//===----------------------------------------------------------------------===//
69// ConstantFPSDNode Class
70//===----------------------------------------------------------------------===//
71
72/// isExactlyValue - We don't rely on operator== working on double values, as
73/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
74/// As such, this method can be used to do an exact bit-for-bit comparison of
75/// two floating point values.
76bool ConstantFPSDNode::isExactlyValue(double V) const {
77 return DoubleToBits(V) == DoubleToBits(Value);
78}
79
80//===----------------------------------------------------------------------===//
81// ISD Class
82//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000083
Chris Lattnerc3aae252005-01-07 07:46:32 +000084/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
85/// when given the operation for (X op Y).
86ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
87 // To perform this operation, we just need to swap the L and G bits of the
88 // operation.
89 unsigned OldL = (Operation >> 2) & 1;
90 unsigned OldG = (Operation >> 1) & 1;
91 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
92 (OldL << 1) | // New G bit
93 (OldG << 2)); // New L bit.
94}
95
96/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
97/// 'op' is a valid SetCC operation.
98ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
99 unsigned Operation = Op;
100 if (isInteger)
101 Operation ^= 7; // Flip L, G, E bits, but not U.
102 else
103 Operation ^= 15; // Flip all of the condition bits.
104 if (Operation > ISD::SETTRUE2)
105 Operation &= ~8; // Don't let N and U bits get set.
106 return ISD::CondCode(Operation);
107}
108
109
110/// isSignedOp - For an integer comparison, return 1 if the comparison is a
111/// signed operation and 2 if the result is an unsigned comparison. Return zero
112/// if the operation does not depend on the sign of the input (setne and seteq).
113static int isSignedOp(ISD::CondCode Opcode) {
114 switch (Opcode) {
115 default: assert(0 && "Illegal integer setcc operation!");
116 case ISD::SETEQ:
117 case ISD::SETNE: return 0;
118 case ISD::SETLT:
119 case ISD::SETLE:
120 case ISD::SETGT:
121 case ISD::SETGE: return 1;
122 case ISD::SETULT:
123 case ISD::SETULE:
124 case ISD::SETUGT:
125 case ISD::SETUGE: return 2;
126 }
127}
128
129/// getSetCCOrOperation - Return the result of a logical OR between different
130/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
131/// returns SETCC_INVALID if it is not possible to represent the resultant
132/// comparison.
133ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
134 bool isInteger) {
135 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
136 // Cannot fold a signed integer setcc with an unsigned integer setcc.
137 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000138
Chris Lattnerc3aae252005-01-07 07:46:32 +0000139 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000140
Chris Lattnerc3aae252005-01-07 07:46:32 +0000141 // If the N and U bits get set then the resultant comparison DOES suddenly
142 // care about orderedness, and is true when ordered.
143 if (Op > ISD::SETTRUE2)
144 Op &= ~16; // Clear the N bit.
145 return ISD::CondCode(Op);
146}
147
148/// getSetCCAndOperation - Return the result of a logical AND between different
149/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
150/// function returns zero if it is not possible to represent the resultant
151/// comparison.
152ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
153 bool isInteger) {
154 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
155 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000156 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000157
158 // Combine all of the condition bits.
159 return ISD::CondCode(Op1 & Op2);
160}
161
Chris Lattnerb48da392005-01-23 04:39:44 +0000162const TargetMachine &SelectionDAG::getTarget() const {
163 return TLI.getTargetMachine();
164}
165
Jim Laskey58b968b2005-08-17 20:08:02 +0000166//===----------------------------------------------------------------------===//
167// SelectionDAG Class
168//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000169
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000170/// RemoveDeadNodes - This method deletes all unreachable nodes in the
171/// SelectionDAG, including nodes (like loads) that have uses of their token
172/// chain but no other uses and no side effect. If a node is passed in as an
173/// argument, it is used as the seed for node deletion.
174void SelectionDAG::RemoveDeadNodes(SDNode *N) {
175 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
176
177 // Create a dummy node (which is not added to allnodes), that adds a reference
178 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000179 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000180
Chris Lattner8b8749f2005-08-17 19:00:20 +0000181 // If we have a hint to start from, use it.
182 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000183
184 Restart:
185 unsigned NumNodes = AllNodeSet.size();
186 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
187 I != E; ++I) {
188 // Try to delete this node.
189 DeleteNodeIfDead(*I, &AllNodeSet);
190
191 // If we actually deleted any nodes, do not use invalid iterators in
192 // AllNodeSet.
193 if (AllNodeSet.size() != NumNodes)
194 goto Restart;
195 }
196
197 // Restore AllNodes.
198 if (AllNodes.size() != NumNodes)
199 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
200
201 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000202 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203}
204
Chris Lattner149c58c2005-08-16 18:17:10 +0000205
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000206void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
207 if (!N->use_empty())
208 return;
209
210 // Okay, we really are going to delete this node. First take this out of the
211 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000212 RemoveNodeFromCSEMaps(N);
213
214 // Next, brutally remove the operand list. This is safe to do, as there are
215 // no cycles in the graph.
216 while (!N->Operands.empty()) {
217 SDNode *O = N->Operands.back().Val;
218 N->Operands.pop_back();
219 O->removeUser(N);
220
221 // Now that we removed this operand, see if there are no uses of it left.
222 DeleteNodeIfDead(O, NodeSet);
223 }
224
225 // Remove the node from the nodes set and delete it.
226 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
227 AllNodeSet.erase(N);
228
229 // Now that the node is gone, check to see if any of the operands of this node
230 // are dead now.
231 delete N;
232}
233
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000234void SelectionDAG::DeleteNode(SDNode *N) {
235 assert(N->use_empty() && "Cannot delete a node that is not dead!");
236
237 // First take this out of the appropriate CSE map.
238 RemoveNodeFromCSEMaps(N);
239
Chris Lattner1e111c72005-09-07 05:37:01 +0000240 // Finally, remove uses due to operands of this node, remove from the
241 // AllNodes list, and delete the node.
242 DeleteNodeNotInCSEMaps(N);
243}
244
245void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
246
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000247 // Remove it from the AllNodes list.
248 for (std::vector<SDNode*>::iterator I = AllNodes.begin(); ; ++I) {
249 assert(I != AllNodes.end() && "Node not in AllNodes list??");
250 if (*I == N) {
251 // Erase from the vector, which is not ordered.
252 std::swap(*I, AllNodes.back());
253 AllNodes.pop_back();
254 break;
255 }
256 }
257
258 // Drop all of the operands and decrement used nodes use counts.
259 while (!N->Operands.empty()) {
260 SDNode *O = N->Operands.back().Val;
261 N->Operands.pop_back();
262 O->removeUser(N);
263 }
264
265 delete N;
266}
267
Chris Lattner149c58c2005-08-16 18:17:10 +0000268/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
269/// correspond to it. This is useful when we're about to delete or repurpose
270/// the node. We don't want future request for structurally identical nodes
271/// to return N anymore.
272void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000273 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000274 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000275 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000276 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000277 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
278 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000279 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000280 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000281 Erased = TargetConstants.erase(std::make_pair(
282 cast<ConstantSDNode>(N)->getValue(),
283 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000284 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000285 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000286 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000287 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000288 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000289 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000290 case ISD::CONDCODE:
291 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
292 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000293 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000294 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
295 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000296 case ISD::GlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000297 Erased = GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000298 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000299 case ISD::TargetGlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000300 Erased =TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000301 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000302 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000303 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000304 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000305 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000306 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000307 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000309 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000310 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000311 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000312 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000313 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000314 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000315 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000316 break;
317 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000318 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000319 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000320 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000321 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000322 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
323 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000324 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000325 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
326 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000327 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000328 case ISD::SRCVALUE: {
329 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000330 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000331 break;
332 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000333 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 Erased = Loads.erase(std::make_pair(N->getOperand(1),
335 std::make_pair(N->getOperand(0),
336 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000337 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000338 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000339 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000340 if (N->getNumOperands() == 0) {
341 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
342 N->getValueType(0)));
343 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000344 Erased =
345 UnaryOps.erase(std::make_pair(N->getOpcode(),
346 std::make_pair(N->getOperand(0),
347 N->getValueType(0))));
348 } else if (N->getNumOperands() == 2) {
349 Erased =
350 BinaryOps.erase(std::make_pair(N->getOpcode(),
351 std::make_pair(N->getOperand(0),
352 N->getOperand(1))));
353 } else {
354 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
355 Erased =
356 OneResultNodes.erase(std::make_pair(N->getOpcode(),
357 std::make_pair(N->getValueType(0),
358 Ops)));
359 }
Chris Lattner385328c2005-05-14 07:42:29 +0000360 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000361 // Remove the node from the ArbitraryNodes map.
362 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
363 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000364 Erased =
365 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
366 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000367 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000368 break;
369 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000370#ifndef NDEBUG
371 // Verify that the node was actually in one of the CSE maps, unless it has a
372 // flag result (which cannot be CSE'd) or is one of the special cases that are
373 // not subject to CSE.
374 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
375 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000376 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000377
378 N->dump();
379 assert(0 && "Node is not in map!");
380 }
381#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000382}
383
Chris Lattner8b8749f2005-08-17 19:00:20 +0000384/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
385/// has been taken out and modified in some way. If the specified node already
386/// exists in the CSE maps, do not modify the maps, but return the existing node
387/// instead. If it doesn't exist, add it and return null.
388///
389SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
390 assert(N->getNumOperands() && "This is a leaf node!");
391 if (N->getOpcode() == ISD::LOAD) {
392 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
393 std::make_pair(N->getOperand(0),
394 N->getValueType(0)))];
395 if (L) return L;
396 L = N;
Chris Lattner95038592005-10-05 06:35:28 +0000397 } else if (N->getOpcode() == ISD::HANDLENODE) {
398 return 0; // never add it.
Chris Lattner8b8749f2005-08-17 19:00:20 +0000399 } else if (N->getNumOperands() == 1) {
400 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
401 std::make_pair(N->getOperand(0),
402 N->getValueType(0)))];
403 if (U) return U;
404 U = N;
405 } else if (N->getNumOperands() == 2) {
406 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
407 std::make_pair(N->getOperand(0),
408 N->getOperand(1)))];
409 if (B) return B;
410 B = N;
411 } else if (N->getNumValues() == 1) {
412 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
413 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
414 std::make_pair(N->getValueType(0), Ops))];
415 if (ORN) return ORN;
416 ORN = N;
417 } else {
418 // Remove the node from the ArbitraryNodes map.
419 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
420 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
421 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
422 std::make_pair(RV, Ops))];
423 if (AN) return AN;
424 AN = N;
425 }
426 return 0;
427
428}
429
430
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000431
Chris Lattner78ec3112003-08-11 14:57:33 +0000432SelectionDAG::~SelectionDAG() {
433 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
434 delete AllNodes[i];
435}
436
Chris Lattner0f2287b2005-04-13 02:38:18 +0000437SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000438 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000439 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000440 return getNode(ISD::AND, Op.getValueType(), Op,
441 getConstant(Imm, Op.getValueType()));
442}
443
Chris Lattnerc3aae252005-01-07 07:46:32 +0000444SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
445 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
446 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000447 if (VT != MVT::i64)
448 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000449
Chris Lattnerc3aae252005-01-07 07:46:32 +0000450 SDNode *&N = Constants[std::make_pair(Val, VT)];
451 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000452 N = new ConstantSDNode(false, Val, VT);
453 AllNodes.push_back(N);
454 return SDOperand(N, 0);
455}
456
457SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
458 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
459 // Mask out any bits that are not valid for this constant.
460 if (VT != MVT::i64)
461 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
462
463 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
464 if (N) return SDOperand(N, 0);
465 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000466 AllNodes.push_back(N);
467 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000468}
469
Chris Lattnerc3aae252005-01-07 07:46:32 +0000470SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
471 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
472 if (VT == MVT::f32)
473 Val = (float)Val; // Mask out extra precision.
474
Chris Lattnerd8658612005-02-17 20:17:32 +0000475 // Do the map lookup using the actual bit pattern for the floating point
476 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
477 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000478 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000479 if (N) return SDOperand(N, 0);
480 N = new ConstantFPSDNode(Val, VT);
481 AllNodes.push_back(N);
482 return SDOperand(N, 0);
483}
484
485
486
487SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
488 MVT::ValueType VT) {
489 SDNode *&N = GlobalValues[GV];
490 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000491 N = new GlobalAddressSDNode(false, GV, VT);
492 AllNodes.push_back(N);
493 return SDOperand(N, 0);
494}
495
496SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
497 MVT::ValueType VT) {
498 SDNode *&N = TargetGlobalValues[GV];
499 if (N) return SDOperand(N, 0);
500 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000501 AllNodes.push_back(N);
502 return SDOperand(N, 0);
503}
504
505SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
506 SDNode *&N = FrameIndices[FI];
507 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000508 N = new FrameIndexSDNode(FI, VT, false);
509 AllNodes.push_back(N);
510 return SDOperand(N, 0);
511}
512
513SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
514 SDNode *&N = TargetFrameIndices[FI];
515 if (N) return SDOperand(N, 0);
516 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000517 AllNodes.push_back(N);
518 return SDOperand(N, 0);
519}
520
Chris Lattner5839bf22005-08-26 17:15:30 +0000521SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
522 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000523 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000524 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000525 AllNodes.push_back(N);
526 return SDOperand(N, 0);
527}
528
Chris Lattner5839bf22005-08-26 17:15:30 +0000529SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
530 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000531 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000532 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000533 AllNodes.push_back(N);
534 return SDOperand(N, 0);
535}
536
537SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
538 SDNode *&N = BBNodes[MBB];
539 if (N) return SDOperand(N, 0);
540 N = new BasicBlockSDNode(MBB);
541 AllNodes.push_back(N);
542 return SDOperand(N, 0);
543}
544
Chris Lattner15e4b012005-07-10 00:07:11 +0000545SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
546 if ((unsigned)VT >= ValueTypeNodes.size())
547 ValueTypeNodes.resize(VT+1);
548 if (ValueTypeNodes[VT] == 0) {
549 ValueTypeNodes[VT] = new VTSDNode(VT);
550 AllNodes.push_back(ValueTypeNodes[VT]);
551 }
552
553 return SDOperand(ValueTypeNodes[VT], 0);
554}
555
Chris Lattnerc3aae252005-01-07 07:46:32 +0000556SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
557 SDNode *&N = ExternalSymbols[Sym];
558 if (N) return SDOperand(N, 0);
559 N = new ExternalSymbolSDNode(Sym, VT);
560 AllNodes.push_back(N);
561 return SDOperand(N, 0);
562}
563
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000564SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
565 if ((unsigned)Cond >= CondCodeNodes.size())
566 CondCodeNodes.resize(Cond+1);
567
Chris Lattner079a27a2005-08-09 20:40:02 +0000568 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000569 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000570 AllNodes.push_back(CondCodeNodes[Cond]);
571 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000572 return SDOperand(CondCodeNodes[Cond], 0);
573}
574
Chris Lattner0fdd7682005-08-30 22:38:38 +0000575SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
576 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
577 if (!Reg) {
578 Reg = new RegisterSDNode(RegNo, VT);
579 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000580 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000581 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000582}
583
Chris Lattner5ae79112005-09-23 00:55:52 +0000584/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
585/// this predicate to simplify operations downstream. V and Mask are known to
586/// be the same type.
587static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
588 const TargetLowering &TLI) {
589 unsigned SrcBits;
590 if (Mask == 0) return true;
591
592 // If we know the result of a setcc has the top bits zero, use this info.
593 switch (Op.getOpcode()) {
Chris Lattner6c4dad02005-10-07 06:37:02 +0000594 case ISD::Constant:
595 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
596
597 case ISD::SETCC:
598 return ((Mask & 1) == 0) &&
599 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
600
601 case ISD::ZEXTLOAD:
602 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
603 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
604 case ISD::ZERO_EXTEND:
605 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
606 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
607 case ISD::AssertZext:
608 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
609 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
610 case ISD::AND:
611 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
612 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
613 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
614
615 // FALL THROUGH
616 case ISD::OR:
617 case ISD::XOR:
618 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
619 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
620 case ISD::SELECT:
621 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
622 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
623 case ISD::SELECT_CC:
624 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
625 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
626 case ISD::SRL:
627 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
628 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
629 uint64_t NewVal = Mask << ShAmt->getValue();
630 SrcBits = MVT::getSizeInBits(Op.getValueType());
631 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
632 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
633 }
634 return false;
635 case ISD::SHL:
636 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
637 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
638 uint64_t NewVal = Mask >> ShAmt->getValue();
639 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
640 }
641 return false;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000642 case ISD::SUB:
643 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
644 // We know that the top bits of C-X are clear if X contains less bits
645 // than C (i.e. no wrap-around can happen). For example, 20-X is
646 // positive if we can prove that X is >= 0 and < 16.
647 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
648 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
649 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
650 uint64_t MaskV = (1ULL << (63-NLZ))-1;
651 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
652 // High bits are clear this value is known to be >= C.
653 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
654 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
655 return true;
656 }
657 }
658 }
659 break;
Chris Lattner6c4dad02005-10-07 06:37:02 +0000660 case ISD::CTTZ:
661 case ISD::CTLZ:
662 case ISD::CTPOP:
663 // Bit counting instructions can not set the high bits of the result
664 // register. The max number of bits sets depends on the input.
665 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
666
667 // TODO we could handle some SRA cases here.
668 default: break;
Chris Lattner5ae79112005-09-23 00:55:52 +0000669 }
670
671 return false;
672}
673
674
675
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000676SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
677 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000678 // These setcc operations always fold.
679 switch (Cond) {
680 default: break;
681 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000682 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000683 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000684 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000685 }
686
Chris Lattner67255a12005-04-07 18:14:58 +0000687 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
688 uint64_t C2 = N2C->getValue();
689 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
690 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000691
Chris Lattnerc3aae252005-01-07 07:46:32 +0000692 // Sign extend the operands if required
693 if (ISD::isSignedIntSetCC(Cond)) {
694 C1 = N1C->getSignExtended();
695 C2 = N2C->getSignExtended();
696 }
697
698 switch (Cond) {
699 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000700 case ISD::SETEQ: return getConstant(C1 == C2, VT);
701 case ISD::SETNE: return getConstant(C1 != C2, VT);
702 case ISD::SETULT: return getConstant(C1 < C2, VT);
703 case ISD::SETUGT: return getConstant(C1 > C2, VT);
704 case ISD::SETULE: return getConstant(C1 <= C2, VT);
705 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
706 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
707 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
708 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
709 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000710 }
Chris Lattner24673922005-04-07 18:58:54 +0000711 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000712 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000713 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
714 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
715
716 // If the comparison constant has bits in the upper part, the
717 // zero-extended value could never match.
718 if (C2 & (~0ULL << InSize)) {
719 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
720 switch (Cond) {
721 case ISD::SETUGT:
722 case ISD::SETUGE:
723 case ISD::SETEQ: return getConstant(0, VT);
724 case ISD::SETULT:
725 case ISD::SETULE:
726 case ISD::SETNE: return getConstant(1, VT);
727 case ISD::SETGT:
728 case ISD::SETGE:
729 // True if the sign bit of C2 is set.
730 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
731 case ISD::SETLT:
732 case ISD::SETLE:
733 // True if the sign bit of C2 isn't set.
734 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
735 default:
736 break;
737 }
738 }
739
740 // Otherwise, we can perform the comparison with the low bits.
741 switch (Cond) {
742 case ISD::SETEQ:
743 case ISD::SETNE:
744 case ISD::SETUGT:
745 case ISD::SETUGE:
746 case ISD::SETULT:
747 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000748 return getSetCC(VT, N1.getOperand(0),
749 getConstant(C2, N1.getOperand(0).getValueType()),
750 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000751 default:
752 break; // todo, be more careful with signed comparisons
753 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000754 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
755 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
756 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
757 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
758 MVT::ValueType ExtDstTy = N1.getValueType();
759 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000760
Chris Lattner7b2880c2005-08-24 22:44:39 +0000761 // If the extended part has any inconsistent bits, it cannot ever
762 // compare equal. In other words, they have to be all ones or all
763 // zeros.
764 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000765 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000766 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
767 return getConstant(Cond == ISD::SETNE, VT);
768
769 // Otherwise, make this a use of a zext.
770 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000771 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000772 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000773 }
774
Chris Lattner67255a12005-04-07 18:14:58 +0000775 uint64_t MinVal, MaxVal;
776 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
777 if (ISD::isSignedIntSetCC(Cond)) {
778 MinVal = 1ULL << (OperandBitSize-1);
779 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
780 MaxVal = ~0ULL >> (65-OperandBitSize);
781 else
782 MaxVal = 0;
783 } else {
784 MinVal = 0;
785 MaxVal = ~0ULL >> (64-OperandBitSize);
786 }
787
788 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
789 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
790 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
791 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000792 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
793 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000794 }
795
796 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
797 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
798 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000799 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
800 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000801 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000802
Nate Begeman72ea2812005-04-14 08:56:52 +0000803 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
804 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000805
Nate Begeman72ea2812005-04-14 08:56:52 +0000806 // Canonicalize setgt X, Min --> setne X, Min
807 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000808 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000809
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000810 // If we have setult X, 1, turn it into seteq X, 0
811 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000812 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
813 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000814 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000815 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000816 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
817 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000818
819 // If we have "setcc X, C1", check to see if we can shrink the immediate
820 // by changing cc.
821
822 // SETUGT X, SINTMAX -> SETLT X, 0
823 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
824 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000825 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000826
827 // FIXME: Implement the rest of these.
828
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000829
830 // Fold bit comparisons when we can.
831 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
832 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
833 if (ConstantSDNode *AndRHS =
834 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
835 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
836 // Perform the xform if the AND RHS is a single bit.
837 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
838 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000839 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000840 TLI.getShiftAmountTy()));
841 }
842 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
843 // (X & 8) == 8 --> (X & 8) >> 3
844 // Perform the xform if C2 is a single bit.
845 if ((C2 & (C2-1)) == 0) {
846 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000847 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000848 }
849 }
850 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000851 }
Chris Lattner67255a12005-04-07 18:14:58 +0000852 } else if (isa<ConstantSDNode>(N1.Val)) {
853 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000854 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000855 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000856
857 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
858 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
859 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000860
Chris Lattnerc3aae252005-01-07 07:46:32 +0000861 switch (Cond) {
862 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000863 case ISD::SETEQ: return getConstant(C1 == C2, VT);
864 case ISD::SETNE: return getConstant(C1 != C2, VT);
865 case ISD::SETLT: return getConstant(C1 < C2, VT);
866 case ISD::SETGT: return getConstant(C1 > C2, VT);
867 case ISD::SETLE: return getConstant(C1 <= C2, VT);
868 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000869 }
870 } else {
871 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000872 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000873 }
874
Nate Begeman0558f612005-10-05 21:44:43 +0000875 if (!CombinerEnabled) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000876 if (N1 == N2) {
877 // We can always fold X == Y for integer setcc's.
878 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000879 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000880 unsigned UOF = ISD::getUnorderedFlavor(Cond);
881 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000882 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000883 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000884 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000885 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
886 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000887 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
888 if (NewCond != Cond)
889 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000890 }
891
Chris Lattner01b3d732005-09-28 22:28:18 +0000892 if (Cond == ISD::SETEQ || Cond == ISD::SETNE) {
Chris Lattner68dc3102005-01-10 02:03:02 +0000893 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
894 N1.getOpcode() == ISD::XOR) {
895 // Simplify (X+Y) == (X+Z) --> Y == Z
896 if (N1.getOpcode() == N2.getOpcode()) {
897 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000898 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000899 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000900 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000901 if (isCommutativeBinOp(N1.getOpcode())) {
902 // If X op Y == Y op X, try other combinations.
903 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000904 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000905 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000906 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000907 }
908 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000909
910 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000911
Chris Lattner5ae79112005-09-23 00:55:52 +0000912 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
913 if (N1.getOpcode() == ISD::XOR)
914 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
915 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N2)) {
916 // If we know that all of the inverted bits are zero, don't bother
917 // performing the inversion.
918 if (MaskedValueIsZero(N1.getOperand(0), ~XORC->getValue(), TLI))
919 return getSetCC(VT, N1.getOperand(0),
920 getConstant(XORC->getValue()^RHSC->getValue(),
921 N1.getValueType()), Cond);
922 }
923
Chris Lattner68dc3102005-01-10 02:03:02 +0000924 // Simplify (X+Z) == X --> Z == 0
925 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000926 return getSetCC(VT, N1.getOperand(1),
927 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000928 if (N1.getOperand(1) == N2) {
929 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000930 return getSetCC(VT, N1.getOperand(0),
931 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000932 else {
933 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
934 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000935 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000936 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000937 N2, getConstant(1, TLI.getShiftAmountTy())),
938 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000939 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000940 }
941 }
942
Chris Lattner68dc3102005-01-10 02:03:02 +0000943 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
944 N2.getOpcode() == ISD::XOR) {
945 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000946 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000947 return getSetCC(VT, N2.getOperand(1),
948 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000949 } else if (N2.getOperand(1) == N1) {
950 if (isCommutativeBinOp(N2.getOpcode())) {
951 return getSetCC(VT, N2.getOperand(0),
952 getConstant(0, N2.getValueType()), Cond);
953 } else {
954 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
955 // X == (Z-X) --> X<<1 == Z
956 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
957 getConstant(1, TLI.getShiftAmountTy())),
958 N2.getOperand(0), Cond);
959 }
960 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000961 }
962 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000963
Chris Lattnerfda2b552005-04-18 04:48:12 +0000964 // Fold away ALL boolean setcc's.
965 if (N1.getValueType() == MVT::i1) {
966 switch (Cond) {
967 default: assert(0 && "Unknown integer setcc!");
968 case ISD::SETEQ: // X == Y -> (X^Y)^1
969 N1 = getNode(ISD::XOR, MVT::i1,
970 getNode(ISD::XOR, MVT::i1, N1, N2),
971 getConstant(1, MVT::i1));
972 break;
973 case ISD::SETNE: // X != Y --> (X^Y)
974 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
975 break;
976 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
977 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
978 N1 = getNode(ISD::AND, MVT::i1, N2,
979 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
980 break;
981 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
982 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
983 N1 = getNode(ISD::AND, MVT::i1, N1,
984 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
985 break;
986 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
987 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
988 N1 = getNode(ISD::OR, MVT::i1, N2,
989 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
990 break;
991 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
992 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
993 N1 = getNode(ISD::OR, MVT::i1, N1,
994 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
995 break;
996 }
997 if (VT != MVT::i1)
998 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
999 return N1;
1000 }
Nate Begeman0558f612005-10-05 21:44:43 +00001001 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001002 // Could not fold it.
1003 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004}
1005
Nate Begemanff663682005-08-13 06:14:17 +00001006SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
1007 SDOperand N3, SDOperand N4,
1008 ISD::CondCode CC) {
1009 MVT::ValueType VT = N3.getValueType();
Nate Begeman1999b4b2005-08-25 20:04:38 +00001010 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman32c392a2005-08-13 06:00:21 +00001011 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1012 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1013 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
1014
1015 // Check to see if we can simplify the select into an fabs node
1016 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
1017 // Allow either -0.0 or 0.0
1018 if (CFP->getValue() == 0.0) {
1019 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1020 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1021 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
1022 N1 == N4.getOperand(0))
1023 return getNode(ISD::FABS, VT, N1);
1024
1025 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1026 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1027 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
1028 N3.getOperand(0) == N4)
1029 return getNode(ISD::FABS, VT, N4);
1030 }
1031 }
1032
Nate Begeman1999b4b2005-08-25 20:04:38 +00001033 // check to see if we're select_cc'ing a select_cc.
1034 // this allows us to turn:
1035 // select_cc set[eq,ne] (select_cc cc, lhs, rhs, 1, 0), 0, true, false ->
1036 // select_cc cc, lhs, rhs, true, false
1037 if ((N1C && N1C->isNullValue() && N2.getOpcode() == ISD::SELECT_CC) ||
1038 (N2C && N2C->isNullValue() && N1.getOpcode() == ISD::SELECT_CC) &&
1039 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1040 SDOperand SCC = N1C ? N2 : N1;
1041 ConstantSDNode *SCCT = dyn_cast<ConstantSDNode>(SCC.getOperand(2));
1042 ConstantSDNode *SCCF = dyn_cast<ConstantSDNode>(SCC.getOperand(3));
1043 if (SCCT && SCCF && SCCF->isNullValue() && SCCT->getValue() == 1ULL) {
1044 if (CC == ISD::SETEQ) std::swap(N3, N4);
1045 return getNode(ISD::SELECT_CC, N3.getValueType(), SCC.getOperand(0),
1046 SCC.getOperand(1), N3, N4, SCC.getOperand(4));
1047 }
1048 }
1049
Nate Begeman32c392a2005-08-13 06:00:21 +00001050 // Check to see if we can perform the "gzip trick", transforming
1051 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1052 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
1053 MVT::isInteger(N1.getValueType()) &&
1054 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
1055 MVT::ValueType XType = N1.getValueType();
1056 MVT::ValueType AType = N3.getValueType();
1057 if (XType >= AType) {
1058 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1059 // single-bit constant. FIXME: remove once the dag combiner
1060 // exists.
1061 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
1062 unsigned ShCtV = Log2_64(N3C->getValue());
1063 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1064 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
1065 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
1066 if (XType > AType)
1067 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1068 return getNode(ISD::AND, AType, Shift, N3);
1069 }
1070 SDOperand Shift = getNode(ISD::SRA, XType, N1,
1071 getConstant(MVT::getSizeInBits(XType)-1,
1072 TLI.getShiftAmountTy()));
1073 if (XType > AType)
1074 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1075 return getNode(ISD::AND, AType, Shift, N3);
1076 }
1077 }
1078
Nate Begeman1999b4b2005-08-25 20:04:38 +00001079 // Check to see if this is the equivalent of setcc
Nate Begemancebd4332005-08-24 04:57:57 +00001080 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +00001081 MVT::ValueType XType = N1.getValueType();
Chris Lattner3ec5d742005-09-09 23:00:07 +00001082 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1083 SDOperand Res = getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
1084 if (Res.getValueType() != VT)
1085 Res = getNode(ISD::ZERO_EXTEND, VT, Res);
1086 return Res;
1087 }
Chris Lattner9d338cf2005-08-25 17:54:58 +00001088
Nate Begemancebd4332005-08-24 04:57:57 +00001089 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1090 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
Chris Lattner9d338cf2005-08-25 17:54:58 +00001091 TLI.isOperationLegal(ISD::CTLZ, XType)) {
Nate Begeman7042f152005-08-23 05:41:12 +00001092 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
1093 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +00001094 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +00001095 TLI.getShiftAmountTy()));
1096 }
Nate Begemancebd4332005-08-24 04:57:57 +00001097 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1098 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
1099 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
1100 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
1101 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
1102 getConstant(MVT::getSizeInBits(XType)-1,
1103 TLI.getShiftAmountTy()));
1104 }
1105 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1106 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
1107 SDOperand Sign = getNode(ISD::SRL, XType, N1,
1108 getConstant(MVT::getSizeInBits(XType)-1,
1109 TLI.getShiftAmountTy()));
1110 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
1111 }
Nate Begeman7042f152005-08-23 05:41:12 +00001112 }
1113
Nate Begeman32c392a2005-08-13 06:00:21 +00001114 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1115 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1116 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1117 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
1118 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
1119 MVT::ValueType XType = N1.getValueType();
1120 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1121 SDOperand Shift = getNode(ISD::SRA, XType, N1,
1122 getConstant(MVT::getSizeInBits(XType)-1,
1123 TLI.getShiftAmountTy()));
1124 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
1125 Shift);
1126 }
1127 }
1128 }
1129
1130 // Could not fold it.
1131 return SDOperand();
1132}
1133
Chris Lattnerc3aae252005-01-07 07:46:32 +00001134/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001135///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001136SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001137 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1138 if (!N) {
1139 N = new SDNode(Opcode, VT);
1140 AllNodes.push_back(N);
1141 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001142 return SDOperand(N, 0);
1143}
1144
Chris Lattnerc3aae252005-01-07 07:46:32 +00001145SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1146 SDOperand Operand) {
1147 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1148 uint64_t Val = C->getValue();
1149 switch (Opcode) {
1150 default: break;
1151 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001152 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1154 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001155 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1156 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001157 }
1158 }
1159
1160 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1161 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001162 case ISD::FNEG:
1163 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001164 case ISD::FP_ROUND:
1165 case ISD::FP_EXTEND:
1166 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001167 case ISD::FP_TO_SINT:
1168 return getConstant((int64_t)C->getValue(), VT);
1169 case ISD::FP_TO_UINT:
1170 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001171 }
1172
1173 unsigned OpOpcode = Operand.Val->getOpcode();
1174 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001175 case ISD::TokenFactor:
1176 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001177 case ISD::SIGN_EXTEND:
1178 if (Operand.getValueType() == VT) return Operand; // noop extension
1179 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1180 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1181 break;
1182 case ISD::ZERO_EXTEND:
1183 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001184 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001185 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001186 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001187 case ISD::ANY_EXTEND:
1188 if (Operand.getValueType() == VT) return Operand; // noop extension
1189 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1190 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1191 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1192 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193 case ISD::TRUNCATE:
1194 if (Operand.getValueType() == VT) return Operand; // noop truncate
1195 if (OpOpcode == ISD::TRUNCATE)
1196 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001197 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1198 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001199 // If the source is smaller than the dest, we still need an extend.
1200 if (Operand.Val->getOperand(0).getValueType() < VT)
1201 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1202 else if (Operand.Val->getOperand(0).getValueType() > VT)
1203 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1204 else
1205 return Operand.Val->getOperand(0);
1206 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001208 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001209 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1210 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001211 Operand.Val->getOperand(0));
1212 if (OpOpcode == ISD::FNEG) // --X -> X
1213 return Operand.Val->getOperand(0);
1214 break;
1215 case ISD::FABS:
1216 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1217 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1218 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 }
1220
Chris Lattner43247a12005-08-25 19:12:10 +00001221 SDNode *N;
1222 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1223 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001224 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001225 E = N = new SDNode(Opcode, Operand);
1226 } else {
1227 N = new SDNode(Opcode, Operand);
1228 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001229 N->setValueTypes(VT);
1230 AllNodes.push_back(N);
1231 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001232}
1233
Chris Lattner36019aa2005-04-18 03:48:41 +00001234
1235
Chris Lattnerc3aae252005-01-07 07:46:32 +00001236SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1237 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001238#ifndef NDEBUG
1239 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001240 case ISD::TokenFactor:
1241 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1242 N2.getValueType() == MVT::Other && "Invalid token factor!");
1243 break;
Chris Lattner76365122005-01-16 02:23:22 +00001244 case ISD::AND:
1245 case ISD::OR:
1246 case ISD::XOR:
1247 case ISD::UDIV:
1248 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001249 case ISD::MULHU:
1250 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001251 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1252 // fall through
1253 case ISD::ADD:
1254 case ISD::SUB:
1255 case ISD::MUL:
1256 case ISD::SDIV:
1257 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001258 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1259 // fall through.
1260 case ISD::FADD:
1261 case ISD::FSUB:
1262 case ISD::FMUL:
1263 case ISD::FDIV:
1264 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001265 assert(N1.getValueType() == N2.getValueType() &&
1266 N1.getValueType() == VT && "Binary operator types must match!");
1267 break;
1268
1269 case ISD::SHL:
1270 case ISD::SRA:
1271 case ISD::SRL:
1272 assert(VT == N1.getValueType() &&
1273 "Shift operators return type must be the same as their first arg");
1274 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001275 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001276 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001277 case ISD::FP_ROUND_INREG: {
1278 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1279 assert(VT == N1.getValueType() && "Not an inreg round!");
1280 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1281 "Cannot FP_ROUND_INREG integer types");
1282 assert(EVT <= VT && "Not rounding down!");
1283 break;
1284 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001285 case ISD::AssertSext:
1286 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001287 case ISD::SIGN_EXTEND_INREG: {
1288 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1289 assert(VT == N1.getValueType() && "Not an inreg extend!");
1290 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1291 "Cannot *_EXTEND_INREG FP types");
1292 assert(EVT <= VT && "Not extending!");
1293 }
1294
Chris Lattner76365122005-01-16 02:23:22 +00001295 default: break;
1296 }
1297#endif
1298
Chris Lattnerc3aae252005-01-07 07:46:32 +00001299 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1300 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1301 if (N1C) {
1302 if (N2C) {
1303 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1304 switch (Opcode) {
1305 case ISD::ADD: return getConstant(C1 + C2, VT);
1306 case ISD::SUB: return getConstant(C1 - C2, VT);
1307 case ISD::MUL: return getConstant(C1 * C2, VT);
1308 case ISD::UDIV:
1309 if (C2) return getConstant(C1 / C2, VT);
1310 break;
1311 case ISD::UREM :
1312 if (C2) return getConstant(C1 % C2, VT);
1313 break;
1314 case ISD::SDIV :
1315 if (C2) return getConstant(N1C->getSignExtended() /
1316 N2C->getSignExtended(), VT);
1317 break;
1318 case ISD::SREM :
1319 if (C2) return getConstant(N1C->getSignExtended() %
1320 N2C->getSignExtended(), VT);
1321 break;
1322 case ISD::AND : return getConstant(C1 & C2, VT);
1323 case ISD::OR : return getConstant(C1 | C2, VT);
1324 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001325 case ISD::SHL : return getConstant(C1 << C2, VT);
1326 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001327 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 default: break;
1329 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001330 } else { // Cannonicalize constant to RHS if commutative
1331 if (isCommutativeBinOp(Opcode)) {
1332 std::swap(N1C, N2C);
1333 std::swap(N1, N2);
1334 }
1335 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001336
Chris Lattner1e111c72005-09-07 05:37:01 +00001337 if (!CombinerEnabled) {
Chris Lattner88218ef2005-01-19 17:29:49 +00001338 switch (Opcode) {
1339 default: break;
1340 case ISD::SHL: // shl 0, X -> 0
1341 if (N1C->isNullValue()) return N1;
1342 break;
1343 case ISD::SRL: // srl 0, X -> 0
1344 if (N1C->isNullValue()) return N1;
1345 break;
1346 case ISD::SRA: // sra -1, X -> -1
1347 if (N1C->isAllOnesValue()) return N1;
1348 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001349 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1350 // Extending a constant? Just return the extended constant.
1351 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1352 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001353 }
Chris Lattner1e111c72005-09-07 05:37:01 +00001354 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001355 }
1356
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001357 if (!CombinerEnabled) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001358 if (N2C) {
1359 uint64_t C2 = N2C->getValue();
1360
1361 switch (Opcode) {
1362 case ISD::ADD:
1363 if (!C2) return N1; // add X, 0 -> X
1364 break;
1365 case ISD::SUB:
1366 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001367 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001368 case ISD::MUL:
1369 if (!C2) return N2; // mul X, 0 -> 0
1370 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1371 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1372
Chris Lattnerb48da392005-01-23 04:39:44 +00001373 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001374 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001375 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001376 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 }
1378 break;
1379
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001380 case ISD::MULHU:
1381 case ISD::MULHS:
1382 if (!C2) return N2; // mul X, 0 -> 0
1383
1384 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1385 return getConstant(0, VT);
1386
1387 // Many others could be handled here, including -1, powers of 2, etc.
1388 break;
1389
Chris Lattnerc3aae252005-01-07 07:46:32 +00001390 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001391 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001392 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001393 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001394 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001395 }
1396 break;
1397
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001398 case ISD::SHL:
1399 case ISD::SRL:
1400 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001401 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001402 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001403 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1404 return getNode(ISD::UNDEF, N1.getValueType());
1405 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001406 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001407
1408 if (Opcode == ISD::SRA) {
1409 // If the sign bit is known to be zero, switch this to a SRL.
1410 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001411 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001412 TLI))
1413 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1414 } else {
1415 // If the part left over is known to be zero, the whole thing is zero.
1416 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1417 if (Opcode == ISD::SRL) {
1418 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1419 return getConstant(0, N1.getValueType());
1420 } else if (Opcode == ISD::SHL) {
1421 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1422 return getConstant(0, N1.getValueType());
1423 }
1424 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001425
1426 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1427 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1428 unsigned OpSAC = OpSA->getValue();
1429 if (N1.getOpcode() == ISD::SHL) {
1430 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1431 return getConstant(0, N1.getValueType());
1432 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1433 getConstant(C2+OpSAC, N2.getValueType()));
1434 } else if (N1.getOpcode() == ISD::SRL) {
1435 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1436 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1437 getConstant(~0ULL << OpSAC, VT));
1438 if (C2 > OpSAC) {
1439 return getNode(ISD::SHL, VT, Mask,
1440 getConstant(C2-OpSAC, N2.getValueType()));
1441 } else {
1442 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1443 return getNode(ISD::SRL, VT, Mask,
1444 getConstant(OpSAC-C2, N2.getValueType()));
1445 }
1446 } else if (N1.getOpcode() == ISD::SRA) {
1447 // if C1 == C2, just mask out low bits.
1448 if (C2 == OpSAC)
1449 return getNode(ISD::AND, VT, N1.getOperand(0),
1450 getConstant(~0ULL << C2, VT));
1451 }
1452 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001453 break;
1454
Chris Lattnerc3aae252005-01-07 07:46:32 +00001455 case ISD::AND:
1456 if (!C2) return N2; // X and 0 -> 0
1457 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001458 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001459
Chris Lattner36019aa2005-04-18 03:48:41 +00001460 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1461 return getConstant(0, VT);
1462
Chris Lattner588bbbf2005-04-21 06:28:15 +00001463 {
1464 uint64_t NotC2 = ~C2;
1465 if (VT != MVT::i64)
1466 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1467
1468 if (MaskedValueIsZero(N1, NotC2, TLI))
1469 return N1; // if (X & ~C2) -> 0, the and is redundant
1470 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001471
Chris Lattnerdea29e22005-04-10 01:13:15 +00001472 // FIXME: Should add a corresponding version of this for
1473 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1474 // we don't have yet.
Chris Lattner4ed11b42005-09-02 00:17:32 +00001475 // FIXME: NOW WE DO, add this.
Chris Lattnerdea29e22005-04-10 01:13:15 +00001476
Chris Lattner0f2287b2005-04-13 02:38:18 +00001477 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1478 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001479 // If we are masking out the part of our input that was extended, just
1480 // mask the input to the extension directly.
1481 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001482 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001483 if ((C2 & (~0ULL << ExtendBits)) == 0)
1484 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001485 } else if (N1.getOpcode() == ISD::OR) {
1486 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1487 if ((ORI->getValue() & C2) == C2) {
1488 // If the 'or' is setting all of the bits that we are masking for,
1489 // we know the result of the AND will be the AND mask itself.
1490 return N2;
1491 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001492 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001493 break;
1494 case ISD::OR:
1495 if (!C2)return N1; // X or 0 -> X
1496 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001497 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001498 break;
1499 case ISD::XOR:
1500 if (!C2) return N1; // X xor 0 -> X
Nate Begeman39f60a22005-09-01 23:25:49 +00001501 if (N2C->getValue() == 1 && N1.Val->getOpcode() == ISD::SETCC) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001502 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001503 // !(X op Y) -> (X !op Y)
1504 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001505 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1506 return getSetCC(SetCC->getValueType(0),
1507 SetCC->getOperand(0), SetCC->getOperand(1),
1508 ISD::getSetCCInverse(CC, isInteger));
Nate Begeman39f60a22005-09-01 23:25:49 +00001509 } else if (N2C->isAllOnesValue()) {
1510 if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001511 SDNode *Op = N1.Val;
1512 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1513 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1514 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1515 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1516 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1517 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1518 if (Op->getOpcode() == ISD::AND)
1519 return getNode(ISD::OR, VT, LHS, RHS);
1520 return getNode(ISD::AND, VT, LHS, RHS);
1521 }
1522 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001523 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001524 }
1525 break;
1526 }
1527
1528 // Reassociate ((X op C1) op C2) if possible.
1529 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1530 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001531 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001532 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001533 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001534 }
1535
1536 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1537 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001538 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001539 if (N2CFP) {
1540 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1541 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001542 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1543 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1544 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1545 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001546 if (C2) return getConstantFP(C1 / C2, VT);
1547 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001548 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001549 if (C2) return getConstantFP(fmod(C1, C2), VT);
1550 break;
1551 default: break;
1552 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001553 } else { // Cannonicalize constant to RHS if commutative
1554 if (isCommutativeBinOp(Opcode)) {
1555 std::swap(N1CFP, N2CFP);
1556 std::swap(N1, N2);
1557 }
1558 }
1559
Nate Begeman99801192005-09-07 23:25:52 +00001560 if (!CombinerEnabled) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001561 if (Opcode == ISD::FP_ROUND_INREG)
1562 return getNode(ISD::FP_EXTEND, VT,
1563 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
Nate Begeman99801192005-09-07 23:25:52 +00001564 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001565 }
1566
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 // Finally, fold operations that do not require constants.
1568 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001569 case ISD::TokenFactor:
Chris Lattner1e111c72005-09-07 05:37:01 +00001570 if (!CombinerEnabled) {
Chris Lattner39908e02005-01-19 18:01:40 +00001571 if (N1.getOpcode() == ISD::EntryToken)
1572 return N2;
1573 if (N2.getOpcode() == ISD::EntryToken)
1574 return N1;
Chris Lattner1e111c72005-09-07 05:37:01 +00001575 }
Chris Lattner39908e02005-01-19 18:01:40 +00001576 break;
Chris Lattner094c8fc2005-10-07 06:10:46 +00001577 case ISD::SDIV: {
1578 if (CombinerEnabled) break;
1579
1580 // If we know the sign bits of both operands are zero, strength reduce to a
1581 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
1582 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
1583 if (MaskedValueIsZero(N2, SignBit, TLI) &&
Chris Lattnerc4ced262005-10-07 15:30:32 +00001584 MaskedValueIsZero(N1, SignBit, TLI)) {
1585 std::cerr << "SDIV [[";
1586 N1.Val->dump(); std::cerr << "]] [[";
1587 N2.Val->dump(); std::cerr << "]] -> udiv\n";
Chris Lattner094c8fc2005-10-07 06:10:46 +00001588 return getNode(ISD::UDIV, VT, N1, N2);
Chris Lattnerc4ced262005-10-07 15:30:32 +00001589 }
Chris Lattner094c8fc2005-10-07 06:10:46 +00001590 break;
1591 }
Chris Lattner39908e02005-01-19 18:01:40 +00001592
Chris Lattnerc3aae252005-01-07 07:46:32 +00001593 case ISD::AND:
1594 case ISD::OR:
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001595 if (!CombinerEnabled) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001596 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1597 SDNode *LHS = N1.Val, *RHS = N2.Val;
1598 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1599 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1600 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1601 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001602
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001603 if (LR == RR && isa<ConstantSDNode>(LR) &&
1604 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1605 // (X != 0) | (Y != 0) -> (X|Y != 0)
1606 // (X == 0) & (Y == 0) -> (X|Y == 0)
1607 // (X < 0) | (Y < 0) -> (X|Y < 0)
1608 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1609 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1610 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1611 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1612 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1613 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001614
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001615 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1616 // (X == -1) & (Y == -1) -> (X&Y == -1)
1617 // (X != -1) | (Y != -1) -> (X&Y != -1)
1618 // (X > -1) | (Y > -1) -> (X&Y > -1)
1619 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1620 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1621 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1622 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1623 LR, Op2);
1624 // (X > -1) & (Y > -1) -> (X|Y > -1)
1625 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1626 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1627 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001628 }
1629 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001630
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001631 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1632 if (LL == RR && LR == RL) {
1633 Op2 = ISD::getSetCCSwappedOperands(Op2);
1634 goto MatchedBackwards;
1635 }
1636
1637 if (LL == RL && LR == RR) {
1638 MatchedBackwards:
1639 ISD::CondCode Result;
1640 bool isInteger = MVT::isInteger(LL.getValueType());
1641 if (Opcode == ISD::OR)
1642 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1643 else
1644 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1645
1646 if (Result != ISD::SETCC_INVALID)
1647 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1648 }
1649 }
1650
Chris Lattner7467c9b2005-04-18 04:11:19 +00001651 // and/or zext(a), zext(b) -> zext(and/or a, b)
1652 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1653 N2.getOpcode() == ISD::ZERO_EXTEND &&
1654 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1655 return getNode(ISD::ZERO_EXTEND, VT,
1656 getNode(Opcode, N1.getOperand(0).getValueType(),
1657 N1.getOperand(0), N2.getOperand(0)));
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001658 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001659 break;
1660 case ISD::XOR:
Nate Begeman223df222005-09-08 20:18:10 +00001661 if (!CombinerEnabled) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001662 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
Nate Begeman223df222005-09-08 20:18:10 +00001663 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001664 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001665 case ISD::ADD:
Nate Begeman223df222005-09-08 20:18:10 +00001666 if (!CombinerEnabled) {
Chris Lattneredeecfc2005-04-10 04:04:49 +00001667 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1668 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1669 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1670 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1671 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1672 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Chris Lattner01b3d732005-09-28 22:28:18 +00001673 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1))
Nate Begeman41aaf702005-06-16 07:06:03 +00001674 return N2.Val->getOperand(0); // A+(B-A) -> B
Nate Begeman223df222005-09-08 20:18:10 +00001675 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001676 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001677 case ISD::FADD:
1678 if (!CombinerEnabled) {
1679 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1680 return getNode(ISD::FSUB, VT, N1, N2.getOperand(0));
1681 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1682 return getNode(ISD::FSUB, VT, N2, N1.getOperand(0));
1683 }
1684 break;
1685
Chris Lattnerabd21822005-01-09 20:09:57 +00001686 case ISD::SUB:
Nate Begeman223df222005-09-08 20:18:10 +00001687 if (!CombinerEnabled) {
Chris Lattnerabd21822005-01-09 20:09:57 +00001688 if (N1.getOpcode() == ISD::ADD) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001689 if (N1.Val->getOperand(0) == N2)
Chris Lattnerabd21822005-01-09 20:09:57 +00001690 return N1.Val->getOperand(1); // (A+B)-A == B
Chris Lattner01b3d732005-09-28 22:28:18 +00001691 if (N1.Val->getOperand(1) == N2)
Chris Lattnerabd21822005-01-09 20:09:57 +00001692 return N1.Val->getOperand(0); // (A+B)-B == A
1693 }
Chris Lattner01b3d732005-09-28 22:28:18 +00001694 }
1695 break;
1696 case ISD::FSUB:
1697 if (!CombinerEnabled) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001698 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
Chris Lattner01b3d732005-09-28 22:28:18 +00001699 return getNode(ISD::FADD, VT, N1, N2.getOperand(0));
Nate Begeman223df222005-09-08 20:18:10 +00001700 }
Chris Lattnerabd21822005-01-09 20:09:57 +00001701 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001702 case ISD::FP_ROUND_INREG:
1703 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1704 break;
1705 case ISD::SIGN_EXTEND_INREG: {
1706 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1707 if (EVT == VT) return N1; // Not actually extending
Nate Begeman223df222005-09-08 20:18:10 +00001708 if (!CombinerEnabled) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001709 // If we are sign extending an extension, use the original source.
Nate Begeman56eb8682005-08-30 02:44:00 +00001710 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG ||
1711 N1.getOpcode() == ISD::AssertSext)
Chris Lattner15e4b012005-07-10 00:07:11 +00001712 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1713 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001714
Chris Lattner15e4b012005-07-10 00:07:11 +00001715 // If we are sign extending a sextload, return just the load.
1716 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001717 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Nate Begeman56eb8682005-08-30 02:44:00 +00001718 return N1;
Chris Lattner15e4b012005-07-10 00:07:11 +00001719
1720 // If we are extending the result of a setcc, and we already know the
1721 // contents of the top bits, eliminate the extension.
1722 if (N1.getOpcode() == ISD::SETCC &&
1723 TLI.getSetCCResultContents() ==
1724 TargetLowering::ZeroOrNegativeOneSetCCResult)
1725 return N1;
Nate Begeman56eb8682005-08-30 02:44:00 +00001726
Chris Lattner15e4b012005-07-10 00:07:11 +00001727 // If we are sign extending the result of an (and X, C) operation, and we
1728 // know the extended bits are zeros already, don't do the extend.
1729 if (N1.getOpcode() == ISD::AND)
1730 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1731 uint64_t Mask = N1C->getValue();
1732 unsigned NumBits = MVT::getSizeInBits(EVT);
1733 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1734 return N1;
1735 }
Nate Begeman223df222005-09-08 20:18:10 +00001736 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001737 break;
1738 }
1739
Nate Begemaneea805e2005-04-13 21:23:31 +00001740 // FIXME: figure out how to safely handle things like
1741 // int foo(int x) { return 1 << (x & 255); }
1742 // int bar() { return foo(256); }
1743#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001744 case ISD::SHL:
1745 case ISD::SRL:
1746 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001747 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001748 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001749 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001750 else if (N2.getOpcode() == ISD::AND)
1751 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1752 // If the and is only masking out bits that cannot effect the shift,
1753 // eliminate the and.
1754 unsigned NumBits = MVT::getSizeInBits(VT);
1755 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1756 return getNode(Opcode, VT, N1, N2.getOperand(0));
1757 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001758 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001759#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001760 }
1761
Chris Lattner27e9b412005-05-11 18:57:39 +00001762 // Memoize this node if possible.
1763 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001764 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1765 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001766 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1767 if (BON) return SDOperand(BON, 0);
1768
1769 BON = N = new SDNode(Opcode, N1, N2);
1770 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001771 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001772 }
1773
Chris Lattner3e011362005-05-14 07:45:46 +00001774 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001775 AllNodes.push_back(N);
1776 return SDOperand(N, 0);
1777}
1778
Chris Lattner88de6e72005-05-12 00:17:04 +00001779// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001780// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001781void SDNode::setAdjCallChain(SDOperand N) {
1782 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001783 assert((getOpcode() == ISD::CALLSEQ_START ||
1784 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001785
1786 Operands[0].Val->removeUser(this);
1787 Operands[0] = N;
1788 N.Val->Uses.push_back(this);
1789}
1790
1791
1792
Chris Lattnerc3aae252005-01-07 07:46:32 +00001793SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001794 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001795 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001796 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1797 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001798 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001799
1800 // Loads have a token chain.
1801 N->setValueTypes(VT, MVT::Other);
1802 AllNodes.push_back(N);
1803 return SDOperand(N, 0);
1804}
1805
Chris Lattner5f056bf2005-07-10 01:55:33 +00001806
1807SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1808 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1809 MVT::ValueType EVT) {
1810 std::vector<SDOperand> Ops;
1811 Ops.reserve(4);
1812 Ops.push_back(Chain);
1813 Ops.push_back(Ptr);
1814 Ops.push_back(SV);
1815 Ops.push_back(getValueType(EVT));
1816 std::vector<MVT::ValueType> VTs;
1817 VTs.reserve(2);
1818 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1819 return getNode(Opcode, VTs, Ops);
1820}
1821
Chris Lattnerc3aae252005-01-07 07:46:32 +00001822SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1823 SDOperand N1, SDOperand N2, SDOperand N3) {
1824 // Perform various simplifications.
1825 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1826 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1827 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1828 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001829 case ISD::SETCC: {
1830 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001831 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001832 if (Simp.Val) return Simp;
1833 break;
1834 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001835 case ISD::SELECT:
1836 if (N1C)
1837 if (N1C->getValue())
1838 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001839 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001840 return N3; // select false, X, Y -> Y
1841
1842 if (N2 == N3) return N2; // select C, X, X -> X
1843
Nate Begeman0558f612005-10-05 21:44:43 +00001844 if (!CombinerEnabled) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001845 if (VT == MVT::i1) { // Boolean SELECT
1846 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001847 if (N2C->getValue()) // select C, 1, X -> C | X
1848 return getNode(ISD::OR, VT, N1, N3);
1849 else // select C, 0, X -> ~C & X
1850 return getNode(ISD::AND, VT,
1851 getNode(ISD::XOR, N1.getValueType(), N1,
1852 getConstant(1, N1.getValueType())), N3);
1853 } else if (N3C) {
1854 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1855 return getNode(ISD::OR, VT,
1856 getNode(ISD::XOR, N1.getValueType(), N1,
1857 getConstant(1, N1.getValueType())), N2);
1858 else // select C, X, 0 -> C & X
1859 return getNode(ISD::AND, VT, N1, N2);
1860 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001861
1862 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1863 return getNode(ISD::OR, VT, N1, N3);
1864 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1865 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001866 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001867 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001868 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1869 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001870 if (Simp.Val) return Simp;
1871 }
Nate Begeman0558f612005-10-05 21:44:43 +00001872 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001873 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001874 case ISD::BRCOND:
1875 if (N2C)
1876 if (N2C->getValue()) // Unconditional branch
1877 return getNode(ISD::BR, MVT::Other, N1, N3);
1878 else
1879 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001880 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001881 }
1882
Chris Lattner385328c2005-05-14 07:42:29 +00001883 std::vector<SDOperand> Ops;
1884 Ops.reserve(3);
1885 Ops.push_back(N1);
1886 Ops.push_back(N2);
1887 Ops.push_back(N3);
1888
Chris Lattner43247a12005-08-25 19:12:10 +00001889 // Memoize node if it doesn't produce a flag.
1890 SDNode *N;
1891 if (VT != MVT::Flag) {
1892 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1893 if (E) return SDOperand(E, 0);
1894 E = N = new SDNode(Opcode, N1, N2, N3);
1895 } else {
1896 N = new SDNode(Opcode, N1, N2, N3);
1897 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001898 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001899 AllNodes.push_back(N);
1900 return SDOperand(N, 0);
1901}
1902
1903SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001904 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001905 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001906 std::vector<SDOperand> Ops;
1907 Ops.reserve(4);
1908 Ops.push_back(N1);
1909 Ops.push_back(N2);
1910 Ops.push_back(N3);
1911 Ops.push_back(N4);
1912 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001913}
1914
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001915SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1916 SDOperand N1, SDOperand N2, SDOperand N3,
1917 SDOperand N4, SDOperand N5) {
1918 std::vector<SDOperand> Ops;
1919 Ops.reserve(5);
1920 Ops.push_back(N1);
1921 Ops.push_back(N2);
1922 Ops.push_back(N3);
1923 Ops.push_back(N4);
1924 Ops.push_back(N5);
1925 return getNode(Opcode, VT, Ops);
1926}
1927
1928
Chris Lattner0437cdd2005-05-09 04:14:13 +00001929SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001930 assert((!V || isa<PointerType>(V->getType())) &&
1931 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001932 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1933 if (N) return SDOperand(N, 0);
1934
1935 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001936 AllNodes.push_back(N);
1937 return SDOperand(N, 0);
1938}
1939
1940SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001941 std::vector<SDOperand> &Ops) {
1942 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001943 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001944 case 1: return getNode(Opcode, VT, Ops[0]);
1945 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1946 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001947 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001948 }
Chris Lattneref847df2005-04-09 03:27:28 +00001949
Chris Lattner89c34632005-05-14 06:20:26 +00001950 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001951 switch (Opcode) {
1952 default: break;
1953 case ISD::BRCONDTWOWAY:
1954 if (N1C)
1955 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001956 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001957 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001958 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001959 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001960 case ISD::BRTWOWAY_CC:
1961 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1962 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1963 "LHS and RHS of comparison must have same type!");
1964 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001965 case ISD::TRUNCSTORE: {
1966 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1967 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1968#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1969 // If this is a truncating store of a constant, convert to the desired type
1970 // and store it instead.
1971 if (isa<Constant>(Ops[0])) {
1972 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1973 if (isa<Constant>(Op))
1974 N1 = Op;
1975 }
1976 // Also for ConstantFP?
1977#endif
1978 if (Ops[0].getValueType() == EVT) // Normal store?
1979 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1980 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1981 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1982 "Can't do FP-INT conversion!");
1983 break;
1984 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001985 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001986 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001987 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1988 "LHS and RHS of condition must have same type!");
1989 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1990 "True and False arms of SelectCC must have same type!");
1991 assert(Ops[2].getValueType() == VT &&
1992 "select_cc node must be of same type as true and false value!");
1993 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1994 cast<CondCodeSDNode>(Ops[4])->get());
1995 if (Simp.Val) return Simp;
1996 break;
1997 }
1998 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001999 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00002000 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
2001 "LHS/RHS of comparison should match types!");
Chris Lattner3ea0b472005-10-05 06:47:48 +00002002
2003 if (CombinerEnabled) break; // xforms moved to dag combine.
2004
Chris Lattner7b2880c2005-08-24 22:44:39 +00002005 // Use SimplifySetCC to simplify SETCC's.
2006 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
2007 cast<CondCodeSDNode>(Ops[1])->get());
2008 if (Simp.Val) {
2009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
2010 if (C->getValue() & 1) // Unconditional branch
2011 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
2012 else
2013 return Ops[0]; // Unconditional Fall through
2014 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
2015 Ops[2] = Simp.getOperand(0);
2016 Ops[3] = Simp.getOperand(1);
2017 Ops[1] = Simp.getOperand(2);
2018 }
2019 }
2020 break;
2021 }
Chris Lattneref847df2005-04-09 03:27:28 +00002022 }
2023
Chris Lattner385328c2005-05-14 07:42:29 +00002024 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00002025 SDNode *N;
2026 if (VT != MVT::Flag) {
2027 SDNode *&E =
2028 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
2029 if (E) return SDOperand(E, 0);
2030 E = N = new SDNode(Opcode, Ops);
2031 } else {
2032 N = new SDNode(Opcode, Ops);
2033 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002034 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00002035 AllNodes.push_back(N);
2036 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002037}
2038
Chris Lattner89c34632005-05-14 06:20:26 +00002039SDOperand SelectionDAG::getNode(unsigned Opcode,
2040 std::vector<MVT::ValueType> &ResultTys,
2041 std::vector<SDOperand> &Ops) {
2042 if (ResultTys.size() == 1)
2043 return getNode(Opcode, ResultTys[0], Ops);
2044
Chris Lattner5f056bf2005-07-10 01:55:33 +00002045 switch (Opcode) {
2046 case ISD::EXTLOAD:
2047 case ISD::SEXTLOAD:
2048 case ISD::ZEXTLOAD: {
2049 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
2050 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
2051 // If they are asking for an extending load from/to the same thing, return a
2052 // normal load.
2053 if (ResultTys[0] == EVT)
2054 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
2055 assert(EVT < ResultTys[0] &&
2056 "Should only be an extending load, not truncating!");
2057 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
2058 "Cannot sign/zero extend a FP load!");
2059 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
2060 "Cannot convert from FP to Int or Int -> FP!");
2061 break;
2062 }
2063
Chris Lattnere89083a2005-05-14 07:25:05 +00002064 // FIXME: figure out how to safely handle things like
2065 // int foo(int x) { return 1 << (x & 255); }
2066 // int bar() { return foo(256); }
2067#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002068 case ISD::SRA_PARTS:
2069 case ISD::SRL_PARTS:
2070 case ISD::SHL_PARTS:
2071 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002072 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002073 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2074 else if (N3.getOpcode() == ISD::AND)
2075 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2076 // If the and is only masking out bits that cannot effect the shift,
2077 // eliminate the and.
2078 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2079 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2080 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2081 }
2082 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002083#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002084 }
Chris Lattner89c34632005-05-14 06:20:26 +00002085
Chris Lattner43247a12005-08-25 19:12:10 +00002086 // Memoize the node unless it returns a flag.
2087 SDNode *N;
2088 if (ResultTys.back() != MVT::Flag) {
2089 SDNode *&E =
2090 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
2091 if (E) return SDOperand(E, 0);
2092 E = N = new SDNode(Opcode, Ops);
2093 } else {
2094 N = new SDNode(Opcode, Ops);
2095 }
Chris Lattner89c34632005-05-14 06:20:26 +00002096 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002097 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002098 return SDOperand(N, 0);
2099}
2100
Chris Lattner149c58c2005-08-16 18:17:10 +00002101
2102/// SelectNodeTo - These are used for target selectors to *mutate* the
2103/// specified node to have the specified return type, Target opcode, and
2104/// operands. Note that target opcodes are stored as
2105/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002106void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2107 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00002108 RemoveNodeFromCSEMaps(N);
2109 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2110 N->setValueTypes(VT);
2111}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002112void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2113 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00002114 RemoveNodeFromCSEMaps(N);
2115 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2116 N->setValueTypes(VT);
2117 N->setOperands(Op1);
2118}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002119void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2120 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00002121 SDOperand Op2) {
2122 RemoveNodeFromCSEMaps(N);
2123 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2124 N->setValueTypes(VT);
2125 N->setOperands(Op1, Op2);
2126}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002127void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00002128 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002129 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00002130 RemoveNodeFromCSEMaps(N);
2131 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2132 N->setValueTypes(VT1, VT2);
2133 N->setOperands(Op1, Op2);
2134}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002135void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2136 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00002137 SDOperand Op2, SDOperand Op3) {
2138 RemoveNodeFromCSEMaps(N);
2139 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2140 N->setValueTypes(VT);
2141 N->setOperands(Op1, Op2, Op3);
2142}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002143void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2144 MVT::ValueType VT1, MVT::ValueType VT2,
2145 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002146 RemoveNodeFromCSEMaps(N);
2147 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2148 N->setValueTypes(VT1, VT2);
2149 N->setOperands(Op1, Op2, Op3);
2150}
2151
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002152void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2153 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00002154 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
2155 RemoveNodeFromCSEMaps(N);
2156 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2157 N->setValueTypes(VT);
2158 N->setOperands(Op1, Op2, Op3, Op4);
2159}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002160void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2161 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00002162 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2163 SDOperand Op5) {
2164 RemoveNodeFromCSEMaps(N);
2165 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2166 N->setValueTypes(VT);
2167 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2168}
Chris Lattner149c58c2005-08-16 18:17:10 +00002169
Chris Lattner8b8749f2005-08-17 19:00:20 +00002170/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2171/// This can cause recursive merging of nodes in the DAG.
2172///
Chris Lattner8b52f212005-08-26 18:36:28 +00002173/// This version assumes From/To have a single result value.
2174///
Chris Lattner1e111c72005-09-07 05:37:01 +00002175void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2176 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002177 SDNode *From = FromN.Val, *To = ToN.Val;
2178 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2179 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002180 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002181
Chris Lattner8b8749f2005-08-17 19:00:20 +00002182 while (!From->use_empty()) {
2183 // Process users until they are all gone.
2184 SDNode *U = *From->use_begin();
2185
2186 // This node is about to morph, remove its old self from the CSE maps.
2187 RemoveNodeFromCSEMaps(U);
2188
2189 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2190 if (U->getOperand(i).Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002191 From->removeUser(U);
2192 U->Operands[i].Val = To;
2193 To->addUser(U);
2194 }
2195
2196 // Now that we have modified U, add it back to the CSE maps. If it already
2197 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002198 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2199 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002200 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002201 if (Deleted) Deleted->push_back(U);
2202 DeleteNodeNotInCSEMaps(U);
2203 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002204 }
2205}
2206
2207/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2208/// This can cause recursive merging of nodes in the DAG.
2209///
2210/// This version assumes From/To have matching types and numbers of result
2211/// values.
2212///
Chris Lattner1e111c72005-09-07 05:37:01 +00002213void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2214 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002215 assert(From != To && "Cannot replace uses of with self");
2216 assert(From->getNumValues() == To->getNumValues() &&
2217 "Cannot use this version of ReplaceAllUsesWith!");
2218 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002219 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002220 return;
2221 }
2222
2223 while (!From->use_empty()) {
2224 // Process users until they are all gone.
2225 SDNode *U = *From->use_begin();
2226
2227 // This node is about to morph, remove its old self from the CSE maps.
2228 RemoveNodeFromCSEMaps(U);
2229
2230 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2231 if (U->getOperand(i).Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002232 From->removeUser(U);
2233 U->Operands[i].Val = To;
2234 To->addUser(U);
2235 }
2236
2237 // Now that we have modified U, add it back to the CSE maps. If it already
2238 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002239 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2240 ReplaceAllUsesWith(U, Existing, Deleted);
2241 // U is now dead.
2242 if (Deleted) Deleted->push_back(U);
2243 DeleteNodeNotInCSEMaps(U);
2244 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002245 }
2246}
2247
Chris Lattner8b52f212005-08-26 18:36:28 +00002248/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2249/// This can cause recursive merging of nodes in the DAG.
2250///
2251/// This version can replace From with any result values. To must match the
2252/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002253void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002254 const std::vector<SDOperand> &To,
2255 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002256 assert(From->getNumValues() == To.size() &&
2257 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002258 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002259 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002260 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002261 return;
2262 }
2263
2264 while (!From->use_empty()) {
2265 // Process users until they are all gone.
2266 SDNode *U = *From->use_begin();
2267
2268 // This node is about to morph, remove its old self from the CSE maps.
2269 RemoveNodeFromCSEMaps(U);
2270
2271 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2272 if (U->getOperand(i).Val == From) {
2273 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002274 From->removeUser(U);
2275 U->Operands[i] = ToOp;
2276 ToOp.Val->addUser(U);
2277 }
2278
2279 // Now that we have modified U, add it back to the CSE maps. If it already
2280 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002281 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2282 ReplaceAllUsesWith(U, Existing, Deleted);
2283 // U is now dead.
2284 if (Deleted) Deleted->push_back(U);
2285 DeleteNodeNotInCSEMaps(U);
2286 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002287 }
2288}
2289
2290
Jim Laskey58b968b2005-08-17 20:08:02 +00002291//===----------------------------------------------------------------------===//
2292// SDNode Class
2293//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002294
Chris Lattner5c884562005-01-12 18:37:47 +00002295/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2296/// indicated value. This method ignores uses of other values defined by this
2297/// operation.
2298bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2299 assert(Value < getNumValues() && "Bad value!");
2300
2301 // If there is only one value, this is easy.
2302 if (getNumValues() == 1)
2303 return use_size() == NUses;
2304 if (Uses.size() < NUses) return false;
2305
2306 SDOperand TheValue(this, Value);
2307
2308 std::set<SDNode*> UsersHandled;
2309
2310 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2311 UI != E; ++UI) {
2312 SDNode *User = *UI;
2313 if (User->getNumOperands() == 1 ||
2314 UsersHandled.insert(User).second) // First time we've seen this?
2315 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2316 if (User->getOperand(i) == TheValue) {
2317 if (NUses == 0)
2318 return false; // too many uses
2319 --NUses;
2320 }
2321 }
2322
2323 // Found exactly the right number of uses?
2324 return NUses == 0;
2325}
2326
2327
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002328const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002329 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002330 default:
2331 if (getOpcode() < ISD::BUILTIN_OP_END)
2332 return "<<Unknown DAG Node>>";
2333 else {
2334 if (G)
2335 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002336 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2337 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002338 return "<<Unknown Target Node>>";
2339 }
2340
Andrew Lenharth95762122005-03-31 21:24:06 +00002341 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002342 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002343 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002344 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002345 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002346 case ISD::AssertSext: return "AssertSext";
2347 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002348 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002349 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002350 case ISD::ConstantFP: return "ConstantFP";
2351 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002352 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002353 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002354 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002355 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002356 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002357 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002358 case ISD::ConstantPool: return "ConstantPool";
2359 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002360 case ISD::CopyToReg: return "CopyToReg";
2361 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002362 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002363 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002364
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002365 // Unary operators
2366 case ISD::FABS: return "fabs";
2367 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002368 case ISD::FSQRT: return "fsqrt";
2369 case ISD::FSIN: return "fsin";
2370 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002371
2372 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002373 case ISD::ADD: return "add";
2374 case ISD::SUB: return "sub";
2375 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002376 case ISD::MULHU: return "mulhu";
2377 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002378 case ISD::SDIV: return "sdiv";
2379 case ISD::UDIV: return "udiv";
2380 case ISD::SREM: return "srem";
2381 case ISD::UREM: return "urem";
2382 case ISD::AND: return "and";
2383 case ISD::OR: return "or";
2384 case ISD::XOR: return "xor";
2385 case ISD::SHL: return "shl";
2386 case ISD::SRA: return "sra";
2387 case ISD::SRL: return "srl";
Chris Lattner01b3d732005-09-28 22:28:18 +00002388 case ISD::FADD: return "fadd";
2389 case ISD::FSUB: return "fsub";
2390 case ISD::FMUL: return "fmul";
2391 case ISD::FDIV: return "fdiv";
2392 case ISD::FREM: return "frem";
2393
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002394 case ISD::SETCC: return "setcc";
2395 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002396 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002397 case ISD::ADD_PARTS: return "add_parts";
2398 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002399 case ISD::SHL_PARTS: return "shl_parts";
2400 case ISD::SRA_PARTS: return "sra_parts";
2401 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002402
Chris Lattner7f644642005-04-28 21:44:03 +00002403 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002404 case ISD::SIGN_EXTEND: return "sign_extend";
2405 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002406 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002407 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002408 case ISD::TRUNCATE: return "truncate";
2409 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002410 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002411 case ISD::FP_EXTEND: return "fp_extend";
2412
2413 case ISD::SINT_TO_FP: return "sint_to_fp";
2414 case ISD::UINT_TO_FP: return "uint_to_fp";
2415 case ISD::FP_TO_SINT: return "fp_to_sint";
2416 case ISD::FP_TO_UINT: return "fp_to_uint";
2417
2418 // Control flow instructions
2419 case ISD::BR: return "br";
2420 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002421 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002422 case ISD::BR_CC: return "br_cc";
2423 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002424 case ISD::RET: return "ret";
2425 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002426 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002427 case ISD::CALLSEQ_START: return "callseq_start";
2428 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002429
2430 // Other operators
2431 case ISD::LOAD: return "load";
2432 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002433 case ISD::EXTLOAD: return "extload";
2434 case ISD::SEXTLOAD: return "sextload";
2435 case ISD::ZEXTLOAD: return "zextload";
2436 case ISD::TRUNCSTORE: return "truncstore";
2437
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002438 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2439 case ISD::EXTRACT_ELEMENT: return "extract_element";
2440 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002441 case ISD::MEMSET: return "memset";
2442 case ISD::MEMCPY: return "memcpy";
2443 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002444
Chris Lattner276260b2005-05-11 04:50:30 +00002445 // Bit counting
2446 case ISD::CTPOP: return "ctpop";
2447 case ISD::CTTZ: return "cttz";
2448 case ISD::CTLZ: return "ctlz";
2449
2450 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002451 case ISD::READPORT: return "readport";
2452 case ISD::WRITEPORT: return "writeport";
2453 case ISD::READIO: return "readio";
2454 case ISD::WRITEIO: return "writeio";
2455
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002456 case ISD::CONDCODE:
2457 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002458 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002459 case ISD::SETOEQ: return "setoeq";
2460 case ISD::SETOGT: return "setogt";
2461 case ISD::SETOGE: return "setoge";
2462 case ISD::SETOLT: return "setolt";
2463 case ISD::SETOLE: return "setole";
2464 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002465
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002466 case ISD::SETO: return "seto";
2467 case ISD::SETUO: return "setuo";
2468 case ISD::SETUEQ: return "setue";
2469 case ISD::SETUGT: return "setugt";
2470 case ISD::SETUGE: return "setuge";
2471 case ISD::SETULT: return "setult";
2472 case ISD::SETULE: return "setule";
2473 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002474
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002475 case ISD::SETEQ: return "seteq";
2476 case ISD::SETGT: return "setgt";
2477 case ISD::SETGE: return "setge";
2478 case ISD::SETLT: return "setlt";
2479 case ISD::SETLE: return "setle";
2480 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002481 }
2482 }
2483}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002484
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002485void SDNode::dump() const { dump(0); }
2486void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002487 std::cerr << (void*)this << ": ";
2488
2489 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2490 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002491 if (getValueType(i) == MVT::Other)
2492 std::cerr << "ch";
2493 else
2494 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002495 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002496 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002497
2498 std::cerr << " ";
2499 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2500 if (i) std::cerr << ", ";
2501 std::cerr << (void*)getOperand(i).Val;
2502 if (unsigned RN = getOperand(i).ResNo)
2503 std::cerr << ":" << RN;
2504 }
2505
2506 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2507 std::cerr << "<" << CSDN->getValue() << ">";
2508 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2509 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002510 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002511 dyn_cast<GlobalAddressSDNode>(this)) {
2512 std::cerr << "<";
2513 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002514 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002515 std::cerr << "<" << FIDN->getIndex() << ">";
2516 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002517 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002518 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002519 std::cerr << "<";
2520 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2521 if (LBB)
2522 std::cerr << LBB->getName() << " ";
2523 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002524 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002525 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2526 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2527 } else {
2528 std::cerr << " #" << R->getReg();
2529 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002530 } else if (const ExternalSymbolSDNode *ES =
2531 dyn_cast<ExternalSymbolSDNode>(this)) {
2532 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002533 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2534 if (M->getValue())
2535 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2536 else
2537 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002538 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2539 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002540 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002541}
2542
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002543static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002544 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2545 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002546 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002547 else
2548 std::cerr << "\n" << std::string(indent+2, ' ')
2549 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002550
Chris Lattnerea946cd2005-01-09 20:38:33 +00002551
2552 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002553 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002554}
2555
Chris Lattnerc3aae252005-01-07 07:46:32 +00002556void SelectionDAG::dump() const {
2557 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002558 std::vector<SDNode*> Nodes(AllNodes);
2559 std::sort(Nodes.begin(), Nodes.end());
2560
2561 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002562 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002563 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002564 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002565
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002566 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002567
Chris Lattnerc3aae252005-01-07 07:46:32 +00002568 std::cerr << "\n\n";
2569}
2570