blob: 60bb6c6795d1617da78516c31cd0bd3c67b5f3c8 [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 Begemanf845b452005-10-08 00:29:44 +000042// FIXME: select ne (select cc, 1, 0), 0, true, false -> select cc, true, false
Chris Lattnera19cea02005-10-09 22:59:08 +000043// FIXME: sext_inreg(SRL) -> SRA:
44// int %simple(uint %X) { %tmp.4 = shr uint %X, ubyte 16
45// %tmp.5 = cast uint %tmp.4 to short %tmp.6 = cast short %tmp.5 to int
46// ret int %tmp.6 }
Nate Begeman1d4d4142005-09-01 00:19:25 +000047//
48//===----------------------------------------------------------------------===//
49
50#define DEBUG_TYPE "dagcombine"
51#include "llvm/ADT/Statistic.h"
52#include "llvm/CodeGen/SelectionDAG.h"
Nate Begeman2300f552005-09-07 00:15:36 +000053#include "llvm/Support/Debug.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000054#include "llvm/Support/MathExtras.h"
55#include "llvm/Target/TargetLowering.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000056#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000057#include <cmath>
58using namespace llvm;
59
60namespace {
61 Statistic<> NodesCombined ("dagcombiner", "Number of dag nodes combined");
62
63 class DAGCombiner {
64 SelectionDAG &DAG;
65 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000066 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000067
68 // Worklist of all of the nodes that need to be simplified.
69 std::vector<SDNode*> WorkList;
70
71 /// AddUsersToWorkList - When an instruction is simplified, add all users of
72 /// the instruction to the work lists because they might get more simplified
73 /// now.
74 ///
75 void AddUsersToWorkList(SDNode *N) {
76 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000077 UI != UE; ++UI)
78 WorkList.push_back(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000079 }
80
81 /// removeFromWorkList - remove all instances of N from the worklist.
82 void removeFromWorkList(SDNode *N) {
83 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
84 WorkList.end());
85 }
86
87 /// visit - call the node-specific routine that knows how to fold each
88 /// particular type of node.
Nate Begeman83e75ec2005-09-06 04:43:02 +000089 SDOperand visit(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +000090
91 // Visitation implementation - Implement dag node combining for different
92 // node types. The semantics are as follows:
93 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +000094 // SDOperand.Val == 0 - No change was made
95 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +000096 //
Nate Begeman83e75ec2005-09-06 04:43:02 +000097 SDOperand visitTokenFactor(SDNode *N);
98 SDOperand visitADD(SDNode *N);
99 SDOperand visitSUB(SDNode *N);
100 SDOperand visitMUL(SDNode *N);
101 SDOperand visitSDIV(SDNode *N);
102 SDOperand visitUDIV(SDNode *N);
103 SDOperand visitSREM(SDNode *N);
104 SDOperand visitUREM(SDNode *N);
105 SDOperand visitMULHU(SDNode *N);
106 SDOperand visitMULHS(SDNode *N);
107 SDOperand visitAND(SDNode *N);
108 SDOperand visitOR(SDNode *N);
109 SDOperand visitXOR(SDNode *N);
110 SDOperand visitSHL(SDNode *N);
111 SDOperand visitSRA(SDNode *N);
112 SDOperand visitSRL(SDNode *N);
113 SDOperand visitCTLZ(SDNode *N);
114 SDOperand visitCTTZ(SDNode *N);
115 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000116 SDOperand visitSELECT(SDNode *N);
117 SDOperand visitSELECT_CC(SDNode *N);
118 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000119 SDOperand visitSIGN_EXTEND(SDNode *N);
120 SDOperand visitZERO_EXTEND(SDNode *N);
121 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
122 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000123
124 SDOperand visitFADD(SDNode *N);
125 SDOperand visitFSUB(SDNode *N);
126 SDOperand visitFMUL(SDNode *N);
127 SDOperand visitFDIV(SDNode *N);
128 SDOperand visitFREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000129 SDOperand visitSINT_TO_FP(SDNode *N);
130 SDOperand visitUINT_TO_FP(SDNode *N);
131 SDOperand visitFP_TO_SINT(SDNode *N);
132 SDOperand visitFP_TO_UINT(SDNode *N);
133 SDOperand visitFP_ROUND(SDNode *N);
134 SDOperand visitFP_ROUND_INREG(SDNode *N);
135 SDOperand visitFP_EXTEND(SDNode *N);
136 SDOperand visitFNEG(SDNode *N);
137 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000138 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000139 SDOperand visitBRCONDTWOWAY(SDNode *N);
140 SDOperand visitBR_CC(SDNode *N);
141 SDOperand visitBRTWOWAY_CC(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000142
Nate Begeman44728a72005-09-19 22:34:01 +0000143 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
144 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
145 SDOperand N3, ISD::CondCode CC);
Nate Begeman452d7be2005-09-16 00:54:12 +0000146 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000147 ISD::CondCode Cond, bool foldBooleans = true);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000148public:
149 DAGCombiner(SelectionDAG &D)
Nate Begeman646d7e22005-09-02 21:18:40 +0000150 : DAG(D), TLI(D.getTargetLoweringInfo()), AfterLegalize(false) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000151
152 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000153 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000154 };
155}
156
157/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
158/// this predicate to simplify operations downstream. V and Mask are known to
159/// be the same type.
160static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
161 const TargetLowering &TLI) {
162 unsigned SrcBits;
163 if (Mask == 0) return true;
164
165 // If we know the result of a setcc has the top bits zero, use this info.
166 switch (Op.getOpcode()) {
Nate Begeman4ebd8052005-09-01 23:24:04 +0000167 case ISD::Constant:
168 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
169 case ISD::SETCC:
Nate Begeman646d7e22005-09-02 21:18:40 +0000170 // FIXME: teach this about non ZeroOrOne values, such as 0 or -1
Nate Begeman4ebd8052005-09-01 23:24:04 +0000171 return ((Mask & 1) == 0) &&
172 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
173 case ISD::ZEXTLOAD:
174 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
175 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
176 case ISD::ZERO_EXTEND:
177 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
178 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
179 case ISD::AssertZext:
180 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
181 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
182 case ISD::AND:
Chris Lattneree899e62005-10-09 22:12:36 +0000183 // If either of the operands has zero bits, the result will too.
184 if (MaskedValueIsZero(Op.getOperand(1), Mask, TLI) ||
185 MaskedValueIsZero(Op.getOperand(0), Mask, TLI))
186 return true;
187
Nate Begeman4ebd8052005-09-01 23:24:04 +0000188 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
189 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
190 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
Chris Lattneree899e62005-10-09 22:12:36 +0000191 return false;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000192 case ISD::OR:
193 case ISD::XOR:
194 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
195 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
196 case ISD::SELECT:
197 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
198 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
199 case ISD::SELECT_CC:
200 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
201 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
202 case ISD::SRL:
203 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
204 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
205 uint64_t NewVal = Mask << ShAmt->getValue();
206 SrcBits = MVT::getSizeInBits(Op.getValueType());
207 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
208 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
209 }
210 return false;
211 case ISD::SHL:
212 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
213 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
214 uint64_t NewVal = Mask >> ShAmt->getValue();
215 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
216 }
217 return false;
Chris Lattnerc4ced262005-10-07 15:30:32 +0000218 case ISD::SUB:
219 if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
220 // We know that the top bits of C-X are clear if X contains less bits
221 // than C (i.e. no wrap-around can happen). For example, 20-X is
222 // positive if we can prove that X is >= 0 and < 16.
223 unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
224 if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
225 unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
226 uint64_t MaskV = (1ULL << (63-NLZ))-1;
227 if (MaskedValueIsZero(Op.getOperand(1), ~MaskV, TLI)) {
228 // High bits are clear this value is known to be >= C.
229 unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
230 if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
231 return true;
232 }
233 }
234 }
235 break;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000236 case ISD::CTTZ:
237 case ISD::CTLZ:
238 case ISD::CTPOP:
239 // Bit counting instructions can not set the high bits of the result
240 // register. The max number of bits sets depends on the input.
241 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
242
243 // TODO we could handle some SRA cases here.
244 default: break;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000245 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000246 return false;
247}
248
Nate Begeman4ebd8052005-09-01 23:24:04 +0000249// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
250// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000251// Also, set the incoming LHS, RHS, and CC references to the appropriate
252// nodes based on the type of node we are checking. This simplifies life a
253// bit for the callers.
254static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
255 SDOperand &CC) {
256 if (N.getOpcode() == ISD::SETCC) {
257 LHS = N.getOperand(0);
258 RHS = N.getOperand(1);
259 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000260 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000261 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000262 if (N.getOpcode() == ISD::SELECT_CC &&
263 N.getOperand(2).getOpcode() == ISD::Constant &&
264 N.getOperand(3).getOpcode() == ISD::Constant &&
265 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000266 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
267 LHS = N.getOperand(0);
268 RHS = N.getOperand(1);
269 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000270 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000271 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000272 return false;
273}
274
Nate Begeman99801192005-09-07 23:25:52 +0000275// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
276// one use. If this is true, it allows the users to invert the operation for
277// free when it is profitable to do so.
278static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000279 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000280 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000281 return true;
282 return false;
283}
284
Nate Begeman452d7be2005-09-16 00:54:12 +0000285// FIXME: This should probably go in the ISD class rather than being duplicated
286// in several files.
287static bool isCommutativeBinOp(unsigned Opcode) {
288 switch (Opcode) {
289 case ISD::ADD:
290 case ISD::MUL:
291 case ISD::AND:
292 case ISD::OR:
293 case ISD::XOR: return true;
294 default: return false; // FIXME: Need commutative info for user ops!
295 }
296}
297
Nate Begeman4ebd8052005-09-01 23:24:04 +0000298void DAGCombiner::Run(bool RunningAfterLegalize) {
299 // set the instance variable, so that the various visit routines may use it.
300 AfterLegalize = RunningAfterLegalize;
301
Nate Begeman646d7e22005-09-02 21:18:40 +0000302 // Add all the dag nodes to the worklist.
303 WorkList.insert(WorkList.end(), DAG.allnodes_begin(), DAG.allnodes_end());
Nate Begeman83e75ec2005-09-06 04:43:02 +0000304
Chris Lattner95038592005-10-05 06:35:28 +0000305 // Create a dummy node (which is not added to allnodes), that adds a reference
306 // to the root node, preventing it from being deleted, and tracking any
307 // changes of the root.
308 HandleSDNode Dummy(DAG.getRoot());
309
Nate Begeman1d4d4142005-09-01 00:19:25 +0000310 // while the worklist isn't empty, inspect the node on the end of it and
311 // try and combine it.
312 while (!WorkList.empty()) {
313 SDNode *N = WorkList.back();
314 WorkList.pop_back();
315
316 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000317 // N is deleted from the DAG, since they too may now be dead or may have a
318 // reduced number of uses, allowing other xforms.
319 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000320 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
321 WorkList.push_back(N->getOperand(i).Val);
322
Nate Begeman1d4d4142005-09-01 00:19:25 +0000323 removeFromWorkList(N);
Chris Lattner95038592005-10-05 06:35:28 +0000324 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000325 continue;
326 }
327
Nate Begeman83e75ec2005-09-06 04:43:02 +0000328 SDOperand RV = visit(N);
329 if (RV.Val) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000330 ++NodesCombined;
Nate Begeman646d7e22005-09-02 21:18:40 +0000331 // If we get back the same node we passed in, rather than a new node or
332 // zero, we know that the node must have defined multiple values and
333 // CombineTo was used. Since CombineTo takes care of the worklist
334 // mechanics for us, we have no work to do in this case.
Nate Begeman83e75ec2005-09-06 04:43:02 +0000335 if (RV.Val != N) {
Nate Begeman2300f552005-09-07 00:15:36 +0000336 DEBUG(std::cerr << "\nReplacing "; N->dump();
337 std::cerr << "\nWith: "; RV.Val->dump();
338 std::cerr << '\n');
Nate Begeman99801192005-09-07 23:25:52 +0000339 DAG.ReplaceAllUsesWith(N, std::vector<SDOperand>(1, RV));
Nate Begeman646d7e22005-09-02 21:18:40 +0000340
341 // Push the new node and any users onto the worklist
Nate Begeman83e75ec2005-09-06 04:43:02 +0000342 WorkList.push_back(RV.Val);
343 AddUsersToWorkList(RV.Val);
Nate Begeman646d7e22005-09-02 21:18:40 +0000344
345 // Nodes can end up on the worklist more than once. Make sure we do
346 // not process a node that has been replaced.
347 removeFromWorkList(N);
Chris Lattner5c46f742005-10-05 06:11:08 +0000348
349 // Finally, since the node is now dead, remove it from the graph.
350 DAG.DeleteNode(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000351 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000352 }
353 }
Chris Lattner95038592005-10-05 06:35:28 +0000354
355 // If the root changed (e.g. it was a dead load, update the root).
356 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000357}
358
Nate Begeman83e75ec2005-09-06 04:43:02 +0000359SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000360 switch(N->getOpcode()) {
361 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000362 case ISD::TokenFactor: return visitTokenFactor(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000363 case ISD::ADD: return visitADD(N);
364 case ISD::SUB: return visitSUB(N);
365 case ISD::MUL: return visitMUL(N);
366 case ISD::SDIV: return visitSDIV(N);
367 case ISD::UDIV: return visitUDIV(N);
368 case ISD::SREM: return visitSREM(N);
369 case ISD::UREM: return visitUREM(N);
370 case ISD::MULHU: return visitMULHU(N);
371 case ISD::MULHS: return visitMULHS(N);
372 case ISD::AND: return visitAND(N);
373 case ISD::OR: return visitOR(N);
374 case ISD::XOR: return visitXOR(N);
375 case ISD::SHL: return visitSHL(N);
376 case ISD::SRA: return visitSRA(N);
377 case ISD::SRL: return visitSRL(N);
378 case ISD::CTLZ: return visitCTLZ(N);
379 case ISD::CTTZ: return visitCTTZ(N);
380 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000381 case ISD::SELECT: return visitSELECT(N);
382 case ISD::SELECT_CC: return visitSELECT_CC(N);
383 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000384 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
385 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
386 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
387 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000388 case ISD::FADD: return visitFADD(N);
389 case ISD::FSUB: return visitFSUB(N);
390 case ISD::FMUL: return visitFMUL(N);
391 case ISD::FDIV: return visitFDIV(N);
392 case ISD::FREM: return visitFREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000393 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
394 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
395 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
396 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
397 case ISD::FP_ROUND: return visitFP_ROUND(N);
398 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
399 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
400 case ISD::FNEG: return visitFNEG(N);
401 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000402 case ISD::BRCOND: return visitBRCOND(N);
403 case ISD::BRCONDTWOWAY: return visitBRCONDTWOWAY(N);
404 case ISD::BR_CC: return visitBR_CC(N);
405 case ISD::BRTWOWAY_CC: return visitBRTWOWAY_CC(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000406 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000407 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000408}
409
Nate Begeman83e75ec2005-09-06 04:43:02 +0000410SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000411 // If the token factor has two operands and one is the entry token, replace
412 // the token factor with the other operand.
413 if (N->getNumOperands() == 2) {
414 if (N->getOperand(0).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000415 return N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000416 if (N->getOperand(1).getOpcode() == ISD::EntryToken)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000417 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000418 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000419 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000420}
421
Nate Begeman83e75ec2005-09-06 04:43:02 +0000422SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000423 SDOperand N0 = N->getOperand(0);
424 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000425 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
426 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000427 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000428
429 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000430 if (N0C && N1C)
Nate Begemanf89d78d2005-09-07 16:09:19 +0000431 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000432 // canonicalize constant to RHS
433 if (N0C && !N1C) {
434 std::swap(N0, N1);
435 std::swap(N0C, N1C);
436 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000437 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000438 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000439 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000440 // fold (add (add x, c1), c2) -> (add x, c1+c2)
441 if (N1C && N0.getOpcode() == ISD::ADD) {
442 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
443 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
444 if (N00C)
445 return DAG.getNode(ISD::ADD, VT, N0.getOperand(1),
446 DAG.getConstant(N1C->getValue()+N00C->getValue(), VT));
447 if (N01C)
448 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
449 DAG.getConstant(N1C->getValue()+N01C->getValue(), VT));
450 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000451 // fold ((0-A) + B) -> B-A
452 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
453 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000454 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455 // fold (A + (0-B)) -> A-B
456 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
457 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000458 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000459 // fold (A+(B-A)) -> B
460 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000461 return N1.getOperand(0);
462 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463}
464
Nate Begeman83e75ec2005-09-06 04:43:02 +0000465SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466 SDOperand N0 = N->getOperand(0);
467 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000468 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000470
471 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000472 if (N0C && N1C)
473 return DAG.getConstant(N0C->getValue() - N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000474 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000475 // fold (sub x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000477 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000478 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +0000479 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000480 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000481 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +0000482 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000483 return N0.getOperand(0);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000484 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000485}
486
Nate Begeman83e75ec2005-09-06 04:43:02 +0000487SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000488 SDOperand N0 = N->getOperand(0);
489 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000490 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
491 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman223df222005-09-08 20:18:10 +0000492 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000493
494 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000495 if (N0C && N1C)
496 return DAG.getConstant(N0C->getValue() * N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000497 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000498 // canonicalize constant to RHS
499 if (N0C && !N1C) {
500 std::swap(N0, N1);
501 std::swap(N0C, N1C);
502 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000503 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000504 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000505 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000506 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +0000507 if (N1C && N1C->isAllOnesValue())
Nate Begeman1d4d4142005-09-01 00:19:25 +0000508 return DAG.getNode(ISD::SUB, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000509 DAG.getConstant(0, N->getValueType(0)), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000510 // fold (mul x, (1 << c)) -> x << c
Nate Begeman646d7e22005-09-02 21:18:40 +0000511 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000512 return DAG.getNode(ISD::SHL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000513 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000514 TLI.getShiftAmountTy()));
Nate Begeman223df222005-09-08 20:18:10 +0000515 // fold (mul (mul x, c1), c2) -> (mul x, c1*c2)
516 if (N1C && N0.getOpcode() == ISD::MUL) {
517 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
518 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
519 if (N00C)
520 return DAG.getNode(ISD::MUL, VT, N0.getOperand(1),
521 DAG.getConstant(N1C->getValue()*N00C->getValue(), VT));
522 if (N01C)
523 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0),
524 DAG.getConstant(N1C->getValue()*N01C->getValue(), VT));
525 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000526 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000527}
528
Nate Begeman83e75ec2005-09-06 04:43:02 +0000529SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000530 SDOperand N0 = N->getOperand(0);
531 SDOperand N1 = N->getOperand(1);
Chris Lattner094c8fc2005-10-07 06:10:46 +0000532 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +0000533 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
534 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000535
536 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000537 if (N0C && N1C && !N1C->isNullValue())
538 return DAG.getConstant(N0C->getSignExtended() / N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000539 N->getValueType(0));
Chris Lattner094c8fc2005-10-07 06:10:46 +0000540
541 // If we know the sign bits of both operands are zero, strength reduce to a
542 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
543 uint64_t SignBit = 1ULL << (MVT::getSizeInBits(VT)-1);
544 if (MaskedValueIsZero(N1, SignBit, TLI) &&
545 MaskedValueIsZero(N0, SignBit, TLI))
546 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
547
548
Nate Begeman83e75ec2005-09-06 04:43:02 +0000549 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000550}
551
Nate Begeman83e75ec2005-09-06 04:43:02 +0000552SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000553 SDOperand N0 = N->getOperand(0);
554 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000555 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
556 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000557
558 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000559 if (N0C && N1C && !N1C->isNullValue())
560 return DAG.getConstant(N0C->getValue() / N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000561 N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000562 // fold (udiv x, (1 << c)) -> x >>u c
Nate Begeman646d7e22005-09-02 21:18:40 +0000563 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 return DAG.getNode(ISD::SRL, N->getValueType(0), N0,
Nate Begeman646d7e22005-09-02 21:18:40 +0000565 DAG.getConstant(Log2_64(N1C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000566 TLI.getShiftAmountTy()));
567 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000568}
569
Nate Begeman83e75ec2005-09-06 04:43:02 +0000570SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571 SDOperand N0 = N->getOperand(0);
572 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000573 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
574 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000575
576 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 if (N0C && N1C && !N1C->isNullValue())
578 return DAG.getConstant(N0C->getSignExtended() % N1C->getSignExtended(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000579 N->getValueType(0));
Nate Begeman83e75ec2005-09-06 04:43:02 +0000580 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000581}
582
Nate Begeman83e75ec2005-09-06 04:43:02 +0000583SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584 SDOperand N0 = N->getOperand(0);
585 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000586 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
587 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000588
589 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000590 if (N0C && N1C && !N1C->isNullValue())
591 return DAG.getConstant(N0C->getValue() % N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000592 N->getValueType(0));
Nate Begeman646d7e22005-09-02 21:18:40 +0000593 // FIXME: c2 power of 2 -> mask?
Nate Begeman83e75ec2005-09-06 04:43:02 +0000594 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595}
596
Nate Begeman83e75ec2005-09-06 04:43:02 +0000597SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000598 SDOperand N0 = N->getOperand(0);
599 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000601
602 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000603 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000604 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000606 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
608 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000609 TLI.getShiftAmountTy()));
610 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000611}
612
Nate Begeman83e75ec2005-09-06 04:43:02 +0000613SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000614 SDOperand N0 = N->getOperand(0);
615 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000616 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617
618 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000619 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000620 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000621 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000622 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000623 return DAG.getConstant(0, N0.getValueType());
624 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000625}
626
Nate Begeman83e75ec2005-09-06 04:43:02 +0000627SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000628 SDOperand N0 = N->getOperand(0);
629 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000630 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000631 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
632 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000633 MVT::ValueType VT = N1.getValueType();
Nate Begeman83e75ec2005-09-06 04:43:02 +0000634 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000635
636 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000637 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000638 return DAG.getConstant(N0C->getValue() & N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000639 // canonicalize constant to RHS
640 if (N0C && !N1C) {
641 std::swap(N0, N1);
642 std::swap(N0C, N1C);
643 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000644 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000645 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000646 return N0;
647 // if (and x, c) is known to be zero, return 0
648 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
649 return DAG.getConstant(0, VT);
650 // fold (and x, c) -> x iff (x & ~c) == 0
651 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
652 TLI))
653 return N0;
Nate Begeman223df222005-09-08 20:18:10 +0000654 // fold (and (and x, c1), c2) -> (and x, c1^c2)
655 if (N1C && N0.getOpcode() == ISD::AND) {
656 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
657 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
658 if (N00C)
659 return DAG.getNode(ISD::AND, VT, N0.getOperand(1),
660 DAG.getConstant(N1C->getValue()&N00C->getValue(), VT));
661 if (N01C)
662 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
663 DAG.getConstant(N1C->getValue()&N01C->getValue(), VT));
664 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000665 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
666 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
667 unsigned ExtendBits =
668 MVT::getSizeInBits(cast<VTSDNode>(N0.getOperand(1))->getVT());
Nate Begeman646d7e22005-09-02 21:18:40 +0000669 if ((N1C->getValue() & (~0ULL << ExtendBits)) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000670 return DAG.getNode(ISD::AND, VT, N0.getOperand(0), N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671 }
672 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
673 if (N0.getOpcode() == ISD::OR)
674 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +0000675 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000676 return N1;
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000677 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
678 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
679 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
680 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
681
682 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
683 MVT::isInteger(LL.getValueType())) {
684 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
685 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
686 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
687 WorkList.push_back(ORNode.Val);
688 return DAG.getSetCC(VT, ORNode, LR, Op1);
689 }
690 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
691 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
692 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
693 WorkList.push_back(ANDNode.Val);
694 return DAG.getSetCC(VT, ANDNode, LR, Op1);
695 }
696 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
697 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
698 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
699 WorkList.push_back(ORNode.Val);
700 return DAG.getSetCC(VT, ORNode, LR, Op1);
701 }
702 }
703 // canonicalize equivalent to ll == rl
704 if (LL == RR && LR == RL) {
705 Op1 = ISD::getSetCCSwappedOperands(Op1);
706 std::swap(RL, RR);
707 }
708 if (LL == RL && LR == RR) {
709 bool isInteger = MVT::isInteger(LL.getValueType());
710 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
711 if (Result != ISD::SETCC_INVALID)
712 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
713 }
714 }
715 // fold (and (zext x), (zext y)) -> (zext (and x, y))
716 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
717 N1.getOpcode() == ISD::ZERO_EXTEND &&
718 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
719 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
720 N0.getOperand(0), N1.getOperand(0));
721 WorkList.push_back(ANDNode.Val);
722 return DAG.getNode(ISD::ZERO_EXTEND, VT, ANDNode);
723 }
Nate Begeman452d7be2005-09-16 00:54:12 +0000724 // fold (and (shl/srl x), (shl/srl y)) -> (shl/srl (and x, y))
725 if (((N0.getOpcode() == ISD::SHL && N1.getOpcode() == ISD::SHL) ||
726 (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SRL)) &&
727 N0.getOperand(1) == N1.getOperand(1)) {
728 SDOperand ANDNode = DAG.getNode(ISD::AND, N0.getOperand(0).getValueType(),
729 N0.getOperand(0), N1.getOperand(0));
730 WorkList.push_back(ANDNode.Val);
731 return DAG.getNode(N0.getOpcode(), VT, ANDNode, N0.getOperand(1));
732 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000733 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000734}
735
Nate Begeman83e75ec2005-09-06 04:43:02 +0000736SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000737 SDOperand N0 = N->getOperand(0);
738 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000739 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +0000740 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
741 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000742 MVT::ValueType VT = N1.getValueType();
743 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000744
745 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000746 if (N0C && N1C)
747 return DAG.getConstant(N0C->getValue() | N1C->getValue(),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000748 N->getValueType(0));
Nate Begeman99801192005-09-07 23:25:52 +0000749 // canonicalize constant to RHS
750 if (N0C && !N1C) {
751 std::swap(N0, N1);
752 std::swap(N0C, N1C);
753 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000754 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000755 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000756 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000757 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000758 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000759 return N1;
760 // fold (or x, c) -> c iff (x & ~c) == 0
761 if (N1C && MaskedValueIsZero(N0,~N1C->getValue() & (~0ULL>>(64-OpSizeInBits)),
762 TLI))
763 return N1;
Nate Begeman223df222005-09-08 20:18:10 +0000764 // fold (or (or x, c1), c2) -> (or x, c1|c2)
765 if (N1C && N0.getOpcode() == ISD::OR) {
766 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
767 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
768 if (N00C)
769 return DAG.getNode(ISD::OR, VT, N0.getOperand(1),
770 DAG.getConstant(N1C->getValue()|N00C->getValue(), VT));
771 if (N01C)
772 return DAG.getNode(ISD::OR, VT, N0.getOperand(0),
773 DAG.getConstant(N1C->getValue()|N01C->getValue(), VT));
774 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000775 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
776 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
777 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
778 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
779
780 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
781 MVT::isInteger(LL.getValueType())) {
782 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
783 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
784 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
785 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
786 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
787 WorkList.push_back(ORNode.Val);
788 return DAG.getSetCC(VT, ORNode, LR, Op1);
789 }
790 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
791 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
792 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
793 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
794 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
795 WorkList.push_back(ANDNode.Val);
796 return DAG.getSetCC(VT, ANDNode, LR, Op1);
797 }
798 }
799 // canonicalize equivalent to ll == rl
800 if (LL == RR && LR == RL) {
801 Op1 = ISD::getSetCCSwappedOperands(Op1);
802 std::swap(RL, RR);
803 }
804 if (LL == RL && LR == RR) {
805 bool isInteger = MVT::isInteger(LL.getValueType());
806 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
807 if (Result != ISD::SETCC_INVALID)
808 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
809 }
810 }
811 // fold (or (zext x), (zext y)) -> (zext (or x, y))
812 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
813 N1.getOpcode() == ISD::ZERO_EXTEND &&
814 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
815 SDOperand ORNode = DAG.getNode(ISD::OR, N0.getOperand(0).getValueType(),
816 N0.getOperand(0), N1.getOperand(0));
817 WorkList.push_back(ORNode.Val);
818 return DAG.getNode(ISD::ZERO_EXTEND, VT, ORNode);
819 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000820 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000821}
822
Nate Begeman83e75ec2005-09-06 04:43:02 +0000823SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000824 SDOperand N0 = N->getOperand(0);
825 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000826 SDOperand LHS, RHS, CC;
827 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
828 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000829 MVT::ValueType VT = N0.getValueType();
830
831 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000832 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000833 return DAG.getConstant(N0C->getValue() ^ N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000834 // canonicalize constant to RHS
835 if (N0C && !N1C) {
836 std::swap(N0, N1);
837 std::swap(N0C, N1C);
838 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000839 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000840 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000841 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000842 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +0000843 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
844 bool isInt = MVT::isInteger(LHS.getValueType());
845 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
846 isInt);
847 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000848 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000849 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000850 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +0000851 assert(0 && "Unhandled SetCC Equivalent!");
852 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853 }
Nate Begeman99801192005-09-07 23:25:52 +0000854 // fold !(x or y) -> (!x and !y) iff x or y are setcc
855 if (N1C && N1C->getValue() == 1 &&
856 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000857 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000858 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
859 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000860 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
861 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000862 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
863 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000864 }
865 }
Nate Begeman99801192005-09-07 23:25:52 +0000866 // fold !(x or y) -> (!x and !y) iff x or y are constants
867 if (N1C && N1C->isAllOnesValue() &&
868 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +0000870 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
871 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000872 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
873 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Nate Begeman99801192005-09-07 23:25:52 +0000874 WorkList.push_back(LHS.Val); WorkList.push_back(RHS.Val);
875 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000876 }
877 }
Nate Begeman223df222005-09-08 20:18:10 +0000878 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
879 if (N1C && N0.getOpcode() == ISD::XOR) {
880 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
881 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
882 if (N00C)
883 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
884 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
885 if (N01C)
886 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
887 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
888 }
889 // fold (xor x, x) -> 0
890 if (N0 == N1)
891 return DAG.getConstant(0, VT);
Nate Begeman39ee1ac2005-09-09 19:49:52 +0000892 // fold (xor (zext x), (zext y)) -> (zext (xor x, y))
893 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
894 N1.getOpcode() == ISD::ZERO_EXTEND &&
895 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
896 SDOperand XORNode = DAG.getNode(ISD::XOR, N0.getOperand(0).getValueType(),
897 N0.getOperand(0), N1.getOperand(0));
898 WorkList.push_back(XORNode.Val);
899 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
900 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000901 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000902}
903
Nate Begeman83e75ec2005-09-06 04:43:02 +0000904SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000905 SDOperand N0 = N->getOperand(0);
906 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000907 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
908 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000909 MVT::ValueType VT = N0.getValueType();
910 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
911
912 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000913 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000914 return DAG.getConstant(N0C->getValue() << N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000915 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000916 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000917 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000918 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000919 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000920 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000921 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000922 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000923 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000924 // if (shl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +0000925 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
926 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000927 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000928 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000929 N0.getOperand(1).getOpcode() == ISD::Constant) {
930 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000931 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000932 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000935 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000936 }
937 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
938 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +0000939 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000940 N0.getOperand(1).getOpcode() == ISD::Constant) {
941 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +0000942 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000943 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
944 DAG.getConstant(~0ULL << c1, VT));
945 if (c2 > c1)
946 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +0000947 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000948 else
Nate Begeman83e75ec2005-09-06 04:43:02 +0000949 return DAG.getNode(ISD::SRL, VT, Mask,
950 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 }
952 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +0000953 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +0000954 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +0000955 DAG.getConstant(~0ULL << N1C->getValue(), VT));
956 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957}
958
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000960 SDOperand N0 = N->getOperand(0);
961 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000962 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
963 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000964 MVT::ValueType VT = N0.getValueType();
965 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
966
967 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000968 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000969 return DAG.getConstant(N0C->getSignExtended() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +0000971 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000972 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000973 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +0000974 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000975 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000976 // fold (sra x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +0000977 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000978 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000980 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000981 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000982 // If the sign bit is known to be zero, switch this to a SRL.
Nate Begeman646d7e22005-09-02 21:18:40 +0000983 if (N1C && MaskedValueIsZero(N0, (1ULL << (OpSizeInBits-1)), TLI))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000984 return DAG.getNode(ISD::SRL, VT, N0, N1);
985 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000986}
987
Nate Begeman83e75ec2005-09-06 04:43:02 +0000988SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000989 SDOperand N0 = N->getOperand(0);
990 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000991 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
992 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000993 MVT::ValueType VT = N0.getValueType();
994 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
995
996 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000997 if (N0C && N1C)
Nate Begeman83e75ec2005-09-06 04:43:02 +0000998 return DAG.getConstant(N0C->getValue() >> N1C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000999 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001000 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001001 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00001003 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001004 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001005 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001006 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001007 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001008 // if (srl x, c) is known to be zero, return 0
Nate Begeman83e75ec2005-09-06 04:43:02 +00001009 if (N1C && MaskedValueIsZero(SDOperand(N, 0), ~0ULL >> (64-OpSizeInBits),TLI))
1010 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001011 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00001012 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00001013 N0.getOperand(1).getOpcode() == ISD::Constant) {
1014 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00001015 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001017 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001018 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001019 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001020 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001021 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001022}
1023
Nate Begeman83e75ec2005-09-06 04:43:02 +00001024SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001025 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001026 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001027
1028 // fold (ctlz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001029 if (N0C)
1030 return DAG.getConstant(CountLeadingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001031 N0.getValueType());
1032 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001033}
1034
Nate Begeman83e75ec2005-09-06 04:43:02 +00001035SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001036 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038
1039 // fold (cttz c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001040 if (N0C)
1041 return DAG.getConstant(CountTrailingZeros_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001042 N0.getValueType());
1043 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001044}
1045
Nate Begeman83e75ec2005-09-06 04:43:02 +00001046SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001047 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001048 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001049
1050 // fold (ctpop c1) -> c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001051 if (N0C)
1052 return DAG.getConstant(CountPopulation_64(N0C->getValue()),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001053 N0.getValueType());
1054 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001055}
1056
Nate Begeman452d7be2005-09-16 00:54:12 +00001057SDOperand DAGCombiner::visitSELECT(SDNode *N) {
1058 SDOperand N0 = N->getOperand(0);
1059 SDOperand N1 = N->getOperand(1);
1060 SDOperand N2 = N->getOperand(2);
1061 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1062 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1063 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1064 MVT::ValueType VT = N->getValueType(0);
Nate Begeman44728a72005-09-19 22:34:01 +00001065
Nate Begeman452d7be2005-09-16 00:54:12 +00001066 // fold select C, X, X -> X
1067 if (N1 == N2)
1068 return N1;
1069 // fold select true, X, Y -> X
1070 if (N0C && !N0C->isNullValue())
1071 return N1;
1072 // fold select false, X, Y -> Y
1073 if (N0C && N0C->isNullValue())
1074 return N2;
1075 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001076 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00001077 return DAG.getNode(ISD::OR, VT, N0, N2);
1078 // fold select C, 0, X -> ~C & X
1079 // FIXME: this should check for C type == X type, not i1?
1080 if (MVT::i1 == VT && N1C && N1C->isNullValue()) {
1081 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1082 WorkList.push_back(XORNode.Val);
1083 return DAG.getNode(ISD::AND, VT, XORNode, N2);
1084 }
1085 // fold select C, X, 1 -> ~C | X
Nate Begeman44728a72005-09-19 22:34:01 +00001086 if (MVT::i1 == VT && N2C && N2C->getValue() == 1) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001087 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
1088 WorkList.push_back(XORNode.Val);
1089 return DAG.getNode(ISD::OR, VT, XORNode, N1);
1090 }
1091 // fold select C, X, 0 -> C & X
1092 // FIXME: this should check for C type == X type, not i1?
1093 if (MVT::i1 == VT && N2C && N2C->isNullValue())
1094 return DAG.getNode(ISD::AND, VT, N0, N1);
1095 // fold X ? X : Y --> X ? 1 : Y --> X | Y
1096 if (MVT::i1 == VT && N0 == N1)
1097 return DAG.getNode(ISD::OR, VT, N0, N2);
1098 // fold X ? Y : X --> X ? Y : 0 --> X & Y
1099 if (MVT::i1 == VT && N0 == N2)
1100 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman44728a72005-09-19 22:34:01 +00001101 // fold selects based on a setcc into other things, such as min/max/abs
1102 if (N0.getOpcode() == ISD::SETCC)
1103 return SimplifySelect(N0, N1, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00001104 return SDOperand();
1105}
1106
1107SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00001108 SDOperand N0 = N->getOperand(0);
1109 SDOperand N1 = N->getOperand(1);
1110 SDOperand N2 = N->getOperand(2);
1111 SDOperand N3 = N->getOperand(3);
1112 SDOperand N4 = N->getOperand(4);
1113 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1114 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1115 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
1116 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
1117
1118 // Determine if the condition we're dealing with is constant
Nate Begemane17daeb2005-10-05 21:43:42 +00001119 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner91559022005-10-05 04:45:43 +00001120 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1121
Nate Begeman44728a72005-09-19 22:34:01 +00001122 // fold select_cc lhs, rhs, x, x, cc -> x
1123 if (N2 == N3)
1124 return N2;
Nate Begeman44728a72005-09-19 22:34:01 +00001125 // fold select_cc into other things, such as min/max/abs
1126 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00001127}
1128
1129SDOperand DAGCombiner::visitSETCC(SDNode *N) {
1130 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
1131 cast<CondCodeSDNode>(N->getOperand(2))->get());
1132}
1133
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001137 MVT::ValueType VT = N->getValueType(0);
1138
Nate Begeman1d4d4142005-09-01 00:19:25 +00001139 // fold (sext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001141 return DAG.getConstant(N0C->getSignExtended(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001142 // fold (sext (sext x)) -> (sext x)
1143 if (N0.getOpcode() == ISD::SIGN_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001144 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
1145 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001146}
1147
Nate Begeman83e75ec2005-09-06 04:43:02 +00001148SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001149 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001150 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001151 MVT::ValueType VT = N->getValueType(0);
1152
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153 // fold (zext c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001154 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001155 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156 // fold (zext (zext x)) -> (zext x)
1157 if (N0.getOpcode() == ISD::ZERO_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001158 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
1159 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160}
1161
Nate Begeman83e75ec2005-09-06 04:43:02 +00001162SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001163 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001164 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001165 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001167 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169 // fold (sext_in_reg c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 if (N0C) {
1171 SDOperand Truncate = DAG.getConstant(N0C->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001172 return DAG.getNode(ISD::SIGN_EXTEND, VT, Truncate);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001173 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt1
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 cast<VTSDNode>(N0.getOperand(1))->getVT() < EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 }
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
1180 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1181 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001183 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00001184 // fold (sext_in_reg (assert_sext x)) -> (assert_sext x)
1185 if (N0.getOpcode() == ISD::AssertSext &&
1186 cast<VTSDNode>(N0.getOperand(1))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001187 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001188 }
1189 // fold (sext_in_reg (sextload x)) -> (sextload x)
1190 if (N0.getOpcode() == ISD::SEXTLOAD &&
1191 cast<VTSDNode>(N0.getOperand(3))->getVT() <= EVT) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00001192 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001193 }
Nate Begeman4ebd8052005-09-01 23:24:04 +00001194 // fold (sext_in_reg (setcc x)) -> setcc x iff (setcc x) == 0 or -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001195 // FIXME: teach isSetCCEquivalent about 0, -1 and then use it here
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 if (N0.getOpcode() == ISD::SETCC &&
1197 TLI.getSetCCResultContents() ==
1198 TargetLowering::ZeroOrNegativeOneSetCCResult)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001199 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001200 // FIXME: this code is currently just ported over from SelectionDAG.cpp
1201 // we probably actually want to handle this in two pieces. Rather than
1202 // checking all the top bits for zero, just check the sign bit here and turn
1203 // it into a zero extend inreg (AND with constant).
1204 // then, let the code for AND figure out if the mask is superfluous rather
1205 // than doing so here.
1206 if (N0.getOpcode() == ISD::AND &&
1207 N0.getOperand(1).getOpcode() == ISD::Constant) {
1208 uint64_t Mask = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
1209 unsigned NumBits = MVT::getSizeInBits(EVT);
1210 if ((Mask & (~0ULL << (NumBits-1))) == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001211 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001212 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001213 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214}
1215
Nate Begeman83e75ec2005-09-06 04:43:02 +00001216SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001217 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001218 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219 MVT::ValueType VT = N->getValueType(0);
1220
1221 // noop truncate
1222 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001223 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001224 // fold (truncate c1) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001225 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001226 return DAG.getConstant(N0C->getValue(), VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227 // fold (truncate (truncate x)) -> (truncate x)
1228 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001229 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001230 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
1231 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND){
1232 if (N0.getValueType() < VT)
1233 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00001234 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001235 else if (N0.getValueType() > VT)
1236 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238 else
1239 // if the source and dest are the same type, we can drop both the extend
1240 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00001241 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001242 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001243 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244}
1245
Chris Lattner01b3d732005-09-28 22:28:18 +00001246SDOperand DAGCombiner::visitFADD(SDNode *N) {
1247 SDOperand N0 = N->getOperand(0);
1248 SDOperand N1 = N->getOperand(1);
1249 MVT::ValueType VT = N->getValueType(0);
1250
1251 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1252 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1253 // fold floating point (fadd c1, c2)
1254 return DAG.getConstantFP(N0CFP->getValue() + N1CFP->getValue(),
1255 N->getValueType(0));
1256 }
1257 // fold (A + (-B)) -> A-B
1258 if (N1.getOpcode() == ISD::FNEG)
1259 return DAG.getNode(ISD::FSUB, VT, N0, N1.getOperand(0));
1260
1261 // fold ((-A) + B) -> B-A
1262 if (N0.getOpcode() == ISD::FNEG)
1263 return DAG.getNode(ISD::FSUB, VT, N1, N0.getOperand(0));
1264
1265 return SDOperand();
1266}
1267
1268SDOperand DAGCombiner::visitFSUB(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 (fsub c1, c2)
1276 return DAG.getConstantFP(N0CFP->getValue() - N1CFP->getValue(),
1277 N->getValueType(0));
1278 }
1279 // fold (A-(-B)) -> A+B
1280 if (N1.getOpcode() == ISD::FNEG)
1281 return DAG.getNode(ISD::FADD, N0.getValueType(), N0, N1.getOperand(0));
1282
1283 return SDOperand();
1284}
1285
1286SDOperand DAGCombiner::visitFMUL(SDNode *N) {
1287 SDOperand N0 = N->getOperand(0);
1288 SDOperand N1 = N->getOperand(1);
1289 MVT::ValueType VT = N->getValueType(0);
1290
1291 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1292 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1293 // fold floating point (fmul c1, c2)
1294 return DAG.getConstantFP(N0CFP->getValue() * N1CFP->getValue(),
1295 N->getValueType(0));
1296 }
1297 return SDOperand();
1298}
1299
1300SDOperand DAGCombiner::visitFDIV(SDNode *N) {
1301 SDOperand N0 = N->getOperand(0);
1302 SDOperand N1 = N->getOperand(1);
1303 MVT::ValueType VT = N->getValueType(0);
1304
1305 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1306 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1307 // fold floating point (fdiv c1, c2)
1308 return DAG.getConstantFP(N0CFP->getValue() / N1CFP->getValue(),
1309 N->getValueType(0));
1310 }
1311 return SDOperand();
1312}
1313
1314SDOperand DAGCombiner::visitFREM(SDNode *N) {
1315 SDOperand N0 = N->getOperand(0);
1316 SDOperand N1 = N->getOperand(1);
1317 MVT::ValueType VT = N->getValueType(0);
1318
1319 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0))
1320 if (ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1321 // fold floating point (frem c1, c2) -> fmod(c1, c2)
1322 return DAG.getConstantFP(fmod(N0CFP->getValue(),N1CFP->getValue()),
1323 N->getValueType(0));
1324 }
1325 return SDOperand();
1326}
1327
1328
Nate Begeman83e75ec2005-09-06 04:43:02 +00001329SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001331 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332
1333 // fold (sint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001334 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335 return DAG.getConstantFP(N0C->getSignExtended(), N->getValueType(0));
1336 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337}
1338
Nate Begeman83e75ec2005-09-06 04:43:02 +00001339SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342
1343 // fold (uint_to_fp c1) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001344 if (N0C)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001345 return DAG.getConstantFP(N0C->getValue(), N->getValueType(0));
1346 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347}
1348
Nate Begeman83e75ec2005-09-06 04:43:02 +00001349SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001350 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001351
1352 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001354 return DAG.getConstant((int64_t)N0CFP->getValue(), N->getValueType(0));
1355 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356}
1357
Nate Begeman83e75ec2005-09-06 04:43:02 +00001358SDOperand DAGCombiner::visitFP_TO_UINT(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_to_uint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001362 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return DAG.getConstant((uint64_t)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::visitFP_ROUND(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
1370 // fold (fp_round c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001372 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1373 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001374}
1375
Nate Begeman83e75ec2005-09-06 04:43:02 +00001376SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377 SDOperand N0 = N->getOperand(0);
1378 MVT::ValueType VT = N->getValueType(0);
1379 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00001380 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001381
Nate Begeman1d4d4142005-09-01 00:19:25 +00001382 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001383 if (N0CFP) {
1384 SDOperand Round = DAG.getConstantFP(N0CFP->getValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001385 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001386 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00001387 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388}
1389
Nate Begeman83e75ec2005-09-06 04:43:02 +00001390SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001391 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392
1393 // fold (fp_extend c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00001394 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001395 return DAG.getConstantFP(N0CFP->getValue(), N->getValueType(0));
1396 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397}
1398
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001400 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001401 // fold (neg c1) -> -c1
Nate Begeman646d7e22005-09-02 21:18:40 +00001402 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001403 return DAG.getConstantFP(-N0CFP->getValue(), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 // fold (neg (sub x, y)) -> (sub y, x)
1405 if (N->getOperand(0).getOpcode() == ISD::SUB)
1406 return DAG.getNode(ISD::SUB, N->getValueType(0), N->getOperand(1),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001407 N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 // fold (neg (neg x)) -> x
1409 if (N->getOperand(0).getOpcode() == ISD::FNEG)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001410 return N->getOperand(0).getOperand(0);
1411 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001412}
1413
Nate Begeman83e75ec2005-09-06 04:43:02 +00001414SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begeman646d7e22005-09-02 21:18:40 +00001415 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001416 // fold (fabs c1) -> fabs(c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001417 if (N0CFP)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001418 return DAG.getConstantFP(fabs(N0CFP->getValue()), N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001419 // fold (fabs (fabs x)) -> (fabs x)
1420 if (N->getOperand(0).getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001421 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001422 // fold (fabs (fneg x)) -> (fabs x)
1423 if (N->getOperand(0).getOpcode() == ISD::FNEG)
1424 return DAG.getNode(ISD::FABS, N->getValueType(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001425 N->getOperand(0).getOperand(0));
1426 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001427}
1428
Nate Begeman44728a72005-09-19 22:34:01 +00001429SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
1430 SDOperand Chain = N->getOperand(0);
1431 SDOperand N1 = N->getOperand(1);
1432 SDOperand N2 = N->getOperand(2);
1433 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1434
1435 // never taken branch, fold to chain
1436 if (N1C && N1C->isNullValue())
1437 return Chain;
1438 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00001439 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00001440 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1441 return SDOperand();
1442}
1443
1444SDOperand DAGCombiner::visitBRCONDTWOWAY(SDNode *N) {
1445 SDOperand Chain = N->getOperand(0);
1446 SDOperand N1 = N->getOperand(1);
1447 SDOperand N2 = N->getOperand(2);
1448 SDOperand N3 = N->getOperand(3);
1449 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1450
1451 // unconditional branch to true mbb
1452 if (N1C && N1C->getValue() == 1)
1453 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
1454 // unconditional branch to false mbb
1455 if (N1C && N1C->isNullValue())
1456 return DAG.getNode(ISD::BR, MVT::Other, Chain, N3);
1457 return SDOperand();
1458}
1459
Chris Lattner3ea0b472005-10-05 06:47:48 +00001460// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
1461//
Nate Begeman44728a72005-09-19 22:34:01 +00001462SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00001463 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
1464 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
1465
1466 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00001467 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
1468 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
1469
1470 // fold br_cc true, dest -> br dest (unconditional branch)
1471 if (SCCC && SCCC->getValue())
1472 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
1473 N->getOperand(4));
1474 // fold br_cc false, dest -> unconditional fall through
1475 if (SCCC && SCCC->isNullValue())
1476 return N->getOperand(0);
1477 // fold to a simpler setcc
1478 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
1479 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
1480 Simp.getOperand(2), Simp.getOperand(0),
1481 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00001482 return SDOperand();
1483}
1484
1485SDOperand DAGCombiner::visitBRTWOWAY_CC(SDNode *N) {
Nate Begemane17daeb2005-10-05 21:43:42 +00001486 SDOperand Chain = N->getOperand(0);
1487 SDOperand CCN = N->getOperand(1);
1488 SDOperand LHS = N->getOperand(2);
1489 SDOperand RHS = N->getOperand(3);
1490 SDOperand N4 = N->getOperand(4);
1491 SDOperand N5 = N->getOperand(5);
1492
1493 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), LHS, RHS,
1494 cast<CondCodeSDNode>(CCN)->get(), false);
1495 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1496
1497 // fold select_cc lhs, rhs, x, x, cc -> x
1498 if (N4 == N5)
1499 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1500 // fold select_cc true, x, y -> x
1501 if (SCCC && SCCC->getValue())
1502 return DAG.getNode(ISD::BR, MVT::Other, Chain, N4);
1503 // fold select_cc false, x, y -> y
1504 if (SCCC && SCCC->isNullValue())
1505 return DAG.getNode(ISD::BR, MVT::Other, Chain, N5);
1506 // fold to a simpler setcc
1507 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
1508 return DAG.getBR2Way_CC(Chain, SCC.getOperand(2), SCC.getOperand(0),
1509 SCC.getOperand(1), N4, N5);
Nate Begeman44728a72005-09-19 22:34:01 +00001510 return SDOperand();
1511}
1512
1513SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00001514 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
1515
1516 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
1517 cast<CondCodeSDNode>(N0.getOperand(2))->get());
1518 // If we got a simplified select_cc node back from SimplifySelectCC, then
1519 // break it down into a new SETCC node, and a new SELECT node, and then return
1520 // the SELECT node, since we were called with a SELECT node.
1521 if (SCC.Val) {
1522 // Check to see if we got a select_cc back (to turn into setcc/select).
1523 // Otherwise, just return whatever node we got back, like fabs.
1524 if (SCC.getOpcode() == ISD::SELECT_CC) {
1525 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
1526 SCC.getOperand(0), SCC.getOperand(1),
1527 SCC.getOperand(4));
1528 WorkList.push_back(SETCC.Val);
1529 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
1530 SCC.getOperand(3), SETCC);
1531 }
1532 return SCC;
1533 }
Nate Begeman44728a72005-09-19 22:34:01 +00001534 return SDOperand();
1535}
1536
1537SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
1538 SDOperand N2, SDOperand N3,
1539 ISD::CondCode CC) {
Nate Begemanf845b452005-10-08 00:29:44 +00001540
1541 MVT::ValueType VT = N2.getValueType();
1542 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1543 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1544 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1545 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1546
1547 // Determine if the condition we're dealing with is constant
1548 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
1549 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
1550
1551 // fold select_cc true, x, y -> x
1552 if (SCCC && SCCC->getValue())
1553 return N2;
1554 // fold select_cc false, x, y -> y
1555 if (SCCC && SCCC->getValue() == 0)
1556 return N3;
1557
1558 // Check to see if we can simplify the select into an fabs node
1559 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
1560 // Allow either -0.0 or 0.0
1561 if (CFP->getValue() == 0.0) {
1562 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1563 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
1564 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
1565 N2 == N3.getOperand(0))
1566 return DAG.getNode(ISD::FABS, VT, N0);
1567
1568 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1569 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
1570 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
1571 N2.getOperand(0) == N3)
1572 return DAG.getNode(ISD::FABS, VT, N3);
1573 }
1574 }
1575
1576 // Check to see if we can perform the "gzip trick", transforming
1577 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
1578 if (N1C && N1C->isNullValue() && N3C && N3C->isNullValue() &&
1579 MVT::isInteger(N0.getValueType()) &&
1580 MVT::isInteger(N2.getValueType()) && CC == ISD::SETLT) {
1581 MVT::ValueType XType = N0.getValueType();
1582 MVT::ValueType AType = N2.getValueType();
1583 if (XType >= AType) {
1584 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1585 // single-bit constant. FIXME: remove once the dag combiner
1586 // exists.
1587 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
1588 unsigned ShCtV = Log2_64(N2C->getValue());
1589 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1590 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
1591 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
1592 WorkList.push_back(Shift.Val);
1593 if (XType > AType) {
1594 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1595 WorkList.push_back(Shift.Val);
1596 }
1597 return DAG.getNode(ISD::AND, AType, Shift, N2);
1598 }
1599 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1600 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1601 TLI.getShiftAmountTy()));
1602 WorkList.push_back(Shift.Val);
1603 if (XType > AType) {
1604 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
1605 WorkList.push_back(Shift.Val);
1606 }
1607 return DAG.getNode(ISD::AND, AType, Shift, N2);
1608 }
1609 }
1610
1611 // Check to see if this is the equivalent of setcc
1612 // FIXME: Turn all of these into setcc if setcc if setcc is legal
1613 // otherwise, go ahead with the folds.
1614 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
1615 MVT::ValueType XType = N0.getValueType();
1616 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
1617 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
1618 if (Res.getValueType() != VT)
1619 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
1620 return Res;
1621 }
1622
1623 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
1624 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
1625 TLI.isOperationLegal(ISD::CTLZ, XType)) {
1626 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
1627 return DAG.getNode(ISD::SRL, XType, Ctlz,
1628 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
1629 TLI.getShiftAmountTy()));
1630 }
1631 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
1632 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
1633 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
1634 N0);
1635 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
1636 DAG.getConstant(~0ULL, XType));
1637 return DAG.getNode(ISD::SRL, XType,
1638 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
1639 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1640 TLI.getShiftAmountTy()));
1641 }
1642 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1643 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
1644 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
1645 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1646 TLI.getShiftAmountTy()));
1647 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
1648 }
1649 }
1650
1651 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1652 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1653 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1654 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1)) {
1655 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0))) {
1656 MVT::ValueType XType = N0.getValueType();
1657 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1658 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
1659 DAG.getConstant(MVT::getSizeInBits(XType)-1,
1660 TLI.getShiftAmountTy()));
1661 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
1662 WorkList.push_back(Shift.Val);
1663 WorkList.push_back(Add.Val);
1664 return DAG.getNode(ISD::XOR, XType, Add, Shift);
1665 }
1666 }
1667 }
1668
Nate Begeman44728a72005-09-19 22:34:01 +00001669 return SDOperand();
1670}
1671
Nate Begeman452d7be2005-09-16 00:54:12 +00001672SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00001673 SDOperand N1, ISD::CondCode Cond,
1674 bool foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001675 // These setcc operations always fold.
1676 switch (Cond) {
1677 default: break;
1678 case ISD::SETFALSE:
1679 case ISD::SETFALSE2: return DAG.getConstant(0, VT);
1680 case ISD::SETTRUE:
1681 case ISD::SETTRUE2: return DAG.getConstant(1, VT);
1682 }
1683
1684 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1685 uint64_t C1 = N1C->getValue();
1686 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val)) {
1687 uint64_t C0 = N0C->getValue();
1688
1689 // Sign extend the operands if required
1690 if (ISD::isSignedIntSetCC(Cond)) {
1691 C0 = N0C->getSignExtended();
1692 C1 = N1C->getSignExtended();
1693 }
1694
1695 switch (Cond) {
1696 default: assert(0 && "Unknown integer setcc!");
1697 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1698 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1699 case ISD::SETULT: return DAG.getConstant(C0 < C1, VT);
1700 case ISD::SETUGT: return DAG.getConstant(C0 > C1, VT);
1701 case ISD::SETULE: return DAG.getConstant(C0 <= C1, VT);
1702 case ISD::SETUGE: return DAG.getConstant(C0 >= C1, VT);
1703 case ISD::SETLT: return DAG.getConstant((int64_t)C0 < (int64_t)C1, VT);
1704 case ISD::SETGT: return DAG.getConstant((int64_t)C0 > (int64_t)C1, VT);
1705 case ISD::SETLE: return DAG.getConstant((int64_t)C0 <= (int64_t)C1, VT);
1706 case ISD::SETGE: return DAG.getConstant((int64_t)C0 >= (int64_t)C1, VT);
1707 }
1708 } else {
1709 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
1710 if (N0.getOpcode() == ISD::ZERO_EXTEND) {
1711 unsigned InSize = MVT::getSizeInBits(N0.getOperand(0).getValueType());
1712
1713 // If the comparison constant has bits in the upper part, the
1714 // zero-extended value could never match.
1715 if (C1 & (~0ULL << InSize)) {
1716 unsigned VSize = MVT::getSizeInBits(N0.getValueType());
1717 switch (Cond) {
1718 case ISD::SETUGT:
1719 case ISD::SETUGE:
1720 case ISD::SETEQ: return DAG.getConstant(0, VT);
1721 case ISD::SETULT:
1722 case ISD::SETULE:
1723 case ISD::SETNE: return DAG.getConstant(1, VT);
1724 case ISD::SETGT:
1725 case ISD::SETGE:
1726 // True if the sign bit of C1 is set.
1727 return DAG.getConstant((C1 & (1ULL << VSize)) != 0, VT);
1728 case ISD::SETLT:
1729 case ISD::SETLE:
1730 // True if the sign bit of C1 isn't set.
1731 return DAG.getConstant((C1 & (1ULL << VSize)) == 0, VT);
1732 default:
1733 break;
1734 }
1735 }
1736
1737 // Otherwise, we can perform the comparison with the low bits.
1738 switch (Cond) {
1739 case ISD::SETEQ:
1740 case ISD::SETNE:
1741 case ISD::SETUGT:
1742 case ISD::SETUGE:
1743 case ISD::SETULT:
1744 case ISD::SETULE:
1745 return DAG.getSetCC(VT, N0.getOperand(0),
1746 DAG.getConstant(C1, N0.getOperand(0).getValueType()),
1747 Cond);
1748 default:
1749 break; // todo, be more careful with signed comparisons
1750 }
1751 } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1752 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
1753 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
1754 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
1755 MVT::ValueType ExtDstTy = N0.getValueType();
1756 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
1757
1758 // If the extended part has any inconsistent bits, it cannot ever
1759 // compare equal. In other words, they have to be all ones or all
1760 // zeros.
1761 uint64_t ExtBits =
1762 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
1763 if ((C1 & ExtBits) != 0 && (C1 & ExtBits) != ExtBits)
1764 return DAG.getConstant(Cond == ISD::SETNE, VT);
1765
1766 SDOperand ZextOp;
1767 MVT::ValueType Op0Ty = N0.getOperand(0).getValueType();
1768 if (Op0Ty == ExtSrcTy) {
1769 ZextOp = N0.getOperand(0);
1770 } else {
1771 int64_t Imm = ~0ULL >> (64-ExtSrcTyBits);
1772 ZextOp = DAG.getNode(ISD::AND, Op0Ty, N0.getOperand(0),
1773 DAG.getConstant(Imm, Op0Ty));
1774 }
1775 WorkList.push_back(ZextOp.Val);
1776 // Otherwise, make this a use of a zext.
1777 return DAG.getSetCC(VT, ZextOp,
1778 DAG.getConstant(C1 & (~0ULL>>(64-ExtSrcTyBits)),
1779 ExtDstTy),
1780 Cond);
1781 }
Chris Lattner5c46f742005-10-05 06:11:08 +00001782
Nate Begeman452d7be2005-09-16 00:54:12 +00001783 uint64_t MinVal, MaxVal;
1784 unsigned OperandBitSize = MVT::getSizeInBits(N1C->getValueType(0));
1785 if (ISD::isSignedIntSetCC(Cond)) {
1786 MinVal = 1ULL << (OperandBitSize-1);
1787 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
1788 MaxVal = ~0ULL >> (65-OperandBitSize);
1789 else
1790 MaxVal = 0;
1791 } else {
1792 MinVal = 0;
1793 MaxVal = ~0ULL >> (64-OperandBitSize);
1794 }
1795
1796 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
1797 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
1798 if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
1799 --C1; // X >= C0 --> X > (C0-1)
1800 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1801 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
1802 }
1803
1804 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
1805 if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
1806 ++C1; // X <= C0 --> X < (C0+1)
1807 return DAG.getSetCC(VT, N0, DAG.getConstant(C1, N1.getValueType()),
1808 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
1809 }
1810
1811 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
1812 return DAG.getConstant(0, VT); // X < MIN --> false
1813
1814 // Canonicalize setgt X, Min --> setne X, Min
1815 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
1816 return DAG.getSetCC(VT, N0, N1, ISD::SETNE);
1817
1818 // If we have setult X, 1, turn it into seteq X, 0
1819 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
1820 return DAG.getSetCC(VT, N0, DAG.getConstant(MinVal, N0.getValueType()),
1821 ISD::SETEQ);
1822 // If we have setugt X, Max-1, turn it into seteq X, Max
1823 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
1824 return DAG.getSetCC(VT, N0, DAG.getConstant(MaxVal, N0.getValueType()),
1825 ISD::SETEQ);
1826
1827 // If we have "setcc X, C0", check to see if we can shrink the immediate
1828 // by changing cc.
1829
1830 // SETUGT X, SINTMAX -> SETLT X, 0
1831 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1832 C1 == (~0ULL >> (65-OperandBitSize)))
1833 return DAG.getSetCC(VT, N0, DAG.getConstant(0, N1.getValueType()),
1834 ISD::SETLT);
1835
1836 // FIXME: Implement the rest of these.
1837
1838 // Fold bit comparisons when we can.
1839 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1840 VT == N0.getValueType() && N0.getOpcode() == ISD::AND)
1841 if (ConstantSDNode *AndRHS =
1842 dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
1843 if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1844 // Perform the xform if the AND RHS is a single bit.
1845 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1846 return DAG.getNode(ISD::SRL, VT, N0,
1847 DAG.getConstant(Log2_64(AndRHS->getValue()),
1848 TLI.getShiftAmountTy()));
1849 }
1850 } else if (Cond == ISD::SETEQ && C1 == AndRHS->getValue()) {
1851 // (X & 8) == 8 --> (X & 8) >> 3
1852 // Perform the xform if C1 is a single bit.
1853 if ((C1 & (C1-1)) == 0) {
1854 return DAG.getNode(ISD::SRL, VT, N0,
1855 DAG.getConstant(Log2_64(C1),TLI.getShiftAmountTy()));
1856 }
1857 }
1858 }
1859 }
1860 } else if (isa<ConstantSDNode>(N0.Val)) {
1861 // Ensure that the constant occurs on the RHS.
1862 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1863 }
1864
1865 if (ConstantFPSDNode *N0C = dyn_cast<ConstantFPSDNode>(N0.Val))
1866 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) {
1867 double C0 = N0C->getValue(), C1 = N1C->getValue();
1868
1869 switch (Cond) {
1870 default: break; // FIXME: Implement the rest of these!
1871 case ISD::SETEQ: return DAG.getConstant(C0 == C1, VT);
1872 case ISD::SETNE: return DAG.getConstant(C0 != C1, VT);
1873 case ISD::SETLT: return DAG.getConstant(C0 < C1, VT);
1874 case ISD::SETGT: return DAG.getConstant(C0 > C1, VT);
1875 case ISD::SETLE: return DAG.getConstant(C0 <= C1, VT);
1876 case ISD::SETGE: return DAG.getConstant(C0 >= C1, VT);
1877 }
1878 } else {
1879 // Ensure that the constant occurs on the RHS.
1880 return DAG.getSetCC(VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
1881 }
1882
1883 if (N0 == N1) {
1884 // We can always fold X == Y for integer setcc's.
1885 if (MVT::isInteger(N0.getValueType()))
1886 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1887 unsigned UOF = ISD::getUnorderedFlavor(Cond);
1888 if (UOF == 2) // FP operators that are undefined on NaNs.
1889 return DAG.getConstant(ISD::isTrueWhenEqual(Cond), VT);
1890 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
1891 return DAG.getConstant(UOF, VT);
1892 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
1893 // if it is not already.
1894 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
1895 if (NewCond != Cond)
1896 return DAG.getSetCC(VT, N0, N1, NewCond);
1897 }
1898
1899 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1900 MVT::isInteger(N0.getValueType())) {
1901 if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
1902 N0.getOpcode() == ISD::XOR) {
1903 // Simplify (X+Y) == (X+Z) --> Y == Z
1904 if (N0.getOpcode() == N1.getOpcode()) {
1905 if (N0.getOperand(0) == N1.getOperand(0))
1906 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1907 if (N0.getOperand(1) == N1.getOperand(1))
1908 return DAG.getSetCC(VT, N0.getOperand(0), N1.getOperand(0), Cond);
1909 if (isCommutativeBinOp(N0.getOpcode())) {
1910 // If X op Y == Y op X, try other combinations.
1911 if (N0.getOperand(0) == N1.getOperand(1))
1912 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(0), Cond);
1913 if (N0.getOperand(1) == N1.getOperand(0))
1914 return DAG.getSetCC(VT, N0.getOperand(1), N1.getOperand(1), Cond);
1915 }
1916 }
1917
Chris Lattner5c46f742005-10-05 06:11:08 +00001918 // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0. Common for condcodes.
1919 if (N0.getOpcode() == ISD::XOR)
1920 if (ConstantSDNode *XORC = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
1921 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
1922 // If we know that all of the inverted bits are zero, don't bother
1923 // performing the inversion.
1924 if (MaskedValueIsZero(N0.getOperand(0), ~XORC->getValue(), TLI))
1925 return DAG.getSetCC(VT, N0.getOperand(0),
1926 DAG.getConstant(XORC->getValue()^RHSC->getValue(),
1927 N0.getValueType()), Cond);
1928 }
1929
Nate Begeman452d7be2005-09-16 00:54:12 +00001930 // Simplify (X+Z) == X --> Z == 0
1931 if (N0.getOperand(0) == N1)
1932 return DAG.getSetCC(VT, N0.getOperand(1),
1933 DAG.getConstant(0, N0.getValueType()), Cond);
1934 if (N0.getOperand(1) == N1) {
1935 if (isCommutativeBinOp(N0.getOpcode()))
1936 return DAG.getSetCC(VT, N0.getOperand(0),
1937 DAG.getConstant(0, N0.getValueType()), Cond);
1938 else {
1939 assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
1940 // (Z-X) == X --> Z == X<<1
1941 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(),
1942 N1,
1943 DAG.getConstant(1,TLI.getShiftAmountTy()));
1944 WorkList.push_back(SH.Val);
1945 return DAG.getSetCC(VT, N0.getOperand(0), SH, Cond);
1946 }
1947 }
1948 }
1949
1950 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
1951 N1.getOpcode() == ISD::XOR) {
1952 // Simplify X == (X+Z) --> Z == 0
1953 if (N1.getOperand(0) == N0) {
1954 return DAG.getSetCC(VT, N1.getOperand(1),
1955 DAG.getConstant(0, N1.getValueType()), Cond);
1956 } else if (N1.getOperand(1) == N0) {
1957 if (isCommutativeBinOp(N1.getOpcode())) {
1958 return DAG.getSetCC(VT, N1.getOperand(0),
1959 DAG.getConstant(0, N1.getValueType()), Cond);
1960 } else {
1961 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
1962 // X == (Z-X) --> X<<1 == Z
1963 SDOperand SH = DAG.getNode(ISD::SHL, N1.getValueType(), N0,
1964 DAG.getConstant(1,TLI.getShiftAmountTy()));
1965 WorkList.push_back(SH.Val);
1966 return DAG.getSetCC(VT, SH, N1.getOperand(0), Cond);
1967 }
1968 }
1969 }
1970 }
1971
1972 // Fold away ALL boolean setcc's.
1973 SDOperand Temp;
Nate Begemane17daeb2005-10-05 21:43:42 +00001974 if (N0.getValueType() == MVT::i1 && foldBooleans) {
Nate Begeman452d7be2005-09-16 00:54:12 +00001975 switch (Cond) {
1976 default: assert(0 && "Unknown integer setcc!");
1977 case ISD::SETEQ: // X == Y -> (X^Y)^1
1978 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1979 N0 = DAG.getNode(ISD::XOR, MVT::i1, Temp, DAG.getConstant(1, MVT::i1));
1980 WorkList.push_back(Temp.Val);
1981 break;
1982 case ISD::SETNE: // X != Y --> (X^Y)
1983 N0 = DAG.getNode(ISD::XOR, MVT::i1, N0, N1);
1984 break;
1985 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
1986 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
1987 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
1988 N0 = DAG.getNode(ISD::AND, MVT::i1, N1, Temp);
1989 WorkList.push_back(Temp.Val);
1990 break;
1991 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
1992 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
1993 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
1994 N0 = DAG.getNode(ISD::AND, MVT::i1, N0, Temp);
1995 WorkList.push_back(Temp.Val);
1996 break;
1997 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
1998 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
1999 Temp = DAG.getNode(ISD::XOR, MVT::i1, N0, DAG.getConstant(1, MVT::i1));
2000 N0 = DAG.getNode(ISD::OR, MVT::i1, N1, Temp);
2001 WorkList.push_back(Temp.Val);
2002 break;
2003 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
2004 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
2005 Temp = DAG.getNode(ISD::XOR, MVT::i1, N1, DAG.getConstant(1, MVT::i1));
2006 N0 = DAG.getNode(ISD::OR, MVT::i1, N0, Temp);
2007 break;
2008 }
2009 if (VT != MVT::i1) {
2010 WorkList.push_back(N0.Val);
2011 // FIXME: If running after legalize, we probably can't do this.
2012 N0 = DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2013 }
2014 return N0;
2015 }
2016
2017 // Could not fold it.
2018 return SDOperand();
2019}
2020
Nate Begeman1d4d4142005-09-01 00:19:25 +00002021// SelectionDAG::Combine - This is the entry point for the file.
2022//
Nate Begeman4ebd8052005-09-01 23:24:04 +00002023void SelectionDAG::Combine(bool RunningAfterLegalize) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002024 /// run - This is the main entry point to this class.
2025 ///
Nate Begeman4ebd8052005-09-01 23:24:04 +00002026 DAGCombiner(*this).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002027}