blob: 0098a56a9a241f67b03555c4f7206b7748aff382 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
12//
13// FIXME: Missing folds
14// sdiv, udiv, srem, urem (X, const) where X is an integer can be expanded into
15// a sequence of multiplies, shifts, and adds. This should be controlled by
16// some kind of hint from the target that int div is expensive.
17// various folds of mulh[s,u] by constants such as -1, powers of 2, etc.
18//
19// FIXME: Should add a corresponding version of fold AND with
20// ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
21// we don't have yet.
22//
Nate Begeman44728a72005-09-19 22:34:01 +000023// FIXME: select C, pow2, pow2 -> something smart
24// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
25// FIXME: (select C, load A, load B) -> load (select C, A, B)
Nate Begeman44728a72005-09-19 22:34:01 +000026// FIXME: Dead stores -> nuke
27// FIXME: shr X, (and Y,31) -> shr X, Y
28// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000029// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000030// FIXME: undef values
Nate Begeman4ebd8052005-09-01 23:24:04 +000031// FIXME: make truncate see through SIGN_EXTEND and AND
Nate Begeman4ebd8052005-09-01 23:24:04 +000032// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000033// FIXME: verify that getNode can't return extends with an operand whose type
34// is >= to that of the extend.
35// FIXME: divide by zero is currently left unfolded. do we want to turn this
36// into an undef?
Nate Begemanf845b452005-10-08 00:29:44 +000037// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattner01a22022005-10-10 22:04:48 +000038// FIXME: reassociate (X+C)+Y into (X+Y)+C if the inner expression has one use
Nate Begeman1d4d4142005-09-01 00:19:25 +000039//
40//===----------------------------------------------------------------------===//
41
42#define DEBUG_TYPE "dagcombine"
43#include "llvm/ADT/Statistic.h"
44#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000045#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000046#include "llvm/Support/MathExtras.h"
47#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000048#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include <cmath>
50using namespace llvm;
51
52namespace {
53 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
54
55 class DAGCombiner {
56 SelectionDAG &DAG;
57 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000058 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000059
60 // Worklist of all of the nodes that need to be simplified.
61 std::vector<SDNode*> WorkList;
62
63 /// AddUsersToWorkList - When an instruction is simplified, add all users of
64 /// the instruction to the work lists because they might get more simplified
65 /// now.
66 ///
67 void AddUsersToWorkList(SDNode *N) {
68 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000069 UI != UE; ++UI)
70 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000071 }
72
73 /// removeFromWorkList - remove all instances of N from the worklist.
74 void removeFromWorkList(SDNode *N) {
75 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
76 WorkList.end());
77 }
78
Chris Lattner01a22022005-10-10 22:04:48 +000079 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner87514ca2005-10-10 22:31:19 +000080 ++NodesCombined;
Chris Lattner01a22022005-10-10 22:04:48 +000081 DEBUG(std::cerr << "\nReplacing "; N->dump();
82 std::cerr << "\nWith: "; To[0].Val->dump();
83 std::cerr << " and " << To.size()-1 << " other values\n");
84 std::vector<SDNode*> NowDead;
85 DAG.ReplaceAllUsesWith(N, To, &NowDead);
86
87 // Push the new nodes and any users onto the worklist
88 for (unsigned i = 0, e = To.size(); i != e; ++i) {
89 WorkList.push_back(To[i].Val);
90 AddUsersToWorkList(To[i].Val);
91 }
92
93 // Nodes can end up on the worklist more than once. Make sure we do
94 // not process a node that has been replaced.
95 removeFromWorkList(N);
96 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
97 removeFromWorkList(NowDead[i]);
98
99 // Finally, since the node is now dead, remove it from the graph.
100 DAG.DeleteNode(N);
101 return SDOperand(N, 0);
102 }
Chris Lattner87514ca2005-10-10 22:31:19 +0000103
104 SDOperand CombineTo(SDNode *N, SDOperand Res) {
105 std::vector<SDOperand> To;
106 To.push_back(Res);
107 return CombineTo(N, To);
108 }
Chris Lattner01a22022005-10-10 22:04:48 +0000109
110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
111 std::vector<SDOperand> To;
112 To.push_back(Res0);
113 To.push_back(Res1);
114 return CombineTo(N, To);
115 }
116
Nate Begeman1d4d4142005-09-01 00:19:25 +0000117 /// visit - call the node-specific routine that knows how to fold each
118 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000119 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000120
121 // Visitation implementation - Implement dag node combining for different
122 // node types. The semantics are as follows:
123 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000124 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000125 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000126 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000127 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000128 SDOperand visitTokenFactor(SDNode *N);
129 SDOperand visitADD(SDNode *N);
130 SDOperand visitSUB(SDNode *N);
131 SDOperand visitMUL(SDNode *N);
132 SDOperand visitSDIV(SDNode *N);
133 SDOperand visitUDIV(SDNode *N);
134 SDOperand visitSREM(SDNode *N);
135 SDOperand visitUREM(SDNode *N);
136 SDOperand visitMULHU(SDNode *N);
137 SDOperand visitMULHS(SDNode *N);
138 SDOperand visitAND(SDNode *N);
139 SDOperand visitOR(SDNode *N);
140 SDOperand visitXOR(SDNode *N);
141 SDOperand visitSHL(SDNode *N);
142 SDOperand visitSRA(SDNode *N);
143 SDOperand visitSRL(SDNode *N);
144 SDOperand visitCTLZ(SDNode *N);
145 SDOperand visitCTTZ(SDNode *N);
146 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000147 SDOperand visitSELECT(SDNode *N);
148 SDOperand visitSELECT_CC(SDNode *N);
149 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitSIGN_EXTEND(SDNode *N);
151 SDOperand visitZERO_EXTEND(SDNode *N);
152 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
153 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000154
155 SDOperand visitFADD(SDNode *N);
156 SDOperand visitFSUB(SDNode *N);
157 SDOperand visitFMUL(SDNode *N);
158 SDOperand visitFDIV(SDNode *N);
159 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000160 SDOperand visitSINT_TO_FP(SDNode *N);
161 SDOperand visitUINT_TO_FP(SDNode *N);
162 SDOperand visitFP_TO_SINT(SDNode *N);
163 SDOperand visitFP_TO_UINT(SDNode *N);
164 SDOperand visitFP_ROUND(SDNode *N);
165 SDOperand visitFP_ROUND_INREG(SDNode *N);
166 SDOperand visitFP_EXTEND(SDNode *N);
167 SDOperand visitFNEG(SDNode *N);
168 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000169 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000170 SDOperand visitBRCONDTWOWAY(SDNode *N);
171 SDOperand visitBR_CC(SDNode *N);
172 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000173
Chris Lattner01a22022005-10-10 22:04:48 +0000174 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000175 SDOperand visitSTORE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000176
Nate Begeman44728a72005-09-19 22:34:01 +0000177 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
178 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
179 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000180 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000181 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000182public:
183 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000184 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000185
186 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000187 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000188 };
189}
190
Nate Begeman07ed4172005-10-10 21:26:48 +0000191/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
192/// this predicate to simplify operations downstream. Op and Mask are known to
Nate Begeman1d4d4142005-09-01 00:19:25 +0000193/// be the same type.
194static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
195 const TargetLowering &TLI) {
196 unsigned SrcBits;
197 if (Mask == 0) return true;
198
199 // If we know the result of a setcc has the top bits zero, use this info.
200 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000201 case ISD::Constant:
202 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
203 case ISD::SETCC:
204 return ((Mask & 1) == 0) &&
205 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
206 case ISD::ZEXTLOAD:
207 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
208 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
209 case ISD::ZERO_EXTEND:
210 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
211 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
212 case ISD::AssertZext:
213 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
214 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
215 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000216 // If either of the operands has zero bits, the result will too.
217 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
218 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
219 return true;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000220 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
221 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
222 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000223 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000224 case ISD::OR:
225 case ISD::XOR:
226 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
227 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
228 case ISD::SELECT:
229 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
230 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
231 case ISD::SELECT_CC:
232 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
233 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
234 case ISD::SRL:
235 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
236 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
237 uint64_t NewVal = Mask << ShAmt->getValue();
238 SrcBits = MVT::getSizeInBits(Op.getValueType());
239 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
240 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
241 }
242 return false;
243 case ISD::SHL:
244 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
245 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
246 uint64_t NewVal = Mask >> ShAmt->getValue();
247 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
248 }
249 return false;
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000250 case ISD::ADD:
Chris Lattnerd7390752005-10-10 16:52:03 +0000251 // (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
Chris Lattnerbba9aa32005-10-10 16:51:40 +0000252 if ((Mask&(Mask+1)) == 0) { // All low bits
253 if (MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
254 MaskedValueIsZero(Op.getOperand(1), Mask, TLI))
255 return true;
256 }
257 break;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000258 case ISD::SUB:
259 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
260 // We know that the top bits of C-X are clear if X contains less bits
261 // than C (i.e. no wrap-around can happen). For example, 20-X is
262 // positive if we can prove that X is >= 0 and < 16.
263 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
264 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
265 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
266 uint64_t MaskV = (1ULL << (63-NLZ))-1;
267 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
268 // High bits are clear this value is known to be >= C.
269 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
270 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
271 return true;
272 }
273 }
274 }
275 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000276 case ISD::CTTZ:
277 case ISD::CTLZ:
278 case ISD::CTPOP:
279 // Bit counting instructions can not set the high bits of the result
280 // register. The max number of bits sets depends on the input.
281 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
282
283 // TODO we could handle some SRA cases here.
284 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000285 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000286 return false;
287}
288
Nate Begeman4ebd8052005-09-01 23:24:04 +0000289// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
290// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000291// Also, set the incoming LHS, RHS, and CC references to the appropriate
292// nodes based on the type of node we are checking. This simplifies life a
293// bit for the callers.
294static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
295 SDOperand &CC) {
296 if (N.getOpcode() == ISD::SETCC) {
297 LHS = N.getOperand(0);
298 RHS = N.getOperand(1);
299 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000300 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000301 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000302 if (N.getOpcode() == ISD::SELECT_CC &&
303 N.getOperand(2).getOpcode() == ISD::Constant &&
304 N.getOperand(3).getOpcode() == ISD::Constant &&
305 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000306 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
307 LHS = N.getOperand(0);
308 RHS = N.getOperand(1);
309 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000311 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000312 return false;
313}
314
Nate Begeman99801192005-09-07 23:25:52 +0000315// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
316// one use. If this is true, it allows the users to invert the operation for
317// free when it is profitable to do so.
318static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000319 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000320 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000321 return true;
322 return false;
323}
324
Nate Begeman452d7be2005-09-16 00:54:12 +0000325// FIXME: This should probably go in the ISD class rather than being duplicated
326// in several files.
327static bool isCommutativeBinOp(unsigned Opcode) {
328 switch (Opcode) {
329 case ISD::ADD:
330 case ISD::MUL:
331 case ISD::AND:
332 case ISD::OR:
333 case ISD::XOR: return true;
334 default: return false; // FIXME: Need commutative info for user ops!
335 }
336}
337
Nate Begeman4ebd8052005-09-01 23:24:04 +0000338void DAGCombiner::Run(bool RunningAfterLegalize) {
339 // set the instance variable, so that the various visit routines may use it.
340 AfterLegalize = RunningAfterLegalize;
341
Nate Begeman646d7e22005-09-02 21:18:40 +0000342 // Add all the dag nodes to the worklist.
343 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000344
Chris Lattner95038592005-10-05 06:35:28 +0000345 // Create a dummy node (which is not added to allnodes), that adds a reference
346 // to the root node, preventing it from being deleted, and tracking any
347 // changes of the root.
348 HandleSDNode Dummy(DAG.getRoot());
349
Nate Begeman1d4d4142005-09-01 00:19:25 +0000350 // while the worklist isn't empty, inspect the node on the end of it and
351 // try and combine it.
352 while (!WorkList.empty()) {
353 SDNode *N = WorkList.back();
354 WorkList.pop_back();
355
356 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000357 // N is deleted from the DAG, since they too may now be dead or may have a
358 // reduced number of uses, allowing other xforms.
359 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 WorkList.push_back(N->getOperand(i).Val);
362
Nate Begeman1d4d4142005-09-01 00:19:25 +0000363 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000364 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000365 continue;
366 }
367
Nate Begeman83e75ec2005-09-06 04:43:02 +0000368 SDOperand RV = visit(N);
369 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000370 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000371 // If we get back the same node we passed in, rather than a new node or
372 // zero, we know that the node must have defined multiple values and
373 // CombineTo was used. Since CombineTo takes care of the worklist
374 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000375 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000376 DEBUG(std::cerr << "\nReplacing "; N->dump();
377 std::cerr << "\nWith: "; RV.Val->dump();
378 std::cerr << '\n');
Chris Lattner01a22022005-10-10 22:04:48 +0000379 std::vector<SDNode*> NowDead;
380 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV), &NowDead);
Nate Begeman646d7e22005-09-02 21:18:40 +0000381
382 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000383 WorkList.push_back(RV.Val);
384 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000385
386 // Nodes can end up on the worklist more than once. Make sure we do
387 // not process a node that has been replaced.
388 removeFromWorkList(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000389 for (unsigned i = 0, e = NowDead.size(); i != e; ++i)
390 removeFromWorkList(NowDead[i]);
Chris Lattner5c46f742005-10-05 06:11:08 +0000391
392 // Finally, since the node is now dead, remove it from the graph.
393 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000394 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 }
396 }
Chris Lattner95038592005-10-05 06:35:28 +0000397
398 // If the root changed (e.g. it was a dead load, update the root).
399 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400}
401
Nate Begeman83e75ec2005-09-06 04:43:02 +0000402SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000403 switch(N->getOpcode()) {
404 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000405 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000406 case ISD::ADD: return visitADD(N);
407 case ISD::SUB: return visitSUB(N);
408 case ISD::MUL: return visitMUL(N);
409 case ISD::SDIV: return visitSDIV(N);
410 case ISD::UDIV: return visitUDIV(N);
411 case ISD::SREM: return visitSREM(N);
412 case ISD::UREM: return visitUREM(N);
413 case ISD::MULHU: return visitMULHU(N);
414 case ISD::MULHS: return visitMULHS(N);
415 case ISD::AND: return visitAND(N);
416 case ISD::OR: return visitOR(N);
417 case ISD::XOR: return visitXOR(N);
418 case ISD::SHL: return visitSHL(N);
419 case ISD::SRA: return visitSRA(N);
420 case ISD::SRL: return visitSRL(N);
421 case ISD::CTLZ: return visitCTLZ(N);
422 case ISD::CTTZ: return visitCTTZ(N);
423 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000424 case ISD::SELECT: return visitSELECT(N);
425 case ISD::SELECT_CC: return visitSELECT_CC(N);
426 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000427 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
428 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
429 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
430 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000431 case ISD::FADD: return visitFADD(N);
432 case ISD::FSUB: return visitFSUB(N);
433 case ISD::FMUL: return visitFMUL(N);
434 case ISD::FDIV: return visitFDIV(N);
435 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000436 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
437 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
438 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
439 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
440 case ISD::FP_ROUND: return visitFP_ROUND(N);
441 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
442 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
443 case ISD::FNEG: return visitFNEG(N);
444 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000445 case ISD::BRCOND: return visitBRCOND(N);
446 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
447 case ISD::BR_CC: return visitBR_CC(N);
448 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000449 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000450 case ISD::STORE: return visitSTORE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453}
454
Nate Begeman83e75ec2005-09-06 04:43:02 +0000455SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000456 // If the token factor has two operands and one is the entry token, replace
457 // the token factor with the other operand.
458 if (N->getNumOperands() == 2) {
459 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000460 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000462 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000464 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465}
466
Nate Begeman83e75ec2005-09-06 04:43:02 +0000467SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 SDOperand N0 = N->getOperand(0);
469 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
471 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000472 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000473
474 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000476 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000477 // canonicalize constant to RHS
478 if (N0C && !N1C) {
479 std::swap(N0, N1);
480 std::swap(N0C, N1C);
481 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000484 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000485 // fold (add (add x, c1), c2) -> (add x, c1+c2)
486 if (N1C && N0.getOpcode() == ISD::ADD) {
487 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
488 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
489 if (N00C)
490 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
491 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
492 if (N01C)
493 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
494 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
495 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000496 // fold ((0-A) + B) -> B-A
497 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
498 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000499 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000500 // fold (A + (0-B)) -> A-B
501 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
502 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000503 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000504 // fold (A+(B-A)) -> B
505 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000506 return N1.getOperand(0);
507 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508}
509
Nate Begeman83e75ec2005-09-06 04:43:02 +0000510SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000511 SDOperand N0 = N->getOperand(0);
512 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000513 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
514 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515
516 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000517 if (N0C && N1C)
518 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000519 N->getValueType(0));
Chris Lattner05b57432005-10-11 06:07:15 +0000520 // fold (sub x, c) -> (add x, -c)
521 if (N1C)
522 return DAG.getNode(ISD::ADD, N0.getValueType(), N0,
523 DAG.getConstant(-N1C->getValue(), N0.getValueType()));
524
Nate Begeman1d4d4142005-09-01 00:19:25 +0000525 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000526 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000527 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000528 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000529 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000530 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000531 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000532}
533
Nate Begeman83e75ec2005-09-06 04:43:02 +0000534SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535 SDOperand N0 = N->getOperand(0);
536 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
538 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000539 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000540
541 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000542 if (N0C && N1C)
543 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000544 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000545 // canonicalize constant to RHS
546 if (N0C && !N1C) {
547 std::swap(N0, N1);
548 std::swap(N0C, N1C);
549 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000551 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000554 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000555 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000556 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000557 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000558 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000559 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000560 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000562 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
563 if (N1C && N0.getOpcode() == ISD::MUL) {
564 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
565 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
566 if (N00C)
567 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
568 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
569 if (N01C)
570 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
571 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
572 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000573 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000574}
575
Nate Begeman83e75ec2005-09-06 04:43:02 +0000576SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000577 SDOperand N0 = N->getOperand(0);
578 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000579 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
581 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000582
583 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000584 if (N0C && N1C && !N1C->isNullValue())
585 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000586 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000587 // If we know the sign bits of both operands are zero, strength reduce to a
588 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
589 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
590 if (MaskedValueIsZero(N1, SignBit, TLI) &&
591 MaskedValueIsZero(N0, SignBit, TLI))
592 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000593 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000594}
595
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597 SDOperand N0 = N->getOperand(0);
598 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000599 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601
602 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000603 if (N0C && N1C && !N1C->isNullValue())
604 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000605 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000609 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000610 TLI.getShiftAmountTy()));
611 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000612}
613
Nate Begeman83e75ec2005-09-06 04:43:02 +0000614SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 SDOperand N0 = N->getOperand(0);
616 SDOperand N1 = N->getOperand(1);
Nate Begeman07ed4172005-10-10 21:26:48 +0000617 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000618 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
619 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620
621 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 if (N0C && N1C && !N1C->isNullValue())
623 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000624 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000625 // If we know the sign bits of both operands are zero, strength reduce to a
626 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
627 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
628 if (MaskedValueIsZero(N1, SignBit, TLI) &&
629 MaskedValueIsZero(N0, SignBit, TLI))
630 return DAG.getNode(ISD::UREM, N1.getValueType(), N0, N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000631 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000632}
633
Nate Begeman83e75ec2005-09-06 04:43:02 +0000634SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635 SDOperand N0 = N->getOperand(0);
636 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
638 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000639
640 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000641 if (N0C && N1C && !N1C->isNullValue())
642 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000643 N->getValueType(0));
Nate Begeman07ed4172005-10-10 21:26:48 +0000644 // fold (urem x, pow2) -> (and x, pow2-1)
645 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
646 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
647 DAG.getConstant(N1C->getValue()-1, N1.getValueType()));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000648 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000649}
650
Nate Begeman83e75ec2005-09-06 04:43:02 +0000651SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000652 SDOperand N0 = N->getOperand(0);
653 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000654 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000655
656 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000657 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000658 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000660 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000661 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
662 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000663 TLI.getShiftAmountTy()));
664 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665}
666
Nate Begeman83e75ec2005-09-06 04:43:02 +0000667SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000668 SDOperand N0 = N->getOperand(0);
669 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671
672 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000673 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000674 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000675 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000676 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000677 return DAG.getConstant(0, N0.getValueType());
678 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679}
680
Nate Begeman83e75ec2005-09-06 04:43:02 +0000681SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000682 SDOperand N0 = N->getOperand(0);
683 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000684 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
686 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000688 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000689
690 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000692 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000693 // canonicalize constant to RHS
694 if (N0C && !N1C) {
695 std::swap(N0, N1);
696 std::swap(N0C, N1C);
697 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000698 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000699 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000700 return N0;
701 // if (and x, c) is known to be zero, return 0
702 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
703 return DAG.getConstant(0, VT);
704 // fold (and x, c) -> x iff (x & ~c) == 0
705 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
706 TLI))
707 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000708 // fold (and (and x, c1), c2) -> (and x, c1^c2)
709 if (N1C && N0.getOpcode() == ISD::AND) {
710 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
711 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
712 if (N00C)
713 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
714 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
715 if (N01C)
716 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
717 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
718 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000719 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
720 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
721 unsigned ExtendBits =
722 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000724 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000725 }
726 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
727 if (N0.getOpcode() == ISD::OR)
728 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000729 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000730 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000731 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
732 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
733 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
734 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
735
736 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
737 MVT::isInteger(LL.getValueType())) {
738 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
739 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
740 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
741 WorkList.push_back(ORNode.Val);
742 return DAG.getSetCC(VT, ORNode, LR, Op1);
743 }
744 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
745 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
746 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
747 WorkList.push_back(ANDNode.Val);
748 return DAG.getSetCC(VT, ANDNode, LR, Op1);
749 }
750 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
751 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
752 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
753 WorkList.push_back(ORNode.Val);
754 return DAG.getSetCC(VT, ORNode, LR, Op1);
755 }
756 }
757 // canonicalize equivalent to ll == rl
758 if (LL == RR && LR == RL) {
759 Op1 = ISD::getSetCCSwappedOperands(Op1);
760 std::swap(RL, RR);
761 }
762 if (LL == RL && LR == RR) {
763 bool isInteger = MVT::isInteger(LL.getValueType());
764 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
765 if (Result != ISD::SETCC_INVALID)
766 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
767 }
768 }
769 // fold (and (zext x), (zext y)) -> (zext (and x, y))
770 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
771 N1.getOpcode() == ISD::ZERO_EXTEND &&
772 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
773 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
774 N0.getOperand(0), N1.getOperand(0));
775 WorkList.push_back(ANDNode.Val);
776 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
777 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000778 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
779 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
780 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
781 N0.getOperand(1) == N1.getOperand(1)) {
782 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
783 N0.getOperand(0), N1.getOperand(0));
784 WorkList.push_back(ANDNode.Val);
785 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
786 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000787 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000788}
789
Nate Begeman83e75ec2005-09-06 04:43:02 +0000790SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791 SDOperand N0 = N->getOperand(0);
792 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000793 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000794 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
795 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000796 MVT::ValueType VT = N1.getValueType();
797 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000798
799 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000800 if (N0C && N1C)
801 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000802 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000803 // canonicalize constant to RHS
804 if (N0C && !N1C) {
805 std::swap(N0, N1);
806 std::swap(N0C, N1C);
807 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000808 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000809 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000810 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000811 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000812 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000813 return N1;
814 // fold (or x, c) -> c iff (x & ~c) == 0
815 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
816 TLI))
817 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000818 // fold (or (or x, c1), c2) -> (or x, c1|c2)
819 if (N1C && N0.getOpcode() == ISD::OR) {
820 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
821 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
822 if (N00C)
823 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
824 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
825 if (N01C)
826 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
827 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
828 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000829 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
830 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
831 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
832 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
833
834 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
835 MVT::isInteger(LL.getValueType())) {
836 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
837 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
838 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
839 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
840 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
841 WorkList.push_back(ORNode.Val);
842 return DAG.getSetCC(VT, ORNode, LR, Op1);
843 }
844 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
845 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
846 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
847 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
848 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
849 WorkList.push_back(ANDNode.Val);
850 return DAG.getSetCC(VT, ANDNode, LR, Op1);
851 }
852 }
853 // canonicalize equivalent to ll == rl
854 if (LL == RR && LR == RL) {
855 Op1 = ISD::getSetCCSwappedOperands(Op1);
856 std::swap(RL, RR);
857 }
858 if (LL == RL && LR == RR) {
859 bool isInteger = MVT::isInteger(LL.getValueType());
860 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
861 if (Result != ISD::SETCC_INVALID)
862 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
863 }
864 }
865 // fold (or (zext x), (zext y)) -> (zext (or x, y))
866 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
867 N1.getOpcode() == ISD::ZERO_EXTEND &&
868 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
869 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
870 N0.getOperand(0), N1.getOperand(0));
871 WorkList.push_back(ORNode.Val);
872 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
873 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000874 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000875}
876
Nate Begeman83e75ec2005-09-06 04:43:02 +0000877SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000878 SDOperand N0 = N->getOperand(0);
879 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000880 SDOperand LHS, RHS, CC;
881 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000883 MVT::ValueType VT = N0.getValueType();
884
885 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000886 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000888 // canonicalize constant to RHS
889 if (N0C && !N1C) {
890 std::swap(N0, N1);
891 std::swap(N0C, N1C);
892 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000893 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000894 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000895 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000897 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
898 bool isInt = MVT::isInteger(LHS.getValueType());
899 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
900 isInt);
901 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000902 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000903 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000905 assert(0 && "Unhandled SetCC Equivalent!");
906 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000907 }
Nate Begeman99801192005-09-07 23:25:52 +0000908 // fold !(x or y) -> (!x and !y) iff x or y are setcc
909 if (N1C && N1C->getValue() == 1 &&
910 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000911 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000912 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
913 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000914 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
915 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000916 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
917 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 }
919 }
Nate Begeman99801192005-09-07 23:25:52 +0000920 // fold !(x or y) -> (!x and !y) iff x or y are constants
921 if (N1C && N1C->isAllOnesValue() &&
922 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000923 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000924 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
925 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
927 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000928 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
929 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000930 }
931 }
Nate Begeman223df222005-09-08 20:18:10 +0000932 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
933 if (N1C && N0.getOpcode() == ISD::XOR) {
934 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
935 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
936 if (N00C)
937 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
938 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
939 if (N01C)
940 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
941 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
942 }
943 // fold (xor x, x) -> 0
944 if (N0 == N1)
945 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000946 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
947 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
948 N1.getOpcode() == ISD::ZERO_EXTEND &&
949 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
950 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
951 N0.getOperand(0), N1.getOperand(0));
952 WorkList.push_back(XORNode.Val);
953 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
954 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000956}
957
Nate Begeman83e75ec2005-09-06 04:43:02 +0000958SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959 SDOperand N0 = N->getOperand(0);
960 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000961 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
962 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000963 MVT::ValueType VT = N0.getValueType();
964 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
965
966 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000967 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000968 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000969 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000974 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000976 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000977 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
980 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000982 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 N0.getOperand(1).getOpcode() == ISD::Constant) {
984 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000985 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000987 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000990 }
991 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
992 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000993 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994 N0.getOperand(1).getOpcode() == ISD::Constant) {
995 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000996 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000997 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
998 DAG.getConstant(~0ULL << c1, VT));
999 if (c2 > c1)
1000 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00001003 return DAG.getNode(ISD::SRL, VT, Mask,
1004 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001005 }
1006 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001007 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00001008 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 DAG.getConstant(~0ULL << N1C->getValue(), VT));
1010 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011}
1012
Nate Begeman83e75ec2005-09-06 04:43:02 +00001013SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001014 SDOperand N0 = N->getOperand(0);
1015 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001016 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 MVT::ValueType VT = N0.getValueType();
1019 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1020
1021 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001022 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001023 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001024 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001025 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001026 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001028 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001029 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001030 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001031 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001032 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001034 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +00001037 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001038 return DAG.getNode(ISD::SRL, VT, N0, N1);
1039 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001040}
1041
Nate Begeman83e75ec2005-09-06 04:43:02 +00001042SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001043 SDOperand N0 = N->getOperand(0);
1044 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001045 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1046 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 MVT::ValueType VT = N0.getValueType();
1048 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
1049
1050 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001051 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001052 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001053 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001054 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001055 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001056 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001057 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001058 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001059 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001060 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001061 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001062 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001063 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1064 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001065 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001066 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001067 N0.getOperand(1).getOpcode() == ISD::Constant) {
1068 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001069 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001070 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001071 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001072 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001073 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001074 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001075 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001076}
1077
Nate Begeman83e75ec2005-09-06 04:43:02 +00001078SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001079 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001080 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001081
1082 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001083 if (N0C)
1084 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001085 N0.getValueType());
1086 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087}
1088
Nate Begeman83e75ec2005-09-06 04:43:02 +00001089SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092
1093 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001094 if (N0C)
1095 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001096 N0.getValueType());
1097 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001098}
1099
Nate Begeman83e75ec2005-09-06 04:43:02 +00001100SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001101 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001102 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103
1104 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 if (N0C)
1106 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001107 N0.getValueType());
1108 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001109}
1110
Nate Begeman452d7be2005-09-16 00:54:12 +00001111SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1112 SDOperand N0 = N->getOperand(0);
1113 SDOperand N1 = N->getOperand(1);
1114 SDOperand N2 = N->getOperand(2);
1115 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1116 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1117 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1118 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001119
Nate Begeman452d7be2005-09-16 00:54:12 +00001120 // fold select C, X, X -> X
1121 if (N1 == N2)
1122 return N1;
1123 // fold select true, X, Y -> X
1124 if (N0C && !N0C->isNullValue())
1125 return N1;
1126 // fold select false, X, Y -> Y
1127 if (N0C && N0C->isNullValue())
1128 return N2;
1129 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001130 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001131 return DAG.getNode(ISD::OR, VT, N0, N2);
1132 // fold select C, 0, X -> ~C & X
1133 // FIXME: this should check for C type == X type, not i1?
1134 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1135 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1136 WorkList.push_back(XORNode.Val);
1137 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1138 }
1139 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001140 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001141 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1142 WorkList.push_back(XORNode.Val);
1143 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1144 }
1145 // fold select C, X, 0 -> C & X
1146 // FIXME: this should check for C type == X type, not i1?
1147 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1148 return DAG.getNode(ISD::AND, VT, N0, N1);
1149 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1150 if (MVT::i1 == VT && N0 == N1)
1151 return DAG.getNode(ISD::OR, VT, N0, N2);
1152 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1153 if (MVT::i1 == VT && N0 == N2)
1154 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001155 // fold selects based on a setcc into other things, such as min/max/abs
1156 if (N0.getOpcode() == ISD::SETCC)
1157 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001158 return SDOperand();
1159}
1160
1161SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001162 SDOperand N0 = N->getOperand(0);
1163 SDOperand N1 = N->getOperand(1);
1164 SDOperand N2 = N->getOperand(2);
1165 SDOperand N3 = N->getOperand(3);
1166 SDOperand N4 = N->getOperand(4);
1167 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1168 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1169 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1170 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1171
1172 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001173 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001174 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1175
Nate Begeman44728a72005-09-19 22:34:01 +00001176 // fold select_cc lhs, rhs, x, x, cc -> x
1177 if (N2 == N3)
1178 return N2;
Nate Begeman44728a72005-09-19 22:34:01 +00001179 // fold select_cc into other things, such as min/max/abs
1180 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001181}
1182
1183SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1184 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1185 cast<CondCodeSDNode>(N->getOperand(2))->get());
1186}
1187
Nate Begeman83e75ec2005-09-06 04:43:02 +00001188SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001189 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001190 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 MVT::ValueType VT = N->getValueType(0);
1192
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001194 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001195 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 // fold (sext (sext x)) -> (sext x)
1197 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001198 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1199 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200}
1201
Nate Begeman83e75ec2005-09-06 04:43:02 +00001202SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001203 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001204 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001205 MVT::ValueType VT = N->getValueType(0);
1206
Nate Begeman1d4d4142005-09-01 00:19:25 +00001207 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001208 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210 // fold (zext (zext x)) -> (zext x)
1211 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001212 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1213 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214}
1215
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001218 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001219 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001220 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001221 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman07ed4172005-10-10 21:26:48 +00001222 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001223
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001225 if (N0C) {
1226 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001227 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001228 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001229 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001230 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001231 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001232 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001233 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001234 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1235 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1236 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001238 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001239 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1240 if (N0.getOpcode() == ISD::AssertSext &&
1241 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001242 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001243 }
1244 // fold (sext_in_reg (sextload x)) -> (sextload x)
1245 if (N0.getOpcode() == ISD::SEXTLOAD &&
1246 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001247 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001248 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001249 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250 if (N0.getOpcode() == ISD::SETCC &&
1251 TLI.getSetCCResultContents() ==
1252 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001253 return N0;
Nate Begeman07ed4172005-10-10 21:26:48 +00001254 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is zero
1255 if (MaskedValueIsZero(N0, 1ULL << (EVTBits-1), TLI))
1256 return DAG.getNode(ISD::AND, N0.getValueType(), N0,
1257 DAG.getConstant(~0ULL >> (64-EVTBits), VT));
1258 // fold (sext_in_reg (srl x)) -> sra x
1259 if (N0.getOpcode() == ISD::SRL &&
1260 N0.getOperand(1).getOpcode() == ISD::Constant &&
1261 cast<ConstantSDNode>(N0.getOperand(1))->getValue() == EVTBits) {
1262 return DAG.getNode(ISD::SRA, N0.getValueType(), N0.getOperand(0),
1263 N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001265 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001266}
1267
Nate Begeman83e75ec2005-09-06 04:43:02 +00001268SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001269 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001270 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001271 MVT::ValueType VT = N->getValueType(0);
1272
1273 // noop truncate
1274 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001275 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001276 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001277 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001278 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001279 // fold (truncate (truncate x)) -> (truncate x)
1280 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001281 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001282 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1283 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1284 if (N0.getValueType() < VT)
1285 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001286 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001287 else if (N0.getValueType() > VT)
1288 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001289 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001290 else
1291 // if the source and dest are the same type, we can drop both the extend
1292 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001293 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001294 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001295 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001296}
1297
Chris Lattner01b3d732005-09-28 22:28:18 +00001298SDOperand DAGCombiner::visitFADD(SDNode *N) {
1299 SDOperand N0 = N->getOperand(0);
1300 SDOperand N1 = N->getOperand(1);
1301 MVT::ValueType VT = N->getValueType(0);
1302
1303 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1304 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1305 // fold floating point (fadd c1, c2)
1306 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1307 N->getValueType(0));
1308 }
1309 // fold (A + (-B)) -> A-B
1310 if (N1.getOpcode() == ISD::FNEG)
1311 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1312
1313 // fold ((-A) + B) -> B-A
1314 if (N0.getOpcode() == ISD::FNEG)
1315 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1316
1317 return SDOperand();
1318}
1319
1320SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1321 SDOperand N0 = N->getOperand(0);
1322 SDOperand N1 = N->getOperand(1);
1323 MVT::ValueType VT = N->getValueType(0);
1324
1325 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1326 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1327 // fold floating point (fsub c1, c2)
1328 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1329 N->getValueType(0));
1330 }
1331 // fold (A-(-B)) -> A+B
1332 if (N1.getOpcode() == ISD::FNEG)
1333 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1334
1335 return SDOperand();
1336}
1337
1338SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1339 SDOperand N0 = N->getOperand(0);
1340 SDOperand N1 = N->getOperand(1);
1341 MVT::ValueType VT = N->getValueType(0);
1342
1343 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1344 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1345 // fold floating point (fmul c1, c2)
1346 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1347 N->getValueType(0));
1348 }
1349 return SDOperand();
1350}
1351
1352SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1353 SDOperand N0 = N->getOperand(0);
1354 SDOperand N1 = N->getOperand(1);
1355 MVT::ValueType VT = N->getValueType(0);
1356
1357 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1358 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1359 // fold floating point (fdiv c1, c2)
1360 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1361 N->getValueType(0));
1362 }
1363 return SDOperand();
1364}
1365
1366SDOperand DAGCombiner::visitFREM(SDNode *N) {
1367 SDOperand N0 = N->getOperand(0);
1368 SDOperand N1 = N->getOperand(1);
1369 MVT::ValueType VT = N->getValueType(0);
1370
1371 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1372 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1373 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1374 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1375 N->getValueType(0));
1376 }
1377 return SDOperand();
1378}
1379
1380
Nate Begeman83e75ec2005-09-06 04:43:02 +00001381SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384
1385 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001386 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001387 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1388 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389}
1390
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001394
1395 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001396 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001397 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1398 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001399}
1400
Nate Begeman83e75ec2005-09-06 04:43:02 +00001401SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001402 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403
1404 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001405 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408}
1409
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001411 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412
1413 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001414 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001415 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1416 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001417}
1418
Nate Begeman83e75ec2005-09-06 04:43:02 +00001419SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001420 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001421
1422 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001423 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001424 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1425 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001426}
1427
Nate Begeman83e75ec2005-09-06 04:43:02 +00001428SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429 SDOperand N0 = N->getOperand(0);
1430 MVT::ValueType VT = N->getValueType(0);
1431 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001432 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001433
Nate Begeman1d4d4142005-09-01 00:19:25 +00001434 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001435 if (N0CFP) {
1436 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001439 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440}
1441
Nate Begeman83e75ec2005-09-06 04:43:02 +00001442SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001443 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001444
1445 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001446 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001447 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1448 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001449}
1450
Nate Begeman83e75ec2005-09-06 04:43:02 +00001451SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001452 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001454 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 // fold (neg (sub x, y)) -> (sub y, x)
1457 if (N->getOperand(0).getOpcode() == ISD::SUB)
1458 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001459 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460 // fold (neg (neg x)) -> x
1461 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 return N->getOperand(0).getOperand(0);
1463 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464}
1465
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001467 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001469 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001470 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001471 // fold (fabs (fabs x)) -> (fabs x)
1472 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474 // fold (fabs (fneg x)) -> (fabs x)
1475 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1476 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001477 N->getOperand(0).getOperand(0));
1478 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001479}
1480
Nate Begeman44728a72005-09-19 22:34:01 +00001481SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1482 SDOperand Chain = N->getOperand(0);
1483 SDOperand N1 = N->getOperand(1);
1484 SDOperand N2 = N->getOperand(2);
1485 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1486
1487 // never taken branch, fold to chain
1488 if (N1C && N1C->isNullValue())
1489 return Chain;
1490 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001491 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001492 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1493 return SDOperand();
1494}
1495
1496SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1497 SDOperand Chain = N->getOperand(0);
1498 SDOperand N1 = N->getOperand(1);
1499 SDOperand N2 = N->getOperand(2);
1500 SDOperand N3 = N->getOperand(3);
1501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1502
1503 // unconditional branch to true mbb
1504 if (N1C && N1C->getValue() == 1)
1505 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1506 // unconditional branch to false mbb
1507 if (N1C && N1C->isNullValue())
1508 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1509 return SDOperand();
1510}
1511
Chris Lattner3ea0b472005-10-05 06:47:48 +00001512// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1513//
Nate Begeman44728a72005-09-19 22:34:01 +00001514SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001515 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1516 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1517
1518 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001519 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1520 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1521
1522 // fold br_cc true, dest -> br dest (unconditional branch)
1523 if (SCCC && SCCC->getValue())
1524 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1525 N->getOperand(4));
1526 // fold br_cc false, dest -> unconditional fall through
1527 if (SCCC && SCCC->isNullValue())
1528 return N->getOperand(0);
1529 // fold to a simpler setcc
1530 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1531 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1532 Simp.getOperand(2), Simp.getOperand(0),
1533 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001534 return SDOperand();
1535}
1536
1537SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001538 SDOperand Chain = N->getOperand(0);
1539 SDOperand CCN = N->getOperand(1);
1540 SDOperand LHS = N->getOperand(2);
1541 SDOperand RHS = N->getOperand(3);
1542 SDOperand N4 = N->getOperand(4);
1543 SDOperand N5 = N->getOperand(5);
1544
1545 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1546 cast<CondCodeSDNode>(CCN)->get(), false);
1547 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1548
1549 // fold select_cc lhs, rhs, x, x, cc -> x
1550 if (N4 == N5)
1551 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1552 // fold select_cc true, x, y -> x
1553 if (SCCC && SCCC->getValue())
1554 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1555 // fold select_cc false, x, y -> y
1556 if (SCCC && SCCC->isNullValue())
1557 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1558 // fold to a simpler setcc
1559 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1560 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1561 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001562 return SDOperand();
1563}
1564
Chris Lattner01a22022005-10-10 22:04:48 +00001565SDOperand DAGCombiner::visitLOAD(SDNode *N) {
1566 SDOperand Chain = N->getOperand(0);
1567 SDOperand Ptr = N->getOperand(1);
1568 SDOperand SrcValue = N->getOperand(2);
1569
1570 // If this load is directly stored, replace the load value with the stored
1571 // value.
1572 // TODO: Handle store large -> read small portion.
1573 // TODO: Handle TRUNCSTORE/EXTLOAD
1574 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1575 Chain.getOperand(1).getValueType() == N->getValueType(0))
1576 return CombineTo(N, Chain.getOperand(1), Chain);
1577
1578 return SDOperand();
1579}
1580
Chris Lattner87514ca2005-10-10 22:31:19 +00001581SDOperand DAGCombiner::visitSTORE(SDNode *N) {
1582 SDOperand Chain = N->getOperand(0);
1583 SDOperand Value = N->getOperand(1);
1584 SDOperand Ptr = N->getOperand(2);
1585 SDOperand SrcValue = N->getOperand(3);
1586
1587 // If this is a store that kills a previous store, remove the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001588 if (Chain.getOpcode() == ISD::STORE && Chain.getOperand(2) == Ptr &&
1589 Chain.Val->hasOneUse() /* Avoid introducing DAG cycles */) {
Chris Lattner87514ca2005-10-10 22:31:19 +00001590 // Create a new store of Value that replaces both stores.
1591 SDNode *PrevStore = Chain.Val;
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001592 if (PrevStore->getOperand(1) == Value) // Same value multiply stored.
1593 return Chain;
Chris Lattner87514ca2005-10-10 22:31:19 +00001594 SDOperand NewStore = DAG.getNode(ISD::STORE, MVT::Other,
1595 PrevStore->getOperand(0), Value, Ptr,
1596 SrcValue);
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001597 CombineTo(N, NewStore); // Nuke this store.
Chris Lattner87514ca2005-10-10 22:31:19 +00001598 CombineTo(PrevStore, NewStore); // Nuke the previous store.
Chris Lattner04ecf6d2005-10-10 23:00:08 +00001599 return SDOperand(N, 0);
Chris Lattner87514ca2005-10-10 22:31:19 +00001600 }
1601
1602 return SDOperand();
1603}
1604
Nate Begeman44728a72005-09-19 22:34:01 +00001605SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00001606 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
1607
1608 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
1609 cast<CondCodeSDNode>(N0.getOperand(2))->get());
1610 // If we got a simplified select_cc node back from SimplifySelectCC, then
1611 // break it down into a new SETCC node, and a new SELECT node, and then return
1612 // the SELECT node, since we were called with a SELECT node.
1613 if (SCC.Val) {
1614 // Check to see if we got a select_cc back (to turn into setcc/select).
1615 // Otherwise, just return whatever node we got back, like fabs.
1616 if (SCC.getOpcode() == ISD::SELECT_CC) {
1617 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
1618 SCC.getOperand(0), SCC.getOperand(1),
1619 SCC.getOperand(4));
1620 WorkList.push_back(SETCC.Val);
1621 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
1622 SCC.getOperand(3), SETCC);
1623 }
1624 return SCC;
1625 }
Nate Begeman44728a72005-09-19 22:34:01 +00001626 return SDOperand();
1627}
1628
1629SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1630 SDOperand N2, SDOperand N3,
1631 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001632
1633 MVT::ValueType VT = N2.getValueType();
1634 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1635 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1636 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1637 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1638
1639 // Determine if the condition we're dealing with is constant
1640 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1641 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1642
1643 // fold select_cc true, x, y -> x
1644 if (SCCC && SCCC->getValue())
1645 return N2;
1646 // fold select_cc false, x, y -> y
1647 if (SCCC && SCCC->getValue() == 0)
1648 return N3;
1649
1650 // Check to see if we can simplify the select into an fabs node
1651 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1652 // Allow either -0.0 or 0.0
1653 if (CFP->getValue() == 0.0) {
1654 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1655 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1656 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1657 N2 == N3.getOperand(0))
1658 return DAG.getNode(ISD::FABS, VT, N0);
1659
1660 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1661 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1662 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1663 N2.getOperand(0) == N3)
1664 return DAG.getNode(ISD::FABS, VT, N3);
1665 }
1666 }
1667
1668 // Check to see if we can perform the "gzip trick", transforming
1669 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1670 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1671 MVT::isInteger(N0.getValueType()) &&
1672 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1673 MVT::ValueType XType = N0.getValueType();
1674 MVT::ValueType AType = N2.getValueType();
1675 if (XType >= AType) {
1676 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00001677 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00001678 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1679 unsigned ShCtV = Log2_64(N2C->getValue());
1680 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1681 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1682 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1683 WorkList.push_back(Shift.Val);
1684 if (XType > AType) {
1685 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1686 WorkList.push_back(Shift.Val);
1687 }
1688 return DAG.getNode(ISD::AND, AType, Shift, N2);
1689 }
1690 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1691 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1692 TLI.getShiftAmountTy()));
1693 WorkList.push_back(Shift.Val);
1694 if (XType > AType) {
1695 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1696 WorkList.push_back(Shift.Val);
1697 }
1698 return DAG.getNode(ISD::AND, AType, Shift, N2);
1699 }
1700 }
Nate Begeman07ed4172005-10-10 21:26:48 +00001701
1702 // fold select C, 16, 0 -> shl C, 4
1703 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
1704 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
1705 // Get a SetCC of the condition
1706 // FIXME: Should probably make sure that setcc is legal if we ever have a
1707 // target where it isn't.
1708 SDOperand Temp, SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1709 WorkList.push_back(SCC.Val);
1710 // cast from setcc result type to select result type
1711 if (AfterLegalize)
1712 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
1713 else
1714 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
1715 WorkList.push_back(Temp.Val);
1716 // shl setcc result by log2 n2c
1717 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
1718 DAG.getConstant(Log2_64(N2C->getValue()),
1719 TLI.getShiftAmountTy()));
1720 }
1721
Nate Begemanf845b452005-10-08 00:29:44 +00001722 // Check to see if this is the equivalent of setcc
1723 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1724 // otherwise, go ahead with the folds.
1725 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1726 MVT::ValueType XType = N0.getValueType();
1727 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1728 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1729 if (Res.getValueType() != VT)
1730 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1731 return Res;
1732 }
1733
1734 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1735 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1736 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1737 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1738 return DAG.getNode(ISD::SRL, XType, Ctlz,
1739 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1740 TLI.getShiftAmountTy()));
1741 }
1742 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1743 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1744 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1745 N0);
1746 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1747 DAG.getConstant(~0ULL, XType));
1748 return DAG.getNode(ISD::SRL, XType,
1749 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1750 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1751 TLI.getShiftAmountTy()));
1752 }
1753 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1754 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1755 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1756 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1757 TLI.getShiftAmountTy()));
1758 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1759 }
1760 }
1761
1762 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1763 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1764 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1765 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1766 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1767 MVT::ValueType XType = N0.getValueType();
1768 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1769 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1770 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1771 TLI.getShiftAmountTy()));
1772 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
1773 WorkList.push_back(Shift.Val);
1774 WorkList.push_back(Add.Val);
1775 return DAG.getNode(ISD::XOR, XType, Add, Shift);
1776 }
1777 }
1778 }
1779
Nate Begeman44728a72005-09-19 22:34:01 +00001780 return SDOperand();
1781}
1782
Nate Begeman452d7be2005-09-16 00:54:12 +00001783SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001784 SDOperand N1, ISD::CondCode Cond,
1785 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001786 // These setcc operations always fold.
1787 switch (Cond) {
1788 default: break;
1789 case ISD::SETFALSE:
1790 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1791 case ISD::SETTRUE:
1792 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1793 }
1794
1795 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1796 uint64_t C1 = N1C->getValue();
1797 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1798 uint64_t C0 = N0C->getValue();
1799
1800 // Sign extend the operands if required
1801 if (ISD::isSignedIntSetCC(Cond)) {
1802 C0 = N0C->getSignExtended();
1803 C1 = N1C->getSignExtended();
1804 }
1805
1806 switch (Cond) {
1807 default: assert(0 && "Unknown integer setcc!");
1808 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1809 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1810 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1811 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1812 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1813 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1814 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1815 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1816 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1817 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1818 }
1819 } else {
1820 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1821 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1822 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1823
1824 // If the comparison constant has bits in the upper part, the
1825 // zero-extended value could never match.
1826 if (C1 & (~0ULL << InSize)) {
1827 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1828 switch (Cond) {
1829 case ISD::SETUGT:
1830 case ISD::SETUGE:
1831 case ISD::SETEQ: return DAG.getConstant(0, VT);
1832 case ISD::SETULT:
1833 case ISD::SETULE:
1834 case ISD::SETNE: return DAG.getConstant(1, VT);
1835 case ISD::SETGT:
1836 case ISD::SETGE:
1837 // True if the sign bit of C1 is set.
1838 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1839 case ISD::SETLT:
1840 case ISD::SETLE:
1841 // True if the sign bit of C1 isn't set.
1842 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1843 default:
1844 break;
1845 }
1846 }
1847
1848 // Otherwise, we can perform the comparison with the low bits.
1849 switch (Cond) {
1850 case ISD::SETEQ:
1851 case ISD::SETNE:
1852 case ISD::SETUGT:
1853 case ISD::SETUGE:
1854 case ISD::SETULT:
1855 case ISD::SETULE:
1856 return DAG.getSetCC(VT, N0.getOperand(0),
1857 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1858 Cond);
1859 default:
1860 break; // todo, be more careful with signed comparisons
1861 }
1862 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1863 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1864 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1865 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1866 MVT::ValueType ExtDstTy = N0.getValueType();
1867 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1868
1869 // If the extended part has any inconsistent bits, it cannot ever
1870 // compare equal. In other words, they have to be all ones or all
1871 // zeros.
1872 uint64_t ExtBits =
1873 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1874 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1875 return DAG.getConstant(Cond == ISD::SETNE, VT);
1876
1877 SDOperand ZextOp;
1878 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1879 if (Op0Ty == ExtSrcTy) {
1880 ZextOp = N0.getOperand(0);
1881 } else {
1882 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1883 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1884 DAG.getConstant(Imm, Op0Ty));
1885 }
1886 WorkList.push_back(ZextOp.Val);
1887 // Otherwise, make this a use of a zext.
1888 return DAG.getSetCC(VT, ZextOp,
1889 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1890 ExtDstTy),
1891 Cond);
1892 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001893
Nate Begeman452d7be2005-09-16 00:54:12 +00001894 uint64_t MinVal, MaxVal;
1895 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1896 if (ISD::isSignedIntSetCC(Cond)) {
1897 MinVal = 1ULL << (OperandBitSize-1);
1898 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1899 MaxVal = ~0ULL >> (65-OperandBitSize);
1900 else
1901 MaxVal = 0;
1902 } else {
1903 MinVal = 0;
1904 MaxVal = ~0ULL >> (64-OperandBitSize);
1905 }
1906
1907 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1908 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1909 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1910 --C1; // X >= C0 --> X > (C0-1)
1911 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1912 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1913 }
1914
1915 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1916 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1917 ++C1; // X <= C0 --> X < (C0+1)
1918 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1919 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1920 }
1921
1922 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1923 return DAG.getConstant(0, VT); // X < MIN --> false
1924
1925 // Canonicalize setgt X, Min --> setne X, Min
1926 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1927 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1928
1929 // If we have setult X, 1, turn it into seteq X, 0
1930 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1931 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1932 ISD::SETEQ);
1933 // If we have setugt X, Max-1, turn it into seteq X, Max
1934 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1935 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1936 ISD::SETEQ);
1937
1938 // If we have "setcc X, C0", check to see if we can shrink the immediate
1939 // by changing cc.
1940
1941 // SETUGT X, SINTMAX -> SETLT X, 0
1942 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1943 C1 == (~0ULL >> (65-OperandBitSize)))
1944 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1945 ISD::SETLT);
1946
1947 // FIXME: Implement the rest of these.
1948
1949 // Fold bit comparisons when we can.
1950 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1951 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1952 if (ConstantSDNode *AndRHS =
1953 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1954 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1955 // Perform the xform if the AND RHS is a single bit.
1956 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1957 return DAG.getNode(ISD::SRL, VT, N0,
1958 DAG.getConstant(Log2_64(AndRHS->getValue()),
1959 TLI.getShiftAmountTy()));
1960 }
1961 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1962 // (X & 8) == 8 --> (X & 8) >> 3
1963 // Perform the xform if C1 is a single bit.
1964 if ((C1 & (C1-1)) == 0) {
1965 return DAG.getNode(ISD::SRL, VT, N0,
1966 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1967 }
1968 }
1969 }
1970 }
1971 } else if (isa<ConstantSDNode>(N0.Val)) {
1972 // Ensure that the constant occurs on the RHS.
1973 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1974 }
1975
1976 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1977 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1978 double C0 = N0C->getValue(), C1 = N1C->getValue();
1979
1980 switch (Cond) {
1981 default: break; // FIXME: Implement the rest of these!
1982 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1983 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1984 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1985 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1986 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1987 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1988 }
1989 } else {
1990 // Ensure that the constant occurs on the RHS.
1991 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1992 }
1993
1994 if (N0 == N1) {
1995 // We can always fold X == Y for integer setcc's.
1996 if (MVT::isInteger(N0.getValueType()))
1997 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1998 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1999 if (UOF == 2) // FP operators that are undefined on NaNs.
2000 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
2001 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
2002 return DAG.getConstant(UOF, VT);
2003 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
2004 // if it is not already.
2005 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
2006 if (NewCond != Cond)
2007 return DAG.getSetCC(VT, N0, N1, NewCond);
2008 }
2009
2010 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
2011 MVT::isInteger(N0.getValueType())) {
2012 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
2013 N0.getOpcode() == ISD::XOR) {
2014 // Simplify (X+Y) == (X+Z) --> Y == Z
2015 if (N0.getOpcode() == N1.getOpcode()) {
2016 if (N0.getOperand(0) == N1.getOperand(0))
2017 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2018 if (N0.getOperand(1) == N1.getOperand(1))
2019 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
2020 if (isCommutativeBinOp(N0.getOpcode())) {
2021 // If X op Y == Y op X, try other combinations.
2022 if (N0.getOperand(0) == N1.getOperand(1))
2023 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
2024 if (N0.getOperand(1) == N1.getOperand(0))
2025 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
2026 }
2027 }
2028
Chris Lattner5c46f742005-10-05 06:11:08 +00002029 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
2030 if (N0.getOpcode() == ISD::XOR)
2031 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
2032 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
2033 // If we know that all of the inverted bits are zero, don't bother
2034 // performing the inversion.
2035 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
2036 return DAG.getSetCC(VT, N0.getOperand(0),
2037 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
2038 N0.getValueType()), Cond);
2039 }
2040
Nate Begeman452d7be2005-09-16 00:54:12 +00002041 // Simplify (X+Z) == X --> Z == 0
2042 if (N0.getOperand(0) == N1)
2043 return DAG.getSetCC(VT, N0.getOperand(1),
2044 DAG.getConstant(0, N0.getValueType()), Cond);
2045 if (N0.getOperand(1) == N1) {
2046 if (isCommutativeBinOp(N0.getOpcode()))
2047 return DAG.getSetCC(VT, N0.getOperand(0),
2048 DAG.getConstant(0, N0.getValueType()), Cond);
2049 else {
2050 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
2051 // (Z-X) == X --> Z == X<<1
2052 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
2053 N1,
2054 DAG.getConstant(1,TLI.getShiftAmountTy()));
2055 WorkList.push_back(SH.Val);
2056 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
2057 }
2058 }
2059 }
2060
2061 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
2062 N1.getOpcode() == ISD::XOR) {
2063 // Simplify X == (X+Z) --> Z == 0
2064 if (N1.getOperand(0) == N0) {
2065 return DAG.getSetCC(VT, N1.getOperand(1),
2066 DAG.getConstant(0, N1.getValueType()), Cond);
2067 } else if (N1.getOperand(1) == N0) {
2068 if (isCommutativeBinOp(N1.getOpcode())) {
2069 return DAG.getSetCC(VT, N1.getOperand(0),
2070 DAG.getConstant(0, N1.getValueType()), Cond);
2071 } else {
2072 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
2073 // X == (Z-X) --> X<<1 == Z
2074 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
2075 DAG.getConstant(1,TLI.getShiftAmountTy()));
2076 WorkList.push_back(SH.Val);
2077 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
2078 }
2079 }
2080 }
2081 }
2082
2083 // Fold away ALL boolean setcc's.
2084 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00002085 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002086 switch (Cond) {
2087 default: assert(0 && "Unknown integer setcc!");
2088 case ISD::SETEQ: // X == Y -> (X^Y)^1
2089 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2090 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
2091 WorkList.push_back(Temp.Val);
2092 break;
2093 case ISD::SETNE: // X != Y --> (X^Y)
2094 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
2095 break;
2096 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
2097 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
2098 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2099 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
2100 WorkList.push_back(Temp.Val);
2101 break;
2102 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
2103 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
2104 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2105 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
2106 WorkList.push_back(Temp.Val);
2107 break;
2108 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
2109 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
2110 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2111 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2112 WorkList.push_back(Temp.Val);
2113 break;
2114 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2115 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2116 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2117 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2118 break;
2119 }
2120 if (VT != MVT::i1) {
2121 WorkList.push_back(N0.Val);
2122 // FIXME: If running after legalize, we probably can't do this.
2123 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2124 }
2125 return N0;
2126 }
2127
2128 // Could not fold it.
2129 return SDOperand();
2130}
2131
Nate Begeman1d4d4142005-09-01 00:19:25 +00002132// SelectionDAG::Combine - This is the entry point for the file.
2133//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002134void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002135 /// run - This is the main entry point to this class.
2136 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002137 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002138}