blob: 68ef11e8bf9158902d5987c018db5d781637a680 [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
231/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
232/// correspond to it. This is useful when we're about to delete or repurpose
233/// the node. We don't want future request for structurally identical nodes
234/// to return N anymore.
235void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000236 switch (N->getOpcode()) {
237 case ISD::Constant:
238 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
239 N->getValueType(0)));
240 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000241 case ISD::TargetConstant:
242 TargetConstants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
243 N->getValueType(0)));
244 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000245 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000246 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
247 ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000248 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000249 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000250 case ISD::CONDCODE:
251 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
252 "Cond code doesn't exist!");
253 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
254 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000255 case ISD::GlobalAddress:
256 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
257 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000258 case ISD::TargetGlobalAddress:
259 TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
260 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 case ISD::FrameIndex:
262 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
263 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000264 case ISD::TargetFrameIndex:
265 TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
266 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000267 case ISD::ConstantPool:
268 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
269 break;
270 case ISD::BasicBlock:
271 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
272 break;
273 case ISD::ExternalSymbol:
274 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
275 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000276 case ISD::VALUETYPE:
277 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
278 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000279 case ISD::Register:
280 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
281 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000282 case ISD::SRCVALUE: {
283 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
284 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
285 break;
286 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000287 case ISD::LOAD:
288 Loads.erase(std::make_pair(N->getOperand(1),
289 std::make_pair(N->getOperand(0),
290 N->getValueType(0))));
291 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000292 default:
293 if (N->getNumOperands() == 1)
294 UnaryOps.erase(std::make_pair(N->getOpcode(),
295 std::make_pair(N->getOperand(0),
296 N->getValueType(0))));
297 else if (N->getNumOperands() == 2)
298 BinaryOps.erase(std::make_pair(N->getOpcode(),
299 std::make_pair(N->getOperand(0),
300 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000301 else if (N->getNumValues() == 1) {
302 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
303 OneResultNodes.erase(std::make_pair(N->getOpcode(),
304 std::make_pair(N->getValueType(0),
305 Ops)));
306 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000307 // Remove the node from the ArbitraryNodes map.
308 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
309 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
310 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
311 std::make_pair(RV, Ops)));
312 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000313 break;
314 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315}
316
Chris Lattner8b8749f2005-08-17 19:00:20 +0000317/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
318/// has been taken out and modified in some way. If the specified node already
319/// exists in the CSE maps, do not modify the maps, but return the existing node
320/// instead. If it doesn't exist, add it and return null.
321///
322SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
323 assert(N->getNumOperands() && "This is a leaf node!");
324 if (N->getOpcode() == ISD::LOAD) {
325 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
326 std::make_pair(N->getOperand(0),
327 N->getValueType(0)))];
328 if (L) return L;
329 L = N;
330 } else if (N->getNumOperands() == 1) {
331 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
332 std::make_pair(N->getOperand(0),
333 N->getValueType(0)))];
334 if (U) return U;
335 U = N;
336 } else if (N->getNumOperands() == 2) {
337 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
338 std::make_pair(N->getOperand(0),
339 N->getOperand(1)))];
340 if (B) return B;
341 B = N;
342 } else if (N->getNumValues() == 1) {
343 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
344 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
345 std::make_pair(N->getValueType(0), Ops))];
346 if (ORN) return ORN;
347 ORN = N;
348 } else {
349 // Remove the node from the ArbitraryNodes map.
350 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
351 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
352 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
353 std::make_pair(RV, Ops))];
354 if (AN) return AN;
355 AN = N;
356 }
357 return 0;
358
359}
360
361
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000362
Chris Lattner78ec3112003-08-11 14:57:33 +0000363SelectionDAG::~SelectionDAG() {
364 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
365 delete AllNodes[i];
366}
367
Chris Lattner0f2287b2005-04-13 02:38:18 +0000368SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000369 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000370 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000371 return getNode(ISD::AND, Op.getValueType(), Op,
372 getConstant(Imm, Op.getValueType()));
373}
374
Chris Lattnerc3aae252005-01-07 07:46:32 +0000375SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
376 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
377 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000378 if (VT != MVT::i64)
379 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000380
Chris Lattnerc3aae252005-01-07 07:46:32 +0000381 SDNode *&N = Constants[std::make_pair(Val, VT)];
382 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000383 N = new ConstantSDNode(false, Val, VT);
384 AllNodes.push_back(N);
385 return SDOperand(N, 0);
386}
387
388SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
389 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
390 // Mask out any bits that are not valid for this constant.
391 if (VT != MVT::i64)
392 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
393
394 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
395 if (N) return SDOperand(N, 0);
396 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000397 AllNodes.push_back(N);
398 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000399}
400
Chris Lattnerc3aae252005-01-07 07:46:32 +0000401SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
402 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
403 if (VT == MVT::f32)
404 Val = (float)Val; // Mask out extra precision.
405
Chris Lattnerd8658612005-02-17 20:17:32 +0000406 // Do the map lookup using the actual bit pattern for the floating point
407 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
408 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000409 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000410 if (N) return SDOperand(N, 0);
411 N = new ConstantFPSDNode(Val, VT);
412 AllNodes.push_back(N);
413 return SDOperand(N, 0);
414}
415
416
417
418SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
419 MVT::ValueType VT) {
420 SDNode *&N = GlobalValues[GV];
421 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000422 N = new GlobalAddressSDNode(false, GV, VT);
423 AllNodes.push_back(N);
424 return SDOperand(N, 0);
425}
426
427SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
428 MVT::ValueType VT) {
429 SDNode *&N = TargetGlobalValues[GV];
430 if (N) return SDOperand(N, 0);
431 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000432 AllNodes.push_back(N);
433 return SDOperand(N, 0);
434}
435
436SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
437 SDNode *&N = FrameIndices[FI];
438 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000439 N = new FrameIndexSDNode(FI, VT, false);
440 AllNodes.push_back(N);
441 return SDOperand(N, 0);
442}
443
444SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
445 SDNode *&N = TargetFrameIndices[FI];
446 if (N) return SDOperand(N, 0);
447 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000448 AllNodes.push_back(N);
449 return SDOperand(N, 0);
450}
451
452SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
453 SDNode *N = ConstantPoolIndices[CPIdx];
454 if (N) return SDOperand(N, 0);
455 N = new ConstantPoolSDNode(CPIdx, VT);
456 AllNodes.push_back(N);
457 return SDOperand(N, 0);
458}
459
460SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
461 SDNode *&N = BBNodes[MBB];
462 if (N) return SDOperand(N, 0);
463 N = new BasicBlockSDNode(MBB);
464 AllNodes.push_back(N);
465 return SDOperand(N, 0);
466}
467
Chris Lattner15e4b012005-07-10 00:07:11 +0000468SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
469 if ((unsigned)VT >= ValueTypeNodes.size())
470 ValueTypeNodes.resize(VT+1);
471 if (ValueTypeNodes[VT] == 0) {
472 ValueTypeNodes[VT] = new VTSDNode(VT);
473 AllNodes.push_back(ValueTypeNodes[VT]);
474 }
475
476 return SDOperand(ValueTypeNodes[VT], 0);
477}
478
Chris Lattnerc3aae252005-01-07 07:46:32 +0000479SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
480 SDNode *&N = ExternalSymbols[Sym];
481 if (N) return SDOperand(N, 0);
482 N = new ExternalSymbolSDNode(Sym, VT);
483 AllNodes.push_back(N);
484 return SDOperand(N, 0);
485}
486
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000487SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
488 if ((unsigned)Cond >= CondCodeNodes.size())
489 CondCodeNodes.resize(Cond+1);
490
Chris Lattner079a27a2005-08-09 20:40:02 +0000491 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000492 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000493 AllNodes.push_back(CondCodeNodes[Cond]);
494 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000495 return SDOperand(CondCodeNodes[Cond], 0);
496}
497
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000498SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
499 if (Reg >= RegNodes.size())
500 RegNodes.resize(Reg+1);
501 RegisterSDNode *&Result = RegNodes[Reg];
502 if (Result) {
503 assert(Result->getValueType(0) == VT &&
504 "Inconsistent value types for machine registers");
505 } else {
506 Result = new RegisterSDNode(Reg, VT);
507 AllNodes.push_back(Result);
508 }
509 return SDOperand(Result, 0);
510}
511
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000512SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
513 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000514 // These setcc operations always fold.
515 switch (Cond) {
516 default: break;
517 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000518 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000519 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000520 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000521 }
522
Chris Lattner67255a12005-04-07 18:14:58 +0000523 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
524 uint64_t C2 = N2C->getValue();
525 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
526 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000527
Chris Lattnerc3aae252005-01-07 07:46:32 +0000528 // Sign extend the operands if required
529 if (ISD::isSignedIntSetCC(Cond)) {
530 C1 = N1C->getSignExtended();
531 C2 = N2C->getSignExtended();
532 }
533
534 switch (Cond) {
535 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000536 case ISD::SETEQ: return getConstant(C1 == C2, VT);
537 case ISD::SETNE: return getConstant(C1 != C2, VT);
538 case ISD::SETULT: return getConstant(C1 < C2, VT);
539 case ISD::SETUGT: return getConstant(C1 > C2, VT);
540 case ISD::SETULE: return getConstant(C1 <= C2, VT);
541 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
542 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
543 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
544 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
545 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000546 }
Chris Lattner24673922005-04-07 18:58:54 +0000547 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000548 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000549 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
550 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
551
552 // If the comparison constant has bits in the upper part, the
553 // zero-extended value could never match.
554 if (C2 & (~0ULL << InSize)) {
555 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
556 switch (Cond) {
557 case ISD::SETUGT:
558 case ISD::SETUGE:
559 case ISD::SETEQ: return getConstant(0, VT);
560 case ISD::SETULT:
561 case ISD::SETULE:
562 case ISD::SETNE: return getConstant(1, VT);
563 case ISD::SETGT:
564 case ISD::SETGE:
565 // True if the sign bit of C2 is set.
566 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
567 case ISD::SETLT:
568 case ISD::SETLE:
569 // True if the sign bit of C2 isn't set.
570 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
571 default:
572 break;
573 }
574 }
575
576 // Otherwise, we can perform the comparison with the low bits.
577 switch (Cond) {
578 case ISD::SETEQ:
579 case ISD::SETNE:
580 case ISD::SETUGT:
581 case ISD::SETUGE:
582 case ISD::SETULT:
583 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000584 return getSetCC(VT, N1.getOperand(0),
585 getConstant(C2, N1.getOperand(0).getValueType()),
586 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000587 default:
588 break; // todo, be more careful with signed comparisons
589 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000590 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
591 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
592 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
593 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
594 MVT::ValueType ExtDstTy = N1.getValueType();
595 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
596
597 // If the extended part has any inconsistent bits, it cannot ever
598 // compare equal. In other words, they have to be all ones or all
599 // zeros.
600 uint64_t ExtBits =
601 (~0ULL >> 64-ExtSrcTyBits) & (~0ULL << (ExtDstTyBits-1));
602 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
603 return getConstant(Cond == ISD::SETNE, VT);
604
605 // Otherwise, make this a use of a zext.
606 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
607 getConstant(C2 & (~0ULL >> 64-ExtSrcTyBits), ExtDstTy),
608 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000609 }
610
Chris Lattner67255a12005-04-07 18:14:58 +0000611 uint64_t MinVal, MaxVal;
612 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
613 if (ISD::isSignedIntSetCC(Cond)) {
614 MinVal = 1ULL << (OperandBitSize-1);
615 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
616 MaxVal = ~0ULL >> (65-OperandBitSize);
617 else
618 MaxVal = 0;
619 } else {
620 MinVal = 0;
621 MaxVal = ~0ULL >> (64-OperandBitSize);
622 }
623
624 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
625 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
626 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
627 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000628 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
629 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000630 }
631
632 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
633 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
634 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000635 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
636 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000637 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000638
Nate Begeman72ea2812005-04-14 08:56:52 +0000639 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
640 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000641
Nate Begeman72ea2812005-04-14 08:56:52 +0000642 // Canonicalize setgt X, Min --> setne X, Min
643 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000644 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000645
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000646 // If we have setult X, 1, turn it into seteq X, 0
647 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000648 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
649 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000650 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000651 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000652 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
653 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000654
655 // If we have "setcc X, C1", check to see if we can shrink the immediate
656 // by changing cc.
657
658 // SETUGT X, SINTMAX -> SETLT X, 0
659 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
660 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000661 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000662
663 // FIXME: Implement the rest of these.
664
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000665
666 // Fold bit comparisons when we can.
667 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
668 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
669 if (ConstantSDNode *AndRHS =
670 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
671 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
672 // Perform the xform if the AND RHS is a single bit.
673 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
674 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000675 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000676 TLI.getShiftAmountTy()));
677 }
678 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
679 // (X & 8) == 8 --> (X & 8) >> 3
680 // Perform the xform if C2 is a single bit.
681 if ((C2 & (C2-1)) == 0) {
682 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000683 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000684 }
685 }
686 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000687 }
Chris Lattner67255a12005-04-07 18:14:58 +0000688 } else if (isa<ConstantSDNode>(N1.Val)) {
689 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000690 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000691 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000692
693 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
694 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
695 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000696
Chris Lattnerc3aae252005-01-07 07:46:32 +0000697 switch (Cond) {
698 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000699 case ISD::SETEQ: return getConstant(C1 == C2, VT);
700 case ISD::SETNE: return getConstant(C1 != C2, VT);
701 case ISD::SETLT: return getConstant(C1 < C2, VT);
702 case ISD::SETGT: return getConstant(C1 > C2, VT);
703 case ISD::SETLE: return getConstant(C1 <= C2, VT);
704 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000705 }
706 } else {
707 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000708 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000709 }
710
711 if (N1 == N2) {
712 // We can always fold X == Y for integer setcc's.
713 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000714 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715 unsigned UOF = ISD::getUnorderedFlavor(Cond);
716 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000717 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000718 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000719 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000720 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
721 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000722 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
723 if (NewCond != Cond)
724 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000725 }
726
Chris Lattner5cdcc582005-01-09 20:52:51 +0000727 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000728 MVT::isInteger(N1.getValueType())) {
729 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
730 N1.getOpcode() == ISD::XOR) {
731 // Simplify (X+Y) == (X+Z) --> Y == Z
732 if (N1.getOpcode() == N2.getOpcode()) {
733 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000734 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000735 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000736 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000737 if (isCommutativeBinOp(N1.getOpcode())) {
738 // If X op Y == Y op X, try other combinations.
739 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000740 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000741 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000742 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000743 }
744 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000745
746 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000747
Chris Lattner68dc3102005-01-10 02:03:02 +0000748 // Simplify (X+Z) == X --> Z == 0
749 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000750 return getSetCC(VT, N1.getOperand(1),
751 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000752 if (N1.getOperand(1) == N2) {
753 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000754 return getSetCC(VT, N1.getOperand(0),
755 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000756 else {
757 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
758 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000759 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000760 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000761 N2, getConstant(1, TLI.getShiftAmountTy())),
762 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000763 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000764 }
765 }
766
Chris Lattner68dc3102005-01-10 02:03:02 +0000767 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
768 N2.getOpcode() == ISD::XOR) {
769 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000770 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000771 return getSetCC(VT, N2.getOperand(1),
772 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000773 } else if (N2.getOperand(1) == N1) {
774 if (isCommutativeBinOp(N2.getOpcode())) {
775 return getSetCC(VT, N2.getOperand(0),
776 getConstant(0, N2.getValueType()), Cond);
777 } else {
778 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
779 // X == (Z-X) --> X<<1 == Z
780 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
781 getConstant(1, TLI.getShiftAmountTy())),
782 N2.getOperand(0), Cond);
783 }
784 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000785 }
786 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000787
Chris Lattnerfda2b552005-04-18 04:48:12 +0000788 // Fold away ALL boolean setcc's.
789 if (N1.getValueType() == MVT::i1) {
790 switch (Cond) {
791 default: assert(0 && "Unknown integer setcc!");
792 case ISD::SETEQ: // X == Y -> (X^Y)^1
793 N1 = getNode(ISD::XOR, MVT::i1,
794 getNode(ISD::XOR, MVT::i1, N1, N2),
795 getConstant(1, MVT::i1));
796 break;
797 case ISD::SETNE: // X != Y --> (X^Y)
798 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
799 break;
800 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
801 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
802 N1 = getNode(ISD::AND, MVT::i1, N2,
803 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
804 break;
805 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
806 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
807 N1 = getNode(ISD::AND, MVT::i1, N1,
808 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
809 break;
810 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
811 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
812 N1 = getNode(ISD::OR, MVT::i1, N2,
813 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
814 break;
815 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
816 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
817 N1 = getNode(ISD::OR, MVT::i1, N1,
818 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
819 break;
820 }
821 if (VT != MVT::i1)
822 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
823 return N1;
824 }
825
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000826 // Could not fold it.
827 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000828}
829
Nate Begemanff663682005-08-13 06:14:17 +0000830SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
831 SDOperand N3, SDOperand N4,
832 ISD::CondCode CC) {
833 MVT::ValueType VT = N3.getValueType();
Nate Begeman32c392a2005-08-13 06:00:21 +0000834 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
835 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
836 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
837
838 // Check to see if we can simplify the select into an fabs node
839 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
840 // Allow either -0.0 or 0.0
841 if (CFP->getValue() == 0.0) {
842 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
843 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
844 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
845 N1 == N4.getOperand(0))
846 return getNode(ISD::FABS, VT, N1);
847
848 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
849 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
850 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
851 N3.getOperand(0) == N4)
852 return getNode(ISD::FABS, VT, N4);
853 }
854 }
855
856 // Check to see if we can perform the "gzip trick", transforming
857 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
858 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
859 MVT::isInteger(N1.getValueType()) &&
860 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
861 MVT::ValueType XType = N1.getValueType();
862 MVT::ValueType AType = N3.getValueType();
863 if (XType >= AType) {
864 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
865 // single-bit constant. FIXME: remove once the dag combiner
866 // exists.
867 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
868 unsigned ShCtV = Log2_64(N3C->getValue());
869 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
870 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
871 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
872 if (XType > AType)
873 Shift = getNode(ISD::TRUNCATE, AType, Shift);
874 return getNode(ISD::AND, AType, Shift, N3);
875 }
876 SDOperand Shift = getNode(ISD::SRA, XType, N1,
877 getConstant(MVT::getSizeInBits(XType)-1,
878 TLI.getShiftAmountTy()));
879 if (XType > AType)
880 Shift = getNode(ISD::TRUNCATE, AType, Shift);
881 return getNode(ISD::AND, AType, Shift, N3);
882 }
883 }
884
Nate Begemancebd4332005-08-24 04:57:57 +0000885 // Check to see if this is the equivalent of setcc X, 0
886 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000887 MVT::ValueType XType = N1.getValueType();
888 if (TLI.getOperationAction(ISD::SETCC, TLI.getSetCCResultTy()) ==
889 TargetLowering::Legal) {
Nate Begemancebd4332005-08-24 04:57:57 +0000890 return getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
Nate Begeman7042f152005-08-23 05:41:12 +0000891 }
Nate Begemancebd4332005-08-24 04:57:57 +0000892 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
893 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
894 TLI.getOperationAction(ISD::CTLZ, XType) == TargetLowering::Legal) {
Nate Begeman7042f152005-08-23 05:41:12 +0000895 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
896 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000897 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000898 TLI.getShiftAmountTy()));
899 }
Nate Begemancebd4332005-08-24 04:57:57 +0000900 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
901 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
902 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
903 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
904 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
905 getConstant(MVT::getSizeInBits(XType)-1,
906 TLI.getShiftAmountTy()));
907 }
908 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
909 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
910 SDOperand Sign = getNode(ISD::SRL, XType, N1,
911 getConstant(MVT::getSizeInBits(XType)-1,
912 TLI.getShiftAmountTy()));
913 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
914 }
Nate Begeman7042f152005-08-23 05:41:12 +0000915 }
916
Nate Begeman32c392a2005-08-13 06:00:21 +0000917 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
918 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
919 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
920 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
921 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
922 MVT::ValueType XType = N1.getValueType();
923 if (SubC->isNullValue() && MVT::isInteger(XType)) {
924 SDOperand Shift = getNode(ISD::SRA, XType, N1,
925 getConstant(MVT::getSizeInBits(XType)-1,
926 TLI.getShiftAmountTy()));
927 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
928 Shift);
929 }
930 }
931 }
932
933 // Could not fold it.
934 return SDOperand();
935}
936
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000938///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000939SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
940 SDNode *N = new SDNode(Opcode, VT);
941 AllNodes.push_back(N);
942 return SDOperand(N, 0);
943}
944
Chris Lattnerc3aae252005-01-07 07:46:32 +0000945SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
946 SDOperand Operand) {
947 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
948 uint64_t Val = C->getValue();
949 switch (Opcode) {
950 default: break;
951 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
952 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
953 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000954 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
955 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956 }
957 }
958
959 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
960 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000961 case ISD::FNEG:
962 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000963 case ISD::FP_ROUND:
964 case ISD::FP_EXTEND:
965 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000966 case ISD::FP_TO_SINT:
967 return getConstant((int64_t)C->getValue(), VT);
968 case ISD::FP_TO_UINT:
969 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000970 }
971
972 unsigned OpOpcode = Operand.Val->getOpcode();
973 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000974 case ISD::TokenFactor:
975 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 case ISD::SIGN_EXTEND:
977 if (Operand.getValueType() == VT) return Operand; // noop extension
978 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
979 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
980 break;
981 case ISD::ZERO_EXTEND:
982 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000983 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000984 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000985 break;
986 case ISD::TRUNCATE:
987 if (Operand.getValueType() == VT) return Operand; // noop truncate
988 if (OpOpcode == ISD::TRUNCATE)
989 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000990 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
991 // If the source is smaller than the dest, we still need an extend.
992 if (Operand.Val->getOperand(0).getValueType() < VT)
993 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
994 else if (Operand.Val->getOperand(0).getValueType() > VT)
995 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
996 else
997 return Operand.Val->getOperand(0);
998 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000999 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001000 case ISD::FNEG:
1001 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
1002 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
1003 Operand.Val->getOperand(0));
1004 if (OpOpcode == ISD::FNEG) // --X -> X
1005 return Operand.Val->getOperand(0);
1006 break;
1007 case ISD::FABS:
1008 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1009 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1010 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 }
1012
1013 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
1014 if (N) return SDOperand(N, 0);
1015 N = new SDNode(Opcode, Operand);
1016 N->setValueTypes(VT);
1017 AllNodes.push_back(N);
1018 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001019}
1020
Chris Lattner36019aa2005-04-18 03:48:41 +00001021/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1022/// this predicate to simplify operations downstream. V and Mask are known to
1023/// be the same type.
1024static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
1025 const TargetLowering &TLI) {
1026 unsigned SrcBits;
1027 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001028
Chris Lattner36019aa2005-04-18 03:48:41 +00001029 // If we know the result of a setcc has the top bits zero, use this info.
1030 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001031 case ISD::Constant:
1032 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1033
1034 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001035 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001036 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1037
1038 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001039 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001040 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1041 case ISD::ZERO_EXTEND:
1042 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1043 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
1044
1045 case ISD::AND:
1046 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001047 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001048 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1049
1050 // FALL THROUGH
1051 case ISD::OR:
1052 case ISD::XOR:
1053 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1054 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1055 case ISD::SELECT:
1056 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1057 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner1d6373c2005-08-24 16:46:55 +00001058 case ISD::SELECT_CC:
1059 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
1060 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001061 case ISD::SRL:
1062 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1063 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1064 uint64_t NewVal = Mask << ShAmt->getValue();
1065 SrcBits = MVT::getSizeInBits(Op.getValueType());
1066 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1067 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1068 }
1069 return false;
1070 case ISD::SHL:
1071 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1072 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1073 uint64_t NewVal = Mask >> ShAmt->getValue();
1074 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1075 }
1076 return false;
Chris Lattner1d6373c2005-08-24 16:46:55 +00001077 case ISD::CTTZ:
1078 case ISD::CTLZ:
1079 case ISD::CTPOP:
1080 // Bit counting instructions can not set the high bits of the result
1081 // register. The max number of bits sets depends on the input.
1082 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
1083
Chris Lattner6ea92792005-04-25 21:03:25 +00001084 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001085 default: break;
1086 }
1087
1088 return false;
1089}
1090
1091
1092
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1094 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001095#ifndef NDEBUG
1096 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001097 case ISD::TokenFactor:
1098 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1099 N2.getValueType() == MVT::Other && "Invalid token factor!");
1100 break;
Chris Lattner76365122005-01-16 02:23:22 +00001101 case ISD::AND:
1102 case ISD::OR:
1103 case ISD::XOR:
1104 case ISD::UDIV:
1105 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001106 case ISD::MULHU:
1107 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001108 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1109 // fall through
1110 case ISD::ADD:
1111 case ISD::SUB:
1112 case ISD::MUL:
1113 case ISD::SDIV:
1114 case ISD::SREM:
1115 assert(N1.getValueType() == N2.getValueType() &&
1116 N1.getValueType() == VT && "Binary operator types must match!");
1117 break;
1118
1119 case ISD::SHL:
1120 case ISD::SRA:
1121 case ISD::SRL:
1122 assert(VT == N1.getValueType() &&
1123 "Shift operators return type must be the same as their first arg");
1124 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001125 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001126 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001127 case ISD::FP_ROUND_INREG: {
1128 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1129 assert(VT == N1.getValueType() && "Not an inreg round!");
1130 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1131 "Cannot FP_ROUND_INREG integer types");
1132 assert(EVT <= VT && "Not rounding down!");
1133 break;
1134 }
1135 case ISD::SIGN_EXTEND_INREG: {
1136 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1137 assert(VT == N1.getValueType() && "Not an inreg extend!");
1138 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1139 "Cannot *_EXTEND_INREG FP types");
1140 assert(EVT <= VT && "Not extending!");
1141 }
1142
Chris Lattner76365122005-01-16 02:23:22 +00001143 default: break;
1144 }
1145#endif
1146
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1148 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1149 if (N1C) {
1150 if (N2C) {
1151 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1152 switch (Opcode) {
1153 case ISD::ADD: return getConstant(C1 + C2, VT);
1154 case ISD::SUB: return getConstant(C1 - C2, VT);
1155 case ISD::MUL: return getConstant(C1 * C2, VT);
1156 case ISD::UDIV:
1157 if (C2) return getConstant(C1 / C2, VT);
1158 break;
1159 case ISD::UREM :
1160 if (C2) return getConstant(C1 % C2, VT);
1161 break;
1162 case ISD::SDIV :
1163 if (C2) return getConstant(N1C->getSignExtended() /
1164 N2C->getSignExtended(), VT);
1165 break;
1166 case ISD::SREM :
1167 if (C2) return getConstant(N1C->getSignExtended() %
1168 N2C->getSignExtended(), VT);
1169 break;
1170 case ISD::AND : return getConstant(C1 & C2, VT);
1171 case ISD::OR : return getConstant(C1 | C2, VT);
1172 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001173 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1174 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1175 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001176 default: break;
1177 }
1178
1179 } else { // Cannonicalize constant to RHS if commutative
1180 if (isCommutativeBinOp(Opcode)) {
1181 std::swap(N1C, N2C);
1182 std::swap(N1, N2);
1183 }
1184 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001185
1186 switch (Opcode) {
1187 default: break;
1188 case ISD::SHL: // shl 0, X -> 0
1189 if (N1C->isNullValue()) return N1;
1190 break;
1191 case ISD::SRL: // srl 0, X -> 0
1192 if (N1C->isNullValue()) return N1;
1193 break;
1194 case ISD::SRA: // sra -1, X -> -1
1195 if (N1C->isAllOnesValue()) return N1;
1196 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001197 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1198 // Extending a constant? Just return the extended constant.
1199 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1200 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001201 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001202 }
1203
1204 if (N2C) {
1205 uint64_t C2 = N2C->getValue();
1206
1207 switch (Opcode) {
1208 case ISD::ADD:
1209 if (!C2) return N1; // add X, 0 -> X
1210 break;
1211 case ISD::SUB:
1212 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001213 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 case ISD::MUL:
1215 if (!C2) return N2; // mul X, 0 -> 0
1216 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1217 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1218
Chris Lattnerb48da392005-01-23 04:39:44 +00001219 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001221 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001222 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001223 }
1224 break;
1225
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001226 case ISD::MULHU:
1227 case ISD::MULHS:
1228 if (!C2) return N2; // mul X, 0 -> 0
1229
1230 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1231 return getConstant(0, VT);
1232
1233 // Many others could be handled here, including -1, powers of 2, etc.
1234 break;
1235
Chris Lattnerc3aae252005-01-07 07:46:32 +00001236 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001237 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001238 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001239 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001240 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 }
1242 break;
1243
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001244 case ISD::SHL:
1245 case ISD::SRL:
1246 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001247 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001248 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001249 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1250 return getNode(ISD::UNDEF, N1.getValueType());
1251 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001252 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001253
1254 if (Opcode == ISD::SRA) {
1255 // If the sign bit is known to be zero, switch this to a SRL.
1256 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001257 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001258 TLI))
1259 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1260 } else {
1261 // If the part left over is known to be zero, the whole thing is zero.
1262 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1263 if (Opcode == ISD::SRL) {
1264 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1265 return getConstant(0, N1.getValueType());
1266 } else if (Opcode == ISD::SHL) {
1267 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1268 return getConstant(0, N1.getValueType());
1269 }
1270 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001271
1272 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1273 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1274 unsigned OpSAC = OpSA->getValue();
1275 if (N1.getOpcode() == ISD::SHL) {
1276 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1277 return getConstant(0, N1.getValueType());
1278 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1279 getConstant(C2+OpSAC, N2.getValueType()));
1280 } else if (N1.getOpcode() == ISD::SRL) {
1281 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1282 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1283 getConstant(~0ULL << OpSAC, VT));
1284 if (C2 > OpSAC) {
1285 return getNode(ISD::SHL, VT, Mask,
1286 getConstant(C2-OpSAC, N2.getValueType()));
1287 } else {
1288 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1289 return getNode(ISD::SRL, VT, Mask,
1290 getConstant(OpSAC-C2, N2.getValueType()));
1291 }
1292 } else if (N1.getOpcode() == ISD::SRA) {
1293 // if C1 == C2, just mask out low bits.
1294 if (C2 == OpSAC)
1295 return getNode(ISD::AND, VT, N1.getOperand(0),
1296 getConstant(~0ULL << C2, VT));
1297 }
1298 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001299 break;
1300
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 case ISD::AND:
1302 if (!C2) return N2; // X and 0 -> 0
1303 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001304 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001305
Chris Lattner36019aa2005-04-18 03:48:41 +00001306 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1307 return getConstant(0, VT);
1308
Chris Lattner588bbbf2005-04-21 06:28:15 +00001309 {
1310 uint64_t NotC2 = ~C2;
1311 if (VT != MVT::i64)
1312 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1313
1314 if (MaskedValueIsZero(N1, NotC2, TLI))
1315 return N1; // if (X & ~C2) -> 0, the and is redundant
1316 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001317
Chris Lattnerdea29e22005-04-10 01:13:15 +00001318 // FIXME: Should add a corresponding version of this for
1319 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1320 // we don't have yet.
1321
Chris Lattner0f2287b2005-04-13 02:38:18 +00001322 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1323 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001324 // If we are masking out the part of our input that was extended, just
1325 // mask the input to the extension directly.
1326 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001327 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001328 if ((C2 & (~0ULL << ExtendBits)) == 0)
1329 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001330 } else if (N1.getOpcode() == ISD::OR) {
1331 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1332 if ((ORI->getValue() & C2) == C2) {
1333 // If the 'or' is setting all of the bits that we are masking for,
1334 // we know the result of the AND will be the AND mask itself.
1335 return N2;
1336 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001337 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001338 break;
1339 case ISD::OR:
1340 if (!C2)return N1; // X or 0 -> X
1341 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001342 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001343 break;
1344 case ISD::XOR:
1345 if (!C2) return N1; // X xor 0 -> X
1346 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001347 if (N1.Val->getOpcode() == ISD::SETCC){
1348 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001349 // !(X op Y) -> (X !op Y)
1350 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001351 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1352 return getSetCC(SetCC->getValueType(0),
1353 SetCC->getOperand(0), SetCC->getOperand(1),
1354 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001355 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1356 SDNode *Op = N1.Val;
1357 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1358 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1359 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1360 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1361 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1362 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1363 if (Op->getOpcode() == ISD::AND)
1364 return getNode(ISD::OR, VT, LHS, RHS);
1365 return getNode(ISD::AND, VT, LHS, RHS);
1366 }
1367 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001368 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001369 }
1370 break;
1371 }
1372
1373 // Reassociate ((X op C1) op C2) if possible.
1374 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1375 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001376 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001377 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1378 }
1379
1380 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1381 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001382 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001383 if (N2CFP) {
1384 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1385 switch (Opcode) {
1386 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1387 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1388 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1389 case ISD::SDIV:
1390 if (C2) return getConstantFP(C1 / C2, VT);
1391 break;
1392 case ISD::SREM :
1393 if (C2) return getConstantFP(fmod(C1, C2), VT);
1394 break;
1395 default: break;
1396 }
1397
1398 } else { // Cannonicalize constant to RHS if commutative
1399 if (isCommutativeBinOp(Opcode)) {
1400 std::swap(N1CFP, N2CFP);
1401 std::swap(N1, N2);
1402 }
1403 }
1404
Chris Lattner15e4b012005-07-10 00:07:11 +00001405 if (Opcode == ISD::FP_ROUND_INREG)
1406 return getNode(ISD::FP_EXTEND, VT,
1407 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1408 }
1409
Chris Lattnerc3aae252005-01-07 07:46:32 +00001410 // Finally, fold operations that do not require constants.
1411 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001412 case ISD::TokenFactor:
1413 if (N1.getOpcode() == ISD::EntryToken)
1414 return N2;
1415 if (N2.getOpcode() == ISD::EntryToken)
1416 return N1;
1417 break;
1418
Chris Lattnerc3aae252005-01-07 07:46:32 +00001419 case ISD::AND:
1420 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001421 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1422 SDNode *LHS = N1.Val, *RHS = N2.Val;
1423 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1424 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1425 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1426 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001427
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001428 if (LR == RR && isa<ConstantSDNode>(LR) &&
1429 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1430 // (X != 0) | (Y != 0) -> (X|Y != 0)
1431 // (X == 0) & (Y == 0) -> (X|Y == 0)
1432 // (X < 0) | (Y < 0) -> (X|Y < 0)
1433 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1434 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1435 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1436 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1437 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1438 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001439
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001440 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1441 // (X == -1) & (Y == -1) -> (X&Y == -1)
1442 // (X != -1) | (Y != -1) -> (X&Y != -1)
1443 // (X > -1) | (Y > -1) -> (X&Y > -1)
1444 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1445 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1446 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1447 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1448 LR, Op2);
1449 // (X > -1) & (Y > -1) -> (X|Y > -1)
1450 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1451 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1452 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 }
1454 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001455
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001456 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1457 if (LL == RR && LR == RL) {
1458 Op2 = ISD::getSetCCSwappedOperands(Op2);
1459 goto MatchedBackwards;
1460 }
1461
1462 if (LL == RL && LR == RR) {
1463 MatchedBackwards:
1464 ISD::CondCode Result;
1465 bool isInteger = MVT::isInteger(LL.getValueType());
1466 if (Opcode == ISD::OR)
1467 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1468 else
1469 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1470
1471 if (Result != ISD::SETCC_INVALID)
1472 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1473 }
1474 }
1475
Chris Lattner7467c9b2005-04-18 04:11:19 +00001476 // and/or zext(a), zext(b) -> zext(and/or a, b)
1477 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1478 N2.getOpcode() == ISD::ZERO_EXTEND &&
1479 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1480 return getNode(ISD::ZERO_EXTEND, VT,
1481 getNode(Opcode, N1.getOperand(0).getValueType(),
1482 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001483 break;
1484 case ISD::XOR:
1485 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1486 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001487 case ISD::ADD:
1488 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1489 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1490 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1491 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001492 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1493 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1494 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1495 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1496 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1497 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001498 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1499 !MVT::isFloatingPoint(N2.getValueType()))
1500 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001501 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001502 case ISD::SUB:
1503 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001504 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001505 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001506 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001507 if (N1.Val->getOperand(1) == N2 &&
1508 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001509 return N1.Val->getOperand(0); // (A+B)-B == A
1510 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001511 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1512 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001513 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001514 case ISD::FP_ROUND_INREG:
1515 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1516 break;
1517 case ISD::SIGN_EXTEND_INREG: {
1518 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1519 if (EVT == VT) return N1; // Not actually extending
1520
1521 // If we are sign extending an extension, use the original source.
1522 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1523 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1524 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001525
Chris Lattner15e4b012005-07-10 00:07:11 +00001526 // If we are sign extending a sextload, return just the load.
1527 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001528 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001529 return N1;
1530
1531 // If we are extending the result of a setcc, and we already know the
1532 // contents of the top bits, eliminate the extension.
1533 if (N1.getOpcode() == ISD::SETCC &&
1534 TLI.getSetCCResultContents() ==
1535 TargetLowering::ZeroOrNegativeOneSetCCResult)
1536 return N1;
1537
1538 // If we are sign extending the result of an (and X, C) operation, and we
1539 // know the extended bits are zeros already, don't do the extend.
1540 if (N1.getOpcode() == ISD::AND)
1541 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1542 uint64_t Mask = N1C->getValue();
1543 unsigned NumBits = MVT::getSizeInBits(EVT);
1544 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1545 return N1;
1546 }
1547 break;
1548 }
1549
Nate Begemaneea805e2005-04-13 21:23:31 +00001550 // FIXME: figure out how to safely handle things like
1551 // int foo(int x) { return 1 << (x & 255); }
1552 // int bar() { return foo(256); }
1553#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001554 case ISD::SHL:
1555 case ISD::SRL:
1556 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001557 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001558 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001559 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001560 else if (N2.getOpcode() == ISD::AND)
1561 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1562 // If the and is only masking out bits that cannot effect the shift,
1563 // eliminate the and.
1564 unsigned NumBits = MVT::getSizeInBits(VT);
1565 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1566 return getNode(Opcode, VT, N1, N2.getOperand(0));
1567 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001568 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001569#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001570 }
1571
Chris Lattner27e9b412005-05-11 18:57:39 +00001572 // Memoize this node if possible.
1573 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001574 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001575 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1576 if (BON) return SDOperand(BON, 0);
1577
1578 BON = N = new SDNode(Opcode, N1, N2);
1579 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001580 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001581 }
1582
Chris Lattner3e011362005-05-14 07:45:46 +00001583 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001584 AllNodes.push_back(N);
1585 return SDOperand(N, 0);
1586}
1587
Chris Lattner88de6e72005-05-12 00:17:04 +00001588// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001589// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001590void SDNode::setAdjCallChain(SDOperand N) {
1591 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001592 assert((getOpcode() == ISD::CALLSEQ_START ||
1593 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001594
1595 Operands[0].Val->removeUser(this);
1596 Operands[0] = N;
1597 N.Val->Uses.push_back(this);
1598}
1599
1600
1601
Chris Lattnerc3aae252005-01-07 07:46:32 +00001602SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001603 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001604 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001605 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1606 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001607 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001608
1609 // Loads have a token chain.
1610 N->setValueTypes(VT, MVT::Other);
1611 AllNodes.push_back(N);
1612 return SDOperand(N, 0);
1613}
1614
Chris Lattner5f056bf2005-07-10 01:55:33 +00001615
1616SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1617 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1618 MVT::ValueType EVT) {
1619 std::vector<SDOperand> Ops;
1620 Ops.reserve(4);
1621 Ops.push_back(Chain);
1622 Ops.push_back(Ptr);
1623 Ops.push_back(SV);
1624 Ops.push_back(getValueType(EVT));
1625 std::vector<MVT::ValueType> VTs;
1626 VTs.reserve(2);
1627 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1628 return getNode(Opcode, VTs, Ops);
1629}
1630
Chris Lattnerc3aae252005-01-07 07:46:32 +00001631SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1632 SDOperand N1, SDOperand N2, SDOperand N3) {
1633 // Perform various simplifications.
1634 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1635 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1636 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1637 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001638 case ISD::SETCC: {
1639 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001640 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001641 if (Simp.Val) return Simp;
1642 break;
1643 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001644 case ISD::SELECT:
1645 if (N1C)
1646 if (N1C->getValue())
1647 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001648 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001649 return N3; // select false, X, Y -> Y
1650
1651 if (N2 == N3) return N2; // select C, X, X -> X
1652
1653 if (VT == MVT::i1) { // Boolean SELECT
1654 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001655 if (N2C->getValue()) // select C, 1, X -> C | X
1656 return getNode(ISD::OR, VT, N1, N3);
1657 else // select C, 0, X -> ~C & X
1658 return getNode(ISD::AND, VT,
1659 getNode(ISD::XOR, N1.getValueType(), N1,
1660 getConstant(1, N1.getValueType())), N3);
1661 } else if (N3C) {
1662 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1663 return getNode(ISD::OR, VT,
1664 getNode(ISD::XOR, N1.getValueType(), N1,
1665 getConstant(1, N1.getValueType())), N2);
1666 else // select C, X, 0 -> C & X
1667 return getNode(ISD::AND, VT, N1, N2);
1668 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001669
1670 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1671 return getNode(ISD::OR, VT, N1, N3);
1672 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1673 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001674 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001675 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001676 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1677 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001678 if (Simp.Val) return Simp;
1679 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001680 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001681 case ISD::BRCOND:
1682 if (N2C)
1683 if (N2C->getValue()) // Unconditional branch
1684 return getNode(ISD::BR, MVT::Other, N1, N3);
1685 else
1686 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001687 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001688 }
1689
Chris Lattner385328c2005-05-14 07:42:29 +00001690 std::vector<SDOperand> Ops;
1691 Ops.reserve(3);
1692 Ops.push_back(N1);
1693 Ops.push_back(N2);
1694 Ops.push_back(N3);
1695
1696 // Memoize nodes.
1697 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1698 if (N) return SDOperand(N, 0);
1699
1700 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001701 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001702 AllNodes.push_back(N);
1703 return SDOperand(N, 0);
1704}
1705
1706SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001707 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001708 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001709 std::vector<SDOperand> Ops;
1710 Ops.reserve(4);
1711 Ops.push_back(N1);
1712 Ops.push_back(N2);
1713 Ops.push_back(N3);
1714 Ops.push_back(N4);
1715 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001716}
1717
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001718SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1719 SDOperand N1, SDOperand N2, SDOperand N3,
1720 SDOperand N4, SDOperand N5) {
1721 std::vector<SDOperand> Ops;
1722 Ops.reserve(5);
1723 Ops.push_back(N1);
1724 Ops.push_back(N2);
1725 Ops.push_back(N3);
1726 Ops.push_back(N4);
1727 Ops.push_back(N5);
1728 return getNode(Opcode, VT, Ops);
1729}
1730
1731
Chris Lattner0437cdd2005-05-09 04:14:13 +00001732SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001733 assert((!V || isa<PointerType>(V->getType())) &&
1734 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001735 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1736 if (N) return SDOperand(N, 0);
1737
1738 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001739 AllNodes.push_back(N);
1740 return SDOperand(N, 0);
1741}
1742
1743SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001744 std::vector<SDOperand> &Ops) {
1745 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001746 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001747 case 1: return getNode(Opcode, VT, Ops[0]);
1748 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1749 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001750 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001751 }
Chris Lattneref847df2005-04-09 03:27:28 +00001752
Chris Lattner89c34632005-05-14 06:20:26 +00001753 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001754 switch (Opcode) {
1755 default: break;
1756 case ISD::BRCONDTWOWAY:
1757 if (N1C)
1758 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001759 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001760 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001761 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001762 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001763 case ISD::BRTWOWAY_CC:
1764 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1765 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1766 "LHS and RHS of comparison must have same type!");
1767 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001768 case ISD::TRUNCSTORE: {
1769 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1770 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1771#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1772 // If this is a truncating store of a constant, convert to the desired type
1773 // and store it instead.
1774 if (isa<Constant>(Ops[0])) {
1775 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1776 if (isa<Constant>(Op))
1777 N1 = Op;
1778 }
1779 // Also for ConstantFP?
1780#endif
1781 if (Ops[0].getValueType() == EVT) // Normal store?
1782 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1783 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1784 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1785 "Can't do FP-INT conversion!");
1786 break;
1787 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001788 case ISD::SELECT_CC: {
1789 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1790 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1791 "LHS and RHS of condition must have same type!");
1792 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1793 "True and False arms of SelectCC must have same type!");
1794 assert(Ops[2].getValueType() == VT &&
1795 "select_cc node must be of same type as true and false value!");
1796 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1797 cast<CondCodeSDNode>(Ops[4])->get());
1798 if (Simp.Val) return Simp;
1799 break;
1800 }
1801 case ISD::BR_CC: {
1802 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1803 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1804 "LHS/RHS of comparison should match types!");
1805 // Use SimplifySetCC to simplify SETCC's.
1806 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
1807 cast<CondCodeSDNode>(Ops[1])->get());
1808 if (Simp.Val) {
1809 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
1810 if (C->getValue() & 1) // Unconditional branch
1811 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
1812 else
1813 return Ops[0]; // Unconditional Fall through
1814 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
1815 Ops[2] = Simp.getOperand(0);
1816 Ops[3] = Simp.getOperand(1);
1817 Ops[1] = Simp.getOperand(2);
1818 }
1819 }
1820 break;
1821 }
Chris Lattneref847df2005-04-09 03:27:28 +00001822 }
1823
Chris Lattner385328c2005-05-14 07:42:29 +00001824 // Memoize nodes.
1825 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1826 if (N) return SDOperand(N, 0);
1827 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001828 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001829 AllNodes.push_back(N);
1830 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001831}
1832
Chris Lattner89c34632005-05-14 06:20:26 +00001833SDOperand SelectionDAG::getNode(unsigned Opcode,
1834 std::vector<MVT::ValueType> &ResultTys,
1835 std::vector<SDOperand> &Ops) {
1836 if (ResultTys.size() == 1)
1837 return getNode(Opcode, ResultTys[0], Ops);
1838
Chris Lattner5f056bf2005-07-10 01:55:33 +00001839 switch (Opcode) {
1840 case ISD::EXTLOAD:
1841 case ISD::SEXTLOAD:
1842 case ISD::ZEXTLOAD: {
1843 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1844 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1845 // If they are asking for an extending load from/to the same thing, return a
1846 // normal load.
1847 if (ResultTys[0] == EVT)
1848 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1849 assert(EVT < ResultTys[0] &&
1850 "Should only be an extending load, not truncating!");
1851 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1852 "Cannot sign/zero extend a FP load!");
1853 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1854 "Cannot convert from FP to Int or Int -> FP!");
1855 break;
1856 }
1857
Chris Lattnere89083a2005-05-14 07:25:05 +00001858 // FIXME: figure out how to safely handle things like
1859 // int foo(int x) { return 1 << (x & 255); }
1860 // int bar() { return foo(256); }
1861#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001862 case ISD::SRA_PARTS:
1863 case ISD::SRL_PARTS:
1864 case ISD::SHL_PARTS:
1865 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001866 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001867 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1868 else if (N3.getOpcode() == ISD::AND)
1869 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1870 // If the and is only masking out bits that cannot effect the shift,
1871 // eliminate the and.
1872 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1873 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1874 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1875 }
1876 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001877#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001878 }
Chris Lattner89c34632005-05-14 06:20:26 +00001879
1880 // Memoize the node.
1881 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1882 Ops))];
1883 if (N) return SDOperand(N, 0);
1884 N = new SDNode(Opcode, Ops);
1885 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001886 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001887 return SDOperand(N, 0);
1888}
1889
Chris Lattner149c58c2005-08-16 18:17:10 +00001890
1891/// SelectNodeTo - These are used for target selectors to *mutate* the
1892/// specified node to have the specified return type, Target opcode, and
1893/// operands. Note that target opcodes are stored as
1894/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1895void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
Chris Lattner7651fa42005-08-24 23:00:29 +00001896 unsigned TargetOpc) {
1897 RemoveNodeFromCSEMaps(N);
1898 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1899 N->setValueTypes(VT);
1900}
1901void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
Chris Lattner149c58c2005-08-16 18:17:10 +00001902 unsigned TargetOpc, SDOperand Op1) {
1903 RemoveNodeFromCSEMaps(N);
1904 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1905 N->setValueTypes(VT);
1906 N->setOperands(Op1);
1907}
1908void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1909 unsigned TargetOpc, SDOperand Op1,
1910 SDOperand Op2) {
1911 RemoveNodeFromCSEMaps(N);
1912 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1913 N->setValueTypes(VT);
1914 N->setOperands(Op1, Op2);
1915}
Chris Lattner99badda2005-08-21 19:48:59 +00001916void SelectionDAG::SelectNodeTo(SDNode *N,
1917 MVT::ValueType VT1, MVT::ValueType VT2,
1918 unsigned TargetOpc, SDOperand Op1,
1919 SDOperand Op2) {
1920 RemoveNodeFromCSEMaps(N);
1921 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1922 N->setValueTypes(VT1, VT2);
1923 N->setOperands(Op1, Op2);
1924}
Chris Lattner149c58c2005-08-16 18:17:10 +00001925void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1926 unsigned TargetOpc, SDOperand Op1,
1927 SDOperand Op2, SDOperand Op3) {
1928 RemoveNodeFromCSEMaps(N);
1929 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1930 N->setValueTypes(VT);
1931 N->setOperands(Op1, Op2, Op3);
1932}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001933void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT1,
1934 MVT::ValueType VT2,
1935 unsigned TargetOpc, SDOperand Op1,
1936 SDOperand Op2, SDOperand Op3) {
1937 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1939 N->setValueTypes(VT1, VT2);
1940 N->setOperands(Op1, Op2, Op3);
1941}
1942
Nate Begeman294a0a12005-08-18 07:30:15 +00001943void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1944 unsigned TargetOpc, SDOperand Op1,
1945 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1946 RemoveNodeFromCSEMaps(N);
1947 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1948 N->setValueTypes(VT);
1949 N->setOperands(Op1, Op2, Op3, Op4);
1950}
Chris Lattner6b09a292005-08-21 18:49:33 +00001951void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1952 unsigned TargetOpc, SDOperand Op1,
1953 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1954 SDOperand Op5) {
1955 RemoveNodeFromCSEMaps(N);
1956 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1957 N->setValueTypes(VT);
1958 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1959}
Chris Lattner149c58c2005-08-16 18:17:10 +00001960
Chris Lattner8b8749f2005-08-17 19:00:20 +00001961/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1962/// This can cause recursive merging of nodes in the DAG.
1963///
1964void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1965 assert(From != To && "Cannot replace uses of with self");
1966 while (!From->use_empty()) {
1967 // Process users until they are all gone.
1968 SDNode *U = *From->use_begin();
1969
1970 // This node is about to morph, remove its old self from the CSE maps.
1971 RemoveNodeFromCSEMaps(U);
1972
1973 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1974 if (U->getOperand(i).Val == From) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001975 assert(U->getOperand(i).getValueType() ==
Chris Lattner8b8749f2005-08-17 19:00:20 +00001976 To->getValueType(U->getOperand(i).ResNo));
1977 From->removeUser(U);
1978 U->Operands[i].Val = To;
1979 To->addUser(U);
1980 }
1981
1982 // Now that we have modified U, add it back to the CSE maps. If it already
1983 // exists there, recursively merge the results together.
1984 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
1985 ReplaceAllUsesWith(U, Existing);
1986 // U is now dead.
1987 }
1988}
1989
Chris Lattner7b2880c2005-08-24 22:44:39 +00001990void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
1991 const std::vector<SDOperand> &To) {
1992 assert(From->getNumValues() == To.size() &&
1993 "Incorrect number of values to replace with!");
1994 if (To.size() == 1 && To[0].ResNo == 0) {
1995 // Degenerate case handled above.
1996 ReplaceAllUsesWith(From, To[0].Val);
1997 return;
1998 }
1999
2000 while (!From->use_empty()) {
2001 // Process users until they are all gone.
2002 SDNode *U = *From->use_begin();
2003
2004 // This node is about to morph, remove its old self from the CSE maps.
2005 RemoveNodeFromCSEMaps(U);
2006
2007 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2008 if (U->getOperand(i).Val == From) {
2009 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
2010 assert(U->getOperand(i).getValueType() == ToOp.getValueType());
2011 From->removeUser(U);
2012 U->Operands[i] = ToOp;
2013 ToOp.Val->addUser(U);
2014 }
2015
2016 // Now that we have modified U, add it back to the CSE maps. If it already
2017 // exists there, recursively merge the results together.
2018 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2019 ReplaceAllUsesWith(U, Existing);
2020 // U is now dead.
2021 }
2022}
2023
2024
Jim Laskey58b968b2005-08-17 20:08:02 +00002025//===----------------------------------------------------------------------===//
2026// SDNode Class
2027//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002028
Chris Lattner5c884562005-01-12 18:37:47 +00002029/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2030/// indicated value. This method ignores uses of other values defined by this
2031/// operation.
2032bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2033 assert(Value < getNumValues() && "Bad value!");
2034
2035 // If there is only one value, this is easy.
2036 if (getNumValues() == 1)
2037 return use_size() == NUses;
2038 if (Uses.size() < NUses) return false;
2039
2040 SDOperand TheValue(this, Value);
2041
2042 std::set<SDNode*> UsersHandled;
2043
2044 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2045 UI != E; ++UI) {
2046 SDNode *User = *UI;
2047 if (User->getNumOperands() == 1 ||
2048 UsersHandled.insert(User).second) // First time we've seen this?
2049 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2050 if (User->getOperand(i) == TheValue) {
2051 if (NUses == 0)
2052 return false; // too many uses
2053 --NUses;
2054 }
2055 }
2056
2057 // Found exactly the right number of uses?
2058 return NUses == 0;
2059}
2060
2061
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002062const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002063 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002064 default:
2065 if (getOpcode() < ISD::BUILTIN_OP_END)
2066 return "<<Unknown DAG Node>>";
2067 else {
2068 if (G)
2069 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2070 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2071 return "<<Unknown Target Node>>";
2072 }
2073
Andrew Lenharth95762122005-03-31 21:24:06 +00002074 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002075 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002076 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002077 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002078 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002079 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002080 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002081 case ISD::ConstantFP: return "ConstantFP";
2082 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002083 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002084 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002085 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002086 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002087 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002088 case ISD::ExternalSymbol: return "ExternalSymbol";
2089 case ISD::ConstantPool: return "ConstantPoolIndex";
2090 case ISD::CopyToReg: return "CopyToReg";
2091 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002092 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002093 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002094
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002095 // Unary operators
2096 case ISD::FABS: return "fabs";
2097 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002098 case ISD::FSQRT: return "fsqrt";
2099 case ISD::FSIN: return "fsin";
2100 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002101
2102 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002103 case ISD::ADD: return "add";
2104 case ISD::SUB: return "sub";
2105 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002106 case ISD::MULHU: return "mulhu";
2107 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002108 case ISD::SDIV: return "sdiv";
2109 case ISD::UDIV: return "udiv";
2110 case ISD::SREM: return "srem";
2111 case ISD::UREM: return "urem";
2112 case ISD::AND: return "and";
2113 case ISD::OR: return "or";
2114 case ISD::XOR: return "xor";
2115 case ISD::SHL: return "shl";
2116 case ISD::SRA: return "sra";
2117 case ISD::SRL: return "srl";
2118
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002119 case ISD::SETCC: return "setcc";
2120 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002121 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002122 case ISD::ADD_PARTS: return "add_parts";
2123 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002124 case ISD::SHL_PARTS: return "shl_parts";
2125 case ISD::SRA_PARTS: return "sra_parts";
2126 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002127
Chris Lattner7f644642005-04-28 21:44:03 +00002128 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002129 case ISD::SIGN_EXTEND: return "sign_extend";
2130 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002131 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002132 case ISD::TRUNCATE: return "truncate";
2133 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002134 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002135 case ISD::FP_EXTEND: return "fp_extend";
2136
2137 case ISD::SINT_TO_FP: return "sint_to_fp";
2138 case ISD::UINT_TO_FP: return "uint_to_fp";
2139 case ISD::FP_TO_SINT: return "fp_to_sint";
2140 case ISD::FP_TO_UINT: return "fp_to_uint";
2141
2142 // Control flow instructions
2143 case ISD::BR: return "br";
2144 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002145 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002146 case ISD::BR_CC: return "br_cc";
2147 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002148 case ISD::RET: return "ret";
2149 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002150 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002151 case ISD::CALLSEQ_START: return "callseq_start";
2152 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002153
2154 // Other operators
2155 case ISD::LOAD: return "load";
2156 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002157 case ISD::EXTLOAD: return "extload";
2158 case ISD::SEXTLOAD: return "sextload";
2159 case ISD::ZEXTLOAD: return "zextload";
2160 case ISD::TRUNCSTORE: return "truncstore";
2161
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002162 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2163 case ISD::EXTRACT_ELEMENT: return "extract_element";
2164 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002165 case ISD::MEMSET: return "memset";
2166 case ISD::MEMCPY: return "memcpy";
2167 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002168
Chris Lattner276260b2005-05-11 04:50:30 +00002169 // Bit counting
2170 case ISD::CTPOP: return "ctpop";
2171 case ISD::CTTZ: return "cttz";
2172 case ISD::CTLZ: return "ctlz";
2173
2174 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002175 case ISD::READPORT: return "readport";
2176 case ISD::WRITEPORT: return "writeport";
2177 case ISD::READIO: return "readio";
2178 case ISD::WRITEIO: return "writeio";
2179
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002180 case ISD::CONDCODE:
2181 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002182 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002183 case ISD::SETOEQ: return "setoeq";
2184 case ISD::SETOGT: return "setogt";
2185 case ISD::SETOGE: return "setoge";
2186 case ISD::SETOLT: return "setolt";
2187 case ISD::SETOLE: return "setole";
2188 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002189
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002190 case ISD::SETO: return "seto";
2191 case ISD::SETUO: return "setuo";
2192 case ISD::SETUEQ: return "setue";
2193 case ISD::SETUGT: return "setugt";
2194 case ISD::SETUGE: return "setuge";
2195 case ISD::SETULT: return "setult";
2196 case ISD::SETULE: return "setule";
2197 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002198
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002199 case ISD::SETEQ: return "seteq";
2200 case ISD::SETGT: return "setgt";
2201 case ISD::SETGE: return "setge";
2202 case ISD::SETLT: return "setlt";
2203 case ISD::SETLE: return "setle";
2204 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002205 }
2206 }
2207}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002208
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002209void SDNode::dump() const { dump(0); }
2210void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002211 std::cerr << (void*)this << ": ";
2212
2213 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2214 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002215 if (getValueType(i) == MVT::Other)
2216 std::cerr << "ch";
2217 else
2218 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002219 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002220 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002221
2222 std::cerr << " ";
2223 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2224 if (i) std::cerr << ", ";
2225 std::cerr << (void*)getOperand(i).Val;
2226 if (unsigned RN = getOperand(i).ResNo)
2227 std::cerr << ":" << RN;
2228 }
2229
2230 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2231 std::cerr << "<" << CSDN->getValue() << ">";
2232 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2233 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002234 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002235 dyn_cast<GlobalAddressSDNode>(this)) {
2236 std::cerr << "<";
2237 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002238 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002239 std::cerr << "<" << FIDN->getIndex() << ">";
2240 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2241 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002242 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002243 std::cerr << "<";
2244 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2245 if (LBB)
2246 std::cerr << LBB->getName() << " ";
2247 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002248 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002249 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2250 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2251 } else {
2252 std::cerr << " #" << R->getReg();
2253 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002254 } else if (const ExternalSymbolSDNode *ES =
2255 dyn_cast<ExternalSymbolSDNode>(this)) {
2256 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002257 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2258 if (M->getValue())
2259 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2260 else
2261 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002262 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2263 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002264 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002265}
2266
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002267static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002268 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2269 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002270 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002271 else
2272 std::cerr << "\n" << std::string(indent+2, ' ')
2273 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002274
Chris Lattnerea946cd2005-01-09 20:38:33 +00002275
2276 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002277 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002278}
2279
Chris Lattnerc3aae252005-01-07 07:46:32 +00002280void SelectionDAG::dump() const {
2281 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002282 std::vector<SDNode*> Nodes(AllNodes);
2283 std::sort(Nodes.begin(), Nodes.end());
2284
2285 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002286 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002287 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002288 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002289
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002290 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002291
Chris Lattnerc3aae252005-01-07 07:46:32 +00002292 std::cerr << "\n\n";
2293}
2294