blob: 3c5219181bfad4c4c9c3371d5b1c48c6c0c3227f [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, 16, 0 -> shr C, 4
24// FIXME: select C, pow2, pow2 -> something smart
25// FIXME: trunc(select X, Y, Z) -> select X, trunc(Y), trunc(Z)
26// FIXME: (select C, load A, load B) -> load (select C, A, B)
27// FIXME: store -> load -> forward substitute
28// FIXME: Dead stores -> nuke
29// FIXME: shr X, (and Y,31) -> shr X, Y
30// FIXME: TRUNC (LOAD) -> EXT_LOAD/LOAD(smaller)
Nate Begeman1d4d4142005-09-01 00:19:25 +000031// FIXME: mul (x, const) -> shifts + adds
Nate Begeman1d4d4142005-09-01 00:19:25 +000032// FIXME: undef values
Nate Begeman1d4d4142005-09-01 00:19:25 +000033// FIXME: zero extend when top bits are 0 -> drop it ?
Nate Begeman4ebd8052005-09-01 23:24:04 +000034// FIXME: make truncate see through SIGN_EXTEND and AND
35// FIXME: sext_in_reg(setcc) on targets that return zero or one, and where
36// EVT != MVT::i1 can drop the sext.
Nate Begeman4ebd8052005-09-01 23:24:04 +000037// FIXME: (sra (sra x, c1), c2) -> (sra x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +000038// FIXME: verify that getNode can't return extends with an operand whose type
39// is >= to that of the extend.
40// FIXME: divide by zero is currently left unfolded. do we want to turn this
41// into an undef?
Nate Begeman1d4d4142005-09-01 00:19:25 +000042//
43//===----------------------------------------------------------------------===//
44
45#define DEBUG_TYPE "dagcombine"
46#include "llvm/ADT/Statistic.h"
47#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000048#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000049#include "llvm/Support/MathExtras.h"
50#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000051#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000052#include <cmath>
53using namespace llvm;
54
55namespace {
56 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
57
58 class DAGCombiner {
59 SelectionDAG &DAG;
60 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000061 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000062
63 // Worklist of all of the nodes that need to be simplified.
64 std::vector<SDNode*> WorkList;
65
66 /// AddUsersToWorkList - When an instruction is simplified, add all users of
67 /// the instruction to the work lists because they might get more simplified
68 /// now.
69 ///
70 void AddUsersToWorkList(SDNode *N) {
71 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000072 UI != UE; ++UI)
73 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 }
75
76 /// removeFromWorkList - remove all instances of N from the worklist.
77 void removeFromWorkList(SDNode *N) {
78 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
79 WorkList.end());
80 }
81
82 /// visit - call the node-specific routine that knows how to fold each
83 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000084 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
86 // Visitation implementation - Implement dag node combining for different
87 // node types. The semantics are as follows:
88 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000089 // SDOperand.Val == 0 - No change was made
90 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000091 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000092 SDOperand visitTokenFactor(SDNode *N);
93 SDOperand visitADD(SDNode *N);
94 SDOperand visitSUB(SDNode *N);
95 SDOperand visitMUL(SDNode *N);
96 SDOperand visitSDIV(SDNode *N);
97 SDOperand visitUDIV(SDNode *N);
98 SDOperand visitSREM(SDNode *N);
99 SDOperand visitUREM(SDNode *N);
100 SDOperand visitMULHU(SDNode *N);
101 SDOperand visitMULHS(SDNode *N);
102 SDOperand visitAND(SDNode *N);
103 SDOperand visitOR(SDNode *N);
104 SDOperand visitXOR(SDNode *N);
105 SDOperand visitSHL(SDNode *N);
106 SDOperand visitSRA(SDNode *N);
107 SDOperand visitSRL(SDNode *N);
108 SDOperand visitCTLZ(SDNode *N);
109 SDOperand visitCTTZ(SDNode *N);
110 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000111 SDOperand visitSELECT(SDNode *N);
112 SDOperand visitSELECT_CC(SDNode *N);
113 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000114 SDOperand visitSIGN_EXTEND(SDNode *N);
115 SDOperand visitZERO_EXTEND(SDNode *N);
116 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
117 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000118
119 SDOperand visitFADD(SDNode *N);
120 SDOperand visitFSUB(SDNode *N);
121 SDOperand visitFMUL(SDNode *N);
122 SDOperand visitFDIV(SDNode *N);
123 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000124 SDOperand visitSINT_TO_FP(SDNode *N);
125 SDOperand visitUINT_TO_FP(SDNode *N);
126 SDOperand visitFP_TO_SINT(SDNode *N);
127 SDOperand visitFP_TO_UINT(SDNode *N);
128 SDOperand visitFP_ROUND(SDNode *N);
129 SDOperand visitFP_ROUND_INREG(SDNode *N);
130 SDOperand visitFP_EXTEND(SDNode *N);
131 SDOperand visitFNEG(SDNode *N);
132 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000133 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000134 SDOperand visitBRCONDTWOWAY(SDNode *N);
135 SDOperand visitBR_CC(SDNode *N);
136 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000137
Nate Begeman44728a72005-09-19 22:34:01 +0000138 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
139 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
140 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000141 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000142 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143public:
144 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000145 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000146
147 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000148 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000149 };
150}
151
152/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
153/// this predicate to simplify operations downstream. V and Mask are known to
154/// be the same type.
155static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
156 const TargetLowering &TLI) {
157 unsigned SrcBits;
158 if (Mask == 0) return true;
159
160 // If we know the result of a setcc has the top bits zero, use this info.
161 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000162 case ISD::Constant:
163 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
164 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000165 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000166 return ((Mask & 1) == 0) &&
167 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
168 case ISD::ZEXTLOAD:
169 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
170 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
171 case ISD::ZERO_EXTEND:
172 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
173 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
174 case ISD::AssertZext:
175 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
176 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
177 case ISD::AND:
178 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
179 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
180 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
181 // FALL THROUGH
182 case ISD::OR:
183 case ISD::XOR:
184 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
185 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
186 case ISD::SELECT:
187 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
188 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
189 case ISD::SELECT_CC:
190 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
191 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
192 case ISD::SRL:
193 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
194 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
195 uint64_t NewVal = Mask << ShAmt->getValue();
196 SrcBits = MVT::getSizeInBits(Op.getValueType());
197 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
198 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
199 }
200 return false;
201 case ISD::SHL:
202 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
203 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
204 uint64_t NewVal = Mask >> ShAmt->getValue();
205 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
206 }
207 return false;
208 case ISD::CTTZ:
209 case ISD::CTLZ:
210 case ISD::CTPOP:
211 // Bit counting instructions can not set the high bits of the result
212 // register. The max number of bits sets depends on the input.
213 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
214
215 // TODO we could handle some SRA cases here.
216 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000217 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000218 return false;
219}
220
Nate Begeman4ebd8052005-09-01 23:24:04 +0000221// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
222// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000223// Also, set the incoming LHS, RHS, and CC references to the appropriate
224// nodes based on the type of node we are checking. This simplifies life a
225// bit for the callers.
226static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
227 SDOperand &CC) {
228 if (N.getOpcode() == ISD::SETCC) {
229 LHS = N.getOperand(0);
230 RHS = N.getOperand(1);
231 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000232 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000233 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000234 if (N.getOpcode() == ISD::SELECT_CC &&
235 N.getOperand(2).getOpcode() == ISD::Constant &&
236 N.getOperand(3).getOpcode() == ISD::Constant &&
237 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000238 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
239 LHS = N.getOperand(0);
240 RHS = N.getOperand(1);
241 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000243 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000244 return false;
245}
246
Nate Begeman99801192005-09-07 23:25:52 +0000247// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
248// one use. If this is true, it allows the users to invert the operation for
249// free when it is profitable to do so.
250static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000251 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000252 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000253 return true;
254 return false;
255}
256
Nate Begeman452d7be2005-09-16 00:54:12 +0000257// FIXME: This should probably go in the ISD class rather than being duplicated
258// in several files.
259static bool isCommutativeBinOp(unsigned Opcode) {
260 switch (Opcode) {
261 case ISD::ADD:
262 case ISD::MUL:
263 case ISD::AND:
264 case ISD::OR:
265 case ISD::XOR: return true;
266 default: return false; // FIXME: Need commutative info for user ops!
267 }
268}
269
Nate Begeman4ebd8052005-09-01 23:24:04 +0000270void DAGCombiner::Run(bool RunningAfterLegalize) {
271 // set the instance variable, so that the various visit routines may use it.
272 AfterLegalize = RunningAfterLegalize;
273
Nate Begeman646d7e22005-09-02 21:18:40 +0000274 // Add all the dag nodes to the worklist.
275 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000276
Chris Lattner95038592005-10-05 06:35:28 +0000277 // Create a dummy node (which is not added to allnodes), that adds a reference
278 // to the root node, preventing it from being deleted, and tracking any
279 // changes of the root.
280 HandleSDNode Dummy(DAG.getRoot());
281
Nate Begeman1d4d4142005-09-01 00:19:25 +0000282 // while the worklist isn't empty, inspect the node on the end of it and
283 // try and combine it.
284 while (!WorkList.empty()) {
285 SDNode *N = WorkList.back();
286 WorkList.pop_back();
287
288 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000289 // N is deleted from the DAG, since they too may now be dead or may have a
290 // reduced number of uses, allowing other xforms.
291 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000292 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
293 WorkList.push_back(N->getOperand(i).Val);
294
Nate Begeman1d4d4142005-09-01 00:19:25 +0000295 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000296 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000297 continue;
298 }
299
Nate Begeman83e75ec2005-09-06 04:43:02 +0000300 SDOperand RV = visit(N);
301 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000302 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000303 // If we get back the same node we passed in, rather than a new node or
304 // zero, we know that the node must have defined multiple values and
305 // CombineTo was used. Since CombineTo takes care of the worklist
306 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000307 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000308 DEBUG(std::cerr << "\nReplacing "; N->dump();
309 std::cerr << "\nWith: "; RV.Val->dump();
310 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000311 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000312
313 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000314 WorkList.push_back(RV.Val);
315 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000316
317 // Nodes can end up on the worklist more than once. Make sure we do
318 // not process a node that has been replaced.
319 removeFromWorkList(N);
Chris Lattner5c46f742005-10-05 06:11:08 +0000320
321 // Finally, since the node is now dead, remove it from the graph.
322 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000323 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000324 }
325 }
Chris Lattner95038592005-10-05 06:35:28 +0000326
327 // If the root changed (e.g. it was a dead load, update the root).
328 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000329}
330
Nate Begeman83e75ec2005-09-06 04:43:02 +0000331SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000332 switch(N->getOpcode()) {
333 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000334 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000335 case ISD::ADD: return visitADD(N);
336 case ISD::SUB: return visitSUB(N);
337 case ISD::MUL: return visitMUL(N);
338 case ISD::SDIV: return visitSDIV(N);
339 case ISD::UDIV: return visitUDIV(N);
340 case ISD::SREM: return visitSREM(N);
341 case ISD::UREM: return visitUREM(N);
342 case ISD::MULHU: return visitMULHU(N);
343 case ISD::MULHS: return visitMULHS(N);
344 case ISD::AND: return visitAND(N);
345 case ISD::OR: return visitOR(N);
346 case ISD::XOR: return visitXOR(N);
347 case ISD::SHL: return visitSHL(N);
348 case ISD::SRA: return visitSRA(N);
349 case ISD::SRL: return visitSRL(N);
350 case ISD::CTLZ: return visitCTLZ(N);
351 case ISD::CTTZ: return visitCTTZ(N);
352 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000353 case ISD::SELECT: return visitSELECT(N);
354 case ISD::SELECT_CC: return visitSELECT_CC(N);
355 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000356 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
357 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
358 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
359 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000360 case ISD::FADD: return visitFADD(N);
361 case ISD::FSUB: return visitFSUB(N);
362 case ISD::FMUL: return visitFMUL(N);
363 case ISD::FDIV: return visitFDIV(N);
364 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000365 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
366 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
367 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
368 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
369 case ISD::FP_ROUND: return visitFP_ROUND(N);
370 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
371 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
372 case ISD::FNEG: return visitFNEG(N);
373 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000374 case ISD::BRCOND: return visitBRCOND(N);
375 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
376 case ISD::BR_CC: return visitBR_CC(N);
377 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000378 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000379 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000380}
381
Nate Begeman83e75ec2005-09-06 04:43:02 +0000382SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000383 // If the token factor has two operands and one is the entry token, replace
384 // the token factor with the other operand.
385 if (N->getNumOperands() == 2) {
386 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000387 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000388 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000389 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000390 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000391 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000392}
393
Nate Begeman83e75ec2005-09-06 04:43:02 +0000394SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000395 SDOperand N0 = N->getOperand(0);
396 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000397 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
398 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000399 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000400
401 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000402 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000403 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000404 // canonicalize constant to RHS
405 if (N0C && !N1C) {
406 std::swap(N0, N1);
407 std::swap(N0C, N1C);
408 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000409 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000410 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000411 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000412 // fold (add (add x, c1), c2) -> (add x, c1+c2)
413 if (N1C && N0.getOpcode() == ISD::ADD) {
414 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
415 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
416 if (N00C)
417 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
418 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
419 if (N01C)
420 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
421 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
422 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000423 // fold ((0-A) + B) -> B-A
424 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
425 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000426 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000427 // fold (A + (0-B)) -> A-B
428 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
429 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000430 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000431 // fold (A+(B-A)) -> B
432 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000433 return N1.getOperand(0);
434 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000435}
436
Nate Begeman83e75ec2005-09-06 04:43:02 +0000437SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000438 SDOperand N0 = N->getOperand(0);
439 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000440 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
441 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000442
443 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000444 if (N0C && N1C)
445 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000446 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000448 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000449 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000450 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000451 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000452 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000454 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000455 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000456 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457}
458
Nate Begeman83e75ec2005-09-06 04:43:02 +0000459SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000460 SDOperand N0 = N->getOperand(0);
461 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
463 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000464 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000465
466 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 if (N0C && N1C)
468 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000469 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000470 // canonicalize constant to RHS
471 if (N0C && !N1C) {
472 std::swap(N0, N1);
473 std::swap(N0C, N1C);
474 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000475 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000477 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000478 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000480 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000481 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000482 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000483 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000484 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000485 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000486 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000487 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
488 if (N1C && N0.getOpcode() == ISD::MUL) {
489 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
490 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
491 if (N00C)
492 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
493 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
494 if (N01C)
495 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
496 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
497 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000498 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000499}
500
Nate Begeman83e75ec2005-09-06 04:43:02 +0000501SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000502 SDOperand N0 = N->getOperand(0);
503 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000504 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
505 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000506
507 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000508 if (N0C && N1C && !N1C->isNullValue())
509 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000510 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000511 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512}
513
Nate Begeman83e75ec2005-09-06 04:43:02 +0000514SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000515 SDOperand N0 = N->getOperand(0);
516 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000517 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
518 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000519
520 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000521 if (N0C && N1C && !N1C->isNullValue())
522 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000523 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000524 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000525 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000526 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000527 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000528 TLI.getShiftAmountTy()));
529 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530}
531
Nate Begeman83e75ec2005-09-06 04:43:02 +0000532SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000533 SDOperand N0 = N->getOperand(0);
534 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000535 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
536 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000537
538 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000539 if (N0C && N1C && !N1C->isNullValue())
540 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000541 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000542 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000543}
544
Nate Begeman83e75ec2005-09-06 04:43:02 +0000545SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000546 SDOperand N0 = N->getOperand(0);
547 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000548 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
549 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550
551 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 if (N0C && N1C && !N1C->isNullValue())
553 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000554 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000556 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000557}
558
Nate Begeman83e75ec2005-09-06 04:43:02 +0000559SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000560 SDOperand N0 = N->getOperand(0);
561 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000562 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563
564 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000565 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000566 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000567 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000568 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000569 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
570 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000571 TLI.getShiftAmountTy()));
572 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573}
574
Nate Begeman83e75ec2005-09-06 04:43:02 +0000575SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000576 SDOperand N0 = N->getOperand(0);
577 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000578 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000579
580 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000581 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000582 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000583 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000584 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585 return DAG.getConstant(0, N0.getValueType());
586 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000587}
588
Nate Begeman83e75ec2005-09-06 04:43:02 +0000589SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000590 SDOperand N0 = N->getOperand(0);
591 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000592 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000593 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
594 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000596 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000597
598 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000599 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000600 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000601 // canonicalize constant to RHS
602 if (N0C && !N1C) {
603 std::swap(N0, N1);
604 std::swap(N0C, N1C);
605 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000606 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000607 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000608 return N0;
609 // if (and x, c) is known to be zero, return 0
610 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
611 return DAG.getConstant(0, VT);
612 // fold (and x, c) -> x iff (x & ~c) == 0
613 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
614 TLI))
615 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000616 // fold (and (and x, c1), c2) -> (and x, c1^c2)
617 if (N1C && N0.getOpcode() == ISD::AND) {
618 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
619 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
620 if (N00C)
621 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
622 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
623 if (N01C)
624 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
625 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
626 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000627 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
628 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
629 unsigned ExtendBits =
630 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000631 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000632 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 }
634 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
635 if (N0.getOpcode() == ISD::OR)
636 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000638 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000639 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
640 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
641 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
642 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
643
644 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
645 MVT::isInteger(LL.getValueType())) {
646 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
647 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
648 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
649 WorkList.push_back(ORNode.Val);
650 return DAG.getSetCC(VT, ORNode, LR, Op1);
651 }
652 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
653 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
654 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
655 WorkList.push_back(ANDNode.Val);
656 return DAG.getSetCC(VT, ANDNode, LR, Op1);
657 }
658 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
659 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
660 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
661 WorkList.push_back(ORNode.Val);
662 return DAG.getSetCC(VT, ORNode, LR, Op1);
663 }
664 }
665 // canonicalize equivalent to ll == rl
666 if (LL == RR && LR == RL) {
667 Op1 = ISD::getSetCCSwappedOperands(Op1);
668 std::swap(RL, RR);
669 }
670 if (LL == RL && LR == RR) {
671 bool isInteger = MVT::isInteger(LL.getValueType());
672 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
673 if (Result != ISD::SETCC_INVALID)
674 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
675 }
676 }
677 // fold (and (zext x), (zext y)) -> (zext (and x, y))
678 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
679 N1.getOpcode() == ISD::ZERO_EXTEND &&
680 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
681 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
682 N0.getOperand(0), N1.getOperand(0));
683 WorkList.push_back(ANDNode.Val);
684 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
685 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000686 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
687 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
688 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
689 N0.getOperand(1) == N1.getOperand(1)) {
690 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
691 N0.getOperand(0), N1.getOperand(0));
692 WorkList.push_back(ANDNode.Val);
693 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
694 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000695 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000696}
697
Nate Begeman83e75ec2005-09-06 04:43:02 +0000698SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000699 SDOperand N0 = N->getOperand(0);
700 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000701 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000702 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
703 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000704 MVT::ValueType VT = N1.getValueType();
705 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000706
707 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000708 if (N0C && N1C)
709 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000710 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000711 // canonicalize constant to RHS
712 if (N0C && !N1C) {
713 std::swap(N0, N1);
714 std::swap(N0C, N1C);
715 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000716 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000717 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000718 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000719 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000720 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000721 return N1;
722 // fold (or x, c) -> c iff (x & ~c) == 0
723 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
724 TLI))
725 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000726 // fold (or (or x, c1), c2) -> (or x, c1|c2)
727 if (N1C && N0.getOpcode() == ISD::OR) {
728 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
729 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
730 if (N00C)
731 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
732 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
733 if (N01C)
734 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
735 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
736 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000737 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
738 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
739 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
740 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
741
742 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
743 MVT::isInteger(LL.getValueType())) {
744 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
745 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
746 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
747 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
748 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
749 WorkList.push_back(ORNode.Val);
750 return DAG.getSetCC(VT, ORNode, LR, Op1);
751 }
752 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
753 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
754 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
755 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
756 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
757 WorkList.push_back(ANDNode.Val);
758 return DAG.getSetCC(VT, ANDNode, LR, Op1);
759 }
760 }
761 // canonicalize equivalent to ll == rl
762 if (LL == RR && LR == RL) {
763 Op1 = ISD::getSetCCSwappedOperands(Op1);
764 std::swap(RL, RR);
765 }
766 if (LL == RL && LR == RR) {
767 bool isInteger = MVT::isInteger(LL.getValueType());
768 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
769 if (Result != ISD::SETCC_INVALID)
770 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
771 }
772 }
773 // fold (or (zext x), (zext y)) -> (zext (or x, y))
774 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
775 N1.getOpcode() == ISD::ZERO_EXTEND &&
776 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
777 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
778 N0.getOperand(0), N1.getOperand(0));
779 WorkList.push_back(ORNode.Val);
780 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
781 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000782 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000783}
784
Nate Begeman83e75ec2005-09-06 04:43:02 +0000785SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000786 SDOperand N0 = N->getOperand(0);
787 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000788 SDOperand LHS, RHS, CC;
789 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
790 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000791 MVT::ValueType VT = N0.getValueType();
792
793 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000794 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000795 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000796 // canonicalize constant to RHS
797 if (N0C && !N1C) {
798 std::swap(N0, N1);
799 std::swap(N0C, N1C);
800 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000801 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000802 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000803 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000804 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000805 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
806 bool isInt = MVT::isInteger(LHS.getValueType());
807 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
808 isInt);
809 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000810 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000811 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000812 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000813 assert(0 && "Unhandled SetCC Equivalent!");
814 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000815 }
Nate Begeman99801192005-09-07 23:25:52 +0000816 // fold !(x or y) -> (!x and !y) iff x or y are setcc
817 if (N1C && N1C->getValue() == 1 &&
818 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000819 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000820 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
821 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000822 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
823 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000824 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
825 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000826 }
827 }
Nate Begeman99801192005-09-07 23:25:52 +0000828 // fold !(x or y) -> (!x and !y) iff x or y are constants
829 if (N1C && N1C->isAllOnesValue() &&
830 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000831 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000832 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
833 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000834 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
835 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000836 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
837 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000838 }
839 }
Nate Begeman223df222005-09-08 20:18:10 +0000840 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
841 if (N1C && N0.getOpcode() == ISD::XOR) {
842 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
843 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
844 if (N00C)
845 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
846 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
847 if (N01C)
848 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
849 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
850 }
851 // fold (xor x, x) -> 0
852 if (N0 == N1)
853 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000854 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
855 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
856 N1.getOpcode() == ISD::ZERO_EXTEND &&
857 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
858 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
859 N0.getOperand(0), N1.getOperand(0));
860 WorkList.push_back(XORNode.Val);
861 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
862 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000863 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864}
865
Nate Begeman83e75ec2005-09-06 04:43:02 +0000866SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000867 SDOperand N0 = N->getOperand(0);
868 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000869 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
870 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000871 MVT::ValueType VT = N0.getValueType();
872 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
873
874 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000875 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000876 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000877 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000878 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000879 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000880 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000881 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000882 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000883 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000884 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000885 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000886 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000887 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
888 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000889 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000890 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000891 N0.getOperand(1).getOpcode() == ISD::Constant) {
892 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000893 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000894 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000895 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000896 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000897 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000898 }
899 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
900 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000901 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902 N0.getOperand(1).getOpcode() == ISD::Constant) {
903 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000904 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
906 DAG.getConstant(~0ULL << c1, VT));
907 if (c2 > c1)
908 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000909 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000910 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000911 return DAG.getNode(ISD::SRL, VT, Mask,
912 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000913 }
914 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000915 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000916 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917 DAG.getConstant(~0ULL << N1C->getValue(), VT));
918 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000919}
920
Nate Begeman83e75ec2005-09-06 04:43:02 +0000921SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000922 SDOperand N0 = N->getOperand(0);
923 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000924 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000926 MVT::ValueType VT = N0.getValueType();
927 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
928
929 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000930 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000931 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000933 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000934 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000935 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000937 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000938 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000940 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000941 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000942 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000943 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000944 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000945 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000946 return DAG.getNode(ISD::SRL, VT, N0, N1);
947 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000948}
949
Nate Begeman83e75ec2005-09-06 04:43:02 +0000950SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 SDOperand N0 = N->getOperand(0);
952 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
954 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955 MVT::ValueType VT = N0.getValueType();
956 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
957
958 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000959 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000960 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000961 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000962 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000963 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000965 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000966 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000967 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000968 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000971 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
972 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000973 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000974 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 N0.getOperand(1).getOpcode() == ISD::Constant) {
976 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000977 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000979 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000980 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000984}
985
Nate Begeman83e75ec2005-09-06 04:43:02 +0000986SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000987 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000988 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989
990 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000991 if (N0C)
992 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 N0.getValueType());
994 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000995}
996
Nate Begeman83e75ec2005-09-06 04:43:02 +0000997SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000998 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000999 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001000
1001 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001002 if (N0C)
1003 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001004 N0.getValueType());
1005 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006}
1007
Nate Begeman83e75ec2005-09-06 04:43:02 +00001008SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001009 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001010 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011
1012 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001013 if (N0C)
1014 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001015 N0.getValueType());
1016 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001017}
1018
Nate Begeman452d7be2005-09-16 00:54:12 +00001019SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1020 SDOperand N0 = N->getOperand(0);
1021 SDOperand N1 = N->getOperand(1);
1022 SDOperand N2 = N->getOperand(2);
1023 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1024 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1025 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1026 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001027
Nate Begeman452d7be2005-09-16 00:54:12 +00001028 // fold select C, X, X -> X
1029 if (N1 == N2)
1030 return N1;
1031 // fold select true, X, Y -> X
1032 if (N0C && !N0C->isNullValue())
1033 return N1;
1034 // fold select false, X, Y -> Y
1035 if (N0C && N0C->isNullValue())
1036 return N2;
1037 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001038 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001039 return DAG.getNode(ISD::OR, VT, N0, N2);
1040 // fold select C, 0, X -> ~C & X
1041 // FIXME: this should check for C type == X type, not i1?
1042 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1043 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1044 WorkList.push_back(XORNode.Val);
1045 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1046 }
1047 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001048 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001049 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1050 WorkList.push_back(XORNode.Val);
1051 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1052 }
1053 // fold select C, X, 0 -> C & X
1054 // FIXME: this should check for C type == X type, not i1?
1055 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1056 return DAG.getNode(ISD::AND, VT, N0, N1);
1057 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1058 if (MVT::i1 == VT && N0 == N1)
1059 return DAG.getNode(ISD::OR, VT, N0, N2);
1060 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1061 if (MVT::i1 == VT && N0 == N2)
1062 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001063 // fold selects based on a setcc into other things, such as min/max/abs
1064 if (N0.getOpcode() == ISD::SETCC)
1065 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001066 return SDOperand();
1067}
1068
1069SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001070 SDOperand N0 = N->getOperand(0);
1071 SDOperand N1 = N->getOperand(1);
1072 SDOperand N2 = N->getOperand(2);
1073 SDOperand N3 = N->getOperand(3);
1074 SDOperand N4 = N->getOperand(4);
1075 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1076 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1077 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1078 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1079
1080 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001081 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001082 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1083
Nate Begeman44728a72005-09-19 22:34:01 +00001084 // fold select_cc lhs, rhs, x, x, cc -> x
1085 if (N2 == N3)
1086 return N2;
1087 // fold select_cc true, x, y -> x
Chris Lattner91559022005-10-05 04:45:43 +00001088 if (SCCC && SCCC->getValue())
Nate Begeman44728a72005-09-19 22:34:01 +00001089 return N2;
1090 // fold select_cc false, x, y -> y
Chris Lattner91559022005-10-05 04:45:43 +00001091 if (SCCC && SCCC->getValue() == 0)
Nate Begeman44728a72005-09-19 22:34:01 +00001092 return N3;
1093 // fold select_cc into other things, such as min/max/abs
1094 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001095}
1096
1097SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1098 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1099 cast<CondCodeSDNode>(N->getOperand(2))->get());
1100}
1101
Nate Begeman83e75ec2005-09-06 04:43:02 +00001102SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001104 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001105 MVT::ValueType VT = N->getValueType(0);
1106
Nate Begeman1d4d4142005-09-01 00:19:25 +00001107 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001108 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001109 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (sext (sext x)) -> (sext x)
1111 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001112 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1113 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001114}
1115
Nate Begeman83e75ec2005-09-06 04:43:02 +00001116SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001117 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001118 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119 MVT::ValueType VT = N->getValueType(0);
1120
Nate Begeman1d4d4142005-09-01 00:19:25 +00001121 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001122 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001123 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001124 // fold (zext (zext x)) -> (zext x)
1125 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001126 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1127 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001128}
1129
Nate Begeman83e75ec2005-09-06 04:43:02 +00001130SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001131 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001133 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001134 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001135 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 if (N0C) {
1139 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001140 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001141 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001142 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001144 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001145 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1148 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1149 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001150 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001151 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001152 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1153 if (N0.getOpcode() == ISD::AssertSext &&
1154 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001155 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156 }
1157 // fold (sext_in_reg (sextload x)) -> (sextload x)
1158 if (N0.getOpcode() == ISD::SEXTLOAD &&
1159 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001160 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001161 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001162 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001164 if (N0.getOpcode() == ISD::SETCC &&
1165 TLI.getSetCCResultContents() ==
1166 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001167 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1169 // we probably actually want to handle this in two pieces. Rather than
1170 // checking all the top bits for zero, just check the sign bit here and turn
1171 // it into a zero extend inreg (AND with constant).
1172 // then, let the code for AND figure out if the mask is superfluous rather
1173 // than doing so here.
1174 if (N0.getOpcode() == ISD::AND &&
1175 N0.getOperand(1).getOpcode() == ISD::Constant) {
1176 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1177 unsigned NumBits = MVT::getSizeInBits(EVT);
1178 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001179 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001180 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001181 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001182}
1183
Nate Begeman83e75ec2005-09-06 04:43:02 +00001184SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001186 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001187 MVT::ValueType VT = N->getValueType(0);
1188
1189 // noop truncate
1190 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001191 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001192 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001193 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001194 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001195 // fold (truncate (truncate x)) -> (truncate x)
1196 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001197 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1199 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1200 if (N0.getValueType() < VT)
1201 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001202 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001203 else if (N0.getValueType() > VT)
1204 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001205 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001206 else
1207 // if the source and dest are the same type, we can drop both the extend
1208 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001209 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001210 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001211 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212}
1213
Chris Lattner01b3d732005-09-28 22:28:18 +00001214SDOperand DAGCombiner::visitFADD(SDNode *N) {
1215 SDOperand N0 = N->getOperand(0);
1216 SDOperand N1 = N->getOperand(1);
1217 MVT::ValueType VT = N->getValueType(0);
1218
1219 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1220 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1221 // fold floating point (fadd c1, c2)
1222 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1223 N->getValueType(0));
1224 }
1225 // fold (A + (-B)) -> A-B
1226 if (N1.getOpcode() == ISD::FNEG)
1227 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1228
1229 // fold ((-A) + B) -> B-A
1230 if (N0.getOpcode() == ISD::FNEG)
1231 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1232
1233 return SDOperand();
1234}
1235
1236SDOperand DAGCombiner::visitFSUB(SDNode *N) {
1237 SDOperand N0 = N->getOperand(0);
1238 SDOperand N1 = N->getOperand(1);
1239 MVT::ValueType VT = N->getValueType(0);
1240
1241 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1242 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1243 // fold floating point (fsub c1, c2)
1244 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1245 N->getValueType(0));
1246 }
1247 // fold (A-(-B)) -> A+B
1248 if (N1.getOpcode() == ISD::FNEG)
1249 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1250
1251 return SDOperand();
1252}
1253
1254SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1255 SDOperand N0 = N->getOperand(0);
1256 SDOperand N1 = N->getOperand(1);
1257 MVT::ValueType VT = N->getValueType(0);
1258
1259 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1260 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1261 // fold floating point (fmul c1, c2)
1262 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1263 N->getValueType(0));
1264 }
1265 return SDOperand();
1266}
1267
1268SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1269 SDOperand N0 = N->getOperand(0);
1270 SDOperand N1 = N->getOperand(1);
1271 MVT::ValueType VT = N->getValueType(0);
1272
1273 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1274 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1275 // fold floating point (fdiv c1, c2)
1276 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1277 N->getValueType(0));
1278 }
1279 return SDOperand();
1280}
1281
1282SDOperand DAGCombiner::visitFREM(SDNode *N) {
1283 SDOperand N0 = N->getOperand(0);
1284 SDOperand N1 = N->getOperand(1);
1285 MVT::ValueType VT = N->getValueType(0);
1286
1287 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1288 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1289 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1290 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1291 N->getValueType(0));
1292 }
1293 return SDOperand();
1294}
1295
1296
Nate Begeman83e75ec2005-09-06 04:43:02 +00001297SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001298 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001299 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300
1301 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001302 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001303 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1304 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001305}
1306
Nate Begeman83e75ec2005-09-06 04:43:02 +00001307SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001308 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001309 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310
1311 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001312 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001313 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1314 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315}
1316
Nate Begeman83e75ec2005-09-06 04:43:02 +00001317SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001319
1320 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001321 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001322 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1323 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001324}
1325
Nate Begeman83e75ec2005-09-06 04:43:02 +00001326SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001327 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328
1329 // fold (fp_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001330 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001331 return DAG.getConstant((uint64_t)N0CFP->getValue(), N->getValueType(0));
1332 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333}
1334
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001336 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337
1338 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001339 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001340 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1341 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342}
1343
Nate Begeman83e75ec2005-09-06 04:43:02 +00001344SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001345 SDOperand N0 = N->getOperand(0);
1346 MVT::ValueType VT = N->getValueType(0);
1347 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001348 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001349
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001351 if (N0CFP) {
1352 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001353 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001354 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001355 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356}
1357
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001359 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001360
1361 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1364 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001365}
1366
Nate Begeman83e75ec2005-09-06 04:43:02 +00001367SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001370 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001371 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372 // fold (neg (sub x, y)) -> (sub y, x)
1373 if (N->getOperand(0).getOpcode() == ISD::SUB)
1374 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001375 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001376 // fold (neg (neg x)) -> x
1377 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001378 return N->getOperand(0).getOperand(0);
1379 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001380}
1381
Nate Begeman83e75ec2005-09-06 04:43:02 +00001382SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001384 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001385 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001386 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001387 // fold (fabs (fabs x)) -> (fabs x)
1388 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001389 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001390 // fold (fabs (fneg x)) -> (fabs x)
1391 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1392 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001393 N->getOperand(0).getOperand(0));
1394 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395}
1396
Nate Begeman44728a72005-09-19 22:34:01 +00001397SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1398 SDOperand Chain = N->getOperand(0);
1399 SDOperand N1 = N->getOperand(1);
1400 SDOperand N2 = N->getOperand(2);
1401 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1402
1403 // never taken branch, fold to chain
1404 if (N1C && N1C->isNullValue())
1405 return Chain;
1406 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001407 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001408 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1409 return SDOperand();
1410}
1411
1412SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1413 SDOperand Chain = N->getOperand(0);
1414 SDOperand N1 = N->getOperand(1);
1415 SDOperand N2 = N->getOperand(2);
1416 SDOperand N3 = N->getOperand(3);
1417 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1418
1419 // unconditional branch to true mbb
1420 if (N1C && N1C->getValue() == 1)
1421 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1422 // unconditional branch to false mbb
1423 if (N1C && N1C->isNullValue())
1424 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1425 return SDOperand();
1426}
1427
Chris Lattner3ea0b472005-10-05 06:47:48 +00001428// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1429//
Nate Begeman44728a72005-09-19 22:34:01 +00001430SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001431 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1432 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1433
1434 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001435 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1436 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1437
1438 // fold br_cc true, dest -> br dest (unconditional branch)
1439 if (SCCC && SCCC->getValue())
1440 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1441 N->getOperand(4));
1442 // fold br_cc false, dest -> unconditional fall through
1443 if (SCCC && SCCC->isNullValue())
1444 return N->getOperand(0);
1445 // fold to a simpler setcc
1446 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1447 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1448 Simp.getOperand(2), Simp.getOperand(0),
1449 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001450 return SDOperand();
1451}
1452
1453SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001454 SDOperand Chain = N->getOperand(0);
1455 SDOperand CCN = N->getOperand(1);
1456 SDOperand LHS = N->getOperand(2);
1457 SDOperand RHS = N->getOperand(3);
1458 SDOperand N4 = N->getOperand(4);
1459 SDOperand N5 = N->getOperand(5);
1460
1461 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1462 cast<CondCodeSDNode>(CCN)->get(), false);
1463 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1464
1465 // fold select_cc lhs, rhs, x, x, cc -> x
1466 if (N4 == N5)
1467 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1468 // fold select_cc true, x, y -> x
1469 if (SCCC && SCCC->getValue())
1470 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1471 // fold select_cc false, x, y -> y
1472 if (SCCC && SCCC->isNullValue())
1473 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1474 // fold to a simpler setcc
1475 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1476 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1477 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001478 return SDOperand();
1479}
1480
1481SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
1482 return SDOperand();
1483}
1484
1485SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1486 SDOperand N2, SDOperand N3,
1487 ISD::CondCode CC) {
1488 return SDOperand();
1489}
1490
Nate Begeman452d7be2005-09-16 00:54:12 +00001491SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001492 SDOperand N1, ISD::CondCode Cond,
1493 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001494 // These setcc operations always fold.
1495 switch (Cond) {
1496 default: break;
1497 case ISD::SETFALSE:
1498 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1499 case ISD::SETTRUE:
1500 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1501 }
1502
1503 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1504 uint64_t C1 = N1C->getValue();
1505 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1506 uint64_t C0 = N0C->getValue();
1507
1508 // Sign extend the operands if required
1509 if (ISD::isSignedIntSetCC(Cond)) {
1510 C0 = N0C->getSignExtended();
1511 C1 = N1C->getSignExtended();
1512 }
1513
1514 switch (Cond) {
1515 default: assert(0 && "Unknown integer setcc!");
1516 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1517 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1518 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1519 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1520 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1521 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1522 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1523 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1524 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1525 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1526 }
1527 } else {
1528 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1529 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1530 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1531
1532 // If the comparison constant has bits in the upper part, the
1533 // zero-extended value could never match.
1534 if (C1 & (~0ULL << InSize)) {
1535 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1536 switch (Cond) {
1537 case ISD::SETUGT:
1538 case ISD::SETUGE:
1539 case ISD::SETEQ: return DAG.getConstant(0, VT);
1540 case ISD::SETULT:
1541 case ISD::SETULE:
1542 case ISD::SETNE: return DAG.getConstant(1, VT);
1543 case ISD::SETGT:
1544 case ISD::SETGE:
1545 // True if the sign bit of C1 is set.
1546 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1547 case ISD::SETLT:
1548 case ISD::SETLE:
1549 // True if the sign bit of C1 isn't set.
1550 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1551 default:
1552 break;
1553 }
1554 }
1555
1556 // Otherwise, we can perform the comparison with the low bits.
1557 switch (Cond) {
1558 case ISD::SETEQ:
1559 case ISD::SETNE:
1560 case ISD::SETUGT:
1561 case ISD::SETUGE:
1562 case ISD::SETULT:
1563 case ISD::SETULE:
1564 return DAG.getSetCC(VT, N0.getOperand(0),
1565 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1566 Cond);
1567 default:
1568 break; // todo, be more careful with signed comparisons
1569 }
1570 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1571 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1572 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1573 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1574 MVT::ValueType ExtDstTy = N0.getValueType();
1575 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1576
1577 // If the extended part has any inconsistent bits, it cannot ever
1578 // compare equal. In other words, they have to be all ones or all
1579 // zeros.
1580 uint64_t ExtBits =
1581 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1582 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1583 return DAG.getConstant(Cond == ISD::SETNE, VT);
1584
1585 SDOperand ZextOp;
1586 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1587 if (Op0Ty == ExtSrcTy) {
1588 ZextOp = N0.getOperand(0);
1589 } else {
1590 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1591 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1592 DAG.getConstant(Imm, Op0Ty));
1593 }
1594 WorkList.push_back(ZextOp.Val);
1595 // Otherwise, make this a use of a zext.
1596 return DAG.getSetCC(VT, ZextOp,
1597 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1598 ExtDstTy),
1599 Cond);
1600 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001601
Nate Begeman452d7be2005-09-16 00:54:12 +00001602 uint64_t MinVal, MaxVal;
1603 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1604 if (ISD::isSignedIntSetCC(Cond)) {
1605 MinVal = 1ULL << (OperandBitSize-1);
1606 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1607 MaxVal = ~0ULL >> (65-OperandBitSize);
1608 else
1609 MaxVal = 0;
1610 } else {
1611 MinVal = 0;
1612 MaxVal = ~0ULL >> (64-OperandBitSize);
1613 }
1614
1615 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1616 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1617 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1618 --C1; // X >= C0 --> X > (C0-1)
1619 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1620 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1621 }
1622
1623 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1624 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1625 ++C1; // X <= C0 --> X < (C0+1)
1626 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1627 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1628 }
1629
1630 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1631 return DAG.getConstant(0, VT); // X < MIN --> false
1632
1633 // Canonicalize setgt X, Min --> setne X, Min
1634 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1635 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1636
1637 // If we have setult X, 1, turn it into seteq X, 0
1638 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1639 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1640 ISD::SETEQ);
1641 // If we have setugt X, Max-1, turn it into seteq X, Max
1642 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1643 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1644 ISD::SETEQ);
1645
1646 // If we have "setcc X, C0", check to see if we can shrink the immediate
1647 // by changing cc.
1648
1649 // SETUGT X, SINTMAX -> SETLT X, 0
1650 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1651 C1 == (~0ULL >> (65-OperandBitSize)))
1652 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1653 ISD::SETLT);
1654
1655 // FIXME: Implement the rest of these.
1656
1657 // Fold bit comparisons when we can.
1658 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1659 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1660 if (ConstantSDNode *AndRHS =
1661 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1662 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1663 // Perform the xform if the AND RHS is a single bit.
1664 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1665 return DAG.getNode(ISD::SRL, VT, N0,
1666 DAG.getConstant(Log2_64(AndRHS->getValue()),
1667 TLI.getShiftAmountTy()));
1668 }
1669 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1670 // (X & 8) == 8 --> (X & 8) >> 3
1671 // Perform the xform if C1 is a single bit.
1672 if ((C1 & (C1-1)) == 0) {
1673 return DAG.getNode(ISD::SRL, VT, N0,
1674 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1675 }
1676 }
1677 }
1678 }
1679 } else if (isa<ConstantSDNode>(N0.Val)) {
1680 // Ensure that the constant occurs on the RHS.
1681 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1682 }
1683
1684 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1685 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1686 double C0 = N0C->getValue(), C1 = N1C->getValue();
1687
1688 switch (Cond) {
1689 default: break; // FIXME: Implement the rest of these!
1690 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1691 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1692 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1693 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1694 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1695 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1696 }
1697 } else {
1698 // Ensure that the constant occurs on the RHS.
1699 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1700 }
1701
1702 if (N0 == N1) {
1703 // We can always fold X == Y for integer setcc's.
1704 if (MVT::isInteger(N0.getValueType()))
1705 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1706 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1707 if (UOF == 2) // FP operators that are undefined on NaNs.
1708 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1709 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1710 return DAG.getConstant(UOF, VT);
1711 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1712 // if it is not already.
1713 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1714 if (NewCond != Cond)
1715 return DAG.getSetCC(VT, N0, N1, NewCond);
1716 }
1717
1718 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1719 MVT::isInteger(N0.getValueType())) {
1720 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1721 N0.getOpcode() == ISD::XOR) {
1722 // Simplify (X+Y) == (X+Z) --> Y == Z
1723 if (N0.getOpcode() == N1.getOpcode()) {
1724 if (N0.getOperand(0) == N1.getOperand(0))
1725 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1726 if (N0.getOperand(1) == N1.getOperand(1))
1727 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1728 if (isCommutativeBinOp(N0.getOpcode())) {
1729 // If X op Y == Y op X, try other combinations.
1730 if (N0.getOperand(0) == N1.getOperand(1))
1731 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1732 if (N0.getOperand(1) == N1.getOperand(0))
1733 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1734 }
1735 }
1736
Chris Lattner5c46f742005-10-05 06:11:08 +00001737 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
1738 if (N0.getOpcode() == ISD::XOR)
1739 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1740 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1741 // If we know that all of the inverted bits are zero, don't bother
1742 // performing the inversion.
1743 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
1744 return DAG.getSetCC(VT, N0.getOperand(0),
1745 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
1746 N0.getValueType()), Cond);
1747 }
1748
Nate Begeman452d7be2005-09-16 00:54:12 +00001749 // Simplify (X+Z) == X --> Z == 0
1750 if (N0.getOperand(0) == N1)
1751 return DAG.getSetCC(VT, N0.getOperand(1),
1752 DAG.getConstant(0, N0.getValueType()), Cond);
1753 if (N0.getOperand(1) == N1) {
1754 if (isCommutativeBinOp(N0.getOpcode()))
1755 return DAG.getSetCC(VT, N0.getOperand(0),
1756 DAG.getConstant(0, N0.getValueType()), Cond);
1757 else {
1758 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1759 // (Z-X) == X --> Z == X<<1
1760 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1761 N1,
1762 DAG.getConstant(1,TLI.getShiftAmountTy()));
1763 WorkList.push_back(SH.Val);
1764 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1765 }
1766 }
1767 }
1768
1769 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1770 N1.getOpcode() == ISD::XOR) {
1771 // Simplify X == (X+Z) --> Z == 0
1772 if (N1.getOperand(0) == N0) {
1773 return DAG.getSetCC(VT, N1.getOperand(1),
1774 DAG.getConstant(0, N1.getValueType()), Cond);
1775 } else if (N1.getOperand(1) == N0) {
1776 if (isCommutativeBinOp(N1.getOpcode())) {
1777 return DAG.getSetCC(VT, N1.getOperand(0),
1778 DAG.getConstant(0, N1.getValueType()), Cond);
1779 } else {
1780 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1781 // X == (Z-X) --> X<<1 == Z
1782 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1783 DAG.getConstant(1,TLI.getShiftAmountTy()));
1784 WorkList.push_back(SH.Val);
1785 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1786 }
1787 }
1788 }
1789 }
1790
1791 // Fold away ALL boolean setcc's.
1792 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00001793 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001794 switch (Cond) {
1795 default: assert(0 && "Unknown integer setcc!");
1796 case ISD::SETEQ: // X == Y -> (X^Y)^1
1797 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1798 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1799 WorkList.push_back(Temp.Val);
1800 break;
1801 case ISD::SETNE: // X != Y --> (X^Y)
1802 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1803 break;
1804 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1805 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1806 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1807 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1808 WorkList.push_back(Temp.Val);
1809 break;
1810 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1811 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1812 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1813 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1814 WorkList.push_back(Temp.Val);
1815 break;
1816 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1817 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1818 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1819 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
1820 WorkList.push_back(Temp.Val);
1821 break;
1822 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
1823 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
1824 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1825 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
1826 break;
1827 }
1828 if (VT != MVT::i1) {
1829 WorkList.push_back(N0.Val);
1830 // FIXME: If running after legalize, we probably can't do this.
1831 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
1832 }
1833 return N0;
1834 }
1835
1836 // Could not fold it.
1837 return SDOperand();
1838}
1839
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840// SelectionDAG::Combine - This is the entry point for the file.
1841//
Nate Begeman4ebd8052005-09-01 23:24:04 +00001842void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 /// run - This is the main entry point to this class.
1844 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00001845 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846}