blob: 36927b358d7f48909668ec2cd758dbf4353c845c [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
16#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4e137af2008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Target/TargetData.h"
Chris Lattner1e3362f2008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetLowering.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
31#include <algorithm>
Dan Gohmand408d392008-05-23 20:40:06 +000032#include <set>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
39namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
42 cl::desc("Turn on alias analysis during testing"));
43
44 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
48//------------------------------ DAGCombiner ---------------------------------//
49
50 class VISIBILITY_HIDDEN DAGCombiner {
51 SelectionDAG &DAG;
Dan Gohman96eb47a2009-01-15 19:20:50 +000052 const TargetLowering &TLI;
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000053 CombineLevel Level;
54 bool LegalOperations;
55 bool LegalTypes;
Dan Gohmanea12c0c2008-08-20 16:30:28 +000056 bool Fast;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Worklist of all of the nodes that need to be simplified.
Evan Cheng56550ae2008-08-29 22:21:44 +000059 std::vector<SDNode*> WorkList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 // AA - Used for DAG load/store alias analysis.
62 AliasAnalysis &AA;
63
64 /// AddUsersToWorkList - When an instruction is simplified, add all users of
65 /// the instruction to the work lists because they might get more simplified
66 /// now.
67 ///
68 void AddUsersToWorkList(SDNode *N) {
69 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
70 UI != UE; ++UI)
Dan Gohman0c97f1d2008-07-27 20:43:25 +000071 AddToWorkList(*UI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 }
73
Dan Gohman6c89ea72007-10-08 17:57:15 +000074 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Dan Gohman8181bd12008-07-27 21:46:04 +000076 SDValue visit(SDNode *N);
Dan Gohman6c89ea72007-10-08 17:57:15 +000077
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 public:
79 /// AddToWorkList - Add to the work list making sure it's instance is at the
80 /// the back (next to be processed.)
81 void AddToWorkList(SDNode *N) {
82 removeFromWorkList(N);
83 WorkList.push_back(N);
84 }
85
Chris Lattner7bcb18f2008-02-03 06:49:24 +000086 /// removeFromWorkList - remove all instances of N from the worklist.
87 ///
88 void removeFromWorkList(SDNode *N) {
89 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
90 WorkList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 }
92
Dan Gohman8181bd12008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattner7bcb18f2008-02-03 06:49:24 +000094 bool AddTo = true);
95
Dan Gohman8181bd12008-07-27 21:46:04 +000096 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 return CombineTo(N, &Res, 1, AddTo);
98 }
99
Dan Gohman8181bd12008-07-27 21:46:04 +0000100 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 bool AddTo = true) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000102 SDValue To[] = { Res0, Res1 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 return CombineTo(N, To, 2, AddTo);
104 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000105
106 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Chris Lattner5872a362008-01-17 07:00:52 +0000107
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 private:
109
110 /// SimplifyDemandedBits - Check the specified integer node value to see if
111 /// it can be simplified or if things it uses can be simplified by bit
112 /// propagation. If so, return true.
Dan Gohman8181bd12008-07-27 21:46:04 +0000113 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman11607792008-02-27 00:25:32 +0000114 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
115 return SimplifyDemandedBits(Op, Demanded);
116 }
117
Dan Gohman8181bd12008-07-27 21:46:04 +0000118 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119
120 bool CombineToPreIndexedLoadStore(SDNode *N);
121 bool CombineToPostIndexedLoadStore(SDNode *N);
122
123
Dan Gohman6c89ea72007-10-08 17:57:15 +0000124 /// combine - call the node-specific routine that knows how to fold each
125 /// particular type of node. If that doesn't do anything, try the
126 /// target-specific DAG combines.
Dan Gohman8181bd12008-07-27 21:46:04 +0000127 SDValue combine(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129 // Visitation implementation - Implement dag node combining for different
130 // node types. The semantics are as follows:
131 // Return Value:
Evan Cheng56550ae2008-08-29 22:21:44 +0000132 // SDValue.getNode() == 0 - No change was made
133 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
134 // otherwise - N should be replaced by the returned Operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 //
Dan Gohman8181bd12008-07-27 21:46:04 +0000136 SDValue visitTokenFactor(SDNode *N);
137 SDValue visitMERGE_VALUES(SDNode *N);
138 SDValue visitADD(SDNode *N);
139 SDValue visitSUB(SDNode *N);
140 SDValue visitADDC(SDNode *N);
141 SDValue visitADDE(SDNode *N);
142 SDValue visitMUL(SDNode *N);
143 SDValue visitSDIV(SDNode *N);
144 SDValue visitUDIV(SDNode *N);
145 SDValue visitSREM(SDNode *N);
146 SDValue visitUREM(SDNode *N);
147 SDValue visitMULHU(SDNode *N);
148 SDValue visitMULHS(SDNode *N);
149 SDValue visitSMUL_LOHI(SDNode *N);
150 SDValue visitUMUL_LOHI(SDNode *N);
151 SDValue visitSDIVREM(SDNode *N);
152 SDValue visitUDIVREM(SDNode *N);
153 SDValue visitAND(SDNode *N);
154 SDValue visitOR(SDNode *N);
155 SDValue visitXOR(SDNode *N);
156 SDValue SimplifyVBinOp(SDNode *N);
157 SDValue visitSHL(SDNode *N);
158 SDValue visitSRA(SDNode *N);
159 SDValue visitSRL(SDNode *N);
160 SDValue visitCTLZ(SDNode *N);
161 SDValue visitCTTZ(SDNode *N);
162 SDValue visitCTPOP(SDNode *N);
163 SDValue visitSELECT(SDNode *N);
164 SDValue visitSELECT_CC(SDNode *N);
165 SDValue visitSETCC(SDNode *N);
166 SDValue visitSIGN_EXTEND(SDNode *N);
167 SDValue visitZERO_EXTEND(SDNode *N);
168 SDValue visitANY_EXTEND(SDNode *N);
169 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
170 SDValue visitTRUNCATE(SDNode *N);
171 SDValue visitBIT_CONVERT(SDNode *N);
172 SDValue visitBUILD_PAIR(SDNode *N);
173 SDValue visitFADD(SDNode *N);
174 SDValue visitFSUB(SDNode *N);
175 SDValue visitFMUL(SDNode *N);
176 SDValue visitFDIV(SDNode *N);
177 SDValue visitFREM(SDNode *N);
178 SDValue visitFCOPYSIGN(SDNode *N);
179 SDValue visitSINT_TO_FP(SDNode *N);
180 SDValue visitUINT_TO_FP(SDNode *N);
181 SDValue visitFP_TO_SINT(SDNode *N);
182 SDValue visitFP_TO_UINT(SDNode *N);
183 SDValue visitFP_ROUND(SDNode *N);
184 SDValue visitFP_ROUND_INREG(SDNode *N);
185 SDValue visitFP_EXTEND(SDNode *N);
186 SDValue visitFNEG(SDNode *N);
187 SDValue visitFABS(SDNode *N);
188 SDValue visitBRCOND(SDNode *N);
189 SDValue visitBR_CC(SDNode *N);
190 SDValue visitLOAD(SDNode *N);
191 SDValue visitSTORE(SDNode *N);
192 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
193 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
194 SDValue visitBUILD_VECTOR(SDNode *N);
195 SDValue visitCONCAT_VECTORS(SDNode *N);
196 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
Dan Gohman8181bd12008-07-27 21:46:04 +0000198 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendlingabb33a22009-01-30 00:45:56 +0000199 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattner91ed3c32007-12-06 07:33:36 +0000202
Dan Gohman8181bd12008-07-27 21:46:04 +0000203 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
204 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
205 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
206 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
207 SDValue N3, ISD::CondCode CC,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 bool NotExtCompare = false);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000209 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
210 bool foldBooleans = true);
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner4a7c8452008-01-26 01:09:19 +0000212 unsigned HiOp);
Dan Gohman8181bd12008-07-27 21:46:04 +0000213 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
214 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
215 SDValue BuildSDIV(SDNode *N);
216 SDValue BuildUDIV(SDNode *N);
217 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
218 SDValue ReduceLoadWidth(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219
Dan Gohman8181bd12008-07-27 21:46:04 +0000220 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattnere8671c52007-10-13 06:35:54 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
223 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000224 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
225 SmallVector<SDValue, 8> &Aliases);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
227 /// isAlias - Return true if there is any possibility that the two addresses
228 /// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +0000229 bool isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman8181bd12008-07-27 21:46:04 +0000231 SDValue Ptr2, int64_t Size2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 const Value *SrcValue2, int SrcValueOffset2);
233
234 /// FindAliasInfo - Extracts the relevant alias information from the memory
235 /// node. Returns true if the operand was a load.
236 bool FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000237 SDValue &Ptr, int64_t &Size,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 const Value *&SrcValue, int &SrcValueOffset);
239
240 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
241 /// looking for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +0000242 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243
244public:
Dan Gohmanea12c0c2008-08-20 16:30:28 +0000245 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 : DAG(D),
247 TLI(D.getTargetLoweringInfo()),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000248 Level(Unrestricted),
249 LegalOperations(false),
250 LegalTypes(false),
Dan Gohmanea12c0c2008-08-20 16:30:28 +0000251 Fast(fast),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 AA(A) {}
253
254 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000255 void Run(CombineLevel AtLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 };
257}
258
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000259
260namespace {
261/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
262/// nodes from the worklist.
263class VISIBILITY_HIDDEN WorkListRemover :
264 public SelectionDAG::DAGUpdateListener {
265 DAGCombiner &DC;
266public:
Dan Gohmana789bff2008-02-20 16:44:09 +0000267 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000268
Duncan Sands3866b1c2008-06-11 11:42:12 +0000269 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000270 DC.removeFromWorkList(N);
271 }
272
273 virtual void NodeUpdated(SDNode *N) {
274 // Ignore updates.
275 }
276};
277}
278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279//===----------------------------------------------------------------------===//
280// TargetLowering::DAGCombinerInfo implementation
281//===----------------------------------------------------------------------===//
282
283void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
284 ((DAGCombiner*)DC)->AddToWorkList(N);
285}
286
Dan Gohman8181bd12008-07-27 21:46:04 +0000287SDValue TargetLowering::DAGCombinerInfo::
288CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
290}
291
Dan Gohman8181bd12008-07-27 21:46:04 +0000292SDValue TargetLowering::DAGCombinerInfo::
293CombineTo(SDNode *N, SDValue Res) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 return ((DAGCombiner*)DC)->CombineTo(N, Res);
295}
296
297
Dan Gohman8181bd12008-07-27 21:46:04 +0000298SDValue TargetLowering::DAGCombinerInfo::
299CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
301}
302
Dan Gohman22cefb02009-01-29 01:59:02 +0000303void TargetLowering::DAGCombinerInfo::
304CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
305 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
306}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307
308//===----------------------------------------------------------------------===//
309// Helper Functions
310//===----------------------------------------------------------------------===//
311
312/// isNegatibleForFree - Return 1 if we can compute the negated form of the
313/// specified expression for the same cost as the expression itself, or 2 if we
314/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000315static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattnere0992b82008-02-26 07:04:54 +0000316 unsigned Depth = 0) {
Dale Johannesenb89072e2007-10-16 23:38:29 +0000317 // No compile time optimizations on this type.
318 if (Op.getValueType() == MVT::ppcf128)
319 return 0;
320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 // fneg is removable even if it has multiple uses.
322 if (Op.getOpcode() == ISD::FNEG) return 2;
323
324 // Don't allow anything with multiple uses.
325 if (!Op.hasOneUse()) return 0;
326
327 // Don't recurse exponentially.
328 if (Depth > 6) return 0;
329
330 switch (Op.getOpcode()) {
331 default: return false;
332 case ISD::ConstantFP:
Chris Lattnere0992b82008-02-26 07:04:54 +0000333 // Don't invert constant FP values after legalize. The negated constant
334 // isn't necessarily legal.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000335 return LegalOperations ? 0 : 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 case ISD::FADD:
337 // FIXME: determine better conditions for this xform.
338 if (!UnsafeFPMath) return 0;
339
340 // -(A+B) -> -A - B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000341 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 return V;
343 // -(A+B) -> -B - A
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000344 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 case ISD::FSUB:
346 // We can't turn -(A-B) into B-A when we honor signed zeros.
347 if (!UnsafeFPMath) return 0;
348
349 // -(A-B) -> B-A
350 return 1;
351
352 case ISD::FMUL:
353 case ISD::FDIV:
354 if (HonorSignDependentRoundingFPMath()) return 0;
355
356 // -(X*Y) -> (-X * Y) or (X*-Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000357 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 return V;
359
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000360 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361
362 case ISD::FP_EXTEND:
363 case ISD::FP_ROUND:
364 case ISD::FSIN:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000365 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 }
367}
368
369/// GetNegatedExpression - If isNegatibleForFree returns true, this function
370/// returns the newly negated expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000371static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000372 bool LegalOperations, unsigned Depth = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 // fneg is removable even if it has multiple uses.
374 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
375
376 // Don't allow anything with multiple uses.
377 assert(Op.hasOneUse() && "Unknown reuse!");
378
379 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
380 switch (Op.getOpcode()) {
381 default: assert(0 && "Unknown code");
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000382 case ISD::ConstantFP: {
383 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
384 V.changeSign();
385 return DAG.getConstantFP(V, Op.getValueType());
386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 case ISD::FADD:
388 // FIXME: determine better conditions for this xform.
389 assert(UnsafeFPMath);
390
391 // -(A+B) -> -A - B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000392 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000393 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000394 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000395 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 Op.getOperand(1));
397 // -(A+B) -> -B - A
Bill Wendlingabb33a22009-01-30 00:45:56 +0000398 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000399 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000400 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 Op.getOperand(0));
402 case ISD::FSUB:
403 // We can't turn -(A-B) into B-A when we honor signed zeros.
404 assert(UnsafeFPMath);
405
406 // -(0-B) -> B
407 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000408 if (N0CFP->getValueAPF().isZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 return Op.getOperand(1);
410
411 // -(A-B) -> B-A
Bill Wendlingabb33a22009-01-30 00:45:56 +0000412 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
413 Op.getOperand(1), Op.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414
415 case ISD::FMUL:
416 case ISD::FDIV:
417 assert(!HonorSignDependentRoundingFPMath());
418
419 // -(X*Y) -> -X * Y
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000420 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000421 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000422 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000423 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 Op.getOperand(1));
425
426 // -(X*Y) -> X * -Y
Bill Wendlingabb33a22009-01-30 00:45:56 +0000427 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 Op.getOperand(0),
Chris Lattnere0992b82008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000430 LegalOperations, Depth+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
432 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 case ISD::FSIN:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000434 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000435 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000436 LegalOperations, Depth+1));
Chris Lattner5872a362008-01-17 07:00:52 +0000437 case ISD::FP_ROUND:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000438 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Chris Lattnere0992b82008-02-26 07:04:54 +0000439 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000440 LegalOperations, Depth+1),
Chris Lattner5872a362008-01-17 07:00:52 +0000441 Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 }
443}
444
445
446// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
447// that selects between the values 1 and 0, making it equivalent to a setcc.
448// Also, set the incoming LHS, RHS, and CC references to the appropriate
449// nodes based on the type of node we are checking. This simplifies life a
450// bit for the callers.
Dan Gohman8181bd12008-07-27 21:46:04 +0000451static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
452 SDValue &CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 if (N.getOpcode() == ISD::SETCC) {
454 LHS = N.getOperand(0);
455 RHS = N.getOperand(1);
456 CC = N.getOperand(2);
457 return true;
458 }
459 if (N.getOpcode() == ISD::SELECT_CC &&
460 N.getOperand(2).getOpcode() == ISD::Constant &&
461 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman9d24dc72008-03-13 22:13:53 +0000462 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
464 LHS = N.getOperand(0);
465 RHS = N.getOperand(1);
466 CC = N.getOperand(4);
467 return true;
468 }
469 return false;
470}
471
472// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
473// one use. If this is true, it allows the users to invert the operation for
474// free when it is profitable to do so.
Dan Gohman8181bd12008-07-27 21:46:04 +0000475static bool isOneUseSetCC(SDValue N) {
476 SDValue N0, N1, N2;
Gabor Greif1c80d112008-08-28 21:40:38 +0000477 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 return true;
479 return false;
480}
481
Bill Wendlingabb33a22009-01-30 00:45:56 +0000482SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
483 SDValue N0, SDValue N1) {
Duncan Sands92c43912008-06-06 12:08:01 +0000484 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
486 if (isa<ConstantSDNode>(N1)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000488 SDValue OpNode = DAG.getNode(Opc, N1.getDebugLoc(), VT,
Bill Wendlingabb33a22009-01-30 00:45:56 +0000489 N0.getOperand(1), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000490 AddToWorkList(OpNode.getNode());
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000491 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 } else if (N0.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000493 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
494 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
495 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000496 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000497 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 }
499 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
502 if (isa<ConstantSDNode>(N0)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000503 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
504 SDValue OpNode = DAG.getNode(Opc, N1.getDebugLoc(), VT,
505 N1.getOperand(1), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000506 AddToWorkList(OpNode.getNode());
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000507 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 } else if (N1.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000509 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000510 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendlingabb33a22009-01-30 00:45:56 +0000511 N1.getOperand(0), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000512 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000513 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 }
515 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000516
Dan Gohman8181bd12008-07-27 21:46:04 +0000517 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518}
519
Dan Gohman8181bd12008-07-27 21:46:04 +0000520SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
521 bool AddTo) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000522 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
523 ++NodesCombined;
524 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +0000525 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000526 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman05452202009-01-21 15:17:51 +0000527 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
528 assert(N->getValueType(i) == To[i].getValueType() &&
529 "Cannot combine value to value of different type!"));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000530 WorkListRemover DeadNodes(*this);
531 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
532
533 if (AddTo) {
534 // Push the new nodes and any users onto the worklist
535 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000536 AddToWorkList(To[i].getNode());
537 AddUsersToWorkList(To[i].getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000538 }
539 }
540
Dan Gohman86bf1392009-01-19 21:44:21 +0000541 // Finally, if the node is now dead, remove it from the graph. The node
542 // may not be dead if the replacement process recursively simplified to
543 // something else needing this node.
544 if (N->use_empty()) {
545 // Nodes can be reintroduced into the worklist. Make sure we do not
546 // process a node that has been replaced.
547 removeFromWorkList(N);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000548
Dan Gohman86bf1392009-01-19 21:44:21 +0000549 // Finally, since the node is now dead, remove it from the graph.
550 DAG.DeleteNode(N);
551 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000552 return SDValue(N, 0);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000553}
554
Dan Gohman22cefb02009-01-29 01:59:02 +0000555void
556DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
557 TLO) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000558 // Replace all uses. If any nodes become isomorphic to other nodes and
559 // are deleted, make sure to remove them from our worklist.
560 WorkListRemover DeadNodes(*this);
561 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohman22cefb02009-01-29 01:59:02 +0000562
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000563 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greif1c80d112008-08-28 21:40:38 +0000564 AddToWorkList(TLO.New.getNode());
565 AddUsersToWorkList(TLO.New.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000566
567 // Finally, if the node is now dead, remove it from the graph. The node
568 // may not be dead if the replacement process recursively simplified to
569 // something else needing this node.
Gabor Greif1c80d112008-08-28 21:40:38 +0000570 if (TLO.Old.getNode()->use_empty()) {
571 removeFromWorkList(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000572
573 // If the operands of this node are only used by the node, they will now
574 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greif1c80d112008-08-28 21:40:38 +0000575 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
576 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
577 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000578
Gabor Greif1c80d112008-08-28 21:40:38 +0000579 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000580 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000581}
582
583/// SimplifyDemandedBits - Check the specified integer node value to see if
584/// it can be simplified or if things it uses can be simplified by bit
585/// propagation. If so, return true.
586bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
587 TargetLowering::TargetLoweringOpt TLO(DAG);
588 APInt KnownZero, KnownOne;
589 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
590 return false;
591
592 // Revisit the node.
593 AddToWorkList(Op.getNode());
594
595 // Replace the old value with the new one.
596 ++NodesCombined;
597 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
598 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
599 DOUT << '\n';
600
601 CommitTargetLoweringOpt(TLO);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000602 return true;
603}
604
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605//===----------------------------------------------------------------------===//
606// Main DAG Combiner implementation
607//===----------------------------------------------------------------------===//
608
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000609void DAGCombiner::Run(CombineLevel AtLevel) {
610 // set the instance variables, so that the various visit routines may use it.
611 Level = AtLevel;
612 LegalOperations = Level >= NoIllegalOperations;
613 LegalTypes = Level >= NoIllegalTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614
Evan Cheng56550ae2008-08-29 22:21:44 +0000615 // Add all the dag nodes to the worklist.
616 WorkList.reserve(DAG.allnodes_size());
617 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
618 E = DAG.allnodes_end(); I != E; ++I)
619 WorkList.push_back(I);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000620
Evan Cheng56550ae2008-08-29 22:21:44 +0000621 // Create a dummy node (which is not added to allnodes), that adds a reference
622 // to the root node, preventing it from being deleted, and tracking any
623 // changes of the root.
624 HandleSDNode Dummy(DAG.getRoot());
625
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 // The root of the dag may dangle to deleted nodes until the dag combiner is
627 // done. Set it to null to avoid confusion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000628 DAG.setRoot(SDValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629
Evan Cheng56550ae2008-08-29 22:21:44 +0000630 // while the worklist isn't empty, inspect the node on the end of it and
631 // try and combine it.
632 while (!WorkList.empty()) {
633 SDNode *N = WorkList.back();
634 WorkList.pop_back();
635
636 // If N has no uses, it is dead. Make sure to revisit all N's operands once
637 // N is deleted from the DAG, since they too may now be dead or may have a
638 // reduced number of uses, allowing other xforms.
639 if (N->use_empty() && N != &Dummy) {
640 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
641 AddToWorkList(N->getOperand(i).getNode());
642
643 DAG.DeleteNode(N);
644 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000646
647 SDValue RV = combine(N);
648
649 if (RV.getNode() == 0)
650 continue;
651
652 ++NodesCombined;
653
654 // If we get back the same node we passed in, rather than a new node or
655 // zero, we know that the node must have defined multiple values and
656 // CombineTo was used. Since CombineTo takes care of the worklist
657 // mechanics for us, we have no work to do in this case.
658 if (RV.getNode() == N)
659 continue;
660
661 assert(N->getOpcode() != ISD::DELETED_NODE &&
662 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
663 "Node was deleted but visit returned new node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664
Evan Cheng56550ae2008-08-29 22:21:44 +0000665 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
666 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
667 DOUT << '\n';
668 WorkListRemover DeadNodes(*this);
669 if (N->getNumValues() == RV.getNode()->getNumValues())
670 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
671 else {
672 assert(N->getValueType(0) == RV.getValueType() &&
673 N->getNumValues() == 1 && "Type mismatch");
674 SDValue OpV = RV;
675 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
676 }
677
678 // Push the new node and any users onto the worklist
679 AddToWorkList(RV.getNode());
680 AddUsersToWorkList(RV.getNode());
681
682 // Add any uses of the old node to the worklist in case this node is the
683 // last one that uses them. They may become dead after this node is
684 // deleted.
685 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
686 AddToWorkList(N->getOperand(i).getNode());
687
Dan Gohman86bf1392009-01-19 21:44:21 +0000688 // Finally, if the node is now dead, remove it from the graph. The node
689 // may not be dead if the replacement process recursively simplified to
690 // something else needing this node.
691 if (N->use_empty()) {
692 // Nodes can be reintroduced into the worklist. Make sure we do not
693 // process a node that has been replaced.
694 removeFromWorkList(N);
Evan Cheng56550ae2008-08-29 22:21:44 +0000695
Dan Gohman86bf1392009-01-19 21:44:21 +0000696 // Finally, since the node is now dead, remove it from the graph.
697 DAG.DeleteNode(N);
698 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000699 }
700
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 // If the root changed (e.g. it was a dead load, update the root).
702 DAG.setRoot(Dummy.getValue());
703}
704
Dan Gohman8181bd12008-07-27 21:46:04 +0000705SDValue DAGCombiner::visit(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 switch(N->getOpcode()) {
707 default: break;
708 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000709 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 case ISD::ADD: return visitADD(N);
711 case ISD::SUB: return visitSUB(N);
712 case ISD::ADDC: return visitADDC(N);
713 case ISD::ADDE: return visitADDE(N);
714 case ISD::MUL: return visitMUL(N);
715 case ISD::SDIV: return visitSDIV(N);
716 case ISD::UDIV: return visitUDIV(N);
717 case ISD::SREM: return visitSREM(N);
718 case ISD::UREM: return visitUREM(N);
719 case ISD::MULHU: return visitMULHU(N);
720 case ISD::MULHS: return visitMULHS(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000721 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
722 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
723 case ISD::SDIVREM: return visitSDIVREM(N);
724 case ISD::UDIVREM: return visitUDIVREM(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 case ISD::AND: return visitAND(N);
726 case ISD::OR: return visitOR(N);
727 case ISD::XOR: return visitXOR(N);
728 case ISD::SHL: return visitSHL(N);
729 case ISD::SRA: return visitSRA(N);
730 case ISD::SRL: return visitSRL(N);
731 case ISD::CTLZ: return visitCTLZ(N);
732 case ISD::CTTZ: return visitCTTZ(N);
733 case ISD::CTPOP: return visitCTPOP(N);
734 case ISD::SELECT: return visitSELECT(N);
735 case ISD::SELECT_CC: return visitSELECT_CC(N);
736 case ISD::SETCC: return visitSETCC(N);
737 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
738 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
739 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
740 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
741 case ISD::TRUNCATE: return visitTRUNCATE(N);
742 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Chengb6290462008-05-12 23:04:07 +0000743 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 case ISD::FADD: return visitFADD(N);
745 case ISD::FSUB: return visitFSUB(N);
746 case ISD::FMUL: return visitFMUL(N);
747 case ISD::FDIV: return visitFDIV(N);
748 case ISD::FREM: return visitFREM(N);
749 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
750 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
751 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
752 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
753 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
754 case ISD::FP_ROUND: return visitFP_ROUND(N);
755 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
756 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
757 case ISD::FNEG: return visitFNEG(N);
758 case ISD::FABS: return visitFABS(N);
759 case ISD::BRCOND: return visitBRCOND(N);
760 case ISD::BR_CC: return visitBR_CC(N);
761 case ISD::LOAD: return visitLOAD(N);
762 case ISD::STORE: return visitSTORE(N);
763 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Chengd7ba7ed2007-10-06 08:19:55 +0000764 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
766 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
767 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
768 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000769 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770}
771
Dan Gohman8181bd12008-07-27 21:46:04 +0000772SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000773 SDValue RV = visit(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000774
775 // If nothing happened, try a target-specific DAG combine.
Gabor Greif1c80d112008-08-28 21:40:38 +0000776 if (RV.getNode() == 0) {
Dan Gohman6c89ea72007-10-08 17:57:15 +0000777 assert(N->getOpcode() != ISD::DELETED_NODE &&
778 "Node was deleted but visit returned NULL!");
779
780 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
781 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
782
783 // Expose the DAG combiner to the target combiner impls.
784 TargetLowering::DAGCombinerInfo
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000785 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000786
787 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
788 }
789 }
790
Evan Chengd1113582008-03-22 01:55:50 +0000791 // If N is a commutative binary node, try commuting it to enable more
792 // sdisel CSE.
Gabor Greif1c80d112008-08-28 21:40:38 +0000793 if (RV.getNode() == 0 &&
Evan Chengd1113582008-03-22 01:55:50 +0000794 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
795 N->getNumValues() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000796 SDValue N0 = N->getOperand(0);
797 SDValue N1 = N->getOperand(1);
Bill Wendling131d6e92009-01-30 01:13:16 +0000798
Evan Chengd1113582008-03-22 01:55:50 +0000799 // Constant operands are canonicalized to RHS.
800 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000801 SDValue Ops[] = { N1, N0 };
Evan Chengd1113582008-03-22 01:55:50 +0000802 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
803 Ops, 2);
Evan Chenge40b51c2008-03-24 23:55:16 +0000804 if (CSENode)
Dan Gohman8181bd12008-07-27 21:46:04 +0000805 return SDValue(CSENode, 0);
Evan Chengd1113582008-03-22 01:55:50 +0000806 }
807 }
808
Dan Gohman6c89ea72007-10-08 17:57:15 +0000809 return RV;
810}
811
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812/// getInputChainForNode - Given a node, return its input chain if it has one,
813/// otherwise return a null sd operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000814static SDValue getInputChainForNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 if (unsigned NumOps = N->getNumOperands()) {
816 if (N->getOperand(0).getValueType() == MVT::Other)
817 return N->getOperand(0);
818 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
819 return N->getOperand(NumOps-1);
820 for (unsigned i = 1; i < NumOps-1; ++i)
821 if (N->getOperand(i).getValueType() == MVT::Other)
822 return N->getOperand(i);
823 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000824 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825}
826
Dan Gohman8181bd12008-07-27 21:46:04 +0000827SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 // If N has two operands, where one has an input chain equal to the other,
829 // the 'other' chain is redundant.
830 if (N->getNumOperands() == 2) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000831 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 return N->getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000833 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 return N->getOperand(1);
835 }
836
837 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000838 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 SmallPtrSet<SDNode*, 16> SeenOps;
840 bool Changed = false; // If we should replace this token factor.
841
842 // Start out with this token factor.
843 TFs.push_back(N);
844
845 // Iterate through token factors. The TFs grows when new token factors are
846 // encountered.
847 for (unsigned i = 0; i < TFs.size(); ++i) {
848 SDNode *TF = TFs[i];
849
850 // Check each of the operands.
851 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000852 SDValue Op = TF->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853
854 switch (Op.getOpcode()) {
855 case ISD::EntryToken:
856 // Entry tokens don't need to be added to the list. They are
857 // rededundant.
858 Changed = true;
859 break;
860
861 case ISD::TokenFactor:
862 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000863 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 // Queue up for processing.
Gabor Greif1c80d112008-08-28 21:40:38 +0000865 TFs.push_back(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 // Clean up in case the token factor is removed.
Gabor Greif1c80d112008-08-28 21:40:38 +0000867 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868 Changed = true;
869 break;
870 }
871 // Fall thru
872
873 default:
874 // Only add if it isn't already in the list.
Gabor Greif1c80d112008-08-28 21:40:38 +0000875 if (SeenOps.insert(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 Ops.push_back(Op);
877 else
878 Changed = true;
879 break;
880 }
881 }
882 }
883
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885
886 // If we've change things around then replace token factor.
887 if (Changed) {
Dan Gohman301f4052008-01-29 13:02:09 +0000888 if (Ops.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 // The entry token is the only possible outcome.
890 Result = DAG.getEntryNode();
891 } else {
892 // New and improved token factor.
Bill Wendling131d6e92009-01-30 01:13:16 +0000893 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
894 MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000896
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 // Don't add users to work list.
898 return CombineTo(N, Result, false);
899 }
900
901 return Result;
902}
903
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000904/// MERGE_VALUES can always be eliminated.
Dan Gohman8181bd12008-07-27 21:46:04 +0000905SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000906 WorkListRemover DeadNodes(*this);
907 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman8181bd12008-07-27 21:46:04 +0000908 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000909 &DeadNodes);
910 removeFromWorkList(N);
911 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000912 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000913}
914
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915static
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000916SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
917 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +0000918 MVT VT = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000919 SDValue N00 = N0.getOperand(0);
920 SDValue N01 = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000922
Gabor Greif1c80d112008-08-28 21:40:38 +0000923 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000925 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
926 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
927 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
928 N00.getOperand(0), N01),
929 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
930 N00.getOperand(1), N01));
931 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 }
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000933
Dan Gohman8181bd12008-07-27 21:46:04 +0000934 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935}
936
937static
Dan Gohman8181bd12008-07-27 21:46:04 +0000938SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendling0d810b82008-11-11 08:25:46 +0000939 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000940 bool LegalOperations) {
Duncan Sands92c43912008-06-06 12:08:01 +0000941 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 unsigned Opc = N->getOpcode();
943 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman8181bd12008-07-27 21:46:04 +0000944 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
945 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendling0d810b82008-11-11 08:25:46 +0000947
948 if (isSlctCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendling0d810b82008-11-11 08:25:46 +0000950 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +0000951 SDValue CCOp = Slct.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 if (CCOp.getOpcode() == ISD::SETCC)
953 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
954 }
955
956 bool DoXform = false;
957 bool InvCC = false;
958 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
959 "Bad input!");
Bill Wendling0d810b82008-11-11 08:25:46 +0000960
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendling0d810b82008-11-11 08:25:46 +0000962 cast<ConstantSDNode>(LHS)->isNullValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963 DoXform = true;
Bill Wendling0d810b82008-11-11 08:25:46 +0000964 } else if (CC != ISD::SETCC_INVALID &&
965 RHS.getOpcode() == ISD::Constant &&
966 cast<ConstantSDNode>(RHS)->isNullValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 std::swap(LHS, RHS);
Dan Gohman8181bd12008-07-27 21:46:04 +0000968 SDValue Op0 = Slct.getOperand(0);
Bill Wendling0d810b82008-11-11 08:25:46 +0000969 MVT OpVT = isSlctCC ? Op0.getValueType() :
970 Op0.getOperand(0).getValueType();
971 bool isInt = OpVT.isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +0000973
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000974 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendling0d810b82008-11-11 08:25:46 +0000975 return SDValue(); // Inverse operator isn't legal.
976
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 DoXform = true;
978 InvCC = true;
979 }
980
981 if (DoXform) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000982 SDValue Result = DAG.getNode(Opc, RHS.getDebugLoc(), VT, OtherOp, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 if (isSlctCC)
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000984 return DAG.getSelectCC(N->getDebugLoc(), OtherOp, Result,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman8181bd12008-07-27 21:46:04 +0000986 SDValue CCOp = Slct.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 if (InvCC)
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000988 CCOp = DAG.getSetCC(Slct.getDebugLoc(), CCOp.getValueType(),
989 CCOp.getOperand(0), CCOp.getOperand(1), CC);
990 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
991 CCOp, OtherOp, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000993 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994}
995
Dan Gohman8181bd12008-07-27 21:46:04 +0000996SDValue DAGCombiner::visitADD(SDNode *N) {
997 SDValue N0 = N->getOperand(0);
998 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1000 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001001 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002
1003 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001004 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001005 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001006 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +00001008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 // fold (add x, undef) -> undef
1010 if (N0.getOpcode() == ISD::UNDEF)
1011 return N0;
1012 if (N1.getOpcode() == ISD::UNDEF)
1013 return N1;
1014 // fold (add c1, c2) -> c1+c2
1015 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001016 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 // canonicalize constant to RHS
1018 if (N0C && !N1C)
Bill Wendlingd850aa52009-01-30 02:31:17 +00001019 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 // fold (add x, 0) -> x
1021 if (N1C && N1C->isNullValue())
1022 return N0;
Dan Gohman36322c72008-10-18 02:06:02 +00001023 // fold (add Sym, c) -> Sym+c
1024 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001025 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman36322c72008-10-18 02:06:02 +00001026 GA->getOpcode() == ISD::GlobalAddress)
1027 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1028 GA->getOffset() +
1029 (uint64_t)N1C->getSExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 // fold ((c1-A)+c2) -> (c1+c2)-A
1031 if (N1C && N0.getOpcode() == ISD::SUB)
1032 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001033 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001034 DAG.getConstant(N1C->getAPIntValue()+
1035 N0C->getAPIntValue(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036 N0.getOperand(1));
1037 // reassociate add
Bill Wendlingabb33a22009-01-30 00:45:56 +00001038 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001039 if (RADD.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 return RADD;
1041 // fold ((0-A) + B) -> B-A
1042 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1043 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001044 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 // fold (A + (0-B)) -> A-B
1046 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1047 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001048 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 // fold (A+(B-A)) -> B
1050 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
1051 return N1.getOperand(0);
Dale Johannesene7e5da32008-11-27 00:43:21 +00001052 // fold ((B-A)+A) -> B
1053 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1054 return N0.getOperand(0);
Dale Johannesend1feb582008-12-02 01:30:54 +00001055 // fold (A+(B-(A+C))) to (B-C)
1056 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001057 N0 == N1.getOperand(1).getOperand(0))
1058 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001059 N1.getOperand(1).getOperand(1));
Dale Johannesend1feb582008-12-02 01:30:54 +00001060 // fold (A+(B-(C+A))) to (B-C)
1061 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001062 N0 == N1.getOperand(1).getOperand(1))
1063 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001064 N1.getOperand(1).getOperand(0));
Dale Johannesend32a9602008-12-23 23:47:22 +00001065 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesend29920f2008-12-02 18:40:40 +00001066 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1067 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001068 N0 == N1.getOperand(0).getOperand(1))
1069 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1070 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesend29920f2008-12-02 18:40:40 +00001071
Dale Johannesend1feb582008-12-02 01:30:54 +00001072 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1073 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1074 SDValue N00 = N0.getOperand(0);
1075 SDValue N01 = N0.getOperand(1);
1076 SDValue N10 = N1.getOperand(0);
1077 SDValue N11 = N1.getOperand(1);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001078
1079 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1080 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1081 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1082 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesend1feb582008-12-02 01:30:54 +00001083 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001084
Dan Gohman8181bd12008-07-27 21:46:04 +00001085 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1086 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087
1088 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands92c43912008-06-06 12:08:01 +00001089 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00001090 APInt LHSZero, LHSOne;
1091 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001092 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001094
Dan Gohmanbea075f2008-02-20 16:33:30 +00001095 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
1097
1098 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1099 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1100 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1101 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001102 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 }
1104 }
1105
1106 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greif1c80d112008-08-28 21:40:38 +00001107 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001108 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001109 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001111 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001112 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001113 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001114 }
1115
1116 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greif1c80d112008-08-28 21:40:38 +00001117 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001118 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greif1c80d112008-08-28 21:40:38 +00001119 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001120 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001121 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001122 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greif1c80d112008-08-28 21:40:38 +00001123 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 }
1125
Dan Gohman8181bd12008-07-27 21:46:04 +00001126 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127}
1128
Dan Gohman8181bd12008-07-27 21:46:04 +00001129SDValue DAGCombiner::visitADDC(SDNode *N) {
1130 SDValue N0 = N->getOperand(0);
1131 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001134 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001135
1136 // If the flag result is dead, turn this into an ADD.
1137 if (N->hasNUsesOfValue(0, 1))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001138 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
1139 DAG.getNode(ISD::CARRY_FALSE,
1140 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141
1142 // canonicalize constant to RHS.
Dan Gohmanedb43e72008-06-23 15:29:14 +00001143 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001144 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145
1146 // fold (addc x, 0) -> x + no carry out
1147 if (N1C && N1C->isNullValue())
Bill Wendling4c196ba2009-01-30 02:38:00 +00001148 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
1149 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150
1151 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmanbea075f2008-02-20 16:33:30 +00001152 APInt LHSZero, LHSOne;
1153 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001154 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling4c196ba2009-01-30 02:38:00 +00001156
Dan Gohmanbea075f2008-02-20 16:33:30 +00001157 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001158 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
1159
1160 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1161 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1162 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1163 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001164 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
1165 DAG.getNode(ISD::CARRY_FALSE,
1166 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 }
1168
Dan Gohman8181bd12008-07-27 21:46:04 +00001169 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170}
1171
Dan Gohman8181bd12008-07-27 21:46:04 +00001172SDValue DAGCombiner::visitADDE(SDNode *N) {
1173 SDValue N0 = N->getOperand(0);
1174 SDValue N1 = N->getOperand(1);
1175 SDValue CarryIn = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1177 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178
1179 // canonicalize constant to RHS
Dan Gohmanedb43e72008-06-23 15:29:14 +00001180 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001181 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1182 N1, N0, CarryIn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001183
1184 // fold (adde x, y, false) -> (addc x, y)
Dan Gohmanedb43e72008-06-23 15:29:14 +00001185 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001186 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187
Dan Gohman8181bd12008-07-27 21:46:04 +00001188 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189}
1190
Dan Gohman8181bd12008-07-27 21:46:04 +00001191SDValue DAGCombiner::visitSUB(SDNode *N) {
1192 SDValue N0 = N->getOperand(0);
1193 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001194 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1195 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001196 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197
1198 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001199 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001200 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001201 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +00001203
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 // fold (sub x, x) -> 0
Evan Chenga15896e2008-03-12 07:02:50 +00001205 if (N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 return DAG.getConstant(0, N->getValueType(0));
1207 // fold (sub c1, c2) -> c1-c2
1208 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001209 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 // fold (sub x, c) -> (add x, -c)
1211 if (N1C)
Bill Wendling697795a2009-01-30 02:42:10 +00001212 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001213 DAG.getConstant(-N1C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 // fold (A+B)-A -> B
1215 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
1216 return N0.getOperand(1);
1217 // fold (A+B)-B -> A
1218 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesend546e832008-12-23 23:01:27 +00001219 return N0.getOperand(0);
Dale Johannesend32a9602008-12-23 23:47:22 +00001220 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001221 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesend546e832008-12-23 23:01:27 +00001222 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1223 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001224 N0.getOperand(1).getOperand(0) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001225 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1226 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesend546e832008-12-23 23:01:27 +00001227 // fold ((A+(C+B))-B) -> A+C
1228 if (N0.getOpcode() == ISD::ADD &&
1229 N0.getOperand(1).getOpcode() == ISD::ADD &&
1230 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001231 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1232 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesenfbc2e4b2008-12-23 01:59:54 +00001233 // fold ((A-(B-C))-C) -> A-B
1234 if (N0.getOpcode() == ISD::SUB &&
1235 N0.getOperand(1).getOpcode() == ISD::SUB &&
1236 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001237 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1238 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greif1c80d112008-08-28 21:40:38 +00001240 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001241 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greif1c80d112008-08-28 21:40:38 +00001242 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 }
Bill Wendling697795a2009-01-30 02:42:10 +00001244
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245 // If either operand of a sub is undef, the result is undef
1246 if (N0.getOpcode() == ISD::UNDEF)
1247 return N0;
1248 if (N1.getOpcode() == ISD::UNDEF)
1249 return N1;
1250
Dan Gohman36322c72008-10-18 02:06:02 +00001251 // If the relocation model supports it, consider symbol offsets.
1252 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001253 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman36322c72008-10-18 02:06:02 +00001254 // fold (sub Sym, c) -> Sym-c
1255 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1256 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1257 GA->getOffset() -
1258 (uint64_t)N1C->getSExtValue());
1259 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1260 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1261 if (GA->getGlobal() == GB->getGlobal())
1262 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1263 VT);
1264 }
1265
Dan Gohman8181bd12008-07-27 21:46:04 +00001266 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267}
1268
Dan Gohman8181bd12008-07-27 21:46:04 +00001269SDValue DAGCombiner::visitMUL(SDNode *N) {
1270 SDValue N0 = N->getOperand(0);
1271 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1273 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001274 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275
1276 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001277 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001278 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001279 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 }
1281
1282 // fold (mul x, undef) -> 0
1283 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1284 return DAG.getConstant(0, VT);
1285 // fold (mul c1, c2) -> c1*c2
1286 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001287 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 // canonicalize constant to RHS
1289 if (N0C && !N1C)
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001290 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 // fold (mul x, 0) -> 0
1292 if (N1C && N1C->isNullValue())
1293 return N1;
1294 // fold (mul x, -1) -> 0-x
1295 if (N1C && N1C->isAllOnesValue())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001296 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1297 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 // fold (mul x, (1 << c)) -> x << c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001299 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001300 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001301 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 TLI.getShiftAmountTy()));
1303 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001304 if (N1C && isPowerOf2_64(-N1C->getSExtValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305 // FIXME: If the input is something that is easily negated (e.g. a
1306 // single-use add), we should put the negate there.
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001307 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1308 DAG.getConstant(0, VT),
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001309 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman40686732008-09-26 21:54:37 +00001310 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001311 TLI.getShiftAmountTy())));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001313 if (N1C && N0.getOpcode() == ISD::SHL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001315 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1316 N1, N0.getOperand(1));
Gabor Greif1c80d112008-08-28 21:40:38 +00001317 AddToWorkList(C3.getNode());
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001318 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1319 N0.getOperand(0), C3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 }
1321
1322 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1323 // use.
1324 {
Dan Gohman8181bd12008-07-27 21:46:04 +00001325 SDValue Sh(0,0), Y(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001326 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1327 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001328 N0.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001329 Sh = N0; Y = N1;
1330 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00001331 isa<ConstantSDNode>(N1.getOperand(1)) &&
1332 N1.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 Sh = N1; Y = N0;
1334 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001335
Gabor Greif1c80d112008-08-28 21:40:38 +00001336 if (Sh.getNode()) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001337 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1338 Sh.getOperand(0), Y);
1339 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1340 Mul, Sh.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 }
1342 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00001345 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001346 isa<ConstantSDNode>(N0.getOperand(1)))
1347 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1348 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1349 N0.getOperand(0), N1),
1350 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1351 N0.getOperand(1), N1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352
1353 // reassociate mul
Bill Wendlingabb33a22009-01-30 00:45:56 +00001354 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001355 if (RMUL.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 return RMUL;
1357
Dan Gohman8181bd12008-07-27 21:46:04 +00001358 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359}
1360
Dan Gohman8181bd12008-07-27 21:46:04 +00001361SDValue DAGCombiner::visitSDIV(SDNode *N) {
1362 SDValue N0 = N->getOperand(0);
1363 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001364 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1365 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001366 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367
1368 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001369 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001370 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001371 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 }
1373
1374 // fold (sdiv c1, c2) -> c1/c2
1375 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001376 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 // fold (sdiv X, 1) -> X
Dan Gohman40686732008-09-26 21:54:37 +00001378 if (N1C && N1C->getSExtValue() == 1LL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 return N0;
1380 // fold (sdiv X, -1) -> 0-X
1381 if (N1C && N1C->isAllOnesValue())
Bill Wendling09f5dc82009-01-30 02:52:17 +00001382 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1383 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384 // If we know the sign bits of both operands are zero, strength reduce to a
1385 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands92c43912008-06-06 12:08:01 +00001386 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001387 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling09f5dc82009-01-30 02:52:17 +00001388 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1389 N0, N1);
Chris Lattner336672f2008-01-27 23:32:17 +00001390 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman9d24dc72008-03-13 22:13:53 +00001392 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman40686732008-09-26 21:54:37 +00001393 (isPowerOf2_64(N1C->getSExtValue()) ||
1394 isPowerOf2_64(-N1C->getSExtValue()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 // If dividing by powers of two is cheap, then don't perform the following
1396 // fold.
1397 if (TLI.isPow2DivCheap())
Dan Gohman8181bd12008-07-27 21:46:04 +00001398 return SDValue();
Bill Wendling09f5dc82009-01-30 02:52:17 +00001399
Dan Gohman40686732008-09-26 21:54:37 +00001400 int64_t pow2 = N1C->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
1402 unsigned lg2 = Log2_64(abs2);
Bill Wendling09f5dc82009-01-30 02:52:17 +00001403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 // Splat the sign bit into the register
Bill Wendling09f5dc82009-01-30 02:52:17 +00001405 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1406 DAG.getConstant(VT.getSizeInBits()-1,
1407 TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00001408 AddToWorkList(SGN.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001411 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1412 DAG.getConstant(VT.getSizeInBits() - lg2,
1413 TLI.getShiftAmountTy()));
1414 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001415 AddToWorkList(SRL.getNode());
1416 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling09f5dc82009-01-30 02:52:17 +00001417 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
1418 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
1419
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 // If we're dividing by a positive value, we're done. Otherwise, we must
1421 // negate the result.
1422 if (pow2 > 0)
1423 return SRA;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001424
Gabor Greif1c80d112008-08-28 21:40:38 +00001425 AddToWorkList(SRA.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001426 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1427 DAG.getConstant(0, VT), SRA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428 }
Bill Wendling09f5dc82009-01-30 02:52:17 +00001429
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 // if integer divide is expensive and we satisfy the requirements, emit an
1431 // alternate sequence.
Dan Gohman40686732008-09-26 21:54:37 +00001432 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001434 SDValue Op = BuildSDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001435 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 }
1437
1438 // undef / X -> 0
1439 if (N0.getOpcode() == ISD::UNDEF)
1440 return DAG.getConstant(0, VT);
1441 // X / undef -> undef
1442 if (N1.getOpcode() == ISD::UNDEF)
1443 return N1;
1444
Dan Gohman8181bd12008-07-27 21:46:04 +00001445 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001446}
1447
Dan Gohman8181bd12008-07-27 21:46:04 +00001448SDValue DAGCombiner::visitUDIV(SDNode *N) {
1449 SDValue N0 = N->getOperand(0);
1450 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001451 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1452 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands92c43912008-06-06 12:08:01 +00001453 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454
1455 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001456 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001458 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 }
1460
1461 // fold (udiv c1, c2) -> c1/c2
1462 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001463 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001465 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingb3552a52009-01-30 02:55:25 +00001466 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001467 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 TLI.getShiftAmountTy()));
1469 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1470 if (N1.getOpcode() == ISD::SHL) {
1471 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001472 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001473 MVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendlingb3552a52009-01-30 02:55:25 +00001474 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1475 N1.getOperand(1),
1476 DAG.getConstant(SHC->getAPIntValue()
1477 .logBase2(),
1478 ADDVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001479 AddToWorkList(Add.getNode());
Bill Wendlingb3552a52009-01-30 02:55:25 +00001480 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 }
1482 }
1483 }
1484 // fold (udiv x, c) -> alternate
Dan Gohman9d24dc72008-03-13 22:13:53 +00001485 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001486 SDValue Op = BuildUDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001487 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488 }
1489
1490 // undef / X -> 0
1491 if (N0.getOpcode() == ISD::UNDEF)
1492 return DAG.getConstant(0, VT);
1493 // X / undef -> undef
1494 if (N1.getOpcode() == ISD::UNDEF)
1495 return N1;
1496
Dan Gohman8181bd12008-07-27 21:46:04 +00001497 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498}
1499
Dan Gohman8181bd12008-07-27 21:46:04 +00001500SDValue DAGCombiner::visitSREM(SDNode *N) {
1501 SDValue N0 = N->getOperand(0);
1502 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1504 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001505 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506
1507 // fold (srem c1, c2) -> c1%c2
1508 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001509 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 // If we know the sign bits of both operands are zero, strength reduce to a
1511 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands92c43912008-06-06 12:08:01 +00001512 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001513 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendlingceba88e2009-01-30 02:57:00 +00001514 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattnerce602f52008-01-27 23:21:58 +00001515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001517 // If X/C can be simplified by the division-by-constant logic, lower
1518 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001520 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001521 AddToWorkList(Div.getNode());
1522 SDValue OptimizedDiv = combine(Div.getNode());
1523 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001524 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1525 OptimizedDiv, N1);
1526 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001527 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001528 return Sub;
1529 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001530 }
1531
1532 // undef % X -> 0
1533 if (N0.getOpcode() == ISD::UNDEF)
1534 return DAG.getConstant(0, VT);
1535 // X % undef -> undef
1536 if (N1.getOpcode() == ISD::UNDEF)
1537 return N1;
1538
Dan Gohman8181bd12008-07-27 21:46:04 +00001539 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540}
1541
Dan Gohman8181bd12008-07-27 21:46:04 +00001542SDValue DAGCombiner::visitUREM(SDNode *N) {
1543 SDValue N0 = N->getOperand(0);
1544 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1546 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001547 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548
1549 // fold (urem c1, c2) -> c1%c2
1550 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001551 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001553 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingceba88e2009-01-30 02:57:00 +00001554 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001555 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1557 if (N1.getOpcode() == ISD::SHL) {
1558 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001559 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001560 SDValue Add =
Bill Wendlingceba88e2009-01-30 02:57:00 +00001561 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands92c43912008-06-06 12:08:01 +00001562 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001563 VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001564 AddToWorkList(Add.getNode());
Bill Wendlingceba88e2009-01-30 02:57:00 +00001565 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 }
1567 }
1568 }
1569
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001570 // If X/C can be simplified by the division-by-constant logic, lower
1571 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001573 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman2e1517f2008-09-08 16:59:01 +00001574 AddToWorkList(Div.getNode());
Gabor Greif1c80d112008-08-28 21:40:38 +00001575 SDValue OptimizedDiv = combine(Div.getNode());
1576 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001577 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1578 OptimizedDiv, N1);
1579 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001580 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001581 return Sub;
1582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583 }
1584
1585 // undef % X -> 0
1586 if (N0.getOpcode() == ISD::UNDEF)
1587 return DAG.getConstant(0, VT);
1588 // X % undef -> undef
1589 if (N1.getOpcode() == ISD::UNDEF)
1590 return N1;
1591
Dan Gohman8181bd12008-07-27 21:46:04 +00001592 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593}
1594
Dan Gohman8181bd12008-07-27 21:46:04 +00001595SDValue DAGCombiner::visitMULHS(SDNode *N) {
1596 SDValue N0 = N->getOperand(0);
1597 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001598 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001599 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600
1601 // fold (mulhs x, 0) -> 0
1602 if (N1C && N1C->isNullValue())
1603 return N1;
1604 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001605 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendlingff9beb92009-01-30 03:00:18 +00001606 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1607 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 TLI.getShiftAmountTy()));
1609 // fold (mulhs x, undef) -> 0
1610 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1611 return DAG.getConstant(0, VT);
1612
Dan Gohman8181bd12008-07-27 21:46:04 +00001613 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614}
1615
Dan Gohman8181bd12008-07-27 21:46:04 +00001616SDValue DAGCombiner::visitMULHU(SDNode *N) {
1617 SDValue N0 = N->getOperand(0);
1618 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001619 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001620 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001621
1622 // fold (mulhu x, 0) -> 0
1623 if (N1C && N1C->isNullValue())
1624 return N1;
1625 // fold (mulhu x, 1) -> 0
Dan Gohman9d24dc72008-03-13 22:13:53 +00001626 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 return DAG.getConstant(0, N0.getValueType());
1628 // fold (mulhu x, undef) -> 0
1629 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1630 return DAG.getConstant(0, VT);
1631
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633}
1634
Dan Gohman6c89ea72007-10-08 17:57:15 +00001635/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1636/// compute two values. LoOp and HiOp give the opcodes for the two computations
1637/// that are being performed. Return true if a simplification was made.
1638///
Dan Gohman8181bd12008-07-27 21:46:04 +00001639SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1640 unsigned HiOp) {
Dan Gohman6c89ea72007-10-08 17:57:15 +00001641 // If the high half is not needed, just compute the low half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001642 bool HiExists = N->hasAnyUseOfValue(1);
1643 if (!HiExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001644 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001645 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001646 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1647 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001648 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001649 }
1650
1651 // If the low half is not needed, just compute the high half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001652 bool LoExists = N->hasAnyUseOfValue(0);
1653 if (!LoExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001654 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001655 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001656 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1657 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001658 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001659 }
1660
Evan Chengddfa8c72007-11-08 09:25:29 +00001661 // If both halves are used, return as it is.
1662 if (LoExists && HiExists)
Dan Gohman8181bd12008-07-27 21:46:04 +00001663 return SDValue();
Evan Chengddfa8c72007-11-08 09:25:29 +00001664
1665 // If the two computed results can be simplified separately, separate them.
Evan Chengddfa8c72007-11-08 09:25:29 +00001666 if (LoExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001667 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1668 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001669 AddToWorkList(Lo.getNode());
1670 SDValue LoOpt = combine(Lo.getNode());
1671 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001672 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001673 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001674 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001675 }
1676
Evan Chengddfa8c72007-11-08 09:25:29 +00001677 if (HiExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001678 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001679 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001680 AddToWorkList(Hi.getNode());
1681 SDValue HiOpt = combine(Hi.getNode());
1682 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001683 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001684 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001685 return CombineTo(N, HiOpt, HiOpt);
Evan Chengddfa8c72007-11-08 09:25:29 +00001686 }
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001687
Dan Gohman8181bd12008-07-27 21:46:04 +00001688 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001689}
1690
Dan Gohman8181bd12008-07-27 21:46:04 +00001691SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1692 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00001693 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001694
Dan Gohman8181bd12008-07-27 21:46:04 +00001695 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001696}
1697
Dan Gohman8181bd12008-07-27 21:46:04 +00001698SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1699 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greif1c80d112008-08-28 21:40:38 +00001700 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001701
Dan Gohman8181bd12008-07-27 21:46:04 +00001702 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001703}
1704
Dan Gohman8181bd12008-07-27 21:46:04 +00001705SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1706 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001707 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001708
Dan Gohman8181bd12008-07-27 21:46:04 +00001709 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001710}
1711
Dan Gohman8181bd12008-07-27 21:46:04 +00001712SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1713 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001714 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001715
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001717}
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1720/// two operands of the same opcode, try to simplify it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001721SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1722 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00001723 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1725
1726 // For each of OP in AND/OR/XOR:
1727 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1728 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1729 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
1730 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
1731 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
1732 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
1733 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001734 SDValue ORNode = DAG.getNode(N->getOpcode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 N0.getOperand(0).getValueType(),
1736 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001737 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001738 return DAG.getNode(N0.getOpcode(), VT, ORNode);
1739 }
1740
1741 // For each of OP in SHL/SRL/SRA/AND...
1742 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1743 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1744 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
1745 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1746 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
1747 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001748 SDValue ORNode = DAG.getNode(N->getOpcode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 N0.getOperand(0).getValueType(),
1750 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001751 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1753 }
1754
Dan Gohman8181bd12008-07-27 21:46:04 +00001755 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756}
1757
Dan Gohman8181bd12008-07-27 21:46:04 +00001758SDValue DAGCombiner::visitAND(SDNode *N) {
1759 SDValue N0 = N->getOperand(0);
1760 SDValue N1 = N->getOperand(1);
1761 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1763 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001764 MVT VT = N1.getValueType();
1765 unsigned BitWidth = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766
1767 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001768 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001769 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001770 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 }
1772
1773 // fold (and x, undef) -> 0
1774 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1775 return DAG.getConstant(0, VT);
1776 // fold (and c1, c2) -> c1&c2
1777 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001778 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 // canonicalize constant to RHS
1780 if (N0C && !N1C)
1781 return DAG.getNode(ISD::AND, VT, N1, N0);
1782 // fold (and x, -1) -> x
1783 if (N1C && N1C->isAllOnesValue())
1784 return N0;
1785 // if (and x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001786 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00001787 APInt::getAllOnesValue(BitWidth)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001788 return DAG.getConstant(0, VT);
1789 // reassociate and
Bill Wendlingabb33a22009-01-30 00:45:56 +00001790 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001791 if (RAND.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792 return RAND;
1793 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1794 if (N1C && N0.getOpcode() == ISD::OR)
1795 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001796 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 return N1;
1798 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1799 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001800 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman07961cd2008-02-25 21:11:39 +00001801 APInt Mask = ~N1C->getAPIntValue();
1802 Mask.trunc(N0Op0.getValueSizeInBits());
1803 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001804 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman07961cd2008-02-25 21:11:39 +00001805 N0Op0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806
1807 // Replace uses of the AND with uses of the Zero extend node.
1808 CombineTo(N, Zext);
1809
1810 // We actually want to replace all uses of the any_extend with the
1811 // zero_extend, to avoid duplicating things. This will later cause this
1812 // AND to be folded.
Gabor Greif1c80d112008-08-28 21:40:38 +00001813 CombineTo(N0.getNode(), Zext);
Dan Gohman8181bd12008-07-27 21:46:04 +00001814 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001815 }
1816 }
1817 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1818 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1819 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1820 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1821
1822 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001823 LL.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001824 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001825 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001826 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001827 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 return DAG.getSetCC(VT, ORNode, LR, Op1);
1829 }
1830 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1831 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001832 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001833 AddToWorkList(ANDNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001834 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1835 }
1836 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1837 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001838 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001839 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 return DAG.getSetCC(VT, ORNode, LR, Op1);
1841 }
1842 }
1843 // canonicalize equivalent to ll == rl
1844 if (LL == RR && LR == RL) {
1845 Op1 = ISD::getSetCCSwappedOperands(Op1);
1846 std::swap(RL, RR);
1847 }
1848 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00001849 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001850 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00001851 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001852 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001853 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1854 }
1855 }
1856
1857 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1858 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001859 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001860 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001861 }
1862
1863 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1864 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands92c43912008-06-06 12:08:01 +00001865 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SimplifyDemandedBits(SDValue(N, 0)))
1867 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001868 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00001869 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001870 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00001871 MVT EVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872 // If we zero all the possible extended bits, then we can turn this into
1873 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001874 unsigned BitWidth = N1.getValueSizeInBits();
1875 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands92c43912008-06-06 12:08:01 +00001876 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001877 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00001878 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001879 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001880 LN0->getBasePtr(), LN0->getSrcValue(),
1881 LN0->getSrcValueOffset(), EVT,
1882 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001883 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001884 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001885 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001886 }
1887 }
1888 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00001889 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 N0.hasOneUse()) {
1891 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00001892 MVT EVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001893 // If we zero all the possible extended bits, then we can turn this into
1894 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001895 unsigned BitWidth = N1.getValueSizeInBits();
1896 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands92c43912008-06-06 12:08:01 +00001897 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001898 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00001899 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001900 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001901 LN0->getBasePtr(), LN0->getSrcValue(),
1902 LN0->getSrcValueOffset(), EVT,
1903 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001905 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001906 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001907 }
1908 }
1909
1910 // fold (and (load x), 255) -> (zextload x, i8)
1911 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1912 if (N1C && N0.getOpcode() == ISD::LOAD) {
1913 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1914 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001915 LN0->isUnindexed() && N0.hasOneUse() &&
1916 // Do not change the width of a volatile load.
1917 !LN0->isVolatile()) {
Duncan Sands6a437fb2008-06-09 11:32:28 +00001918 MVT EVT = MVT::Other;
1919 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1920 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1921 EVT = MVT::getIntegerVT(ActiveBits);
1922
1923 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sands3ea93352008-06-16 08:14:38 +00001924 // Do not generate loads of non-round integer types since these can
1925 // be expensive (and would be wrong if the type is not byte sized).
1926 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001927 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands92c43912008-06-06 12:08:01 +00001928 MVT PtrType = N0.getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 // For big endian targets, we need to add an offset to the pointer to
1930 // load the correct bytes. For little endian systems, we merely need to
1931 // read fewer bytes from the same pointer.
Duncan Sands92c43912008-06-06 12:08:01 +00001932 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1933 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sands4f18d4f2007-11-09 08:57:19 +00001934 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsa3691432007-10-28 12:59:45 +00001935 unsigned Alignment = LN0->getAlignment();
Dan Gohman8181bd12008-07-27 21:46:04 +00001936 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00001937 if (TLI.isBigEndian()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1939 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsa3691432007-10-28 12:59:45 +00001940 Alignment = MinAlign(Alignment, PtrOff);
1941 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001942 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00001943 SDValue Load =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
1945 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsa3691432007-10-28 12:59:45 +00001946 LN0->isVolatile(), Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001947 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001948 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001949 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 }
1951 }
1952 }
1953
Dan Gohman8181bd12008-07-27 21:46:04 +00001954 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001955}
1956
Dan Gohman8181bd12008-07-27 21:46:04 +00001957SDValue DAGCombiner::visitOR(SDNode *N) {
1958 SDValue N0 = N->getOperand(0);
1959 SDValue N1 = N->getOperand(1);
1960 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001961 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1962 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00001963 MVT VT = N1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964
1965 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001966 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001967 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001968 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 }
1970
1971 // fold (or x, undef) -> -1
1972 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1973 return DAG.getConstant(~0ULL, VT);
1974 // fold (or c1, c2) -> c1|c2
1975 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001976 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977 // canonicalize constant to RHS
1978 if (N0C && !N1C)
1979 return DAG.getNode(ISD::OR, VT, N1, N0);
1980 // fold (or x, 0) -> x
1981 if (N1C && N1C->isNullValue())
1982 return N0;
1983 // fold (or x, -1) -> -1
1984 if (N1C && N1C->isAllOnesValue())
1985 return N1;
1986 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman07961cd2008-02-25 21:11:39 +00001987 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 return N1;
1989 // reassociate or
Bill Wendlingabb33a22009-01-30 00:45:56 +00001990 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001991 if (ROR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001992 return ROR;
1993 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00001994 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995 isa<ConstantSDNode>(N0.getOperand(1))) {
1996 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1997 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1998 N1),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001999 DAG.getConstant(N1C->getAPIntValue() |
2000 C1->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 }
2002 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2003 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2004 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2005 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
2006
2007 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00002008 LL.getValueType().isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
2010 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002011 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002013 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002014 AddToWorkList(ORNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 return DAG.getSetCC(VT, ORNode, LR, Op1);
2016 }
2017 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
2018 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
2019 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2020 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002021 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002022 AddToWorkList(ANDNode.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002023 return DAG.getSetCC(VT, ANDNode, LR, Op1);
2024 }
2025 }
2026 // canonicalize equivalent to ll == rl
2027 if (LL == RR && LR == RL) {
2028 Op1 = ISD::getSetCCSwappedOperands(Op1);
2029 std::swap(RL, RR);
2030 }
2031 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00002032 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00002034 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002035 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
2037 }
2038 }
2039
2040 // Simplify: or (op x...), (op y...) -> (op (or x, y))
2041 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002042 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002043 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044 }
2045
2046 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
2047 if (N0.getOpcode() == ISD::AND &&
2048 N1.getOpcode() == ISD::AND &&
2049 N0.getOperand(1).getOpcode() == ISD::Constant &&
2050 N1.getOperand(1).getOpcode() == ISD::Constant &&
2051 // Don't increase # computations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002052 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 // We can only do this xform if we know that bits from X that are set in C2
2054 // but not in C1 are already zero. Likewise for Y.
Dan Gohman07961cd2008-02-25 21:11:39 +00002055 const APInt &LHSMask =
2056 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2057 const APInt &RHSMask =
2058 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002059
2060 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2061 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002062 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002063 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
2064 }
2065 }
2066
2067
2068 // See if this is some rotate idiom.
2069 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman8181bd12008-07-27 21:46:04 +00002070 return SDValue(Rot, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002071
Dan Gohman8181bd12008-07-27 21:46:04 +00002072 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002073}
2074
2075
2076/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002077static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002078 if (Op.getOpcode() == ISD::AND) {
2079 if (isa<ConstantSDNode>(Op.getOperand(1))) {
2080 Mask = Op.getOperand(1);
2081 Op = Op.getOperand(0);
2082 } else {
2083 return false;
2084 }
2085 }
2086
2087 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2088 Shift = Op;
2089 return true;
2090 }
2091 return false;
2092}
2093
2094
2095// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2096// idioms for rotate, and if the target supports rotation instructions, generate
2097// a rot[lr].
Dan Gohman8181bd12008-07-27 21:46:04 +00002098SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sands2418bec2008-06-13 19:07:40 +00002099 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands92c43912008-06-06 12:08:01 +00002100 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002101 if (!TLI.isTypeLegal(VT)) return 0;
2102
2103 // The target must have at least one rotate flavor.
Dan Gohman52c51aa2009-01-28 17:46:25 +00002104 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2105 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 if (!HasROTL && !HasROTR) return 0;
Duncan Sands2418bec2008-06-13 19:07:40 +00002107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002109 SDValue LHSShift; // The shift.
2110 SDValue LHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002111 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2112 return 0; // Not part of a rotate.
2113
Dan Gohman8181bd12008-07-27 21:46:04 +00002114 SDValue RHSShift; // The shift.
2115 SDValue RHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2117 return 0; // Not part of a rotate.
2118
2119 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2120 return 0; // Not shifting the same value.
2121
2122 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2123 return 0; // Shifts must disagree.
2124
2125 // Canonicalize shl to left side in a shl/srl pair.
2126 if (RHSShift.getOpcode() == ISD::SHL) {
2127 std::swap(LHS, RHS);
2128 std::swap(LHSShift, RHSShift);
2129 std::swap(LHSMask , RHSMask );
2130 }
2131
Duncan Sands92c43912008-06-06 12:08:01 +00002132 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00002133 SDValue LHSShiftArg = LHSShift.getOperand(0);
2134 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2135 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136
2137 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2138 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
2139 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2140 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002141 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2142 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002143 if ((LShVal + RShVal) != OpSizeInBits)
2144 return 0;
2145
Dan Gohman8181bd12008-07-27 21:46:04 +00002146 SDValue Rot;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 if (HasROTL)
2148 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
2149 else
2150 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
2151
2152 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00002153 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002154 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155
Gabor Greif1c80d112008-08-28 21:40:38 +00002156 if (LHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002157 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2158 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002160 if (RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002161 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2162 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 }
2164
2165 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2166 }
2167
Gabor Greif1c80d112008-08-28 21:40:38 +00002168 return Rot.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002169 }
2170
2171 // If there is a mask here, and we have a variable shift, we can't be sure
2172 // that we're masking out the right stuff.
Gabor Greif1c80d112008-08-28 21:40:38 +00002173 if (LHSMask.getNode() || RHSMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002174 return 0;
2175
2176 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2177 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
2178 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2179 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
2180 if (ConstantSDNode *SUBC =
2181 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002182 if (SUBC->getAPIntValue() == OpSizeInBits) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002183 if (HasROTL)
Gabor Greif1c80d112008-08-28 21:40:38 +00002184 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002185 else
Gabor Greif1c80d112008-08-28 21:40:38 +00002186 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002187 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002188 }
2189 }
2190
2191 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2192 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
2193 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2194 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
2195 if (ConstantSDNode *SUBC =
2196 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002197 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling89f05f52008-08-31 01:13:31 +00002198 if (HasROTR)
Gabor Greif1c80d112008-08-28 21:40:38 +00002199 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling89f05f52008-08-31 01:13:31 +00002200 else
2201 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002202 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002203 }
2204 }
2205
Dan Gohman921581d2008-10-17 01:23:35 +00002206 // Look for sign/zext/any-extended or truncate cases:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002207 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2208 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002209 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2210 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002211 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2212 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002213 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2214 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002215 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2216 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002217 if (RExtOp0.getOpcode() == ISD::SUB &&
2218 RExtOp0.getOperand(1) == LExtOp0) {
2219 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002220 // (rotl x, y)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002221 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002222 // (rotr x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002223 if (ConstantSDNode *SUBC =
2224 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002225 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greifb420b9d2008-08-30 19:29:20 +00002226 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2227 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002228 }
2229 }
2230 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2231 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendlinga70293d2008-08-31 01:04:56 +00002232 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002233 // (rotr x, y)
Bill Wendlinga70293d2008-08-31 01:04:56 +00002234 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002235 // (rotl x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002236 if (ConstantSDNode *SUBC =
2237 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002238 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendlinga70293d2008-08-31 01:04:56 +00002239 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2240 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002241 }
2242 }
2243 }
2244 }
2245
2246 return 0;
2247}
2248
2249
Dan Gohman8181bd12008-07-27 21:46:04 +00002250SDValue DAGCombiner::visitXOR(SDNode *N) {
2251 SDValue N0 = N->getOperand(0);
2252 SDValue N1 = N->getOperand(1);
2253 SDValue LHS, RHS, CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2255 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002256 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002257
2258 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00002259 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002260 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002261 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002262 }
2263
Evan Cheng5d00cb42008-03-25 20:08:07 +00002264 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2265 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2266 return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002267 // fold (xor x, undef) -> undef
2268 if (N0.getOpcode() == ISD::UNDEF)
2269 return N0;
2270 if (N1.getOpcode() == ISD::UNDEF)
2271 return N1;
2272 // fold (xor c1, c2) -> c1^c2
2273 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002274 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 // canonicalize constant to RHS
2276 if (N0C && !N1C)
2277 return DAG.getNode(ISD::XOR, VT, N1, N0);
2278 // fold (xor x, 0) -> x
2279 if (N1C && N1C->isNullValue())
2280 return N0;
2281 // reassociate xor
Bill Wendlingabb33a22009-01-30 00:45:56 +00002282 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002283 if (RXOR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 return RXOR;
Bill Wendling0d810b82008-11-11 08:25:46 +00002285
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002286 // fold !(x cc y) -> (x !cc y)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002287 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002288 bool isInt = LHS.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2290 isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +00002291
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002292 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendling0d810b82008-11-11 08:25:46 +00002293 switch (N0.getOpcode()) {
2294 default:
2295 assert(0 && "Unhandled SetCC Equivalent!");
2296 abort();
2297 case ISD::SETCC:
2298 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2299 case ISD::SELECT_CC:
2300 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2301 N0.getOperand(3), NotCC);
2302 }
2303 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 }
Bill Wendling0d810b82008-11-11 08:25:46 +00002305
Chris Lattnere27cd502007-09-10 21:39:07 +00002306 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002307 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00002308 N0.getNode()->hasOneUse() &&
2309 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman8181bd12008-07-27 21:46:04 +00002310 SDValue V = N0.getOperand(0);
Chris Lattnere27cd502007-09-10 21:39:07 +00002311 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sandsbed21472007-10-10 09:54:50 +00002312 DAG.getConstant(1, V.getValueType()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002313 AddToWorkList(V.getNode());
Chris Lattnere27cd502007-09-10 21:39:07 +00002314 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2315 }
2316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman9d24dc72008-03-13 22:13:53 +00002318 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002319 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002320 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002321 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2322 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
2323 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2324 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002325 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002326 return DAG.getNode(NewOpcode, VT, LHS, RHS);
2327 }
2328 }
2329 // fold !(x or y) -> (!x and !y) iff x or y are constants
2330 if (N1C && N1C->isAllOnesValue() &&
2331 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002332 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2334 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
2335 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2336 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002337 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 return DAG.getNode(NewOpcode, VT, LHS, RHS);
2339 }
2340 }
2341 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2342 if (N1C && N0.getOpcode() == ISD::XOR) {
2343 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2344 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2345 if (N00C)
2346 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman9d24dc72008-03-13 22:13:53 +00002347 DAG.getConstant(N1C->getAPIntValue()^
2348 N00C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002349 if (N01C)
2350 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman9d24dc72008-03-13 22:13:53 +00002351 DAG.getConstant(N1C->getAPIntValue()^
2352 N01C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353 }
2354 // fold (xor x, x) -> 0
2355 if (N0 == N1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002356 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 return DAG.getConstant(0, VT);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002358 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 // Produce a vector of zeros.
Dan Gohman8181bd12008-07-27 21:46:04 +00002360 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2361 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
2363 }
2364 }
2365
2366 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2367 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002368 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002369 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 }
2371
2372 // Simplify the expression using non-local knowledge.
Duncan Sands92c43912008-06-06 12:08:01 +00002373 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00002374 SimplifyDemandedBits(SDValue(N, 0)))
2375 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376
Dan Gohman8181bd12008-07-27 21:46:04 +00002377 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002378}
2379
Chris Lattner91ed3c32007-12-06 07:33:36 +00002380/// visitShiftByConstant - Handle transforms common to the three shifts, when
2381/// the shift amount is a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +00002382SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002383 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +00002384 if (!LHS->hasOneUse()) return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002385
2386 // We want to pull some binops through shifts, so that we have (and (shift))
2387 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2388 // thing happens with address calculations, so it's important to canonicalize
2389 // it.
2390 bool HighBitSet = false; // Can we transform this if the high bit is set?
2391
2392 switch (LHS->getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002393 default: return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002394 case ISD::OR:
2395 case ISD::XOR:
2396 HighBitSet = false; // We can only transform sra if the high bit is clear.
2397 break;
2398 case ISD::AND:
2399 HighBitSet = true; // We can only transform sra if the high bit is set.
2400 break;
2401 case ISD::ADD:
2402 if (N->getOpcode() != ISD::SHL)
Dan Gohman8181bd12008-07-27 21:46:04 +00002403 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner91ed3c32007-12-06 07:33:36 +00002404 HighBitSet = false; // We can only transform sra if the high bit is clear.
2405 break;
2406 }
2407
2408 // We require the RHS of the binop to be a constant as well.
2409 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002410 if (!BinOpCst) return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002411
Chris Lattnerdcd19762007-12-06 07:47:55 +00002412
2413 // FIXME: disable this for unless the input to the binop is a shift by a
2414 // constant. If it is not a shift, it pessimizes some common cases like:
2415 //
2416 //void foo(int *X, int i) { X[i & 1235] = 1; }
2417 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greif1c80d112008-08-28 21:40:38 +00002418 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerdcd19762007-12-06 07:47:55 +00002419 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2420 BinOpLHSVal->getOpcode() != ISD::SRA &&
2421 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2422 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00002423 return SDValue();
Chris Lattnerdcd19762007-12-06 07:47:55 +00002424
Duncan Sands92c43912008-06-06 12:08:01 +00002425 MVT VT = N->getValueType(0);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002426
2427 // If this is a signed shift right, and the high bit is modified
2428 // by the logical operation, do not perform the transformation.
2429 // The highBitSet boolean indicates the value of the high bit of
2430 // the constant which would cause it to be modified for this
2431 // operation.
2432 if (N->getOpcode() == ISD::SRA) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002433 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2434 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman8181bd12008-07-27 21:46:04 +00002435 return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002436 }
2437
2438 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00002439 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattner91ed3c32007-12-06 07:33:36 +00002440 LHS->getOperand(1), N->getOperand(1));
2441
2442 // Create the new shift.
Dan Gohman8181bd12008-07-27 21:46:04 +00002443 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattner91ed3c32007-12-06 07:33:36 +00002444 N->getOperand(1));
2445
2446 // Create the new binop.
2447 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2448}
2449
2450
Dan Gohman8181bd12008-07-27 21:46:04 +00002451SDValue DAGCombiner::visitSHL(SDNode *N) {
2452 SDValue N0 = N->getOperand(0);
2453 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2455 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002456 MVT VT = N0.getValueType();
2457 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002458
2459 // fold (shl c1, c2) -> c1<<c2
2460 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002461 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462 // fold (shl 0, x) -> 0
2463 if (N0C && N0C->isNullValue())
2464 return N0;
2465 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002466 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002467 return DAG.getNode(ISD::UNDEF, VT);
2468 // fold (shl x, 0) -> x
2469 if (N1C && N1C->isNullValue())
2470 return N0;
2471 // if (shl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002472 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands92c43912008-06-06 12:08:01 +00002473 APInt::getAllOnesValue(VT.getSizeInBits())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 return DAG.getConstant(0, VT);
Evan Cheng76a64c72008-08-30 02:03:58 +00002475 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2476 // iff (trunc c) == c
2477 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002478 N1.getOperand(0).getOpcode() == ISD::AND &&
2479 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002480 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002481 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002482 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002483 SDValue N100 = N1.getOperand(0).getOperand(0);
djg51bef2e2009-01-27 20:39:34 +00002484 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2485 N101C->getZExtValue();
Evan Cheng11c34292008-09-22 18:19:24 +00002486 return DAG.getNode(ISD::SHL, VT, N0,
2487 DAG.getNode(ISD::AND, TruncVT,
2488 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002489 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002490 }
2491 }
2492
Dan Gohman8181bd12008-07-27 21:46:04 +00002493 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2494 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
2496 if (N1C && N0.getOpcode() == ISD::SHL &&
2497 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002498 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2499 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 if (c1 + c2 > OpSizeInBits)
2501 return DAG.getConstant(0, VT);
2502 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
2503 DAG.getConstant(c1 + c2, N1.getValueType()));
2504 }
2505 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2506 // (srl (and x, -1 << c1), c1-c2)
2507 if (N1C && N0.getOpcode() == ISD::SRL &&
2508 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002509 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2510 uint64_t c2 = N1C->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00002511 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 DAG.getConstant(~0ULL << c1, VT));
2513 if (c2 > c1)
2514 return DAG.getNode(ISD::SHL, VT, Mask,
2515 DAG.getConstant(c2-c1, N1.getValueType()));
2516 else
2517 return DAG.getNode(ISD::SRL, VT, Mask,
2518 DAG.getConstant(c1-c2, N1.getValueType()));
2519 }
2520 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
2521 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
2522 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002523 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002524
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002525 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002526}
2527
Dan Gohman8181bd12008-07-27 21:46:04 +00002528SDValue DAGCombiner::visitSRA(SDNode *N) {
2529 SDValue N0 = N->getOperand(0);
2530 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2532 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002533 MVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534
2535 // fold (sra c1, c2) -> c1>>c2
2536 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002537 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538 // fold (sra 0, x) -> 0
2539 if (N0C && N0C->isNullValue())
2540 return N0;
2541 // fold (sra -1, x) -> -1
2542 if (N0C && N0C->isAllOnesValue())
2543 return N0;
2544 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002545 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 return DAG.getNode(ISD::UNDEF, VT);
2547 // fold (sra x, 0) -> x
2548 if (N1C && N1C->isNullValue())
2549 return N0;
2550 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2551 // sext_inreg.
2552 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002553 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands6a437fb2008-06-09 11:32:28 +00002554 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002555 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2557 DAG.getValueType(EVT));
2558 }
Duncan Sands2418bec2008-06-13 19:07:40 +00002559
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2561 if (N1C && N0.getOpcode() == ISD::SRA) {
2562 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002563 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002564 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2566 DAG.getConstant(Sum, N1C->getValueType(0)));
2567 }
2568 }
Christopher Lambfc5c1642008-03-19 08:30:06 +00002569
2570 // fold sra (shl X, m), result_size - n
2571 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lamb21e8a952008-03-20 04:31:39 +00002572 // result_size - n != m.
2573 // If truncate is free for the target sext(shl) is likely to result in better
2574 // code.
Christopher Lambfc5c1642008-03-19 08:30:06 +00002575 if (N0.getOpcode() == ISD::SHL) {
2576 // Get the two constanst of the shifts, CN0 = m, CN = n.
2577 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2578 if (N01C && N1C) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002579 // Determine what the truncate's result bitsize and type would be.
Duncan Sands92c43912008-06-06 12:08:01 +00002580 unsigned VTValSize = VT.getSizeInBits();
2581 MVT TruncVT =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002582 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lamb21e8a952008-03-20 04:31:39 +00002583 // Determine the residual right-shift amount.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002584 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands2418bec2008-06-13 19:07:40 +00002585
Christopher Lamb21e8a952008-03-20 04:31:39 +00002586 // If the shift is not a no-op (in which case this should be just a sign
2587 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greifb420b9d2008-08-30 19:29:20 +00002588 // on that type, and the the truncate to that type is both legal and free,
Christopher Lamb21e8a952008-03-20 04:31:39 +00002589 // perform the transform.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002590 if (ShiftAmt &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00002591 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2592 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Chengca0e80f2008-03-20 02:18:41 +00002593 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002594
Dan Gohman8181bd12008-07-27 21:46:04 +00002595 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2596 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2597 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lamb21e8a952008-03-20 04:31:39 +00002598 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lambfc5c1642008-03-19 08:30:06 +00002599 }
2600 }
2601 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602
Evan Cheng76a64c72008-08-30 02:03:58 +00002603 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2604 // iff (trunc c) == c
2605 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002606 N1.getOperand(0).getOpcode() == ISD::AND &&
2607 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002608 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002609 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002610 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002611 SDValue N100 = N1.getOperand(0).getOperand(0);
djg51bef2e2009-01-27 20:39:34 +00002612 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2613 N101C->getZExtValue();
Evan Cheng11c34292008-09-22 18:19:24 +00002614 return DAG.getNode(ISD::SRA, VT, N0,
2615 DAG.getNode(ISD::AND, TruncVT,
2616 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002617 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002618 }
2619 }
2620
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002621 // Simplify, based on bits shifted out of the LHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00002622 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2623 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624
2625
2626 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman07961cd2008-02-25 21:11:39 +00002627 if (DAG.SignBitIsZero(N0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002629
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002630 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631}
2632
Dan Gohman8181bd12008-07-27 21:46:04 +00002633SDValue DAGCombiner::visitSRL(SDNode *N) {
2634 SDValue N0 = N->getOperand(0);
2635 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2637 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00002638 MVT VT = N0.getValueType();
2639 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640
2641 // fold (srl c1, c2) -> c1 >>u c2
2642 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002643 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 // fold (srl 0, x) -> 0
2645 if (N0C && N0C->isNullValue())
2646 return N0;
2647 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002648 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 return DAG.getNode(ISD::UNDEF, VT);
2650 // fold (srl x, 0) -> x
2651 if (N1C && N1C->isNullValue())
2652 return N0;
2653 // if (srl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002654 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00002655 APInt::getAllOnesValue(OpSizeInBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656 return DAG.getConstant(0, VT);
2657
2658 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
2659 if (N1C && N0.getOpcode() == ISD::SRL &&
2660 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002661 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2662 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 if (c1 + c2 > OpSizeInBits)
2664 return DAG.getConstant(0, VT);
2665 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
2666 DAG.getConstant(c1 + c2, N1.getValueType()));
2667 }
2668
2669 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2670 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2671 // Shifting in all undef bits?
Duncan Sands92c43912008-06-06 12:08:01 +00002672 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002673 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 return DAG.getNode(ISD::UNDEF, VT);
2675
Dan Gohman8181bd12008-07-27 21:46:04 +00002676 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002677 AddToWorkList(SmallShift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2679 }
2680
2681 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2682 // bit, which is unmodified by sra.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002683 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002684 if (N0.getOpcode() == ISD::SRA)
2685 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2686 }
2687
2688 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2689 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands92c43912008-06-06 12:08:01 +00002690 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00002691 APInt KnownZero, KnownOne;
Duncan Sands92c43912008-06-06 12:08:01 +00002692 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
2694
2695 // If any of the input bits are KnownOne, then the input couldn't be all
2696 // zeros, thus the result of the srl will always be zero.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002697 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698
2699 // If all of the bits input the to ctlz node are known to be zero, then
2700 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002701 APInt UnknownBits = ~KnownZero & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002702 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2703
2704 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2705 if ((UnknownBits & (UnknownBits-1)) == 0) {
2706 // Okay, we know that only that the single bit specified by UnknownBits
2707 // could be set on input to the CTLZ node. If this bit is set, the SRL
2708 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2709 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002710 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman8181bd12008-07-27 21:46:04 +00002711 SDValue Op = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 if (ShAmt) {
2713 Op = DAG.getNode(ISD::SRL, VT, Op,
2714 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002715 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002716 }
2717 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2718 }
2719 }
Evan Cheng76a64c72008-08-30 02:03:58 +00002720
2721 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2722 // iff (trunc c) == c
2723 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002724 N1.getOperand(0).getOpcode() == ISD::AND &&
2725 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002726 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002727 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002728 MVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002729 SDValue N100 = N1.getOperand(0).getOperand(0);
djg51bef2e2009-01-27 20:39:34 +00002730 uint64_t TruncC = TruncVT.getIntegerVTBitMask() &
2731 N101C->getZExtValue();
Evan Cheng11c34292008-09-22 18:19:24 +00002732 return DAG.getNode(ISD::SRL, VT, N0,
2733 DAG.getNode(ISD::AND, TruncVT,
2734 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002735 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002736 }
2737 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002738
2739 // fold operands of srl based on knowledge that the low bits are not
2740 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00002741 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2742 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002744 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745}
2746
Dan Gohman8181bd12008-07-27 21:46:04 +00002747SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2748 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002749 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002750
2751 // fold (ctlz c1) -> c2
2752 if (isa<ConstantSDNode>(N0))
2753 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002754 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755}
2756
Dan Gohman8181bd12008-07-27 21:46:04 +00002757SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2758 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002759 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760
2761 // fold (cttz c1) -> c2
2762 if (isa<ConstantSDNode>(N0))
2763 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002764 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765}
2766
Dan Gohman8181bd12008-07-27 21:46:04 +00002767SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2768 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002769 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770
2771 // fold (ctpop c1) -> c2
2772 if (isa<ConstantSDNode>(N0))
2773 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002774 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775}
2776
Dan Gohman8181bd12008-07-27 21:46:04 +00002777SDValue DAGCombiner::visitSELECT(SDNode *N) {
2778 SDValue N0 = N->getOperand(0);
2779 SDValue N1 = N->getOperand(1);
2780 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002781 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2782 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2783 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands92c43912008-06-06 12:08:01 +00002784 MVT VT = N->getValueType(0);
2785 MVT VT0 = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002786
2787 // fold select C, X, X -> X
2788 if (N1 == N2)
2789 return N1;
2790 // fold select true, X, Y -> X
2791 if (N0C && !N0C->isNullValue())
2792 return N1;
2793 // fold select false, X, Y -> Y
2794 if (N0C && N0C->isNullValue())
2795 return N2;
2796 // fold select C, 1, X -> C | X
Duncan Sands92c43912008-06-06 12:08:01 +00002797 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 return DAG.getNode(ISD::OR, VT, N0, N2);
Bob Wilsonee1fe312009-01-22 22:05:48 +00002799 // fold select C, 0, 1 -> C ^ 1
2800 if (VT.isInteger() &&
2801 (VT0 == MVT::i1 ||
2802 (VT0.isInteger() &&
2803 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00002804 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002805 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Chengff601dc2007-08-18 05:57:05 +00002806 if (VT == VT0)
2807 return XORNode;
Gabor Greif1c80d112008-08-28 21:40:38 +00002808 AddToWorkList(XORNode.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00002809 if (VT.bitsGT(VT0))
Evan Chengff601dc2007-08-18 05:57:05 +00002810 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2811 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2812 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813 // fold select C, 0, X -> ~C & X
Dale Johannesen53e0ad72007-12-06 17:53:31 +00002814 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bob Wilson81a42cf2009-01-22 17:39:32 +00002815 SDValue NOTNode = DAG.getNOT(N0, VT);
2816 AddToWorkList(NOTNode.getNode());
2817 return DAG.getNode(ISD::AND, VT, NOTNode, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 }
2819 // fold select C, X, 1 -> ~C | X
Dan Gohman9d24dc72008-03-13 22:13:53 +00002820 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bob Wilson81a42cf2009-01-22 17:39:32 +00002821 SDValue NOTNode = DAG.getNOT(N0, VT);
2822 AddToWorkList(NOTNode.getNode());
2823 return DAG.getNode(ISD::OR, VT, NOTNode, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002824 }
2825 // fold select C, X, 0 -> C & X
Duncan Sands92c43912008-06-06 12:08:01 +00002826 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002827 return DAG.getNode(ISD::AND, VT, N0, N1);
2828 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands92c43912008-06-06 12:08:01 +00002829 if (VT == MVT::i1 && N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830 return DAG.getNode(ISD::OR, VT, N0, N2);
2831 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands92c43912008-06-06 12:08:01 +00002832 if (VT == MVT::i1 && N0 == N2)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833 return DAG.getNode(ISD::AND, VT, N0, N1);
2834
2835 // If we can fold this based on the true/false value, do so.
2836 if (SimplifySelectOps(N, N1, N2))
Dan Gohman8181bd12008-07-27 21:46:04 +00002837 return SDValue(N, 0); // Don't revisit N.
Duncan Sands2418bec2008-06-13 19:07:40 +00002838
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002840 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 // FIXME:
2842 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2843 // having to say they don't support SELECT_CC on every type the DAG knows
2844 // about, since there is no way to mark an opcode illegal at all value types
Dan Gohman52c51aa2009-01-28 17:46:25 +00002845 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002846 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2847 N1, N2, N0.getOperand(2));
2848 else
2849 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002850 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002851 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852}
2853
Dan Gohman8181bd12008-07-27 21:46:04 +00002854SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2855 SDValue N0 = N->getOperand(0);
2856 SDValue N1 = N->getOperand(1);
2857 SDValue N2 = N->getOperand(2);
2858 SDValue N3 = N->getOperand(3);
2859 SDValue N4 = N->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002860 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2861
2862 // fold select_cc lhs, rhs, x, x, cc -> x
2863 if (N2 == N3)
2864 return N2;
2865
2866 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00002867 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2868 N0, N1, CC, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00002869 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870
Gabor Greif1c80d112008-08-28 21:40:38 +00002871 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002872 if (!SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 return N2; // cond always true -> true val
2874 else
2875 return N3; // cond always false -> false val
2876 }
2877
2878 // Fold to a simpler select_cc
Gabor Greif1c80d112008-08-28 21:40:38 +00002879 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002880 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2881 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2882 SCC.getOperand(2));
2883
2884 // If we can fold this based on the true/false value, do so.
2885 if (SimplifySelectOps(N, N2, N3))
Dan Gohman8181bd12008-07-27 21:46:04 +00002886 return SDValue(N, 0); // Don't revisit N.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002887
2888 // fold select_cc into other things, such as min/max/abs
2889 return SimplifySelectCC(N0, N1, N2, N3, CC);
2890}
2891
Dan Gohman8181bd12008-07-27 21:46:04 +00002892SDValue DAGCombiner::visitSETCC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2894 cast<CondCodeSDNode>(N->getOperand(2))->get());
2895}
2896
Evan Cheng9decb332007-10-29 19:58:20 +00002897// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2898// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2899// transformation. Returns true if extension are possible and the above
2900// mentioned transformation is profitable.
Dan Gohman8181bd12008-07-27 21:46:04 +00002901static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng9decb332007-10-29 19:58:20 +00002902 unsigned ExtOpc,
2903 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman96eb47a2009-01-15 19:20:50 +00002904 const TargetLowering &TLI) {
Evan Cheng9decb332007-10-29 19:58:20 +00002905 bool HasCopyToRegUses = false;
2906 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifb420b9d2008-08-30 19:29:20 +00002907 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2908 UE = N0.getNode()->use_end();
Evan Cheng9decb332007-10-29 19:58:20 +00002909 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002910 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002911 if (User == N)
2912 continue;
2913 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2914 if (User->getOpcode() == ISD::SETCC) {
2915 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2916 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2917 // Sign bits will be lost after a zext.
2918 return false;
2919 bool Add = false;
2920 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002921 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002922 if (UseOp == N0)
2923 continue;
2924 if (!isa<ConstantSDNode>(UseOp))
2925 return false;
2926 Add = true;
2927 }
2928 if (Add)
2929 ExtendNodes.push_back(User);
2930 } else {
2931 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002932 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002933 if (UseOp == N0) {
2934 // If truncate from extended type to original load type is free
2935 // on this target, then it's ok to extend a CopyToReg.
2936 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2937 HasCopyToRegUses = true;
2938 else
2939 return false;
2940 }
2941 }
2942 }
2943 }
2944
2945 if (HasCopyToRegUses) {
2946 bool BothLiveOut = false;
2947 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2948 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002949 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002950 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SDValue UseOp = User->getOperand(i);
Gabor Greif1c80d112008-08-28 21:40:38 +00002952 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng9decb332007-10-29 19:58:20 +00002953 BothLiveOut = true;
2954 break;
2955 }
2956 }
2957 }
2958 if (BothLiveOut)
2959 // Both unextended and extended values are live out. There had better be
2960 // good a reason for the transformation.
2961 return ExtendNodes.size();
2962 }
2963 return true;
2964}
2965
Dan Gohman8181bd12008-07-27 21:46:04 +00002966SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2967 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002968 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969
2970 // fold (sext c1) -> c1
2971 if (isa<ConstantSDNode>(N0))
2972 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
2973
2974 // fold (sext (sext x)) -> (sext x)
2975 // fold (sext (aext x)) -> (sext x)
2976 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
2977 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
2978
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002979 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00002980 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2981 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greif1c80d112008-08-28 21:40:38 +00002982 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2983 if (NarrowLoad.getNode()) {
2984 if (NarrowLoad.getNode() != N0.getNode())
2985 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2987 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002988
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00002989 // See if the value being truncated is already sign extended. If so, just
2990 // eliminate the trunc/sext pair.
Dan Gohman8181bd12008-07-27 21:46:04 +00002991 SDValue Op = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00002992 unsigned OpBits = Op.getValueType().getSizeInBits();
2993 unsigned MidBits = N0.getValueType().getSizeInBits();
2994 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
2996
2997 if (OpBits == DestBits) {
2998 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2999 // bits, it is already ready.
3000 if (NumSignBits > DestBits-MidBits)
3001 return Op;
3002 } else if (OpBits < DestBits) {
3003 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3004 // bits, just sext from i32.
3005 if (NumSignBits > OpBits-MidBits)
3006 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
3007 } else {
3008 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3009 // bits, just truncate to i32.
3010 if (NumSignBits > OpBits-MidBits)
3011 return DAG.getNode(ISD::TRUNCATE, VT, Op);
3012 }
3013
3014 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003015 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3016 N0.getValueType())) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003017 if (Op.getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003019 else if (Op.getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3021 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
3022 DAG.getValueType(N0.getValueType()));
3023 }
3024 }
3025
3026 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003027 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003028 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003029 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003030 bool DoXform = true;
3031 SmallVector<SDNode*, 4> SetCCs;
3032 if (!N0.hasOneUse())
3033 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3034 if (DoXform) {
3035 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003036 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003037 LN0->getBasePtr(), LN0->getSrcValue(),
3038 LN0->getSrcValueOffset(),
3039 N0.getValueType(),
3040 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003041 CombineTo(N, ExtLoad);
Dan Gohman8181bd12008-07-27 21:46:04 +00003042 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003043 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng9decb332007-10-29 19:58:20 +00003044 // Extend SetCC uses if necessary.
3045 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3046 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003047 SmallVector<SDValue, 4> Ops;
Evan Cheng9decb332007-10-29 19:58:20 +00003048 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003049 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003050 if (SOp == Trunc)
3051 Ops.push_back(ExtLoad);
3052 else
3053 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
3054 }
3055 Ops.push_back(SetCC->getOperand(2));
3056 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3057 &Ops[0], Ops.size()));
3058 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003059 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003060 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 }
3062
3063 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3064 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003065 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3066 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003067 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003068 MVT EVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003069 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003070 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003071 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003072 LN0->getBasePtr(), LN0->getSrcValue(),
3073 LN0->getSrcValueOffset(), EVT,
3074 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003075 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003076 CombineTo(N0.getNode(),
3077 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003079 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 }
3081 }
3082
3083 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3084 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003085 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3087 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3088 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003089 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 }
3091
Dan Gohman415e13a2008-04-28 16:58:24 +00003092 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003093 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman5b37f9d2008-04-28 18:47:17 +00003094 DAG.SignBitIsZero(N0))
Dan Gohman415e13a2008-04-28 16:58:24 +00003095 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3096
Dan Gohman8181bd12008-07-27 21:46:04 +00003097 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098}
3099
Dan Gohman8181bd12008-07-27 21:46:04 +00003100SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3101 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003102 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103
3104 // fold (zext c1) -> c1
3105 if (isa<ConstantSDNode>(N0))
3106 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3107 // fold (zext (zext x)) -> (zext x)
3108 // fold (zext (aext x)) -> (zext x)
3109 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
3110 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
3111
3112 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3113 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
3114 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003115 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3116 if (NarrowLoad.getNode()) {
3117 if (NarrowLoad.getNode() != N0.getNode())
3118 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3120 }
3121 }
3122
3123 // fold (zext (truncate x)) -> (and x, mask)
3124 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003125 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SDValue Op = N0.getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003127 if (Op.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003129 } else if (Op.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3131 }
3132 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3133 }
3134
3135 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3136 if (N0.getOpcode() == ISD::AND &&
3137 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3138 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003139 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003140 if (X.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003142 } else if (X.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3144 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003145 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003146 Mask.zext(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3148 }
3149
3150 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003151 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003152 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003153 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003154 bool DoXform = true;
3155 SmallVector<SDNode*, 4> SetCCs;
3156 if (!N0.hasOneUse())
3157 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3158 if (DoXform) {
3159 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003160 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003161 LN0->getBasePtr(), LN0->getSrcValue(),
3162 LN0->getSrcValueOffset(),
3163 N0.getValueType(),
3164 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003165 CombineTo(N, ExtLoad);
Dan Gohman8181bd12008-07-27 21:46:04 +00003166 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003167 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng9decb332007-10-29 19:58:20 +00003168 // Extend SetCC uses if necessary.
3169 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3170 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003171 SmallVector<SDValue, 4> Ops;
Evan Cheng9decb332007-10-29 19:58:20 +00003172 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003173 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003174 if (SOp == Trunc)
3175 Ops.push_back(ExtLoad);
3176 else
Evan Cheng06aaf4c2007-10-30 20:11:21 +00003177 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng9decb332007-10-29 19:58:20 +00003178 }
3179 Ops.push_back(SetCC->getOperand(2));
3180 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3181 &Ops[0], Ops.size()));
3182 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003183 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003184 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185 }
3186
3187 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3188 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003189 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3190 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003191 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003192 MVT EVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003193 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003194 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003195 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003196 LN0->getBasePtr(), LN0->getSrcValue(),
3197 LN0->getSrcValueOffset(), EVT,
3198 LN0->isVolatile(), LN0->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00003199 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003200 CombineTo(N0.getNode(),
3201 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sands2418bec2008-06-13 19:07:40 +00003202 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003203 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands2418bec2008-06-13 19:07:40 +00003204 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 }
3206
3207 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3208 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003209 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003210 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3211 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3212 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003213 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 }
3215
Dan Gohman8181bd12008-07-27 21:46:04 +00003216 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217}
3218
Dan Gohman8181bd12008-07-27 21:46:04 +00003219SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3220 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003221 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003222
3223 // fold (aext c1) -> c1
3224 if (isa<ConstantSDNode>(N0))
3225 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3226 // fold (aext (aext x)) -> (aext x)
3227 // fold (aext (zext x)) -> (zext x)
3228 // fold (aext (sext x)) -> (sext x)
3229 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3230 N0.getOpcode() == ISD::ZERO_EXTEND ||
3231 N0.getOpcode() == ISD::SIGN_EXTEND)
3232 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3233
3234 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3235 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3236 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003237 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3238 if (NarrowLoad.getNode()) {
3239 if (NarrowLoad.getNode() != N0.getNode())
3240 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3242 }
3243 }
3244
3245 // fold (aext (truncate x))
3246 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003247 SDValue TruncOp = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 if (TruncOp.getValueType() == VT)
3249 return TruncOp; // x iff x size == zext size.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003250 if (TruncOp.getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3252 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3253 }
3254
3255 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3256 if (N0.getOpcode() == ISD::AND &&
3257 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3258 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003260 if (X.getValueType().bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003261 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003262 } else if (X.getValueType().bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3264 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003265 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003266 Mask.zext(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3268 }
3269
3270 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003271 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003272 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003273 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003275 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003276 LN0->getBasePtr(), LN0->getSrcValue(),
3277 LN0->getSrcValueOffset(),
3278 N0.getValueType(),
3279 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003280 CombineTo(N, ExtLoad);
Dan Gohman759ed292008-07-31 00:50:31 +00003281 // Redirect any chain users to the new load.
Evan Chengc296a302008-08-29 23:20:46 +00003282 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3283 SDValue(ExtLoad.getNode(), 1));
Dan Gohman759ed292008-07-31 00:50:31 +00003284 // If any node needs the original loaded value, recompute it.
3285 if (!LN0->use_empty())
3286 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3287 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003288 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289 }
3290
3291 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3292 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3293 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
3294 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003295 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 N0.hasOneUse()) {
3297 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003298 MVT EVT = LN0->getMemoryVT();
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003300 LN0->getChain(), LN0->getBasePtr(),
3301 LN0->getSrcValue(),
3302 LN0->getSrcValueOffset(), EVT,
3303 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 CombineTo(N, ExtLoad);
Evan Chengc296a302008-08-29 23:20:46 +00003305 CombineTo(N0.getNode(),
3306 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003308 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 }
3310
3311 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3312 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003313 SDValue SCC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3315 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3316 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003317 if (SCC.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 return SCC;
3319 }
3320
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322}
3323
Chris Lattnere8671c52007-10-13 06:35:54 +00003324/// GetDemandedBits - See if the specified operand can be simplified with the
3325/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman8181bd12008-07-27 21:46:04 +00003326/// simpler operand, otherwise return a null SDValue.
3327SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattnere8671c52007-10-13 06:35:54 +00003328 switch (V.getOpcode()) {
3329 default: break;
3330 case ISD::OR:
3331 case ISD::XOR:
3332 // If the LHS or RHS don't contribute bits to the or, drop them.
3333 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3334 return V.getOperand(1);
3335 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3336 return V.getOperand(0);
3337 break;
Chris Lattnerb77ea552007-10-13 06:58:48 +00003338 case ISD::SRL:
3339 // Only look at single-use SRLs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003340 if (!V.getNode()->hasOneUse())
Chris Lattnerb77ea552007-10-13 06:58:48 +00003341 break;
3342 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3343 // See if we can recursively simplify the LHS.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003344 unsigned Amt = RHSC->getZExtValue();
Dan Gohman77d852f2009-01-03 19:22:06 +00003345 // Watch out for shift count overflow though.
3346 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman07961cd2008-02-25 21:11:39 +00003347 APInt NewMask = Mask << Amt;
Dan Gohman8181bd12008-07-27 21:46:04 +00003348 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00003349 if (SimplifyLHS.getNode()) {
Chris Lattnerb77ea552007-10-13 06:58:48 +00003350 return DAG.getNode(ISD::SRL, V.getValueType(),
3351 SimplifyLHS, V.getOperand(1));
3352 }
3353 }
Chris Lattnere8671c52007-10-13 06:35:54 +00003354 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003355 return SDValue();
Chris Lattnere8671c52007-10-13 06:35:54 +00003356}
3357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003358/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3359/// bits and then truncated to a narrower type and where N is a multiple
3360/// of number of bits of the narrower type, transform it to a narrower load
3361/// from address + N / num of bits of new type. If the result is to be
3362/// extended, also fold the extension to form a extending load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003363SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 unsigned Opc = N->getOpcode();
3365 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman8181bd12008-07-27 21:46:04 +00003366 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003367 MVT VT = N->getValueType(0);
Dan Gohman05452202009-01-21 15:17:51 +00003368 MVT EVT = VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369
Dan Gohman29c3cef2008-08-14 20:04:46 +00003370 // This transformation isn't valid for vector loads.
3371 if (VT.isVector())
3372 return SDValue();
3373
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3375 // extended to VT.
3376 if (Opc == ISD::SIGN_EXTEND_INREG) {
3377 ExtType = ISD::SEXTLOAD;
3378 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003379 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00003380 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 }
3382
Duncan Sands92c43912008-06-06 12:08:01 +00003383 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384 unsigned ShAmt = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3386 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003387 ShAmt = N01->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003388 // Is the shift amount a multiple of size of VT?
3389 if ((ShAmt & (EVTBits-1)) == 0) {
3390 N0 = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003391 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman8181bd12008-07-27 21:46:04 +00003392 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393 }
3394 }
3395 }
3396
Duncan Sands3ea93352008-06-16 08:14:38 +00003397 // Do not generate loads of non-round integer types since these can
3398 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman50187c22009-01-20 01:06:45 +00003399 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && EVT.isRound() &&
Dan Gohman759ed292008-07-31 00:50:31 +00003400 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003401 // Do not change the width of a volatile load.
3402 !cast<LoadSDNode>(N0)->isVolatile()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00003404 MVT PtrType = N0.getOperand(1).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003405 // For big endian targets, we need to adjust the offset to the pointer to
3406 // load the correct bytes.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003407 if (TLI.isBigEndian()) {
Dan Gohman759ed292008-07-31 00:50:31 +00003408 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands92c43912008-06-06 12:08:01 +00003409 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sands4f18d4f2007-11-09 08:57:19 +00003410 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsa3691432007-10-28 12:59:45 +00003413 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman8181bd12008-07-27 21:46:04 +00003414 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003415 DAG.getConstant(PtrOff, PtrType));
Gabor Greif1c80d112008-08-28 21:40:38 +00003416 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00003417 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003418 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003419 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsa3691432007-10-28 12:59:45 +00003420 LN0->isVolatile(), NewAlign)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003422 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3423 EVT, LN0->isVolatile(), NewAlign);
Dan Gohman05452202009-01-21 15:17:51 +00003424 // Replace the old load's chain with the new load's chain.
3425 WorkListRemover DeadNodes(*this);
3426 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3427 &DeadNodes);
3428 // Return the new loaded value.
3429 return Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 }
3431
Dan Gohman8181bd12008-07-27 21:46:04 +00003432 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003433}
3434
3435
Dan Gohman8181bd12008-07-27 21:46:04 +00003436SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3437 SDValue N0 = N->getOperand(0);
3438 SDValue N1 = N->getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00003439 MVT VT = N->getValueType(0);
3440 MVT EVT = cast<VTSDNode>(N1)->getVT();
3441 unsigned VTBits = VT.getSizeInBits();
3442 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443
3444 // fold (sext_in_reg c1) -> c1
3445 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
3446 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
3447
3448 // If the input is already sign extended, just drop the extension.
Duncan Sands92c43912008-06-06 12:08:01 +00003449 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003450 return N0;
3451
3452 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3453 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00003454 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003455 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
3456 }
3457
Dan Gohman759ed292008-07-31 00:50:31 +00003458 // fold (sext_in_reg (sext x)) -> (sext x)
3459 // fold (sext_in_reg (aext x)) -> (sext x)
3460 // if x is small enough.
3461 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3462 SDValue N00 = N0.getOperand(0);
3463 if (N00.getValueType().getSizeInBits() < EVTBits)
3464 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3465 }
3466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003467 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00003468 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003469 return DAG.getZeroExtendInReg(N0, EVT);
3470
3471 // fold operands of sext_in_reg based on knowledge that the top bits are not
3472 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00003473 if (SimplifyDemandedBits(SDValue(N, 0)))
3474 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003475
3476 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3477 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman8181bd12008-07-27 21:46:04 +00003478 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003479 if (NarrowLoad.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480 return NarrowLoad;
3481
3482 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3483 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3484 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3485 if (N0.getOpcode() == ISD::SRL) {
3486 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003487 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003488 // We can turn this into an SRA iff the input to the SRL is already sign
3489 // extended enough.
3490 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003491 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3493 }
3494 }
3495
3496 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00003497 if (ISD::isEXTLoad(N0.getNode()) &&
3498 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003499 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003500 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003501 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003502 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003503 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003504 LN0->getBasePtr(), LN0->getSrcValue(),
3505 LN0->getSrcValueOffset(), EVT,
3506 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003507 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003508 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003509 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003510 }
3511 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00003512 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 N0.hasOneUse() &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003514 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003515 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003516 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003517 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00003518 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003519 LN0->getBasePtr(), LN0->getSrcValue(),
3520 LN0->getSrcValueOffset(), EVT,
3521 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003522 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003523 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003526 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003527}
3528
Dan Gohman8181bd12008-07-27 21:46:04 +00003529SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3530 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003531 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532
3533 // noop truncate
3534 if (N0.getValueType() == N->getValueType(0))
3535 return N0;
3536 // fold (truncate c1) -> c1
3537 if (isa<ConstantSDNode>(N0))
3538 return DAG.getNode(ISD::TRUNCATE, VT, N0);
3539 // fold (truncate (truncate x)) -> (truncate x)
3540 if (N0.getOpcode() == ISD::TRUNCATE)
3541 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
3542 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
3543 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3544 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003545 if (N0.getOperand(0).getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 // if the source is smaller than the dest, we still need an extend
3547 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00003548 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549 // if the source is larger than the dest, than we just need the truncate
3550 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
3551 else
3552 // if the source and dest are the same type, we can drop both the extend
3553 // and the truncate
3554 return N0.getOperand(0);
3555 }
3556
Chris Lattnere8671c52007-10-13 06:35:54 +00003557 // See if we can simplify the input to this truncate through knowledge that
3558 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3559 // -> trunc y
Dan Gohman8181bd12008-07-27 21:46:04 +00003560 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00003561 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00003562 VT.getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00003563 if (Shorter.getNode())
Chris Lattnere8671c52007-10-13 06:35:54 +00003564 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3565
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003566 // fold (truncate (load x)) -> (smaller load x)
3567 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
3568 return ReduceLoadWidth(N);
3569}
3570
Evan Chengb6290462008-05-12 23:04:07 +00003571static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003572 SDValue Elt = N->getOperand(i);
Evan Chengb6290462008-05-12 23:04:07 +00003573 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greif1c80d112008-08-28 21:40:38 +00003574 return Elt.getNode();
3575 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00003576}
3577
3578/// CombineConsecutiveLoads - build_pair (load, load) -> load
3579/// if load locations are consecutive.
Dan Gohman8181bd12008-07-27 21:46:04 +00003580SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Chengb6290462008-05-12 23:04:07 +00003581 assert(N->getOpcode() == ISD::BUILD_PAIR);
3582
3583 SDNode *LD1 = getBuildPairElt(N, 0);
3584 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00003585 return SDValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003586 MVT LD1VT = LD1->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003587 SDNode *LD2 = getBuildPairElt(N, 1);
3588 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3589 if (ISD::isNON_EXTLoad(LD2) &&
3590 LD2->hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003591 // If both are volatile this would reduce the number of volatile loads.
3592 // If one is volatile it might be ok, but play conservative and bail out.
3593 !cast<LoadSDNode>(LD1)->isVolatile() &&
3594 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003595 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Chengb6290462008-05-12 23:04:07 +00003596 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3597 unsigned Align = LD->getAlignment();
Dan Gohman404e8542008-09-04 15:39:15 +00003598 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00003599 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sands2418bec2008-06-13 19:07:40 +00003600 if (NewAlign <= Align &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003601 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Chengb6290462008-05-12 23:04:07 +00003602 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3603 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sands2418bec2008-06-13 19:07:40 +00003604 false, Align);
Evan Chengb6290462008-05-12 23:04:07 +00003605 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00003607}
3608
Dan Gohman8181bd12008-07-27 21:46:04 +00003609SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3610 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003611 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003612
3613 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3614 // Only do this before legalize, since afterward the target may be depending
3615 // on the bitconvert.
3616 // First check to see if this is all constant.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003617 if (!LegalTypes &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003618 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003619 VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003620 bool isSimple = true;
3621 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3622 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3623 N0.getOperand(i).getOpcode() != ISD::Constant &&
3624 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3625 isSimple = false;
3626 break;
3627 }
3628
Duncan Sands92c43912008-06-06 12:08:01 +00003629 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3630 assert(!DestEltVT.isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003631 "Element type of vector ValueType must not be vector!");
3632 if (isSimple) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003633 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634 }
3635 }
3636
Dan Gohman83c6e8a2008-09-05 01:58:21 +00003637 // If the input is a constant, let getNode fold it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003638 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003639 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greif1c80d112008-08-28 21:40:38 +00003640 if (Res.getNode() != N) return Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003641 }
3642
3643 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3644 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3645
3646 // fold (conv (load x)) -> (load (conv*)x)
Evan Chengd7ba7ed2007-10-06 08:19:55 +00003647 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greif1c80d112008-08-28 21:40:38 +00003648 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003649 // Do not change the width of a volatile load.
3650 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003651 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003652 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman404e8542008-09-04 15:39:15 +00003653 unsigned Align = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00003654 getABITypeAlignment(VT.getTypeForMVT());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003655 unsigned OrigAlign = LN0->getAlignment();
3656 if (Align <= OrigAlign) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003657 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003658 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3659 LN0->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 AddToWorkList(N);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003661 CombineTo(N0.getNode(),
3662 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003663 Load.getValue(1));
3664 return Load;
3665 }
3666 }
Duncan Sands2418bec2008-06-13 19:07:40 +00003667
Chris Lattneref26cbc2008-01-27 17:42:27 +00003668 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3669 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3670 // This often reduces constant pool loads.
3671 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003672 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003673 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00003674 AddToWorkList(NewConv.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003675
Duncan Sands92c43912008-06-06 12:08:01 +00003676 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003677 if (N0.getOpcode() == ISD::FNEG)
3678 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3679 assert(N0.getOpcode() == ISD::FABS);
3680 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3681 }
3682
3683 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3684 // Note that we don't handle copysign(x,cst) because this can always be folded
3685 // to an fneg or fabs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003686 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner336672f2008-01-27 23:32:17 +00003687 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands92c43912008-06-06 12:08:01 +00003688 VT.isInteger() && !VT.isVector()) {
3689 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003690 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3691 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3692 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3693 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003694
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003695 // If X has a different width than the result/lhs, sext it or truncate it.
3696 unsigned VTWidth = VT.getSizeInBits();
3697 if (OrigXWidth < VTWidth) {
3698 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3699 AddToWorkList(X.getNode());
3700 } else if (OrigXWidth > VTWidth) {
3701 // To get the sign bit in the right place, we have to shift it right
3702 // before truncating.
3703 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3704 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3705 AddToWorkList(X.getNode());
3706 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3707 AddToWorkList(X.getNode());
3708 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003709
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003710 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3711 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3712 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003713
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003714 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3715 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3716 AddToWorkList(Cst.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003717
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003718 return DAG.getNode(ISD::OR, VT, X, Cst);
3719 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003720 }
Evan Chengb6290462008-05-12 23:04:07 +00003721
3722 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3723 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003724 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3725 if (CombineLD.getNode())
Evan Chengb6290462008-05-12 23:04:07 +00003726 return CombineLD;
3727 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003728
Dan Gohman8181bd12008-07-27 21:46:04 +00003729 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003730}
3731
Dan Gohman8181bd12008-07-27 21:46:04 +00003732SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands92c43912008-06-06 12:08:01 +00003733 MVT VT = N->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003734 return CombineConsecutiveLoads(N, VT);
3735}
3736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
3738/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3739/// destination element value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00003740SDValue DAGCombiner::
Duncan Sands92c43912008-06-06 12:08:01 +00003741ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3742 MVT SrcEltVT = BV->getOperand(0).getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743
3744 // If this is already the right type, we're done.
Dan Gohman8181bd12008-07-27 21:46:04 +00003745 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746
Duncan Sands92c43912008-06-06 12:08:01 +00003747 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3748 unsigned DstBitSize = DstEltVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749
3750 // If this is a conversion of N elements of one type to N elements of another
3751 // type, convert each element. This handles FP<->INT cases.
3752 if (SrcBitSize == DstBitSize) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003753 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3755 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greif1c80d112008-08-28 21:40:38 +00003756 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757 }
Duncan Sands92c43912008-06-06 12:08:01 +00003758 MVT VT = MVT::getVectorVT(DstEltVT,
3759 BV->getValueType(0).getVectorNumElements());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003760 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3761 }
3762
3763 // Otherwise, we're growing or shrinking the elements. To avoid having to
3764 // handle annoying details of growing/shrinking FP values, we convert them to
3765 // int first.
Duncan Sands92c43912008-06-06 12:08:01 +00003766 if (SrcEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003767 // Convert the input float vector to a int vector where the elements are the
3768 // same sizes.
3769 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands6a437fb2008-06-09 11:32:28 +00003770 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003771 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003772 SrcEltVT = IntVT;
3773 }
3774
3775 // Now we know the input is an integer vector. If the output is a FP type,
3776 // convert to integer first, then to FP of the right size.
Duncan Sands92c43912008-06-06 12:08:01 +00003777 if (DstEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands6a437fb2008-06-09 11:32:28 +00003779 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003780 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781
3782 // Next, convert to FP elements of the same size.
3783 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
3784 }
3785
3786 // Okay, we know the src/dst types are both integers of differing types.
3787 // Handling growing first.
Duncan Sands92c43912008-06-06 12:08:01 +00003788 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 if (SrcBitSize < DstBitSize) {
3790 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3791
Dan Gohman8181bd12008-07-27 21:46:04 +00003792 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003793 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
3794 i += NumInputsPerOutput) {
3795 bool isLE = TLI.isLittleEndian();
Dan Gohmand047c3e2008-03-03 23:51:38 +00003796 APInt NewBits = APInt(DstBitSize, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003797 bool EltIsUndef = true;
3798 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3799 // Shift the previously computed bits over.
3800 NewBits <<= SrcBitSize;
Dan Gohman8181bd12008-07-27 21:46:04 +00003801 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003802 if (Op.getOpcode() == ISD::UNDEF) continue;
3803 EltIsUndef = false;
3804
Dan Gohmand047c3e2008-03-03 23:51:38 +00003805 NewBits |=
3806 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003807 }
3808
3809 if (EltIsUndef)
3810 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3811 else
3812 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3813 }
3814
Duncan Sands92c43912008-06-06 12:08:01 +00003815 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003816 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3817 }
3818
3819 // Finally, this must be the case where we are shrinking elements: each input
3820 // turns into multiple outputs.
Evan Chengd1045a62008-02-18 23:04:32 +00003821 bool isS2V = ISD::isScalarToVector(BV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003822 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands92c43912008-06-06 12:08:01 +00003823 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman8181bd12008-07-27 21:46:04 +00003824 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3826 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3827 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3828 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3829 continue;
3830 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003831 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003832 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00003833 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohmand047c3e2008-03-03 23:51:38 +00003835 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengd1045a62008-02-18 23:04:32 +00003836 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3837 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohmand047c3e2008-03-03 23:51:38 +00003838 OpVal = OpVal.lshr(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003839 }
3840
3841 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003842 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3844 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
3846}
3847
3848
3849
Dan Gohman8181bd12008-07-27 21:46:04 +00003850SDValue DAGCombiner::visitFADD(SDNode *N) {
3851 SDValue N0 = N->getOperand(0);
3852 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3854 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003855 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003856
3857 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003858 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003859 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003860 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 }
3862
3863 // fold (fadd c1, c2) -> c1+c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003864 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 return DAG.getNode(ISD::FADD, VT, N0, N1);
3866 // canonicalize constant to RHS
3867 if (N0CFP && !N1CFP)
3868 return DAG.getNode(ISD::FADD, VT, N1, N0);
Dan Gohman3d015562009-01-22 21:58:43 +00003869 // fold (A + 0) -> A
3870 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3871 return N0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 // fold (A + (-B)) -> A-B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003873 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003874 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003875 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003876 // fold ((-A) + B) -> B-A
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003877 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003878 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003879 GetNegatedExpression(N0, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003880
3881 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3882 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003883 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003884 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3885 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3886
Dan Gohman8181bd12008-07-27 21:46:04 +00003887 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888}
3889
Dan Gohman8181bd12008-07-27 21:46:04 +00003890SDValue DAGCombiner::visitFSUB(SDNode *N) {
3891 SDValue N0 = N->getOperand(0);
3892 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3894 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003895 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003896
3897 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003898 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003899 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003900 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 }
3902
3903 // fold (fsub c1, c2) -> c1-c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003904 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman46ef3eb2009-01-23 19:10:37 +00003906 // fold (A-0) -> A
3907 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3908 return N0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909 // fold (0-B) -> -B
Dale Johannesen7604c1b2007-08-31 23:34:27 +00003910 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003911 if (isNegatibleForFree(N1, LegalOperations))
3912 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman3d015562009-01-22 21:58:43 +00003913 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3914 return DAG.getNode(ISD::FNEG, VT, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003915 }
3916 // fold (A-(-B)) -> A+B
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003917 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattnere0992b82008-02-26 07:04:54 +00003918 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003919 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920
Dan Gohman8181bd12008-07-27 21:46:04 +00003921 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922}
3923
Dan Gohman8181bd12008-07-27 21:46:04 +00003924SDValue DAGCombiner::visitFMUL(SDNode *N) {
3925 SDValue N0 = N->getOperand(0);
3926 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3928 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003929 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930
3931 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003932 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003933 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003934 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003935 }
3936
3937 // fold (fmul c1, c2) -> c1*c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003938 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 return DAG.getNode(ISD::FMUL, VT, N0, N1);
3940 // canonicalize constant to RHS
3941 if (N0CFP && !N1CFP)
3942 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Dan Gohman3d015562009-01-22 21:58:43 +00003943 // fold (A * 0) -> 0
3944 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3945 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003946 // fold (fmul X, 2.0) -> (fadd X, X)
3947 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3948 return DAG.getNode(ISD::FADD, VT, N0, N0);
3949 // fold (fmul X, -1.0) -> (fneg X)
3950 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman3d015562009-01-22 21:58:43 +00003951 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3952 return DAG.getNode(ISD::FNEG, VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953
3954 // -X * -Y -> X*Y
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003955 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3956 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 // Both can be negated for free, check to see if at least one is cheaper
3958 // negated.
3959 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003960 return DAG.getNode(ISD::FMUL, VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003961 GetNegatedExpression(N0, DAG, LegalOperations),
3962 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003963 }
3964 }
3965
3966 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3967 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003968 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003969 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3970 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3971
Dan Gohman8181bd12008-07-27 21:46:04 +00003972 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973}
3974
Dan Gohman8181bd12008-07-27 21:46:04 +00003975SDValue DAGCombiner::visitFDIV(SDNode *N) {
3976 SDValue N0 = N->getOperand(0);
3977 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3979 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00003980 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981
3982 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003983 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003984 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003985 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003986 }
3987
3988 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesenb89072e2007-10-16 23:38:29 +00003989 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 return DAG.getNode(ISD::FDIV, VT, N0, N1);
3991
3992
3993 // -X / -Y -> X*Y
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003994 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3995 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 // Both can be negated for free, check to see if at least one is cheaper
3997 // negated.
3998 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattnere0992b82008-02-26 07:04:54 +00003999 return DAG.getNode(ISD::FDIV, VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004000 GetNegatedExpression(N0, DAG, LegalOperations),
4001 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002 }
4003 }
4004
Dan Gohman8181bd12008-07-27 21:46:04 +00004005 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006}
4007
Dan Gohman8181bd12008-07-27 21:46:04 +00004008SDValue DAGCombiner::visitFREM(SDNode *N) {
4009 SDValue N0 = N->getOperand(0);
4010 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004011 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4012 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00004013 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014
4015 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesenb89072e2007-10-16 23:38:29 +00004016 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 return DAG.getNode(ISD::FREM, VT, N0, N1);
4018
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020}
4021
Dan Gohman8181bd12008-07-27 21:46:04 +00004022SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4023 SDValue N0 = N->getOperand(0);
4024 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004025 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4026 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands92c43912008-06-06 12:08:01 +00004027 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028
Dale Johannesenb89072e2007-10-16 23:38:29 +00004029 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
4031
4032 if (N1CFP) {
Dale Johannesenc53301c2007-08-26 01:18:27 +00004033 const APFloat& V = N1CFP->getValueAPF();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4035 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman3d015562009-01-22 21:58:43 +00004036 if (!V.isNegative()) {
4037 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
4038 return DAG.getNode(ISD::FABS, VT, N0);
4039 } else {
4040 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
4041 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
4042 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 }
4044
4045 // copysign(fabs(x), y) -> copysign(x, y)
4046 // copysign(fneg(x), y) -> copysign(x, y)
4047 // copysign(copysign(x,z), y) -> copysign(x, y)
4048 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4049 N0.getOpcode() == ISD::FCOPYSIGN)
4050 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
4051
4052 // copysign(x, abs(y)) -> abs(x)
4053 if (N1.getOpcode() == ISD::FABS)
4054 return DAG.getNode(ISD::FABS, VT, N0);
4055
4056 // copysign(x, copysign(y,z)) -> copysign(x, z)
4057 if (N1.getOpcode() == ISD::FCOPYSIGN)
4058 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4059
4060 // copysign(x, fp_extend(y)) -> copysign(x, y)
4061 // copysign(x, fp_round(y)) -> copysign(x, y)
4062 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4063 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4064
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066}
4067
4068
4069
Dan Gohman8181bd12008-07-27 21:46:04 +00004070SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4071 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004073 MVT VT = N->getValueType(0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004074 MVT OpVT = N0.getValueType();
4075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004076 // fold (sint_to_fp c1) -> c1fp
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004077 if (N0C && OpVT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004079
4080 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4081 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004082 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4083 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004084 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4085 if (DAG.SignBitIsZero(N0))
4086 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4087 }
4088
4089
Dan Gohman8181bd12008-07-27 21:46:04 +00004090 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091}
4092
Dan Gohman8181bd12008-07-27 21:46:04 +00004093SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4094 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004096 MVT VT = N->getValueType(0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004097 MVT OpVT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098
4099 // fold (uint_to_fp c1) -> c1fp
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004100 if (N0C && OpVT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004102
4103 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4104 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004105 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4106 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004107 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4108 if (DAG.SignBitIsZero(N0))
4109 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4110 }
4111
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113}
4114
Dan Gohman8181bd12008-07-27 21:46:04 +00004115SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4116 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004118 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119
4120 // fold (fp_to_sint c1fp) -> c1
4121 if (N0CFP)
4122 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124}
4125
Dan Gohman8181bd12008-07-27 21:46:04 +00004126SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4127 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004129 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130
4131 // fold (fp_to_uint c1fp) -> c1
Dale Johannesenb89072e2007-10-16 23:38:29 +00004132 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004134 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135}
4136
Dan Gohman8181bd12008-07-27 21:46:04 +00004137SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4138 SDValue N0 = N->getOperand(0);
4139 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004141 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142
4143 // fold (fp_round c1fp) -> c1fp
Dale Johannesenb89072e2007-10-16 23:38:29 +00004144 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner5872a362008-01-17 07:00:52 +00004145 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146
4147 // fold (fp_round (fp_extend x)) -> x
4148 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4149 return N0.getOperand(0);
4150
Chris Lattner7afb8552008-01-24 06:45:35 +00004151 // fold (fp_round (fp_round x)) -> (fp_round x)
4152 if (N0.getOpcode() == ISD::FP_ROUND) {
4153 // This is a value preserving truncation if both round's are.
4154 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004155 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner7afb8552008-01-24 06:45:35 +00004156 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4157 DAG.getIntPtrConstant(IsTrunc));
4158 }
4159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greif1c80d112008-08-28 21:40:38 +00004161 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004162 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00004163 AddToWorkList(Tmp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4165 }
4166
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004168}
4169
Dan Gohman8181bd12008-07-27 21:46:04 +00004170SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4171 SDValue N0 = N->getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004172 MVT VT = N->getValueType(0);
4173 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4175
4176 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004177 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00004178 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
4180 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004181 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182}
4183
Dan Gohman8181bd12008-07-27 21:46:04 +00004184SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4185 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004187 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188
Chris Lattner6f981fc2007-12-29 06:55:23 +00004189 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00004190 if (N->hasOneUse() &&
djgc2517d32009-01-26 04:35:06 +00004191 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman8181bd12008-07-27 21:46:04 +00004192 return SDValue();
Chris Lattner5872a362008-01-17 07:00:52 +00004193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 // fold (fp_extend c1fp) -> c1fp
Dale Johannesenb89072e2007-10-16 23:38:29 +00004195 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner5872a362008-01-17 07:00:52 +00004197
4198 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4199 // value of X.
Gabor Greifb420b9d2008-08-30 19:29:20 +00004200 if (N0.getOpcode() == ISD::FP_ROUND
4201 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004202 SDValue In = N0.getOperand(0);
Chris Lattner5872a362008-01-17 07:00:52 +00004203 if (In.getValueType() == VT) return In;
Duncan Sandsec142ee2008-06-08 20:54:56 +00004204 if (VT.bitsLT(In.getValueType()))
Chris Lattner5872a362008-01-17 07:00:52 +00004205 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4206 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4207 }
4208
4209 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00004210 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004211 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00004212 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00004214 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004215 LN0->getBasePtr(), LN0->getSrcValue(),
4216 LN0->getSrcValueOffset(),
4217 N0.getValueType(),
4218 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00004220 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4221 ExtLoad, DAG.getIntPtrConstant(1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004223 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004224 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004225
Dan Gohman8181bd12008-07-27 21:46:04 +00004226 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004227}
4228
Dan Gohman8181bd12008-07-27 21:46:04 +00004229SDValue DAGCombiner::visitFNEG(SDNode *N) {
4230 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004232 if (isNegatibleForFree(N0, LegalOperations))
4233 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234
Chris Lattneref26cbc2008-01-27 17:42:27 +00004235 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4236 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004237 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004238 N0.getOperand(0).getValueType().isInteger() &&
4239 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004240 SDValue Int = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004241 MVT IntVT = Int.getValueType();
4242 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattneref26cbc2008-01-27 17:42:27 +00004243 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands92c43912008-06-06 12:08:01 +00004244 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004245 AddToWorkList(Int.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00004246 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4247 }
4248 }
4249
Dan Gohman8181bd12008-07-27 21:46:04 +00004250 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251}
4252
Dan Gohman8181bd12008-07-27 21:46:04 +00004253SDValue DAGCombiner::visitFABS(SDNode *N) {
4254 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands92c43912008-06-06 12:08:01 +00004256 MVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257
4258 // fold (fabs c1) -> fabs(c1)
Dale Johannesenb89072e2007-10-16 23:38:29 +00004259 if (N0CFP && VT != MVT::ppcf128)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 return DAG.getNode(ISD::FABS, VT, N0);
4261 // fold (fabs (fabs x)) -> (fabs x)
4262 if (N0.getOpcode() == ISD::FABS)
4263 return N->getOperand(0);
4264 // fold (fabs (fneg x)) -> (fabs x)
4265 // fold (fabs (fcopysign x, y)) -> (fabs x)
4266 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4267 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4268
Chris Lattneref26cbc2008-01-27 17:42:27 +00004269 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4270 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004271 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004272 N0.getOperand(0).getValueType().isInteger() &&
4273 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004274 SDValue Int = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004275 MVT IntVT = Int.getValueType();
4276 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattneref26cbc2008-01-27 17:42:27 +00004277 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands92c43912008-06-06 12:08:01 +00004278 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004279 AddToWorkList(Int.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00004280 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4281 }
4282 }
4283
Dan Gohman8181bd12008-07-27 21:46:04 +00004284 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285}
4286
Dan Gohman8181bd12008-07-27 21:46:04 +00004287SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4288 SDValue Chain = N->getOperand(0);
4289 SDValue N1 = N->getOperand(1);
4290 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004291 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4292
4293 // never taken branch, fold to chain
4294 if (N1C && N1C->isNullValue())
4295 return Chain;
4296 // unconditional branch
Dan Gohman9d24dc72008-03-13 22:13:53 +00004297 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
4299 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4300 // on the target.
4301 if (N1.getOpcode() == ISD::SETCC &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004302 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4304 N1.getOperand(0), N1.getOperand(1), N2);
4305 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004306 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307}
4308
4309// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4310//
Dan Gohman8181bd12008-07-27 21:46:04 +00004311SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004313 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314
Duncan Sands6a437fb2008-06-09 11:32:28 +00004315 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands4a361272009-01-01 15:52:00 +00004316 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004317 CondLHS, CondRHS, CC->get(), false);
Gabor Greif1c80d112008-08-28 21:40:38 +00004318 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319
Gabor Greif1c80d112008-08-28 21:40:38 +00004320 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004321
4322 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman9d24dc72008-03-13 22:13:53 +00004323 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004324 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4325 N->getOperand(4));
4326 // fold br_cc false, dest -> unconditional fall through
4327 if (SCCC && SCCC->isNullValue())
4328 return N->getOperand(0);
4329
4330 // fold to a simpler setcc
Gabor Greif1c80d112008-08-28 21:40:38 +00004331 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4333 Simp.getOperand(2), Simp.getOperand(0),
4334 Simp.getOperand(1), N->getOperand(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336}
4337
4338
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004339/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4340/// pre-indexed load / store when the base pointer is an add or subtract
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004341/// and it has other uses besides the load / store. After the
4342/// transformation, the new indexed load / store has effectively folded
4343/// the add / subtract in and all of its other uses are redirected to the
4344/// new load / store.
4345bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004346 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347 return false;
4348
4349 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004350 SDValue Ptr;
Duncan Sands92c43912008-06-06 12:08:01 +00004351 MVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004353 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004355 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
4357 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4358 return false;
4359 Ptr = LD->getBasePtr();
4360 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004361 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004363 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4365 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4366 return false;
4367 Ptr = ST->getBasePtr();
4368 isLoad = false;
4369 } else
4370 return false;
4371
4372 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4373 // out. There is no reason to make this a preinc/predec.
4374 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greif1c80d112008-08-28 21:40:38 +00004375 Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 return false;
4377
4378 // Ask the target to do addressing mode selection.
Dan Gohman8181bd12008-07-27 21:46:04 +00004379 SDValue BasePtr;
4380 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4382 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4383 return false;
4384 // Don't create a indexed load / store with zero offset.
4385 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004386 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387 return false;
4388
4389 // Try turning it into a pre-indexed load / store except when:
4390 // 1) The new base ptr is a frame index.
4391 // 2) If N is a store and the new base ptr is either the same as or is a
4392 // predecessor of the value being stored.
4393 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
4394 // that would create a cycle.
4395 // 4) All uses are load / store ops that use it as old base ptr.
4396
4397 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4398 // (plus the implicit offset) to a register to preinc anyway.
4399 if (isa<FrameIndexSDNode>(BasePtr))
4400 return false;
4401
4402 // Check #2.
4403 if (!isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004404 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004405 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 return false;
4407 }
4408
4409 // Now check for #3 and #4.
4410 bool RealUse = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004411 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4412 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004413 SDNode *Use = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 if (Use == N)
4415 continue;
Evan Chengd9387682008-03-04 00:41:45 +00004416 if (Use->isPredecessorOf(N))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 return false;
4418
4419 if (!((Use->getOpcode() == ISD::LOAD &&
4420 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004421 (Use->getOpcode() == ISD::STORE &&
4422 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 RealUse = true;
4424 }
4425 if (!RealUse)
4426 return false;
4427
Dan Gohman8181bd12008-07-27 21:46:04 +00004428 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004429 if (isLoad)
Dan Gohman8181bd12008-07-27 21:46:04 +00004430 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 else
Dan Gohman8181bd12008-07-27 21:46:04 +00004432 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433 ++PreIndexedNodes;
4434 ++NodesCombined;
4435 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004436 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437 DOUT << '\n';
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004438 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004440 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004441 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004442 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004443 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004445 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004446 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447 }
4448
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449 // Finally, since the node is now dead, remove it from the graph.
4450 DAG.DeleteNode(N);
4451
4452 // Replace the uses of Ptr with uses of the updated base value.
4453 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004454 &DeadNodes);
Gabor Greif1c80d112008-08-28 21:40:38 +00004455 removeFromWorkList(Ptr.getNode());
4456 DAG.DeleteNode(Ptr.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457
4458 return true;
4459}
4460
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004461/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462/// add / sub of the base pointer node into a post-indexed load / store.
4463/// The transformation folded the add / subtract into the new indexed
4464/// load / store effectively and all of its uses are redirected to the
4465/// new load / store.
4466bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004467 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 return false;
4469
4470 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004471 SDValue Ptr;
Duncan Sands92c43912008-06-06 12:08:01 +00004472 MVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004474 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004476 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4478 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4479 return false;
4480 Ptr = LD->getBasePtr();
4481 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004482 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004484 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4486 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4487 return false;
4488 Ptr = ST->getBasePtr();
4489 isLoad = false;
4490 } else
4491 return false;
4492
Gabor Greif1c80d112008-08-28 21:40:38 +00004493 if (Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004494 return false;
4495
Gabor Greif1c80d112008-08-28 21:40:38 +00004496 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4497 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004498 SDNode *Op = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 if (Op == N ||
4500 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4501 continue;
4502
Dan Gohman8181bd12008-07-27 21:46:04 +00004503 SDValue BasePtr;
4504 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4506 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4507 if (Ptr == Offset)
4508 std::swap(BasePtr, Offset);
4509 if (Ptr != BasePtr)
4510 continue;
4511 // Don't create a indexed load / store with zero offset.
4512 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004513 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514 continue;
4515
4516 // Try turning it into a post-indexed load / store except when
4517 // 1) All uses are load / store ops that use it as base ptr.
4518 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4519 // nor a successor of N. Otherwise, if Op is folded that would
4520 // create a cycle.
4521
4522 // Check for #1.
4523 bool TryNext = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004524 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4525 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004526 SDNode *Use = *II;
Gabor Greif1c80d112008-08-28 21:40:38 +00004527 if (Use == Ptr.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 continue;
4529
4530 // If all the uses are load / store addresses, then don't do the
4531 // transformation.
4532 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4533 bool RealUse = false;
4534 for (SDNode::use_iterator III = Use->use_begin(),
4535 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004536 SDNode *UseUse = *III;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004538 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004539 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004540 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 RealUse = true;
4542 }
4543
4544 if (!RealUse) {
4545 TryNext = true;
4546 break;
4547 }
4548 }
4549 }
4550 if (TryNext)
4551 continue;
4552
4553 // Check for #2
Evan Chengd9387682008-03-04 00:41:45 +00004554 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004555 SDValue Result = isLoad
4556 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4557 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 ++PostIndexedNodes;
4559 ++NodesCombined;
4560 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004561 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562 DOUT << '\n';
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004563 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004565 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004566 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004567 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004568 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004570 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004571 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004572 }
4573
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574 // Finally, since the node is now dead, remove it from the graph.
4575 DAG.DeleteNode(N);
4576
4577 // Replace the uses of Use with uses of the updated base value.
Dan Gohman8181bd12008-07-27 21:46:04 +00004578 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579 Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004580 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581 removeFromWorkList(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582 DAG.DeleteNode(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 return true;
4584 }
4585 }
4586 }
4587 return false;
4588}
4589
Chris Lattner4e137af2008-01-25 07:20:16 +00004590/// InferAlignment - If we can infer some alignment information from this
4591/// pointer, return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00004592static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004593 // If this is a direct reference to a stack slot, use information about the
4594 // stack slot's alignment.
Chris Lattner1e3362f2008-01-26 19:45:50 +00004595 int FrameIdx = 1 << 31;
4596 int64_t FrameOffset = 0;
Chris Lattner4e137af2008-01-25 07:20:16 +00004597 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1e3362f2008-01-26 19:45:50 +00004598 FrameIdx = FI->getIndex();
4599 } else if (Ptr.getOpcode() == ISD::ADD &&
4600 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4601 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4602 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4603 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner4e137af2008-01-25 07:20:16 +00004604 }
Chris Lattner1e3362f2008-01-26 19:45:50 +00004605
4606 if (FrameIdx != (1 << 31)) {
4607 // FIXME: Handle FI+CST.
4608 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4609 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohmanb0a2ff92008-08-11 18:27:03 +00004610 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1e3362f2008-01-26 19:45:50 +00004611
4612 // The alignment of the frame index can be determined from its offset from
4613 // the incoming frame position. If the frame object is at offset 32 and
4614 // the stack is guaranteed to be 16-byte aligned, then we know that the
4615 // object is 16-byte aligned.
4616 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4617 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4618
4619 // Finally, the frame object itself may have a known alignment. Factor
4620 // the alignment + offset into a new alignment. For example, if we know
4621 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4622 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4623 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4624 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4625 FrameOffset);
4626 return std::max(Align, FIInfoAlign);
4627 }
4628 }
Chris Lattner4e137af2008-01-25 07:20:16 +00004629
4630 return 0;
4631}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004632
Dan Gohman8181bd12008-07-27 21:46:04 +00004633SDValue DAGCombiner::visitLOAD(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004635 SDValue Chain = LD->getChain();
4636 SDValue Ptr = LD->getBasePtr();
Chris Lattner4e137af2008-01-25 07:20:16 +00004637
4638 // Try to infer better alignment information than the load already has.
Dan Gohmanea12c0c2008-08-20 16:30:28 +00004639 if (!Fast && LD->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004640 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4641 if (Align > LD->getAlignment())
4642 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4643 Chain, Ptr, LD->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004644 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004645 LD->isVolatile(), Align);
4646 }
4647 }
4648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649
4650 // If load is not volatile and there are no uses of the loaded value (and
4651 // the updated indexed value in case of indexed loads), change uses of the
4652 // chain value into uses of the chain input (i.e. delete the dead load).
4653 if (!LD->isVolatile()) {
4654 if (N->getValueType(1) == MVT::Other) {
4655 // Unindexed loads.
Evan Chenge8b886a2008-01-16 23:11:54 +00004656 if (N->hasNUsesOfValue(0, 0)) {
4657 // It's not safe to use the two value CombineTo variant here. e.g.
4658 // v1, chain2 = load chain1, loc
4659 // v2, chain3 = load chain2, loc
4660 // v3 = add v2, c
Chris Lattnerbb67c192008-01-24 07:57:06 +00004661 // Now we replace use of chain2 with chain1. This makes the second load
4662 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Chenge8b886a2008-01-16 23:11:54 +00004663 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004664 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattnerbb67c192008-01-24 07:57:06 +00004665 DOUT << "\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004666 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004667 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattnerbb67c192008-01-24 07:57:06 +00004668 if (N->use_empty()) {
4669 removeFromWorkList(N);
4670 DAG.DeleteNode(N);
4671 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004672 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge8b886a2008-01-16 23:11:54 +00004673 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 } else {
4675 // Indexed loads.
4676 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4677 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004678 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Chenge8b886a2008-01-16 23:11:54 +00004679 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greif1c80d112008-08-28 21:40:38 +00004680 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Chenge8b886a2008-01-16 23:11:54 +00004681 DOUT << " and 2 other values\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004682 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004683 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4684 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner667f9c12008-01-17 07:20:38 +00004685 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004686 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004687 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Chenge8b886a2008-01-16 23:11:54 +00004688 removeFromWorkList(N);
Evan Chenge8b886a2008-01-16 23:11:54 +00004689 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004690 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691 }
4692 }
4693 }
4694
4695 // If this load is directly stored, replace the load value with the stored
4696 // value.
4697 // TODO: Handle store large -> read small portion.
4698 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohman729b5ff2008-03-31 20:32:52 +00004699 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4700 !LD->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004701 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4703 if (PrevST->getBasePtr() == Ptr &&
4704 PrevST->getValue().getValueType() == N->getValueType(0))
4705 return CombineTo(N, Chain.getOperand(1), Chain);
4706 }
4707 }
4708
4709 if (CombinerAA) {
4710 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004711 SDValue BetterChain = FindBetterChain(N, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004712
4713 // If there is a better chain.
4714 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004715 SDValue ReplLoad;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716
4717 // Replace the chain to void dependency.
4718 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4719 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsa3691432007-10-28 12:59:45 +00004720 LD->getSrcValue(), LD->getSrcValueOffset(),
4721 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722 } else {
4723 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4724 LD->getValueType(0),
4725 BetterChain, Ptr, LD->getSrcValue(),
4726 LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004727 LD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 LD->isVolatile(),
4729 LD->getAlignment());
4730 }
4731
4732 // Create token factor to keep old chain connected.
Dan Gohman8181bd12008-07-27 21:46:04 +00004733 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734 Chain, ReplLoad.getValue(1));
4735
4736 // Replace uses with load result and token factor. Don't add users
4737 // to work list.
4738 return CombineTo(N, ReplLoad.getValue(0), Token, false);
4739 }
4740 }
4741
4742 // Try transforming N to an indexed load.
4743 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004744 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004745
Dan Gohman8181bd12008-07-27 21:46:04 +00004746 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747}
4748
Chris Lattner2e023772008-01-08 23:08:06 +00004749
Dan Gohman8181bd12008-07-27 21:46:04 +00004750SDValue DAGCombiner::visitSTORE(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004752 SDValue Chain = ST->getChain();
4753 SDValue Value = ST->getValue();
4754 SDValue Ptr = ST->getBasePtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755
Chris Lattner4e137af2008-01-25 07:20:16 +00004756 // Try to infer better alignment information than the store already has.
Dan Gohmanea12c0c2008-08-20 16:30:28 +00004757 if (!Fast && ST->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004758 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4759 if (Align > ST->getAlignment())
4760 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004761 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004762 ST->isVolatile(), Align);
4763 }
4764 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004765
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004766 // If this is a store of a bit convert, store the input value if the
4767 // resultant store does not need a higher alignment than the original.
4768 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004769 ST->isUnindexed()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 unsigned Align = ST->getAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00004771 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman404e8542008-09-04 15:39:15 +00004772 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00004773 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sands2418bec2008-06-13 19:07:40 +00004774 if (Align <= OrigAlign &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004775 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman52c51aa2009-01-28 17:46:25 +00004776 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman55a11de2008-06-28 00:45:22 +00004778 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004780
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
4782 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands2418bec2008-06-13 19:07:40 +00004783 // NOTE: If the original store is volatile, this transform must not increase
4784 // the number of stores. For example, on x86-32 an f64 can be stored in one
4785 // processor operation but an i64 (which is not legal) requires two. So the
4786 // transform should not be done in this case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004788 SDValue Tmp;
Duncan Sands92c43912008-06-06 12:08:01 +00004789 switch (CFP->getValueType(0).getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 default: assert(0 && "Unknown FP type");
Dale Johannesen1b4181d2007-09-18 18:36:59 +00004791 case MVT::f80: // We don't do this for these yet.
4792 case MVT::f128:
4793 case MVT::ppcf128:
4794 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 case MVT::f32:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004796 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004797 !ST->isVolatile()) ||
4798 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00004799 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004800 bitcastToAPInt().getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
4802 ST->getSrcValueOffset(), ST->isVolatile(),
4803 ST->getAlignment());
4804 }
4805 break;
4806 case MVT::f64:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004807 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004808 !ST->isVolatile()) ||
4809 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004810 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00004811 getZExtValue(), MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
4813 ST->getSrcValueOffset(), ST->isVolatile(),
4814 ST->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00004815 } else if (!ST->isVolatile() &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00004816 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsa3691432007-10-28 12:59:45 +00004817 // Many FP stores are not made apparent until after legalize, e.g. for
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 // argument passing. Since this is so common, custom legalize the
4819 // 64-bit integer store into two 32-bit stores.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00004820 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00004821 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4822 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00004823 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824
4825 int SVOffset = ST->getSrcValueOffset();
4826 unsigned Alignment = ST->getAlignment();
4827 bool isVolatile = ST->isVolatile();
4828
Dan Gohman8181bd12008-07-27 21:46:04 +00004829 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 ST->getSrcValueOffset(),
4831 isVolatile, ST->getAlignment());
4832 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4833 DAG.getConstant(4, Ptr.getValueType()));
4834 SVOffset += 4;
Duncan Sandsa3691432007-10-28 12:59:45 +00004835 Alignment = MinAlign(Alignment, 4U);
Dan Gohman8181bd12008-07-27 21:46:04 +00004836 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 SVOffset, isVolatile, Alignment);
4838 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4839 }
4840 break;
4841 }
4842 }
4843 }
4844
4845 if (CombinerAA) {
4846 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004847 SDValue BetterChain = FindBetterChain(N, Chain);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004848
4849 // If there is a better chain.
4850 if (Chain != BetterChain) {
4851 // Replace the chain to avoid dependency.
Dan Gohman8181bd12008-07-27 21:46:04 +00004852 SDValue ReplStore;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004853 if (ST->isTruncatingStore()) {
4854 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00004855 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004856 ST->getMemoryVT(),
Chris Lattner667f9c12008-01-17 07:20:38 +00004857 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858 } else {
4859 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00004860 ST->getSrcValue(), ST->getSrcValueOffset(),
4861 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862 }
4863
4864 // Create token to keep both nodes around.
Dan Gohman8181bd12008-07-27 21:46:04 +00004865 SDValue Token =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4867
4868 // Don't add users to work list.
4869 return CombineTo(N, Token, false);
4870 }
4871 }
4872
4873 // Try transforming N to an indexed store.
4874 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004875 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004876
Chris Lattner447d8e82007-12-29 06:26:16 +00004877 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner3bc08502008-01-17 19:59:44 +00004878 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004879 Value.getValueType().isInteger()) {
Chris Lattnere8671c52007-10-13 06:35:54 +00004880 // See if we can simplify the input to this truncstore with knowledge that
4881 // only the low bits are being used. For example:
4882 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman8181bd12008-07-27 21:46:04 +00004883 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00004884 GetDemandedBits(Value,
4885 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00004886 ST->getMemoryVT().getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00004887 AddToWorkList(Value.getNode());
4888 if (Shorter.getNode())
Chris Lattnere8671c52007-10-13 06:35:54 +00004889 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004890 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnere8671c52007-10-13 06:35:54 +00004891 ST->isVolatile(), ST->getAlignment());
Chris Lattnerb77ea552007-10-13 06:58:48 +00004892
4893 // Otherwise, see if we can simplify the operation with
4894 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman11607792008-02-27 00:25:32 +00004895 if (SimplifyDemandedBits(Value,
4896 APInt::getLowBitsSet(
4897 Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00004898 ST->getMemoryVT().getSizeInBits())))
Dan Gohman8181bd12008-07-27 21:46:04 +00004899 return SDValue(N, 0);
Chris Lattnere8671c52007-10-13 06:35:54 +00004900 }
4901
Chris Lattner447d8e82007-12-29 06:26:16 +00004902 // If this is a load followed by a store to the same location, then the store
4903 // is dead/noop.
4904 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004905 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004906 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner2e023772008-01-08 23:08:06 +00004907 // There can't be any side effects between the load and store, such as
4908 // a call or store.
Dan Gohman8181bd12008-07-27 21:46:04 +00004909 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner447d8e82007-12-29 06:26:16 +00004910 // The store is dead, remove it.
4911 return Chain;
4912 }
4913 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004914
Chris Lattner3bc08502008-01-17 19:59:44 +00004915 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4916 // truncating store. We can do this even if this is already a truncstore.
4917 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greif1c80d112008-08-28 21:40:38 +00004918 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00004919 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004920 ST->getMemoryVT())) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004921 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004922 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner3bc08502008-01-17 19:59:44 +00004923 ST->isVolatile(), ST->getAlignment());
4924 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004925
Dan Gohman8181bd12008-07-27 21:46:04 +00004926 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927}
4928
Dan Gohman8181bd12008-07-27 21:46:04 +00004929SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4930 SDValue InVec = N->getOperand(0);
4931 SDValue InVal = N->getOperand(1);
4932 SDValue EltNo = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004933
4934 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4935 // vector with the inserted element.
4936 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004937 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greifb420b9d2008-08-30 19:29:20 +00004938 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4939 InVec.getNode()->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 if (Elt < Ops.size())
4941 Ops[Elt] = InVal;
4942 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4943 &Ops[0], Ops.size());
4944 }
4945
Dan Gohman8181bd12008-07-27 21:46:04 +00004946 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947}
4948
Dan Gohman8181bd12008-07-27 21:46:04 +00004949SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang3512a532009-01-17 00:07:25 +00004950 // (vextract (scalar_to_vector val, 0) -> val
4951 SDValue InVec = N->getOperand(0);
Mon P Wang3512a532009-01-17 00:07:25 +00004952
Mon P Wang017cf182009-01-18 06:43:40 +00004953 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
4954 return InVec.getOperand(0);
Evan Cheng411fc172008-05-13 08:35:03 +00004955
4956 // Perform only after legalization to ensure build_vector / vector_shuffle
4957 // optimizations have already been done.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004958 if (!LegalOperations) return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004959
Mon P Wang3512a532009-01-17 00:07:25 +00004960 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4961 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4962 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wang017cf182009-01-18 06:43:40 +00004963 SDValue EltNo = N->getOperand(1);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004964
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004965 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004966 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004967 bool NewLoad = false;
Mon P Wang3c673102008-12-11 00:26:16 +00004968 bool BCNumEltsChanged = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004969 MVT VT = InVec.getValueType();
4970 MVT EVT = VT.getVectorElementType();
4971 MVT LVT = EVT;
Evan Cheng411fc172008-05-13 08:35:03 +00004972 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands92c43912008-06-06 12:08:01 +00004973 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wang3c673102008-12-11 00:26:16 +00004974 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004975 return SDValue();
Mon P Wang3c673102008-12-11 00:26:16 +00004976 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4977 BCNumEltsChanged = true;
Evan Cheng411fc172008-05-13 08:35:03 +00004978 InVec = InVec.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00004979 EVT = BCVT.getVectorElementType();
Evan Cheng411fc172008-05-13 08:35:03 +00004980 NewLoad = true;
4981 }
Evan Chengd7ba7ed2007-10-06 08:19:55 +00004982
Evan Cheng411fc172008-05-13 08:35:03 +00004983 LoadSDNode *LN0 = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00004984 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng411fc172008-05-13 08:35:03 +00004985 LN0 = cast<LoadSDNode>(InVec);
4986 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4987 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004988 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00004989 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4990 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4991 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4992 // =>
4993 // (load $addr+1*size)
Mon P Wang3c673102008-12-11 00:26:16 +00004994
4995 // If the bit convert changed the number of elements, it is unsafe
4996 // to examine the mask.
4997 if (BCNumEltsChanged)
4998 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00004999 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005000 getOperand(Elt))->getZExtValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005001 unsigned NumElems = InVec.getOperand(2).getNumOperands();
5002 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5003 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5004 InVec = InVec.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00005005 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005006 LN0 = cast<LoadSDNode>(InVec);
5007 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005008 }
5009 }
Duncan Sandsc218a5a2008-06-15 20:12:31 +00005010 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman8181bd12008-07-27 21:46:04 +00005011 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005012
5013 unsigned Align = LN0->getAlignment();
5014 if (NewLoad) {
5015 // Check the resultant load doesn't need a higher alignment than the
5016 // original load.
Dan Gohman404e8542008-09-04 15:39:15 +00005017 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands92c43912008-06-06 12:08:01 +00005018 getABITypeAlignment(LVT.getTypeForMVT());
Dan Gohman52c51aa2009-01-28 17:46:25 +00005019 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00005020 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005021 Align = NewAlign;
5022 }
5023
Dan Gohman8181bd12008-07-27 21:46:04 +00005024 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng411fc172008-05-13 08:35:03 +00005025 if (Elt) {
Duncan Sands92c43912008-06-06 12:08:01 +00005026 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
5027 MVT PtrType = NewPtr.getValueType();
Evan Cheng411fc172008-05-13 08:35:03 +00005028 if (TLI.isBigEndian())
Duncan Sands92c43912008-06-06 12:08:01 +00005029 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng411fc172008-05-13 08:35:03 +00005030 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
5031 DAG.getConstant(PtrOff, PtrType));
5032 }
5033 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
5034 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5035 LN0->isVolatile(), Align);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005036 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005037 return SDValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005038}
5039
5040
Dan Gohman8181bd12008-07-27 21:46:04 +00005041SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005042 unsigned NumInScalars = N->getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00005043 MVT VT = N->getValueType(0);
5044 unsigned NumElts = VT.getVectorNumElements();
5045 MVT EltType = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046
5047 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5048 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5049 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005050 SDValue VecIn1, VecIn2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051 for (unsigned i = 0; i != NumInScalars; ++i) {
5052 // Ignore undef inputs.
5053 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5054
5055 // If this input is something other than a EXTRACT_VECTOR_ELT with a
5056 // constant index, bail out.
5057 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5058 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005059 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 break;
5061 }
5062
5063 // If the input vector type disagrees with the result of the build_vector,
5064 // we can't make a shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00005065 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005066 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005067 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 break;
5069 }
5070
5071 // Otherwise, remember this. We allow up to two distinct input vectors.
5072 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5073 continue;
5074
Gabor Greif1c80d112008-08-28 21:40:38 +00005075 if (VecIn1.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076 VecIn1 = ExtractedFromVec;
Gabor Greif1c80d112008-08-28 21:40:38 +00005077 } else if (VecIn2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005078 VecIn2 = ExtractedFromVec;
5079 } else {
5080 // Too many inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005081 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082 break;
5083 }
5084 }
5085
5086 // If everything is good, we can make a shuffle operation.
Gabor Greif1c80d112008-08-28 21:40:38 +00005087 if (VecIn1.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005088 SmallVector<SDValue, 8> BuildVecIndices;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005089 for (unsigned i = 0; i != NumInScalars; ++i) {
5090 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
5091 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
5092 continue;
5093 }
5094
Dan Gohman8181bd12008-07-27 21:46:04 +00005095 SDValue Extract = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096
5097 // If extracting from the first vector, just use the index directly.
5098 if (Extract.getOperand(0) == VecIn1) {
5099 BuildVecIndices.push_back(Extract.getOperand(1));
5100 continue;
5101 }
5102
5103 // Otherwise, use InIdx + VecSize
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005104 unsigned Idx =
5105 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner5872a362008-01-17 07:00:52 +00005106 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005107 }
5108
5109 // Add count and size info.
Duncan Sands92c43912008-06-06 12:08:01 +00005110 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005111 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5112 return SDValue();
5113
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005114 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005115 SDValue Ops[5];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 Ops[0] = VecIn1;
Gabor Greif1c80d112008-08-28 21:40:38 +00005117 if (VecIn2.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005118 Ops[1] = VecIn2;
5119 } else {
5120 // Use an undef build_vector as input for the second operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00005121 std::vector<SDValue> UnOps(NumInScalars,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005122 DAG.getNode(ISD::UNDEF,
5123 EltType));
5124 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
5125 &UnOps[0], UnOps.size());
Gabor Greif1c80d112008-08-28 21:40:38 +00005126 AddToWorkList(Ops[1].getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127 }
5128 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
5129 &BuildVecIndices[0], BuildVecIndices.size());
5130 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
5131 }
5132
Dan Gohman8181bd12008-07-27 21:46:04 +00005133 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134}
5135
Dan Gohman8181bd12008-07-27 21:46:04 +00005136SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5138 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5139 // inputs come from at most two distinct vectors, turn this into a shuffle
5140 // node.
5141
5142 // If we only have one input vector, we don't need to do any concatenation.
5143 if (N->getNumOperands() == 1) {
5144 return N->getOperand(0);
5145 }
5146
Dan Gohman8181bd12008-07-27 21:46:04 +00005147 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148}
5149
Dan Gohman8181bd12008-07-27 21:46:04 +00005150SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5151 SDValue ShufMask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152 unsigned NumElts = ShufMask.getNumOperands();
5153
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005154 SDValue N0 = N->getOperand(0);
5155 SDValue N1 = N->getOperand(1);
5156
5157 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5158 "Vector shuffle must be normalized in DAG");
5159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5161 bool isIdentity = true;
5162 for (unsigned i = 0; i != NumElts; ++i) {
5163 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005164 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165 isIdentity = false;
5166 break;
5167 }
5168 }
5169 if (isIdentity) return N->getOperand(0);
5170
5171 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5172 isIdentity = true;
5173 for (unsigned i = 0; i != NumElts; ++i) {
5174 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005175 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5176 i+NumElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 isIdentity = false;
5178 break;
5179 }
5180 }
5181 if (isIdentity) return N->getOperand(1);
5182
5183 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5184 // needed at all.
5185 bool isUnary = true;
5186 bool isSplat = true;
5187 int VecNum = -1;
5188 unsigned BaseIdx = 0;
5189 for (unsigned i = 0; i != NumElts; ++i)
5190 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005191 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005192 int V = (Idx < NumElts) ? 0 : 1;
5193 if (VecNum == -1) {
5194 VecNum = V;
5195 BaseIdx = Idx;
5196 } else {
5197 if (BaseIdx != Idx)
5198 isSplat = false;
5199 if (VecNum != V) {
5200 isUnary = false;
5201 break;
5202 }
5203 }
5204 }
5205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 // Normalize unary shuffle so the RHS is undef.
5207 if (isUnary && VecNum == 1)
5208 std::swap(N0, N1);
5209
5210 // If it is a splat, check if the argument vector is a build_vector with
5211 // all scalar elements the same.
5212 if (isSplat) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005213 SDNode *V = N0.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214
5215 // If this is a bit convert that changes the element type of the vector but
5216 // not the number of vector elements, look through it. Be careful not to
5217 // look though conversions that change things like v4f32 to v2f64.
5218 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005219 SDValue ConvInput = V->getOperand(0);
Evan Cheng76b20d12008-07-22 20:42:56 +00005220 if (ConvInput.getValueType().isVector() &&
5221 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greif1c80d112008-08-28 21:40:38 +00005222 V = ConvInput.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005223 }
5224
5225 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5226 unsigned NumElems = V->getNumOperands();
5227 if (NumElems > BaseIdx) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005228 SDValue Base;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 bool AllSame = true;
5230 for (unsigned i = 0; i != NumElems; ++i) {
5231 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5232 Base = V->getOperand(i);
5233 break;
5234 }
5235 }
5236 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greif1c80d112008-08-28 21:40:38 +00005237 if (!Base.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005238 return N0;
5239 for (unsigned i = 0; i != NumElems; ++i) {
Evan Cheng8d68c2b2007-09-18 21:54:37 +00005240 if (V->getOperand(i) != Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241 AllSame = false;
5242 break;
5243 }
5244 }
5245 // Splat of <x, x, x, x>, return <x, x, x, x>
5246 if (AllSame)
5247 return N0;
5248 }
5249 }
5250 }
5251
5252 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5253 // into an undef.
5254 if (isUnary || N0 == N1) {
5255 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5256 // first operand.
Dan Gohman8181bd12008-07-27 21:46:04 +00005257 SmallVector<SDValue, 8> MappedOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005258 for (unsigned i = 0; i != NumElts; ++i) {
5259 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005260 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5261 NumElts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005262 MappedOps.push_back(ShufMask.getOperand(i));
5263 } else {
5264 unsigned NewIdx =
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005265 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5266 NumElts;
Duncan Sandsd3ace282008-07-21 10:20:31 +00005267 MappedOps.push_back(DAG.getConstant(NewIdx,
5268 ShufMask.getOperand(i).getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005269 }
5270 }
5271 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
5272 &MappedOps[0], MappedOps.size());
Gabor Greif1c80d112008-08-28 21:40:38 +00005273 AddToWorkList(ShufMask.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5275 N0,
5276 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5277 ShufMask);
5278 }
5279
Dan Gohman8181bd12008-07-27 21:46:04 +00005280 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281}
5282
5283/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
5284/// an AND to a vector_shuffle with the destination vector and a zero vector.
5285/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
5286/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman8181bd12008-07-27 21:46:04 +00005287SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5288 SDValue LHS = N->getOperand(0);
5289 SDValue RHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 if (N->getOpcode() == ISD::AND) {
5291 if (RHS.getOpcode() == ISD::BIT_CONVERT)
5292 RHS = RHS.getOperand(0);
5293 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005294 std::vector<SDValue> IdxOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005295 unsigned NumOps = RHS.getNumOperands();
5296 unsigned NumElts = NumOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005298 SDValue Elt = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 if (!isa<ConstantSDNode>(Elt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005300 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands193c2bd2008-10-19 14:58:05 +00005302 IdxOps.push_back(DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands193c2bd2008-10-19 14:58:05 +00005304 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005305 else
Dan Gohman8181bd12008-07-27 21:46:04 +00005306 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307 }
5308
5309 // Let's see if the target supports this vector_shuffle.
Duncan Sands193c2bd2008-10-19 14:58:05 +00005310 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312
5313 // Return the new VECTOR_SHUFFLE node.
Duncan Sands193c2bd2008-10-19 14:58:05 +00005314 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00005315 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng4fdfdac2008-11-05 06:04:18 +00005316 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman8181bd12008-07-27 21:46:04 +00005317 std::vector<SDValue> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
5319 Ops.push_back(LHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00005320 AddToWorkList(LHS.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00005321 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
5323 &ZeroOps[0], ZeroOps.size()));
Evan Cheng4fdfdac2008-11-05 06:04:18 +00005324 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 &IdxOps[0], IdxOps.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005326 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 &Ops[0], Ops.size());
Dan Gohman4c219902008-07-16 16:13:58 +00005328 if (VT != N->getValueType(0))
5329 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330 return Result;
5331 }
5332 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005333 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334}
5335
5336/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman8181bd12008-07-27 21:46:04 +00005337SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 // After legalize, the target may be depending on adds and other
5339 // binary ops to provide legal ways to construct constants or other
5340 // things. Simplifying them may result in a loss of legality.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005341 if (LegalOperations) return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005342
Duncan Sands92c43912008-06-06 12:08:01 +00005343 MVT VT = N->getValueType(0);
5344 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345
Duncan Sands92c43912008-06-06 12:08:01 +00005346 MVT EltType = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005347 SDValue LHS = N->getOperand(0);
5348 SDValue RHS = N->getOperand(1);
5349 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00005350 if (Shuffle.getNode()) return Shuffle;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005351
5352 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
5353 // this operation.
5354 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5355 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005356 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005358 SDValue LHSOp = LHS.getOperand(i);
5359 SDValue RHSOp = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005360 // If these two elements can't be folded, bail out.
5361 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5362 LHSOp.getOpcode() != ISD::Constant &&
5363 LHSOp.getOpcode() != ISD::ConstantFP) ||
5364 (RHSOp.getOpcode() != ISD::UNDEF &&
5365 RHSOp.getOpcode() != ISD::Constant &&
5366 RHSOp.getOpcode() != ISD::ConstantFP))
5367 break;
5368 // Can't fold divide by zero.
5369 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5370 N->getOpcode() == ISD::FDIV) {
5371 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005372 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005374 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375 break;
5376 }
5377 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greif1c80d112008-08-28 21:40:38 +00005378 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5380 Ops.back().getOpcode() == ISD::Constant ||
5381 Ops.back().getOpcode() == ISD::ConstantFP) &&
5382 "Scalar binop didn't fold!");
5383 }
5384
5385 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands92c43912008-06-06 12:08:01 +00005386 MVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005387 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
5388 }
5389 }
5390
Dan Gohman8181bd12008-07-27 21:46:04 +00005391 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392}
5393
Dan Gohman8181bd12008-07-27 21:46:04 +00005394SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5396
Dan Gohman8181bd12008-07-27 21:46:04 +00005397 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005398 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5399 // If we got a simplified select_cc node back from SimplifySelectCC, then
5400 // break it down into a new SETCC node, and a new SELECT node, and then return
5401 // the SELECT node, since we were called with a SELECT node.
Gabor Greif1c80d112008-08-28 21:40:38 +00005402 if (SCC.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005403 // Check to see if we got a select_cc back (to turn into setcc/select).
5404 // Otherwise, just return whatever node we got back, like fabs.
5405 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005406 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005407 SCC.getOperand(0), SCC.getOperand(1),
5408 SCC.getOperand(4));
Gabor Greif1c80d112008-08-28 21:40:38 +00005409 AddToWorkList(SETCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5411 SCC.getOperand(3), SETCC);
5412 }
5413 return SCC;
5414 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005415 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416}
5417
5418/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5419/// are the two values being selected between, see if we can simplify the
5420/// select. Callers of this should assume that TheSelect is deleted if this
5421/// returns true. As such, they should return the appropriate thing (e.g. the
5422/// node) back to the top-level of the DAG combiner loop to avoid it being
5423/// looked at.
5424///
Dan Gohman8181bd12008-07-27 21:46:04 +00005425bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5426 SDValue RHS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427
5428 // If this is a select from two identical things, try to pull the operation
5429 // through the select.
5430 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
5431 // If this is a load and the token chain is identical, replace the select
5432 // of two loads with a load through a select of the address to load from.
5433 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5434 // constants have been dropped into the constant pool.
5435 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00005436 // Do not let this transformation reduce the number of volatile loads.
5437 !cast<LoadSDNode>(LHS)->isVolatile() &&
5438 !cast<LoadSDNode>(RHS)->isVolatile() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 // Token chains must be identical.
5440 LHS.getOperand(0) == RHS.getOperand(0)) {
5441 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5442 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5443
5444 // If this is an EXTLOAD, the VT's must match.
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005445 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446 // FIXME: this conflates two src values, discarding one. This is not
5447 // the right thing to do, but nothing uses srcvalues now. When they do,
5448 // turn SrcValue into a list of locations.
Dan Gohman8181bd12008-07-27 21:46:04 +00005449 SDValue Addr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005450 if (TheSelect->getOpcode() == ISD::SELECT) {
5451 // Check that the condition doesn't reach either load. If so, folding
5452 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005453 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5454 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5456 TheSelect->getOperand(0), LLD->getBasePtr(),
5457 RLD->getBasePtr());
5458 }
5459 } else {
5460 // Check that the condition doesn't reach either load. If so, folding
5461 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005462 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5463 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5464 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5465 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005466 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
5467 TheSelect->getOperand(0),
5468 TheSelect->getOperand(1),
5469 LLD->getBasePtr(), RLD->getBasePtr(),
5470 TheSelect->getOperand(4));
5471 }
5472 }
5473
Gabor Greif1c80d112008-08-28 21:40:38 +00005474 if (Addr.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005475 SDValue Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5477 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5478 Addr,LLD->getSrcValue(),
5479 LLD->getSrcValueOffset(),
5480 LLD->isVolatile(),
5481 LLD->getAlignment());
5482 else {
5483 Load = DAG.getExtLoad(LLD->getExtensionType(),
5484 TheSelect->getValueType(0),
5485 LLD->getChain(), Addr, LLD->getSrcValue(),
5486 LLD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005487 LLD->getMemoryVT(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 LLD->isVolatile(),
5489 LLD->getAlignment());
5490 }
5491 // Users of the select now use the result of the load.
5492 CombineTo(TheSelect, Load);
5493
5494 // Users of the old loads now use the new load's chain. We know the
5495 // old-load value is dead now.
Gabor Greif1c80d112008-08-28 21:40:38 +00005496 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5497 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 return true;
5499 }
5500 }
5501 }
5502 }
5503
5504 return false;
5505}
5506
Dan Gohman8181bd12008-07-27 21:46:04 +00005507SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5508 SDValue N2, SDValue N3,
5509 ISD::CondCode CC, bool NotExtCompare) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005510
Duncan Sands92c43912008-06-06 12:08:01 +00005511 MVT VT = N2.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +00005512 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5513 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5514 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005515
5516 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00005517 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5518 N0, N1, CC, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00005519 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5520 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005521
5522 // fold select_cc true, x, y -> x
Dan Gohman9d24dc72008-03-13 22:13:53 +00005523 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 return N2;
5525 // fold select_cc false, x, y -> y
Dan Gohman9d24dc72008-03-13 22:13:53 +00005526 if (SCCC && SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527 return N3;
5528
5529 // Check to see if we can simplify the select into an fabs node
5530 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5531 // Allow either -0.0 or 0.0
Dale Johannesen7f2c1d12007-08-25 22:10:57 +00005532 if (CFP->getValueAPF().isZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5534 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5535 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5536 N2 == N3.getOperand(0))
5537 return DAG.getNode(ISD::FABS, VT, N0);
5538
5539 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5540 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5541 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5542 N2.getOperand(0) == N3)
5543 return DAG.getNode(ISD::FABS, VT, N3);
5544 }
5545 }
5546
5547 // Check to see if we can perform the "gzip trick", transforming
5548 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
5549 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands92c43912008-06-06 12:08:01 +00005550 N0.getValueType().isInteger() &&
5551 N2.getValueType().isInteger() &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005552 (N1C->isNullValue() || // (a < 0) ? b : 0
5553 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands92c43912008-06-06 12:08:01 +00005554 MVT XType = N0.getValueType();
5555 MVT AType = N2.getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00005556 if (XType.bitsGE(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
5558 // single-bit constant.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005559 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5560 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands92c43912008-06-06 12:08:01 +00005561 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00005562 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5563 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greif1c80d112008-08-28 21:40:38 +00005564 AddToWorkList(Shift.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00005565 if (XType.bitsGT(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005567 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005568 }
5569 return DAG.getNode(ISD::AND, AType, Shift, N2);
5570 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005571 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005572 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005573 TLI.getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00005574 AddToWorkList(Shift.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00005575 if (XType.bitsGT(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005577 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 }
5579 return DAG.getNode(ISD::AND, AType, Shift, N2);
5580 }
5581 }
5582
5583 // fold select C, 16, 0 -> shl C, 4
Dan Gohman9d24dc72008-03-13 22:13:53 +00005584 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands8cf4a822008-11-23 15:47:28 +00005585 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586
5587 // If the caller doesn't want us to simplify this into a zext of a compare,
5588 // don't do it.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005589 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591
5592 // Get a SetCC of the condition
5593 // FIXME: Should probably make sure that setcc is legal if we ever have a
5594 // target where it isn't.
Dan Gohman8181bd12008-07-27 21:46:04 +00005595 SDValue Temp, SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 // cast from setcc result type to select result type
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005597 if (LegalTypes) {
Duncan Sands4a361272009-01-01 15:52:00 +00005598 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5599 N0, N1, CC);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005600 if (N2.getValueType().bitsLT(SCC.getValueType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5602 else
5603 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
5604 } else {
5605 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
5606 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
5607 }
Gabor Greif1c80d112008-08-28 21:40:38 +00005608 AddToWorkList(SCC.getNode());
5609 AddToWorkList(Temp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610
Dan Gohman9d24dc72008-03-13 22:13:53 +00005611 if (N2C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612 return Temp;
5613 // shl setcc result by log2 n2c
5614 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman9d24dc72008-03-13 22:13:53 +00005615 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616 TLI.getShiftAmountTy()));
5617 }
5618
5619 // Check to see if this is the equivalent of setcc
5620 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5621 // otherwise, go ahead with the folds.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005622 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005623 MVT XType = N0.getValueType();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005624 if (!LegalOperations ||
Duncan Sands4a361272009-01-01 15:52:00 +00005625 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5626 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627 if (Res.getValueType() != VT)
5628 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5629 return Res;
5630 }
5631
5632 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5633 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005634 (!LegalOperations ||
Duncan Sands6ae1a0632008-06-14 17:48:34 +00005635 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005636 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005637 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands92c43912008-06-06 12:08:01 +00005638 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005639 TLI.getShiftAmountTy()));
5640 }
5641 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5642 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005643 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 N0);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005645 SDValue NotN0 = DAG.getNOT(N0, XType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005646 return DAG.getNode(ISD::SRL, XType,
5647 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands92c43912008-06-06 12:08:01 +00005648 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 TLI.getShiftAmountTy()));
5650 }
5651 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5652 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005653 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005654 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655 TLI.getShiftAmountTy()));
5656 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5657 }
5658 }
5659
5660 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5661 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5662 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
5663 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands92c43912008-06-06 12:08:01 +00005664 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5665 MVT XType = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005666 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005667 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005668 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005669 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005670 AddToWorkList(Shift.getNode());
5671 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005672 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5673 }
5674 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5675 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5676 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5677 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5678 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands92c43912008-06-06 12:08:01 +00005679 MVT XType = N0.getValueType();
5680 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005681 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands92c43912008-06-06 12:08:01 +00005682 DAG.getConstant(XType.getSizeInBits()-1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683 TLI.getShiftAmountTy()));
Dan Gohman8181bd12008-07-27 21:46:04 +00005684 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005685 AddToWorkList(Shift.getNode());
5686 AddToWorkList(Add.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005687 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5688 }
5689 }
5690 }
5691
Dan Gohman8181bd12008-07-27 21:46:04 +00005692 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005693}
5694
5695/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman8181bd12008-07-27 21:46:04 +00005696SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5697 SDValue N1, ISD::CondCode Cond,
5698 bool foldBooleans) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005699 TargetLowering::DAGCombinerInfo
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005700 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005701 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
5702}
5703
5704/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5705/// return a DAG expression to select that will generate the same value by
5706/// multiplying by a magic number. See:
5707/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00005708SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005709 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00005710 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005711
5712 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
5713 ii != ee; ++ii)
5714 AddToWorkList(*ii);
5715 return S;
5716}
5717
5718/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5719/// return a DAG expression to select that will generate the same value by
5720/// multiplying by a magic number. See:
5721/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00005722SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005723 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00005724 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725
5726 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
5727 ii != ee; ++ii)
5728 AddToWorkList(*ii);
5729 return S;
5730}
5731
5732/// FindBaseOffset - Return true if base is known not to alias with anything
5733/// but itself. Provides base object and offset as results.
Dan Gohman8181bd12008-07-27 21:46:04 +00005734static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005735 // Assume it is a primitive operation.
5736 Base = Ptr; Offset = 0;
5737
5738 // If it's an adding a simple constant then integrate the offset.
5739 if (Base.getOpcode() == ISD::ADD) {
5740 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5741 Base = Base.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005742 Offset += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743 }
5744 }
5745
5746 // If it's any of the following then it can't alias with anything but itself.
5747 return isa<FrameIndexSDNode>(Base) ||
5748 isa<ConstantPoolSDNode>(Base) ||
5749 isa<GlobalAddressSDNode>(Base);
5750}
5751
5752/// isAlias - Return true if there is any possibility that the two addresses
5753/// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +00005754bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005755 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman8181bd12008-07-27 21:46:04 +00005756 SDValue Ptr2, int64_t Size2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757 const Value *SrcValue2, int SrcValueOffset2)
5758{
5759 // If they are the same then they must be aliases.
5760 if (Ptr1 == Ptr2) return true;
5761
5762 // Gather base node and offset information.
Dan Gohman8181bd12008-07-27 21:46:04 +00005763 SDValue Base1, Base2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 int64_t Offset1, Offset2;
5765 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5766 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5767
5768 // If they have a same base address then...
5769 if (Base1 == Base2) {
5770 // Check to see if the addresses overlap.
5771 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5772 }
5773
5774 // If we know both bases then they can't alias.
5775 if (KnownBase1 && KnownBase2) return false;
5776
5777 if (CombinerGlobalAA) {
5778 // Use alias analysis information.
Dan Gohmane142c2e2007-08-27 16:32:11 +00005779 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5780 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5781 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782 AliasAnalysis::AliasResult AAResult =
5783 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
5784 if (AAResult == AliasAnalysis::NoAlias)
5785 return false;
5786 }
5787
5788 // Otherwise we have to assume they alias.
5789 return true;
5790}
5791
5792/// FindAliasInfo - Extracts the relevant alias information from the memory
5793/// node. Returns true if the operand was a load.
5794bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +00005795 SDValue &Ptr, int64_t &Size,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005796 const Value *&SrcValue, int &SrcValueOffset) {
5797 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5798 Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00005799 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 SrcValue = LD->getSrcValue();
5801 SrcValueOffset = LD->getSrcValueOffset();
5802 return true;
5803 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
5804 Ptr = ST->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00005805 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005806 SrcValue = ST->getSrcValue();
5807 SrcValueOffset = ST->getSrcValueOffset();
5808 } else {
5809 assert(0 && "FindAliasInfo expected a memory operand");
5810 }
5811
5812 return false;
5813}
5814
5815/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5816/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00005817void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5818 SmallVector<SDValue, 8> &Aliases) {
5819 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820 std::set<SDNode *> Visited; // Visited node set.
5821
5822 // Get alias information for node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005823 SDValue Ptr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005824 int64_t Size;
5825 const Value *SrcValue;
5826 int SrcValueOffset;
5827 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
5828
5829 // Starting off.
5830 Chains.push_back(OriginalChain);
5831
5832 // Look at each chain and determine if it is an alias. If so, add it to the
5833 // aliases list. If not, then continue up the chain looking for the next
5834 // candidate.
5835 while (!Chains.empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005836 SDValue Chain = Chains.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005837 Chains.pop_back();
5838
5839 // Don't bother if we've been before.
Gabor Greif1c80d112008-08-28 21:40:38 +00005840 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5841 Visited.insert(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005842
5843 switch (Chain.getOpcode()) {
5844 case ISD::EntryToken:
5845 // Entry token is ideal chain operand, but handled in FindBetterChain.
5846 break;
5847
5848 case ISD::LOAD:
5849 case ISD::STORE: {
5850 // Get alias information for Chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00005851 SDValue OpPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005852 int64_t OpSize;
5853 const Value *OpSrcValue;
5854 int OpSrcValueOffset;
Gabor Greif1c80d112008-08-28 21:40:38 +00005855 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 OpSrcValue, OpSrcValueOffset);
5857
5858 // If chain is alias then stop here.
5859 if (!(IsLoad && IsOpLoad) &&
5860 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5861 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
5862 Aliases.push_back(Chain);
5863 } else {
5864 // Look further up the chain.
5865 Chains.push_back(Chain.getOperand(0));
5866 // Clean up old chain.
Gabor Greif1c80d112008-08-28 21:40:38 +00005867 AddToWorkList(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868 }
5869 break;
5870 }
5871
5872 case ISD::TokenFactor:
5873 // We have to check each of the operands of the token factor, so we queue
5874 // then up. Adding the operands to the queue (stack) in reverse order
5875 // maintains the original order and increases the likelihood that getNode
5876 // will find a matching token factor (CSE.)
5877 for (unsigned n = Chain.getNumOperands(); n;)
5878 Chains.push_back(Chain.getOperand(--n));
5879 // Eliminate the token factor if we can.
Gabor Greif1c80d112008-08-28 21:40:38 +00005880 AddToWorkList(Chain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005881 break;
5882
5883 default:
5884 // For all other instructions we will just have to take what we can get.
5885 Aliases.push_back(Chain);
5886 break;
5887 }
5888 }
5889}
5890
5891/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5892/// for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +00005893SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5894 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005895
5896 // Accumulate all the aliases to this node.
5897 GatherAllAliases(N, OldChain, Aliases);
5898
5899 if (Aliases.size() == 0) {
5900 // If no operands then chain to entry token.
5901 return DAG.getEntryNode();
5902 } else if (Aliases.size() == 1) {
5903 // If a single operand then chain to it. We don't need to revisit it.
5904 return Aliases[0];
5905 }
5906
5907 // Construct a custom tailored token factor.
Dan Gohman8181bd12008-07-27 21:46:04 +00005908 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005909 &Aliases[0], Aliases.size());
5910
5911 // Make sure the old chain gets cleaned up.
Gabor Greif1c80d112008-08-28 21:40:38 +00005912 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005913
5914 return NewChain;
5915}
5916
5917// SelectionDAG::Combine - This is the entry point for the file.
5918//
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005919void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005920 /// run - This is the main entry point to this class.
5921 ///
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005922 DAGCombiner(*this, AA, Fast).Run(Level);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923}