blob: 9793478dba827057225e82d7e7cbc4e82a47529d [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();
Chris Lattner9d338cf2005-08-25 17:54:58 +0000900 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy()))
Nate Begemancebd4332005-08-24 04:57:57 +0000901 return getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
Chris Lattner9d338cf2005-08-25 17:54:58 +0000902
Nate Begemancebd4332005-08-24 04:57:57 +0000903 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
904 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
Chris Lattner9d338cf2005-08-25 17:54:58 +0000905 TLI.isOperationLegal(ISD::CTLZ, XType)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000906 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
907 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000908 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000909 TLI.getShiftAmountTy()));
910 }
Nate Begemancebd4332005-08-24 04:57:57 +0000911 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
912 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
913 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
914 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
915 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
916 getConstant(MVT::getSizeInBits(XType)-1,
917 TLI.getShiftAmountTy()));
918 }
919 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
920 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
921 SDOperand Sign = getNode(ISD::SRL, XType, N1,
922 getConstant(MVT::getSizeInBits(XType)-1,
923 TLI.getShiftAmountTy()));
924 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
925 }
Nate Begeman7042f152005-08-23 05:41:12 +0000926 }
927
Nate Begeman32c392a2005-08-13 06:00:21 +0000928 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
929 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
930 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
931 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
932 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
933 MVT::ValueType XType = N1.getValueType();
934 if (SubC->isNullValue() && MVT::isInteger(XType)) {
935 SDOperand Shift = getNode(ISD::SRA, XType, N1,
936 getConstant(MVT::getSizeInBits(XType)-1,
937 TLI.getShiftAmountTy()));
938 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
939 Shift);
940 }
941 }
942 }
943
944 // Could not fold it.
945 return SDOperand();
946}
947
Chris Lattnerc3aae252005-01-07 07:46:32 +0000948/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000949///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000950SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
951 SDNode *N = new SDNode(Opcode, VT);
952 AllNodes.push_back(N);
953 return SDOperand(N, 0);
954}
955
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
957 SDOperand Operand) {
958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
959 uint64_t Val = C->getValue();
960 switch (Opcode) {
961 default: break;
962 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
963 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
964 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000965 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
966 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000967 }
968 }
969
970 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
971 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000972 case ISD::FNEG:
973 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000974 case ISD::FP_ROUND:
975 case ISD::FP_EXTEND:
976 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000977 case ISD::FP_TO_SINT:
978 return getConstant((int64_t)C->getValue(), VT);
979 case ISD::FP_TO_UINT:
980 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 }
982
983 unsigned OpOpcode = Operand.Val->getOpcode();
984 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000985 case ISD::TokenFactor:
986 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000987 case ISD::SIGN_EXTEND:
988 if (Operand.getValueType() == VT) return Operand; // noop extension
989 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
990 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
991 break;
992 case ISD::ZERO_EXTEND:
993 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000994 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000995 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996 break;
997 case ISD::TRUNCATE:
998 if (Operand.getValueType() == VT) return Operand; // noop truncate
999 if (OpOpcode == ISD::TRUNCATE)
1000 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001001 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
1002 // If the source is smaller than the dest, we still need an extend.
1003 if (Operand.Val->getOperand(0).getValueType() < VT)
1004 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1005 else if (Operand.Val->getOperand(0).getValueType() > VT)
1006 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1007 else
1008 return Operand.Val->getOperand(0);
1009 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001011 case ISD::FNEG:
1012 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
1013 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
1014 Operand.Val->getOperand(0));
1015 if (OpOpcode == ISD::FNEG) // --X -> X
1016 return Operand.Val->getOperand(0);
1017 break;
1018 case ISD::FABS:
1019 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1020 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1021 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001022 }
1023
1024 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
1025 if (N) return SDOperand(N, 0);
1026 N = new SDNode(Opcode, Operand);
1027 N->setValueTypes(VT);
1028 AllNodes.push_back(N);
1029 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001030}
1031
Chris Lattner36019aa2005-04-18 03:48:41 +00001032/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1033/// this predicate to simplify operations downstream. V and Mask are known to
1034/// be the same type.
1035static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
1036 const TargetLowering &TLI) {
1037 unsigned SrcBits;
1038 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001039
Chris Lattner36019aa2005-04-18 03:48:41 +00001040 // If we know the result of a setcc has the top bits zero, use this info.
1041 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001042 case ISD::Constant:
1043 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1044
1045 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001046 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001047 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1048
1049 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001050 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001051 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1052 case ISD::ZERO_EXTEND:
1053 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1054 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
1055
1056 case ISD::AND:
1057 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001058 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001059 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1060
1061 // FALL THROUGH
1062 case ISD::OR:
1063 case ISD::XOR:
1064 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1065 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1066 case ISD::SELECT:
1067 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1068 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner1d6373c2005-08-24 16:46:55 +00001069 case ISD::SELECT_CC:
1070 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
1071 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001072 case ISD::SRL:
1073 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1074 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1075 uint64_t NewVal = Mask << ShAmt->getValue();
1076 SrcBits = MVT::getSizeInBits(Op.getValueType());
1077 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1078 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1079 }
1080 return false;
1081 case ISD::SHL:
1082 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1083 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1084 uint64_t NewVal = Mask >> ShAmt->getValue();
1085 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1086 }
1087 return false;
Chris Lattner1d6373c2005-08-24 16:46:55 +00001088 case ISD::CTTZ:
1089 case ISD::CTLZ:
1090 case ISD::CTPOP:
1091 // Bit counting instructions can not set the high bits of the result
1092 // register. The max number of bits sets depends on the input.
1093 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
1094
Chris Lattner6ea92792005-04-25 21:03:25 +00001095 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001096 default: break;
1097 }
1098
1099 return false;
1100}
1101
1102
1103
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1105 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001106#ifndef NDEBUG
1107 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001108 case ISD::TokenFactor:
1109 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1110 N2.getValueType() == MVT::Other && "Invalid token factor!");
1111 break;
Chris Lattner76365122005-01-16 02:23:22 +00001112 case ISD::AND:
1113 case ISD::OR:
1114 case ISD::XOR:
1115 case ISD::UDIV:
1116 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001117 case ISD::MULHU:
1118 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001119 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1120 // fall through
1121 case ISD::ADD:
1122 case ISD::SUB:
1123 case ISD::MUL:
1124 case ISD::SDIV:
1125 case ISD::SREM:
1126 assert(N1.getValueType() == N2.getValueType() &&
1127 N1.getValueType() == VT && "Binary operator types must match!");
1128 break;
1129
1130 case ISD::SHL:
1131 case ISD::SRA:
1132 case ISD::SRL:
1133 assert(VT == N1.getValueType() &&
1134 "Shift operators return type must be the same as their first arg");
1135 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001136 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001137 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001138 case ISD::FP_ROUND_INREG: {
1139 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1140 assert(VT == N1.getValueType() && "Not an inreg round!");
1141 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1142 "Cannot FP_ROUND_INREG integer types");
1143 assert(EVT <= VT && "Not rounding down!");
1144 break;
1145 }
1146 case ISD::SIGN_EXTEND_INREG: {
1147 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1148 assert(VT == N1.getValueType() && "Not an inreg extend!");
1149 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1150 "Cannot *_EXTEND_INREG FP types");
1151 assert(EVT <= VT && "Not extending!");
1152 }
1153
Chris Lattner76365122005-01-16 02:23:22 +00001154 default: break;
1155 }
1156#endif
1157
Chris Lattnerc3aae252005-01-07 07:46:32 +00001158 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1159 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1160 if (N1C) {
1161 if (N2C) {
1162 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1163 switch (Opcode) {
1164 case ISD::ADD: return getConstant(C1 + C2, VT);
1165 case ISD::SUB: return getConstant(C1 - C2, VT);
1166 case ISD::MUL: return getConstant(C1 * C2, VT);
1167 case ISD::UDIV:
1168 if (C2) return getConstant(C1 / C2, VT);
1169 break;
1170 case ISD::UREM :
1171 if (C2) return getConstant(C1 % C2, VT);
1172 break;
1173 case ISD::SDIV :
1174 if (C2) return getConstant(N1C->getSignExtended() /
1175 N2C->getSignExtended(), VT);
1176 break;
1177 case ISD::SREM :
1178 if (C2) return getConstant(N1C->getSignExtended() %
1179 N2C->getSignExtended(), VT);
1180 break;
1181 case ISD::AND : return getConstant(C1 & C2, VT);
1182 case ISD::OR : return getConstant(C1 | C2, VT);
1183 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001184 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1185 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1186 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001187 default: break;
1188 }
1189
1190 } else { // Cannonicalize constant to RHS if commutative
1191 if (isCommutativeBinOp(Opcode)) {
1192 std::swap(N1C, N2C);
1193 std::swap(N1, N2);
1194 }
1195 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001196
1197 switch (Opcode) {
1198 default: break;
1199 case ISD::SHL: // shl 0, X -> 0
1200 if (N1C->isNullValue()) return N1;
1201 break;
1202 case ISD::SRL: // srl 0, X -> 0
1203 if (N1C->isNullValue()) return N1;
1204 break;
1205 case ISD::SRA: // sra -1, X -> -1
1206 if (N1C->isAllOnesValue()) return N1;
1207 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001208 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1209 // Extending a constant? Just return the extended constant.
1210 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1211 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001212 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213 }
1214
1215 if (N2C) {
1216 uint64_t C2 = N2C->getValue();
1217
1218 switch (Opcode) {
1219 case ISD::ADD:
1220 if (!C2) return N1; // add X, 0 -> X
1221 break;
1222 case ISD::SUB:
1223 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001224 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001225 case ISD::MUL:
1226 if (!C2) return N2; // mul X, 0 -> 0
1227 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1228 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1229
Chris Lattnerb48da392005-01-23 04:39:44 +00001230 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001231 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001232 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001233 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001234 }
1235 break;
1236
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001237 case ISD::MULHU:
1238 case ISD::MULHS:
1239 if (!C2) return N2; // mul X, 0 -> 0
1240
1241 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1242 return getConstant(0, VT);
1243
1244 // Many others could be handled here, including -1, powers of 2, etc.
1245 break;
1246
Chris Lattnerc3aae252005-01-07 07:46:32 +00001247 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001248 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001249 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001250 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001251 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001252 }
1253 break;
1254
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001255 case ISD::SHL:
1256 case ISD::SRL:
1257 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001258 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001259 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001260 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1261 return getNode(ISD::UNDEF, N1.getValueType());
1262 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001263 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001264
1265 if (Opcode == ISD::SRA) {
1266 // If the sign bit is known to be zero, switch this to a SRL.
1267 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001268 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001269 TLI))
1270 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1271 } else {
1272 // If the part left over is known to be zero, the whole thing is zero.
1273 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1274 if (Opcode == ISD::SRL) {
1275 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1276 return getConstant(0, N1.getValueType());
1277 } else if (Opcode == ISD::SHL) {
1278 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1279 return getConstant(0, N1.getValueType());
1280 }
1281 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001282
1283 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1284 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1285 unsigned OpSAC = OpSA->getValue();
1286 if (N1.getOpcode() == ISD::SHL) {
1287 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1288 return getConstant(0, N1.getValueType());
1289 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1290 getConstant(C2+OpSAC, N2.getValueType()));
1291 } else if (N1.getOpcode() == ISD::SRL) {
1292 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1293 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1294 getConstant(~0ULL << OpSAC, VT));
1295 if (C2 > OpSAC) {
1296 return getNode(ISD::SHL, VT, Mask,
1297 getConstant(C2-OpSAC, N2.getValueType()));
1298 } else {
1299 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1300 return getNode(ISD::SRL, VT, Mask,
1301 getConstant(OpSAC-C2, N2.getValueType()));
1302 }
1303 } else if (N1.getOpcode() == ISD::SRA) {
1304 // if C1 == C2, just mask out low bits.
1305 if (C2 == OpSAC)
1306 return getNode(ISD::AND, VT, N1.getOperand(0),
1307 getConstant(~0ULL << C2, VT));
1308 }
1309 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001310 break;
1311
Chris Lattnerc3aae252005-01-07 07:46:32 +00001312 case ISD::AND:
1313 if (!C2) return N2; // X and 0 -> 0
1314 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001315 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001316
Chris Lattner36019aa2005-04-18 03:48:41 +00001317 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1318 return getConstant(0, VT);
1319
Chris Lattner588bbbf2005-04-21 06:28:15 +00001320 {
1321 uint64_t NotC2 = ~C2;
1322 if (VT != MVT::i64)
1323 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1324
1325 if (MaskedValueIsZero(N1, NotC2, TLI))
1326 return N1; // if (X & ~C2) -> 0, the and is redundant
1327 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001328
Chris Lattnerdea29e22005-04-10 01:13:15 +00001329 // FIXME: Should add a corresponding version of this for
1330 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1331 // we don't have yet.
1332
Chris Lattner0f2287b2005-04-13 02:38:18 +00001333 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1334 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001335 // If we are masking out the part of our input that was extended, just
1336 // mask the input to the extension directly.
1337 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001338 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001339 if ((C2 & (~0ULL << ExtendBits)) == 0)
1340 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001341 } else if (N1.getOpcode() == ISD::OR) {
1342 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1343 if ((ORI->getValue() & C2) == C2) {
1344 // If the 'or' is setting all of the bits that we are masking for,
1345 // we know the result of the AND will be the AND mask itself.
1346 return N2;
1347 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001348 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001349 break;
1350 case ISD::OR:
1351 if (!C2)return N1; // X or 0 -> X
1352 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001353 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001354 break;
1355 case ISD::XOR:
1356 if (!C2) return N1; // X xor 0 -> X
1357 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001358 if (N1.Val->getOpcode() == ISD::SETCC){
1359 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001360 // !(X op Y) -> (X !op Y)
1361 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001362 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1363 return getSetCC(SetCC->getValueType(0),
1364 SetCC->getOperand(0), SetCC->getOperand(1),
1365 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001366 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1367 SDNode *Op = N1.Val;
1368 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1369 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1370 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1371 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1372 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1373 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1374 if (Op->getOpcode() == ISD::AND)
1375 return getNode(ISD::OR, VT, LHS, RHS);
1376 return getNode(ISD::AND, VT, LHS, RHS);
1377 }
1378 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001379 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001380 }
1381 break;
1382 }
1383
1384 // Reassociate ((X op C1) op C2) if possible.
1385 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1386 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001387 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001388 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1389 }
1390
1391 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1392 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001393 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001394 if (N2CFP) {
1395 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1396 switch (Opcode) {
1397 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1398 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1399 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1400 case ISD::SDIV:
1401 if (C2) return getConstantFP(C1 / C2, VT);
1402 break;
1403 case ISD::SREM :
1404 if (C2) return getConstantFP(fmod(C1, C2), VT);
1405 break;
1406 default: break;
1407 }
1408
1409 } else { // Cannonicalize constant to RHS if commutative
1410 if (isCommutativeBinOp(Opcode)) {
1411 std::swap(N1CFP, N2CFP);
1412 std::swap(N1, N2);
1413 }
1414 }
1415
Chris Lattner15e4b012005-07-10 00:07:11 +00001416 if (Opcode == ISD::FP_ROUND_INREG)
1417 return getNode(ISD::FP_EXTEND, VT,
1418 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1419 }
1420
Chris Lattnerc3aae252005-01-07 07:46:32 +00001421 // Finally, fold operations that do not require constants.
1422 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001423 case ISD::TokenFactor:
1424 if (N1.getOpcode() == ISD::EntryToken)
1425 return N2;
1426 if (N2.getOpcode() == ISD::EntryToken)
1427 return N1;
1428 break;
1429
Chris Lattnerc3aae252005-01-07 07:46:32 +00001430 case ISD::AND:
1431 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001432 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1433 SDNode *LHS = N1.Val, *RHS = N2.Val;
1434 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1435 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1436 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1437 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001438
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001439 if (LR == RR && isa<ConstantSDNode>(LR) &&
1440 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1441 // (X != 0) | (Y != 0) -> (X|Y != 0)
1442 // (X == 0) & (Y == 0) -> (X|Y == 0)
1443 // (X < 0) | (Y < 0) -> (X|Y < 0)
1444 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1445 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1446 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1447 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1448 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1449 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001450
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001451 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1452 // (X == -1) & (Y == -1) -> (X&Y == -1)
1453 // (X != -1) | (Y != -1) -> (X&Y != -1)
1454 // (X > -1) | (Y > -1) -> (X&Y > -1)
1455 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1456 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1457 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1458 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1459 LR, Op2);
1460 // (X > -1) & (Y > -1) -> (X|Y > -1)
1461 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1462 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1463 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001464 }
1465 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001466
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001467 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1468 if (LL == RR && LR == RL) {
1469 Op2 = ISD::getSetCCSwappedOperands(Op2);
1470 goto MatchedBackwards;
1471 }
1472
1473 if (LL == RL && LR == RR) {
1474 MatchedBackwards:
1475 ISD::CondCode Result;
1476 bool isInteger = MVT::isInteger(LL.getValueType());
1477 if (Opcode == ISD::OR)
1478 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1479 else
1480 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1481
1482 if (Result != ISD::SETCC_INVALID)
1483 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1484 }
1485 }
1486
Chris Lattner7467c9b2005-04-18 04:11:19 +00001487 // and/or zext(a), zext(b) -> zext(and/or a, b)
1488 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1489 N2.getOpcode() == ISD::ZERO_EXTEND &&
1490 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1491 return getNode(ISD::ZERO_EXTEND, VT,
1492 getNode(Opcode, N1.getOperand(0).getValueType(),
1493 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001494 break;
1495 case ISD::XOR:
1496 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1497 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001498 case ISD::ADD:
1499 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1500 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1501 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1502 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001503 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1504 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1505 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1506 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1507 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1508 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001509 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1510 !MVT::isFloatingPoint(N2.getValueType()))
1511 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001512 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001513 case ISD::SUB:
1514 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001515 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001516 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001517 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001518 if (N1.Val->getOperand(1) == N2 &&
1519 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001520 return N1.Val->getOperand(0); // (A+B)-B == A
1521 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001522 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1523 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001524 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001525 case ISD::FP_ROUND_INREG:
1526 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1527 break;
1528 case ISD::SIGN_EXTEND_INREG: {
1529 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1530 if (EVT == VT) return N1; // Not actually extending
1531
1532 // If we are sign extending an extension, use the original source.
1533 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1534 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1535 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001536
Chris Lattner15e4b012005-07-10 00:07:11 +00001537 // If we are sign extending a sextload, return just the load.
1538 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001539 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001540 return N1;
1541
1542 // If we are extending the result of a setcc, and we already know the
1543 // contents of the top bits, eliminate the extension.
1544 if (N1.getOpcode() == ISD::SETCC &&
1545 TLI.getSetCCResultContents() ==
1546 TargetLowering::ZeroOrNegativeOneSetCCResult)
1547 return N1;
1548
1549 // If we are sign extending the result of an (and X, C) operation, and we
1550 // know the extended bits are zeros already, don't do the extend.
1551 if (N1.getOpcode() == ISD::AND)
1552 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1553 uint64_t Mask = N1C->getValue();
1554 unsigned NumBits = MVT::getSizeInBits(EVT);
1555 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1556 return N1;
1557 }
1558 break;
1559 }
1560
Nate Begemaneea805e2005-04-13 21:23:31 +00001561 // FIXME: figure out how to safely handle things like
1562 // int foo(int x) { return 1 << (x & 255); }
1563 // int bar() { return foo(256); }
1564#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001565 case ISD::SHL:
1566 case ISD::SRL:
1567 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001568 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001569 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001570 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001571 else if (N2.getOpcode() == ISD::AND)
1572 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1573 // If the and is only masking out bits that cannot effect the shift,
1574 // eliminate the and.
1575 unsigned NumBits = MVT::getSizeInBits(VT);
1576 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1577 return getNode(Opcode, VT, N1, N2.getOperand(0));
1578 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001579 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001580#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001581 }
1582
Chris Lattner27e9b412005-05-11 18:57:39 +00001583 // Memoize this node if possible.
1584 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001585 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001586 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1587 if (BON) return SDOperand(BON, 0);
1588
1589 BON = N = new SDNode(Opcode, N1, N2);
1590 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001591 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001592 }
1593
Chris Lattner3e011362005-05-14 07:45:46 +00001594 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001595 AllNodes.push_back(N);
1596 return SDOperand(N, 0);
1597}
1598
Chris Lattner88de6e72005-05-12 00:17:04 +00001599// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001600// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001601void SDNode::setAdjCallChain(SDOperand N) {
1602 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001603 assert((getOpcode() == ISD::CALLSEQ_START ||
1604 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001605
1606 Operands[0].Val->removeUser(this);
1607 Operands[0] = N;
1608 N.Val->Uses.push_back(this);
1609}
1610
1611
1612
Chris Lattnerc3aae252005-01-07 07:46:32 +00001613SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001614 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001615 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001616 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1617 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001618 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001619
1620 // Loads have a token chain.
1621 N->setValueTypes(VT, MVT::Other);
1622 AllNodes.push_back(N);
1623 return SDOperand(N, 0);
1624}
1625
Chris Lattner5f056bf2005-07-10 01:55:33 +00001626
1627SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1628 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1629 MVT::ValueType EVT) {
1630 std::vector<SDOperand> Ops;
1631 Ops.reserve(4);
1632 Ops.push_back(Chain);
1633 Ops.push_back(Ptr);
1634 Ops.push_back(SV);
1635 Ops.push_back(getValueType(EVT));
1636 std::vector<MVT::ValueType> VTs;
1637 VTs.reserve(2);
1638 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1639 return getNode(Opcode, VTs, Ops);
1640}
1641
Chris Lattnerc3aae252005-01-07 07:46:32 +00001642SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1643 SDOperand N1, SDOperand N2, SDOperand N3) {
1644 // Perform various simplifications.
1645 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1646 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1647 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1648 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001649 case ISD::SETCC: {
1650 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001651 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001652 if (Simp.Val) return Simp;
1653 break;
1654 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001655 case ISD::SELECT:
1656 if (N1C)
1657 if (N1C->getValue())
1658 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001659 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001660 return N3; // select false, X, Y -> Y
1661
1662 if (N2 == N3) return N2; // select C, X, X -> X
1663
1664 if (VT == MVT::i1) { // Boolean SELECT
1665 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001666 if (N2C->getValue()) // select C, 1, X -> C | X
1667 return getNode(ISD::OR, VT, N1, N3);
1668 else // select C, 0, X -> ~C & X
1669 return getNode(ISD::AND, VT,
1670 getNode(ISD::XOR, N1.getValueType(), N1,
1671 getConstant(1, N1.getValueType())), N3);
1672 } else if (N3C) {
1673 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1674 return getNode(ISD::OR, VT,
1675 getNode(ISD::XOR, N1.getValueType(), N1,
1676 getConstant(1, N1.getValueType())), N2);
1677 else // select C, X, 0 -> C & X
1678 return getNode(ISD::AND, VT, N1, N2);
1679 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001680
1681 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1682 return getNode(ISD::OR, VT, N1, N3);
1683 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1684 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001685 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001686 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001687 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1688 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001689 if (Simp.Val) return Simp;
1690 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001691 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001692 case ISD::BRCOND:
1693 if (N2C)
1694 if (N2C->getValue()) // Unconditional branch
1695 return getNode(ISD::BR, MVT::Other, N1, N3);
1696 else
1697 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001698 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001699 }
1700
Chris Lattner385328c2005-05-14 07:42:29 +00001701 std::vector<SDOperand> Ops;
1702 Ops.reserve(3);
1703 Ops.push_back(N1);
1704 Ops.push_back(N2);
1705 Ops.push_back(N3);
1706
1707 // Memoize nodes.
1708 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1709 if (N) return SDOperand(N, 0);
1710
1711 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001712 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001713 AllNodes.push_back(N);
1714 return SDOperand(N, 0);
1715}
1716
1717SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001718 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001719 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001720 std::vector<SDOperand> Ops;
1721 Ops.reserve(4);
1722 Ops.push_back(N1);
1723 Ops.push_back(N2);
1724 Ops.push_back(N3);
1725 Ops.push_back(N4);
1726 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001727}
1728
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001729SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1730 SDOperand N1, SDOperand N2, SDOperand N3,
1731 SDOperand N4, SDOperand N5) {
1732 std::vector<SDOperand> Ops;
1733 Ops.reserve(5);
1734 Ops.push_back(N1);
1735 Ops.push_back(N2);
1736 Ops.push_back(N3);
1737 Ops.push_back(N4);
1738 Ops.push_back(N5);
1739 return getNode(Opcode, VT, Ops);
1740}
1741
1742
Chris Lattner0437cdd2005-05-09 04:14:13 +00001743SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001744 assert((!V || isa<PointerType>(V->getType())) &&
1745 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001746 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1747 if (N) return SDOperand(N, 0);
1748
1749 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001750 AllNodes.push_back(N);
1751 return SDOperand(N, 0);
1752}
1753
1754SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001755 std::vector<SDOperand> &Ops) {
1756 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001757 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001758 case 1: return getNode(Opcode, VT, Ops[0]);
1759 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1760 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001761 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001762 }
Chris Lattneref847df2005-04-09 03:27:28 +00001763
Chris Lattner89c34632005-05-14 06:20:26 +00001764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001765 switch (Opcode) {
1766 default: break;
1767 case ISD::BRCONDTWOWAY:
1768 if (N1C)
1769 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001770 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001771 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001772 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001773 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001774 case ISD::BRTWOWAY_CC:
1775 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1776 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1777 "LHS and RHS of comparison must have same type!");
1778 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001779 case ISD::TRUNCSTORE: {
1780 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1781 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1782#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1783 // If this is a truncating store of a constant, convert to the desired type
1784 // and store it instead.
1785 if (isa<Constant>(Ops[0])) {
1786 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1787 if (isa<Constant>(Op))
1788 N1 = Op;
1789 }
1790 // Also for ConstantFP?
1791#endif
1792 if (Ops[0].getValueType() == EVT) // Normal store?
1793 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1794 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1795 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1796 "Can't do FP-INT conversion!");
1797 break;
1798 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001799 case ISD::SELECT_CC: {
1800 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1801 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1802 "LHS and RHS of condition must have same type!");
1803 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1804 "True and False arms of SelectCC must have same type!");
1805 assert(Ops[2].getValueType() == VT &&
1806 "select_cc node must be of same type as true and false value!");
1807 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1808 cast<CondCodeSDNode>(Ops[4])->get());
1809 if (Simp.Val) return Simp;
1810 break;
1811 }
1812 case ISD::BR_CC: {
1813 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1814 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1815 "LHS/RHS of comparison should match types!");
1816 // Use SimplifySetCC to simplify SETCC's.
1817 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
1818 cast<CondCodeSDNode>(Ops[1])->get());
1819 if (Simp.Val) {
1820 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
1821 if (C->getValue() & 1) // Unconditional branch
1822 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
1823 else
1824 return Ops[0]; // Unconditional Fall through
1825 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
1826 Ops[2] = Simp.getOperand(0);
1827 Ops[3] = Simp.getOperand(1);
1828 Ops[1] = Simp.getOperand(2);
1829 }
1830 }
1831 break;
1832 }
Chris Lattneref847df2005-04-09 03:27:28 +00001833 }
1834
Chris Lattner385328c2005-05-14 07:42:29 +00001835 // Memoize nodes.
1836 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1837 if (N) return SDOperand(N, 0);
1838 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001839 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001840 AllNodes.push_back(N);
1841 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001842}
1843
Chris Lattner89c34632005-05-14 06:20:26 +00001844SDOperand SelectionDAG::getNode(unsigned Opcode,
1845 std::vector<MVT::ValueType> &ResultTys,
1846 std::vector<SDOperand> &Ops) {
1847 if (ResultTys.size() == 1)
1848 return getNode(Opcode, ResultTys[0], Ops);
1849
Chris Lattner5f056bf2005-07-10 01:55:33 +00001850 switch (Opcode) {
1851 case ISD::EXTLOAD:
1852 case ISD::SEXTLOAD:
1853 case ISD::ZEXTLOAD: {
1854 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1855 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1856 // If they are asking for an extending load from/to the same thing, return a
1857 // normal load.
1858 if (ResultTys[0] == EVT)
1859 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1860 assert(EVT < ResultTys[0] &&
1861 "Should only be an extending load, not truncating!");
1862 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1863 "Cannot sign/zero extend a FP load!");
1864 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1865 "Cannot convert from FP to Int or Int -> FP!");
1866 break;
1867 }
1868
Chris Lattnere89083a2005-05-14 07:25:05 +00001869 // FIXME: figure out how to safely handle things like
1870 // int foo(int x) { return 1 << (x & 255); }
1871 // int bar() { return foo(256); }
1872#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001873 case ISD::SRA_PARTS:
1874 case ISD::SRL_PARTS:
1875 case ISD::SHL_PARTS:
1876 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001877 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001878 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1879 else if (N3.getOpcode() == ISD::AND)
1880 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1881 // If the and is only masking out bits that cannot effect the shift,
1882 // eliminate the and.
1883 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1884 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1885 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1886 }
1887 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001888#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001889 }
Chris Lattner89c34632005-05-14 06:20:26 +00001890
1891 // Memoize the node.
1892 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1893 Ops))];
1894 if (N) return SDOperand(N, 0);
1895 N = new SDNode(Opcode, Ops);
1896 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001897 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001898 return SDOperand(N, 0);
1899}
1900
Chris Lattner149c58c2005-08-16 18:17:10 +00001901
1902/// SelectNodeTo - These are used for target selectors to *mutate* the
1903/// specified node to have the specified return type, Target opcode, and
1904/// operands. Note that target opcodes are stored as
1905/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1906void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
Chris Lattner7651fa42005-08-24 23:00:29 +00001907 unsigned TargetOpc) {
1908 RemoveNodeFromCSEMaps(N);
1909 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1910 N->setValueTypes(VT);
1911}
1912void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
Chris Lattner149c58c2005-08-16 18:17:10 +00001913 unsigned TargetOpc, SDOperand Op1) {
1914 RemoveNodeFromCSEMaps(N);
1915 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1916 N->setValueTypes(VT);
1917 N->setOperands(Op1);
1918}
1919void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1920 unsigned TargetOpc, SDOperand Op1,
1921 SDOperand Op2) {
1922 RemoveNodeFromCSEMaps(N);
1923 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1924 N->setValueTypes(VT);
1925 N->setOperands(Op1, Op2);
1926}
Chris Lattner99badda2005-08-21 19:48:59 +00001927void SelectionDAG::SelectNodeTo(SDNode *N,
1928 MVT::ValueType VT1, MVT::ValueType VT2,
1929 unsigned TargetOpc, SDOperand Op1,
1930 SDOperand Op2) {
1931 RemoveNodeFromCSEMaps(N);
1932 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1933 N->setValueTypes(VT1, VT2);
1934 N->setOperands(Op1, Op2);
1935}
Chris Lattner149c58c2005-08-16 18:17:10 +00001936void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1937 unsigned TargetOpc, SDOperand Op1,
1938 SDOperand Op2, SDOperand Op3) {
1939 RemoveNodeFromCSEMaps(N);
1940 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1941 N->setValueTypes(VT);
1942 N->setOperands(Op1, Op2, Op3);
1943}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001944void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT1,
1945 MVT::ValueType VT2,
1946 unsigned TargetOpc, SDOperand Op1,
1947 SDOperand Op2, SDOperand Op3) {
1948 RemoveNodeFromCSEMaps(N);
1949 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1950 N->setValueTypes(VT1, VT2);
1951 N->setOperands(Op1, Op2, Op3);
1952}
1953
Nate Begeman294a0a12005-08-18 07:30:15 +00001954void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1955 unsigned TargetOpc, SDOperand Op1,
1956 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1957 RemoveNodeFromCSEMaps(N);
1958 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1959 N->setValueTypes(VT);
1960 N->setOperands(Op1, Op2, Op3, Op4);
1961}
Chris Lattner6b09a292005-08-21 18:49:33 +00001962void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1963 unsigned TargetOpc, SDOperand Op1,
1964 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1965 SDOperand Op5) {
1966 RemoveNodeFromCSEMaps(N);
1967 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1968 N->setValueTypes(VT);
1969 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1970}
Chris Lattner149c58c2005-08-16 18:17:10 +00001971
Chris Lattner8b8749f2005-08-17 19:00:20 +00001972/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1973/// This can cause recursive merging of nodes in the DAG.
1974///
1975void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1976 assert(From != To && "Cannot replace uses of with self");
1977 while (!From->use_empty()) {
1978 // Process users until they are all gone.
1979 SDNode *U = *From->use_begin();
1980
1981 // This node is about to morph, remove its old self from the CSE maps.
1982 RemoveNodeFromCSEMaps(U);
1983
1984 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1985 if (U->getOperand(i).Val == From) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00001986 assert(U->getOperand(i).getValueType() ==
Chris Lattner8b8749f2005-08-17 19:00:20 +00001987 To->getValueType(U->getOperand(i).ResNo));
1988 From->removeUser(U);
1989 U->Operands[i].Val = To;
1990 To->addUser(U);
1991 }
1992
1993 // Now that we have modified U, add it back to the CSE maps. If it already
1994 // exists there, recursively merge the results together.
1995 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
1996 ReplaceAllUsesWith(U, Existing);
1997 // U is now dead.
1998 }
1999}
2000
Chris Lattner7b2880c2005-08-24 22:44:39 +00002001void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2002 const std::vector<SDOperand> &To) {
2003 assert(From->getNumValues() == To.size() &&
2004 "Incorrect number of values to replace with!");
2005 if (To.size() == 1 && To[0].ResNo == 0) {
2006 // Degenerate case handled above.
2007 ReplaceAllUsesWith(From, To[0].Val);
2008 return;
2009 }
2010
2011 while (!From->use_empty()) {
2012 // Process users until they are all gone.
2013 SDNode *U = *From->use_begin();
2014
2015 // This node is about to morph, remove its old self from the CSE maps.
2016 RemoveNodeFromCSEMaps(U);
2017
2018 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2019 if (U->getOperand(i).Val == From) {
2020 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
2021 assert(U->getOperand(i).getValueType() == ToOp.getValueType());
2022 From->removeUser(U);
2023 U->Operands[i] = ToOp;
2024 ToOp.Val->addUser(U);
2025 }
2026
2027 // Now that we have modified U, add it back to the CSE maps. If it already
2028 // exists there, recursively merge the results together.
2029 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2030 ReplaceAllUsesWith(U, Existing);
2031 // U is now dead.
2032 }
2033}
2034
2035
Jim Laskey58b968b2005-08-17 20:08:02 +00002036//===----------------------------------------------------------------------===//
2037// SDNode Class
2038//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002039
Chris Lattner5c884562005-01-12 18:37:47 +00002040/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2041/// indicated value. This method ignores uses of other values defined by this
2042/// operation.
2043bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2044 assert(Value < getNumValues() && "Bad value!");
2045
2046 // If there is only one value, this is easy.
2047 if (getNumValues() == 1)
2048 return use_size() == NUses;
2049 if (Uses.size() < NUses) return false;
2050
2051 SDOperand TheValue(this, Value);
2052
2053 std::set<SDNode*> UsersHandled;
2054
2055 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2056 UI != E; ++UI) {
2057 SDNode *User = *UI;
2058 if (User->getNumOperands() == 1 ||
2059 UsersHandled.insert(User).second) // First time we've seen this?
2060 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2061 if (User->getOperand(i) == TheValue) {
2062 if (NUses == 0)
2063 return false; // too many uses
2064 --NUses;
2065 }
2066 }
2067
2068 // Found exactly the right number of uses?
2069 return NUses == 0;
2070}
2071
2072
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002073const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002074 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002075 default:
2076 if (getOpcode() < ISD::BUILTIN_OP_END)
2077 return "<<Unknown DAG Node>>";
2078 else {
2079 if (G)
2080 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2081 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2082 return "<<Unknown Target Node>>";
2083 }
2084
Andrew Lenharth95762122005-03-31 21:24:06 +00002085 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002086 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002087 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002088 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002089 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002090 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002091 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002092 case ISD::ConstantFP: return "ConstantFP";
2093 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002094 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002095 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002096 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002097 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002098 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002099 case ISD::ExternalSymbol: return "ExternalSymbol";
2100 case ISD::ConstantPool: return "ConstantPoolIndex";
Chris Lattner4025a9c2005-08-25 05:03:06 +00002101 case ISD::TargetConstantPool: return "TargetConstantPoolIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002102 case ISD::CopyToReg: return "CopyToReg";
2103 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002104 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002105 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002106
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002107 // Unary operators
2108 case ISD::FABS: return "fabs";
2109 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002110 case ISD::FSQRT: return "fsqrt";
2111 case ISD::FSIN: return "fsin";
2112 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002113
2114 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002115 case ISD::ADD: return "add";
2116 case ISD::SUB: return "sub";
2117 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002118 case ISD::MULHU: return "mulhu";
2119 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002120 case ISD::SDIV: return "sdiv";
2121 case ISD::UDIV: return "udiv";
2122 case ISD::SREM: return "srem";
2123 case ISD::UREM: return "urem";
2124 case ISD::AND: return "and";
2125 case ISD::OR: return "or";
2126 case ISD::XOR: return "xor";
2127 case ISD::SHL: return "shl";
2128 case ISD::SRA: return "sra";
2129 case ISD::SRL: return "srl";
2130
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002131 case ISD::SETCC: return "setcc";
2132 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002133 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002134 case ISD::ADD_PARTS: return "add_parts";
2135 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002136 case ISD::SHL_PARTS: return "shl_parts";
2137 case ISD::SRA_PARTS: return "sra_parts";
2138 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002139
Chris Lattner7f644642005-04-28 21:44:03 +00002140 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002141 case ISD::SIGN_EXTEND: return "sign_extend";
2142 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002143 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002144 case ISD::TRUNCATE: return "truncate";
2145 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002146 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002147 case ISD::FP_EXTEND: return "fp_extend";
2148
2149 case ISD::SINT_TO_FP: return "sint_to_fp";
2150 case ISD::UINT_TO_FP: return "uint_to_fp";
2151 case ISD::FP_TO_SINT: return "fp_to_sint";
2152 case ISD::FP_TO_UINT: return "fp_to_uint";
2153
2154 // Control flow instructions
2155 case ISD::BR: return "br";
2156 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002157 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002158 case ISD::BR_CC: return "br_cc";
2159 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002160 case ISD::RET: return "ret";
2161 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002162 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002163 case ISD::CALLSEQ_START: return "callseq_start";
2164 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002165
2166 // Other operators
2167 case ISD::LOAD: return "load";
2168 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002169 case ISD::EXTLOAD: return "extload";
2170 case ISD::SEXTLOAD: return "sextload";
2171 case ISD::ZEXTLOAD: return "zextload";
2172 case ISD::TRUNCSTORE: return "truncstore";
2173
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002174 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2175 case ISD::EXTRACT_ELEMENT: return "extract_element";
2176 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002177 case ISD::MEMSET: return "memset";
2178 case ISD::MEMCPY: return "memcpy";
2179 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002180
Chris Lattner276260b2005-05-11 04:50:30 +00002181 // Bit counting
2182 case ISD::CTPOP: return "ctpop";
2183 case ISD::CTTZ: return "cttz";
2184 case ISD::CTLZ: return "ctlz";
2185
2186 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002187 case ISD::READPORT: return "readport";
2188 case ISD::WRITEPORT: return "writeport";
2189 case ISD::READIO: return "readio";
2190 case ISD::WRITEIO: return "writeio";
2191
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002192 case ISD::CONDCODE:
2193 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002194 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002195 case ISD::SETOEQ: return "setoeq";
2196 case ISD::SETOGT: return "setogt";
2197 case ISD::SETOGE: return "setoge";
2198 case ISD::SETOLT: return "setolt";
2199 case ISD::SETOLE: return "setole";
2200 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002201
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002202 case ISD::SETO: return "seto";
2203 case ISD::SETUO: return "setuo";
2204 case ISD::SETUEQ: return "setue";
2205 case ISD::SETUGT: return "setugt";
2206 case ISD::SETUGE: return "setuge";
2207 case ISD::SETULT: return "setult";
2208 case ISD::SETULE: return "setule";
2209 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002210
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002211 case ISD::SETEQ: return "seteq";
2212 case ISD::SETGT: return "setgt";
2213 case ISD::SETGE: return "setge";
2214 case ISD::SETLT: return "setlt";
2215 case ISD::SETLE: return "setle";
2216 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002217 }
2218 }
2219}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002220
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002221void SDNode::dump() const { dump(0); }
2222void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002223 std::cerr << (void*)this << ": ";
2224
2225 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2226 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002227 if (getValueType(i) == MVT::Other)
2228 std::cerr << "ch";
2229 else
2230 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002231 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002232 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002233
2234 std::cerr << " ";
2235 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2236 if (i) std::cerr << ", ";
2237 std::cerr << (void*)getOperand(i).Val;
2238 if (unsigned RN = getOperand(i).ResNo)
2239 std::cerr << ":" << RN;
2240 }
2241
2242 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2243 std::cerr << "<" << CSDN->getValue() << ">";
2244 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2245 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002246 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002247 dyn_cast<GlobalAddressSDNode>(this)) {
2248 std::cerr << "<";
2249 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002250 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002251 std::cerr << "<" << FIDN->getIndex() << ">";
2252 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2253 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002254 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002255 std::cerr << "<";
2256 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2257 if (LBB)
2258 std::cerr << LBB->getName() << " ";
2259 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002260 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002261 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2262 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2263 } else {
2264 std::cerr << " #" << R->getReg();
2265 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002266 } else if (const ExternalSymbolSDNode *ES =
2267 dyn_cast<ExternalSymbolSDNode>(this)) {
2268 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002269 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2270 if (M->getValue())
2271 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2272 else
2273 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002274 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2275 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002276 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002277}
2278
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002279static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002280 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2281 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002282 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002283 else
2284 std::cerr << "\n" << std::string(indent+2, ' ')
2285 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002286
Chris Lattnerea946cd2005-01-09 20:38:33 +00002287
2288 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002289 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002290}
2291
Chris Lattnerc3aae252005-01-07 07:46:32 +00002292void SelectionDAG::dump() const {
2293 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002294 std::vector<SDNode*> Nodes(AllNodes);
2295 std::sort(Nodes.begin(), Nodes.end());
2296
2297 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002298 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002299 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002300 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002301
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002302 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002303
Chris Lattnerc3aae252005-01-07 07:46:32 +00002304 std::cerr << "\n\n";
2305}
2306