blob: 3fdc7fe995f8624f34a9a5e2736d96fe3bd342e2 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner061a1ea2005-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"
Reid Spencereb04d9b2004-07-04 12:19:56 +000019#include <iostream>
Chris Lattner9c667932005-01-07 21:09:16 +000020#include <set>
Chris Lattner061a1ea2005-01-07 07:46:32 +000021#include <cmath>
Chris Lattner560b5e42004-06-02 04:28:06 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner061a1ea2005-01-07 07:46:32 +000024/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
25/// when given the operation for (X op Y).
26ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
27 // To perform this operation, we just need to swap the L and G bits of the
28 // operation.
29 unsigned OldL = (Operation >> 2) & 1;
30 unsigned OldG = (Operation >> 1) & 1;
31 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
32 (OldL << 1) | // New G bit
33 (OldG << 2)); // New L bit.
34}
35
36/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
37/// 'op' is a valid SetCC operation.
38ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
39 unsigned Operation = Op;
40 if (isInteger)
41 Operation ^= 7; // Flip L, G, E bits, but not U.
42 else
43 Operation ^= 15; // Flip all of the condition bits.
44 if (Operation > ISD::SETTRUE2)
45 Operation &= ~8; // Don't let N and U bits get set.
46 return ISD::CondCode(Operation);
47}
48
49
50/// isSignedOp - For an integer comparison, return 1 if the comparison is a
51/// signed operation and 2 if the result is an unsigned comparison. Return zero
52/// if the operation does not depend on the sign of the input (setne and seteq).
53static int isSignedOp(ISD::CondCode Opcode) {
54 switch (Opcode) {
55 default: assert(0 && "Illegal integer setcc operation!");
56 case ISD::SETEQ:
57 case ISD::SETNE: return 0;
58 case ISD::SETLT:
59 case ISD::SETLE:
60 case ISD::SETGT:
61 case ISD::SETGE: return 1;
62 case ISD::SETULT:
63 case ISD::SETULE:
64 case ISD::SETUGT:
65 case ISD::SETUGE: return 2;
66 }
67}
68
69/// getSetCCOrOperation - Return the result of a logical OR between different
70/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
71/// returns SETCC_INVALID if it is not possible to represent the resultant
72/// comparison.
73ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
74 bool isInteger) {
75 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
76 // Cannot fold a signed integer setcc with an unsigned integer setcc.
77 return ISD::SETCC_INVALID;
78
79 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
80
81 // If the N and U bits get set then the resultant comparison DOES suddenly
82 // care about orderedness, and is true when ordered.
83 if (Op > ISD::SETTRUE2)
84 Op &= ~16; // Clear the N bit.
85 return ISD::CondCode(Op);
86}
87
88/// getSetCCAndOperation - Return the result of a logical AND between different
89/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
90/// function returns zero if it is not possible to represent the resultant
91/// comparison.
92ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
93 bool isInteger) {
94 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
95 // Cannot fold a signed setcc with an unsigned setcc.
96 return ISD::SETCC_INVALID;
97
98 // Combine all of the condition bits.
99 return ISD::CondCode(Op1 & Op2);
100}
101
Chris Lattner9c667932005-01-07 21:09:16 +0000102/// RemoveDeadNodes - This method deletes all unreachable nodes in the
103/// SelectionDAG, including nodes (like loads) that have uses of their token
104/// chain but no other uses and no side effect. If a node is passed in as an
105/// argument, it is used as the seed for node deletion.
106void SelectionDAG::RemoveDeadNodes(SDNode *N) {
107 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
108
109 // Create a dummy node (which is not added to allnodes), that adds a reference
110 // to the root node, preventing it from being deleted.
111 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
112
113 DeleteNodeIfDead(N, &AllNodeSet);
114
115 Restart:
116 unsigned NumNodes = AllNodeSet.size();
117 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
118 I != E; ++I) {
119 // Try to delete this node.
120 DeleteNodeIfDead(*I, &AllNodeSet);
121
122 // If we actually deleted any nodes, do not use invalid iterators in
123 // AllNodeSet.
124 if (AllNodeSet.size() != NumNodes)
125 goto Restart;
126 }
127
128 // Restore AllNodes.
129 if (AllNodes.size() != NumNodes)
130 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
131
132 // If the root changed (e.g. it was a dead load, update the root).
133 setRoot(DummyNode->getOperand(0));
134
135 // Now that we are done with the dummy node, delete it.
136 DummyNode->getOperand(0).Val->removeUser(DummyNode);
137 delete DummyNode;
138}
139
140void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
141 if (!N->use_empty())
142 return;
143
144 // Okay, we really are going to delete this node. First take this out of the
145 // appropriate CSE map.
146 switch (N->getOpcode()) {
147 case ISD::Constant:
148 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
149 N->getValueType(0)));
150 break;
151 case ISD::ConstantFP:
152 ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(),
153 N->getValueType(0)));
154 break;
155 case ISD::GlobalAddress:
156 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
157 break;
158 case ISD::FrameIndex:
159 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
160 break;
161 case ISD::ConstantPool:
162 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
163 break;
164 case ISD::BasicBlock:
165 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
166 break;
167 case ISD::ExternalSymbol:
168 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
169 break;
170
171 case ISD::LOAD:
172 Loads.erase(std::make_pair(N->getOperand(1),
173 std::make_pair(N->getOperand(0),
174 N->getValueType(0))));
175 break;
176 case ISD::SETCC:
177 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
178 N->getOperand(1)),
179 cast<SetCCSDNode>(N)->getCondition()));
180 break;
181 default:
182 if (N->getNumOperands() == 1)
183 UnaryOps.erase(std::make_pair(N->getOpcode(),
184 std::make_pair(N->getOperand(0),
185 N->getValueType(0))));
186 else if (N->getNumOperands() == 2)
187 BinaryOps.erase(std::make_pair(N->getOpcode(),
188 std::make_pair(N->getOperand(0),
189 N->getOperand(1))));
190 break;
191 }
192
193 // Next, brutally remove the operand list.
194 std::vector<SDNode*> Operands;
195 while (!N->Operands.empty()) {
196 SDOperand O = N->Operands.back();
197 N->Operands.pop_back();
198 Operands.push_back(O.Val);
199 O.Val->removeUser(N);
200 }
201
202 // Remove the node from the nodes set and delete it.
203 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
204 AllNodeSet.erase(N);
205 delete N;
206
207 // Now that the node is gone, check to see if any of the operands of this node
208 // are dead now.
209
210 // Remove duplicate operand entries.
211 std::sort(Operands.begin(), Operands.end());
212 Operands.erase(std::unique(Operands.begin(), Operands.end()),
213 Operands.end());
214
215 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
216 DeleteNodeIfDead(Operands[i], NodeSet);
217}
218
219
Chris Lattner600d3082003-08-11 14:57:33 +0000220SelectionDAG::~SelectionDAG() {
221 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
222 delete AllNodes[i];
223}
224
Chris Lattner061a1ea2005-01-07 07:46:32 +0000225SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
226 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
227 // Mask out any bits that are not valid for this constant.
228 Val &= (1ULL << MVT::getSizeInBits(VT)) - 1;
Chris Lattner600d3082003-08-11 14:57:33 +0000229
Chris Lattner061a1ea2005-01-07 07:46:32 +0000230 SDNode *&N = Constants[std::make_pair(Val, VT)];
231 if (N) return SDOperand(N, 0);
232 N = new ConstantSDNode(Val, VT);
233 AllNodes.push_back(N);
234 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000235}
236
Chris Lattner061a1ea2005-01-07 07:46:32 +0000237SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
238 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
239 if (VT == MVT::f32)
240 Val = (float)Val; // Mask out extra precision.
241
242 SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
243 if (N) return SDOperand(N, 0);
244 N = new ConstantFPSDNode(Val, VT);
245 AllNodes.push_back(N);
246 return SDOperand(N, 0);
247}
248
249
250
251SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
252 MVT::ValueType VT) {
253 SDNode *&N = GlobalValues[GV];
254 if (N) return SDOperand(N, 0);
255 N = new GlobalAddressSDNode(GV,VT);
256 AllNodes.push_back(N);
257 return SDOperand(N, 0);
258}
259
260SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
261 SDNode *&N = FrameIndices[FI];
262 if (N) return SDOperand(N, 0);
263 N = new FrameIndexSDNode(FI, VT);
264 AllNodes.push_back(N);
265 return SDOperand(N, 0);
266}
267
268SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
269 SDNode *N = ConstantPoolIndices[CPIdx];
270 if (N) return SDOperand(N, 0);
271 N = new ConstantPoolSDNode(CPIdx, VT);
272 AllNodes.push_back(N);
273 return SDOperand(N, 0);
274}
275
276SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
277 SDNode *&N = BBNodes[MBB];
278 if (N) return SDOperand(N, 0);
279 N = new BasicBlockSDNode(MBB);
280 AllNodes.push_back(N);
281 return SDOperand(N, 0);
282}
283
284SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
285 SDNode *&N = ExternalSymbols[Sym];
286 if (N) return SDOperand(N, 0);
287 N = new ExternalSymbolSDNode(Sym, VT);
288 AllNodes.push_back(N);
289 return SDOperand(N, 0);
290}
291
292SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, SDOperand N1,
293 SDOperand N2) {
294 // These setcc operations always fold.
295 switch (Cond) {
296 default: break;
297 case ISD::SETFALSE:
298 case ISD::SETFALSE2: return getConstant(0, MVT::i1);
299 case ISD::SETTRUE:
300 case ISD::SETTRUE2: return getConstant(1, MVT::i1);
301 }
302
303 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
304 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
305 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
306
307 // Sign extend the operands if required
308 if (ISD::isSignedIntSetCC(Cond)) {
309 C1 = N1C->getSignExtended();
310 C2 = N2C->getSignExtended();
311 }
312
313 switch (Cond) {
314 default: assert(0 && "Unknown integer setcc!");
315 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
316 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
317 case ISD::SETULT: return getConstant(C1 < C2, MVT::i1);
318 case ISD::SETUGT: return getConstant(C1 > C2, MVT::i1);
319 case ISD::SETULE: return getConstant(C1 <= C2, MVT::i1);
320 case ISD::SETUGE: return getConstant(C1 >= C2, MVT::i1);
321 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
322 case ISD::SETGT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
323 case ISD::SETLE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
324 case ISD::SETGE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
325 }
326 } else {
327 // Ensure that the constant occurs on the RHS.
328 Cond = ISD::getSetCCSwappedOperands(Cond);
329 std::swap(N1, N2);
330 }
331
332 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
333 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
334 double C1 = N1C->getValue(), C2 = N2C->getValue();
335
336 switch (Cond) {
337 default: break; // FIXME: Implement the rest of these!
338 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
339 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
340 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
341 case ISD::SETGT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
342 case ISD::SETLE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
343 case ISD::SETGE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
344 }
345 } else {
346 // Ensure that the constant occurs on the RHS.
347 Cond = ISD::getSetCCSwappedOperands(Cond);
348 std::swap(N1, N2);
349 }
350
351 if (N1 == N2) {
352 // We can always fold X == Y for integer setcc's.
353 if (MVT::isInteger(N1.getValueType()))
354 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
355 unsigned UOF = ISD::getUnorderedFlavor(Cond);
356 if (UOF == 2) // FP operators that are undefined on NaNs.
357 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
358 if (UOF == ISD::isTrueWhenEqual(Cond))
359 return getConstant(UOF, MVT::i1);
360 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
361 // if it is not already.
362 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
363 }
364
365
366 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2), Cond)];
367 if (N) return SDOperand(N, 0);
368 N = new SetCCSDNode(Cond, N1, N2);
369 AllNodes.push_back(N);
370 return SDOperand(N, 0);
371}
372
373
374
375/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000376///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000377SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
378 SDNode *N = new SDNode(Opcode, VT);
379 AllNodes.push_back(N);
380 return SDOperand(N, 0);
381}
382
383static const Type *getTypeFor(MVT::ValueType VT) {
384 switch (VT) {
385 default: assert(0 && "Unknown MVT!");
386 case MVT::i1: return Type::BoolTy;
387 case MVT::i8: return Type::UByteTy;
388 case MVT::i16: return Type::UShortTy;
389 case MVT::i32: return Type::UIntTy;
390 case MVT::i64: return Type::ULongTy;
391 case MVT::f32: return Type::FloatTy;
392 case MVT::f64: return Type::DoubleTy;
Chris Lattner600d3082003-08-11 14:57:33 +0000393 }
394}
395
Chris Lattner061a1ea2005-01-07 07:46:32 +0000396SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
397 SDOperand Operand) {
398 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
399 uint64_t Val = C->getValue();
400 switch (Opcode) {
401 default: break;
402 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
403 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
404 case ISD::TRUNCATE: return getConstant(Val, VT);
405 }
406 }
407
408 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
409 switch (Opcode) {
410 case ISD::FP_ROUND:
411 case ISD::FP_EXTEND:
412 return getConstantFP(C->getValue(), VT);
413 }
414
415 unsigned OpOpcode = Operand.Val->getOpcode();
416 switch (Opcode) {
417 case ISD::SIGN_EXTEND:
418 if (Operand.getValueType() == VT) return Operand; // noop extension
419 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
420 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
421 break;
422 case ISD::ZERO_EXTEND:
423 if (Operand.getValueType() == VT) return Operand; // noop extension
424 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
425 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
426 break;
427 case ISD::TRUNCATE:
428 if (Operand.getValueType() == VT) return Operand; // noop truncate
429 if (OpOpcode == ISD::TRUNCATE)
430 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
431 break;
432 }
433
434 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
435 if (N) return SDOperand(N, 0);
436 N = new SDNode(Opcode, Operand);
437 N->setValueTypes(VT);
438 AllNodes.push_back(N);
439 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000440}
441
Chris Lattner061a1ea2005-01-07 07:46:32 +0000442static bool isCommutativeBinOp(unsigned Opcode) {
443 switch (Opcode) {
444 case ISD::ADD:
445 case ISD::MUL:
446 case ISD::AND:
447 case ISD::OR:
448 case ISD::XOR: return true;
449 default: return false; // FIXME: Need commutative info for user ops!
Chris Lattner600d3082003-08-11 14:57:33 +0000450 }
Chris Lattner600d3082003-08-11 14:57:33 +0000451}
Chris Lattner061a1ea2005-01-07 07:46:32 +0000452
453static bool isAssociativeBinOp(unsigned Opcode) {
454 switch (Opcode) {
455 case ISD::ADD:
456 case ISD::MUL:
457 case ISD::AND:
458 case ISD::OR:
459 case ISD::XOR: return true;
460 default: return false; // FIXME: Need associative info for user ops!
461 }
462}
463
464static unsigned ExactLog2(uint64_t Val) {
465 unsigned Count = 0;
466 while (Val != 1) {
467 Val >>= 1;
468 ++Count;
469 }
470 return Count;
471}
472
473// isInvertibleForFree - Return true if there is no cost to emitting the logical
474// inverse of this node.
475static bool isInvertibleForFree(SDOperand N) {
476 if (isa<ConstantSDNode>(N.Val)) return true;
477 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
478 return true;
479 return false;
480}
481
482
483SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
484 SDOperand N1, SDOperand N2) {
485 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
486 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
487 if (N1C) {
488 if (N2C) {
489 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
490 switch (Opcode) {
491 case ISD::ADD: return getConstant(C1 + C2, VT);
492 case ISD::SUB: return getConstant(C1 - C2, VT);
493 case ISD::MUL: return getConstant(C1 * C2, VT);
494 case ISD::UDIV:
495 if (C2) return getConstant(C1 / C2, VT);
496 break;
497 case ISD::UREM :
498 if (C2) return getConstant(C1 % C2, VT);
499 break;
500 case ISD::SDIV :
501 if (C2) return getConstant(N1C->getSignExtended() /
502 N2C->getSignExtended(), VT);
503 break;
504 case ISD::SREM :
505 if (C2) return getConstant(N1C->getSignExtended() %
506 N2C->getSignExtended(), VT);
507 break;
508 case ISD::AND : return getConstant(C1 & C2, VT);
509 case ISD::OR : return getConstant(C1 | C2, VT);
510 case ISD::XOR : return getConstant(C1 ^ C2, VT);
511 default: break;
512 }
513
514 } else { // Cannonicalize constant to RHS if commutative
515 if (isCommutativeBinOp(Opcode)) {
516 std::swap(N1C, N2C);
517 std::swap(N1, N2);
518 }
519 }
520 }
521
522 if (N2C) {
523 uint64_t C2 = N2C->getValue();
524
525 switch (Opcode) {
526 case ISD::ADD:
527 if (!C2) return N1; // add X, 0 -> X
528 break;
529 case ISD::SUB:
530 if (!C2) return N1; // sub X, 0 -> X
531 break;
532 case ISD::MUL:
533 if (!C2) return N2; // mul X, 0 -> 0
534 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
535 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
536
537 // FIXME: This should only be done if the target supports shift
538 // operations.
539 if ((C2 & C2-1) == 0) {
540 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
541 return getNode(ISD::SHL, VT, N1, ShAmt);
542 }
543 break;
544
545 case ISD::UDIV:
546 // FIXME: This should only be done if the target supports shift
547 // operations.
548 if ((C2 & C2-1) == 0 && C2) {
549 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
550 return getNode(ISD::SRL, VT, N1, ShAmt);
551 }
552 break;
553
554 case ISD::AND:
555 if (!C2) return N2; // X and 0 -> 0
556 if (N2C->isAllOnesValue())
557 return N1; // X and -1 -> X
558 break;
559 case ISD::OR:
560 if (!C2)return N1; // X or 0 -> X
561 if (N2C->isAllOnesValue())
562 return N2; // X or -1 -> -1
563 break;
564 case ISD::XOR:
565 if (!C2) return N1; // X xor 0 -> X
566 if (N2C->isAllOnesValue()) {
567 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
568 // !(X op Y) -> (X !op Y)
569 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
570 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
571 SetCC->getOperand(0), SetCC->getOperand(1));
572 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
573 SDNode *Op = N1.Val;
574 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
575 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
576 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
577 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
578 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
579 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
580 if (Op->getOpcode() == ISD::AND)
581 return getNode(ISD::OR, VT, LHS, RHS);
582 return getNode(ISD::AND, VT, LHS, RHS);
583 }
584 }
585 // X xor -1 -> not(x) ?
586 }
587 break;
588 }
589
590 // Reassociate ((X op C1) op C2) if possible.
591 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
592 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
593 return getNode(Opcode, VT, N3C->getOperand(0),
594 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
595 }
596
597 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
598 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
599 if (N1CFP)
600 if (N2CFP) {
601 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
602 switch (Opcode) {
603 case ISD::ADD: return getConstantFP(C1 + C2, VT);
604 case ISD::SUB: return getConstantFP(C1 - C2, VT);
605 case ISD::MUL: return getConstantFP(C1 * C2, VT);
606 case ISD::SDIV:
607 if (C2) return getConstantFP(C1 / C2, VT);
608 break;
609 case ISD::SREM :
610 if (C2) return getConstantFP(fmod(C1, C2), VT);
611 break;
612 default: break;
613 }
614
615 } else { // Cannonicalize constant to RHS if commutative
616 if (isCommutativeBinOp(Opcode)) {
617 std::swap(N1CFP, N2CFP);
618 std::swap(N1, N2);
619 }
620 }
621
622 // Finally, fold operations that do not require constants.
623 switch (Opcode) {
624 case ISD::AND:
625 case ISD::OR:
626 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
627 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
628 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
629 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
630 ISD::CondCode Op2 = RHS->getCondition();
631
632 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
633 if (LL == RR && LR == RL) {
634 Op2 = ISD::getSetCCSwappedOperands(Op2);
635 goto MatchedBackwards;
636 }
637
638 if (LL == RL && LR == RR) {
639 MatchedBackwards:
640 ISD::CondCode Result;
641 bool isInteger = MVT::isInteger(LL.getValueType());
642 if (Opcode == ISD::OR)
643 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
644 isInteger);
645 else
646 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
647 isInteger);
648 if (Result != ISD::SETCC_INVALID)
649 return getSetCC(Result, LL, LR);
650 }
651 }
652 break;
653 case ISD::XOR:
654 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
655 break;
656 }
657
658 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
659 if (N) return SDOperand(N, 0);
660 N = new SDNode(Opcode, N1, N2);
661 N->setValueTypes(VT);
662
663 AllNodes.push_back(N);
664 return SDOperand(N, 0);
665}
666
667SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
668 SDOperand Chain, SDOperand Ptr) {
669 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
670 if (N) return SDOperand(N, 0);
671 N = new SDNode(ISD::LOAD, Chain, Ptr);
672
673 // Loads have a token chain.
674 N->setValueTypes(VT, MVT::Other);
675 AllNodes.push_back(N);
676 return SDOperand(N, 0);
677}
678
679
680SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
681 SDOperand N1, SDOperand N2, SDOperand N3) {
682 // Perform various simplifications.
683 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
684 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
685 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
686 switch (Opcode) {
687 case ISD::SELECT:
688 if (N1C)
689 if (N1C->getValue())
690 return N2; // select true, X, Y -> X
691 else
692 return N3; // select false, X, Y -> Y
693
694 if (N2 == N3) return N2; // select C, X, X -> X
695
696 if (VT == MVT::i1) { // Boolean SELECT
697 if (N2C) {
698 if (N3C) {
699 if (N2C->getValue()) // select C, 1, 0 -> C
700 return N1;
701 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
702 }
703
704 if (N2C->getValue()) // select C, 1, X -> C | X
705 return getNode(ISD::OR, VT, N1, N3);
706 else // select C, 0, X -> ~C & X
707 return getNode(ISD::AND, VT,
708 getNode(ISD::XOR, N1.getValueType(), N1,
709 getConstant(1, N1.getValueType())), N3);
710 } else if (N3C) {
711 if (N3C->getValue()) // select C, X, 1 -> ~C | X
712 return getNode(ISD::OR, VT,
713 getNode(ISD::XOR, N1.getValueType(), N1,
714 getConstant(1, N1.getValueType())), N2);
715 else // select C, X, 0 -> C & X
716 return getNode(ISD::AND, VT, N1, N2);
717 }
718 }
719
720 break;
721 }
722
723 SDNode *N = new SDNode(Opcode, N1, N2, N3);
724 switch (Opcode) {
725 default:
726 N->setValueTypes(VT);
727 break;
728 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
729 N->setValueTypes(VT, MVT::Other);
730 break;
731 }
732
733 // FIXME: memoize NODES
734 AllNodes.push_back(N);
735 return SDOperand(N, 0);
736}
737
738SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
739 std::vector<SDOperand> &Children) {
740 switch (Children.size()) {
741 case 0: return getNode(Opcode, VT);
742 case 1: return getNode(Opcode, VT, Children[0]);
743 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
744 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
745 default:
746 // FIXME: MEMOIZE!!
747 SDNode *N = new SDNode(Opcode, Children);
748 N->setValueTypes(VT);
749 AllNodes.push_back(N);
750 return SDOperand(N, 0);
751 }
752}
753
754
755
756void SDNode::dump() const {
757 std::cerr << (void*)this << ": ";
758
759 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
760 if (i) std::cerr << ",";
761 switch (getValueType(i)) {
762 default: assert(0 && "Unknown value type!");
763 case MVT::i1: std::cerr << "i1"; break;
764 case MVT::i8: std::cerr << "i8"; break;
765 case MVT::i16: std::cerr << "i16"; break;
766 case MVT::i32: std::cerr << "i32"; break;
767 case MVT::i64: std::cerr << "i64"; break;
768 case MVT::f32: std::cerr << "f32"; break;
769 case MVT::f64: std::cerr << "f64"; break;
770 case MVT::Other: std::cerr << "ch"; break;
771 }
772 }
773 std::cerr << " = ";
774
775 switch (getOpcode()) {
776 default: std::cerr << "<<Unknown>>"; break;
777 case ISD::EntryToken: std::cerr << "EntryToken"; break;
778 case ISD::Constant: std::cerr << "Constant"; break;
779 case ISD::ConstantFP: std::cerr << "ConstantFP"; break;
780 case ISD::GlobalAddress: std::cerr << "GlobalAddress"; break;
781 case ISD::FrameIndex: std::cerr << "FrameIndex"; break;
782 case ISD::BasicBlock: std::cerr << "BasicBlock"; break;
783 case ISD::ExternalSymbol: std::cerr << "ExternalSymbol"; break;
784 case ISD::ConstantPool: std::cerr << "ConstantPoolIndex"; break;
785 case ISD::CopyToReg: std::cerr << "CopyToReg"; break;
786 case ISD::CopyFromReg: std::cerr << "CopyFromReg"; break;
787
788 case ISD::ADD: std::cerr << "add"; break;
789 case ISD::SUB: std::cerr << "sub"; break;
790 case ISD::MUL: std::cerr << "mul"; break;
791 case ISD::SDIV: std::cerr << "sdiv"; break;
792 case ISD::UDIV: std::cerr << "udiv"; break;
793 case ISD::SREM: std::cerr << "srem"; break;
794 case ISD::UREM: std::cerr << "urem"; break;
795 case ISD::AND: std::cerr << "and"; break;
796 case ISD::OR: std::cerr << "or"; break;
797 case ISD::XOR: std::cerr << "xor"; break;
798 case ISD::SHL: std::cerr << "shl"; break;
799 case ISD::SRA: std::cerr << "sra"; break;
800 case ISD::SRL: std::cerr << "srl"; break;
801
802 case ISD::SETCC: std::cerr << "setcc"; break;
803 case ISD::SELECT: std::cerr << "select"; break;
804 case ISD::ADDC: std::cerr << "addc"; break;
805 case ISD::SUBB: std::cerr << "subb"; break;
806
807 // Conversion operators.
808 case ISD::SIGN_EXTEND: std::cerr << "sign_extend"; break;
809 case ISD::ZERO_EXTEND: std::cerr << "zero_extend"; break;
810 case ISD::TRUNCATE: std::cerr << "truncate"; break;
811 case ISD::FP_ROUND: std::cerr << "fp_round"; break;
812 case ISD::FP_EXTEND: std::cerr << "fp_extend"; break;
813
814 // Control flow instructions
815 case ISD::BR: std::cerr << "br"; break;
816 case ISD::BRCOND: std::cerr << "brcond"; break;
817 case ISD::RET: std::cerr << "ret"; break;
818 case ISD::CALL: std::cerr << "call"; break;
819 case ISD::ADJCALLSTACKDOWN: std::cerr << "adjcallstackdown"; break;
820 case ISD::ADJCALLSTACKUP: std::cerr << "adjcallstackup"; break;
821
822 // Other operators
823 case ISD::LOAD: std::cerr << "load"; break;
824 case ISD::STORE: std::cerr << "store"; break;
825 case ISD::DYNAMIC_STACKALLOC: std::cerr << "dynamic_stackalloc"; break;
826 case ISD::EXTRACT_ELEMENT: std::cerr << "extract_element"; break;
827 case ISD::BUILD_PAIR: std::cerr << "build_pair"; break;
828 }
829
830 std::cerr << " ";
831 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
832 if (i) std::cerr << ", ";
833 std::cerr << (void*)getOperand(i).Val;
834 if (unsigned RN = getOperand(i).ResNo)
835 std::cerr << ":" << RN;
836 }
837
838 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
839 std::cerr << "<" << CSDN->getValue() << ">";
840 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
841 std::cerr << "<" << CSDN->getValue() << ">";
842 } else if (const GlobalAddressSDNode *GADN =
843 dyn_cast<GlobalAddressSDNode>(this)) {
844 std::cerr << "<";
845 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
846 } else if (const FrameIndexSDNode *FIDN =
847 dyn_cast<FrameIndexSDNode>(this)) {
848 std::cerr << "<" << FIDN->getIndex() << ">";
849 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
850 std::cerr << "<" << CP->getIndex() << ">";
851 } else if (const BasicBlockSDNode *BBDN =
852 dyn_cast<BasicBlockSDNode>(this)) {
853 std::cerr << "<";
854 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
855 if (LBB)
856 std::cerr << LBB->getName() << " ";
857 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
858 } else if (const CopyRegSDNode *C2V = dyn_cast<CopyRegSDNode>(this)) {
859 std::cerr << "<reg #" << C2V->getReg() << ">";
860 } else if (const ExternalSymbolSDNode *ES =
861 dyn_cast<ExternalSymbolSDNode>(this)) {
862 std::cerr << "'" << ES->getSymbol() << "'";
863 } else if (const SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(this)) {
864 std::cerr << " - condition = ";
865 switch (SetCC->getCondition()) {
866 default: assert(0 && "Unknown setcc condition!");
867 case ISD::SETOEQ: std::cerr << "setoeq"; break;
868 case ISD::SETOGT: std::cerr << "setogt"; break;
869 case ISD::SETOGE: std::cerr << "setoge"; break;
870 case ISD::SETOLT: std::cerr << "setolt"; break;
871 case ISD::SETOLE: std::cerr << "setole"; break;
872 case ISD::SETONE: std::cerr << "setone"; break;
873
874 case ISD::SETO: std::cerr << "seto"; break;
875 case ISD::SETUO: std::cerr << "setuo"; break;
876 case ISD::SETUEQ: std::cerr << "setue"; break;
877 case ISD::SETUGT: std::cerr << "setugt"; break;
878 case ISD::SETUGE: std::cerr << "setuge"; break;
879 case ISD::SETULT: std::cerr << "setult"; break;
880 case ISD::SETULE: std::cerr << "setule"; break;
881 case ISD::SETUNE: std::cerr << "setune"; break;
882
883 case ISD::SETEQ: std::cerr << "seteq"; break;
884 case ISD::SETGT: std::cerr << "setgt"; break;
885 case ISD::SETGE: std::cerr << "setge"; break;
886 case ISD::SETLT: std::cerr << "setlt"; break;
887 case ISD::SETLE: std::cerr << "setle"; break;
888 case ISD::SETNE: std::cerr << "setne"; break;
889 }
890 }
891
892}
893
894void SelectionDAG::dump() const {
895 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
896 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i) {
897 std::cerr << "\n ";
898 AllNodes[i]->dump();
899 }
900 std::cerr << "\n\n";
901}
902