blob: 72352f10fde37e928c63522fae8ec134db90203d [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;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000270 case ISD::TargetConstantPool:
271 TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
272 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000273 case ISD::BasicBlock:
274 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
275 break;
276 case ISD::ExternalSymbol:
277 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
278 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000279 case ISD::VALUETYPE:
280 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
281 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000282 case ISD::Register:
283 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
284 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000285 case ISD::SRCVALUE: {
286 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
287 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
288 break;
289 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000290 case ISD::LOAD:
291 Loads.erase(std::make_pair(N->getOperand(1),
292 std::make_pair(N->getOperand(0),
293 N->getValueType(0))));
294 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000295 default:
296 if (N->getNumOperands() == 1)
297 UnaryOps.erase(std::make_pair(N->getOpcode(),
298 std::make_pair(N->getOperand(0),
299 N->getValueType(0))));
300 else if (N->getNumOperands() == 2)
301 BinaryOps.erase(std::make_pair(N->getOpcode(),
302 std::make_pair(N->getOperand(0),
303 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000304 else if (N->getNumValues() == 1) {
305 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
306 OneResultNodes.erase(std::make_pair(N->getOpcode(),
307 std::make_pair(N->getValueType(0),
308 Ops)));
309 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000310 // Remove the node from the ArbitraryNodes map.
311 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
312 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
313 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
314 std::make_pair(RV, Ops)));
315 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000316 break;
317 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000318}
319
Chris Lattner8b8749f2005-08-17 19:00:20 +0000320/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
321/// has been taken out and modified in some way. If the specified node already
322/// exists in the CSE maps, do not modify the maps, but return the existing node
323/// instead. If it doesn't exist, add it and return null.
324///
325SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
326 assert(N->getNumOperands() && "This is a leaf node!");
327 if (N->getOpcode() == ISD::LOAD) {
328 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
329 std::make_pair(N->getOperand(0),
330 N->getValueType(0)))];
331 if (L) return L;
332 L = N;
333 } else if (N->getNumOperands() == 1) {
334 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
335 std::make_pair(N->getOperand(0),
336 N->getValueType(0)))];
337 if (U) return U;
338 U = N;
339 } else if (N->getNumOperands() == 2) {
340 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
341 std::make_pair(N->getOperand(0),
342 N->getOperand(1)))];
343 if (B) return B;
344 B = N;
345 } else if (N->getNumValues() == 1) {
346 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
347 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
348 std::make_pair(N->getValueType(0), Ops))];
349 if (ORN) return ORN;
350 ORN = N;
351 } else {
352 // Remove the node from the ArbitraryNodes map.
353 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
354 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
355 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
356 std::make_pair(RV, Ops))];
357 if (AN) return AN;
358 AN = N;
359 }
360 return 0;
361
362}
363
364
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000365
Chris Lattner78ec3112003-08-11 14:57:33 +0000366SelectionDAG::~SelectionDAG() {
367 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
368 delete AllNodes[i];
369}
370
Chris Lattner0f2287b2005-04-13 02:38:18 +0000371SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000372 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000373 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000374 return getNode(ISD::AND, Op.getValueType(), Op,
375 getConstant(Imm, Op.getValueType()));
376}
377
Chris Lattnerc3aae252005-01-07 07:46:32 +0000378SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
379 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
380 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000381 if (VT != MVT::i64)
382 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000383
Chris Lattnerc3aae252005-01-07 07:46:32 +0000384 SDNode *&N = Constants[std::make_pair(Val, VT)];
385 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000386 N = new ConstantSDNode(false, Val, VT);
387 AllNodes.push_back(N);
388 return SDOperand(N, 0);
389}
390
391SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
392 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
393 // Mask out any bits that are not valid for this constant.
394 if (VT != MVT::i64)
395 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
396
397 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
398 if (N) return SDOperand(N, 0);
399 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000400 AllNodes.push_back(N);
401 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000402}
403
Chris Lattnerc3aae252005-01-07 07:46:32 +0000404SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
405 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
406 if (VT == MVT::f32)
407 Val = (float)Val; // Mask out extra precision.
408
Chris Lattnerd8658612005-02-17 20:17:32 +0000409 // Do the map lookup using the actual bit pattern for the floating point
410 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
411 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000412 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000413 if (N) return SDOperand(N, 0);
414 N = new ConstantFPSDNode(Val, VT);
415 AllNodes.push_back(N);
416 return SDOperand(N, 0);
417}
418
419
420
421SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
422 MVT::ValueType VT) {
423 SDNode *&N = GlobalValues[GV];
424 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000425 N = new GlobalAddressSDNode(false, GV, VT);
426 AllNodes.push_back(N);
427 return SDOperand(N, 0);
428}
429
430SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
431 MVT::ValueType VT) {
432 SDNode *&N = TargetGlobalValues[GV];
433 if (N) return SDOperand(N, 0);
434 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000435 AllNodes.push_back(N);
436 return SDOperand(N, 0);
437}
438
439SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
440 SDNode *&N = FrameIndices[FI];
441 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000442 N = new FrameIndexSDNode(FI, VT, false);
443 AllNodes.push_back(N);
444 return SDOperand(N, 0);
445}
446
447SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
448 SDNode *&N = TargetFrameIndices[FI];
449 if (N) return SDOperand(N, 0);
450 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000451 AllNodes.push_back(N);
452 return SDOperand(N, 0);
453}
454
455SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
456 SDNode *N = ConstantPoolIndices[CPIdx];
457 if (N) return SDOperand(N, 0);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000458 N = new ConstantPoolSDNode(CPIdx, VT, false);
459 AllNodes.push_back(N);
460 return SDOperand(N, 0);
461}
462
463SDOperand SelectionDAG::getTargetConstantPool(unsigned CPIdx,
464 MVT::ValueType VT) {
465 SDNode *N = TargetConstantPoolIndices[CPIdx];
466 if (N) return SDOperand(N, 0);
467 N = new ConstantPoolSDNode(CPIdx, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000468 AllNodes.push_back(N);
469 return SDOperand(N, 0);
470}
471
472SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
473 SDNode *&N = BBNodes[MBB];
474 if (N) return SDOperand(N, 0);
475 N = new BasicBlockSDNode(MBB);
476 AllNodes.push_back(N);
477 return SDOperand(N, 0);
478}
479
Chris Lattner15e4b012005-07-10 00:07:11 +0000480SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
481 if ((unsigned)VT >= ValueTypeNodes.size())
482 ValueTypeNodes.resize(VT+1);
483 if (ValueTypeNodes[VT] == 0) {
484 ValueTypeNodes[VT] = new VTSDNode(VT);
485 AllNodes.push_back(ValueTypeNodes[VT]);
486 }
487
488 return SDOperand(ValueTypeNodes[VT], 0);
489}
490
Chris Lattnerc3aae252005-01-07 07:46:32 +0000491SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
492 SDNode *&N = ExternalSymbols[Sym];
493 if (N) return SDOperand(N, 0);
494 N = new ExternalSymbolSDNode(Sym, VT);
495 AllNodes.push_back(N);
496 return SDOperand(N, 0);
497}
498
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000499SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
500 if ((unsigned)Cond >= CondCodeNodes.size())
501 CondCodeNodes.resize(Cond+1);
502
Chris Lattner079a27a2005-08-09 20:40:02 +0000503 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000504 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000505 AllNodes.push_back(CondCodeNodes[Cond]);
506 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000507 return SDOperand(CondCodeNodes[Cond], 0);
508}
509
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000510SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
511 if (Reg >= RegNodes.size())
512 RegNodes.resize(Reg+1);
513 RegisterSDNode *&Result = RegNodes[Reg];
514 if (Result) {
515 assert(Result->getValueType(0) == VT &&
516 "Inconsistent value types for machine registers");
517 } else {
518 Result = new RegisterSDNode(Reg, VT);
519 AllNodes.push_back(Result);
520 }
521 return SDOperand(Result, 0);
522}
523
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000524SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
525 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000526 // These setcc operations always fold.
527 switch (Cond) {
528 default: break;
529 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000530 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000531 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000532 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000533 }
534
Chris Lattner67255a12005-04-07 18:14:58 +0000535 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
536 uint64_t C2 = N2C->getValue();
537 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
538 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000539
Chris Lattnerc3aae252005-01-07 07:46:32 +0000540 // Sign extend the operands if required
541 if (ISD::isSignedIntSetCC(Cond)) {
542 C1 = N1C->getSignExtended();
543 C2 = N2C->getSignExtended();
544 }
545
546 switch (Cond) {
547 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000548 case ISD::SETEQ: return getConstant(C1 == C2, VT);
549 case ISD::SETNE: return getConstant(C1 != C2, VT);
550 case ISD::SETULT: return getConstant(C1 < C2, VT);
551 case ISD::SETUGT: return getConstant(C1 > C2, VT);
552 case ISD::SETULE: return getConstant(C1 <= C2, VT);
553 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
554 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
555 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
556 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
557 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000558 }
Chris Lattner24673922005-04-07 18:58:54 +0000559 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000560 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000561 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
562 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
563
564 // If the comparison constant has bits in the upper part, the
565 // zero-extended value could never match.
566 if (C2 & (~0ULL << InSize)) {
567 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
568 switch (Cond) {
569 case ISD::SETUGT:
570 case ISD::SETUGE:
571 case ISD::SETEQ: return getConstant(0, VT);
572 case ISD::SETULT:
573 case ISD::SETULE:
574 case ISD::SETNE: return getConstant(1, VT);
575 case ISD::SETGT:
576 case ISD::SETGE:
577 // True if the sign bit of C2 is set.
578 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
579 case ISD::SETLT:
580 case ISD::SETLE:
581 // True if the sign bit of C2 isn't set.
582 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
583 default:
584 break;
585 }
586 }
587
588 // Otherwise, we can perform the comparison with the low bits.
589 switch (Cond) {
590 case ISD::SETEQ:
591 case ISD::SETNE:
592 case ISD::SETUGT:
593 case ISD::SETUGE:
594 case ISD::SETULT:
595 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000596 return getSetCC(VT, N1.getOperand(0),
597 getConstant(C2, N1.getOperand(0).getValueType()),
598 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000599 default:
600 break; // todo, be more careful with signed comparisons
601 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000602 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
603 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
604 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
605 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
606 MVT::ValueType ExtDstTy = N1.getValueType();
607 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
608
609 // If the extended part has any inconsistent bits, it cannot ever
610 // compare equal. In other words, they have to be all ones or all
611 // zeros.
612 uint64_t ExtBits =
613 (~0ULL >> 64-ExtSrcTyBits) & (~0ULL << (ExtDstTyBits-1));
614 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
615 return getConstant(Cond == ISD::SETNE, VT);
616
617 // Otherwise, make this a use of a zext.
618 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
619 getConstant(C2 & (~0ULL >> 64-ExtSrcTyBits), ExtDstTy),
620 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000621 }
622
Chris Lattner67255a12005-04-07 18:14:58 +0000623 uint64_t MinVal, MaxVal;
624 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
625 if (ISD::isSignedIntSetCC(Cond)) {
626 MinVal = 1ULL << (OperandBitSize-1);
627 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
628 MaxVal = ~0ULL >> (65-OperandBitSize);
629 else
630 MaxVal = 0;
631 } else {
632 MinVal = 0;
633 MaxVal = ~0ULL >> (64-OperandBitSize);
634 }
635
636 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
637 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
638 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
639 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000640 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
641 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000642 }
643
644 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
645 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
646 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000647 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
648 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000649 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000650
Nate Begeman72ea2812005-04-14 08:56:52 +0000651 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
652 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000653
Nate Begeman72ea2812005-04-14 08:56:52 +0000654 // Canonicalize setgt X, Min --> setne X, Min
655 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000656 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000657
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000658 // If we have setult X, 1, turn it into seteq X, 0
659 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000660 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
661 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000662 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000663 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000664 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
665 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000666
667 // If we have "setcc X, C1", check to see if we can shrink the immediate
668 // by changing cc.
669
670 // SETUGT X, SINTMAX -> SETLT X, 0
671 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
672 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000673 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000674
675 // FIXME: Implement the rest of these.
676
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000677
678 // Fold bit comparisons when we can.
679 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
680 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
681 if (ConstantSDNode *AndRHS =
682 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
683 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
684 // Perform the xform if the AND RHS is a single bit.
685 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
686 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000687 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000688 TLI.getShiftAmountTy()));
689 }
690 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
691 // (X & 8) == 8 --> (X & 8) >> 3
692 // Perform the xform if C2 is a single bit.
693 if ((C2 & (C2-1)) == 0) {
694 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000695 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000696 }
697 }
698 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000699 }
Chris Lattner67255a12005-04-07 18:14:58 +0000700 } else if (isa<ConstantSDNode>(N1.Val)) {
701 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000702 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000703 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000704
705 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
706 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
707 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000708
Chris Lattnerc3aae252005-01-07 07:46:32 +0000709 switch (Cond) {
710 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000711 case ISD::SETEQ: return getConstant(C1 == C2, VT);
712 case ISD::SETNE: return getConstant(C1 != C2, VT);
713 case ISD::SETLT: return getConstant(C1 < C2, VT);
714 case ISD::SETGT: return getConstant(C1 > C2, VT);
715 case ISD::SETLE: return getConstant(C1 <= C2, VT);
716 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000717 }
718 } else {
719 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000720 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000721 }
722
723 if (N1 == N2) {
724 // We can always fold X == Y for integer setcc's.
725 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000726 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 unsigned UOF = ISD::getUnorderedFlavor(Cond);
728 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000729 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000730 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000731 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000732 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
733 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000734 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
735 if (NewCond != Cond)
736 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 }
738
Chris Lattner5cdcc582005-01-09 20:52:51 +0000739 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000740 MVT::isInteger(N1.getValueType())) {
741 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
742 N1.getOpcode() == ISD::XOR) {
743 // Simplify (X+Y) == (X+Z) --> Y == Z
744 if (N1.getOpcode() == N2.getOpcode()) {
745 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000746 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000747 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000748 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000749 if (isCommutativeBinOp(N1.getOpcode())) {
750 // If X op Y == Y op X, try other combinations.
751 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000752 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000753 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000754 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000755 }
756 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000757
758 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000759
Chris Lattner68dc3102005-01-10 02:03:02 +0000760 // Simplify (X+Z) == X --> Z == 0
761 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000762 return getSetCC(VT, N1.getOperand(1),
763 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000764 if (N1.getOperand(1) == N2) {
765 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000766 return getSetCC(VT, N1.getOperand(0),
767 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000768 else {
769 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
770 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000771 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000772 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000773 N2, getConstant(1, TLI.getShiftAmountTy())),
774 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000775 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000776 }
777 }
778
Chris Lattner68dc3102005-01-10 02:03:02 +0000779 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
780 N2.getOpcode() == ISD::XOR) {
781 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000782 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000783 return getSetCC(VT, N2.getOperand(1),
784 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000785 } else if (N2.getOperand(1) == N1) {
786 if (isCommutativeBinOp(N2.getOpcode())) {
787 return getSetCC(VT, N2.getOperand(0),
788 getConstant(0, N2.getValueType()), Cond);
789 } else {
790 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
791 // X == (Z-X) --> X<<1 == Z
792 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
793 getConstant(1, TLI.getShiftAmountTy())),
794 N2.getOperand(0), Cond);
795 }
796 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000797 }
798 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000799
Chris Lattnerfda2b552005-04-18 04:48:12 +0000800 // Fold away ALL boolean setcc's.
801 if (N1.getValueType() == MVT::i1) {
802 switch (Cond) {
803 default: assert(0 && "Unknown integer setcc!");
804 case ISD::SETEQ: // X == Y -> (X^Y)^1
805 N1 = getNode(ISD::XOR, MVT::i1,
806 getNode(ISD::XOR, MVT::i1, N1, N2),
807 getConstant(1, MVT::i1));
808 break;
809 case ISD::SETNE: // X != Y --> (X^Y)
810 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
811 break;
812 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
813 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
814 N1 = getNode(ISD::AND, MVT::i1, N2,
815 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
816 break;
817 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
818 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
819 N1 = getNode(ISD::AND, MVT::i1, N1,
820 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
821 break;
822 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
823 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
824 N1 = getNode(ISD::OR, MVT::i1, N2,
825 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
826 break;
827 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
828 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
829 N1 = getNode(ISD::OR, MVT::i1, N1,
830 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
831 break;
832 }
833 if (VT != MVT::i1)
834 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
835 return N1;
836 }
837
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000838 // Could not fold it.
839 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000840}
841
Nate Begemanff663682005-08-13 06:14:17 +0000842SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
843 SDOperand N3, SDOperand N4,
844 ISD::CondCode CC) {
845 MVT::ValueType VT = N3.getValueType();
Nate Begeman32c392a2005-08-13 06:00:21 +0000846 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
847 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
848 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
849
850 // Check to see if we can simplify the select into an fabs node
851 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
852 // Allow either -0.0 or 0.0
853 if (CFP->getValue() == 0.0) {
854 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
855 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
856 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
857 N1 == N4.getOperand(0))
858 return getNode(ISD::FABS, VT, N1);
859
860 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
861 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
862 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
863 N3.getOperand(0) == N4)
864 return getNode(ISD::FABS, VT, N4);
865 }
866 }
867
868 // Check to see if we can perform the "gzip trick", transforming
869 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
870 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
871 MVT::isInteger(N1.getValueType()) &&
872 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
873 MVT::ValueType XType = N1.getValueType();
874 MVT::ValueType AType = N3.getValueType();
875 if (XType >= AType) {
876 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
877 // single-bit constant. FIXME: remove once the dag combiner
878 // exists.
879 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
880 unsigned ShCtV = Log2_64(N3C->getValue());
881 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
882 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
883 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
884 if (XType > AType)
885 Shift = getNode(ISD::TRUNCATE, AType, Shift);
886 return getNode(ISD::AND, AType, Shift, N3);
887 }
888 SDOperand Shift = getNode(ISD::SRA, XType, N1,
889 getConstant(MVT::getSizeInBits(XType)-1,
890 TLI.getShiftAmountTy()));
891 if (XType > AType)
892 Shift = getNode(ISD::TRUNCATE, AType, Shift);
893 return getNode(ISD::AND, AType, Shift, N3);
894 }
895 }
896
Nate Begemancebd4332005-08-24 04:57:57 +0000897 // Check to see if this is the equivalent of setcc X, 0
898 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000899 MVT::ValueType XType = N1.getValueType();
900 if (TLI.getOperationAction(ISD::SETCC, TLI.getSetCCResultTy()) ==
901 TargetLowering::Legal) {
Nate Begemancebd4332005-08-24 04:57:57 +0000902 return getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
Nate Begeman7042f152005-08-23 05:41:12 +0000903 }
Nate Begemancebd4332005-08-24 04:57:57 +0000904 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
905 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
906 TLI.getOperationAction(ISD::CTLZ, XType) == TargetLowering::Legal) {
Nate Begeman7042f152005-08-23 05:41:12 +0000907 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
908 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000909 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000910 TLI.getShiftAmountTy()));
911 }
Nate Begemancebd4332005-08-24 04:57:57 +0000912 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
913 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
914 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
915 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
916 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
917 getConstant(MVT::getSizeInBits(XType)-1,
918 TLI.getShiftAmountTy()));
919 }
920 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
921 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
922 SDOperand Sign = getNode(ISD::SRL, XType, N1,
923 getConstant(MVT::getSizeInBits(XType)-1,
924 TLI.getShiftAmountTy()));
925 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
926 }
Nate Begeman7042f152005-08-23 05:41:12 +0000927 }
928
Nate Begeman32c392a2005-08-13 06:00:21 +0000929 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
930 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
931 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
932 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
933 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
934 MVT::ValueType XType = N1.getValueType();
935 if (SubC->isNullValue() && MVT::isInteger(XType)) {
936 SDOperand Shift = getNode(ISD::SRA, XType, N1,
937 getConstant(MVT::getSizeInBits(XType)-1,
938 TLI.getShiftAmountTy()));
939 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
940 Shift);
941 }
942 }
943 }
944
945 // Could not fold it.
946 return SDOperand();
947}
948
Chris Lattnerc3aae252005-01-07 07:46:32 +0000949/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000950///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000951SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
952 SDNode *N = new SDNode(Opcode, VT);
953 AllNodes.push_back(N);
954 return SDOperand(N, 0);
955}
956
Chris Lattnerc3aae252005-01-07 07:46:32 +0000957SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
958 SDOperand Operand) {
959 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
960 uint64_t Val = C->getValue();
961 switch (Opcode) {
962 default: break;
963 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
964 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
965 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000966 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
967 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000968 }
969 }
970
971 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
972 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000973 case ISD::FNEG:
974 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975 case ISD::FP_ROUND:
976 case ISD::FP_EXTEND:
977 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000978 case ISD::FP_TO_SINT:
979 return getConstant((int64_t)C->getValue(), VT);
980 case ISD::FP_TO_UINT:
981 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000982 }
983
984 unsigned OpOpcode = Operand.Val->getOpcode();
985 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000986 case ISD::TokenFactor:
987 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 case ISD::SIGN_EXTEND:
989 if (Operand.getValueType() == VT) return Operand; // noop extension
990 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
991 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
992 break;
993 case ISD::ZERO_EXTEND:
994 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000995 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000996 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997 break;
998 case ISD::TRUNCATE:
999 if (Operand.getValueType() == VT) return Operand; // noop truncate
1000 if (OpOpcode == ISD::TRUNCATE)
1001 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001002 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
1003 // If the source is smaller than the dest, we still need an extend.
1004 if (Operand.Val->getOperand(0).getValueType() < VT)
1005 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1006 else if (Operand.Val->getOperand(0).getValueType() > VT)
1007 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1008 else
1009 return Operand.Val->getOperand(0);
1010 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001012 case ISD::FNEG:
1013 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
1014 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
1015 Operand.Val->getOperand(0));
1016 if (OpOpcode == ISD::FNEG) // --X -> X
1017 return Operand.Val->getOperand(0);
1018 break;
1019 case ISD::FABS:
1020 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1021 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1022 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023 }
1024
1025 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
1026 if (N) return SDOperand(N, 0);
1027 N = new SDNode(Opcode, Operand);
1028 N->setValueTypes(VT);
1029 AllNodes.push_back(N);
1030 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001031}
1032
Chris Lattner36019aa2005-04-18 03:48:41 +00001033/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1034/// this predicate to simplify operations downstream. V and Mask are known to
1035/// be the same type.
1036static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
1037 const TargetLowering &TLI) {
1038 unsigned SrcBits;
1039 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001040
Chris Lattner36019aa2005-04-18 03:48:41 +00001041 // If we know the result of a setcc has the top bits zero, use this info.
1042 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001043 case ISD::Constant:
1044 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1045
1046 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001047 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001048 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1049
1050 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001051 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001052 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1053 case ISD::ZERO_EXTEND:
1054 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1055 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
1056
1057 case ISD::AND:
1058 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001059 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001060 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1061
1062 // FALL THROUGH
1063 case ISD::OR:
1064 case ISD::XOR:
1065 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1066 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1067 case ISD::SELECT:
1068 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1069 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner1d6373c2005-08-24 16:46:55 +00001070 case ISD::SELECT_CC:
1071 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
1072 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001073 case ISD::SRL:
1074 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1075 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1076 uint64_t NewVal = Mask << ShAmt->getValue();
1077 SrcBits = MVT::getSizeInBits(Op.getValueType());
1078 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1079 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1080 }
1081 return false;
1082 case ISD::SHL:
1083 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1084 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1085 uint64_t NewVal = Mask >> ShAmt->getValue();
1086 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1087 }
1088 return false;
Chris Lattner1d6373c2005-08-24 16:46:55 +00001089 case ISD::CTTZ:
1090 case ISD::CTLZ:
1091 case ISD::CTPOP:
1092 // Bit counting instructions can not set the high bits of the result
1093 // register. The max number of bits sets depends on the input.
1094 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
1095
Chris Lattner6ea92792005-04-25 21:03:25 +00001096 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001097 default: break;
1098 }
1099
1100 return false;
1101}
1102
1103
1104
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1106 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001107#ifndef NDEBUG
1108 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001109 case ISD::TokenFactor:
1110 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1111 N2.getValueType() == MVT::Other && "Invalid token factor!");
1112 break;
Chris Lattner76365122005-01-16 02:23:22 +00001113 case ISD::AND:
1114 case ISD::OR:
1115 case ISD::XOR:
1116 case ISD::UDIV:
1117 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001118 case ISD::MULHU:
1119 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001120 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1121 // fall through
1122 case ISD::ADD:
1123 case ISD::SUB:
1124 case ISD::MUL:
1125 case ISD::SDIV:
1126 case ISD::SREM:
1127 assert(N1.getValueType() == N2.getValueType() &&
1128 N1.getValueType() == VT && "Binary operator types must match!");
1129 break;
1130
1131 case ISD::SHL:
1132 case ISD::SRA:
1133 case ISD::SRL:
1134 assert(VT == N1.getValueType() &&
1135 "Shift operators return type must be the same as their first arg");
1136 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001137 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001138 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001139 case ISD::FP_ROUND_INREG: {
1140 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1141 assert(VT == N1.getValueType() && "Not an inreg round!");
1142 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1143 "Cannot FP_ROUND_INREG integer types");
1144 assert(EVT <= VT && "Not rounding down!");
1145 break;
1146 }
1147 case ISD::SIGN_EXTEND_INREG: {
1148 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1149 assert(VT == N1.getValueType() && "Not an inreg extend!");
1150 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1151 "Cannot *_EXTEND_INREG FP types");
1152 assert(EVT <= VT && "Not extending!");
1153 }
1154
Chris Lattner76365122005-01-16 02:23:22 +00001155 default: break;
1156 }
1157#endif
1158
Chris Lattnerc3aae252005-01-07 07:46:32 +00001159 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1160 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1161 if (N1C) {
1162 if (N2C) {
1163 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1164 switch (Opcode) {
1165 case ISD::ADD: return getConstant(C1 + C2, VT);
1166 case ISD::SUB: return getConstant(C1 - C2, VT);
1167 case ISD::MUL: return getConstant(C1 * C2, VT);
1168 case ISD::UDIV:
1169 if (C2) return getConstant(C1 / C2, VT);
1170 break;
1171 case ISD::UREM :
1172 if (C2) return getConstant(C1 % C2, VT);
1173 break;
1174 case ISD::SDIV :
1175 if (C2) return getConstant(N1C->getSignExtended() /
1176 N2C->getSignExtended(), VT);
1177 break;
1178 case ISD::SREM :
1179 if (C2) return getConstant(N1C->getSignExtended() %
1180 N2C->getSignExtended(), VT);
1181 break;
1182 case ISD::AND : return getConstant(C1 & C2, VT);
1183 case ISD::OR : return getConstant(C1 | C2, VT);
1184 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001185 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1186 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1187 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001188 default: break;
1189 }
1190
1191 } else { // Cannonicalize constant to RHS if commutative
1192 if (isCommutativeBinOp(Opcode)) {
1193 std::swap(N1C, N2C);
1194 std::swap(N1, N2);
1195 }
1196 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001197
1198 switch (Opcode) {
1199 default: break;
1200 case ISD::SHL: // shl 0, X -> 0
1201 if (N1C->isNullValue()) return N1;
1202 break;
1203 case ISD::SRL: // srl 0, X -> 0
1204 if (N1C->isNullValue()) return N1;
1205 break;
1206 case ISD::SRA: // sra -1, X -> -1
1207 if (N1C->isAllOnesValue()) return N1;
1208 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001209 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1210 // Extending a constant? Just return the extended constant.
1211 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1212 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001213 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 }
1215
1216 if (N2C) {
1217 uint64_t C2 = N2C->getValue();
1218
1219 switch (Opcode) {
1220 case ISD::ADD:
1221 if (!C2) return N1; // add X, 0 -> X
1222 break;
1223 case ISD::SUB:
1224 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001225 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 case ISD::MUL:
1227 if (!C2) return N2; // mul X, 0 -> 0
1228 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1229 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1230
Chris Lattnerb48da392005-01-23 04:39:44 +00001231 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001232 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001233 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001234 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001235 }
1236 break;
1237
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001238 case ISD::MULHU:
1239 case ISD::MULHS:
1240 if (!C2) return N2; // mul X, 0 -> 0
1241
1242 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1243 return getConstant(0, VT);
1244
1245 // Many others could be handled here, including -1, powers of 2, etc.
1246 break;
1247
Chris Lattnerc3aae252005-01-07 07:46:32 +00001248 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001249 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001250 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001251 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001252 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001253 }
1254 break;
1255
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001256 case ISD::SHL:
1257 case ISD::SRL:
1258 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001259 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001260 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001261 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1262 return getNode(ISD::UNDEF, N1.getValueType());
1263 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001264 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001265
1266 if (Opcode == ISD::SRA) {
1267 // If the sign bit is known to be zero, switch this to a SRL.
1268 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001269 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001270 TLI))
1271 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1272 } else {
1273 // If the part left over is known to be zero, the whole thing is zero.
1274 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1275 if (Opcode == ISD::SRL) {
1276 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1277 return getConstant(0, N1.getValueType());
1278 } else if (Opcode == ISD::SHL) {
1279 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1280 return getConstant(0, N1.getValueType());
1281 }
1282 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001283
1284 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1285 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1286 unsigned OpSAC = OpSA->getValue();
1287 if (N1.getOpcode() == ISD::SHL) {
1288 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1289 return getConstant(0, N1.getValueType());
1290 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1291 getConstant(C2+OpSAC, N2.getValueType()));
1292 } else if (N1.getOpcode() == ISD::SRL) {
1293 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1294 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1295 getConstant(~0ULL << OpSAC, VT));
1296 if (C2 > OpSAC) {
1297 return getNode(ISD::SHL, VT, Mask,
1298 getConstant(C2-OpSAC, N2.getValueType()));
1299 } else {
1300 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1301 return getNode(ISD::SRL, VT, Mask,
1302 getConstant(OpSAC-C2, N2.getValueType()));
1303 }
1304 } else if (N1.getOpcode() == ISD::SRA) {
1305 // if C1 == C2, just mask out low bits.
1306 if (C2 == OpSAC)
1307 return getNode(ISD::AND, VT, N1.getOperand(0),
1308 getConstant(~0ULL << C2, VT));
1309 }
1310 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001311 break;
1312
Chris Lattnerc3aae252005-01-07 07:46:32 +00001313 case ISD::AND:
1314 if (!C2) return N2; // X and 0 -> 0
1315 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001316 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001317
Chris Lattner36019aa2005-04-18 03:48:41 +00001318 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1319 return getConstant(0, VT);
1320
Chris Lattner588bbbf2005-04-21 06:28:15 +00001321 {
1322 uint64_t NotC2 = ~C2;
1323 if (VT != MVT::i64)
1324 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1325
1326 if (MaskedValueIsZero(N1, NotC2, TLI))
1327 return N1; // if (X & ~C2) -> 0, the and is redundant
1328 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001329
Chris Lattnerdea29e22005-04-10 01:13:15 +00001330 // FIXME: Should add a corresponding version of this for
1331 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1332 // we don't have yet.
1333
Chris Lattner0f2287b2005-04-13 02:38:18 +00001334 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1335 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001336 // If we are masking out the part of our input that was extended, just
1337 // mask the input to the extension directly.
1338 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001339 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001340 if ((C2 & (~0ULL << ExtendBits)) == 0)
1341 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001342 } else if (N1.getOpcode() == ISD::OR) {
1343 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1344 if ((ORI->getValue() & C2) == C2) {
1345 // If the 'or' is setting all of the bits that we are masking for,
1346 // we know the result of the AND will be the AND mask itself.
1347 return N2;
1348 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001349 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 break;
1351 case ISD::OR:
1352 if (!C2)return N1; // X or 0 -> X
1353 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001354 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001355 break;
1356 case ISD::XOR:
1357 if (!C2) return N1; // X xor 0 -> X
1358 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001359 if (N1.Val->getOpcode() == ISD::SETCC){
1360 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 // !(X op Y) -> (X !op Y)
1362 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001363 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1364 return getSetCC(SetCC->getValueType(0),
1365 SetCC->getOperand(0), SetCC->getOperand(1),
1366 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001367 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1368 SDNode *Op = N1.Val;
1369 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1370 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1371 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1372 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1373 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1374 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1375 if (Op->getOpcode() == ISD::AND)
1376 return getNode(ISD::OR, VT, LHS, RHS);
1377 return getNode(ISD::AND, VT, LHS, RHS);
1378 }
1379 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001380 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 }
1382 break;
1383 }
1384
1385 // Reassociate ((X op C1) op C2) if possible.
1386 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1387 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001388 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001389 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1390 }
1391
1392 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1393 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001394 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001395 if (N2CFP) {
1396 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1397 switch (Opcode) {
1398 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1399 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1400 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1401 case ISD::SDIV:
1402 if (C2) return getConstantFP(C1 / C2, VT);
1403 break;
1404 case ISD::SREM :
1405 if (C2) return getConstantFP(fmod(C1, C2), VT);
1406 break;
1407 default: break;
1408 }
1409
1410 } else { // Cannonicalize constant to RHS if commutative
1411 if (isCommutativeBinOp(Opcode)) {
1412 std::swap(N1CFP, N2CFP);
1413 std::swap(N1, N2);
1414 }
1415 }
1416
Chris Lattner15e4b012005-07-10 00:07:11 +00001417 if (Opcode == ISD::FP_ROUND_INREG)
1418 return getNode(ISD::FP_EXTEND, VT,
1419 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1420 }
1421
Chris Lattnerc3aae252005-01-07 07:46:32 +00001422 // Finally, fold operations that do not require constants.
1423 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001424 case ISD::TokenFactor:
1425 if (N1.getOpcode() == ISD::EntryToken)
1426 return N2;
1427 if (N2.getOpcode() == ISD::EntryToken)
1428 return N1;
1429 break;
1430
Chris Lattnerc3aae252005-01-07 07:46:32 +00001431 case ISD::AND:
1432 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001433 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1434 SDNode *LHS = N1.Val, *RHS = N2.Val;
1435 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1436 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1437 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1438 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001439
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001440 if (LR == RR && isa<ConstantSDNode>(LR) &&
1441 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1442 // (X != 0) | (Y != 0) -> (X|Y != 0)
1443 // (X == 0) & (Y == 0) -> (X|Y == 0)
1444 // (X < 0) | (Y < 0) -> (X|Y < 0)
1445 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1446 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1447 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1448 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1449 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1450 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001451
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001452 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1453 // (X == -1) & (Y == -1) -> (X&Y == -1)
1454 // (X != -1) | (Y != -1) -> (X&Y != -1)
1455 // (X > -1) | (Y > -1) -> (X&Y > -1)
1456 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1457 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1458 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1459 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1460 LR, Op2);
1461 // (X > -1) & (Y > -1) -> (X|Y > -1)
1462 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1463 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1464 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001465 }
1466 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001467
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001468 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1469 if (LL == RR && LR == RL) {
1470 Op2 = ISD::getSetCCSwappedOperands(Op2);
1471 goto MatchedBackwards;
1472 }
1473
1474 if (LL == RL && LR == RR) {
1475 MatchedBackwards:
1476 ISD::CondCode Result;
1477 bool isInteger = MVT::isInteger(LL.getValueType());
1478 if (Opcode == ISD::OR)
1479 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1480 else
1481 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1482
1483 if (Result != ISD::SETCC_INVALID)
1484 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1485 }
1486 }
1487
Chris Lattner7467c9b2005-04-18 04:11:19 +00001488 // and/or zext(a), zext(b) -> zext(and/or a, b)
1489 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1490 N2.getOpcode() == ISD::ZERO_EXTEND &&
1491 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1492 return getNode(ISD::ZERO_EXTEND, VT,
1493 getNode(Opcode, N1.getOperand(0).getValueType(),
1494 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001495 break;
1496 case ISD::XOR:
1497 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1498 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001499 case ISD::ADD:
1500 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1501 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1502 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1503 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001504 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1505 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1506 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1507 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1508 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1509 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001510 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1511 !MVT::isFloatingPoint(N2.getValueType()))
1512 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001513 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001514 case ISD::SUB:
1515 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001516 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001517 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001518 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001519 if (N1.Val->getOperand(1) == N2 &&
1520 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001521 return N1.Val->getOperand(0); // (A+B)-B == A
1522 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001523 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1524 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001525 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001526 case ISD::FP_ROUND_INREG:
1527 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1528 break;
1529 case ISD::SIGN_EXTEND_INREG: {
1530 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1531 if (EVT == VT) return N1; // Not actually extending
1532
1533 // If we are sign extending an extension, use the original source.
1534 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1535 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1536 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001537
Chris Lattner15e4b012005-07-10 00:07:11 +00001538 // If we are sign extending a sextload, return just the load.
1539 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001540 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001541 return N1;
1542
1543 // If we are extending the result of a setcc, and we already know the
1544 // contents of the top bits, eliminate the extension.
1545 if (N1.getOpcode() == ISD::SETCC &&
1546 TLI.getSetCCResultContents() ==
1547 TargetLowering::ZeroOrNegativeOneSetCCResult)
1548 return N1;
1549
1550 // If we are sign extending the result of an (and X, C) operation, and we
1551 // know the extended bits are zeros already, don't do the extend.
1552 if (N1.getOpcode() == ISD::AND)
1553 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1554 uint64_t Mask = N1C->getValue();
1555 unsigned NumBits = MVT::getSizeInBits(EVT);
1556 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1557 return N1;
1558 }
1559 break;
1560 }
1561
Nate Begemaneea805e2005-04-13 21:23:31 +00001562 // FIXME: figure out how to safely handle things like
1563 // int foo(int x) { return 1 << (x & 255); }
1564 // int bar() { return foo(256); }
1565#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001566 case ISD::SHL:
1567 case ISD::SRL:
1568 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001569 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001570 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001571 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001572 else if (N2.getOpcode() == ISD::AND)
1573 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1574 // If the and is only masking out bits that cannot effect the shift,
1575 // eliminate the and.
1576 unsigned NumBits = MVT::getSizeInBits(VT);
1577 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1578 return getNode(Opcode, VT, N1, N2.getOperand(0));
1579 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001580 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001581#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001582 }
1583
Chris Lattner27e9b412005-05-11 18:57:39 +00001584 // Memoize this node if possible.
1585 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001586 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001587 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1588 if (BON) return SDOperand(BON, 0);
1589
1590 BON = N = new SDNode(Opcode, N1, N2);
1591 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001592 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001593 }
1594
Chris Lattner3e011362005-05-14 07:45:46 +00001595 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001596 AllNodes.push_back(N);
1597 return SDOperand(N, 0);
1598}
1599
Chris Lattner88de6e72005-05-12 00:17:04 +00001600// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001601// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001602void SDNode::setAdjCallChain(SDOperand N) {
1603 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001604 assert((getOpcode() == ISD::CALLSEQ_START ||
1605 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001606
1607 Operands[0].Val->removeUser(this);
1608 Operands[0] = N;
1609 N.Val->Uses.push_back(this);
1610}
1611
1612
1613
Chris Lattnerc3aae252005-01-07 07:46:32 +00001614SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001615 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001616 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001617 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1618 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001619 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001620
1621 // Loads have a token chain.
1622 N->setValueTypes(VT, MVT::Other);
1623 AllNodes.push_back(N);
1624 return SDOperand(N, 0);
1625}
1626
Chris Lattner5f056bf2005-07-10 01:55:33 +00001627
1628SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1629 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1630 MVT::ValueType EVT) {
1631 std::vector<SDOperand> Ops;
1632 Ops.reserve(4);
1633 Ops.push_back(Chain);
1634 Ops.push_back(Ptr);
1635 Ops.push_back(SV);
1636 Ops.push_back(getValueType(EVT));
1637 std::vector<MVT::ValueType> VTs;
1638 VTs.reserve(2);
1639 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1640 return getNode(Opcode, VTs, Ops);
1641}
1642
Chris Lattnerc3aae252005-01-07 07:46:32 +00001643SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1644 SDOperand N1, SDOperand N2, SDOperand N3) {
1645 // Perform various simplifications.
1646 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1647 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1648 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1649 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001650 case ISD::SETCC: {
1651 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001652 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001653 if (Simp.Val) return Simp;
1654 break;
1655 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001656 case ISD::SELECT:
1657 if (N1C)
1658 if (N1C->getValue())
1659 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001660 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001661 return N3; // select false, X, Y -> Y
1662
1663 if (N2 == N3) return N2; // select C, X, X -> X
1664
1665 if (VT == MVT::i1) { // Boolean SELECT
1666 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001667 if (N2C->getValue()) // select C, 1, X -> C | X
1668 return getNode(ISD::OR, VT, N1, N3);
1669 else // select C, 0, X -> ~C & X
1670 return getNode(ISD::AND, VT,
1671 getNode(ISD::XOR, N1.getValueType(), N1,
1672 getConstant(1, N1.getValueType())), N3);
1673 } else if (N3C) {
1674 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1675 return getNode(ISD::OR, VT,
1676 getNode(ISD::XOR, N1.getValueType(), N1,
1677 getConstant(1, N1.getValueType())), N2);
1678 else // select C, X, 0 -> C & X
1679 return getNode(ISD::AND, VT, N1, N2);
1680 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001681
1682 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1683 return getNode(ISD::OR, VT, N1, N3);
1684 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1685 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001686 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001687 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001688 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1689 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001690 if (Simp.Val) return Simp;
1691 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001692 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001693 case ISD::BRCOND:
1694 if (N2C)
1695 if (N2C->getValue()) // Unconditional branch
1696 return getNode(ISD::BR, MVT::Other, N1, N3);
1697 else
1698 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001699 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001700 }
1701
Chris Lattner385328c2005-05-14 07:42:29 +00001702 std::vector<SDOperand> Ops;
1703 Ops.reserve(3);
1704 Ops.push_back(N1);
1705 Ops.push_back(N2);
1706 Ops.push_back(N3);
1707
1708 // Memoize nodes.
1709 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1710 if (N) return SDOperand(N, 0);
1711
1712 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001713 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001714 AllNodes.push_back(N);
1715 return SDOperand(N, 0);
1716}
1717
1718SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001719 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001720 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001721 std::vector<SDOperand> Ops;
1722 Ops.reserve(4);
1723 Ops.push_back(N1);
1724 Ops.push_back(N2);
1725 Ops.push_back(N3);
1726 Ops.push_back(N4);
1727 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001728}
1729
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001730SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1731 SDOperand N1, SDOperand N2, SDOperand N3,
1732 SDOperand N4, SDOperand N5) {
1733 std::vector<SDOperand> Ops;
1734 Ops.reserve(5);
1735 Ops.push_back(N1);
1736 Ops.push_back(N2);
1737 Ops.push_back(N3);
1738 Ops.push_back(N4);
1739 Ops.push_back(N5);
1740 return getNode(Opcode, VT, Ops);
1741}
1742
1743
Chris Lattner0437cdd2005-05-09 04:14:13 +00001744SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001745 assert((!V || isa<PointerType>(V->getType())) &&
1746 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001747 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1748 if (N) return SDOperand(N, 0);
1749
1750 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001751 AllNodes.push_back(N);
1752 return SDOperand(N, 0);
1753}
1754
1755SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001756 std::vector<SDOperand> &Ops) {
1757 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001758 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001759 case 1: return getNode(Opcode, VT, Ops[0]);
1760 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1761 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001762 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001763 }
Chris Lattneref847df2005-04-09 03:27:28 +00001764
Chris Lattner89c34632005-05-14 06:20:26 +00001765 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001766 switch (Opcode) {
1767 default: break;
1768 case ISD::BRCONDTWOWAY:
1769 if (N1C)
1770 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001771 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001772 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001773 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001774 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001775 case ISD::BRTWOWAY_CC:
1776 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1777 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1778 "LHS and RHS of comparison must have same type!");
1779 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001780 case ISD::TRUNCSTORE: {
1781 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1782 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1783#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1784 // If this is a truncating store of a constant, convert to the desired type
1785 // and store it instead.
1786 if (isa<Constant>(Ops[0])) {
1787 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1788 if (isa<Constant>(Op))
1789 N1 = Op;
1790 }
1791 // Also for ConstantFP?
1792#endif
1793 if (Ops[0].getValueType() == EVT) // Normal store?
1794 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1795 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1796 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1797 "Can't do FP-INT conversion!");
1798 break;
1799 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001800 case ISD::SELECT_CC: {
1801 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1802 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1803 "LHS and RHS of condition must have same type!");
1804 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1805 "True and False arms of SelectCC must have same type!");
1806 assert(Ops[2].getValueType() == VT &&
1807 "select_cc node must be of same type as true and false value!");
1808 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1809 cast<CondCodeSDNode>(Ops[4])->get());
1810 if (Simp.Val) return Simp;
1811 break;
1812 }
1813 case ISD::BR_CC: {
1814 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1815 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1816 "LHS/RHS of comparison should match types!");
1817 // Use SimplifySetCC to simplify SETCC's.
1818 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
1819 cast<CondCodeSDNode>(Ops[1])->get());
1820 if (Simp.Val) {
1821 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
1822 if (C->getValue() & 1) // Unconditional branch
1823 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
1824 else
1825 return Ops[0]; // Unconditional Fall through
1826 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
1827 Ops[2] = Simp.getOperand(0);
1828 Ops[3] = Simp.getOperand(1);
1829 Ops[1] = Simp.getOperand(2);
1830 }
1831 }
1832 break;
1833 }
Chris Lattneref847df2005-04-09 03:27:28 +00001834 }
1835
Chris Lattner385328c2005-05-14 07:42:29 +00001836 // Memoize nodes.
1837 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1838 if (N) return SDOperand(N, 0);
1839 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001840 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001841 AllNodes.push_back(N);
1842 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001843}
1844
Chris Lattner89c34632005-05-14 06:20:26 +00001845SDOperand SelectionDAG::getNode(unsigned Opcode,
1846 std::vector<MVT::ValueType> &ResultTys,
1847 std::vector<SDOperand> &Ops) {
1848 if (ResultTys.size() == 1)
1849 return getNode(Opcode, ResultTys[0], Ops);
1850
Chris Lattner5f056bf2005-07-10 01:55:33 +00001851 switch (Opcode) {
1852 case ISD::EXTLOAD:
1853 case ISD::SEXTLOAD:
1854 case ISD::ZEXTLOAD: {
1855 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1856 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1857 // If they are asking for an extending load from/to the same thing, return a
1858 // normal load.
1859 if (ResultTys[0] == EVT)
1860 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1861 assert(EVT < ResultTys[0] &&
1862 "Should only be an extending load, not truncating!");
1863 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1864 "Cannot sign/zero extend a FP load!");
1865 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1866 "Cannot convert from FP to Int or Int -> FP!");
1867 break;
1868 }
1869
Chris Lattnere89083a2005-05-14 07:25:05 +00001870 // FIXME: figure out how to safely handle things like
1871 // int foo(int x) { return 1 << (x & 255); }
1872 // int bar() { return foo(256); }
1873#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001874 case ISD::SRA_PARTS:
1875 case ISD::SRL_PARTS:
1876 case ISD::SHL_PARTS:
1877 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001878 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001879 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1880 else if (N3.getOpcode() == ISD::AND)
1881 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1882 // If the and is only masking out bits that cannot effect the shift,
1883 // eliminate the and.
1884 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1885 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1886 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1887 }
1888 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001889#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001890 }
Chris Lattner89c34632005-05-14 06:20:26 +00001891
1892 // Memoize the node.
1893 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1894 Ops))];
1895 if (N) return SDOperand(N, 0);
1896 N = new SDNode(Opcode, Ops);
1897 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001898 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001899 return SDOperand(N, 0);
1900}
1901
Chris Lattner149c58c2005-08-16 18:17:10 +00001902
1903/// SelectNodeTo - These are used for target selectors to *mutate* the
1904/// specified node to have the specified return type, Target opcode, and
1905/// operands. Note that target opcodes are stored as
1906/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1907void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
Chris Lattner7651fa42005-08-24 23:00:29 +00001908 unsigned TargetOpc) {
1909 RemoveNodeFromCSEMaps(N);
1910 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1911 N->setValueTypes(VT);
1912}
1913void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
Chris Lattner149c58c2005-08-16 18:17:10 +00001914 unsigned TargetOpc, SDOperand Op1) {
1915 RemoveNodeFromCSEMaps(N);
1916 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1917 N->setValueTypes(VT);
1918 N->setOperands(Op1);
1919}
1920void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1921 unsigned TargetOpc, SDOperand Op1,
1922 SDOperand Op2) {
1923 RemoveNodeFromCSEMaps(N);
1924 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1925 N->setValueTypes(VT);
1926 N->setOperands(Op1, Op2);
1927}
Chris Lattner99badda2005-08-21 19:48:59 +00001928void SelectionDAG::SelectNodeTo(SDNode *N,
1929 MVT::ValueType VT1, MVT::ValueType VT2,
1930 unsigned TargetOpc, SDOperand Op1,
1931 SDOperand Op2) {
1932 RemoveNodeFromCSEMaps(N);
1933 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1934 N->setValueTypes(VT1, VT2);
1935 N->setOperands(Op1, Op2);
1936}
Chris Lattner149c58c2005-08-16 18:17:10 +00001937void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1938 unsigned TargetOpc, SDOperand Op1,
1939 SDOperand Op2, SDOperand Op3) {
1940 RemoveNodeFromCSEMaps(N);
1941 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1942 N->setValueTypes(VT);
1943 N->setOperands(Op1, Op2, Op3);
1944}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001945void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT1,
1946 MVT::ValueType VT2,
1947 unsigned TargetOpc, SDOperand Op1,
1948 SDOperand Op2, SDOperand Op3) {
1949 RemoveNodeFromCSEMaps(N);
1950 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1951 N->setValueTypes(VT1, VT2);
1952 N->setOperands(Op1, Op2, Op3);
1953}
1954
Nate Begeman294a0a12005-08-18 07:30:15 +00001955void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1956 unsigned TargetOpc, SDOperand Op1,
1957 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1958 RemoveNodeFromCSEMaps(N);
1959 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1960 N->setValueTypes(VT);
1961 N->setOperands(Op1, Op2, Op3, Op4);
1962}
Chris Lattner6b09a292005-08-21 18:49:33 +00001963void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1964 unsigned TargetOpc, SDOperand Op1,
1965 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1966 SDOperand Op5) {
1967 RemoveNodeFromCSEMaps(N);
1968 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1969 N->setValueTypes(VT);
1970 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1971}
Chris Lattner149c58c2005-08-16 18:17:10 +00001972
Chris Lattner8b8749f2005-08-17 19:00:20 +00001973/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1974/// This can cause recursive merging of nodes in the DAG.
1975///
1976void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1977 assert(From != To && "Cannot replace uses of with self");
1978 while (!From->use_empty()) {
1979 // Process users until they are all gone.
1980 SDNode *U = *From->use_begin();
1981
1982 // This node is about to morph, remove its old self from the CSE maps.
1983 RemoveNodeFromCSEMaps(U);
1984
1985 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1986 if (U->getOperand(i).Val == From) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001987 assert(U->getOperand(i).getValueType() ==
Chris Lattner8b8749f2005-08-17 19:00:20 +00001988 To->getValueType(U->getOperand(i).ResNo));
1989 From->removeUser(U);
1990 U->Operands[i].Val = To;
1991 To->addUser(U);
1992 }
1993
1994 // Now that we have modified U, add it back to the CSE maps. If it already
1995 // exists there, recursively merge the results together.
1996 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
1997 ReplaceAllUsesWith(U, Existing);
1998 // U is now dead.
1999 }
2000}
2001
Chris Lattner7b2880c2005-08-24 22:44:39 +00002002void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2003 const std::vector<SDOperand> &To) {
2004 assert(From->getNumValues() == To.size() &&
2005 "Incorrect number of values to replace with!");
2006 if (To.size() == 1 && To[0].ResNo == 0) {
2007 // Degenerate case handled above.
2008 ReplaceAllUsesWith(From, To[0].Val);
2009 return;
2010 }
2011
2012 while (!From->use_empty()) {
2013 // Process users until they are all gone.
2014 SDNode *U = *From->use_begin();
2015
2016 // This node is about to morph, remove its old self from the CSE maps.
2017 RemoveNodeFromCSEMaps(U);
2018
2019 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2020 if (U->getOperand(i).Val == From) {
2021 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
2022 assert(U->getOperand(i).getValueType() == ToOp.getValueType());
2023 From->removeUser(U);
2024 U->Operands[i] = ToOp;
2025 ToOp.Val->addUser(U);
2026 }
2027
2028 // Now that we have modified U, add it back to the CSE maps. If it already
2029 // exists there, recursively merge the results together.
2030 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2031 ReplaceAllUsesWith(U, Existing);
2032 // U is now dead.
2033 }
2034}
2035
2036
Jim Laskey58b968b2005-08-17 20:08:02 +00002037//===----------------------------------------------------------------------===//
2038// SDNode Class
2039//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002040
Chris Lattner5c884562005-01-12 18:37:47 +00002041/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2042/// indicated value. This method ignores uses of other values defined by this
2043/// operation.
2044bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2045 assert(Value < getNumValues() && "Bad value!");
2046
2047 // If there is only one value, this is easy.
2048 if (getNumValues() == 1)
2049 return use_size() == NUses;
2050 if (Uses.size() < NUses) return false;
2051
2052 SDOperand TheValue(this, Value);
2053
2054 std::set<SDNode*> UsersHandled;
2055
2056 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2057 UI != E; ++UI) {
2058 SDNode *User = *UI;
2059 if (User->getNumOperands() == 1 ||
2060 UsersHandled.insert(User).second) // First time we've seen this?
2061 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2062 if (User->getOperand(i) == TheValue) {
2063 if (NUses == 0)
2064 return false; // too many uses
2065 --NUses;
2066 }
2067 }
2068
2069 // Found exactly the right number of uses?
2070 return NUses == 0;
2071}
2072
2073
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002074const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002075 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002076 default:
2077 if (getOpcode() < ISD::BUILTIN_OP_END)
2078 return "<<Unknown DAG Node>>";
2079 else {
2080 if (G)
2081 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2082 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2083 return "<<Unknown Target Node>>";
2084 }
2085
Andrew Lenharth95762122005-03-31 21:24:06 +00002086 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002087 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002088 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002089 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002090 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002091 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002092 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002093 case ISD::ConstantFP: return "ConstantFP";
2094 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002095 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002096 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002097 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002098 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002099 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002100 case ISD::ExternalSymbol: return "ExternalSymbol";
2101 case ISD::ConstantPool: return "ConstantPoolIndex";
Chris Lattner4025a9c2005-08-25 05:03:06 +00002102 case ISD::TargetConstantPool: return "TargetConstantPoolIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002103 case ISD::CopyToReg: return "CopyToReg";
2104 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002105 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002106 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002107
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002108 // Unary operators
2109 case ISD::FABS: return "fabs";
2110 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002111 case ISD::FSQRT: return "fsqrt";
2112 case ISD::FSIN: return "fsin";
2113 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002114
2115 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002116 case ISD::ADD: return "add";
2117 case ISD::SUB: return "sub";
2118 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002119 case ISD::MULHU: return "mulhu";
2120 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002121 case ISD::SDIV: return "sdiv";
2122 case ISD::UDIV: return "udiv";
2123 case ISD::SREM: return "srem";
2124 case ISD::UREM: return "urem";
2125 case ISD::AND: return "and";
2126 case ISD::OR: return "or";
2127 case ISD::XOR: return "xor";
2128 case ISD::SHL: return "shl";
2129 case ISD::SRA: return "sra";
2130 case ISD::SRL: return "srl";
2131
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002132 case ISD::SETCC: return "setcc";
2133 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002134 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002135 case ISD::ADD_PARTS: return "add_parts";
2136 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002137 case ISD::SHL_PARTS: return "shl_parts";
2138 case ISD::SRA_PARTS: return "sra_parts";
2139 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002140
Chris Lattner7f644642005-04-28 21:44:03 +00002141 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002142 case ISD::SIGN_EXTEND: return "sign_extend";
2143 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002144 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002145 case ISD::TRUNCATE: return "truncate";
2146 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002147 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002148 case ISD::FP_EXTEND: return "fp_extend";
2149
2150 case ISD::SINT_TO_FP: return "sint_to_fp";
2151 case ISD::UINT_TO_FP: return "uint_to_fp";
2152 case ISD::FP_TO_SINT: return "fp_to_sint";
2153 case ISD::FP_TO_UINT: return "fp_to_uint";
2154
2155 // Control flow instructions
2156 case ISD::BR: return "br";
2157 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002158 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002159 case ISD::BR_CC: return "br_cc";
2160 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002161 case ISD::RET: return "ret";
2162 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002163 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002164 case ISD::CALLSEQ_START: return "callseq_start";
2165 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002166
2167 // Other operators
2168 case ISD::LOAD: return "load";
2169 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002170 case ISD::EXTLOAD: return "extload";
2171 case ISD::SEXTLOAD: return "sextload";
2172 case ISD::ZEXTLOAD: return "zextload";
2173 case ISD::TRUNCSTORE: return "truncstore";
2174
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002175 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2176 case ISD::EXTRACT_ELEMENT: return "extract_element";
2177 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002178 case ISD::MEMSET: return "memset";
2179 case ISD::MEMCPY: return "memcpy";
2180 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002181
Chris Lattner276260b2005-05-11 04:50:30 +00002182 // Bit counting
2183 case ISD::CTPOP: return "ctpop";
2184 case ISD::CTTZ: return "cttz";
2185 case ISD::CTLZ: return "ctlz";
2186
2187 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002188 case ISD::READPORT: return "readport";
2189 case ISD::WRITEPORT: return "writeport";
2190 case ISD::READIO: return "readio";
2191 case ISD::WRITEIO: return "writeio";
2192
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002193 case ISD::CONDCODE:
2194 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002195 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002196 case ISD::SETOEQ: return "setoeq";
2197 case ISD::SETOGT: return "setogt";
2198 case ISD::SETOGE: return "setoge";
2199 case ISD::SETOLT: return "setolt";
2200 case ISD::SETOLE: return "setole";
2201 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002202
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002203 case ISD::SETO: return "seto";
2204 case ISD::SETUO: return "setuo";
2205 case ISD::SETUEQ: return "setue";
2206 case ISD::SETUGT: return "setugt";
2207 case ISD::SETUGE: return "setuge";
2208 case ISD::SETULT: return "setult";
2209 case ISD::SETULE: return "setule";
2210 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002211
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002212 case ISD::SETEQ: return "seteq";
2213 case ISD::SETGT: return "setgt";
2214 case ISD::SETGE: return "setge";
2215 case ISD::SETLT: return "setlt";
2216 case ISD::SETLE: return "setle";
2217 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002218 }
2219 }
2220}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002221
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002222void SDNode::dump() const { dump(0); }
2223void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002224 std::cerr << (void*)this << ": ";
2225
2226 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2227 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002228 if (getValueType(i) == MVT::Other)
2229 std::cerr << "ch";
2230 else
2231 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002232 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002233 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002234
2235 std::cerr << " ";
2236 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2237 if (i) std::cerr << ", ";
2238 std::cerr << (void*)getOperand(i).Val;
2239 if (unsigned RN = getOperand(i).ResNo)
2240 std::cerr << ":" << RN;
2241 }
2242
2243 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2244 std::cerr << "<" << CSDN->getValue() << ">";
2245 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2246 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002247 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002248 dyn_cast<GlobalAddressSDNode>(this)) {
2249 std::cerr << "<";
2250 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002251 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002252 std::cerr << "<" << FIDN->getIndex() << ">";
2253 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2254 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002255 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002256 std::cerr << "<";
2257 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2258 if (LBB)
2259 std::cerr << LBB->getName() << " ";
2260 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002261 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002262 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2263 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2264 } else {
2265 std::cerr << " #" << R->getReg();
2266 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002267 } else if (const ExternalSymbolSDNode *ES =
2268 dyn_cast<ExternalSymbolSDNode>(this)) {
2269 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002270 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2271 if (M->getValue())
2272 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2273 else
2274 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002275 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2276 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002277 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002278}
2279
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002280static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002281 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2282 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002283 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002284 else
2285 std::cerr << "\n" << std::string(indent+2, ' ')
2286 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002287
Chris Lattnerea946cd2005-01-09 20:38:33 +00002288
2289 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002290 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002291}
2292
Chris Lattnerc3aae252005-01-07 07:46:32 +00002293void SelectionDAG::dump() const {
2294 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002295 std::vector<SDNode*> Nodes(AllNodes);
2296 std::sort(Nodes.begin(), Nodes.end());
2297
2298 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002299 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002300 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002301 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002302
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002303 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002304
Chris Lattnerc3aae252005-01-07 07:46:32 +00002305 std::cerr << "\n\n";
2306}
2307