blob: e494da3644d69323dc6adbb7d934c106c5069726 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +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.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000052 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000053 CombineLevel Level;
54 bool LegalOperations;
55 bool LegalTypes;
Dan Gohmana2676512008-08-20 16:30:28 +000056 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000059 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000060
Jim Laskeyc7c3f112006-10-16 20:52:31 +000061 // AA - Used for DAG load/store alias analysis.
62 AliasAnalysis &AA;
63
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 /// 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();
Nate Begeman4ebd8052005-09-01 23:24:04 +000070 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000071 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000072 }
73
Dan Gohman389079b2007-10-08 17:57:15 +000074 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000076 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000077
Chris Lattner24664722006-03-01 04:53:38 +000078 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000079 /// AddToWorkList - Add to the work list making sure it's instance is at the
80 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000081 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000082 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000083 WorkList.push_back(N);
84 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000085
Chris Lattnerf8dc0612008-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());
Chris Lattner01a22022005-10-10 22:04:48 +000091 }
Nate Begeman368e18d2006-02-16 21:11:51 +000092
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000094 bool AddTo = true);
95
Dan Gohman475871a2008-07-27 21:46:04 +000096 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000097 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000098 }
99
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +0000101 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000103 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000104 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000105
Chris Lattner24664722006-03-01 04:53:38 +0000106 private:
107
Chris Lattner012f2412006-02-17 21:58:01 +0000108 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000109 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000110 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000111 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000112 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
113 return SimplifyDemandedBits(Op, Demanded);
114 }
115
Dan Gohman475871a2008-07-27 21:46:04 +0000116 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000117
Chris Lattner448f2192006-11-11 00:39:41 +0000118 bool CombineToPreIndexedLoadStore(SDNode *N);
119 bool CombineToPostIndexedLoadStore(SDNode *N);
120
121
Dan Gohman389079b2007-10-08 17:57:15 +0000122 /// combine - call the node-specific routine that knows how to fold each
123 /// particular type of node. If that doesn't do anything, try the
124 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000126
127 // Visitation implementation - Implement dag node combining for different
128 // node types. The semantics are as follows:
129 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000130 // SDValue.getNode() == 0 - No change was made
131 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
132 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000133 //
Dan Gohman475871a2008-07-27 21:46:04 +0000134 SDValue visitTokenFactor(SDNode *N);
135 SDValue visitMERGE_VALUES(SDNode *N);
136 SDValue visitADD(SDNode *N);
137 SDValue visitSUB(SDNode *N);
138 SDValue visitADDC(SDNode *N);
139 SDValue visitADDE(SDNode *N);
140 SDValue visitMUL(SDNode *N);
141 SDValue visitSDIV(SDNode *N);
142 SDValue visitUDIV(SDNode *N);
143 SDValue visitSREM(SDNode *N);
144 SDValue visitUREM(SDNode *N);
145 SDValue visitMULHU(SDNode *N);
146 SDValue visitMULHS(SDNode *N);
147 SDValue visitSMUL_LOHI(SDNode *N);
148 SDValue visitUMUL_LOHI(SDNode *N);
149 SDValue visitSDIVREM(SDNode *N);
150 SDValue visitUDIVREM(SDNode *N);
151 SDValue visitAND(SDNode *N);
152 SDValue visitOR(SDNode *N);
153 SDValue visitXOR(SDNode *N);
154 SDValue SimplifyVBinOp(SDNode *N);
155 SDValue visitSHL(SDNode *N);
156 SDValue visitSRA(SDNode *N);
157 SDValue visitSRL(SDNode *N);
158 SDValue visitCTLZ(SDNode *N);
159 SDValue visitCTTZ(SDNode *N);
160 SDValue visitCTPOP(SDNode *N);
161 SDValue visitSELECT(SDNode *N);
162 SDValue visitSELECT_CC(SDNode *N);
163 SDValue visitSETCC(SDNode *N);
164 SDValue visitSIGN_EXTEND(SDNode *N);
165 SDValue visitZERO_EXTEND(SDNode *N);
166 SDValue visitANY_EXTEND(SDNode *N);
167 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
168 SDValue visitTRUNCATE(SDNode *N);
169 SDValue visitBIT_CONVERT(SDNode *N);
170 SDValue visitBUILD_PAIR(SDNode *N);
171 SDValue visitFADD(SDNode *N);
172 SDValue visitFSUB(SDNode *N);
173 SDValue visitFMUL(SDNode *N);
174 SDValue visitFDIV(SDNode *N);
175 SDValue visitFREM(SDNode *N);
176 SDValue visitFCOPYSIGN(SDNode *N);
177 SDValue visitSINT_TO_FP(SDNode *N);
178 SDValue visitUINT_TO_FP(SDNode *N);
179 SDValue visitFP_TO_SINT(SDNode *N);
180 SDValue visitFP_TO_UINT(SDNode *N);
181 SDValue visitFP_ROUND(SDNode *N);
182 SDValue visitFP_ROUND_INREG(SDNode *N);
183 SDValue visitFP_EXTEND(SDNode *N);
184 SDValue visitFNEG(SDNode *N);
185 SDValue visitFABS(SDNode *N);
186 SDValue visitBRCOND(SDNode *N);
187 SDValue visitBR_CC(SDNode *N);
188 SDValue visitLOAD(SDNode *N);
189 SDValue visitSTORE(SDNode *N);
190 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
191 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
192 SDValue visitBUILD_VECTOR(SDNode *N);
193 SDValue visitCONCAT_VECTORS(SDNode *N);
194 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000195
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDValue XformToShuffleWithZero(SDNode *N);
197 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000198
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
202 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
203 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
204 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
205 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000206 bool NotExtCompare = false);
Duncan Sands25cf2272008-11-24 14:53:14 +0000207 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
208 bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000210 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000211 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
212 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
213 SDValue BuildSDIV(SDNode *N);
214 SDValue BuildUDIV(SDNode *N);
215 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
216 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000217
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000219
Jim Laskey6ff23e52006-10-04 16:53:27 +0000220 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
221 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000222 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
223 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000224
Jim Laskey096c22e2006-10-18 12:29:57 +0000225 /// isAlias - Return true if there is any possibility that the two addresses
226 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000227 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000228 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000229 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000230 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000231
Jim Laskey7ca56af2006-10-11 13:47:09 +0000232 /// FindAliasInfo - Extracts the relevant alias information from the memory
233 /// node. Returns true if the operand was a load.
234 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000236 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000237
Jim Laskey279f0532006-09-25 16:29:54 +0000238 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000239 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000240 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000241
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242public:
Dan Gohmana2676512008-08-20 16:30:28 +0000243 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000244 : DAG(D),
245 TLI(D.getTargetLoweringInfo()),
Duncan Sands25cf2272008-11-24 14:53:14 +0000246 Level(Unrestricted),
247 LegalOperations(false),
248 LegalTypes(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000249 Fast(fast),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000250 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000251
252 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000253 void Run(CombineLevel AtLevel);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000254 };
255}
256
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000257
258namespace {
259/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
260/// nodes from the worklist.
261class VISIBILITY_HIDDEN WorkListRemover :
262 public SelectionDAG::DAGUpdateListener {
263 DAGCombiner &DC;
264public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000265 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000266
Duncan Sandsedfcf592008-06-11 11:42:12 +0000267 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000268 DC.removeFromWorkList(N);
269 }
270
271 virtual void NodeUpdated(SDNode *N) {
272 // Ignore updates.
273 }
274};
275}
276
Chris Lattner24664722006-03-01 04:53:38 +0000277//===----------------------------------------------------------------------===//
278// TargetLowering::DAGCombinerInfo implementation
279//===----------------------------------------------------------------------===//
280
281void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
282 ((DAGCombiner*)DC)->AddToWorkList(N);
283}
284
Dan Gohman475871a2008-07-27 21:46:04 +0000285SDValue TargetLowering::DAGCombinerInfo::
286CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000287 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000288}
289
Dan Gohman475871a2008-07-27 21:46:04 +0000290SDValue TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000292 return ((DAGCombiner*)DC)->CombineTo(N, Res);
293}
294
295
Dan Gohman475871a2008-07-27 21:46:04 +0000296SDValue TargetLowering::DAGCombinerInfo::
297CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000298 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
299}
300
301
Chris Lattner24664722006-03-01 04:53:38 +0000302//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000303// Helper Functions
304//===----------------------------------------------------------------------===//
305
306/// isNegatibleForFree - Return 1 if we can compute the negated form of the
307/// specified expression for the same cost as the expression itself, or 2 if we
308/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000309static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000310 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000311 // No compile time optimizations on this type.
312 if (Op.getValueType() == MVT::ppcf128)
313 return 0;
314
Chris Lattner29446522007-05-14 22:04:50 +0000315 // fneg is removable even if it has multiple uses.
316 if (Op.getOpcode() == ISD::FNEG) return 2;
317
318 // Don't allow anything with multiple uses.
319 if (!Op.hasOneUse()) return 0;
320
Chris Lattner3adf9512007-05-25 02:19:06 +0000321 // Don't recurse exponentially.
322 if (Depth > 6) return 0;
323
Chris Lattner29446522007-05-14 22:04:50 +0000324 switch (Op.getOpcode()) {
325 default: return false;
326 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000327 // Don't invert constant FP values after legalize. The negated constant
328 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000329 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000330 case ISD::FADD:
331 // FIXME: determine better conditions for this xform.
332 if (!UnsafeFPMath) return 0;
333
334 // -(A+B) -> -A - B
Duncan Sands25cf2272008-11-24 14:53:14 +0000335 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000336 return V;
337 // -(A+B) -> -B - A
Duncan Sands25cf2272008-11-24 14:53:14 +0000338 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000339 case ISD::FSUB:
340 // We can't turn -(A-B) into B-A when we honor signed zeros.
341 if (!UnsafeFPMath) return 0;
342
343 // -(A-B) -> B-A
344 return 1;
345
346 case ISD::FMUL:
347 case ISD::FDIV:
348 if (HonorSignDependentRoundingFPMath()) return 0;
349
350 // -(X*Y) -> (-X * Y) or (X*-Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000351 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000352 return V;
353
Duncan Sands25cf2272008-11-24 14:53:14 +0000354 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000355
356 case ISD::FP_EXTEND:
357 case ISD::FP_ROUND:
358 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000359 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000360 }
361}
362
363/// GetNegatedExpression - If isNegatibleForFree returns true, this function
364/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000365static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000366 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000367 // fneg is removable even if it has multiple uses.
368 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
369
370 // Don't allow anything with multiple uses.
371 assert(Op.hasOneUse() && "Unknown reuse!");
372
Chris Lattner3adf9512007-05-25 02:19:06 +0000373 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000374 switch (Op.getOpcode()) {
375 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000376 case ISD::ConstantFP: {
377 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
378 V.changeSign();
379 return DAG.getConstantFP(V, Op.getValueType());
380 }
Chris Lattner29446522007-05-14 22:04:50 +0000381 case ISD::FADD:
382 // FIXME: determine better conditions for this xform.
383 assert(UnsafeFPMath);
384
385 // -(A+B) -> -A - B
Duncan Sands25cf2272008-11-24 14:53:14 +0000386 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000387 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000388 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000389 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000390 Op.getOperand(1));
391 // -(A+B) -> -B - A
392 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000393 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000394 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000395 Op.getOperand(0));
396 case ISD::FSUB:
397 // We can't turn -(A-B) into B-A when we honor signed zeros.
398 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000399
400 // -(0-B) -> B
401 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000402 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000403 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000404
405 // -(A-B) -> B-A
406 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
407 Op.getOperand(0));
408
409 case ISD::FMUL:
410 case ISD::FDIV:
411 assert(!HonorSignDependentRoundingFPMath());
412
413 // -(X*Y) -> -X * Y
Duncan Sands25cf2272008-11-24 14:53:14 +0000414 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000416 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000417 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000418 Op.getOperand(1));
419
420 // -(X*Y) -> X * -Y
421 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
422 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000423 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000424 LegalOperations, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000425
426 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000427 case ISD::FSIN:
428 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000430 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000431 case ISD::FP_ROUND:
432 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000433 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000434 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000435 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000436 }
437}
Chris Lattner24664722006-03-01 04:53:38 +0000438
439
Nate Begeman4ebd8052005-09-01 23:24:04 +0000440// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
441// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000442// Also, set the incoming LHS, RHS, and CC references to the appropriate
443// nodes based on the type of node we are checking. This simplifies life a
444// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000445static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
446 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000447 if (N.getOpcode() == ISD::SETCC) {
448 LHS = N.getOperand(0);
449 RHS = N.getOperand(1);
450 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000451 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 if (N.getOpcode() == ISD::SELECT_CC &&
454 N.getOperand(2).getOpcode() == ISD::Constant &&
455 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000456 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
458 LHS = N.getOperand(0);
459 RHS = N.getOperand(1);
460 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463 return false;
464}
465
Nate Begeman99801192005-09-07 23:25:52 +0000466// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
467// one use. If this is true, it allows the users to invert the operation for
468// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000469static bool isOneUseSetCC(SDValue N) {
470 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000471 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000472 return true;
473 return false;
474}
475
Dan Gohman475871a2008-07-27 21:46:04 +0000476SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000477 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000478 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
479 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
480 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
481 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000483 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
485 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000486 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000487 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000488 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
489 }
490 }
491 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
492 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
493 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
494 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000495 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000496 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
498 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000499 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000500 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
502 }
503 }
Dan Gohman475871a2008-07-27 21:46:04 +0000504 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505}
506
Dan Gohman475871a2008-07-27 21:46:04 +0000507SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
508 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000509 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
510 ++NodesCombined;
511 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000512 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000513 DOUT << " and " << NumTo-1 << " other values\n";
Dan Gohman764fd0c2009-01-21 15:17:51 +0000514 DEBUG(for (unsigned i = 0, e = NumTo; i != e; ++i)
515 assert(N->getValueType(i) == To[i].getValueType() &&
516 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000517 WorkListRemover DeadNodes(*this);
518 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
519
520 if (AddTo) {
521 // Push the new nodes and any users onto the worklist
522 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000523 AddToWorkList(To[i].getNode());
524 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000525 }
526 }
527
Dan Gohmandbe664a2009-01-19 21:44:21 +0000528 // Finally, if the node is now dead, remove it from the graph. The node
529 // may not be dead if the replacement process recursively simplified to
530 // something else needing this node.
531 if (N->use_empty()) {
532 // Nodes can be reintroduced into the worklist. Make sure we do not
533 // process a node that has been replaced.
534 removeFromWorkList(N);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000535
Dan Gohmandbe664a2009-01-19 21:44:21 +0000536 // Finally, since the node is now dead, remove it from the graph.
537 DAG.DeleteNode(N);
538 }
Dan Gohman475871a2008-07-27 21:46:04 +0000539 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000540}
541
542/// SimplifyDemandedBits - Check the specified integer node value to see if
543/// it can be simplified or if things it uses can be simplified by bit
544/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000545bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Duncan Sands25cf2272008-11-24 14:53:14 +0000546 TargetLowering::TargetLoweringOpt TLO(DAG);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000547 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
549 return false;
550
551 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000552 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000553
554 // Replace the old value with the new one.
555 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000556 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
557 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000558 DOUT << '\n';
559
560 // Replace all uses. If any nodes become isomorphic to other nodes and
561 // are deleted, make sure to remove them from our worklist.
562 WorkListRemover DeadNodes(*this);
563 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
564
565 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000566 AddToWorkList(TLO.New.getNode());
567 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000568
569 // Finally, if the node is now dead, remove it from the graph. The node
570 // may not be dead if the replacement process recursively simplified to
571 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000572 if (TLO.Old.getNode()->use_empty()) {
573 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000574
575 // If the operands of this node are only used by the node, they will now
576 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000577 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
578 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
579 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000580
Gabor Greifba36cb52008-08-28 21:40:38 +0000581 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000582 }
583 return true;
584}
585
Chris Lattner29446522007-05-14 22:04:50 +0000586//===----------------------------------------------------------------------===//
587// Main DAG Combiner implementation
588//===----------------------------------------------------------------------===//
589
Duncan Sands25cf2272008-11-24 14:53:14 +0000590void DAGCombiner::Run(CombineLevel AtLevel) {
591 // set the instance variables, so that the various visit routines may use it.
592 Level = AtLevel;
593 LegalOperations = Level >= NoIllegalOperations;
594 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000595
Evan Cheng17a568b2008-08-29 22:21:44 +0000596 // Add all the dag nodes to the worklist.
597 WorkList.reserve(DAG.allnodes_size());
598 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
599 E = DAG.allnodes_end(); I != E; ++I)
600 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000601
Evan Cheng17a568b2008-08-29 22:21:44 +0000602 // Create a dummy node (which is not added to allnodes), that adds a reference
603 // to the root node, preventing it from being deleted, and tracking any
604 // changes of the root.
605 HandleSDNode Dummy(DAG.getRoot());
606
Jim Laskey26f7fa72006-10-17 19:33:52 +0000607 // The root of the dag may dangle to deleted nodes until the dag combiner is
608 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000609 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000610
Evan Cheng17a568b2008-08-29 22:21:44 +0000611 // while the worklist isn't empty, inspect the node on the end of it and
612 // try and combine it.
613 while (!WorkList.empty()) {
614 SDNode *N = WorkList.back();
615 WorkList.pop_back();
616
617 // If N has no uses, it is dead. Make sure to revisit all N's operands once
618 // N is deleted from the DAG, since they too may now be dead or may have a
619 // reduced number of uses, allowing other xforms.
620 if (N->use_empty() && N != &Dummy) {
621 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
622 AddToWorkList(N->getOperand(i).getNode());
623
624 DAG.DeleteNode(N);
625 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000626 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000627
628 SDValue RV = combine(N);
629
630 if (RV.getNode() == 0)
631 continue;
632
633 ++NodesCombined;
634
635 // If we get back the same node we passed in, rather than a new node or
636 // zero, we know that the node must have defined multiple values and
637 // CombineTo was used. Since CombineTo takes care of the worklist
638 // mechanics for us, we have no work to do in this case.
639 if (RV.getNode() == N)
640 continue;
641
642 assert(N->getOpcode() != ISD::DELETED_NODE &&
643 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
644 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000645
Evan Cheng17a568b2008-08-29 22:21:44 +0000646 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
647 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
648 DOUT << '\n';
649 WorkListRemover DeadNodes(*this);
650 if (N->getNumValues() == RV.getNode()->getNumValues())
651 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
652 else {
653 assert(N->getValueType(0) == RV.getValueType() &&
654 N->getNumValues() == 1 && "Type mismatch");
655 SDValue OpV = RV;
656 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
657 }
658
659 // Push the new node and any users onto the worklist
660 AddToWorkList(RV.getNode());
661 AddUsersToWorkList(RV.getNode());
662
663 // Add any uses of the old node to the worklist in case this node is the
664 // last one that uses them. They may become dead after this node is
665 // deleted.
666 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
667 AddToWorkList(N->getOperand(i).getNode());
668
Dan Gohmandbe664a2009-01-19 21:44:21 +0000669 // Finally, if the node is now dead, remove it from the graph. The node
670 // may not be dead if the replacement process recursively simplified to
671 // something else needing this node.
672 if (N->use_empty()) {
673 // Nodes can be reintroduced into the worklist. Make sure we do not
674 // process a node that has been replaced.
675 removeFromWorkList(N);
Evan Cheng17a568b2008-08-29 22:21:44 +0000676
Dan Gohmandbe664a2009-01-19 21:44:21 +0000677 // Finally, since the node is now dead, remove it from the graph.
678 DAG.DeleteNode(N);
679 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000680 }
681
Chris Lattner95038592005-10-05 06:35:28 +0000682 // If the root changed (e.g. it was a dead load, update the root).
683 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684}
685
Dan Gohman475871a2008-07-27 21:46:04 +0000686SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000687 switch(N->getOpcode()) {
688 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000689 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000690 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000691 case ISD::ADD: return visitADD(N);
692 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000693 case ISD::ADDC: return visitADDC(N);
694 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 case ISD::MUL: return visitMUL(N);
696 case ISD::SDIV: return visitSDIV(N);
697 case ISD::UDIV: return visitUDIV(N);
698 case ISD::SREM: return visitSREM(N);
699 case ISD::UREM: return visitUREM(N);
700 case ISD::MULHU: return visitMULHU(N);
701 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000702 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
703 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
704 case ISD::SDIVREM: return visitSDIVREM(N);
705 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000706 case ISD::AND: return visitAND(N);
707 case ISD::OR: return visitOR(N);
708 case ISD::XOR: return visitXOR(N);
709 case ISD::SHL: return visitSHL(N);
710 case ISD::SRA: return visitSRA(N);
711 case ISD::SRL: return visitSRL(N);
712 case ISD::CTLZ: return visitCTLZ(N);
713 case ISD::CTTZ: return visitCTTZ(N);
714 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000715 case ISD::SELECT: return visitSELECT(N);
716 case ISD::SELECT_CC: return visitSELECT_CC(N);
717 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
719 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000720 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000721 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
722 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000723 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000724 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000725 case ISD::FADD: return visitFADD(N);
726 case ISD::FSUB: return visitFSUB(N);
727 case ISD::FMUL: return visitFMUL(N);
728 case ISD::FDIV: return visitFDIV(N);
729 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000730 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000731 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
732 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
733 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
734 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
735 case ISD::FP_ROUND: return visitFP_ROUND(N);
736 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
737 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
738 case ISD::FNEG: return visitFNEG(N);
739 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000740 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000741 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000742 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000743 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000744 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000745 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000746 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
747 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000748 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000749 }
Dan Gohman475871a2008-07-27 21:46:04 +0000750 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000751}
752
Dan Gohman475871a2008-07-27 21:46:04 +0000753SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000754
Dan Gohman475871a2008-07-27 21:46:04 +0000755 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000756
757 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000758 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000759 assert(N->getOpcode() != ISD::DELETED_NODE &&
760 "Node was deleted but visit returned NULL!");
761
762 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
763 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
764
765 // Expose the DAG combiner to the target combiner impls.
766 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +0000767 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000768
769 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
770 }
771 }
772
Evan Cheng08b11732008-03-22 01:55:50 +0000773 // If N is a commutative binary node, try commuting it to enable more
774 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000775 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000776 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
777 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000778 SDValue N0 = N->getOperand(0);
779 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000780 // Constant operands are canonicalized to RHS.
781 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000782 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000783 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
784 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000785 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000786 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000787 }
788 }
789
Dan Gohman389079b2007-10-08 17:57:15 +0000790 return RV;
791}
792
Chris Lattner6270f682006-10-08 22:57:01 +0000793/// getInputChainForNode - Given a node, return its input chain if it has one,
794/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000795static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000796 if (unsigned NumOps = N->getNumOperands()) {
797 if (N->getOperand(0).getValueType() == MVT::Other)
798 return N->getOperand(0);
799 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
800 return N->getOperand(NumOps-1);
801 for (unsigned i = 1; i < NumOps-1; ++i)
802 if (N->getOperand(i).getValueType() == MVT::Other)
803 return N->getOperand(i);
804 }
Dan Gohman475871a2008-07-27 21:46:04 +0000805 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000806}
807
Dan Gohman475871a2008-07-27 21:46:04 +0000808SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000809 // If N has two operands, where one has an input chain equal to the other,
810 // the 'other' chain is redundant.
811 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000812 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000813 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000814 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000815 return N->getOperand(1);
816 }
817
Chris Lattnerc76d4412007-05-16 06:37:59 +0000818 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000819 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000820 SmallPtrSet<SDNode*, 16> SeenOps;
821 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000822
823 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000824 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000825
Jim Laskey71382342006-10-07 23:37:56 +0000826 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000827 // encountered.
828 for (unsigned i = 0; i < TFs.size(); ++i) {
829 SDNode *TF = TFs[i];
830
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 // Check each of the operands.
832 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000833 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000834
Jim Laskey6ff23e52006-10-04 16:53:27 +0000835 switch (Op.getOpcode()) {
836 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000837 // Entry tokens don't need to be added to the list. They are
838 // rededundant.
839 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000840 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000841
Jim Laskey6ff23e52006-10-04 16:53:27 +0000842 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000843 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000844 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000845 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000846 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000847 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000848 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849 Changed = true;
850 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000851 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 // Fall thru
853
854 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000855 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000856 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000857 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000858 else
859 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000860 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000861 }
862 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000863 }
864
Dan Gohman475871a2008-07-27 21:46:04 +0000865 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000866
867 // If we've change things around then replace token factor.
868 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000869 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000870 // The entry token is the only possible outcome.
871 Result = DAG.getEntryNode();
872 } else {
873 // New and improved token factor.
874 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000875 }
Jim Laskey274062c2006-10-13 23:32:28 +0000876
877 // Don't add users to work list.
878 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000879 }
Jim Laskey279f0532006-09-25 16:29:54 +0000880
Jim Laskey6ff23e52006-10-04 16:53:27 +0000881 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000882}
883
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000884/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000885SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000886 WorkListRemover DeadNodes(*this);
887 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000888 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000889 &DeadNodes);
890 removeFromWorkList(N);
891 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000892 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000893}
894
895
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000896static
Dan Gohman475871a2008-07-27 21:46:04 +0000897SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000898 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000899 SDValue N00 = N0.getOperand(0);
900 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000901 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000902 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000903 isa<ConstantSDNode>(N00.getOperand(1))) {
904 N0 = DAG.getNode(ISD::ADD, VT,
905 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
906 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
907 return DAG.getNode(ISD::ADD, VT, N0, N1);
908 }
Dan Gohman475871a2008-07-27 21:46:04 +0000909 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000910}
911
Evan Chengb13cdbd2007-06-21 07:39:16 +0000912static
Dan Gohman475871a2008-07-27 21:46:04 +0000913SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000914 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sands25cf2272008-11-24 14:53:14 +0000915 bool LegalOperations) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000916 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000917 unsigned Opc = N->getOpcode();
918 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
920 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000921 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000922
923 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000924 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000925 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000926 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000927 if (CCOp.getOpcode() == ISD::SETCC)
928 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
929 }
930
931 bool DoXform = false;
932 bool InvCC = false;
933 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
934 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000935
Evan Chengb13cdbd2007-06-21 07:39:16 +0000936 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000937 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000938 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000939 } else if (CC != ISD::SETCC_INVALID &&
940 RHS.getOpcode() == ISD::Constant &&
941 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000942 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000943 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000944 MVT OpVT = isSlctCC ? Op0.getValueType() :
945 Op0.getOperand(0).getValueType();
946 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000947 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000948
Duncan Sands25cf2272008-11-24 14:53:14 +0000949 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendlingae89bb12008-11-11 08:25:46 +0000950 return SDValue(); // Inverse operator isn't legal.
951
Evan Chengb13cdbd2007-06-21 07:39:16 +0000952 DoXform = true;
953 InvCC = true;
954 }
955
956 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000957 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000958 if (isSlctCC)
959 return DAG.getSelectCC(OtherOp, Result,
960 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000961 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000962 if (InvCC)
963 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
964 CCOp.getOperand(1), CC);
965 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
966 }
Dan Gohman475871a2008-07-27 21:46:04 +0000967 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000968}
969
Dan Gohman475871a2008-07-27 21:46:04 +0000970SDValue DAGCombiner::visitADD(SDNode *N) {
971 SDValue N0 = N->getOperand(0);
972 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
974 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000975 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000976
977 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000978 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000981 }
Bill Wendling2476e5d2008-12-10 22:36:00 +0000982
Dan Gohman613e0d82007-07-03 14:03:57 +0000983 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000984 if (N0.getOpcode() == ISD::UNDEF)
985 return N0;
986 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000987 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000988 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000989 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000990 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000991 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000992 if (N0C && !N1C)
993 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000994 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000995 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000996 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000997 // fold (add Sym, c) -> Sym+c
998 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +0000999 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001000 GA->getOpcode() == ISD::GlobalAddress)
1001 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1002 GA->getOffset() +
1003 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001004 // fold ((c1-A)+c2) -> (c1+c2)-A
1005 if (N1C && N0.getOpcode() == ISD::SUB)
1006 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
1007 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001008 DAG.getConstant(N1C->getAPIntValue()+
1009 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001010 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001011 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +00001012 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001013 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001014 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001015 // fold ((0-A) + B) -> B-A
1016 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1017 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001018 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 // fold (A + (0-B)) -> A-B
1020 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1021 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001022 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001023 // fold (A+(B-A)) -> B
1024 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001025 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001026 // fold ((B-A)+A) -> B
1027 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1028 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001029 // fold (A+(B-(A+C))) to (B-C)
1030 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1031 N0 == N1.getOperand(1).getOperand(0)) {
1032 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1033 N1.getOperand(1).getOperand(1));
1034 }
1035 // fold (A+(B-(C+A))) to (B-C)
1036 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1037 N0 == N1.getOperand(1).getOperand(1)) {
1038 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1039 N1.getOperand(1).getOperand(0));
1040 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001041 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001042 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1043 N1.getOperand(0).getOpcode() == ISD::SUB &&
1044 N0 == N1.getOperand(0).getOperand(1)) {
1045 return DAG.getNode(N1.getOpcode(), VT, N1.getOperand(0).getOperand(0),
1046 N1.getOperand(1));
1047 }
1048
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001049 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1050 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1051 SDValue N00 = N0.getOperand(0);
1052 SDValue N01 = N0.getOperand(1);
1053 SDValue N10 = N1.getOperand(0);
1054 SDValue N11 = N1.getOperand(1);
1055 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) {
1056 return DAG.getNode(ISD::SUB, VT,
1057 DAG.getNode(ISD::ADD, VT, N00, N10),
1058 DAG.getNode(ISD::ADD, VT, N01, N11));
1059 }
1060 }
Chris Lattner947c2892006-03-13 06:51:27 +00001061
Dan Gohman475871a2008-07-27 21:46:04 +00001062 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1063 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001064
1065 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001066 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001067 APInt LHSZero, LHSOne;
1068 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001069 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001070 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001071 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001072 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001073
1074 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1075 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1076 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1077 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1078 return DAG.getNode(ISD::OR, VT, N0, N1);
1079 }
1080 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001081
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001082 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001083 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001084 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001085 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001086 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001087 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001088 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001089 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001090 }
1091
Evan Chengb13cdbd2007-06-21 07:39:16 +00001092 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001093 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001094 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001095 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001096 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001097 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001098 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001099 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001100 }
1101
Dan Gohman475871a2008-07-27 21:46:04 +00001102 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001103}
1104
Dan Gohman475871a2008-07-27 21:46:04 +00001105SDValue DAGCombiner::visitADDC(SDNode *N) {
1106 SDValue N0 = N->getOperand(0);
1107 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001108 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1109 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001110 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001111
1112 // If the flag result is dead, turn this into an ADD.
1113 if (N->hasNUsesOfValue(0, 1))
1114 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001115 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001116
1117 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001118 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001119 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001120
Chris Lattnerb6541762007-03-04 20:40:38 +00001121 // fold (addc x, 0) -> x + no carry out
1122 if (N1C && N1C->isNullValue())
1123 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1124
1125 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001126 APInt LHSZero, LHSOne;
1127 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001128 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001129 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001130 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001131 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001132
1133 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1134 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1135 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1136 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1137 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1138 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1139 }
Chris Lattner91153682007-03-04 20:03:15 +00001140
Dan Gohman475871a2008-07-27 21:46:04 +00001141 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001142}
1143
Dan Gohman475871a2008-07-27 21:46:04 +00001144SDValue DAGCombiner::visitADDE(SDNode *N) {
1145 SDValue N0 = N->getOperand(0);
1146 SDValue N1 = N->getOperand(1);
1147 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001148 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1149 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001150 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001151
1152 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001153 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001154 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001155
Chris Lattnerb6541762007-03-04 20:40:38 +00001156 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001157 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001158 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001159
Dan Gohman475871a2008-07-27 21:46:04 +00001160 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001161}
1162
1163
1164
Dan Gohman475871a2008-07-27 21:46:04 +00001165SDValue DAGCombiner::visitSUB(SDNode *N) {
1166 SDValue N0 = N->getOperand(0);
1167 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001168 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1169 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001170 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171
Dan Gohman7f321562007-06-25 16:23:39 +00001172 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001173 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001174 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001175 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001176 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001177
Chris Lattner854077d2005-10-17 01:07:11 +00001178 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001179 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001180 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001181 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001182 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001183 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001184 // fold (sub x, c) -> (add x, -c)
1185 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001186 return DAG.getNode(ISD::ADD, VT, N0,
1187 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001188 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001189 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001190 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001191 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001192 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001193 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001194 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001195 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001196 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1197 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001198 N0.getOperand(1).getOperand(0) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001199 return DAG.getNode(N0.getOperand(1).getOpcode(), VT, N0.getOperand(0),
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001200 N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001201 // fold ((A+(C+B))-B) -> A+C
1202 if (N0.getOpcode() == ISD::ADD &&
1203 N0.getOperand(1).getOpcode() == ISD::ADD &&
1204 N0.getOperand(1).getOperand(1) == N1)
1205 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
1206 N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001207 // fold ((A-(B-C))-C) -> A-B
1208 if (N0.getOpcode() == ISD::SUB &&
1209 N0.getOperand(1).getOpcode() == ISD::SUB &&
1210 N0.getOperand(1).getOperand(1) == N1)
1211 return DAG.getNode(ISD::SUB, VT, N0.getOperand(0),
1212 N0.getOperand(1).getOperand(0));
Evan Chengb13cdbd2007-06-21 07:39:16 +00001213 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001214 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001215 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001216 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001217 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001218 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001219 if (N0.getOpcode() == ISD::UNDEF)
1220 return N0;
1221 if (N1.getOpcode() == ISD::UNDEF)
1222 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001223
Dan Gohman6520e202008-10-18 02:06:02 +00001224 // If the relocation model supports it, consider symbol offsets.
1225 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001226 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001227 // fold (sub Sym, c) -> Sym-c
1228 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1229 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1230 GA->getOffset() -
1231 (uint64_t)N1C->getSExtValue());
1232 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1233 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1234 if (GA->getGlobal() == GB->getGlobal())
1235 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1236 VT);
1237 }
1238
Dan Gohman475871a2008-07-27 21:46:04 +00001239 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240}
1241
Dan Gohman475871a2008-07-27 21:46:04 +00001242SDValue DAGCombiner::visitMUL(SDNode *N) {
1243 SDValue N0 = N->getOperand(0);
1244 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001245 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1246 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001247 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001248
Dan Gohman7f321562007-06-25 16:23:39 +00001249 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001250 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001251 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001253 }
Dan Gohman7f321562007-06-25 16:23:39 +00001254
Dan Gohman613e0d82007-07-03 14:03:57 +00001255 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001256 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001257 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001258 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001259 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001260 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001261 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001262 if (N0C && !N1C)
1263 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001265 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001266 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001268 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001269 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001270 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001271 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001272 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001273 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001274 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001275 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001276 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001277 // FIXME: If the input is something that is easily negated (e.g. a
1278 // single-use add), we should put the negate there.
1279 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1280 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001281 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001282 TLI.getShiftAmountTy())));
1283 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001284
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001285 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1286 if (N1C && N0.getOpcode() == ISD::SHL &&
1287 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001288 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001289 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001290 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1291 }
1292
1293 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1294 // use.
1295 {
Dan Gohman475871a2008-07-27 21:46:04 +00001296 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001297 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1298 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001299 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001300 Sh = N0; Y = N1;
1301 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001302 isa<ConstantSDNode>(N1.getOperand(1)) &&
1303 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001304 Sh = N1; Y = N0;
1305 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001306 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001308 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1309 }
1310 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001311 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001312 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001313 isa<ConstantSDNode>(N0.getOperand(1))) {
1314 return DAG.getNode(ISD::ADD, VT,
1315 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1316 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1317 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001318
Nate Begemancd4d58c2006-02-03 06:46:56 +00001319 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001320 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001321 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001322 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001323
Dan Gohman475871a2008-07-27 21:46:04 +00001324 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325}
1326
Dan Gohman475871a2008-07-27 21:46:04 +00001327SDValue DAGCombiner::visitSDIV(SDNode *N) {
1328 SDValue N0 = N->getOperand(0);
1329 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001330 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1331 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001332 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001333
Dan Gohman7f321562007-06-25 16:23:39 +00001334 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001335 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001336 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001337 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001338 }
Dan Gohman7f321562007-06-25 16:23:39 +00001339
Nate Begeman1d4d4142005-09-01 00:19:25 +00001340 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001341 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001342 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001343 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001344 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001345 return N0;
1346 // fold (sdiv X, -1) -> 0-X
1347 if (N1C && N1C->isAllOnesValue())
1348 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001349 // If we know the sign bits of both operands are zero, strength reduce to a
1350 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001351 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001352 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001353 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1354 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001355 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001356 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001357 (isPowerOf2_64(N1C->getSExtValue()) ||
1358 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001359 // If dividing by powers of two is cheap, then don't perform the following
1360 // fold.
1361 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001362 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001363 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001364 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001365 unsigned lg2 = Log2_64(abs2);
1366 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001367 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001368 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001369 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001370 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001371 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001373 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001374 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001375 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001376 AddToWorkList(SRL.getNode());
1377 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001378 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001379 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001380 // If we're dividing by a positive value, we're done. Otherwise, we must
1381 // negate the result.
1382 if (pow2 > 0)
1383 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001384 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001385 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1386 }
Nate Begeman69575232005-10-20 02:15:44 +00001387 // if integer divide is expensive and we satisfy the requirements, emit an
1388 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001389 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001390 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001391 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001392 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001393 }
Dan Gohman7f321562007-06-25 16:23:39 +00001394
Dan Gohman613e0d82007-07-03 14:03:57 +00001395 // undef / X -> 0
1396 if (N0.getOpcode() == ISD::UNDEF)
1397 return DAG.getConstant(0, VT);
1398 // X / undef -> undef
1399 if (N1.getOpcode() == ISD::UNDEF)
1400 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001401
Dan Gohman475871a2008-07-27 21:46:04 +00001402 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001403}
1404
Dan Gohman475871a2008-07-27 21:46:04 +00001405SDValue DAGCombiner::visitUDIV(SDNode *N) {
1406 SDValue N0 = N->getOperand(0);
1407 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1409 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001410 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001411
Dan Gohman7f321562007-06-25 16:23:39 +00001412 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001413 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001415 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001416 }
Dan Gohman7f321562007-06-25 16:23:39 +00001417
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001419 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001420 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001421 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001422 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001423 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001424 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001425 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001426 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1427 if (N1.getOpcode() == ISD::SHL) {
1428 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001429 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001430 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001431 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001432 DAG.getConstant(SHC->getAPIntValue()
1433 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001434 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001435 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001436 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001437 }
1438 }
1439 }
Nate Begeman69575232005-10-20 02:15:44 +00001440 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001441 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001442 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001443 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001444 }
Dan Gohman7f321562007-06-25 16:23:39 +00001445
Dan Gohman613e0d82007-07-03 14:03:57 +00001446 // undef / X -> 0
1447 if (N0.getOpcode() == ISD::UNDEF)
1448 return DAG.getConstant(0, VT);
1449 // X / undef -> undef
1450 if (N1.getOpcode() == ISD::UNDEF)
1451 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001452
Dan Gohman475871a2008-07-27 21:46:04 +00001453 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001454}
1455
Dan Gohman475871a2008-07-27 21:46:04 +00001456SDValue DAGCombiner::visitSREM(SDNode *N) {
1457 SDValue N0 = N->getOperand(0);
1458 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001459 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1460 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001461 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001462
1463 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001464 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001465 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001466 // If we know the sign bits of both operands are zero, strength reduce to a
1467 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001468 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001469 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001470 return DAG.getNode(ISD::UREM, VT, N0, N1);
1471 }
Chris Lattner26d29902006-10-12 20:58:32 +00001472
Dan Gohman77003042007-11-26 23:46:11 +00001473 // If X/C can be simplified by the division-by-constant logic, lower
1474 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001475 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001476 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001477 AddToWorkList(Div.getNode());
1478 SDValue OptimizedDiv = combine(Div.getNode());
1479 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001480 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1481 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001482 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001483 return Sub;
1484 }
Chris Lattner26d29902006-10-12 20:58:32 +00001485 }
1486
Dan Gohman613e0d82007-07-03 14:03:57 +00001487 // undef % X -> 0
1488 if (N0.getOpcode() == ISD::UNDEF)
1489 return DAG.getConstant(0, VT);
1490 // X % undef -> undef
1491 if (N1.getOpcode() == ISD::UNDEF)
1492 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001493
Dan Gohman475871a2008-07-27 21:46:04 +00001494 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001495}
1496
Dan Gohman475871a2008-07-27 21:46:04 +00001497SDValue DAGCombiner::visitUREM(SDNode *N) {
1498 SDValue N0 = N->getOperand(0);
1499 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001502 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001503
1504 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001505 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001506 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001507 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001508 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1509 return DAG.getNode(ISD::AND, VT, N0,
1510 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001511 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1512 if (N1.getOpcode() == ISD::SHL) {
1513 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001514 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001515 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001516 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001517 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001518 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001519 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001520 return DAG.getNode(ISD::AND, VT, N0, Add);
1521 }
1522 }
1523 }
Chris Lattner26d29902006-10-12 20:58:32 +00001524
Dan Gohman77003042007-11-26 23:46:11 +00001525 // If X/C can be simplified by the division-by-constant logic, lower
1526 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001527 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001528 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001529 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001530 SDValue OptimizedDiv = combine(Div.getNode());
1531 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001532 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1533 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001534 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001535 return Sub;
1536 }
Chris Lattner26d29902006-10-12 20:58:32 +00001537 }
1538
Dan Gohman613e0d82007-07-03 14:03:57 +00001539 // undef % X -> 0
1540 if (N0.getOpcode() == ISD::UNDEF)
1541 return DAG.getConstant(0, VT);
1542 // X % undef -> undef
1543 if (N1.getOpcode() == ISD::UNDEF)
1544 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001545
Dan Gohman475871a2008-07-27 21:46:04 +00001546 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001547}
1548
Dan Gohman475871a2008-07-27 21:46:04 +00001549SDValue DAGCombiner::visitMULHS(SDNode *N) {
1550 SDValue N0 = N->getOperand(0);
1551 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001553 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001554
1555 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001556 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001557 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001558 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001559 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001560 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001561 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001562 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001563 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001564 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001565 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001566
Dan Gohman475871a2008-07-27 21:46:04 +00001567 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001568}
1569
Dan Gohman475871a2008-07-27 21:46:04 +00001570SDValue DAGCombiner::visitMULHU(SDNode *N) {
1571 SDValue N0 = N->getOperand(0);
1572 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001573 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001574 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001575
1576 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001577 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001578 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001579 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001580 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001581 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001582 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001583 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001584 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001585
Dan Gohman475871a2008-07-27 21:46:04 +00001586 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001587}
1588
Dan Gohman389079b2007-10-08 17:57:15 +00001589/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1590/// compute two values. LoOp and HiOp give the opcodes for the two computations
1591/// that are being performed. Return true if a simplification was made.
1592///
Dan Gohman475871a2008-07-27 21:46:04 +00001593SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1594 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001595 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001596 bool HiExists = N->hasAnyUseOfValue(1);
1597 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001598 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001599 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001600 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001601 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001602 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001603 }
1604
1605 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001606 bool LoExists = N->hasAnyUseOfValue(0);
1607 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001608 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001609 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001610 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001611 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001612 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001613 }
1614
Evan Cheng44711942007-11-08 09:25:29 +00001615 // If both halves are used, return as it is.
1616 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001617 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001618
1619 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001620 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001621 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001622 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001623 AddToWorkList(Lo.getNode());
1624 SDValue LoOpt = combine(Lo.getNode());
1625 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001626 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001627 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001628 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001629 }
1630
Evan Cheng44711942007-11-08 09:25:29 +00001631 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001633 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001634 AddToWorkList(Hi.getNode());
1635 SDValue HiOpt = combine(Hi.getNode());
1636 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001637 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001638 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001639 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001640 }
Dan Gohman475871a2008-07-27 21:46:04 +00001641 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001642}
1643
Dan Gohman475871a2008-07-27 21:46:04 +00001644SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1645 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001646 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001647
Dan Gohman475871a2008-07-27 21:46:04 +00001648 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001649}
1650
Dan Gohman475871a2008-07-27 21:46:04 +00001651SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1652 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001653 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001654
Dan Gohman475871a2008-07-27 21:46:04 +00001655 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001656}
1657
Dan Gohman475871a2008-07-27 21:46:04 +00001658SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1659 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001660 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001661
Dan Gohman475871a2008-07-27 21:46:04 +00001662 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001663}
1664
Dan Gohman475871a2008-07-27 21:46:04 +00001665SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1666 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001667 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001668
Dan Gohman475871a2008-07-27 21:46:04 +00001669 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001670}
1671
Chris Lattner35e5c142006-05-05 05:51:50 +00001672/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1673/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001674SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1675 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001676 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001677 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1678
Chris Lattner540121f2006-05-05 06:31:05 +00001679 // For each of OP in AND/OR/XOR:
1680 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1681 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1682 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001683 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001684 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001685 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001686 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001687 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001688 N0.getOperand(0).getValueType(),
1689 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001690 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001691 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001692 }
1693
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001694 // For each of OP in SHL/SRL/SRA/AND...
1695 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1696 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1697 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001698 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001699 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001700 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001701 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001702 N0.getOperand(0).getValueType(),
1703 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001705 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1706 }
1707
Dan Gohman475871a2008-07-27 21:46:04 +00001708 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001709}
1710
Dan Gohman475871a2008-07-27 21:46:04 +00001711SDValue DAGCombiner::visitAND(SDNode *N) {
1712 SDValue N0 = N->getOperand(0);
1713 SDValue N1 = N->getOperand(1);
1714 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001715 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1716 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001717 MVT VT = N1.getValueType();
1718 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001719
Dan Gohman7f321562007-06-25 16:23:39 +00001720 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001721 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001722 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001723 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001724 }
Dan Gohman7f321562007-06-25 16:23:39 +00001725
Dan Gohman613e0d82007-07-03 14:03:57 +00001726 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001727 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001728 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001729 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001730 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001731 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001732 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001733 if (N0C && !N1C)
1734 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001735 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001736 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001737 return N0;
1738 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001739 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001740 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001741 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001742 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001743 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001744 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001745 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001746 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001747 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001748 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001749 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001750 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001751 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1752 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001753 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001754 APInt Mask = ~N1C->getAPIntValue();
1755 Mask.trunc(N0Op0.getValueSizeInBits());
1756 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001757 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001758 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001759
1760 // Replace uses of the AND with uses of the Zero extend node.
1761 CombineTo(N, Zext);
1762
Chris Lattner3603cd62006-02-02 07:17:31 +00001763 // We actually want to replace all uses of the any_extend with the
1764 // zero_extend, to avoid duplicating things. This will later cause this
1765 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001766 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001767 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001768 }
1769 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001770 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1771 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1772 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1773 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1774
1775 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001776 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001777 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001778 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001779 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001780 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001781 return DAG.getSetCC(VT, ORNode, LR, Op1);
1782 }
1783 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1784 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001785 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001786 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001787 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1788 }
1789 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1790 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001791 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001792 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001793 return DAG.getSetCC(VT, ORNode, LR, Op1);
1794 }
1795 }
1796 // canonicalize equivalent to ll == rl
1797 if (LL == RR && LR == RL) {
1798 Op1 = ISD::getSetCCSwappedOperands(Op1);
1799 std::swap(RL, RR);
1800 }
1801 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001802 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001803 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001804 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001805 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001806 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1807 }
1808 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001809
1810 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1811 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001812 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001813 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001814 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001815
Nate Begemande996292006-02-03 22:24:05 +00001816 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1817 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001818 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001819 SimplifyDemandedBits(SDValue(N, 0)))
1820 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001821 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001822 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001823 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001824 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001825 // If we zero all the possible extended bits, then we can turn this into
1826 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001827 unsigned BitWidth = N1.getValueSizeInBits();
1828 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001829 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001830 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001831 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001832 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001833 LN0->getBasePtr(), LN0->getSrcValue(),
1834 LN0->getSrcValueOffset(), EVT,
1835 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001836 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001838 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001839 }
1840 }
1841 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001842 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001843 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001844 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001845 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001846 // If we zero all the possible extended bits, then we can turn this into
1847 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001848 unsigned BitWidth = N1.getValueSizeInBits();
1849 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001850 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001851 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001852 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001853 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001854 LN0->getBasePtr(), LN0->getSrcValue(),
1855 LN0->getSrcValueOffset(), EVT,
1856 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001857 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001858 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001859 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001860 }
1861 }
Chris Lattner15045b62006-02-28 06:35:35 +00001862
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001863 // fold (and (load x), 255) -> (zextload x, i8)
1864 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001865 if (N1C && N0.getOpcode() == ISD::LOAD) {
1866 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1867 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001868 LN0->isUnindexed() && N0.hasOneUse() &&
1869 // Do not change the width of a volatile load.
1870 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001871 MVT EVT = MVT::Other;
1872 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1873 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1874 EVT = MVT::getIntegerVT(ActiveBits);
1875
1876 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001877 // Do not generate loads of non-round integer types since these can
1878 // be expensive (and would be wrong if the type is not byte sized).
1879 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001880 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001881 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001882 // For big endian targets, we need to add an offset to the pointer to
1883 // load the correct bytes. For little endian systems, we merely need to
1884 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001885 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1886 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001887 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001888 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001889 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001890 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001891 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1892 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001893 Alignment = MinAlign(Alignment, PtrOff);
1894 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001895 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001896 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001897 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001898 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001899 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001900 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001901 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001902 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001903 }
Chris Lattner15045b62006-02-28 06:35:35 +00001904 }
1905 }
1906
Dan Gohman475871a2008-07-27 21:46:04 +00001907 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001908}
1909
Dan Gohman475871a2008-07-27 21:46:04 +00001910SDValue DAGCombiner::visitOR(SDNode *N) {
1911 SDValue N0 = N->getOperand(0);
1912 SDValue N1 = N->getOperand(1);
1913 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001914 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1915 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001916 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001917
Dan Gohman7f321562007-06-25 16:23:39 +00001918 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001919 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001921 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001922 }
Dan Gohman7f321562007-06-25 16:23:39 +00001923
Dan Gohman613e0d82007-07-03 14:03:57 +00001924 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001925 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001926 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001927 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001928 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001929 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001930 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001931 if (N0C && !N1C)
1932 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001934 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001935 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001936 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001937 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001938 return N1;
1939 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001940 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001941 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001942 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001943 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001944 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001945 return ROR;
1946 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001947 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001948 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001949 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1950 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1951 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001952 DAG.getConstant(N1C->getAPIntValue() |
1953 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001954 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001955 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1956 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1957 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1958 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1959
1960 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001961 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001962 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1963 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001964 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001965 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001966 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001967 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001968 return DAG.getSetCC(VT, ORNode, LR, Op1);
1969 }
1970 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1971 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1972 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1973 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001974 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001975 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001976 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1977 }
1978 }
1979 // canonicalize equivalent to ll == rl
1980 if (LL == RR && LR == RL) {
1981 Op1 = ISD::getSetCCSwappedOperands(Op1);
1982 std::swap(RL, RR);
1983 }
1984 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001985 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001986 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001987 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001988 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001989 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1990 }
1991 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001992
1993 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1994 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001995 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001997 }
Chris Lattner516b9622006-09-14 20:50:57 +00001998
Chris Lattner1ec72732006-09-14 21:11:37 +00001999 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
2000 if (N0.getOpcode() == ISD::AND &&
2001 N1.getOpcode() == ISD::AND &&
2002 N0.getOperand(1).getOpcode() == ISD::Constant &&
2003 N1.getOperand(1).getOpcode() == ISD::Constant &&
2004 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002005 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002006 // We can only do this xform if we know that bits from X that are set in C2
2007 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002008 const APInt &LHSMask =
2009 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2010 const APInt &RHSMask =
2011 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00002012
Dan Gohmanea859be2007-06-22 14:59:07 +00002013 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2014 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00002016 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
2017 }
2018 }
2019
2020
Chris Lattner516b9622006-09-14 20:50:57 +00002021 // See if this is some rotate idiom.
2022 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00002023 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002024
Dan Gohman475871a2008-07-27 21:46:04 +00002025 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002026}
2027
Chris Lattner516b9622006-09-14 20:50:57 +00002028
2029/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002030static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002031 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002032 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002033 Mask = Op.getOperand(1);
2034 Op = Op.getOperand(0);
2035 } else {
2036 return false;
2037 }
2038 }
2039
2040 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2041 Shift = Op;
2042 return true;
2043 }
2044 return false;
2045}
2046
2047
2048// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2049// idioms for rotate, and if the target supports rotation instructions, generate
2050// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00002051SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002052 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002053 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002054 if (!TLI.isTypeLegal(VT)) return 0;
2055
2056 // The target must have at least one rotate flavor.
2057 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
2058 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
2059 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002060
Chris Lattner516b9622006-09-14 20:50:57 +00002061 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002062 SDValue LHSShift; // The shift.
2063 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002064 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2065 return 0; // Not part of a rotate.
2066
Dan Gohman475871a2008-07-27 21:46:04 +00002067 SDValue RHSShift; // The shift.
2068 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002069 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2070 return 0; // Not part of a rotate.
2071
2072 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2073 return 0; // Not shifting the same value.
2074
2075 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2076 return 0; // Shifts must disagree.
2077
2078 // Canonicalize shl to left side in a shl/srl pair.
2079 if (RHSShift.getOpcode() == ISD::SHL) {
2080 std::swap(LHS, RHS);
2081 std::swap(LHSShift, RHSShift);
2082 std::swap(LHSMask , RHSMask );
2083 }
2084
Duncan Sands83ec4b62008-06-06 12:08:01 +00002085 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SDValue LHSShiftArg = LHSShift.getOperand(0);
2087 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2088 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002089
2090 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2091 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002092 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2093 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002094 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2095 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002096 if ((LShVal + RShVal) != OpSizeInBits)
2097 return 0;
2098
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002100 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002101 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002102 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002103 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002104
2105 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002106 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002107 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002108
Gabor Greifba36cb52008-08-28 21:40:38 +00002109 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002110 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2111 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002112 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002113 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002114 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2115 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002116 }
2117
2118 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2119 }
2120
Gabor Greifba36cb52008-08-28 21:40:38 +00002121 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002122 }
2123
2124 // If there is a mask here, and we have a variable shift, we can't be sure
2125 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002126 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002127 return 0;
2128
2129 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2130 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002131 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2132 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002133 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002134 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002135 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002136 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002137 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002138 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002139 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002140 }
Chris Lattner516b9622006-09-14 20:50:57 +00002141 }
2142 }
2143
2144 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2145 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002146 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2147 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002148 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002149 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002150 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002151 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002152 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002153 else
2154 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002155 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002156 }
2157 }
2158
Dan Gohman74feef22008-10-17 01:23:35 +00002159 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002160 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2161 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002162 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2163 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002164 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2165 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002166 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2167 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2169 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002170 if (RExtOp0.getOpcode() == ISD::SUB &&
2171 RExtOp0.getOperand(1) == LExtOp0) {
2172 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002173 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002174 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002175 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002176 if (ConstantSDNode *SUBC =
2177 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002178 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002179 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2180 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002181 }
2182 }
2183 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2184 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002185 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002186 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002187 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002188 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002189 if (ConstantSDNode *SUBC =
2190 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002191 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002192 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2193 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002194 }
2195 }
Chris Lattner516b9622006-09-14 20:50:57 +00002196 }
2197 }
2198
2199 return 0;
2200}
2201
2202
Dan Gohman475871a2008-07-27 21:46:04 +00002203SDValue DAGCombiner::visitXOR(SDNode *N) {
2204 SDValue N0 = N->getOperand(0);
2205 SDValue N1 = N->getOperand(1);
2206 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002207 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2208 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002210
Dan Gohman7f321562007-06-25 16:23:39 +00002211 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002213 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002214 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002215 }
Dan Gohman7f321562007-06-25 16:23:39 +00002216
Evan Cheng26471c42008-03-25 20:08:07 +00002217 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2218 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2219 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002220 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002221 if (N0.getOpcode() == ISD::UNDEF)
2222 return N0;
2223 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002224 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002225 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002226 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002227 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002228 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002229 if (N0C && !N1C)
2230 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002232 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002233 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002234 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002237 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002238
Nate Begeman1d4d4142005-09-01 00:19:25 +00002239 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002240 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002241 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002242 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2243 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002244
Duncan Sands25cf2272008-11-24 14:53:14 +00002245 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002246 switch (N0.getOpcode()) {
2247 default:
2248 assert(0 && "Unhandled SetCC Equivalent!");
2249 abort();
2250 case ISD::SETCC:
2251 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2252 case ISD::SELECT_CC:
2253 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2254 N0.getOperand(3), NotCC);
2255 }
2256 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002257 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002258
Chris Lattner61c5ff42007-09-10 21:39:07 +00002259 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002260 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002261 N0.getNode()->hasOneUse() &&
2262 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002263 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002264 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002265 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002266 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002267 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2268 }
2269
Nate Begeman99801192005-09-07 23:25:52 +00002270 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002271 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002272 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002273 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002274 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2275 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002276 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2277 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002278 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002279 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002280 }
2281 }
Nate Begeman99801192005-09-07 23:25:52 +00002282 // fold !(x or y) -> (!x and !y) iff x or y are constants
2283 if (N1C && N1C->isAllOnesValue() &&
2284 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002285 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002286 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2287 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002288 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2289 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002290 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002291 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002292 }
2293 }
Nate Begeman223df222005-09-08 20:18:10 +00002294 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2295 if (N1C && N0.getOpcode() == ISD::XOR) {
2296 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2297 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2298 if (N00C)
2299 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002300 DAG.getConstant(N1C->getAPIntValue()^
2301 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002302 if (N01C)
2303 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002304 DAG.getConstant(N1C->getAPIntValue()^
2305 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002306 }
2307 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002308 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002310 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002311 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002312 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002313 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2314 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002315 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002316 }
2317 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002318
2319 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2320 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002321 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002322 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002323 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002324
Chris Lattner3e104b12006-04-08 04:15:24 +00002325 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002326 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002327 SimplifyDemandedBits(SDValue(N, 0)))
2328 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002329
Dan Gohman475871a2008-07-27 21:46:04 +00002330 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002331}
2332
Chris Lattnere70da202007-12-06 07:33:36 +00002333/// visitShiftByConstant - Handle transforms common to the three shifts, when
2334/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002335SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002336 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002337 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002338
2339 // We want to pull some binops through shifts, so that we have (and (shift))
2340 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2341 // thing happens with address calculations, so it's important to canonicalize
2342 // it.
2343 bool HighBitSet = false; // Can we transform this if the high bit is set?
2344
2345 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002346 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002347 case ISD::OR:
2348 case ISD::XOR:
2349 HighBitSet = false; // We can only transform sra if the high bit is clear.
2350 break;
2351 case ISD::AND:
2352 HighBitSet = true; // We can only transform sra if the high bit is set.
2353 break;
2354 case ISD::ADD:
2355 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002356 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002357 HighBitSet = false; // We can only transform sra if the high bit is clear.
2358 break;
2359 }
2360
2361 // We require the RHS of the binop to be a constant as well.
2362 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002363 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002364
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002365
2366 // FIXME: disable this for unless the input to the binop is a shift by a
2367 // constant. If it is not a shift, it pessimizes some common cases like:
2368 //
2369 //void foo(int *X, int i) { X[i & 1235] = 1; }
2370 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002371 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002372 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2373 BinOpLHSVal->getOpcode() != ISD::SRA &&
2374 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2375 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002376 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002377
Duncan Sands83ec4b62008-06-06 12:08:01 +00002378 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002379
2380 // If this is a signed shift right, and the high bit is modified
2381 // by the logical operation, do not perform the transformation.
2382 // The highBitSet boolean indicates the value of the high bit of
2383 // the constant which would cause it to be modified for this
2384 // operation.
2385 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002386 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2387 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002388 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002389 }
2390
2391 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002392 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002393 LHS->getOperand(1), N->getOperand(1));
2394
2395 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002397 N->getOperand(1));
2398
2399 // Create the new binop.
2400 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2401}
2402
2403
Dan Gohman475871a2008-07-27 21:46:04 +00002404SDValue DAGCombiner::visitSHL(SDNode *N) {
2405 SDValue N0 = N->getOperand(0);
2406 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002407 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2408 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002409 MVT VT = N0.getValueType();
2410 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002411
2412 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002413 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002414 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002415 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002416 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002417 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002418 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002419 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002420 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002422 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002423 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002424 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002425 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002426 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002427 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002428 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2429 // iff (trunc c) == c
2430 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002431 N1.getOperand(0).getOpcode() == ISD::AND &&
2432 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002433 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002434 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002435 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002436 SDValue N100 = N1.getOperand(0).getOperand(0);
2437 return DAG.getNode(ISD::SHL, VT, N0,
2438 DAG.getNode(ISD::AND, TruncVT,
2439 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2440 DAG.getConstant(N101C->getZExtValue(),
2441 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002442 }
2443 }
2444
Dan Gohman475871a2008-07-27 21:46:04 +00002445 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2446 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002447 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002448 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002450 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2451 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002453 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002455 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002456 }
2457 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2458 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002459 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002461 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2462 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002463 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 DAG.getConstant(~0ULL << c1, VT));
2465 if (c2 > c1)
2466 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002467 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002469 return DAG.getNode(ISD::SRL, VT, Mask,
2470 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002471 }
2472 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002473 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002474 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002475 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002476
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002477 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002478}
2479
Dan Gohman475871a2008-07-27 21:46:04 +00002480SDValue DAGCombiner::visitSRA(SDNode *N) {
2481 SDValue N0 = N->getOperand(0);
2482 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002483 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2484 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002485 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002486
2487 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002488 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002489 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002490 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002491 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002492 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002494 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002495 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002496 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002497 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002498 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002499 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002500 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002501 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002502 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2503 // sext_inreg.
2504 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002505 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002506 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002507 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002508 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2509 DAG.getValueType(EVT));
2510 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002511
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002512 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2513 if (N1C && N0.getOpcode() == ISD::SRA) {
2514 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002515 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002516 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002517 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2518 DAG.getConstant(Sum, N1C->getValueType(0)));
2519 }
2520 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002521
2522 // fold sra (shl X, m), result_size - n
2523 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002524 // result_size - n != m.
2525 // If truncate is free for the target sext(shl) is likely to result in better
2526 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002527 if (N0.getOpcode() == ISD::SHL) {
2528 // Get the two constanst of the shifts, CN0 = m, CN = n.
2529 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2530 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002531 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002532 unsigned VTValSize = VT.getSizeInBits();
2533 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002534 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002535 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002536 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002537
Christopher Lambb9b04282008-03-20 04:31:39 +00002538 // If the shift is not a no-op (in which case this should be just a sign
2539 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002540 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002541 // perform the transform.
Duncan Sands25cf2272008-11-24 14:53:14 +00002542 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002543 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2544 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002545 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002546
Dan Gohman475871a2008-07-27 21:46:04 +00002547 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2548 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2549 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002550 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002551 }
2552 }
2553 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002554
Evan Chengeb9f8922008-08-30 02:03:58 +00002555 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2556 // iff (trunc c) == c
2557 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002558 N1.getOperand(0).getOpcode() == ISD::AND &&
2559 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002560 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002561 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002562 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002563 SDValue N100 = N1.getOperand(0).getOperand(0);
2564 return DAG.getNode(ISD::SRA, VT, N0,
2565 DAG.getNode(ISD::AND, TruncVT,
2566 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2567 DAG.getConstant(N101C->getZExtValue(),
2568 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002569 }
2570 }
2571
Chris Lattnera8504462006-05-08 20:51:54 +00002572 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002573 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2574 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002575
2576
Nate Begeman1d4d4142005-09-01 00:19:25 +00002577 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002578 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002579 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002580
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002581 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002582}
2583
Dan Gohman475871a2008-07-27 21:46:04 +00002584SDValue DAGCombiner::visitSRL(SDNode *N) {
2585 SDValue N0 = N->getOperand(0);
2586 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002587 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2588 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002589 MVT VT = N0.getValueType();
2590 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002591
2592 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002593 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002594 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002595 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002596 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002597 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002598 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002599 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002600 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002601 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002602 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002603 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002604 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002605 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002606 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002607 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002608
Nate Begeman1d4d4142005-09-01 00:19:25 +00002609 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002610 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002611 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002612 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2613 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002614 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002615 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002616 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002617 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002618 }
Chris Lattner350bec02006-04-02 06:11:11 +00002619
Chris Lattner06afe072006-05-05 22:53:17 +00002620 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2621 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2622 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002624 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002625 return DAG.getNode(ISD::UNDEF, VT);
2626
Dan Gohman475871a2008-07-27 21:46:04 +00002627 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002628 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002629 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2630 }
2631
Chris Lattner3657ffe2006-10-12 20:23:19 +00002632 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2633 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002634 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002635 if (N0.getOpcode() == ISD::SRA)
2636 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2637 }
2638
Chris Lattner350bec02006-04-02 06:11:11 +00002639 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2640 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002641 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002642 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002643 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002644 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002645
2646 // If any of the input bits are KnownOne, then the input couldn't be all
2647 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002648 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002649
2650 // If all of the bits input the to ctlz node are known to be zero, then
2651 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002652 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002653 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2654
2655 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2656 if ((UnknownBits & (UnknownBits-1)) == 0) {
2657 // Okay, we know that only that the single bit specified by UnknownBits
2658 // could be set on input to the CTLZ node. If this bit is set, the SRL
2659 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2660 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002661 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002662 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002663 if (ShAmt) {
2664 Op = DAG.getNode(ISD::SRL, VT, Op,
2665 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002666 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002667 }
2668 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2669 }
2670 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002671
2672 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2673 // iff (trunc c) == c
2674 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002675 N1.getOperand(0).getOpcode() == ISD::AND &&
2676 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002677 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002678 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002679 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002680 SDValue N100 = N1.getOperand(0).getOperand(0);
2681 return DAG.getNode(ISD::SRL, VT, N0,
2682 DAG.getNode(ISD::AND, TruncVT,
2683 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2684 DAG.getConstant(N101C->getZExtValue(),
2685 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002686 }
2687 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002688
2689 // fold operands of srl based on knowledge that the low bits are not
2690 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002691 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2692 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002693
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002694 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002695}
2696
Dan Gohman475871a2008-07-27 21:46:04 +00002697SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2698 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002699 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002700
2701 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002702 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002703 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002704 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002705}
2706
Dan Gohman475871a2008-07-27 21:46:04 +00002707SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2708 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002709 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002710
2711 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002712 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002713 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002714 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002715}
2716
Dan Gohman475871a2008-07-27 21:46:04 +00002717SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2718 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002719 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002720
2721 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002722 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002723 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002724 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002725}
2726
Dan Gohman475871a2008-07-27 21:46:04 +00002727SDValue DAGCombiner::visitSELECT(SDNode *N) {
2728 SDValue N0 = N->getOperand(0);
2729 SDValue N1 = N->getOperand(1);
2730 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002731 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2732 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2733 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002734 MVT VT = N->getValueType(0);
2735 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002736
Nate Begeman452d7be2005-09-16 00:54:12 +00002737 // fold select C, X, X -> X
2738 if (N1 == N2)
2739 return N1;
2740 // fold select true, X, Y -> X
2741 if (N0C && !N0C->isNullValue())
2742 return N1;
2743 // fold select false, X, Y -> Y
2744 if (N0C && N0C->isNullValue())
2745 return N2;
2746 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002747 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002748 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002749 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002750 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002751 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002752 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002753 if (VT == VT0)
2754 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002755 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002756 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002757 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2758 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2759 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002760 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002761 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bob Wilson4c245462009-01-22 17:39:32 +00002762 SDValue NOTNode = DAG.getNOT(N0, VT);
2763 AddToWorkList(NOTNode.getNode());
2764 return DAG.getNode(ISD::AND, VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002765 }
2766 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002767 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bob Wilson4c245462009-01-22 17:39:32 +00002768 SDValue NOTNode = DAG.getNOT(N0, VT);
2769 AddToWorkList(NOTNode.getNode());
2770 return DAG.getNode(ISD::OR, VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00002771 }
2772 // fold select C, X, 0 -> C & X
2773 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002775 return DAG.getNode(ISD::AND, VT, N0, N1);
2776 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002777 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002778 return DAG.getNode(ISD::OR, VT, N0, N2);
2779 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002780 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002781 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002782
Chris Lattner40c62d52005-10-18 06:04:22 +00002783 // If we can fold this based on the true/false value, do so.
2784 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002785 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002786
Nate Begeman44728a72005-09-19 22:34:01 +00002787 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002788 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002789 // FIXME:
2790 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2791 // having to say they don't support SELECT_CC on every type the DAG knows
2792 // about, since there is no way to mark an opcode illegal at all value types
2793 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2794 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2795 N1, N2, N0.getOperand(2));
2796 else
2797 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002798 }
Dan Gohman475871a2008-07-27 21:46:04 +00002799 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002800}
2801
Dan Gohman475871a2008-07-27 21:46:04 +00002802SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2803 SDValue N0 = N->getOperand(0);
2804 SDValue N1 = N->getOperand(1);
2805 SDValue N2 = N->getOperand(2);
2806 SDValue N3 = N->getOperand(3);
2807 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002808 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2809
Nate Begeman44728a72005-09-19 22:34:01 +00002810 // fold select_cc lhs, rhs, x, x, cc -> x
2811 if (N2 == N3)
2812 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002813
Chris Lattner5f42a242006-09-20 06:19:26 +00002814 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002815 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2816 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002817 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002818
Gabor Greifba36cb52008-08-28 21:40:38 +00002819 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002820 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002821 return N2; // cond always true -> true val
2822 else
2823 return N3; // cond always false -> false val
2824 }
2825
2826 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002827 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002828 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2829 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2830 SCC.getOperand(2));
2831
Chris Lattner40c62d52005-10-18 06:04:22 +00002832 // If we can fold this based on the true/false value, do so.
2833 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002834 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002835
Nate Begeman44728a72005-09-19 22:34:01 +00002836 // fold select_cc into other things, such as min/max/abs
2837 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002838}
2839
Dan Gohman475871a2008-07-27 21:46:04 +00002840SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002841 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2842 cast<CondCodeSDNode>(N->getOperand(2))->get());
2843}
2844
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002845// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2846// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2847// transformation. Returns true if extension are possible and the above
2848// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002849static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002850 unsigned ExtOpc,
2851 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002852 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002853 bool HasCopyToRegUses = false;
2854 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002855 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2856 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002857 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002858 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002859 if (User == N)
2860 continue;
2861 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2862 if (User->getOpcode() == ISD::SETCC) {
2863 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2864 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2865 // Sign bits will be lost after a zext.
2866 return false;
2867 bool Add = false;
2868 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002869 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002870 if (UseOp == N0)
2871 continue;
2872 if (!isa<ConstantSDNode>(UseOp))
2873 return false;
2874 Add = true;
2875 }
2876 if (Add)
2877 ExtendNodes.push_back(User);
2878 } else {
2879 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002880 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002881 if (UseOp == N0) {
2882 // If truncate from extended type to original load type is free
2883 // on this target, then it's ok to extend a CopyToReg.
2884 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2885 HasCopyToRegUses = true;
2886 else
2887 return false;
2888 }
2889 }
2890 }
2891 }
2892
2893 if (HasCopyToRegUses) {
2894 bool BothLiveOut = false;
2895 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2896 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002897 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002898 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002899 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002900 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002901 BothLiveOut = true;
2902 break;
2903 }
2904 }
2905 }
2906 if (BothLiveOut)
2907 // Both unextended and extended values are live out. There had better be
2908 // good a reason for the transformation.
2909 return ExtendNodes.size();
2910 }
2911 return true;
2912}
2913
Dan Gohman475871a2008-07-27 21:46:04 +00002914SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2915 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002916 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002917
Nate Begeman1d4d4142005-09-01 00:19:25 +00002918 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002919 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002920 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002921
Nate Begeman1d4d4142005-09-01 00:19:25 +00002922 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002923 // fold (sext (aext x)) -> (sext x)
2924 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002925 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002926
Chris Lattner22558872007-02-26 03:13:59 +00002927 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002928 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2929 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002930 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2931 if (NarrowLoad.getNode()) {
2932 if (NarrowLoad.getNode() != N0.getNode())
2933 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002934 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2935 }
Evan Chengc88138f2007-03-22 01:54:19 +00002936
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002937 // See if the value being truncated is already sign extended. If so, just
2938 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002939 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002940 unsigned OpBits = Op.getValueType().getSizeInBits();
2941 unsigned MidBits = N0.getValueType().getSizeInBits();
2942 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002943 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002944
2945 if (OpBits == DestBits) {
2946 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2947 // bits, it is already ready.
2948 if (NumSignBits > DestBits-MidBits)
2949 return Op;
2950 } else if (OpBits < DestBits) {
2951 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2952 // bits, just sext from i32.
2953 if (NumSignBits > OpBits-MidBits)
2954 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2955 } else {
2956 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2957 // bits, just truncate to i32.
2958 if (NumSignBits > OpBits-MidBits)
2959 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002960 }
Chris Lattner22558872007-02-26 03:13:59 +00002961
2962 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00002963 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2964 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002965 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002966 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002967 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002968 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2969 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2970 DAG.getValueType(N0.getValueType()));
2971 }
Chris Lattner6007b842006-09-21 06:00:20 +00002972 }
Chris Lattner310b5782006-05-06 23:06:26 +00002973
Evan Cheng110dec22005-12-14 02:19:23 +00002974 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002975 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002976 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002977 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002978 bool DoXform = true;
2979 SmallVector<SDNode*, 4> SetCCs;
2980 if (!N0.hasOneUse())
2981 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2982 if (DoXform) {
2983 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002984 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002985 LN0->getBasePtr(), LN0->getSrcValue(),
2986 LN0->getSrcValueOffset(),
2987 N0.getValueType(),
2988 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002989 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002991 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002992 // Extend SetCC uses if necessary.
2993 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2994 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002995 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002996 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002997 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002998 if (SOp == Trunc)
2999 Ops.push_back(ExtLoad);
3000 else
3001 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
3002 }
3003 Ops.push_back(SetCC->getOperand(2));
3004 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3005 &Ops[0], Ops.size()));
3006 }
Dan Gohman475871a2008-07-27 21:46:04 +00003007 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003008 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003009 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003010
3011 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3012 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003013 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3014 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003015 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003016 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003017 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003018 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003020 LN0->getBasePtr(), LN0->getSrcValue(),
3021 LN0->getSrcValueOffset(), EVT,
3022 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003023 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003024 CombineTo(N0.getNode(),
3025 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003026 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003027 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003028 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003029 }
3030
Chris Lattner20a35c32007-04-11 05:32:27 +00003031 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3032 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003033 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003034 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3035 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3036 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003037 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003038 }
3039
Dan Gohman8f0ad582008-04-28 16:58:24 +00003040 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003041 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003042 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00003043 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3044
Dan Gohman475871a2008-07-27 21:46:04 +00003045 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003046}
3047
Dan Gohman475871a2008-07-27 21:46:04 +00003048SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3049 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003050 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003051
Nate Begeman1d4d4142005-09-01 00:19:25 +00003052 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003053 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003054 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003055 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003056 // fold (zext (aext x)) -> (zext x)
3057 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003058 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003059
Evan Chengc88138f2007-03-22 01:54:19 +00003060 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3061 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003062 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003063 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3064 if (NarrowLoad.getNode()) {
3065 if (NarrowLoad.getNode() != N0.getNode())
3066 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003067 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3068 }
Evan Chengc88138f2007-03-22 01:54:19 +00003069 }
3070
Chris Lattner6007b842006-09-21 06:00:20 +00003071 // fold (zext (truncate x)) -> (and x, mask)
3072 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003073 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003074 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003075 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003076 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003077 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003078 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3079 }
3080 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3081 }
3082
Chris Lattner111c2282006-09-21 06:14:31 +00003083 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3084 if (N0.getOpcode() == ISD::AND &&
3085 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3086 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003087 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003088 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003089 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003090 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003091 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3092 }
Dan Gohman220a8232008-03-03 23:51:38 +00003093 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003094 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003095 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3096 }
3097
Evan Cheng110dec22005-12-14 02:19:23 +00003098 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003099 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003100 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003101 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003102 bool DoXform = true;
3103 SmallVector<SDNode*, 4> SetCCs;
3104 if (!N0.hasOneUse())
3105 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3106 if (DoXform) {
3107 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003109 LN0->getBasePtr(), LN0->getSrcValue(),
3110 LN0->getSrcValueOffset(),
3111 N0.getValueType(),
3112 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003113 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003114 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003115 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003116 // Extend SetCC uses if necessary.
3117 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3118 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003120 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003121 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003122 if (SOp == Trunc)
3123 Ops.push_back(ExtLoad);
3124 else
Evan Chengde1631b2007-10-30 20:11:21 +00003125 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003126 }
3127 Ops.push_back(SetCC->getOperand(2));
3128 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3129 &Ops[0], Ops.size()));
3130 }
Dan Gohman475871a2008-07-27 21:46:04 +00003131 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003132 }
Evan Cheng110dec22005-12-14 02:19:23 +00003133 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003134
3135 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3136 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003137 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3138 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003139 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003140 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003141 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003142 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003143 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003144 LN0->getBasePtr(), LN0->getSrcValue(),
3145 LN0->getSrcValueOffset(), EVT,
3146 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003147 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003148 CombineTo(N0.getNode(),
3149 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003150 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003151 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003152 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003153 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003154
3155 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3156 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003157 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003158 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3159 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003160 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003161 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003162 }
3163
Dan Gohman475871a2008-07-27 21:46:04 +00003164 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003165}
3166
Dan Gohman475871a2008-07-27 21:46:04 +00003167SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3168 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003169 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003170
3171 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003172 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003173 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3174 // fold (aext (aext x)) -> (aext x)
3175 // fold (aext (zext x)) -> (zext x)
3176 // fold (aext (sext x)) -> (sext x)
3177 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3178 N0.getOpcode() == ISD::ZERO_EXTEND ||
3179 N0.getOpcode() == ISD::SIGN_EXTEND)
3180 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3181
Evan Chengc88138f2007-03-22 01:54:19 +00003182 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3183 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3184 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003185 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3186 if (NarrowLoad.getNode()) {
3187 if (NarrowLoad.getNode() != N0.getNode())
3188 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003189 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3190 }
Evan Chengc88138f2007-03-22 01:54:19 +00003191 }
3192
Chris Lattner84750582006-09-20 06:29:17 +00003193 // fold (aext (truncate x))
3194 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003195 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003196 if (TruncOp.getValueType() == VT)
3197 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003198 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003199 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3200 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3201 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003202
3203 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3204 if (N0.getOpcode() == ISD::AND &&
3205 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3206 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003207 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003208 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003209 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003210 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003211 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3212 }
Dan Gohman220a8232008-03-03 23:51:38 +00003213 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003214 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003215 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3216 }
3217
Chris Lattner5ffc0662006-05-05 05:58:59 +00003218 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003219 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003220 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003221 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003222 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003223 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003224 LN0->getBasePtr(), LN0->getSrcValue(),
3225 LN0->getSrcValueOffset(),
3226 N0.getValueType(),
3227 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003228 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003229 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003230 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3231 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003232 // If any node needs the original loaded value, recompute it.
3233 if (!LN0->use_empty())
3234 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3235 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003236 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003237 }
3238
3239 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3240 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3241 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003242 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003243 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003244 N0.hasOneUse()) {
3245 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003246 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003247 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003248 LN0->getChain(), LN0->getBasePtr(),
3249 LN0->getSrcValue(),
3250 LN0->getSrcValueOffset(), EVT,
3251 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003252 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003253 CombineTo(N0.getNode(),
3254 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003255 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003256 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003257 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003258
3259 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3260 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003261 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003262 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3263 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003264 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003265 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003266 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003267 }
3268
Dan Gohman475871a2008-07-27 21:46:04 +00003269 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003270}
3271
Chris Lattner2b4c2792007-10-13 06:35:54 +00003272/// GetDemandedBits - See if the specified operand can be simplified with the
3273/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003274/// simpler operand, otherwise return a null SDValue.
3275SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003276 switch (V.getOpcode()) {
3277 default: break;
3278 case ISD::OR:
3279 case ISD::XOR:
3280 // If the LHS or RHS don't contribute bits to the or, drop them.
3281 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3282 return V.getOperand(1);
3283 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3284 return V.getOperand(0);
3285 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003286 case ISD::SRL:
3287 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003288 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003289 break;
3290 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3291 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003292 unsigned Amt = RHSC->getZExtValue();
Dan Gohmancc91d632009-01-03 19:22:06 +00003293 // Watch out for shift count overflow though.
3294 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003295 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003296 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003297 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003298 return DAG.getNode(ISD::SRL, V.getValueType(),
3299 SimplifyLHS, V.getOperand(1));
3300 }
3301 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003302 }
Dan Gohman475871a2008-07-27 21:46:04 +00003303 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003304}
3305
Evan Chengc88138f2007-03-22 01:54:19 +00003306/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3307/// bits and then truncated to a narrower type and where N is a multiple
3308/// of number of bits of the narrower type, transform it to a narrower load
3309/// from address + N / num of bits of new type. If the result is to be
3310/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003311SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003312 unsigned Opc = N->getOpcode();
3313 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003314 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003315 MVT VT = N->getValueType(0);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003316 MVT EVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00003317
Dan Gohman7f8613e2008-08-14 20:04:46 +00003318 // This transformation isn't valid for vector loads.
3319 if (VT.isVector())
3320 return SDValue();
3321
Evan Chenge177e302007-03-23 22:13:36 +00003322 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3323 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003324 if (Opc == ISD::SIGN_EXTEND_INREG) {
3325 ExtType = ISD::SEXTLOAD;
3326 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003327 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003328 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003329 }
3330
Duncan Sands83ec4b62008-06-06 12:08:01 +00003331 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003332 unsigned ShAmt = 0;
3333 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3334 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003335 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003336 // Is the shift amount a multiple of size of VT?
3337 if ((ShAmt & (EVTBits-1)) == 0) {
3338 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003339 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003340 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003341 }
3342 }
3343 }
3344
Duncan Sandsad205a72008-06-16 08:14:38 +00003345 // Do not generate loads of non-round integer types since these can
3346 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohmanf5add582009-01-20 01:06:45 +00003347 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && EVT.isRound() &&
Dan Gohman75dcf082008-07-31 00:50:31 +00003348 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003349 // Do not change the width of a volatile load.
3350 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003351 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003352 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003353 // For big endian targets, we need to adjust the offset to the pointer to
3354 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003355 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003356 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003357 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003358 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3359 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003360 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003361 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003362 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003363 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003364 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003365 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003366 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003367 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003368 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003369 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003370 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3371 EVT, LN0->isVolatile(), NewAlign);
Dan Gohman764fd0c2009-01-21 15:17:51 +00003372 // Replace the old load's chain with the new load's chain.
3373 WorkListRemover DeadNodes(*this);
3374 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3375 &DeadNodes);
3376 // Return the new loaded value.
3377 return Load;
Evan Chengc88138f2007-03-22 01:54:19 +00003378 }
3379
Dan Gohman475871a2008-07-27 21:46:04 +00003380 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003381}
3382
Chris Lattner5ffc0662006-05-05 05:58:59 +00003383
Dan Gohman475871a2008-07-27 21:46:04 +00003384SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3385 SDValue N0 = N->getOperand(0);
3386 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003387 MVT VT = N->getValueType(0);
3388 MVT EVT = cast<VTSDNode>(N1)->getVT();
3389 unsigned VTBits = VT.getSizeInBits();
3390 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003391
Nate Begeman1d4d4142005-09-01 00:19:25 +00003392 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003393 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003394 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003395
Chris Lattner541a24f2006-05-06 22:43:44 +00003396 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003397 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003398 return N0;
3399
Nate Begeman646d7e22005-09-02 21:18:40 +00003400 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3401 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003402 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003403 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003404 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003405
Dan Gohman75dcf082008-07-31 00:50:31 +00003406 // fold (sext_in_reg (sext x)) -> (sext x)
3407 // fold (sext_in_reg (aext x)) -> (sext x)
3408 // if x is small enough.
3409 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3410 SDValue N00 = N0.getOperand(0);
3411 if (N00.getValueType().getSizeInBits() < EVTBits)
3412 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3413 }
3414
Chris Lattner95a5e052007-04-17 19:03:21 +00003415 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003416 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003417 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003418
Chris Lattner95a5e052007-04-17 19:03:21 +00003419 // fold operands of sext_in_reg based on knowledge that the top bits are not
3420 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003421 if (SimplifyDemandedBits(SDValue(N, 0)))
3422 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003423
Evan Chengc88138f2007-03-22 01:54:19 +00003424 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3425 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003426 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003427 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003428 return NarrowLoad;
3429
Chris Lattner4b37e872006-05-08 21:18:59 +00003430 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3431 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3432 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3433 if (N0.getOpcode() == ISD::SRL) {
3434 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003435 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003436 // We can turn this into an SRA iff the input to the SRL is already sign
3437 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003438 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003439 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003440 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3441 }
3442 }
Evan Chengc88138f2007-03-22 01:54:19 +00003443
Nate Begemanded49632005-10-13 03:11:28 +00003444 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003445 if (ISD::isEXTLoad(N0.getNode()) &&
3446 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003447 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003448 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003449 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003450 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003452 LN0->getBasePtr(), LN0->getSrcValue(),
3453 LN0->getSrcValueOffset(), EVT,
3454 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003455 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003456 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003457 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003458 }
3459 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003460 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003461 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003462 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003463 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003464 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003465 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003466 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003467 LN0->getBasePtr(), LN0->getSrcValue(),
3468 LN0->getSrcValueOffset(), EVT,
3469 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003470 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003471 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003472 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003473 }
Dan Gohman475871a2008-07-27 21:46:04 +00003474 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003475}
3476
Dan Gohman475871a2008-07-27 21:46:04 +00003477SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3478 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003479 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003480
3481 // noop truncate
3482 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003483 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003484 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003485 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003486 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003487 // fold (truncate (truncate x)) -> (truncate x)
3488 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003489 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003490 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003491 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3492 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003493 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003494 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003495 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003496 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003497 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003498 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003499 else
3500 // if the source and dest are the same type, we can drop both the extend
3501 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003502 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003503 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003504
Chris Lattner2b4c2792007-10-13 06:35:54 +00003505 // See if we can simplify the input to this truncate through knowledge that
3506 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3507 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003508 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003509 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003510 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003511 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003512 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3513
Nate Begeman3df4d522005-10-12 20:40:40 +00003514 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003515 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003516 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003517}
3518
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003519static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003520 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003521 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003522 return Elt.getNode();
3523 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003524}
3525
3526/// CombineConsecutiveLoads - build_pair (load, load) -> load
3527/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003528SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003529 assert(N->getOpcode() == ISD::BUILD_PAIR);
3530
3531 SDNode *LD1 = getBuildPairElt(N, 0);
3532 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003533 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003534 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003535 SDNode *LD2 = getBuildPairElt(N, 1);
3536 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3537 if (ISD::isNON_EXTLoad(LD2) &&
3538 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003539 // If both are volatile this would reduce the number of volatile loads.
3540 // If one is volatile it might be ok, but play conservative and bail out.
3541 !cast<LoadSDNode>(LD1)->isVolatile() &&
3542 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003543 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003544 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3545 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003546 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003547 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003548 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003549 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003550 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3551 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003552 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003553 }
Dan Gohman475871a2008-07-27 21:46:04 +00003554 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003555}
3556
Dan Gohman475871a2008-07-27 21:46:04 +00003557SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3558 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003559 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003560
Dan Gohman7f321562007-06-25 16:23:39 +00003561 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3562 // Only do this before legalize, since afterward the target may be depending
3563 // on the bitconvert.
3564 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003565 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003566 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003567 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003568 bool isSimple = true;
3569 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3570 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3571 N0.getOperand(i).getOpcode() != ISD::Constant &&
3572 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3573 isSimple = false;
3574 break;
3575 }
3576
Duncan Sands83ec4b62008-06-06 12:08:01 +00003577 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3578 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003579 "Element type of vector ValueType must not be vector!");
3580 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003581 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003582 }
3583 }
3584
Dan Gohman3dd168d2008-09-05 01:58:21 +00003585 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003586 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003588 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003589 }
3590
Chris Lattnerc8547d82005-12-23 05:37:50 +00003591 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3592 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003593
Chris Lattner57104102005-12-23 05:44:41 +00003594 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003595 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003596 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003597 // Do not change the width of a volatile load.
3598 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003599 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003600 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003601 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003602 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003603 unsigned OrigAlign = LN0->getAlignment();
3604 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003605 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003606 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3607 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003608 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003609 CombineTo(N0.getNode(),
3610 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003611 Load.getValue(1));
3612 return Load;
3613 }
Chris Lattner57104102005-12-23 05:44:41 +00003614 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003615
Chris Lattner3bd39d42008-01-27 17:42:27 +00003616 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3617 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3618 // This often reduces constant pool loads.
3619 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003620 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003621 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003622 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003623
Duncan Sands83ec4b62008-06-06 12:08:01 +00003624 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003625 if (N0.getOpcode() == ISD::FNEG)
3626 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3627 assert(N0.getOpcode() == ISD::FABS);
3628 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3629 }
3630
3631 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3632 // Note that we don't handle copysign(x,cst) because this can always be folded
3633 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003634 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003635 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003636 VT.isInteger() && !VT.isVector()) {
3637 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sands25cf2272008-11-24 14:53:14 +00003638 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3639 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3640 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3641 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003642
Duncan Sands25cf2272008-11-24 14:53:14 +00003643 // If X has a different width than the result/lhs, sext it or truncate it.
3644 unsigned VTWidth = VT.getSizeInBits();
3645 if (OrigXWidth < VTWidth) {
3646 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3647 AddToWorkList(X.getNode());
3648 } else if (OrigXWidth > VTWidth) {
3649 // To get the sign bit in the right place, we have to shift it right
3650 // before truncating.
3651 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3652 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3653 AddToWorkList(X.getNode());
3654 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3655 AddToWorkList(X.getNode());
3656 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003657
Duncan Sands25cf2272008-11-24 14:53:14 +00003658 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3659 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3660 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003661
Duncan Sands25cf2272008-11-24 14:53:14 +00003662 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3663 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3664 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003665
Duncan Sands25cf2272008-11-24 14:53:14 +00003666 return DAG.getNode(ISD::OR, VT, X, Cst);
3667 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003668 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003669
3670 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3671 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003672 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3673 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003674 return CombineLD;
3675 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003676
Dan Gohman475871a2008-07-27 21:46:04 +00003677 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003678}
3679
Dan Gohman475871a2008-07-27 21:46:04 +00003680SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003681 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003682 return CombineConsecutiveLoads(N, VT);
3683}
3684
Dan Gohman7f321562007-06-25 16:23:39 +00003685/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003686/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3687/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003688SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003689ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3690 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003691
3692 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003693 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003694
Duncan Sands83ec4b62008-06-06 12:08:01 +00003695 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3696 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003697
3698 // If this is a conversion of N elements of one type to N elements of another
3699 // type, convert each element. This handles FP<->INT cases.
3700 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003701 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003702 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003703 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003704 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003705 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003706 MVT VT = MVT::getVectorVT(DstEltVT,
3707 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003708 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003709 }
3710
3711 // Otherwise, we're growing or shrinking the elements. To avoid having to
3712 // handle annoying details of growing/shrinking FP values, we convert them to
3713 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003714 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003715 // Convert the input float vector to a int vector where the elements are the
3716 // same sizes.
3717 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003718 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003719 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003720 SrcEltVT = IntVT;
3721 }
3722
3723 // Now we know the input is an integer vector. If the output is a FP type,
3724 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003725 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003726 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003727 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003728 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003729
3730 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003731 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003732 }
3733
3734 // Okay, we know the src/dst types are both integers of differing types.
3735 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003736 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003737 if (SrcBitSize < DstBitSize) {
3738 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3739
Dan Gohman475871a2008-07-27 21:46:04 +00003740 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003741 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003742 i += NumInputsPerOutput) {
3743 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003744 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003745 bool EltIsUndef = true;
3746 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3747 // Shift the previously computed bits over.
3748 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003749 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003750 if (Op.getOpcode() == ISD::UNDEF) continue;
3751 EltIsUndef = false;
3752
Dan Gohman220a8232008-03-03 23:51:38 +00003753 NewBits |=
3754 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003755 }
3756
3757 if (EltIsUndef)
3758 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3759 else
3760 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3761 }
3762
Duncan Sands83ec4b62008-06-06 12:08:01 +00003763 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003764 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003765 }
3766
3767 // Finally, this must be the case where we are shrinking elements: each input
3768 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003769 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003770 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003771 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003772 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003773 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003774 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3775 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3776 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3777 continue;
3778 }
Dan Gohman220a8232008-03-03 23:51:38 +00003779 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003780 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003781 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003782 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003783 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003784 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3785 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003786 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003787 }
3788
3789 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003790 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003791 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3792 }
Dan Gohman7f321562007-06-25 16:23:39 +00003793 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003794}
3795
3796
3797
Dan Gohman475871a2008-07-27 21:46:04 +00003798SDValue DAGCombiner::visitFADD(SDNode *N) {
3799 SDValue N0 = N->getOperand(0);
3800 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003801 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3802 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003804
Dan Gohman7f321562007-06-25 16:23:39 +00003805 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003806 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003808 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003809 }
Dan Gohman7f321562007-06-25 16:23:39 +00003810
Nate Begemana0e221d2005-10-18 00:28:13 +00003811 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003812 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003813 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003814 // canonicalize constant to RHS
3815 if (N0CFP && !N1CFP)
3816 return DAG.getNode(ISD::FADD, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003817 // fold (A + 0) -> A
3818 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3819 return N0;
Chris Lattner01b3d732005-09-28 22:28:18 +00003820 // fold (A + (-B)) -> A-B
Duncan Sands25cf2272008-11-24 14:53:14 +00003821 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003822 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003823 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner01b3d732005-09-28 22:28:18 +00003824 // fold ((-A) + B) -> B-A
Duncan Sands25cf2272008-11-24 14:53:14 +00003825 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003826 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00003827 GetNegatedExpression(N0, DAG, LegalOperations));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003828
3829 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3830 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003831 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003832 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3833 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3834
Dan Gohman475871a2008-07-27 21:46:04 +00003835 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003836}
3837
Dan Gohman475871a2008-07-27 21:46:04 +00003838SDValue DAGCombiner::visitFSUB(SDNode *N) {
3839 SDValue N0 = N->getOperand(0);
3840 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003841 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3842 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003843 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003844
Dan Gohman7f321562007-06-25 16:23:39 +00003845 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003846 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003847 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003848 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003849 }
Dan Gohman7f321562007-06-25 16:23:39 +00003850
Nate Begemana0e221d2005-10-18 00:28:13 +00003851 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003852 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003853 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003854 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003855 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00003856 if (isNegatibleForFree(N1, LegalOperations))
3857 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00003858 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3859 return DAG.getNode(ISD::FNEG, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003860 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003861 // fold (A-(-B)) -> A+B
Duncan Sands25cf2272008-11-24 14:53:14 +00003862 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattner0254e702008-02-26 07:04:54 +00003863 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003864 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003865
Dan Gohman475871a2008-07-27 21:46:04 +00003866 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003867}
3868
Dan Gohman475871a2008-07-27 21:46:04 +00003869SDValue DAGCombiner::visitFMUL(SDNode *N) {
3870 SDValue N0 = N->getOperand(0);
3871 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003872 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3873 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003874 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003875
Dan Gohman7f321562007-06-25 16:23:39 +00003876 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003878 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003879 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003880 }
Dan Gohman7f321562007-06-25 16:23:39 +00003881
Nate Begeman11af4ea2005-10-17 20:40:11 +00003882 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003883 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003884 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003885 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003886 if (N0CFP && !N1CFP)
3887 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00003888 // fold (A * 0) -> 0
3889 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
3890 return N1;
Nate Begeman11af4ea2005-10-17 20:40:11 +00003891 // fold (fmul X, 2.0) -> (fadd X, X)
3892 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3893 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003894 // fold (fmul X, -1.0) -> (fneg X)
3895 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00003896 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3897 return DAG.getNode(ISD::FNEG, VT, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003898
3899 // -X * -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003900 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3901 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003902 // Both can be negated for free, check to see if at least one is cheaper
3903 // negated.
3904 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003905 return DAG.getNode(ISD::FMUL, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003906 GetNegatedExpression(N0, DAG, LegalOperations),
3907 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003908 }
3909 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003910
3911 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3912 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003913 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003914 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3915 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3916
Dan Gohman475871a2008-07-27 21:46:04 +00003917 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003918}
3919
Dan Gohman475871a2008-07-27 21:46:04 +00003920SDValue DAGCombiner::visitFDIV(SDNode *N) {
3921 SDValue N0 = N->getOperand(0);
3922 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003923 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3924 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003925 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003926
Dan Gohman7f321562007-06-25 16:23:39 +00003927 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003928 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003929 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003930 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003931 }
Dan Gohman7f321562007-06-25 16:23:39 +00003932
Nate Begemana148d982006-01-18 22:35:16 +00003933 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003934 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003935 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003936
3937
3938 // -X / -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003939 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3940 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003941 // Both can be negated for free, check to see if at least one is cheaper
3942 // negated.
3943 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003944 return DAG.getNode(ISD::FDIV, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003945 GetNegatedExpression(N0, DAG, LegalOperations),
3946 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003947 }
3948 }
3949
Dan Gohman475871a2008-07-27 21:46:04 +00003950 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003951}
3952
Dan Gohman475871a2008-07-27 21:46:04 +00003953SDValue DAGCombiner::visitFREM(SDNode *N) {
3954 SDValue N0 = N->getOperand(0);
3955 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003956 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3957 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003958 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003959
Nate Begemana148d982006-01-18 22:35:16 +00003960 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003961 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003962 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003963
Dan Gohman475871a2008-07-27 21:46:04 +00003964 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003965}
3966
Dan Gohman475871a2008-07-27 21:46:04 +00003967SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3968 SDValue N0 = N->getOperand(0);
3969 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003970 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3971 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003972 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003973
Dale Johannesendb44bf82007-10-16 23:38:29 +00003974 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003975 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3976
3977 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003978 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003979 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3980 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00003981 if (!V.isNegative()) {
3982 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
3983 return DAG.getNode(ISD::FABS, VT, N0);
3984 } else {
3985 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
3986 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3987 }
Chris Lattner12d83032006-03-05 05:30:57 +00003988 }
3989
3990 // copysign(fabs(x), y) -> copysign(x, y)
3991 // copysign(fneg(x), y) -> copysign(x, y)
3992 // copysign(copysign(x,z), y) -> copysign(x, y)
3993 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3994 N0.getOpcode() == ISD::FCOPYSIGN)
3995 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3996
3997 // copysign(x, abs(y)) -> abs(x)
3998 if (N1.getOpcode() == ISD::FABS)
3999 return DAG.getNode(ISD::FABS, VT, N0);
4000
4001 // copysign(x, copysign(y,z)) -> copysign(x, z)
4002 if (N1.getOpcode() == ISD::FCOPYSIGN)
4003 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4004
4005 // copysign(x, fp_extend(y)) -> copysign(x, y)
4006 // copysign(x, fp_round(y)) -> copysign(x, y)
4007 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4008 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4009
Dan Gohman475871a2008-07-27 21:46:04 +00004010 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004011}
4012
4013
Chris Lattner01b3d732005-09-28 22:28:18 +00004014
Dan Gohman475871a2008-07-27 21:46:04 +00004015SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4016 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004017 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004018 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004019 MVT OpVT = N0.getValueType();
4020
Nate Begeman1d4d4142005-09-01 00:19:25 +00004021 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004022 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004023 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004024
4025 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4026 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00004027 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00004028 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
4029 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4030 if (DAG.SignBitIsZero(N0))
4031 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4032 }
4033
4034
Dan Gohman475871a2008-07-27 21:46:04 +00004035 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004036}
4037
Dan Gohman475871a2008-07-27 21:46:04 +00004038SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4039 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004040 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004041 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004042 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004043
Nate Begeman1d4d4142005-09-01 00:19:25 +00004044 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004045 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004046 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004047
4048 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4049 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00004050 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00004051 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
4052 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4053 if (DAG.SignBitIsZero(N0))
4054 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4055 }
4056
Dan Gohman475871a2008-07-27 21:46:04 +00004057 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004058}
4059
Dan Gohman475871a2008-07-27 21:46:04 +00004060SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4061 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004062 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004063 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004064
4065 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004066 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004067 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004068 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004069}
4070
Dan Gohman475871a2008-07-27 21:46:04 +00004071SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4072 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004073 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004074 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004075
4076 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004077 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004078 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004079 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004080}
4081
Dan Gohman475871a2008-07-27 21:46:04 +00004082SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4083 SDValue N0 = N->getOperand(0);
4084 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004085 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004086 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004087
4088 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004089 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004090 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004091
4092 // fold (fp_round (fp_extend x)) -> x
4093 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4094 return N0.getOperand(0);
4095
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004096 // fold (fp_round (fp_round x)) -> (fp_round x)
4097 if (N0.getOpcode() == ISD::FP_ROUND) {
4098 // This is a value preserving truncation if both round's are.
4099 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004100 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004101 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4102 DAG.getIntPtrConstant(IsTrunc));
4103 }
4104
Chris Lattner79dbea52006-03-13 06:26:26 +00004105 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004106 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004108 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004109 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4110 }
4111
Dan Gohman475871a2008-07-27 21:46:04 +00004112 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004113}
4114
Dan Gohman475871a2008-07-27 21:46:04 +00004115SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4116 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004117 MVT VT = N->getValueType(0);
4118 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004119 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004120
Nate Begeman1d4d4142005-09-01 00:19:25 +00004121 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004122 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004123 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004124 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004125 }
Dan Gohman475871a2008-07-27 21:46:04 +00004126 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004127}
4128
Dan Gohman475871a2008-07-27 21:46:04 +00004129SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4130 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004131 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004132 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004133
Chris Lattner5938bef2007-12-29 06:55:23 +00004134 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004135 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004136 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4137 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004138
Nate Begeman1d4d4142005-09-01 00:19:25 +00004139 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004140 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004141 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004142
4143 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4144 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004145 if (N0.getOpcode() == ISD::FP_ROUND
4146 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004147 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004148 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004149 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004150 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4151 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4152 }
4153
4154 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004155 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004156 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004157 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004158 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004159 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004160 LN0->getBasePtr(), LN0->getSrcValue(),
4161 LN0->getSrcValueOffset(),
4162 N0.getValueType(),
4163 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004164 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004165 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4166 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004167 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004168 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004169 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004170
Dan Gohman475871a2008-07-27 21:46:04 +00004171 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004172}
4173
Dan Gohman475871a2008-07-27 21:46:04 +00004174SDValue DAGCombiner::visitFNEG(SDNode *N) {
4175 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004176
Duncan Sands25cf2272008-11-24 14:53:14 +00004177 if (isNegatibleForFree(N0, LegalOperations))
4178 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004179
Chris Lattner3bd39d42008-01-27 17:42:27 +00004180 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4181 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004182 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004183 N0.getOperand(0).getValueType().isInteger() &&
4184 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004185 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004186 MVT IntVT = Int.getValueType();
4187 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004188 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004189 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004190 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004191 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4192 }
4193 }
4194
Dan Gohman475871a2008-07-27 21:46:04 +00004195 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004196}
4197
Dan Gohman475871a2008-07-27 21:46:04 +00004198SDValue DAGCombiner::visitFABS(SDNode *N) {
4199 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004200 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004201 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004202
Nate Begeman1d4d4142005-09-01 00:19:25 +00004203 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004204 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004205 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004206 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004207 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004208 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004209 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004210 // fold (fabs (fcopysign x, y)) -> (fabs x)
4211 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4212 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4213
Chris Lattner3bd39d42008-01-27 17:42:27 +00004214 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4215 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004216 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004217 N0.getOperand(0).getValueType().isInteger() &&
4218 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004219 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004220 MVT IntVT = Int.getValueType();
4221 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004222 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004223 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004224 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004225 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4226 }
4227 }
4228
Dan Gohman475871a2008-07-27 21:46:04 +00004229 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004230}
4231
Dan Gohman475871a2008-07-27 21:46:04 +00004232SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4233 SDValue Chain = N->getOperand(0);
4234 SDValue N1 = N->getOperand(1);
4235 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004236 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4237
4238 // never taken branch, fold to chain
4239 if (N1C && N1C->isNullValue())
4240 return Chain;
4241 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004242 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004243 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004244 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4245 // on the target.
4246 if (N1.getOpcode() == ISD::SETCC &&
4247 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4248 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4249 N1.getOperand(0), N1.getOperand(1), N2);
4250 }
Dan Gohman475871a2008-07-27 21:46:04 +00004251 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004252}
4253
Chris Lattner3ea0b472005-10-05 06:47:48 +00004254// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4255//
Dan Gohman475871a2008-07-27 21:46:04 +00004256SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004257 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004258 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004259
Duncan Sands8eab8a22008-06-09 11:32:28 +00004260 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004261 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sands25cf2272008-11-24 14:53:14 +00004262 CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004263 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004264
Gabor Greifba36cb52008-08-28 21:40:38 +00004265 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004266
4267 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004268 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004269 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4270 N->getOperand(4));
4271 // fold br_cc false, dest -> unconditional fall through
4272 if (SCCC && SCCC->isNullValue())
4273 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004274
Nate Begemane17daeb2005-10-05 21:43:42 +00004275 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004276 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004277 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4278 Simp.getOperand(2), Simp.getOperand(0),
4279 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004280 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004281}
4282
Chris Lattner448f2192006-11-11 00:39:41 +00004283
Duncan Sandsec87aa82008-06-15 20:12:31 +00004284/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4285/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004286/// and it has other uses besides the load / store. After the
4287/// transformation, the new indexed load / store has effectively folded
4288/// the add / subtract in and all of its other uses are redirected to the
4289/// new load / store.
4290bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004291 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004292 return false;
4293
4294 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004295 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004296 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004297 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004298 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004299 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004300 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004301 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004302 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4303 return false;
4304 Ptr = LD->getBasePtr();
4305 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004306 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004307 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004308 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004309 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4310 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4311 return false;
4312 Ptr = ST->getBasePtr();
4313 isLoad = false;
4314 } else
4315 return false;
4316
Chris Lattner9f1794e2006-11-11 00:56:29 +00004317 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4318 // out. There is no reason to make this a preinc/predec.
4319 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004320 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004321 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004322
Chris Lattner9f1794e2006-11-11 00:56:29 +00004323 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004324 SDValue BasePtr;
4325 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4327 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4328 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004329 // Don't create a indexed load / store with zero offset.
4330 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004331 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004332 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004333
Chris Lattner41e53fd2006-11-11 01:00:15 +00004334 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004335 // 1) The new base ptr is a frame index.
4336 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00004337 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004338 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004339 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004340 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004341
Chris Lattner41e53fd2006-11-11 01:00:15 +00004342 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4343 // (plus the implicit offset) to a register to preinc anyway.
4344 if (isa<FrameIndexSDNode>(BasePtr))
4345 return false;
4346
4347 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004348 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004349 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004350 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004351 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004352 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004353
Evan Chengc843abe2007-05-24 02:35:39 +00004354 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004355 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004356 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4357 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004358 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004359 if (Use == N)
4360 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004361 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004362 return false;
4363
4364 if (!((Use->getOpcode() == ISD::LOAD &&
4365 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004366 (Use->getOpcode() == ISD::STORE &&
4367 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004368 RealUse = true;
4369 }
4370 if (!RealUse)
4371 return false;
4372
Dan Gohman475871a2008-07-27 21:46:04 +00004373 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004374 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004375 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004376 else
Dan Gohman475871a2008-07-27 21:46:04 +00004377 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004378 ++PreIndexedNodes;
4379 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004380 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004381 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004382 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004383 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004384 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004385 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004386 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004387 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004388 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004389 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004390 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004391 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004392 }
4393
Chris Lattner9f1794e2006-11-11 00:56:29 +00004394 // Finally, since the node is now dead, remove it from the graph.
4395 DAG.DeleteNode(N);
4396
4397 // Replace the uses of Ptr with uses of the updated base value.
4398 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004399 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004400 removeFromWorkList(Ptr.getNode());
4401 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004402
4403 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004404}
4405
Duncan Sandsec87aa82008-06-15 20:12:31 +00004406/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004407/// add / sub of the base pointer node into a post-indexed load / store.
4408/// The transformation folded the add / subtract into the new indexed
4409/// load / store effectively and all of its uses are redirected to the
4410/// new load / store.
4411bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004412 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004413 return false;
4414
4415 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004416 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004417 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004418 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004419 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004420 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004421 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004422 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4423 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4424 return false;
4425 Ptr = LD->getBasePtr();
4426 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004427 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004428 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004429 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004430 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4431 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4432 return false;
4433 Ptr = ST->getBasePtr();
4434 isLoad = false;
4435 } else
4436 return false;
4437
Gabor Greifba36cb52008-08-28 21:40:38 +00004438 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004439 return false;
4440
Gabor Greifba36cb52008-08-28 21:40:38 +00004441 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4442 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004443 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004444 if (Op == N ||
4445 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4446 continue;
4447
Dan Gohman475871a2008-07-27 21:46:04 +00004448 SDValue BasePtr;
4449 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004450 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4451 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4452 if (Ptr == Offset)
4453 std::swap(BasePtr, Offset);
4454 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004455 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004456 // Don't create a indexed load / store with zero offset.
4457 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004458 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004459 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004460
Chris Lattner9f1794e2006-11-11 00:56:29 +00004461 // Try turning it into a post-indexed load / store except when
4462 // 1) All uses are load / store ops that use it as base ptr.
4463 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4464 // nor a successor of N. Otherwise, if Op is folded that would
4465 // create a cycle.
4466
4467 // Check for #1.
4468 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004469 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4470 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004471 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004472 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004473 continue;
4474
Chris Lattner9f1794e2006-11-11 00:56:29 +00004475 // If all the uses are load / store addresses, then don't do the
4476 // transformation.
4477 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4478 bool RealUse = false;
4479 for (SDNode::use_iterator III = Use->use_begin(),
4480 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004481 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004482 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004483 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004484 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004485 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004486 RealUse = true;
4487 }
Chris Lattner448f2192006-11-11 00:39:41 +00004488
Chris Lattner9f1794e2006-11-11 00:56:29 +00004489 if (!RealUse) {
4490 TryNext = true;
4491 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004492 }
4493 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004494 }
4495 if (TryNext)
4496 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004497
Chris Lattner9f1794e2006-11-11 00:56:29 +00004498 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004499 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004500 SDValue Result = isLoad
4501 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4502 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004503 ++PostIndexedNodes;
4504 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004505 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004506 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004507 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004508 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004509 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004510 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004511 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004512 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004513 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004514 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004515 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004516 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004517 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004518
Chris Lattner9f1794e2006-11-11 00:56:29 +00004519 // Finally, since the node is now dead, remove it from the graph.
4520 DAG.DeleteNode(N);
4521
4522 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004523 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004524 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004525 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004526 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004527 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004528 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004529 }
4530 }
4531 }
4532 return false;
4533}
4534
Chris Lattner00161a62008-01-25 07:20:16 +00004535/// InferAlignment - If we can infer some alignment information from this
4536/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004537static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004538 // If this is a direct reference to a stack slot, use information about the
4539 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004540 int FrameIdx = 1 << 31;
4541 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004542 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004543 FrameIdx = FI->getIndex();
4544 } else if (Ptr.getOpcode() == ISD::ADD &&
4545 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4546 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4547 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4548 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004549 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004550
4551 if (FrameIdx != (1 << 31)) {
4552 // FIXME: Handle FI+CST.
4553 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4554 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004555 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004556
4557 // The alignment of the frame index can be determined from its offset from
4558 // the incoming frame position. If the frame object is at offset 32 and
4559 // the stack is guaranteed to be 16-byte aligned, then we know that the
4560 // object is 16-byte aligned.
4561 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4562 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4563
4564 // Finally, the frame object itself may have a known alignment. Factor
4565 // the alignment + offset into a new alignment. For example, if we know
4566 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4567 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4568 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4569 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4570 FrameOffset);
4571 return std::max(Align, FIInfoAlign);
4572 }
4573 }
Chris Lattner00161a62008-01-25 07:20:16 +00004574
4575 return 0;
4576}
Chris Lattner448f2192006-11-11 00:39:41 +00004577
Dan Gohman475871a2008-07-27 21:46:04 +00004578SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004579 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004580 SDValue Chain = LD->getChain();
4581 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004582
4583 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004584 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004585 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4586 if (Align > LD->getAlignment())
4587 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4588 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004589 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004590 LD->isVolatile(), Align);
4591 }
4592 }
4593
Evan Cheng45a7ca92007-05-01 00:38:21 +00004594
4595 // If load is not volatile and there are no uses of the loaded value (and
4596 // the updated indexed value in case of indexed loads), change uses of the
4597 // chain value into uses of the chain input (i.e. delete the dead load).
4598 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004599 if (N->getValueType(1) == MVT::Other) {
4600 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004601 if (N->hasNUsesOfValue(0, 0)) {
4602 // It's not safe to use the two value CombineTo variant here. e.g.
4603 // v1, chain2 = load chain1, loc
4604 // v2, chain3 = load chain2, loc
4605 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004606 // Now we replace use of chain2 with chain1. This makes the second load
4607 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004608 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004609 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004610 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004611 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004612 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004613 if (N->use_empty()) {
4614 removeFromWorkList(N);
4615 DAG.DeleteNode(N);
4616 }
Dan Gohman475871a2008-07-27 21:46:04 +00004617 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004618 }
Evan Cheng498f5592007-05-01 08:53:39 +00004619 } else {
4620 // Indexed loads.
4621 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4622 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004623 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004624 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004625 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004626 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004627 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004628 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4629 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004630 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004631 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004632 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004633 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004634 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004635 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004636 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004637 }
4638 }
Chris Lattner01a22022005-10-10 22:04:48 +00004639
4640 // If this load is directly stored, replace the load value with the stored
4641 // value.
4642 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004643 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004644 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4645 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004646 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004647 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4648 if (PrevST->getBasePtr() == Ptr &&
4649 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004650 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004651 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004652 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004653
Jim Laskey7ca56af2006-10-11 13:47:09 +00004654 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004655 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004656 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004657
Jim Laskey6ff23e52006-10-04 16:53:27 +00004658 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004659 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004660 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004661
Jim Laskey279f0532006-09-25 16:29:54 +00004662 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004663 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4664 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004665 LD->getSrcValue(), LD->getSrcValueOffset(),
4666 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004667 } else {
4668 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4669 LD->getValueType(0),
4670 BetterChain, Ptr, LD->getSrcValue(),
4671 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004672 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004673 LD->isVolatile(),
4674 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004675 }
Jim Laskey279f0532006-09-25 16:29:54 +00004676
Jim Laskey6ff23e52006-10-04 16:53:27 +00004677 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004678 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004679 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004680
Jim Laskey274062c2006-10-13 23:32:28 +00004681 // Replace uses with load result and token factor. Don't add users
4682 // to work list.
4683 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004684 }
4685 }
4686
Evan Cheng7fc033a2006-11-03 03:06:21 +00004687 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004688 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004689 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004690
Dan Gohman475871a2008-07-27 21:46:04 +00004691 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004692}
4693
Chris Lattner07649d92008-01-08 23:08:06 +00004694
Dan Gohman475871a2008-07-27 21:46:04 +00004695SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004696 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004697 SDValue Chain = ST->getChain();
4698 SDValue Value = ST->getValue();
4699 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004700
Chris Lattner00161a62008-01-25 07:20:16 +00004701 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004702 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004703 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4704 if (Align > ST->getAlignment())
4705 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004706 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004707 ST->isVolatile(), Align);
4708 }
4709 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004710
Evan Cheng59d5b682007-05-07 21:27:48 +00004711 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004712 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004713 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004714 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004715 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004716 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004717 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004718 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004719 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004720 ((!LegalOperations && !ST->isVolatile()) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004721 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004722 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004723 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004724 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004725
Nate Begeman2cbba892006-12-11 02:23:46 +00004726 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004727 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004728 // NOTE: If the original store is volatile, this transform must not increase
4729 // the number of stores. For example, on x86-32 an f64 can be stored in one
4730 // processor operation but an i64 (which is not legal) requires two. So the
4731 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004732 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004733 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004734 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004735 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004736 case MVT::f80: // We don't do this for these yet.
4737 case MVT::f128:
4738 case MVT::ppcf128:
4739 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004740 case MVT::f32:
Duncan Sands25cf2272008-11-24 14:53:14 +00004741 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
4742 !ST->isVolatile()) || TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004743 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004744 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004745 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004746 ST->getSrcValueOffset(), ST->isVolatile(),
4747 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004748 }
4749 break;
4750 case MVT::f64:
Duncan Sands25cf2272008-11-24 14:53:14 +00004751 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
4752 !ST->isVolatile()) || TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004753 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004754 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004755 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004756 ST->getSrcValueOffset(), ST->isVolatile(),
4757 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004758 } else if (!ST->isVolatile() &&
4759 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004760 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004761 // argument passing. Since this is so common, custom legalize the
4762 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004763 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004764 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4765 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004766 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004767
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004768 int SVOffset = ST->getSrcValueOffset();
4769 unsigned Alignment = ST->getAlignment();
4770 bool isVolatile = ST->isVolatile();
4771
Dan Gohman475871a2008-07-27 21:46:04 +00004772 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004773 ST->getSrcValueOffset(),
4774 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004775 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4776 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004777 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004778 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004779 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004780 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004781 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4782 }
4783 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004784 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004785 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004786 }
4787
Jim Laskey279f0532006-09-25 16:29:54 +00004788 if (CombinerAA) {
4789 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004790 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004791
Jim Laskey6ff23e52006-10-04 16:53:27 +00004792 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004793 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004794 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004795 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004796 if (ST->isTruncatingStore()) {
4797 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004798 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004799 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004800 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004801 } else {
4802 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004803 ST->getSrcValue(), ST->getSrcValueOffset(),
4804 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004805 }
4806
Jim Laskey279f0532006-09-25 16:29:54 +00004807 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004808 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004809 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4810
4811 // Don't add users to work list.
4812 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004813 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004814 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004815
Evan Cheng33dbedc2006-11-05 09:31:14 +00004816 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004817 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004818 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004819
Chris Lattner3c872852007-12-29 06:26:16 +00004820 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004821 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004823 // See if we can simplify the input to this truncstore with knowledge that
4824 // only the low bits are being used. For example:
4825 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004826 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004827 GetDemandedBits(Value,
4828 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004829 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004830 AddToWorkList(Value.getNode());
4831 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004832 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004833 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004834 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004835
4836 // Otherwise, see if we can simplify the operation with
4837 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004838 if (SimplifyDemandedBits(Value,
4839 APInt::getLowBitsSet(
4840 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004841 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004842 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004843 }
4844
Chris Lattner3c872852007-12-29 06:26:16 +00004845 // If this is a load followed by a store to the same location, then the store
4846 // is dead/noop.
4847 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004848 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004849 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004850 // There can't be any side effects between the load and store, such as
4851 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004852 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004853 // The store is dead, remove it.
4854 return Chain;
4855 }
4856 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004857
Chris Lattnerddf89562008-01-17 19:59:44 +00004858 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4859 // truncating store. We can do this even if this is already a truncstore.
4860 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004861 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004862 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004863 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004864 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004865 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004866 ST->isVolatile(), ST->getAlignment());
4867 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004868
Dan Gohman475871a2008-07-27 21:46:04 +00004869 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004870}
4871
Dan Gohman475871a2008-07-27 21:46:04 +00004872SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4873 SDValue InVec = N->getOperand(0);
4874 SDValue InVal = N->getOperand(1);
4875 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004876
4877 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4878 // vector with the inserted element.
4879 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004880 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004881 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4882 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004883 if (Elt < Ops.size())
4884 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004885 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4886 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004887 }
4888
Dan Gohman475871a2008-07-27 21:46:04 +00004889 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004890}
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004893 // (vextract (scalar_to_vector val, 0) -> val
4894 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004895
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004896 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
4897 return InVec.getOperand(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004898
4899 // Perform only after legalization to ensure build_vector / vector_shuffle
4900 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00004901 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004902
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004903 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4904 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4905 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004906 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004907
Evan Cheng513da432007-10-06 08:19:55 +00004908 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004909 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004910 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00004911 bool BCNumEltsChanged = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004912 MVT VT = InVec.getValueType();
4913 MVT EVT = VT.getVectorElementType();
4914 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004915 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004916 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wanga60b5232008-12-11 00:26:16 +00004917 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004918 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00004919 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4920 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004921 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004922 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004923 NewLoad = true;
4924 }
Evan Cheng513da432007-10-06 08:19:55 +00004925
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004926 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004927 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004928 LN0 = cast<LoadSDNode>(InVec);
4929 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4930 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004931 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004932 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4933 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4934 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4935 // =>
4936 // (load $addr+1*size)
Mon P Wanga60b5232008-12-11 00:26:16 +00004937
4938 // If the bit convert changed the number of elements, it is unsafe
4939 // to examine the mask.
4940 if (BCNumEltsChanged)
4941 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004942 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004943 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004944 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4945 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4946 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4947 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004948 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004949 LN0 = cast<LoadSDNode>(InVec);
4950 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004951 }
4952 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004953 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004954 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004955
4956 unsigned Align = LN0->getAlignment();
4957 if (NewLoad) {
4958 // Check the resultant load doesn't need a higher alignment than the
4959 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004960 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004962 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004963 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004964 Align = NewAlign;
4965 }
4966
Dan Gohman475871a2008-07-27 21:46:04 +00004967 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004968 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004969 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4970 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004971 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004972 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004973 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4974 DAG.getConstant(PtrOff, PtrType));
4975 }
4976 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4977 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4978 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004979 }
Dan Gohman475871a2008-07-27 21:46:04 +00004980 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004981}
4982
4983
Dan Gohman475871a2008-07-27 21:46:04 +00004984SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004985 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004986 MVT VT = N->getValueType(0);
4987 unsigned NumElts = VT.getVectorNumElements();
4988 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004989
Dan Gohman7f321562007-06-25 16:23:39 +00004990 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4991 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4992 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004993 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004994 for (unsigned i = 0; i != NumInScalars; ++i) {
4995 // Ignore undef inputs.
4996 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4997
Dan Gohman7f321562007-06-25 16:23:39 +00004998 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004999 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00005000 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00005001 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005002 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005003 break;
5004 }
5005
Dan Gohman7f321562007-06-25 16:23:39 +00005006 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005007 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005008 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005009 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005010 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005011 break;
5012 }
5013
5014 // Otherwise, remember this. We allow up to two distinct input vectors.
5015 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5016 continue;
5017
Gabor Greifba36cb52008-08-28 21:40:38 +00005018 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005019 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005020 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005021 VecIn2 = ExtractedFromVec;
5022 } else {
5023 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005024 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005025 break;
5026 }
5027 }
5028
5029 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005030 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005031 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005032 for (unsigned i = 0; i != NumInScalars; ++i) {
5033 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00005034 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005035 continue;
5036 }
5037
Dan Gohman475871a2008-07-27 21:46:04 +00005038 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005039
5040 // If extracting from the first vector, just use the index directly.
5041 if (Extract.getOperand(0) == VecIn1) {
5042 BuildVecIndices.push_back(Extract.getOperand(1));
5043 continue;
5044 }
5045
5046 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005047 unsigned Idx =
5048 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005049 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005050 }
5051
5052 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005053 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sands25cf2272008-11-24 14:53:14 +00005054 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5055 return SDValue();
5056
Dan Gohman7f321562007-06-25 16:23:39 +00005057 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00005058 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005059 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00005060 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005061 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00005062 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005063 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005064 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00005065 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00005066 EltType));
5067 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005068 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005069 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00005070 }
Dan Gohman7f321562007-06-25 16:23:39 +00005071 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005072 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005073 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005074 }
5075
Dan Gohman475871a2008-07-27 21:46:04 +00005076 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005077}
5078
Dan Gohman475871a2008-07-27 21:46:04 +00005079SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005080 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5081 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5082 // inputs come from at most two distinct vectors, turn this into a shuffle
5083 // node.
5084
5085 // If we only have one input vector, we don't need to do any concatenation.
5086 if (N->getNumOperands() == 1) {
5087 return N->getOperand(0);
5088 }
5089
Dan Gohman475871a2008-07-27 21:46:04 +00005090 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005091}
5092
Dan Gohman475871a2008-07-27 21:46:04 +00005093SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5094 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005095 unsigned NumElts = ShufMask.getNumOperands();
5096
Mon P Wangaeb06d22008-11-10 04:46:22 +00005097 SDValue N0 = N->getOperand(0);
5098 SDValue N1 = N->getOperand(1);
5099
5100 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5101 "Vector shuffle must be normalized in DAG");
5102
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005103 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5104 bool isIdentity = true;
5105 for (unsigned i = 0; i != NumElts; ++i) {
5106 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005107 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005108 isIdentity = false;
5109 break;
5110 }
5111 }
5112 if (isIdentity) return N->getOperand(0);
5113
5114 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5115 isIdentity = true;
5116 for (unsigned i = 0; i != NumElts; ++i) {
5117 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005118 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5119 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005120 isIdentity = false;
5121 break;
5122 }
5123 }
5124 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005125
5126 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5127 // needed at all.
5128 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005129 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005130 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005131 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005132 for (unsigned i = 0; i != NumElts; ++i)
5133 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005134 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005135 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005136 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005137 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005138 BaseIdx = Idx;
5139 } else {
5140 if (BaseIdx != Idx)
5141 isSplat = false;
5142 if (VecNum != V) {
5143 isUnary = false;
5144 break;
5145 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005146 }
5147 }
5148
Evan Chenge7bec0d2006-07-20 22:44:41 +00005149 // Normalize unary shuffle so the RHS is undef.
5150 if (isUnary && VecNum == 1)
5151 std::swap(N0, N1);
5152
Evan Cheng917ec982006-07-21 08:25:53 +00005153 // If it is a splat, check if the argument vector is a build_vector with
5154 // all scalar elements the same.
5155 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005156 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005157
Dan Gohman7f321562007-06-25 16:23:39 +00005158 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005159 // not the number of vector elements, look through it. Be careful not to
5160 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005161 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005162 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005163 if (ConvInput.getValueType().isVector() &&
5164 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005165 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005166 }
5167
Dan Gohman7f321562007-06-25 16:23:39 +00005168 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5169 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005170 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005171 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005172 bool AllSame = true;
5173 for (unsigned i = 0; i != NumElems; ++i) {
5174 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5175 Base = V->getOperand(i);
5176 break;
5177 }
5178 }
5179 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005180 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005181 return N0;
5182 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005183 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005184 AllSame = false;
5185 break;
5186 }
5187 }
5188 // Splat of <x, x, x, x>, return <x, x, x, x>
5189 if (AllSame)
5190 return N0;
5191 }
5192 }
5193 }
5194
Evan Chenge7bec0d2006-07-20 22:44:41 +00005195 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5196 // into an undef.
5197 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005198 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5199 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005200 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005201 for (unsigned i = 0; i != NumElts; ++i) {
5202 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005203 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5204 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005205 MappedOps.push_back(ShufMask.getOperand(i));
5206 } else {
5207 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005208 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5209 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005210 MappedOps.push_back(DAG.getConstant(NewIdx,
5211 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005212 }
5213 }
Dan Gohman7f321562007-06-25 16:23:39 +00005214 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005215 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005216 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005217 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5218 N0,
5219 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5220 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005221 }
Dan Gohman7f321562007-06-25 16:23:39 +00005222
Dan Gohman475871a2008-07-27 21:46:04 +00005223 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005224}
5225
Evan Cheng44f1f092006-04-20 08:56:16 +00005226/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005227/// an AND to a vector_shuffle with the destination vector and a zero vector.
5228/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005229/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005230SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5231 SDValue LHS = N->getOperand(0);
5232 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005233 if (N->getOpcode() == ISD::AND) {
5234 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005235 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005236 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005237 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005238 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005239 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005240 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005241 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005242 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005243 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005244 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005245 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005246 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005247 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005248 else
Dan Gohman475871a2008-07-27 21:46:04 +00005249 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005250 }
5251
5252 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005253 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005254 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005255
Dan Gohman7f321562007-06-25 16:23:39 +00005256 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005257 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005258 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005259 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005260 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005261 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005262 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005263 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005264 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005265 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005266 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005267 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005268 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005269 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005270 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005271 if (VT != N->getValueType(0))
5272 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005273 return Result;
5274 }
5275 }
Dan Gohman475871a2008-07-27 21:46:04 +00005276 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005277}
5278
Dan Gohman7f321562007-06-25 16:23:39 +00005279/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005280SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005281 // After legalize, the target may be depending on adds and other
5282 // binary ops to provide legal ways to construct constants or other
5283 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005284 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005285
Duncan Sands83ec4b62008-06-06 12:08:01 +00005286 MVT VT = N->getValueType(0);
5287 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005288
Duncan Sands83ec4b62008-06-06 12:08:01 +00005289 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue LHS = N->getOperand(0);
5291 SDValue RHS = N->getOperand(1);
5292 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005293 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005294
Dan Gohman7f321562007-06-25 16:23:39 +00005295 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005296 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005297 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5298 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005299 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005300 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SDValue LHSOp = LHS.getOperand(i);
5302 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005303 // If these two elements can't be folded, bail out.
5304 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5305 LHSOp.getOpcode() != ISD::Constant &&
5306 LHSOp.getOpcode() != ISD::ConstantFP) ||
5307 (RHSOp.getOpcode() != ISD::UNDEF &&
5308 RHSOp.getOpcode() != ISD::Constant &&
5309 RHSOp.getOpcode() != ISD::ConstantFP))
5310 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005311 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005312 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5313 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005314 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005315 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005316 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005317 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005318 break;
5319 }
Dan Gohman7f321562007-06-25 16:23:39 +00005320 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005321 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005322 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5323 Ops.back().getOpcode() == ISD::Constant ||
5324 Ops.back().getOpcode() == ISD::ConstantFP) &&
5325 "Scalar binop didn't fold!");
5326 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005327
Dan Gohman7f321562007-06-25 16:23:39 +00005328 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005329 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005330 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005331 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005332 }
5333
Dan Gohman475871a2008-07-27 21:46:04 +00005334 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005335}
5336
Dan Gohman475871a2008-07-27 21:46:04 +00005337SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005338 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5339
Dan Gohman475871a2008-07-27 21:46:04 +00005340 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005341 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5342 // If we got a simplified select_cc node back from SimplifySelectCC, then
5343 // break it down into a new SETCC node, and a new SELECT node, and then return
5344 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005345 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005346 // Check to see if we got a select_cc back (to turn into setcc/select).
5347 // Otherwise, just return whatever node we got back, like fabs.
5348 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005350 SCC.getOperand(0), SCC.getOperand(1),
5351 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005352 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005353 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5354 SCC.getOperand(3), SETCC);
5355 }
5356 return SCC;
5357 }
Dan Gohman475871a2008-07-27 21:46:04 +00005358 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005359}
5360
Chris Lattner40c62d52005-10-18 06:04:22 +00005361/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5362/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005363/// select. Callers of this should assume that TheSelect is deleted if this
5364/// returns true. As such, they should return the appropriate thing (e.g. the
5365/// node) back to the top-level of the DAG combiner loop to avoid it being
5366/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005367///
Dan Gohman475871a2008-07-27 21:46:04 +00005368bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5369 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005370
5371 // If this is a select from two identical things, try to pull the operation
5372 // through the select.
5373 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005374 // If this is a load and the token chain is identical, replace the select
5375 // of two loads with a load through a select of the address to load from.
5376 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5377 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005378 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005379 // Do not let this transformation reduce the number of volatile loads.
5380 !cast<LoadSDNode>(LHS)->isVolatile() &&
5381 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005382 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005383 LHS.getOperand(0) == RHS.getOperand(0)) {
5384 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5385 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5386
5387 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005388 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005389 // FIXME: this conflates two src values, discarding one. This is not
5390 // the right thing to do, but nothing uses srcvalues now. When they do,
5391 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005392 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005393 if (TheSelect->getOpcode() == ISD::SELECT) {
5394 // Check that the condition doesn't reach either load. If so, folding
5395 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005396 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5397 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005398 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5399 TheSelect->getOperand(0), LLD->getBasePtr(),
5400 RLD->getBasePtr());
5401 }
5402 } else {
5403 // Check that the condition doesn't reach either load. If so, folding
5404 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005405 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5406 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5407 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5408 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005409 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005410 TheSelect->getOperand(0),
5411 TheSelect->getOperand(1),
5412 LLD->getBasePtr(), RLD->getBasePtr(),
5413 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005414 }
Evan Cheng466685d2006-10-09 20:57:25 +00005415 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005416
Gabor Greifba36cb52008-08-28 21:40:38 +00005417 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005419 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5420 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5421 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005422 LLD->getSrcValueOffset(),
5423 LLD->isVolatile(),
5424 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005425 else {
5426 Load = DAG.getExtLoad(LLD->getExtensionType(),
5427 TheSelect->getValueType(0),
5428 LLD->getChain(), Addr, LLD->getSrcValue(),
5429 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005430 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005431 LLD->isVolatile(),
5432 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005433 }
5434 // Users of the select now use the result of the load.
5435 CombineTo(TheSelect, Load);
5436
5437 // Users of the old loads now use the new load's chain. We know the
5438 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005439 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5440 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005441 return true;
5442 }
Evan Chengc5484282006-10-04 00:56:09 +00005443 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005444 }
5445 }
5446
5447 return false;
5448}
5449
Dan Gohman475871a2008-07-27 21:46:04 +00005450SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5451 SDValue N2, SDValue N3,
5452 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005453
Duncan Sands83ec4b62008-06-06 12:08:01 +00005454 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005455 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5456 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5457 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005458
5459 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005460 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5461 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005462 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5463 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005464
5465 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005466 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005467 return N2;
5468 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005469 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005470 return N3;
5471
5472 // Check to see if we can simplify the select into an fabs node
5473 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5474 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005475 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005476 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5477 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5478 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5479 N2 == N3.getOperand(0))
5480 return DAG.getNode(ISD::FABS, VT, N0);
5481
5482 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5483 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5484 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5485 N2.getOperand(0) == N3)
5486 return DAG.getNode(ISD::FABS, VT, N3);
5487 }
5488 }
5489
5490 // Check to see if we can perform the "gzip trick", transforming
5491 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005492 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005493 N0.getValueType().isInteger() &&
5494 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005495 (N1C->isNullValue() || // (a < 0) ? b : 0
5496 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005497 MVT XType = N0.getValueType();
5498 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005499 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005500 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005501 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005502 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5503 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005504 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005505 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5506 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005507 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005508 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005509 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005510 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005511 }
5512 return DAG.getNode(ISD::AND, AType, Shift, N2);
5513 }
Dan Gohman475871a2008-07-27 21:46:04 +00005514 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005515 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005516 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005517 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005518 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005519 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005520 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005521 }
5522 return DAG.getNode(ISD::AND, AType, Shift, N2);
5523 }
5524 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005525
5526 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005527 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005528 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005529
5530 // If the caller doesn't want us to simplify this into a zext of a compare,
5531 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005532 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005533 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005534
Nate Begeman07ed4172005-10-10 21:26:48 +00005535 // Get a SetCC of the condition
5536 // FIXME: Should probably make sure that setcc is legal if we ever have a
5537 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005538 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005539 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005540 if (LegalTypes) {
Duncan Sands5480c042009-01-01 15:52:00 +00005541 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5542 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005543 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005544 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5545 else
5546 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005547 } else {
5548 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005549 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005550 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005551 AddToWorkList(SCC.getNode());
5552 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005553
Dan Gohman002e5d02008-03-13 22:13:53 +00005554 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005555 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005556 // shl setcc result by log2 n2c
5557 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005558 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005559 TLI.getShiftAmountTy()));
5560 }
5561
Nate Begemanf845b452005-10-08 00:29:44 +00005562 // Check to see if this is the equivalent of setcc
5563 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5564 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005565 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005566 MVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005567 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005568 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5569 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005570 if (Res.getValueType() != VT)
5571 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5572 return Res;
5573 }
5574
5575 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5576 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005577 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005578 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005579 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005580 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005581 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005582 TLI.getShiftAmountTy()));
5583 }
5584 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5585 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005587 N0);
Bob Wilson4c245462009-01-22 17:39:32 +00005588 SDValue NotN0 = DAG.getNOT(N0, XType);
Nate Begemanf845b452005-10-08 00:29:44 +00005589 return DAG.getNode(ISD::SRL, XType,
5590 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005591 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005592 TLI.getShiftAmountTy()));
5593 }
5594 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5595 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005596 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005597 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005598 TLI.getShiftAmountTy()));
5599 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5600 }
5601 }
5602
5603 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5604 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5605 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005606 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005607 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5608 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005609 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005610 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005611 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005612 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005613 AddToWorkList(Shift.getNode());
5614 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005615 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5616 }
5617 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5618 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5619 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5620 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5621 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005622 MVT XType = N0.getValueType();
5623 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005624 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005625 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005626 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005627 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005628 AddToWorkList(Shift.getNode());
5629 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005630 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5631 }
5632 }
5633 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005634
Dan Gohman475871a2008-07-27 21:46:04 +00005635 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005636}
5637
Evan Chengfa1eb272007-02-08 22:13:59 +00005638/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005639SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5640 SDValue N1, ISD::CondCode Cond,
5641 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005642 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +00005643 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Evan Chengfa1eb272007-02-08 22:13:59 +00005644 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005645}
5646
Nate Begeman69575232005-10-20 02:15:44 +00005647/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5648/// return a DAG expression to select that will generate the same value by
5649/// multiplying by a magic number. See:
5650/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005651SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005652 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005653 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005654
Andrew Lenharth232c9102006-06-12 16:07:18 +00005655 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005656 ii != ee; ++ii)
5657 AddToWorkList(*ii);
5658 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005659}
5660
5661/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5662/// return a DAG expression to select that will generate the same value by
5663/// multiplying by a magic number. See:
5664/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005665SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005666 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005667 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005668
Andrew Lenharth232c9102006-06-12 16:07:18 +00005669 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005670 ii != ee; ++ii)
5671 AddToWorkList(*ii);
5672 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005673}
5674
Jim Laskey71382342006-10-07 23:37:56 +00005675/// FindBaseOffset - Return true if base is known not to alias with anything
5676/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005677static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005678 // Assume it is a primitive operation.
5679 Base = Ptr; Offset = 0;
5680
5681 // If it's an adding a simple constant then integrate the offset.
5682 if (Base.getOpcode() == ISD::ADD) {
5683 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5684 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005685 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005686 }
5687 }
5688
5689 // If it's any of the following then it can't alias with anything but itself.
5690 return isa<FrameIndexSDNode>(Base) ||
5691 isa<ConstantPoolSDNode>(Base) ||
5692 isa<GlobalAddressSDNode>(Base);
5693}
5694
5695/// isAlias - Return true if there is any possibility that the two addresses
5696/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005697bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005698 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005699 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005700 const Value *SrcValue2, int SrcValueOffset2)
5701{
Jim Laskey71382342006-10-07 23:37:56 +00005702 // If they are the same then they must be aliases.
5703 if (Ptr1 == Ptr2) return true;
5704
5705 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005706 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005707 int64_t Offset1, Offset2;
5708 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5709 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5710
5711 // If they have a same base address then...
5712 if (Base1 == Base2) {
5713 // Check to see if the addresses overlap.
5714 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5715 }
5716
Jim Laskey096c22e2006-10-18 12:29:57 +00005717 // If we know both bases then they can't alias.
5718 if (KnownBase1 && KnownBase2) return false;
5719
Jim Laskey07a27092006-10-18 19:08:31 +00005720 if (CombinerGlobalAA) {
5721 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005722 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5723 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5724 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005725 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005726 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005727 if (AAResult == AliasAnalysis::NoAlias)
5728 return false;
5729 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005730
5731 // Otherwise we have to assume they alias.
5732 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005733}
5734
5735/// FindAliasInfo - Extracts the relevant alias information from the memory
5736/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005737bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005738 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005739 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005740 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5741 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005742 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005743 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005744 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005745 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005746 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005747 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005748 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005749 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005750 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005751 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005752 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005753 }
5754
5755 return false;
5756}
5757
Jim Laskey6ff23e52006-10-04 16:53:27 +00005758/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5759/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005760void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5761 SmallVector<SDValue, 8> &Aliases) {
5762 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005763 std::set<SDNode *> Visited; // Visited node set.
5764
Jim Laskey279f0532006-09-25 16:29:54 +00005765 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005766 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005767 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005768 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005769 int SrcValueOffset;
5770 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005771
Jim Laskey6ff23e52006-10-04 16:53:27 +00005772 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005773 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005774
Jim Laskeybc588b82006-10-05 15:07:25 +00005775 // Look at each chain and determine if it is an alias. If so, add it to the
5776 // aliases list. If not, then continue up the chain looking for the next
5777 // candidate.
5778 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005779 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005780 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005781
Jim Laskeybc588b82006-10-05 15:07:25 +00005782 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005783 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5784 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005785
5786 switch (Chain.getOpcode()) {
5787 case ISD::EntryToken:
5788 // Entry token is ideal chain operand, but handled in FindBetterChain.
5789 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005790
Jim Laskeybc588b82006-10-05 15:07:25 +00005791 case ISD::LOAD:
5792 case ISD::STORE: {
5793 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005794 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005795 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005796 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005797 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005798 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005799 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005800
5801 // If chain is alias then stop here.
5802 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005803 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5804 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005805 Aliases.push_back(Chain);
5806 } else {
5807 // Look further up the chain.
5808 Chains.push_back(Chain.getOperand(0));
5809 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005810 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005811 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005812 break;
5813 }
5814
5815 case ISD::TokenFactor:
5816 // We have to check each of the operands of the token factor, so we queue
5817 // then up. Adding the operands to the queue (stack) in reverse order
5818 // maintains the original order and increases the likelihood that getNode
5819 // will find a matching token factor (CSE.)
5820 for (unsigned n = Chain.getNumOperands(); n;)
5821 Chains.push_back(Chain.getOperand(--n));
5822 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005823 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005824 break;
5825
5826 default:
5827 // For all other instructions we will just have to take what we can get.
5828 Aliases.push_back(Chain);
5829 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005830 }
5831 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005832}
5833
5834/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5835/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005836SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5837 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005838
Jim Laskey6ff23e52006-10-04 16:53:27 +00005839 // Accumulate all the aliases to this node.
5840 GatherAllAliases(N, OldChain, Aliases);
5841
5842 if (Aliases.size() == 0) {
5843 // If no operands then chain to entry token.
5844 return DAG.getEntryNode();
5845 } else if (Aliases.size() == 1) {
5846 // If a single operand then chain to it. We don't need to revisit it.
5847 return Aliases[0];
5848 }
5849
5850 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005851 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005852 &Aliases[0], Aliases.size());
5853
5854 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005855 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005856
5857 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005858}
5859
Nate Begeman1d4d4142005-09-01 00:19:25 +00005860// SelectionDAG::Combine - This is the entry point for the file.
5861//
Duncan Sands25cf2272008-11-24 14:53:14 +00005862void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005863 /// run - This is the main entry point to this class.
5864 ///
Duncan Sands25cf2272008-11-24 14:53:14 +00005865 DAGCombiner(*this, AA, Fast).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005866}