blob: dd5afbaab13596e2dfa76cc4905d716bbb11c7e7 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner5cdcc582005-01-09 20:52:51 +000030static bool isCommutativeBinOp(unsigned Opcode) {
31 switch (Opcode) {
32 case ISD::ADD:
33 case ISD::MUL:
34 case ISD::AND:
35 case ISD::OR:
36 case ISD::XOR: return true;
37 default: return false; // FIXME: Need commutative info for user ops!
38 }
39}
40
41static bool isAssociativeBinOp(unsigned Opcode) {
42 switch (Opcode) {
43 case ISD::ADD:
44 case ISD::MUL:
45 case ISD::AND:
46 case ISD::OR:
47 case ISD::XOR: return true;
48 default: return false; // FIXME: Need associative info for user ops!
49 }
50}
51
Chris Lattner5cdcc582005-01-09 20:52:51 +000052// isInvertibleForFree - Return true if there is no cost to emitting the logical
53// inverse of this node.
54static bool isInvertibleForFree(SDOperand N) {
55 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000056 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000057 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000058 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000059}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
69bool ConstantFPSDNode::isExactlyValue(double V) const {
70 return DoubleToBits(V) == DoubleToBits(Value);
71}
72
73//===----------------------------------------------------------------------===//
74// ISD Class
75//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000076
Chris Lattnerc3aae252005-01-07 07:46:32 +000077/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
78/// when given the operation for (X op Y).
79ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
80 // To perform this operation, we just need to swap the L and G bits of the
81 // operation.
82 unsigned OldL = (Operation >> 2) & 1;
83 unsigned OldG = (Operation >> 1) & 1;
84 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
85 (OldL << 1) | // New G bit
86 (OldG << 2)); // New L bit.
87}
88
89/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
90/// 'op' is a valid SetCC operation.
91ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
92 unsigned Operation = Op;
93 if (isInteger)
94 Operation ^= 7; // Flip L, G, E bits, but not U.
95 else
96 Operation ^= 15; // Flip all of the condition bits.
97 if (Operation > ISD::SETTRUE2)
98 Operation &= ~8; // Don't let N and U bits get set.
99 return ISD::CondCode(Operation);
100}
101
102
103/// isSignedOp - For an integer comparison, return 1 if the comparison is a
104/// signed operation and 2 if the result is an unsigned comparison. Return zero
105/// if the operation does not depend on the sign of the input (setne and seteq).
106static int isSignedOp(ISD::CondCode Opcode) {
107 switch (Opcode) {
108 default: assert(0 && "Illegal integer setcc operation!");
109 case ISD::SETEQ:
110 case ISD::SETNE: return 0;
111 case ISD::SETLT:
112 case ISD::SETLE:
113 case ISD::SETGT:
114 case ISD::SETGE: return 1;
115 case ISD::SETULT:
116 case ISD::SETULE:
117 case ISD::SETUGT:
118 case ISD::SETUGE: return 2;
119 }
120}
121
122/// getSetCCOrOperation - Return the result of a logical OR between different
123/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
124/// returns SETCC_INVALID if it is not possible to represent the resultant
125/// comparison.
126ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
127 bool isInteger) {
128 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
129 // Cannot fold a signed integer setcc with an unsigned integer setcc.
130 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131
Chris Lattnerc3aae252005-01-07 07:46:32 +0000132 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 // If the N and U bits get set then the resultant comparison DOES suddenly
135 // care about orderedness, and is true when ordered.
136 if (Op > ISD::SETTRUE2)
137 Op &= ~16; // Clear the N bit.
138 return ISD::CondCode(Op);
139}
140
141/// getSetCCAndOperation - Return the result of a logical AND between different
142/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
143/// function returns zero if it is not possible to represent the resultant
144/// comparison.
145ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
146 bool isInteger) {
147 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
148 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000149 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000150
151 // Combine all of the condition bits.
152 return ISD::CondCode(Op1 & Op2);
153}
154
Chris Lattnerb48da392005-01-23 04:39:44 +0000155const TargetMachine &SelectionDAG::getTarget() const {
156 return TLI.getTargetMachine();
157}
158
Jim Laskey58b968b2005-08-17 20:08:02 +0000159//===----------------------------------------------------------------------===//
160// SelectionDAG Class
161//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000162
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000163/// RemoveDeadNodes - This method deletes all unreachable nodes in the
164/// SelectionDAG, including nodes (like loads) that have uses of their token
165/// chain but no other uses and no side effect. If a node is passed in as an
166/// argument, it is used as the seed for node deletion.
167void SelectionDAG::RemoveDeadNodes(SDNode *N) {
168 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
169
170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
172 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
173
Chris Lattner8b8749f2005-08-17 19:00:20 +0000174 // If we have a hint to start from, use it.
175 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000176
177 Restart:
178 unsigned NumNodes = AllNodeSet.size();
179 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
180 I != E; ++I) {
181 // Try to delete this node.
182 DeleteNodeIfDead(*I, &AllNodeSet);
183
184 // If we actually deleted any nodes, do not use invalid iterators in
185 // AllNodeSet.
186 if (AllNodeSet.size() != NumNodes)
187 goto Restart;
188 }
189
190 // Restore AllNodes.
191 if (AllNodes.size() != NumNodes)
192 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
193
194 // If the root changed (e.g. it was a dead load, update the root).
195 setRoot(DummyNode->getOperand(0));
196
197 // Now that we are done with the dummy node, delete it.
198 DummyNode->getOperand(0).Val->removeUser(DummyNode);
199 delete DummyNode;
200}
201
Chris Lattner149c58c2005-08-16 18:17:10 +0000202
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
204 if (!N->use_empty())
205 return;
206
207 // Okay, we really are going to delete this node. First take this out of the
208 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000209 RemoveNodeFromCSEMaps(N);
210
211 // Next, brutally remove the operand list. This is safe to do, as there are
212 // no cycles in the graph.
213 while (!N->Operands.empty()) {
214 SDNode *O = N->Operands.back().Val;
215 N->Operands.pop_back();
216 O->removeUser(N);
217
218 // Now that we removed this operand, see if there are no uses of it left.
219 DeleteNodeIfDead(O, NodeSet);
220 }
221
222 // Remove the node from the nodes set and delete it.
223 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
224 AllNodeSet.erase(N);
225
226 // Now that the node is gone, check to see if any of the operands of this node
227 // are dead now.
228 delete N;
229}
230
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000231void SelectionDAG::DeleteNode(SDNode *N) {
232 assert(N->use_empty() && "Cannot delete a node that is not dead!");
233
234 // First take this out of the appropriate CSE map.
235 RemoveNodeFromCSEMaps(N);
236
237 // Remove it from the AllNodes list.
238 for (std::vector<SDNode*>::iterator I = AllNodes.begin(); ; ++I) {
239 assert(I != AllNodes.end() && "Node not in AllNodes list??");
240 if (*I == N) {
241 // Erase from the vector, which is not ordered.
242 std::swap(*I, AllNodes.back());
243 AllNodes.pop_back();
244 break;
245 }
246 }
247
248 // Drop all of the operands and decrement used nodes use counts.
249 while (!N->Operands.empty()) {
250 SDNode *O = N->Operands.back().Val;
251 N->Operands.pop_back();
252 O->removeUser(N);
253 }
254
255 delete N;
256}
257
Chris Lattner149c58c2005-08-16 18:17:10 +0000258/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259/// correspond to it. This is useful when we're about to delete or repurpose
260/// the node. We don't want future request for structurally identical nodes
261/// to return N anymore.
262void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000263 switch (N->getOpcode()) {
264 case ISD::Constant:
265 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
266 N->getValueType(0)));
267 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000268 case ISD::TargetConstant:
269 TargetConstants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
270 N->getValueType(0)));
271 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000272 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000273 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
274 ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000276 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000277 case ISD::CONDCODE:
278 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
279 "Cond code doesn't exist!");
280 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
281 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000282 case ISD::GlobalAddress:
283 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
284 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000285 case ISD::TargetGlobalAddress:
286 TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
287 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000288 case ISD::FrameIndex:
289 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
290 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000291 case ISD::TargetFrameIndex:
292 TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
293 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000294 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000295 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000296 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000297 case ISD::TargetConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000298 TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000299 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000300 case ISD::BasicBlock:
301 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
302 break;
303 case ISD::ExternalSymbol:
304 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
305 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000306 case ISD::VALUETYPE:
307 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
308 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000309 case ISD::Register:
Chris Lattner0fdd7682005-08-30 22:38:38 +0000310 RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
311 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000312 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000313 case ISD::SRCVALUE: {
314 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
315 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
316 break;
317 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318 case ISD::LOAD:
319 Loads.erase(std::make_pair(N->getOperand(1),
320 std::make_pair(N->getOperand(0),
321 N->getValueType(0))));
322 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000323 default:
324 if (N->getNumOperands() == 1)
325 UnaryOps.erase(std::make_pair(N->getOpcode(),
326 std::make_pair(N->getOperand(0),
327 N->getValueType(0))));
328 else if (N->getNumOperands() == 2)
329 BinaryOps.erase(std::make_pair(N->getOpcode(),
330 std::make_pair(N->getOperand(0),
331 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000332 else if (N->getNumValues() == 1) {
333 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
334 OneResultNodes.erase(std::make_pair(N->getOpcode(),
335 std::make_pair(N->getValueType(0),
336 Ops)));
337 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000338 // Remove the node from the ArbitraryNodes map.
339 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
340 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
341 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
342 std::make_pair(RV, Ops)));
343 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000344 break;
345 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000346}
347
Chris Lattner8b8749f2005-08-17 19:00:20 +0000348/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
349/// has been taken out and modified in some way. If the specified node already
350/// exists in the CSE maps, do not modify the maps, but return the existing node
351/// instead. If it doesn't exist, add it and return null.
352///
353SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
354 assert(N->getNumOperands() && "This is a leaf node!");
355 if (N->getOpcode() == ISD::LOAD) {
356 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
357 std::make_pair(N->getOperand(0),
358 N->getValueType(0)))];
359 if (L) return L;
360 L = N;
361 } else if (N->getNumOperands() == 1) {
362 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
363 std::make_pair(N->getOperand(0),
364 N->getValueType(0)))];
365 if (U) return U;
366 U = N;
367 } else if (N->getNumOperands() == 2) {
368 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
369 std::make_pair(N->getOperand(0),
370 N->getOperand(1)))];
371 if (B) return B;
372 B = N;
373 } else if (N->getNumValues() == 1) {
374 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
375 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
376 std::make_pair(N->getValueType(0), Ops))];
377 if (ORN) return ORN;
378 ORN = N;
379 } else {
380 // Remove the node from the ArbitraryNodes map.
381 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
382 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
383 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
384 std::make_pair(RV, Ops))];
385 if (AN) return AN;
386 AN = N;
387 }
388 return 0;
389
390}
391
392
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000393
Chris Lattner78ec3112003-08-11 14:57:33 +0000394SelectionDAG::~SelectionDAG() {
395 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
396 delete AllNodes[i];
397}
398
Chris Lattner0f2287b2005-04-13 02:38:18 +0000399SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000400 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000401 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000402 return getNode(ISD::AND, Op.getValueType(), Op,
403 getConstant(Imm, Op.getValueType()));
404}
405
Chris Lattnerc3aae252005-01-07 07:46:32 +0000406SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
407 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
408 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000409 if (VT != MVT::i64)
410 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000411
Chris Lattnerc3aae252005-01-07 07:46:32 +0000412 SDNode *&N = Constants[std::make_pair(Val, VT)];
413 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000414 N = new ConstantSDNode(false, Val, VT);
415 AllNodes.push_back(N);
416 return SDOperand(N, 0);
417}
418
419SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
420 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
421 // Mask out any bits that are not valid for this constant.
422 if (VT != MVT::i64)
423 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
424
425 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
426 if (N) return SDOperand(N, 0);
427 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000428 AllNodes.push_back(N);
429 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000430}
431
Chris Lattnerc3aae252005-01-07 07:46:32 +0000432SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
433 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
434 if (VT == MVT::f32)
435 Val = (float)Val; // Mask out extra precision.
436
Chris Lattnerd8658612005-02-17 20:17:32 +0000437 // Do the map lookup using the actual bit pattern for the floating point
438 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
439 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000440 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000441 if (N) return SDOperand(N, 0);
442 N = new ConstantFPSDNode(Val, VT);
443 AllNodes.push_back(N);
444 return SDOperand(N, 0);
445}
446
447
448
449SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
450 MVT::ValueType VT) {
451 SDNode *&N = GlobalValues[GV];
452 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000453 N = new GlobalAddressSDNode(false, GV, VT);
454 AllNodes.push_back(N);
455 return SDOperand(N, 0);
456}
457
458SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
459 MVT::ValueType VT) {
460 SDNode *&N = TargetGlobalValues[GV];
461 if (N) return SDOperand(N, 0);
462 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000463 AllNodes.push_back(N);
464 return SDOperand(N, 0);
465}
466
467SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
468 SDNode *&N = FrameIndices[FI];
469 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000470 N = new FrameIndexSDNode(FI, VT, false);
471 AllNodes.push_back(N);
472 return SDOperand(N, 0);
473}
474
475SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
476 SDNode *&N = TargetFrameIndices[FI];
477 if (N) return SDOperand(N, 0);
478 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000479 AllNodes.push_back(N);
480 return SDOperand(N, 0);
481}
482
Chris Lattner5839bf22005-08-26 17:15:30 +0000483SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
484 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000485 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000486 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000487 AllNodes.push_back(N);
488 return SDOperand(N, 0);
489}
490
Chris Lattner5839bf22005-08-26 17:15:30 +0000491SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
492 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000493 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000494 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000495 AllNodes.push_back(N);
496 return SDOperand(N, 0);
497}
498
499SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
500 SDNode *&N = BBNodes[MBB];
501 if (N) return SDOperand(N, 0);
502 N = new BasicBlockSDNode(MBB);
503 AllNodes.push_back(N);
504 return SDOperand(N, 0);
505}
506
Chris Lattner15e4b012005-07-10 00:07:11 +0000507SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
508 if ((unsigned)VT >= ValueTypeNodes.size())
509 ValueTypeNodes.resize(VT+1);
510 if (ValueTypeNodes[VT] == 0) {
511 ValueTypeNodes[VT] = new VTSDNode(VT);
512 AllNodes.push_back(ValueTypeNodes[VT]);
513 }
514
515 return SDOperand(ValueTypeNodes[VT], 0);
516}
517
Chris Lattnerc3aae252005-01-07 07:46:32 +0000518SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
519 SDNode *&N = ExternalSymbols[Sym];
520 if (N) return SDOperand(N, 0);
521 N = new ExternalSymbolSDNode(Sym, VT);
522 AllNodes.push_back(N);
523 return SDOperand(N, 0);
524}
525
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000526SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
527 if ((unsigned)Cond >= CondCodeNodes.size())
528 CondCodeNodes.resize(Cond+1);
529
Chris Lattner079a27a2005-08-09 20:40:02 +0000530 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000531 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000532 AllNodes.push_back(CondCodeNodes[Cond]);
533 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000534 return SDOperand(CondCodeNodes[Cond], 0);
535}
536
Chris Lattner0fdd7682005-08-30 22:38:38 +0000537SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
538 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
539 if (!Reg) {
540 Reg = new RegisterSDNode(RegNo, VT);
541 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000542 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000543 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000544}
545
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000546SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
547 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000548 // These setcc operations always fold.
549 switch (Cond) {
550 default: break;
551 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000552 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000553 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000554 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000555 }
556
Chris Lattner67255a12005-04-07 18:14:58 +0000557 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
558 uint64_t C2 = N2C->getValue();
559 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
560 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000561
Chris Lattnerc3aae252005-01-07 07:46:32 +0000562 // Sign extend the operands if required
563 if (ISD::isSignedIntSetCC(Cond)) {
564 C1 = N1C->getSignExtended();
565 C2 = N2C->getSignExtended();
566 }
567
568 switch (Cond) {
569 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000570 case ISD::SETEQ: return getConstant(C1 == C2, VT);
571 case ISD::SETNE: return getConstant(C1 != C2, VT);
572 case ISD::SETULT: return getConstant(C1 < C2, VT);
573 case ISD::SETUGT: return getConstant(C1 > C2, VT);
574 case ISD::SETULE: return getConstant(C1 <= C2, VT);
575 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
576 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
577 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
578 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
579 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000580 }
Chris Lattner24673922005-04-07 18:58:54 +0000581 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000582 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000583 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
584 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
585
586 // If the comparison constant has bits in the upper part, the
587 // zero-extended value could never match.
588 if (C2 & (~0ULL << InSize)) {
589 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
590 switch (Cond) {
591 case ISD::SETUGT:
592 case ISD::SETUGE:
593 case ISD::SETEQ: return getConstant(0, VT);
594 case ISD::SETULT:
595 case ISD::SETULE:
596 case ISD::SETNE: return getConstant(1, VT);
597 case ISD::SETGT:
598 case ISD::SETGE:
599 // True if the sign bit of C2 is set.
600 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
601 case ISD::SETLT:
602 case ISD::SETLE:
603 // True if the sign bit of C2 isn't set.
604 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
605 default:
606 break;
607 }
608 }
609
610 // Otherwise, we can perform the comparison with the low bits.
611 switch (Cond) {
612 case ISD::SETEQ:
613 case ISD::SETNE:
614 case ISD::SETUGT:
615 case ISD::SETUGE:
616 case ISD::SETULT:
617 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000618 return getSetCC(VT, N1.getOperand(0),
619 getConstant(C2, N1.getOperand(0).getValueType()),
620 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000621 default:
622 break; // todo, be more careful with signed comparisons
623 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000624 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
625 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
626 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
627 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
628 MVT::ValueType ExtDstTy = N1.getValueType();
629 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000630
Chris Lattner7b2880c2005-08-24 22:44:39 +0000631 // If the extended part has any inconsistent bits, it cannot ever
632 // compare equal. In other words, they have to be all ones or all
633 // zeros.
634 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000635 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000636 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
637 return getConstant(Cond == ISD::SETNE, VT);
638
639 // Otherwise, make this a use of a zext.
640 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000641 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000642 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000643 }
644
Chris Lattner67255a12005-04-07 18:14:58 +0000645 uint64_t MinVal, MaxVal;
646 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
647 if (ISD::isSignedIntSetCC(Cond)) {
648 MinVal = 1ULL << (OperandBitSize-1);
649 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
650 MaxVal = ~0ULL >> (65-OperandBitSize);
651 else
652 MaxVal = 0;
653 } else {
654 MinVal = 0;
655 MaxVal = ~0ULL >> (64-OperandBitSize);
656 }
657
658 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
659 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
660 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
661 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000662 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
663 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000664 }
665
666 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
667 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
668 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000669 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
670 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000671 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000672
Nate Begeman72ea2812005-04-14 08:56:52 +0000673 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
674 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000675
Nate Begeman72ea2812005-04-14 08:56:52 +0000676 // Canonicalize setgt X, Min --> setne X, Min
677 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000678 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000679
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000680 // If we have setult X, 1, turn it into seteq X, 0
681 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000682 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
683 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000684 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000685 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000686 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
687 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000688
689 // If we have "setcc X, C1", check to see if we can shrink the immediate
690 // by changing cc.
691
692 // SETUGT X, SINTMAX -> SETLT X, 0
693 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
694 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000695 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000696
697 // FIXME: Implement the rest of these.
698
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000699
700 // Fold bit comparisons when we can.
701 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
702 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
703 if (ConstantSDNode *AndRHS =
704 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
705 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
706 // Perform the xform if the AND RHS is a single bit.
707 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
708 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000709 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000710 TLI.getShiftAmountTy()));
711 }
712 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
713 // (X & 8) == 8 --> (X & 8) >> 3
714 // Perform the xform if C2 is a single bit.
715 if ((C2 & (C2-1)) == 0) {
716 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000717 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000718 }
719 }
720 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000721 }
Chris Lattner67255a12005-04-07 18:14:58 +0000722 } else if (isa<ConstantSDNode>(N1.Val)) {
723 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000724 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000725 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000726
727 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
728 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
729 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000730
Chris Lattnerc3aae252005-01-07 07:46:32 +0000731 switch (Cond) {
732 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000733 case ISD::SETEQ: return getConstant(C1 == C2, VT);
734 case ISD::SETNE: return getConstant(C1 != C2, VT);
735 case ISD::SETLT: return getConstant(C1 < C2, VT);
736 case ISD::SETGT: return getConstant(C1 > C2, VT);
737 case ISD::SETLE: return getConstant(C1 <= C2, VT);
738 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000739 }
740 } else {
741 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000742 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000743 }
744
745 if (N1 == N2) {
746 // We can always fold X == Y for integer setcc's.
747 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000748 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000749 unsigned UOF = ISD::getUnorderedFlavor(Cond);
750 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000751 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000752 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000753 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000754 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
755 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000756 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
757 if (NewCond != Cond)
758 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000759 }
760
Chris Lattner5cdcc582005-01-09 20:52:51 +0000761 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000762 MVT::isInteger(N1.getValueType())) {
763 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
764 N1.getOpcode() == ISD::XOR) {
765 // Simplify (X+Y) == (X+Z) --> Y == Z
766 if (N1.getOpcode() == N2.getOpcode()) {
767 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000768 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000769 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000770 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000771 if (isCommutativeBinOp(N1.getOpcode())) {
772 // If X op Y == Y op X, try other combinations.
773 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000774 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000775 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000776 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000777 }
778 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000779
780 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000781
Chris Lattner68dc3102005-01-10 02:03:02 +0000782 // Simplify (X+Z) == X --> Z == 0
783 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000784 return getSetCC(VT, N1.getOperand(1),
785 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000786 if (N1.getOperand(1) == N2) {
787 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000788 return getSetCC(VT, N1.getOperand(0),
789 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000790 else {
791 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
792 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000793 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000794 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000795 N2, getConstant(1, TLI.getShiftAmountTy())),
796 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000797 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000798 }
799 }
800
Chris Lattner68dc3102005-01-10 02:03:02 +0000801 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
802 N2.getOpcode() == ISD::XOR) {
803 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000804 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000805 return getSetCC(VT, N2.getOperand(1),
806 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000807 } else if (N2.getOperand(1) == N1) {
808 if (isCommutativeBinOp(N2.getOpcode())) {
809 return getSetCC(VT, N2.getOperand(0),
810 getConstant(0, N2.getValueType()), Cond);
811 } else {
812 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
813 // X == (Z-X) --> X<<1 == Z
814 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
815 getConstant(1, TLI.getShiftAmountTy())),
816 N2.getOperand(0), Cond);
817 }
818 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000819 }
820 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821
Chris Lattnerfda2b552005-04-18 04:48:12 +0000822 // Fold away ALL boolean setcc's.
823 if (N1.getValueType() == MVT::i1) {
824 switch (Cond) {
825 default: assert(0 && "Unknown integer setcc!");
826 case ISD::SETEQ: // X == Y -> (X^Y)^1
827 N1 = getNode(ISD::XOR, MVT::i1,
828 getNode(ISD::XOR, MVT::i1, N1, N2),
829 getConstant(1, MVT::i1));
830 break;
831 case ISD::SETNE: // X != Y --> (X^Y)
832 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
833 break;
834 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
835 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
836 N1 = getNode(ISD::AND, MVT::i1, N2,
837 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
838 break;
839 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
840 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
841 N1 = getNode(ISD::AND, MVT::i1, N1,
842 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
843 break;
844 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
845 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
846 N1 = getNode(ISD::OR, MVT::i1, N2,
847 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
848 break;
849 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
850 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
851 N1 = getNode(ISD::OR, MVT::i1, N1,
852 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
853 break;
854 }
855 if (VT != MVT::i1)
856 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
857 return N1;
858 }
859
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000860 // Could not fold it.
861 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000862}
863
Nate Begemanff663682005-08-13 06:14:17 +0000864SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
865 SDOperand N3, SDOperand N4,
866 ISD::CondCode CC) {
867 MVT::ValueType VT = N3.getValueType();
Nate Begeman1999b4b2005-08-25 20:04:38 +0000868 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman32c392a2005-08-13 06:00:21 +0000869 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
870 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
871 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
872
873 // Check to see if we can simplify the select into an fabs node
874 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
875 // Allow either -0.0 or 0.0
876 if (CFP->getValue() == 0.0) {
877 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
878 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
879 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
880 N1 == N4.getOperand(0))
881 return getNode(ISD::FABS, VT, N1);
882
883 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
884 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
885 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
886 N3.getOperand(0) == N4)
887 return getNode(ISD::FABS, VT, N4);
888 }
889 }
890
Nate Begeman1999b4b2005-08-25 20:04:38 +0000891 // check to see if we're select_cc'ing a select_cc.
892 // this allows us to turn:
893 // select_cc set[eq,ne] (select_cc cc, lhs, rhs, 1, 0), 0, true, false ->
894 // select_cc cc, lhs, rhs, true, false
895 if ((N1C && N1C->isNullValue() && N2.getOpcode() == ISD::SELECT_CC) ||
896 (N2C && N2C->isNullValue() && N1.getOpcode() == ISD::SELECT_CC) &&
897 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
898 SDOperand SCC = N1C ? N2 : N1;
899 ConstantSDNode *SCCT = dyn_cast<ConstantSDNode>(SCC.getOperand(2));
900 ConstantSDNode *SCCF = dyn_cast<ConstantSDNode>(SCC.getOperand(3));
901 if (SCCT && SCCF && SCCF->isNullValue() && SCCT->getValue() == 1ULL) {
902 if (CC == ISD::SETEQ) std::swap(N3, N4);
903 return getNode(ISD::SELECT_CC, N3.getValueType(), SCC.getOperand(0),
904 SCC.getOperand(1), N3, N4, SCC.getOperand(4));
905 }
906 }
907
Nate Begeman32c392a2005-08-13 06:00:21 +0000908 // Check to see if we can perform the "gzip trick", transforming
909 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
910 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
911 MVT::isInteger(N1.getValueType()) &&
912 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
913 MVT::ValueType XType = N1.getValueType();
914 MVT::ValueType AType = N3.getValueType();
915 if (XType >= AType) {
916 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
917 // single-bit constant. FIXME: remove once the dag combiner
918 // exists.
919 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
920 unsigned ShCtV = Log2_64(N3C->getValue());
921 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
922 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
923 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
924 if (XType > AType)
925 Shift = getNode(ISD::TRUNCATE, AType, Shift);
926 return getNode(ISD::AND, AType, Shift, N3);
927 }
928 SDOperand Shift = getNode(ISD::SRA, XType, N1,
929 getConstant(MVT::getSizeInBits(XType)-1,
930 TLI.getShiftAmountTy()));
931 if (XType > AType)
932 Shift = getNode(ISD::TRUNCATE, AType, Shift);
933 return getNode(ISD::AND, AType, Shift, N3);
934 }
935 }
936
Nate Begeman1999b4b2005-08-25 20:04:38 +0000937 // Check to see if this is the equivalent of setcc
Nate Begemancebd4332005-08-24 04:57:57 +0000938 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000939 MVT::ValueType XType = N1.getValueType();
Chris Lattner9d338cf2005-08-25 17:54:58 +0000940 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy()))
Nate Begemancebd4332005-08-24 04:57:57 +0000941 return getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
Chris Lattner9d338cf2005-08-25 17:54:58 +0000942
Nate Begemancebd4332005-08-24 04:57:57 +0000943 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
944 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
Chris Lattner9d338cf2005-08-25 17:54:58 +0000945 TLI.isOperationLegal(ISD::CTLZ, XType)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000946 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
947 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000948 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000949 TLI.getShiftAmountTy()));
950 }
Nate Begemancebd4332005-08-24 04:57:57 +0000951 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
952 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
953 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
954 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
955 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
956 getConstant(MVT::getSizeInBits(XType)-1,
957 TLI.getShiftAmountTy()));
958 }
959 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
960 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
961 SDOperand Sign = getNode(ISD::SRL, XType, N1,
962 getConstant(MVT::getSizeInBits(XType)-1,
963 TLI.getShiftAmountTy()));
964 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
965 }
Nate Begeman7042f152005-08-23 05:41:12 +0000966 }
967
Nate Begeman32c392a2005-08-13 06:00:21 +0000968 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
969 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
970 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
971 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
972 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
973 MVT::ValueType XType = N1.getValueType();
974 if (SubC->isNullValue() && MVT::isInteger(XType)) {
975 SDOperand Shift = getNode(ISD::SRA, XType, N1,
976 getConstant(MVT::getSizeInBits(XType)-1,
977 TLI.getShiftAmountTy()));
978 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
979 Shift);
980 }
981 }
982 }
983
984 // Could not fold it.
985 return SDOperand();
986}
987
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000989///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000990SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
991 SDNode *N = new SDNode(Opcode, VT);
992 AllNodes.push_back(N);
993 return SDOperand(N, 0);
994}
995
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
997 SDOperand Operand) {
998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
999 uint64_t Val = C->getValue();
1000 switch (Opcode) {
1001 default: break;
1002 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001003 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1005 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001006 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1007 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008 }
1009 }
1010
1011 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1012 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001013 case ISD::FNEG:
1014 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001015 case ISD::FP_ROUND:
1016 case ISD::FP_EXTEND:
1017 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001018 case ISD::FP_TO_SINT:
1019 return getConstant((int64_t)C->getValue(), VT);
1020 case ISD::FP_TO_UINT:
1021 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001022 }
1023
1024 unsigned OpOpcode = Operand.Val->getOpcode();
1025 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001026 case ISD::TokenFactor:
1027 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028 case ISD::SIGN_EXTEND:
1029 if (Operand.getValueType() == VT) return Operand; // noop extension
1030 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1031 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1032 break;
1033 case ISD::ZERO_EXTEND:
1034 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001035 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001036 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001037 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001038 case ISD::ANY_EXTEND:
1039 if (Operand.getValueType() == VT) return Operand; // noop extension
1040 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1041 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1042 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1043 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044 case ISD::TRUNCATE:
1045 if (Operand.getValueType() == VT) return Operand; // noop truncate
1046 if (OpOpcode == ISD::TRUNCATE)
1047 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001048 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1049 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001050 // If the source is smaller than the dest, we still need an extend.
1051 if (Operand.Val->getOperand(0).getValueType() < VT)
1052 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1053 else if (Operand.Val->getOperand(0).getValueType() > VT)
1054 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1055 else
1056 return Operand.Val->getOperand(0);
1057 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001059 case ISD::FNEG:
1060 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
1061 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
1062 Operand.Val->getOperand(0));
1063 if (OpOpcode == ISD::FNEG) // --X -> X
1064 return Operand.Val->getOperand(0);
1065 break;
1066 case ISD::FABS:
1067 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1068 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1069 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 }
1071
Chris Lattner43247a12005-08-25 19:12:10 +00001072 SDNode *N;
1073 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1074 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001075 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001076 E = N = new SDNode(Opcode, Operand);
1077 } else {
1078 N = new SDNode(Opcode, Operand);
1079 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001080 N->setValueTypes(VT);
1081 AllNodes.push_back(N);
1082 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001083}
1084
Chris Lattner36019aa2005-04-18 03:48:41 +00001085/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1086/// this predicate to simplify operations downstream. V and Mask are known to
1087/// be the same type.
1088static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
1089 const TargetLowering &TLI) {
1090 unsigned SrcBits;
1091 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001092
Chris Lattner36019aa2005-04-18 03:48:41 +00001093 // If we know the result of a setcc has the top bits zero, use this info.
1094 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001095 case ISD::Constant:
1096 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1097
1098 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001099 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001100 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1101
1102 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001103 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001104 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1105 case ISD::ZERO_EXTEND:
1106 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1107 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
Nate Begeman9f52f282005-08-31 00:43:08 +00001108 case ISD::AssertZext:
1109 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
Nate Begemanfe75a282005-08-31 00:43:49 +00001110 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
Chris Lattner36019aa2005-04-18 03:48:41 +00001111 case ISD::AND:
1112 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001113 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001114 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1115
1116 // FALL THROUGH
1117 case ISD::OR:
1118 case ISD::XOR:
1119 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1120 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1121 case ISD::SELECT:
1122 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1123 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner1d6373c2005-08-24 16:46:55 +00001124 case ISD::SELECT_CC:
1125 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
1126 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001127 case ISD::SRL:
1128 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1129 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1130 uint64_t NewVal = Mask << ShAmt->getValue();
1131 SrcBits = MVT::getSizeInBits(Op.getValueType());
1132 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1133 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1134 }
1135 return false;
1136 case ISD::SHL:
1137 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1138 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1139 uint64_t NewVal = Mask >> ShAmt->getValue();
1140 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1141 }
1142 return false;
Chris Lattner1d6373c2005-08-24 16:46:55 +00001143 case ISD::CTTZ:
1144 case ISD::CTLZ:
1145 case ISD::CTPOP:
1146 // Bit counting instructions can not set the high bits of the result
1147 // register. The max number of bits sets depends on the input.
1148 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
1149
Chris Lattner6ea92792005-04-25 21:03:25 +00001150 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001151 default: break;
1152 }
1153
1154 return false;
1155}
1156
1157
1158
Chris Lattnerc3aae252005-01-07 07:46:32 +00001159SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1160 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001161#ifndef NDEBUG
1162 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001163 case ISD::TokenFactor:
1164 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1165 N2.getValueType() == MVT::Other && "Invalid token factor!");
1166 break;
Chris Lattner76365122005-01-16 02:23:22 +00001167 case ISD::AND:
1168 case ISD::OR:
1169 case ISD::XOR:
1170 case ISD::UDIV:
1171 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001172 case ISD::MULHU:
1173 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001174 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1175 // fall through
1176 case ISD::ADD:
1177 case ISD::SUB:
1178 case ISD::MUL:
1179 case ISD::SDIV:
1180 case ISD::SREM:
1181 assert(N1.getValueType() == N2.getValueType() &&
1182 N1.getValueType() == VT && "Binary operator types must match!");
1183 break;
1184
1185 case ISD::SHL:
1186 case ISD::SRA:
1187 case ISD::SRL:
1188 assert(VT == N1.getValueType() &&
1189 "Shift operators return type must be the same as their first arg");
1190 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001191 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001192 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001193 case ISD::FP_ROUND_INREG: {
1194 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1195 assert(VT == N1.getValueType() && "Not an inreg round!");
1196 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1197 "Cannot FP_ROUND_INREG integer types");
1198 assert(EVT <= VT && "Not rounding down!");
1199 break;
1200 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001201 case ISD::AssertSext:
1202 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001203 case ISD::SIGN_EXTEND_INREG: {
1204 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1205 assert(VT == N1.getValueType() && "Not an inreg extend!");
1206 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1207 "Cannot *_EXTEND_INREG FP types");
1208 assert(EVT <= VT && "Not extending!");
1209 }
1210
Chris Lattner76365122005-01-16 02:23:22 +00001211 default: break;
1212 }
1213#endif
1214
Chris Lattnerc3aae252005-01-07 07:46:32 +00001215 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1216 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1217 if (N1C) {
1218 if (N2C) {
1219 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1220 switch (Opcode) {
1221 case ISD::ADD: return getConstant(C1 + C2, VT);
1222 case ISD::SUB: return getConstant(C1 - C2, VT);
1223 case ISD::MUL: return getConstant(C1 * C2, VT);
1224 case ISD::UDIV:
1225 if (C2) return getConstant(C1 / C2, VT);
1226 break;
1227 case ISD::UREM :
1228 if (C2) return getConstant(C1 % C2, VT);
1229 break;
1230 case ISD::SDIV :
1231 if (C2) return getConstant(N1C->getSignExtended() /
1232 N2C->getSignExtended(), VT);
1233 break;
1234 case ISD::SREM :
1235 if (C2) return getConstant(N1C->getSignExtended() %
1236 N2C->getSignExtended(), VT);
1237 break;
1238 case ISD::AND : return getConstant(C1 & C2, VT);
1239 case ISD::OR : return getConstant(C1 | C2, VT);
1240 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001241 case ISD::SHL : return getConstant(C1 << C2, VT);
1242 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001243 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001244 default: break;
1245 }
1246
1247 } else { // Cannonicalize constant to RHS if commutative
1248 if (isCommutativeBinOp(Opcode)) {
1249 std::swap(N1C, N2C);
1250 std::swap(N1, N2);
1251 }
1252 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001253
1254 switch (Opcode) {
1255 default: break;
1256 case ISD::SHL: // shl 0, X -> 0
1257 if (N1C->isNullValue()) return N1;
1258 break;
1259 case ISD::SRL: // srl 0, X -> 0
1260 if (N1C->isNullValue()) return N1;
1261 break;
1262 case ISD::SRA: // sra -1, X -> -1
1263 if (N1C->isAllOnesValue()) return N1;
1264 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001265 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1266 // Extending a constant? Just return the extended constant.
1267 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1268 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001269 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001270 }
1271
1272 if (N2C) {
1273 uint64_t C2 = N2C->getValue();
1274
1275 switch (Opcode) {
1276 case ISD::ADD:
1277 if (!C2) return N1; // add X, 0 -> X
1278 break;
1279 case ISD::SUB:
1280 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001281 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001282 case ISD::MUL:
1283 if (!C2) return N2; // mul X, 0 -> 0
1284 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1285 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1286
Chris Lattnerb48da392005-01-23 04:39:44 +00001287 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001288 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001289 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001290 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001291 }
1292 break;
1293
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001294 case ISD::MULHU:
1295 case ISD::MULHS:
1296 if (!C2) return N2; // mul X, 0 -> 0
1297
1298 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1299 return getConstant(0, VT);
1300
1301 // Many others could be handled here, including -1, powers of 2, etc.
1302 break;
1303
Chris Lattnerc3aae252005-01-07 07:46:32 +00001304 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001305 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001306 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001307 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001308 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001309 }
1310 break;
1311
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001312 case ISD::SHL:
1313 case ISD::SRL:
1314 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001315 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001316 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001317 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1318 return getNode(ISD::UNDEF, N1.getValueType());
1319 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001320 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001321
1322 if (Opcode == ISD::SRA) {
1323 // If the sign bit is known to be zero, switch this to a SRL.
1324 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001325 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001326 TLI))
1327 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1328 } else {
1329 // If the part left over is known to be zero, the whole thing is zero.
1330 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1331 if (Opcode == ISD::SRL) {
1332 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1333 return getConstant(0, N1.getValueType());
1334 } else if (Opcode == ISD::SHL) {
1335 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1336 return getConstant(0, N1.getValueType());
1337 }
1338 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001339
1340 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1341 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1342 unsigned OpSAC = OpSA->getValue();
1343 if (N1.getOpcode() == ISD::SHL) {
1344 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1345 return getConstant(0, N1.getValueType());
1346 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1347 getConstant(C2+OpSAC, N2.getValueType()));
1348 } else if (N1.getOpcode() == ISD::SRL) {
1349 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1350 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1351 getConstant(~0ULL << OpSAC, VT));
1352 if (C2 > OpSAC) {
1353 return getNode(ISD::SHL, VT, Mask,
1354 getConstant(C2-OpSAC, N2.getValueType()));
1355 } else {
1356 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1357 return getNode(ISD::SRL, VT, Mask,
1358 getConstant(OpSAC-C2, N2.getValueType()));
1359 }
1360 } else if (N1.getOpcode() == ISD::SRA) {
1361 // if C1 == C2, just mask out low bits.
1362 if (C2 == OpSAC)
1363 return getNode(ISD::AND, VT, N1.getOperand(0),
1364 getConstant(~0ULL << C2, VT));
1365 }
1366 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001367 break;
1368
Chris Lattnerc3aae252005-01-07 07:46:32 +00001369 case ISD::AND:
1370 if (!C2) return N2; // X and 0 -> 0
1371 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001372 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001373
Chris Lattner36019aa2005-04-18 03:48:41 +00001374 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1375 return getConstant(0, VT);
1376
Chris Lattner588bbbf2005-04-21 06:28:15 +00001377 {
1378 uint64_t NotC2 = ~C2;
1379 if (VT != MVT::i64)
1380 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1381
1382 if (MaskedValueIsZero(N1, NotC2, TLI))
1383 return N1; // if (X & ~C2) -> 0, the and is redundant
1384 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001385
Chris Lattnerdea29e22005-04-10 01:13:15 +00001386 // FIXME: Should add a corresponding version of this for
1387 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1388 // we don't have yet.
Chris Lattner4ed11b42005-09-02 00:17:32 +00001389 // FIXME: NOW WE DO, add this.
Chris Lattnerdea29e22005-04-10 01:13:15 +00001390
Chris Lattner0f2287b2005-04-13 02:38:18 +00001391 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1392 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001393 // If we are masking out the part of our input that was extended, just
1394 // mask the input to the extension directly.
1395 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001396 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001397 if ((C2 & (~0ULL << ExtendBits)) == 0)
1398 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001399 } else if (N1.getOpcode() == ISD::OR) {
1400 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1401 if ((ORI->getValue() & C2) == C2) {
1402 // If the 'or' is setting all of the bits that we are masking for,
1403 // we know the result of the AND will be the AND mask itself.
1404 return N2;
1405 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001406 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001407 break;
1408 case ISD::OR:
1409 if (!C2)return N1; // X or 0 -> X
1410 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001411 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001412 break;
1413 case ISD::XOR:
1414 if (!C2) return N1; // X xor 0 -> X
Nate Begeman39f60a22005-09-01 23:25:49 +00001415 if (N2C->getValue() == 1 && N1.Val->getOpcode() == ISD::SETCC) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001416 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001417 // !(X op Y) -> (X !op Y)
1418 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001419 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1420 return getSetCC(SetCC->getValueType(0),
1421 SetCC->getOperand(0), SetCC->getOperand(1),
1422 ISD::getSetCCInverse(CC, isInteger));
Nate Begeman39f60a22005-09-01 23:25:49 +00001423 } else if (N2C->isAllOnesValue()) {
1424 if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001425 SDNode *Op = N1.Val;
1426 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1427 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1428 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1429 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1430 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1431 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1432 if (Op->getOpcode() == ISD::AND)
1433 return getNode(ISD::OR, VT, LHS, RHS);
1434 return getNode(ISD::AND, VT, LHS, RHS);
1435 }
1436 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001437 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001438 }
1439 break;
1440 }
1441
1442 // Reassociate ((X op C1) op C2) if possible.
1443 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1444 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001445 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001446 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1447 }
1448
1449 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1450 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001451 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001452 if (N2CFP) {
1453 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1454 switch (Opcode) {
1455 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1456 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1457 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1458 case ISD::SDIV:
1459 if (C2) return getConstantFP(C1 / C2, VT);
1460 break;
1461 case ISD::SREM :
1462 if (C2) return getConstantFP(fmod(C1, C2), VT);
1463 break;
1464 default: break;
1465 }
1466
1467 } else { // Cannonicalize constant to RHS if commutative
1468 if (isCommutativeBinOp(Opcode)) {
1469 std::swap(N1CFP, N2CFP);
1470 std::swap(N1, N2);
1471 }
1472 }
1473
Chris Lattner15e4b012005-07-10 00:07:11 +00001474 if (Opcode == ISD::FP_ROUND_INREG)
1475 return getNode(ISD::FP_EXTEND, VT,
1476 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1477 }
1478
Chris Lattnerc3aae252005-01-07 07:46:32 +00001479 // Finally, fold operations that do not require constants.
1480 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001481 case ISD::TokenFactor:
1482 if (N1.getOpcode() == ISD::EntryToken)
1483 return N2;
1484 if (N2.getOpcode() == ISD::EntryToken)
1485 return N1;
1486 break;
1487
Chris Lattnerc3aae252005-01-07 07:46:32 +00001488 case ISD::AND:
1489 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001490 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1491 SDNode *LHS = N1.Val, *RHS = N2.Val;
1492 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1493 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1494 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1495 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001496
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001497 if (LR == RR && isa<ConstantSDNode>(LR) &&
1498 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1499 // (X != 0) | (Y != 0) -> (X|Y != 0)
1500 // (X == 0) & (Y == 0) -> (X|Y == 0)
1501 // (X < 0) | (Y < 0) -> (X|Y < 0)
1502 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1503 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1504 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1505 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1506 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1507 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001508
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001509 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1510 // (X == -1) & (Y == -1) -> (X&Y == -1)
1511 // (X != -1) | (Y != -1) -> (X&Y != -1)
1512 // (X > -1) | (Y > -1) -> (X&Y > -1)
1513 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1514 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1515 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1516 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1517 LR, Op2);
1518 // (X > -1) & (Y > -1) -> (X|Y > -1)
1519 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1520 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1521 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001522 }
1523 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001524
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001525 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1526 if (LL == RR && LR == RL) {
1527 Op2 = ISD::getSetCCSwappedOperands(Op2);
1528 goto MatchedBackwards;
1529 }
1530
1531 if (LL == RL && LR == RR) {
1532 MatchedBackwards:
1533 ISD::CondCode Result;
1534 bool isInteger = MVT::isInteger(LL.getValueType());
1535 if (Opcode == ISD::OR)
1536 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1537 else
1538 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1539
1540 if (Result != ISD::SETCC_INVALID)
1541 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1542 }
1543 }
1544
Chris Lattner7467c9b2005-04-18 04:11:19 +00001545 // and/or zext(a), zext(b) -> zext(and/or a, b)
1546 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1547 N2.getOpcode() == ISD::ZERO_EXTEND &&
1548 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1549 return getNode(ISD::ZERO_EXTEND, VT,
1550 getNode(Opcode, N1.getOperand(0).getValueType(),
1551 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001552 break;
1553 case ISD::XOR:
1554 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1555 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001556 case ISD::ADD:
1557 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1558 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1559 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1560 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001561 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1562 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1563 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1564 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1565 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1566 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001567 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1568 !MVT::isFloatingPoint(N2.getValueType()))
1569 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001570 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001571 case ISD::SUB:
1572 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001573 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001574 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001575 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001576 if (N1.Val->getOperand(1) == N2 &&
1577 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001578 return N1.Val->getOperand(0); // (A+B)-B == A
1579 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001580 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1581 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001582 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001583 case ISD::FP_ROUND_INREG:
1584 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1585 break;
1586 case ISD::SIGN_EXTEND_INREG: {
1587 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1588 if (EVT == VT) return N1; // Not actually extending
1589
1590 // If we are sign extending an extension, use the original source.
Nate Begeman56eb8682005-08-30 02:44:00 +00001591 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG ||
1592 N1.getOpcode() == ISD::AssertSext)
Chris Lattner15e4b012005-07-10 00:07:11 +00001593 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1594 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001595
Chris Lattner15e4b012005-07-10 00:07:11 +00001596 // If we are sign extending a sextload, return just the load.
1597 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001598 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Nate Begeman56eb8682005-08-30 02:44:00 +00001599 return N1;
Chris Lattner15e4b012005-07-10 00:07:11 +00001600
1601 // If we are extending the result of a setcc, and we already know the
1602 // contents of the top bits, eliminate the extension.
1603 if (N1.getOpcode() == ISD::SETCC &&
1604 TLI.getSetCCResultContents() ==
1605 TargetLowering::ZeroOrNegativeOneSetCCResult)
1606 return N1;
Nate Begeman56eb8682005-08-30 02:44:00 +00001607
Chris Lattner15e4b012005-07-10 00:07:11 +00001608 // If we are sign extending the result of an (and X, C) operation, and we
1609 // know the extended bits are zeros already, don't do the extend.
1610 if (N1.getOpcode() == ISD::AND)
1611 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1612 uint64_t Mask = N1C->getValue();
1613 unsigned NumBits = MVT::getSizeInBits(EVT);
1614 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1615 return N1;
1616 }
1617 break;
1618 }
1619
Nate Begemaneea805e2005-04-13 21:23:31 +00001620 // FIXME: figure out how to safely handle things like
1621 // int foo(int x) { return 1 << (x & 255); }
1622 // int bar() { return foo(256); }
1623#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001624 case ISD::SHL:
1625 case ISD::SRL:
1626 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001627 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001628 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001629 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001630 else if (N2.getOpcode() == ISD::AND)
1631 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1632 // If the and is only masking out bits that cannot effect the shift,
1633 // eliminate the and.
1634 unsigned NumBits = MVT::getSizeInBits(VT);
1635 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1636 return getNode(Opcode, VT, N1, N2.getOperand(0));
1637 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001638 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001639#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001640 }
1641
Chris Lattner27e9b412005-05-11 18:57:39 +00001642 // Memoize this node if possible.
1643 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001644 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1645 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001646 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1647 if (BON) return SDOperand(BON, 0);
1648
1649 BON = N = new SDNode(Opcode, N1, N2);
1650 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001651 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001652 }
1653
Chris Lattner3e011362005-05-14 07:45:46 +00001654 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001655 AllNodes.push_back(N);
1656 return SDOperand(N, 0);
1657}
1658
Chris Lattner88de6e72005-05-12 00:17:04 +00001659// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001660// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001661void SDNode::setAdjCallChain(SDOperand N) {
1662 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001663 assert((getOpcode() == ISD::CALLSEQ_START ||
1664 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001665
1666 Operands[0].Val->removeUser(this);
1667 Operands[0] = N;
1668 N.Val->Uses.push_back(this);
1669}
1670
1671
1672
Chris Lattnerc3aae252005-01-07 07:46:32 +00001673SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001674 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001675 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001676 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1677 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001678 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001679
1680 // Loads have a token chain.
1681 N->setValueTypes(VT, MVT::Other);
1682 AllNodes.push_back(N);
1683 return SDOperand(N, 0);
1684}
1685
Chris Lattner5f056bf2005-07-10 01:55:33 +00001686
1687SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1688 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1689 MVT::ValueType EVT) {
1690 std::vector<SDOperand> Ops;
1691 Ops.reserve(4);
1692 Ops.push_back(Chain);
1693 Ops.push_back(Ptr);
1694 Ops.push_back(SV);
1695 Ops.push_back(getValueType(EVT));
1696 std::vector<MVT::ValueType> VTs;
1697 VTs.reserve(2);
1698 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1699 return getNode(Opcode, VTs, Ops);
1700}
1701
Chris Lattnerc3aae252005-01-07 07:46:32 +00001702SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1703 SDOperand N1, SDOperand N2, SDOperand N3) {
1704 // Perform various simplifications.
1705 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1706 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1707 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1708 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001709 case ISD::SETCC: {
1710 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001711 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001712 if (Simp.Val) return Simp;
1713 break;
1714 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001715 case ISD::SELECT:
1716 if (N1C)
1717 if (N1C->getValue())
1718 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001719 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001720 return N3; // select false, X, Y -> Y
1721
1722 if (N2 == N3) return N2; // select C, X, X -> X
1723
1724 if (VT == MVT::i1) { // Boolean SELECT
1725 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001726 if (N2C->getValue()) // select C, 1, X -> C | X
1727 return getNode(ISD::OR, VT, N1, N3);
1728 else // select C, 0, X -> ~C & X
1729 return getNode(ISD::AND, VT,
1730 getNode(ISD::XOR, N1.getValueType(), N1,
1731 getConstant(1, N1.getValueType())), N3);
1732 } else if (N3C) {
1733 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1734 return getNode(ISD::OR, VT,
1735 getNode(ISD::XOR, N1.getValueType(), N1,
1736 getConstant(1, N1.getValueType())), N2);
1737 else // select C, X, 0 -> C & X
1738 return getNode(ISD::AND, VT, N1, N2);
1739 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001740
1741 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1742 return getNode(ISD::OR, VT, N1, N3);
1743 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1744 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001745 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001746 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001747 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1748 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001749 if (Simp.Val) return Simp;
1750 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001752 case ISD::BRCOND:
1753 if (N2C)
1754 if (N2C->getValue()) // Unconditional branch
1755 return getNode(ISD::BR, MVT::Other, N1, N3);
1756 else
1757 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001758 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001759 }
1760
Chris Lattner385328c2005-05-14 07:42:29 +00001761 std::vector<SDOperand> Ops;
1762 Ops.reserve(3);
1763 Ops.push_back(N1);
1764 Ops.push_back(N2);
1765 Ops.push_back(N3);
1766
Chris Lattner43247a12005-08-25 19:12:10 +00001767 // Memoize node if it doesn't produce a flag.
1768 SDNode *N;
1769 if (VT != MVT::Flag) {
1770 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1771 if (E) return SDOperand(E, 0);
1772 E = N = new SDNode(Opcode, N1, N2, N3);
1773 } else {
1774 N = new SDNode(Opcode, N1, N2, N3);
1775 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001776 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001777 AllNodes.push_back(N);
1778 return SDOperand(N, 0);
1779}
1780
1781SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001782 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001783 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001784 std::vector<SDOperand> Ops;
1785 Ops.reserve(4);
1786 Ops.push_back(N1);
1787 Ops.push_back(N2);
1788 Ops.push_back(N3);
1789 Ops.push_back(N4);
1790 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001791}
1792
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001793SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1794 SDOperand N1, SDOperand N2, SDOperand N3,
1795 SDOperand N4, SDOperand N5) {
1796 std::vector<SDOperand> Ops;
1797 Ops.reserve(5);
1798 Ops.push_back(N1);
1799 Ops.push_back(N2);
1800 Ops.push_back(N3);
1801 Ops.push_back(N4);
1802 Ops.push_back(N5);
1803 return getNode(Opcode, VT, Ops);
1804}
1805
1806
Chris Lattner0437cdd2005-05-09 04:14:13 +00001807SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001808 assert((!V || isa<PointerType>(V->getType())) &&
1809 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001810 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1811 if (N) return SDOperand(N, 0);
1812
1813 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001814 AllNodes.push_back(N);
1815 return SDOperand(N, 0);
1816}
1817
1818SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001819 std::vector<SDOperand> &Ops) {
1820 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001821 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001822 case 1: return getNode(Opcode, VT, Ops[0]);
1823 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1824 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001825 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001826 }
Chris Lattneref847df2005-04-09 03:27:28 +00001827
Chris Lattner89c34632005-05-14 06:20:26 +00001828 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001829 switch (Opcode) {
1830 default: break;
1831 case ISD::BRCONDTWOWAY:
1832 if (N1C)
1833 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001834 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001835 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001836 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001837 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001838 case ISD::BRTWOWAY_CC:
1839 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1840 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1841 "LHS and RHS of comparison must have same type!");
1842 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001843 case ISD::TRUNCSTORE: {
1844 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1845 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1846#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1847 // If this is a truncating store of a constant, convert to the desired type
1848 // and store it instead.
1849 if (isa<Constant>(Ops[0])) {
1850 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1851 if (isa<Constant>(Op))
1852 N1 = Op;
1853 }
1854 // Also for ConstantFP?
1855#endif
1856 if (Ops[0].getValueType() == EVT) // Normal store?
1857 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1858 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1859 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1860 "Can't do FP-INT conversion!");
1861 break;
1862 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001863 case ISD::SELECT_CC: {
1864 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1865 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1866 "LHS and RHS of condition must have same type!");
1867 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1868 "True and False arms of SelectCC must have same type!");
1869 assert(Ops[2].getValueType() == VT &&
1870 "select_cc node must be of same type as true and false value!");
1871 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1872 cast<CondCodeSDNode>(Ops[4])->get());
1873 if (Simp.Val) return Simp;
1874 break;
1875 }
1876 case ISD::BR_CC: {
1877 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1878 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1879 "LHS/RHS of comparison should match types!");
1880 // Use SimplifySetCC to simplify SETCC's.
1881 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
1882 cast<CondCodeSDNode>(Ops[1])->get());
1883 if (Simp.Val) {
1884 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
1885 if (C->getValue() & 1) // Unconditional branch
1886 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
1887 else
1888 return Ops[0]; // Unconditional Fall through
1889 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
1890 Ops[2] = Simp.getOperand(0);
1891 Ops[3] = Simp.getOperand(1);
1892 Ops[1] = Simp.getOperand(2);
1893 }
1894 }
1895 break;
1896 }
Chris Lattneref847df2005-04-09 03:27:28 +00001897 }
1898
Chris Lattner385328c2005-05-14 07:42:29 +00001899 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001900 SDNode *N;
1901 if (VT != MVT::Flag) {
1902 SDNode *&E =
1903 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1904 if (E) return SDOperand(E, 0);
1905 E = N = new SDNode(Opcode, Ops);
1906 } else {
1907 N = new SDNode(Opcode, Ops);
1908 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001909 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001910 AllNodes.push_back(N);
1911 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001912}
1913
Chris Lattner89c34632005-05-14 06:20:26 +00001914SDOperand SelectionDAG::getNode(unsigned Opcode,
1915 std::vector<MVT::ValueType> &ResultTys,
1916 std::vector<SDOperand> &Ops) {
1917 if (ResultTys.size() == 1)
1918 return getNode(Opcode, ResultTys[0], Ops);
1919
Chris Lattner5f056bf2005-07-10 01:55:33 +00001920 switch (Opcode) {
1921 case ISD::EXTLOAD:
1922 case ISD::SEXTLOAD:
1923 case ISD::ZEXTLOAD: {
1924 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1925 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1926 // If they are asking for an extending load from/to the same thing, return a
1927 // normal load.
1928 if (ResultTys[0] == EVT)
1929 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1930 assert(EVT < ResultTys[0] &&
1931 "Should only be an extending load, not truncating!");
1932 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1933 "Cannot sign/zero extend a FP load!");
1934 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1935 "Cannot convert from FP to Int or Int -> FP!");
1936 break;
1937 }
1938
Chris Lattnere89083a2005-05-14 07:25:05 +00001939 // FIXME: figure out how to safely handle things like
1940 // int foo(int x) { return 1 << (x & 255); }
1941 // int bar() { return foo(256); }
1942#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001943 case ISD::SRA_PARTS:
1944 case ISD::SRL_PARTS:
1945 case ISD::SHL_PARTS:
1946 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001947 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001948 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1949 else if (N3.getOpcode() == ISD::AND)
1950 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1951 // If the and is only masking out bits that cannot effect the shift,
1952 // eliminate the and.
1953 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1954 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1955 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1956 }
1957 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001958#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001959 }
Chris Lattner89c34632005-05-14 06:20:26 +00001960
Chris Lattner43247a12005-08-25 19:12:10 +00001961 // Memoize the node unless it returns a flag.
1962 SDNode *N;
1963 if (ResultTys.back() != MVT::Flag) {
1964 SDNode *&E =
1965 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1966 if (E) return SDOperand(E, 0);
1967 E = N = new SDNode(Opcode, Ops);
1968 } else {
1969 N = new SDNode(Opcode, Ops);
1970 }
Chris Lattner89c34632005-05-14 06:20:26 +00001971 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001972 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001973 return SDOperand(N, 0);
1974}
1975
Chris Lattner149c58c2005-08-16 18:17:10 +00001976
1977/// SelectNodeTo - These are used for target selectors to *mutate* the
1978/// specified node to have the specified return type, Target opcode, and
1979/// operands. Note that target opcodes are stored as
1980/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001981void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1982 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00001983 RemoveNodeFromCSEMaps(N);
1984 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1985 N->setValueTypes(VT);
1986}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001987void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1988 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00001989 RemoveNodeFromCSEMaps(N);
1990 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1991 N->setValueTypes(VT);
1992 N->setOperands(Op1);
1993}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001994void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1995 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001996 SDOperand Op2) {
1997 RemoveNodeFromCSEMaps(N);
1998 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1999 N->setValueTypes(VT);
2000 N->setOperands(Op1, Op2);
2001}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002002void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00002003 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002004 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00002005 RemoveNodeFromCSEMaps(N);
2006 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2007 N->setValueTypes(VT1, VT2);
2008 N->setOperands(Op1, Op2);
2009}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002010void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2011 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00002012 SDOperand Op2, SDOperand Op3) {
2013 RemoveNodeFromCSEMaps(N);
2014 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2015 N->setValueTypes(VT);
2016 N->setOperands(Op1, Op2, Op3);
2017}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002018void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2019 MVT::ValueType VT1, MVT::ValueType VT2,
2020 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002021 RemoveNodeFromCSEMaps(N);
2022 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2023 N->setValueTypes(VT1, VT2);
2024 N->setOperands(Op1, Op2, Op3);
2025}
2026
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002027void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2028 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00002029 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
2030 RemoveNodeFromCSEMaps(N);
2031 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2032 N->setValueTypes(VT);
2033 N->setOperands(Op1, Op2, Op3, Op4);
2034}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002035void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2036 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00002037 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2038 SDOperand Op5) {
2039 RemoveNodeFromCSEMaps(N);
2040 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2041 N->setValueTypes(VT);
2042 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2043}
Chris Lattner149c58c2005-08-16 18:17:10 +00002044
Chris Lattner8b8749f2005-08-17 19:00:20 +00002045/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2046/// This can cause recursive merging of nodes in the DAG.
2047///
Chris Lattner8b52f212005-08-26 18:36:28 +00002048/// This version assumes From/To have a single result value.
2049///
2050void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN) {
2051 SDNode *From = FromN.Val, *To = ToN.Val;
2052 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2053 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002054 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002055
Chris Lattner8b8749f2005-08-17 19:00:20 +00002056 while (!From->use_empty()) {
2057 // Process users until they are all gone.
2058 SDNode *U = *From->use_begin();
2059
2060 // This node is about to morph, remove its old self from the CSE maps.
2061 RemoveNodeFromCSEMaps(U);
2062
2063 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2064 if (U->getOperand(i).Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002065 From->removeUser(U);
2066 U->Operands[i].Val = To;
2067 To->addUser(U);
2068 }
2069
2070 // Now that we have modified U, add it back to the CSE maps. If it already
2071 // exists there, recursively merge the results together.
2072 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2073 ReplaceAllUsesWith(U, Existing);
2074 // U is now dead.
2075 }
2076}
2077
2078/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2079/// This can cause recursive merging of nodes in the DAG.
2080///
2081/// This version assumes From/To have matching types and numbers of result
2082/// values.
2083///
2084void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
2085 assert(From != To && "Cannot replace uses of with self");
2086 assert(From->getNumValues() == To->getNumValues() &&
2087 "Cannot use this version of ReplaceAllUsesWith!");
2088 if (From->getNumValues() == 1) { // If possible, use the faster version.
2089 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0));
2090 return;
2091 }
2092
2093 while (!From->use_empty()) {
2094 // Process users until they are all gone.
2095 SDNode *U = *From->use_begin();
2096
2097 // This node is about to morph, remove its old self from the CSE maps.
2098 RemoveNodeFromCSEMaps(U);
2099
2100 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2101 if (U->getOperand(i).Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002102 From->removeUser(U);
2103 U->Operands[i].Val = To;
2104 To->addUser(U);
2105 }
2106
2107 // Now that we have modified U, add it back to the CSE maps. If it already
2108 // exists there, recursively merge the results together.
2109 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2110 ReplaceAllUsesWith(U, Existing);
Chris Lattner8b52f212005-08-26 18:36:28 +00002111 // U is now dead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002112 }
2113}
2114
Chris Lattner8b52f212005-08-26 18:36:28 +00002115/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2116/// This can cause recursive merging of nodes in the DAG.
2117///
2118/// This version can replace From with any result values. To must match the
2119/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002120void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2121 const std::vector<SDOperand> &To) {
2122 assert(From->getNumValues() == To.size() &&
2123 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002124 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002125 // Degenerate case handled above.
Chris Lattner8b52f212005-08-26 18:36:28 +00002126 ReplaceAllUsesWith(SDOperand(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002127 return;
2128 }
2129
2130 while (!From->use_empty()) {
2131 // Process users until they are all gone.
2132 SDNode *U = *From->use_begin();
2133
2134 // This node is about to morph, remove its old self from the CSE maps.
2135 RemoveNodeFromCSEMaps(U);
2136
2137 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2138 if (U->getOperand(i).Val == From) {
2139 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002140 From->removeUser(U);
2141 U->Operands[i] = ToOp;
2142 ToOp.Val->addUser(U);
2143 }
2144
2145 // Now that we have modified U, add it back to the CSE maps. If it already
2146 // exists there, recursively merge the results together.
2147 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2148 ReplaceAllUsesWith(U, Existing);
2149 // U is now dead.
2150 }
2151}
2152
2153
Jim Laskey58b968b2005-08-17 20:08:02 +00002154//===----------------------------------------------------------------------===//
2155// SDNode Class
2156//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002157
Chris Lattner5c884562005-01-12 18:37:47 +00002158/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2159/// indicated value. This method ignores uses of other values defined by this
2160/// operation.
2161bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2162 assert(Value < getNumValues() && "Bad value!");
2163
2164 // If there is only one value, this is easy.
2165 if (getNumValues() == 1)
2166 return use_size() == NUses;
2167 if (Uses.size() < NUses) return false;
2168
2169 SDOperand TheValue(this, Value);
2170
2171 std::set<SDNode*> UsersHandled;
2172
2173 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2174 UI != E; ++UI) {
2175 SDNode *User = *UI;
2176 if (User->getNumOperands() == 1 ||
2177 UsersHandled.insert(User).second) // First time we've seen this?
2178 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2179 if (User->getOperand(i) == TheValue) {
2180 if (NUses == 0)
2181 return false; // too many uses
2182 --NUses;
2183 }
2184 }
2185
2186 // Found exactly the right number of uses?
2187 return NUses == 0;
2188}
2189
2190
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002191const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002192 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002193 default:
2194 if (getOpcode() < ISD::BUILTIN_OP_END)
2195 return "<<Unknown DAG Node>>";
2196 else {
2197 if (G)
2198 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2199 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2200 return "<<Unknown Target Node>>";
2201 }
2202
Andrew Lenharth95762122005-03-31 21:24:06 +00002203 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002204 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002205 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002206 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002207 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002208 case ISD::AssertSext: return "AssertSext";
2209 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002210 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002211 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002212 case ISD::ConstantFP: return "ConstantFP";
2213 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002214 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002215 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002216 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002217 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002218 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002219 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002220 case ISD::ConstantPool: return "ConstantPool";
2221 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002222 case ISD::CopyToReg: return "CopyToReg";
2223 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002224 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002225 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002226
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002227 // Unary operators
2228 case ISD::FABS: return "fabs";
2229 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002230 case ISD::FSQRT: return "fsqrt";
2231 case ISD::FSIN: return "fsin";
2232 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002233
2234 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002235 case ISD::ADD: return "add";
2236 case ISD::SUB: return "sub";
2237 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002238 case ISD::MULHU: return "mulhu";
2239 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002240 case ISD::SDIV: return "sdiv";
2241 case ISD::UDIV: return "udiv";
2242 case ISD::SREM: return "srem";
2243 case ISD::UREM: return "urem";
2244 case ISD::AND: return "and";
2245 case ISD::OR: return "or";
2246 case ISD::XOR: return "xor";
2247 case ISD::SHL: return "shl";
2248 case ISD::SRA: return "sra";
2249 case ISD::SRL: return "srl";
2250
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002251 case ISD::SETCC: return "setcc";
2252 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002253 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002254 case ISD::ADD_PARTS: return "add_parts";
2255 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002256 case ISD::SHL_PARTS: return "shl_parts";
2257 case ISD::SRA_PARTS: return "sra_parts";
2258 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002259
Chris Lattner7f644642005-04-28 21:44:03 +00002260 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002261 case ISD::SIGN_EXTEND: return "sign_extend";
2262 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002263 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002264 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002265 case ISD::TRUNCATE: return "truncate";
2266 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002267 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002268 case ISD::FP_EXTEND: return "fp_extend";
2269
2270 case ISD::SINT_TO_FP: return "sint_to_fp";
2271 case ISD::UINT_TO_FP: return "uint_to_fp";
2272 case ISD::FP_TO_SINT: return "fp_to_sint";
2273 case ISD::FP_TO_UINT: return "fp_to_uint";
2274
2275 // Control flow instructions
2276 case ISD::BR: return "br";
2277 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002278 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002279 case ISD::BR_CC: return "br_cc";
2280 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002281 case ISD::RET: return "ret";
2282 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002283 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002284 case ISD::CALLSEQ_START: return "callseq_start";
2285 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002286
2287 // Other operators
2288 case ISD::LOAD: return "load";
2289 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002290 case ISD::EXTLOAD: return "extload";
2291 case ISD::SEXTLOAD: return "sextload";
2292 case ISD::ZEXTLOAD: return "zextload";
2293 case ISD::TRUNCSTORE: return "truncstore";
2294
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002295 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2296 case ISD::EXTRACT_ELEMENT: return "extract_element";
2297 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002298 case ISD::MEMSET: return "memset";
2299 case ISD::MEMCPY: return "memcpy";
2300 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002301
Chris Lattner276260b2005-05-11 04:50:30 +00002302 // Bit counting
2303 case ISD::CTPOP: return "ctpop";
2304 case ISD::CTTZ: return "cttz";
2305 case ISD::CTLZ: return "ctlz";
2306
2307 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002308 case ISD::READPORT: return "readport";
2309 case ISD::WRITEPORT: return "writeport";
2310 case ISD::READIO: return "readio";
2311 case ISD::WRITEIO: return "writeio";
2312
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002313 case ISD::CONDCODE:
2314 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002315 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002316 case ISD::SETOEQ: return "setoeq";
2317 case ISD::SETOGT: return "setogt";
2318 case ISD::SETOGE: return "setoge";
2319 case ISD::SETOLT: return "setolt";
2320 case ISD::SETOLE: return "setole";
2321 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002322
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002323 case ISD::SETO: return "seto";
2324 case ISD::SETUO: return "setuo";
2325 case ISD::SETUEQ: return "setue";
2326 case ISD::SETUGT: return "setugt";
2327 case ISD::SETUGE: return "setuge";
2328 case ISD::SETULT: return "setult";
2329 case ISD::SETULE: return "setule";
2330 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002331
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002332 case ISD::SETEQ: return "seteq";
2333 case ISD::SETGT: return "setgt";
2334 case ISD::SETGE: return "setge";
2335 case ISD::SETLT: return "setlt";
2336 case ISD::SETLE: return "setle";
2337 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002338 }
2339 }
2340}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002341
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002342void SDNode::dump() const { dump(0); }
2343void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002344 std::cerr << (void*)this << ": ";
2345
2346 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2347 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002348 if (getValueType(i) == MVT::Other)
2349 std::cerr << "ch";
2350 else
2351 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002352 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002353 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002354
2355 std::cerr << " ";
2356 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2357 if (i) std::cerr << ", ";
2358 std::cerr << (void*)getOperand(i).Val;
2359 if (unsigned RN = getOperand(i).ResNo)
2360 std::cerr << ":" << RN;
2361 }
2362
2363 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2364 std::cerr << "<" << CSDN->getValue() << ">";
2365 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2366 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002367 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002368 dyn_cast<GlobalAddressSDNode>(this)) {
2369 std::cerr << "<";
2370 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002371 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002372 std::cerr << "<" << FIDN->getIndex() << ">";
2373 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002374 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002375 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002376 std::cerr << "<";
2377 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2378 if (LBB)
2379 std::cerr << LBB->getName() << " ";
2380 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002381 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002382 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2383 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2384 } else {
2385 std::cerr << " #" << R->getReg();
2386 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002387 } else if (const ExternalSymbolSDNode *ES =
2388 dyn_cast<ExternalSymbolSDNode>(this)) {
2389 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002390 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2391 if (M->getValue())
2392 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2393 else
2394 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002395 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2396 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002397 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002398}
2399
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002400static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002401 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2402 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002403 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002404 else
2405 std::cerr << "\n" << std::string(indent+2, ' ')
2406 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002407
Chris Lattnerea946cd2005-01-09 20:38:33 +00002408
2409 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002410 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002411}
2412
Chris Lattnerc3aae252005-01-07 07:46:32 +00002413void SelectionDAG::dump() const {
2414 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002415 std::vector<SDNode*> Nodes(AllNodes);
2416 std::sort(Nodes.begin(), Nodes.end());
2417
2418 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002419 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002420 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002421 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002422
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002423 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002424
Chris Lattnerc3aae252005-01-07 07:46:32 +00002425 std::cerr << "\n\n";
2426}
2427