blob: 783bfce9cc39d211dff083bb9c65f7796f7da973 [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;
52 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";
514 WorkListRemover DeadNodes(*this);
515 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
516
517 if (AddTo) {
518 // Push the new nodes and any users onto the worklist
519 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000520 AddToWorkList(To[i].getNode());
521 AddUsersToWorkList(To[i].getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000522 }
523 }
524
525 // Nodes can be reintroduced into the worklist. Make sure we do not
526 // process a node that has been replaced.
527 removeFromWorkList(N);
528
529 // Finally, since the node is now dead, remove it from the graph.
530 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000531 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000532}
533
534/// SimplifyDemandedBits - Check the specified integer node value to see if
535/// it can be simplified or if things it uses can be simplified by bit
536/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000537bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Duncan Sands25cf2272008-11-24 14:53:14 +0000538 TargetLowering::TargetLoweringOpt TLO(DAG);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000539 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000540 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
541 return false;
542
543 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000544 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000545
546 // Replace the old value with the new one.
547 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000548 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
549 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000550 DOUT << '\n';
551
552 // Replace all uses. If any nodes become isomorphic to other nodes and
553 // are deleted, make sure to remove them from our worklist.
554 WorkListRemover DeadNodes(*this);
555 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
556
557 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000558 AddToWorkList(TLO.New.getNode());
559 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000560
561 // Finally, if the node is now dead, remove it from the graph. The node
562 // may not be dead if the replacement process recursively simplified to
563 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000564 if (TLO.Old.getNode()->use_empty()) {
565 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000566
567 // If the operands of this node are only used by the node, they will now
568 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000569 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
570 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
571 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000572
Gabor Greifba36cb52008-08-28 21:40:38 +0000573 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000574 }
575 return true;
576}
577
Chris Lattner29446522007-05-14 22:04:50 +0000578//===----------------------------------------------------------------------===//
579// Main DAG Combiner implementation
580//===----------------------------------------------------------------------===//
581
Duncan Sands25cf2272008-11-24 14:53:14 +0000582void DAGCombiner::Run(CombineLevel AtLevel) {
583 // set the instance variables, so that the various visit routines may use it.
584 Level = AtLevel;
585 LegalOperations = Level >= NoIllegalOperations;
586 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000587
Evan Cheng17a568b2008-08-29 22:21:44 +0000588 // Add all the dag nodes to the worklist.
589 WorkList.reserve(DAG.allnodes_size());
590 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
591 E = DAG.allnodes_end(); I != E; ++I)
592 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000593
Evan Cheng17a568b2008-08-29 22:21:44 +0000594 // Create a dummy node (which is not added to allnodes), that adds a reference
595 // to the root node, preventing it from being deleted, and tracking any
596 // changes of the root.
597 HandleSDNode Dummy(DAG.getRoot());
598
Jim Laskey26f7fa72006-10-17 19:33:52 +0000599 // The root of the dag may dangle to deleted nodes until the dag combiner is
600 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000601 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000602
Evan Cheng17a568b2008-08-29 22:21:44 +0000603 // while the worklist isn't empty, inspect the node on the end of it and
604 // try and combine it.
605 while (!WorkList.empty()) {
606 SDNode *N = WorkList.back();
607 WorkList.pop_back();
608
609 // If N has no uses, it is dead. Make sure to revisit all N's operands once
610 // N is deleted from the DAG, since they too may now be dead or may have a
611 // reduced number of uses, allowing other xforms.
612 if (N->use_empty() && N != &Dummy) {
613 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
614 AddToWorkList(N->getOperand(i).getNode());
615
616 DAG.DeleteNode(N);
617 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000619
620 SDValue RV = combine(N);
621
622 if (RV.getNode() == 0)
623 continue;
624
625 ++NodesCombined;
626
627 // If we get back the same node we passed in, rather than a new node or
628 // zero, we know that the node must have defined multiple values and
629 // CombineTo was used. Since CombineTo takes care of the worklist
630 // mechanics for us, we have no work to do in this case.
631 if (RV.getNode() == N)
632 continue;
633
634 assert(N->getOpcode() != ISD::DELETED_NODE &&
635 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
636 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000637
Evan Cheng17a568b2008-08-29 22:21:44 +0000638 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
639 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
640 DOUT << '\n';
641 WorkListRemover DeadNodes(*this);
642 if (N->getNumValues() == RV.getNode()->getNumValues())
643 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
644 else {
645 assert(N->getValueType(0) == RV.getValueType() &&
646 N->getNumValues() == 1 && "Type mismatch");
647 SDValue OpV = RV;
648 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
649 }
650
651 // Push the new node and any users onto the worklist
652 AddToWorkList(RV.getNode());
653 AddUsersToWorkList(RV.getNode());
654
655 // Add any uses of the old node to the worklist in case this node is the
656 // last one that uses them. They may become dead after this node is
657 // deleted.
658 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
659 AddToWorkList(N->getOperand(i).getNode());
660
661 // Nodes can be reintroduced into the worklist. Make sure we do not
662 // process a node that has been replaced.
663 removeFromWorkList(N);
664
665 // Finally, since the node is now dead, remove it from the graph.
666 DAG.DeleteNode(N);
667 }
668
Chris Lattner95038592005-10-05 06:35:28 +0000669 // If the root changed (e.g. it was a dead load, update the root).
670 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000671}
672
Dan Gohman475871a2008-07-27 21:46:04 +0000673SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000674 switch(N->getOpcode()) {
675 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000676 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000677 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000678 case ISD::ADD: return visitADD(N);
679 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000680 case ISD::ADDC: return visitADDC(N);
681 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000682 case ISD::MUL: return visitMUL(N);
683 case ISD::SDIV: return visitSDIV(N);
684 case ISD::UDIV: return visitUDIV(N);
685 case ISD::SREM: return visitSREM(N);
686 case ISD::UREM: return visitUREM(N);
687 case ISD::MULHU: return visitMULHU(N);
688 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000689 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
690 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
691 case ISD::SDIVREM: return visitSDIVREM(N);
692 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000693 case ISD::AND: return visitAND(N);
694 case ISD::OR: return visitOR(N);
695 case ISD::XOR: return visitXOR(N);
696 case ISD::SHL: return visitSHL(N);
697 case ISD::SRA: return visitSRA(N);
698 case ISD::SRL: return visitSRL(N);
699 case ISD::CTLZ: return visitCTLZ(N);
700 case ISD::CTTZ: return visitCTTZ(N);
701 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000702 case ISD::SELECT: return visitSELECT(N);
703 case ISD::SELECT_CC: return visitSELECT_CC(N);
704 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000705 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
706 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000707 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000708 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
709 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000710 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000711 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000712 case ISD::FADD: return visitFADD(N);
713 case ISD::FSUB: return visitFSUB(N);
714 case ISD::FMUL: return visitFMUL(N);
715 case ISD::FDIV: return visitFDIV(N);
716 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000717 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
719 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
720 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
721 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
722 case ISD::FP_ROUND: return visitFP_ROUND(N);
723 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
724 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
725 case ISD::FNEG: return visitFNEG(N);
726 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000727 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000728 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000729 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000730 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000731 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000732 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000733 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
734 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000735 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000736 }
Dan Gohman475871a2008-07-27 21:46:04 +0000737 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000738}
739
Dan Gohman475871a2008-07-27 21:46:04 +0000740SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000741
Dan Gohman475871a2008-07-27 21:46:04 +0000742 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000743
744 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000745 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000746 assert(N->getOpcode() != ISD::DELETED_NODE &&
747 "Node was deleted but visit returned NULL!");
748
749 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
750 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
751
752 // Expose the DAG combiner to the target combiner impls.
753 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +0000754 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000755
756 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
757 }
758 }
759
Evan Cheng08b11732008-03-22 01:55:50 +0000760 // If N is a commutative binary node, try commuting it to enable more
761 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000762 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000763 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
764 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000765 SDValue N0 = N->getOperand(0);
766 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000767 // Constant operands are canonicalized to RHS.
768 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000769 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000770 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
771 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000772 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000773 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000774 }
775 }
776
Dan Gohman389079b2007-10-08 17:57:15 +0000777 return RV;
778}
779
Chris Lattner6270f682006-10-08 22:57:01 +0000780/// getInputChainForNode - Given a node, return its input chain if it has one,
781/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000782static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000783 if (unsigned NumOps = N->getNumOperands()) {
784 if (N->getOperand(0).getValueType() == MVT::Other)
785 return N->getOperand(0);
786 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
787 return N->getOperand(NumOps-1);
788 for (unsigned i = 1; i < NumOps-1; ++i)
789 if (N->getOperand(i).getValueType() == MVT::Other)
790 return N->getOperand(i);
791 }
Dan Gohman475871a2008-07-27 21:46:04 +0000792 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000793}
794
Dan Gohman475871a2008-07-27 21:46:04 +0000795SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000796 // If N has two operands, where one has an input chain equal to the other,
797 // the 'other' chain is redundant.
798 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000799 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000800 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000801 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000802 return N->getOperand(1);
803 }
804
Chris Lattnerc76d4412007-05-16 06:37:59 +0000805 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000806 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000807 SmallPtrSet<SDNode*, 16> SeenOps;
808 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000809
810 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000811 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000812
Jim Laskey71382342006-10-07 23:37:56 +0000813 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000814 // encountered.
815 for (unsigned i = 0; i < TFs.size(); ++i) {
816 SDNode *TF = TFs[i];
817
Jim Laskey6ff23e52006-10-04 16:53:27 +0000818 // Check each of the operands.
819 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000820 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000821
Jim Laskey6ff23e52006-10-04 16:53:27 +0000822 switch (Op.getOpcode()) {
823 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000824 // Entry tokens don't need to be added to the list. They are
825 // rededundant.
826 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000828
Jim Laskey6ff23e52006-10-04 16:53:27 +0000829 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000830 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000833 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000835 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000836 Changed = true;
837 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000838 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000839 // Fall thru
840
841 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000842 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000843 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000844 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000845 else
846 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000847 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000848 }
849 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000850 }
851
Dan Gohman475871a2008-07-27 21:46:04 +0000852 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000853
854 // If we've change things around then replace token factor.
855 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000856 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000857 // The entry token is the only possible outcome.
858 Result = DAG.getEntryNode();
859 } else {
860 // New and improved token factor.
861 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000862 }
Jim Laskey274062c2006-10-13 23:32:28 +0000863
864 // Don't add users to work list.
865 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000866 }
Jim Laskey279f0532006-09-25 16:29:54 +0000867
Jim Laskey6ff23e52006-10-04 16:53:27 +0000868 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000869}
870
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000871/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000872SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000873 WorkListRemover DeadNodes(*this);
874 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000875 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000876 &DeadNodes);
877 removeFromWorkList(N);
878 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000879 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000880}
881
882
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000883static
Dan Gohman475871a2008-07-27 21:46:04 +0000884SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000885 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue N00 = N0.getOperand(0);
887 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000888 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000889 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000890 isa<ConstantSDNode>(N00.getOperand(1))) {
891 N0 = DAG.getNode(ISD::ADD, VT,
892 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
893 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
894 return DAG.getNode(ISD::ADD, VT, N0, N1);
895 }
Dan Gohman475871a2008-07-27 21:46:04 +0000896 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000897}
898
Evan Chengb13cdbd2007-06-21 07:39:16 +0000899static
Dan Gohman475871a2008-07-27 21:46:04 +0000900SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000901 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sands25cf2272008-11-24 14:53:14 +0000902 bool LegalOperations) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000903 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000904 unsigned Opc = N->getOpcode();
905 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000906 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
907 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000908 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000909
910 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000911 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000912 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000913 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000914 if (CCOp.getOpcode() == ISD::SETCC)
915 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
916 }
917
918 bool DoXform = false;
919 bool InvCC = false;
920 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
921 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000922
Evan Chengb13cdbd2007-06-21 07:39:16 +0000923 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000924 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000925 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000926 } else if (CC != ISD::SETCC_INVALID &&
927 RHS.getOpcode() == ISD::Constant &&
928 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000929 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000930 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000931 MVT OpVT = isSlctCC ? Op0.getValueType() :
932 Op0.getOperand(0).getValueType();
933 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000934 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000935
Duncan Sands25cf2272008-11-24 14:53:14 +0000936 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendlingae89bb12008-11-11 08:25:46 +0000937 return SDValue(); // Inverse operator isn't legal.
938
Evan Chengb13cdbd2007-06-21 07:39:16 +0000939 DoXform = true;
940 InvCC = true;
941 }
942
943 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000945 if (isSlctCC)
946 return DAG.getSelectCC(OtherOp, Result,
947 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000948 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000949 if (InvCC)
950 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
951 CCOp.getOperand(1), CC);
952 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
953 }
Dan Gohman475871a2008-07-27 21:46:04 +0000954 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000955}
956
Dan Gohman475871a2008-07-27 21:46:04 +0000957SDValue DAGCombiner::visitADD(SDNode *N) {
958 SDValue N0 = N->getOperand(0);
959 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
961 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000963
964 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000965 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000966 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000967 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000968 }
Bill Wendling2476e5d2008-12-10 22:36:00 +0000969
Dan Gohman613e0d82007-07-03 14:03:57 +0000970 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000971 if (N0.getOpcode() == ISD::UNDEF)
972 return N0;
973 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000974 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000975 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000976 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000977 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000978 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000979 if (N0C && !N1C)
980 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000981 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000982 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000983 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000984 // fold (add Sym, c) -> Sym+c
985 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +0000986 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +0000987 GA->getOpcode() == ISD::GlobalAddress)
988 return DAG.getGlobalAddress(GA->getGlobal(), VT,
989 GA->getOffset() +
990 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000991 // fold ((c1-A)+c2) -> (c1+c2)-A
992 if (N1C && N0.getOpcode() == ISD::SUB)
993 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
994 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000995 DAG.getConstant(N1C->getAPIntValue()+
996 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000997 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000998 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +0000999 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001000 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001001 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001002 // fold ((0-A) + B) -> B-A
1003 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1004 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001005 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001006 // fold (A + (0-B)) -> A-B
1007 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1008 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001009 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001010 // fold (A+(B-A)) -> B
1011 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001012 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001013 // fold ((B-A)+A) -> B
1014 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1015 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001016 // fold (A+(B-(A+C))) to (B-C)
1017 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1018 N0 == N1.getOperand(1).getOperand(0)) {
1019 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1020 N1.getOperand(1).getOperand(1));
1021 }
1022 // fold (A+(B-(C+A))) to (B-C)
1023 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1024 N0 == N1.getOperand(1).getOperand(1)) {
1025 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1026 N1.getOperand(1).getOperand(0));
1027 }
Dale Johannesen34d79852008-12-02 18:40:40 +00001028 // fold (A+((B-A)+-C)) to (B+-C)
1029 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1030 N1.getOperand(0).getOpcode() == ISD::SUB &&
1031 N0 == N1.getOperand(0).getOperand(1)) {
1032 return DAG.getNode(N1.getOpcode(), VT, N1.getOperand(0).getOperand(0),
1033 N1.getOperand(1));
1034 }
1035
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001036 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1037 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1038 SDValue N00 = N0.getOperand(0);
1039 SDValue N01 = N0.getOperand(1);
1040 SDValue N10 = N1.getOperand(0);
1041 SDValue N11 = N1.getOperand(1);
1042 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) {
1043 return DAG.getNode(ISD::SUB, VT,
1044 DAG.getNode(ISD::ADD, VT, N00, N10),
1045 DAG.getNode(ISD::ADD, VT, N01, N11));
1046 }
1047 }
Chris Lattner947c2892006-03-13 06:51:27 +00001048
Dan Gohman475871a2008-07-27 21:46:04 +00001049 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1050 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001051
1052 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001053 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001054 APInt LHSZero, LHSOne;
1055 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001056 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001057 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001058 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001059 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001060
1061 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1062 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1063 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1064 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1065 return DAG.getNode(ISD::OR, VT, N0, N1);
1066 }
1067 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001068
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001069 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001070 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001071 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001072 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001073 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001074 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001075 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001076 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001077 }
1078
Evan Chengb13cdbd2007-06-21 07:39:16 +00001079 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001081 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001083 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001084 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001085 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001087 }
1088
Dan Gohman475871a2008-07-27 21:46:04 +00001089 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001090}
1091
Dan Gohman475871a2008-07-27 21:46:04 +00001092SDValue DAGCombiner::visitADDC(SDNode *N) {
1093 SDValue N0 = N->getOperand(0);
1094 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001095 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1096 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001097 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001098
1099 // If the flag result is dead, turn this into an ADD.
1100 if (N->hasNUsesOfValue(0, 1))
1101 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001102 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001103
1104 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001105 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001106 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001107
Chris Lattnerb6541762007-03-04 20:40:38 +00001108 // fold (addc x, 0) -> x + no carry out
1109 if (N1C && N1C->isNullValue())
1110 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1111
1112 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001113 APInt LHSZero, LHSOne;
1114 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001115 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001116 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001117 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001118 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001119
1120 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1121 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1122 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1123 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1124 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1125 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1126 }
Chris Lattner91153682007-03-04 20:03:15 +00001127
Dan Gohman475871a2008-07-27 21:46:04 +00001128 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001129}
1130
Dan Gohman475871a2008-07-27 21:46:04 +00001131SDValue DAGCombiner::visitADDE(SDNode *N) {
1132 SDValue N0 = N->getOperand(0);
1133 SDValue N1 = N->getOperand(1);
1134 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001135 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1136 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001137 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001138
1139 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001140 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001141 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001142
Chris Lattnerb6541762007-03-04 20:40:38 +00001143 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001144 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001145 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001146
Dan Gohman475871a2008-07-27 21:46:04 +00001147 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001148}
1149
1150
1151
Dan Gohman475871a2008-07-27 21:46:04 +00001152SDValue DAGCombiner::visitSUB(SDNode *N) {
1153 SDValue N0 = N->getOperand(0);
1154 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001155 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1156 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001157 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001158
Dan Gohman7f321562007-06-25 16:23:39 +00001159 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001160 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001161 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001162 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001163 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001164
Chris Lattner854077d2005-10-17 01:07:11 +00001165 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001166 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001167 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001169 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001170 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001171 // fold (sub x, c) -> (add x, -c)
1172 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001173 return DAG.getNode(ISD::ADD, VT, N0,
1174 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001176 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001179 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001180 return N0.getOperand(0);
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001181 // fold ((A+(B-C))-B) -> A-C
1182 if (N0.getOpcode() == ISD::ADD &&
1183 N0.getOperand(1).getOpcode() == ISD::SUB &&
1184 N0.getOperand(1).getOperand(0) == N1)
1185 return DAG.getNode(ISD::SUB, VT, N0.getOperand(0),
1186 N0.getOperand(1).getOperand(1));
Evan Chengb13cdbd2007-06-21 07:39:16 +00001187 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001188 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001189 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001190 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001191 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001192 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001193 if (N0.getOpcode() == ISD::UNDEF)
1194 return N0;
1195 if (N1.getOpcode() == ISD::UNDEF)
1196 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001197
Dan Gohman6520e202008-10-18 02:06:02 +00001198 // If the relocation model supports it, consider symbol offsets.
1199 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001200 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001201 // fold (sub Sym, c) -> Sym-c
1202 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1203 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1204 GA->getOffset() -
1205 (uint64_t)N1C->getSExtValue());
1206 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1207 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1208 if (GA->getGlobal() == GB->getGlobal())
1209 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1210 VT);
1211 }
1212
Dan Gohman475871a2008-07-27 21:46:04 +00001213 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001214}
1215
Dan Gohman475871a2008-07-27 21:46:04 +00001216SDValue DAGCombiner::visitMUL(SDNode *N) {
1217 SDValue N0 = N->getOperand(0);
1218 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001219 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1220 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001221 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222
Dan Gohman7f321562007-06-25 16:23:39 +00001223 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001224 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001226 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001227 }
Dan Gohman7f321562007-06-25 16:23:39 +00001228
Dan Gohman613e0d82007-07-03 14:03:57 +00001229 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001230 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001231 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001233 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001234 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001235 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001236 if (N0C && !N1C)
1237 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001239 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001240 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001241 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001242 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001243 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001244 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001245 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001246 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001247 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001248 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001249 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001250 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001251 // FIXME: If the input is something that is easily negated (e.g. a
1252 // single-use add), we should put the negate there.
1253 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1254 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001255 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001256 TLI.getShiftAmountTy())));
1257 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001258
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001259 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1260 if (N1C && N0.getOpcode() == ISD::SHL &&
1261 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001263 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001264 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1265 }
1266
1267 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1268 // use.
1269 {
Dan Gohman475871a2008-07-27 21:46:04 +00001270 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001271 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1272 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001273 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001274 Sh = N0; Y = N1;
1275 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001276 isa<ConstantSDNode>(N1.getOperand(1)) &&
1277 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001278 Sh = N1; Y = N0;
1279 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001281 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001282 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1283 }
1284 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001285 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001287 isa<ConstantSDNode>(N0.getOperand(1))) {
1288 return DAG.getNode(ISD::ADD, VT,
1289 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1290 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1291 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001292
Nate Begemancd4d58c2006-02-03 06:46:56 +00001293 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001294 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001295 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001296 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001297
Dan Gohman475871a2008-07-27 21:46:04 +00001298 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001299}
1300
Dan Gohman475871a2008-07-27 21:46:04 +00001301SDValue DAGCombiner::visitSDIV(SDNode *N) {
1302 SDValue N0 = N->getOperand(0);
1303 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001304 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1305 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001306 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001307
Dan Gohman7f321562007-06-25 16:23:39 +00001308 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001309 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001310 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001311 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001312 }
Dan Gohman7f321562007-06-25 16:23:39 +00001313
Nate Begeman1d4d4142005-09-01 00:19:25 +00001314 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001315 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001316 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001317 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001318 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001319 return N0;
1320 // fold (sdiv X, -1) -> 0-X
1321 if (N1C && N1C->isAllOnesValue())
1322 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001323 // If we know the sign bits of both operands are zero, strength reduce to a
1324 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001325 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001326 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001327 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1328 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001329 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001330 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001331 (isPowerOf2_64(N1C->getSExtValue()) ||
1332 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001333 // If dividing by powers of two is cheap, then don't perform the following
1334 // fold.
1335 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001336 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001337 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001338 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001339 unsigned lg2 = Log2_64(abs2);
1340 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001341 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001342 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001343 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001344 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001345 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001346 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001347 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001348 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001350 AddToWorkList(SRL.getNode());
1351 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001353 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001354 // If we're dividing by a positive value, we're done. Otherwise, we must
1355 // negate the result.
1356 if (pow2 > 0)
1357 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001358 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001359 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1360 }
Nate Begeman69575232005-10-20 02:15:44 +00001361 // if integer divide is expensive and we satisfy the requirements, emit an
1362 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001363 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001364 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001366 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001367 }
Dan Gohman7f321562007-06-25 16:23:39 +00001368
Dan Gohman613e0d82007-07-03 14:03:57 +00001369 // undef / X -> 0
1370 if (N0.getOpcode() == ISD::UNDEF)
1371 return DAG.getConstant(0, VT);
1372 // X / undef -> undef
1373 if (N1.getOpcode() == ISD::UNDEF)
1374 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001375
Dan Gohman475871a2008-07-27 21:46:04 +00001376 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001377}
1378
Dan Gohman475871a2008-07-27 21:46:04 +00001379SDValue DAGCombiner::visitUDIV(SDNode *N) {
1380 SDValue N0 = N->getOperand(0);
1381 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001382 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1383 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001384 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001385
Dan Gohman7f321562007-06-25 16:23:39 +00001386 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001387 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001390 }
Dan Gohman7f321562007-06-25 16:23:39 +00001391
Nate Begeman1d4d4142005-09-01 00:19:25 +00001392 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001394 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001395 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001396 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001397 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001398 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001399 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001400 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1401 if (N1.getOpcode() == ISD::SHL) {
1402 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001403 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001404 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001405 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001406 DAG.getConstant(SHC->getAPIntValue()
1407 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001408 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001409 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001410 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001411 }
1412 }
1413 }
Nate Begeman69575232005-10-20 02:15:44 +00001414 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001415 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001416 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001417 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001418 }
Dan Gohman7f321562007-06-25 16:23:39 +00001419
Dan Gohman613e0d82007-07-03 14:03:57 +00001420 // undef / X -> 0
1421 if (N0.getOpcode() == ISD::UNDEF)
1422 return DAG.getConstant(0, VT);
1423 // X / undef -> undef
1424 if (N1.getOpcode() == ISD::UNDEF)
1425 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001426
Dan Gohman475871a2008-07-27 21:46:04 +00001427 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001428}
1429
Dan Gohman475871a2008-07-27 21:46:04 +00001430SDValue DAGCombiner::visitSREM(SDNode *N) {
1431 SDValue N0 = N->getOperand(0);
1432 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001433 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1434 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001435 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001436
1437 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001438 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001439 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001440 // If we know the sign bits of both operands are zero, strength reduce to a
1441 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001442 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001443 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001444 return DAG.getNode(ISD::UREM, VT, N0, N1);
1445 }
Chris Lattner26d29902006-10-12 20:58:32 +00001446
Dan Gohman77003042007-11-26 23:46:11 +00001447 // If X/C can be simplified by the division-by-constant logic, lower
1448 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001449 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001451 AddToWorkList(Div.getNode());
1452 SDValue OptimizedDiv = combine(Div.getNode());
1453 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001454 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1455 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001456 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001457 return Sub;
1458 }
Chris Lattner26d29902006-10-12 20:58:32 +00001459 }
1460
Dan Gohman613e0d82007-07-03 14:03:57 +00001461 // undef % X -> 0
1462 if (N0.getOpcode() == ISD::UNDEF)
1463 return DAG.getConstant(0, VT);
1464 // X % undef -> undef
1465 if (N1.getOpcode() == ISD::UNDEF)
1466 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001467
Dan Gohman475871a2008-07-27 21:46:04 +00001468 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469}
1470
Dan Gohman475871a2008-07-27 21:46:04 +00001471SDValue DAGCombiner::visitUREM(SDNode *N) {
1472 SDValue N0 = N->getOperand(0);
1473 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001474 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1475 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001476 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477
1478 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001479 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001480 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001481 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001482 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1483 return DAG.getNode(ISD::AND, VT, N0,
1484 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001485 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1486 if (N1.getOpcode() == ISD::SHL) {
1487 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001488 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001489 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001490 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001491 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001492 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001493 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001494 return DAG.getNode(ISD::AND, VT, N0, Add);
1495 }
1496 }
1497 }
Chris Lattner26d29902006-10-12 20:58:32 +00001498
Dan Gohman77003042007-11-26 23:46:11 +00001499 // If X/C can be simplified by the division-by-constant logic, lower
1500 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001501 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001502 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001503 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001504 SDValue OptimizedDiv = combine(Div.getNode());
1505 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001506 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1507 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001508 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001509 return Sub;
1510 }
Chris Lattner26d29902006-10-12 20:58:32 +00001511 }
1512
Dan Gohman613e0d82007-07-03 14:03:57 +00001513 // undef % X -> 0
1514 if (N0.getOpcode() == ISD::UNDEF)
1515 return DAG.getConstant(0, VT);
1516 // X % undef -> undef
1517 if (N1.getOpcode() == ISD::UNDEF)
1518 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001519
Dan Gohman475871a2008-07-27 21:46:04 +00001520 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001521}
1522
Dan Gohman475871a2008-07-27 21:46:04 +00001523SDValue DAGCombiner::visitMULHS(SDNode *N) {
1524 SDValue N0 = N->getOperand(0);
1525 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001526 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001527 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001528
1529 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001530 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001531 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001532 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001533 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001534 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001535 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001536 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001537 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001538 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001539 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001540
Dan Gohman475871a2008-07-27 21:46:04 +00001541 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001542}
1543
Dan Gohman475871a2008-07-27 21:46:04 +00001544SDValue DAGCombiner::visitMULHU(SDNode *N) {
1545 SDValue N0 = N->getOperand(0);
1546 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001547 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001548 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001549
1550 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001551 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001552 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001553 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001554 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001555 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001556 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001557 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001558 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001559
Dan Gohman475871a2008-07-27 21:46:04 +00001560 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001561}
1562
Dan Gohman389079b2007-10-08 17:57:15 +00001563/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1564/// compute two values. LoOp and HiOp give the opcodes for the two computations
1565/// that are being performed. Return true if a simplification was made.
1566///
Dan Gohman475871a2008-07-27 21:46:04 +00001567SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1568 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001569 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001570 bool HiExists = N->hasAnyUseOfValue(1);
1571 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001572 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001573 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001574 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001575 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001576 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001577 }
1578
1579 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001580 bool LoExists = N->hasAnyUseOfValue(0);
1581 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001582 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001583 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001584 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001585 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001586 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001587 }
1588
Evan Cheng44711942007-11-08 09:25:29 +00001589 // If both halves are used, return as it is.
1590 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001591 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001592
1593 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001594 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001595 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001596 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001597 AddToWorkList(Lo.getNode());
1598 SDValue LoOpt = combine(Lo.getNode());
1599 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001600 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001601 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001602 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001603 }
1604
Evan Cheng44711942007-11-08 09:25:29 +00001605 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001606 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001607 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001608 AddToWorkList(Hi.getNode());
1609 SDValue HiOpt = combine(Hi.getNode());
1610 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001611 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001612 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001613 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001614 }
Dan Gohman475871a2008-07-27 21:46:04 +00001615 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001616}
1617
Dan Gohman475871a2008-07-27 21:46:04 +00001618SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1619 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001620 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001621
Dan Gohman475871a2008-07-27 21:46:04 +00001622 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001623}
1624
Dan Gohman475871a2008-07-27 21:46:04 +00001625SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1626 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001627 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001628
Dan Gohman475871a2008-07-27 21:46:04 +00001629 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001630}
1631
Dan Gohman475871a2008-07-27 21:46:04 +00001632SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1633 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001634 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001635
Dan Gohman475871a2008-07-27 21:46:04 +00001636 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001637}
1638
Dan Gohman475871a2008-07-27 21:46:04 +00001639SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1640 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001641 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001642
Dan Gohman475871a2008-07-27 21:46:04 +00001643 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001644}
1645
Chris Lattner35e5c142006-05-05 05:51:50 +00001646/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1647/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001648SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1649 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001650 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001651 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1652
Chris Lattner540121f2006-05-05 06:31:05 +00001653 // For each of OP in AND/OR/XOR:
1654 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1655 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1656 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001657 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001658 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001659 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001660 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001661 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001662 N0.getOperand(0).getValueType(),
1663 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001664 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001665 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001666 }
1667
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001668 // For each of OP in SHL/SRL/SRA/AND...
1669 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1670 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1671 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001672 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001673 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001674 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001675 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001676 N0.getOperand(0).getValueType(),
1677 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001678 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001679 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1680 }
1681
Dan Gohman475871a2008-07-27 21:46:04 +00001682 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001683}
1684
Dan Gohman475871a2008-07-27 21:46:04 +00001685SDValue DAGCombiner::visitAND(SDNode *N) {
1686 SDValue N0 = N->getOperand(0);
1687 SDValue N1 = N->getOperand(1);
1688 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001689 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1690 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001691 MVT VT = N1.getValueType();
1692 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001693
Dan Gohman7f321562007-06-25 16:23:39 +00001694 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001695 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001696 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001697 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001698 }
Dan Gohman7f321562007-06-25 16:23:39 +00001699
Dan Gohman613e0d82007-07-03 14:03:57 +00001700 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001701 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001702 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001703 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001704 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001705 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001706 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001707 if (N0C && !N1C)
1708 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001709 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001710 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001711 return N0;
1712 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001713 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001714 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001715 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001716 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001717 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001718 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001719 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001720 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001721 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001722 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001723 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001724 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001725 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1726 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001727 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001728 APInt Mask = ~N1C->getAPIntValue();
1729 Mask.trunc(N0Op0.getValueSizeInBits());
1730 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001731 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001732 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001733
1734 // Replace uses of the AND with uses of the Zero extend node.
1735 CombineTo(N, Zext);
1736
Chris Lattner3603cd62006-02-02 07:17:31 +00001737 // We actually want to replace all uses of the any_extend with the
1738 // zero_extend, to avoid duplicating things. This will later cause this
1739 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001740 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001741 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001742 }
1743 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001744 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1745 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1746 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1747 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1748
1749 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001750 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001751 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001752 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001753 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001754 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001755 return DAG.getSetCC(VT, ORNode, LR, Op1);
1756 }
1757 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1758 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001759 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001760 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001761 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1762 }
1763 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1764 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001765 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001766 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001767 return DAG.getSetCC(VT, ORNode, LR, Op1);
1768 }
1769 }
1770 // canonicalize equivalent to ll == rl
1771 if (LL == RR && LR == RL) {
1772 Op1 = ISD::getSetCCSwappedOperands(Op1);
1773 std::swap(RL, RR);
1774 }
1775 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001776 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001777 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001778 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001779 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001780 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1781 }
1782 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001783
1784 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1785 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001786 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001787 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001788 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001789
Nate Begemande996292006-02-03 22:24:05 +00001790 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1791 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001792 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001793 SimplifyDemandedBits(SDValue(N, 0)))
1794 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001795 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001796 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001797 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001798 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001799 // If we zero all the possible extended bits, then we can turn this into
1800 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001801 unsigned BitWidth = N1.getValueSizeInBits();
1802 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001803 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001804 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001805 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001806 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001807 LN0->getBasePtr(), LN0->getSrcValue(),
1808 LN0->getSrcValueOffset(), EVT,
1809 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001810 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001811 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001812 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001813 }
1814 }
1815 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001816 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001817 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001818 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001819 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001820 // If we zero all the possible extended bits, then we can turn this into
1821 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001822 unsigned BitWidth = N1.getValueSizeInBits();
1823 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001824 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001825 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001826 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001828 LN0->getBasePtr(), LN0->getSrcValue(),
1829 LN0->getSrcValueOffset(), EVT,
1830 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001831 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001832 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001833 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001834 }
1835 }
Chris Lattner15045b62006-02-28 06:35:35 +00001836
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001837 // fold (and (load x), 255) -> (zextload x, i8)
1838 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001839 if (N1C && N0.getOpcode() == ISD::LOAD) {
1840 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1841 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001842 LN0->isUnindexed() && N0.hasOneUse() &&
1843 // Do not change the width of a volatile load.
1844 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001845 MVT EVT = MVT::Other;
1846 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1847 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1848 EVT = MVT::getIntegerVT(ActiveBits);
1849
1850 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001851 // Do not generate loads of non-round integer types since these can
1852 // be expensive (and would be wrong if the type is not byte sized).
1853 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001854 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001855 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001856 // For big endian targets, we need to add an offset to the pointer to
1857 // load the correct bytes. For little endian systems, we merely need to
1858 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001859 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1860 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001861 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001862 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001863 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001864 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001865 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1866 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001867 Alignment = MinAlign(Alignment, PtrOff);
1868 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001869 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001870 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001871 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001872 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001873 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001874 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001875 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001876 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001877 }
Chris Lattner15045b62006-02-28 06:35:35 +00001878 }
1879 }
1880
Dan Gohman475871a2008-07-27 21:46:04 +00001881 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001882}
1883
Dan Gohman475871a2008-07-27 21:46:04 +00001884SDValue DAGCombiner::visitOR(SDNode *N) {
1885 SDValue N0 = N->getOperand(0);
1886 SDValue N1 = N->getOperand(1);
1887 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001888 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1889 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001890 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001891
Dan Gohman7f321562007-06-25 16:23:39 +00001892 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001894 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001895 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001896 }
Dan Gohman7f321562007-06-25 16:23:39 +00001897
Dan Gohman613e0d82007-07-03 14:03:57 +00001898 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001899 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001900 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001901 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001902 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001903 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001904 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001905 if (N0C && !N1C)
1906 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001907 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001908 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001909 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001911 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001912 return N1;
1913 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001914 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001915 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001916 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001917 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001918 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001919 return ROR;
1920 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001921 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001922 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001923 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1924 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1925 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001926 DAG.getConstant(N1C->getAPIntValue() |
1927 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001928 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001929 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1930 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1931 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1932 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1933
1934 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001935 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001936 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1937 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001938 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001939 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001942 return DAG.getSetCC(VT, ORNode, LR, Op1);
1943 }
1944 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1945 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1946 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1947 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001948 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001949 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001950 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1951 }
1952 }
1953 // canonicalize equivalent to ll == rl
1954 if (LL == RR && LR == RL) {
1955 Op1 = ISD::getSetCCSwappedOperands(Op1);
1956 std::swap(RL, RR);
1957 }
1958 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001959 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001960 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001961 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001962 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001963 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1964 }
1965 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001966
1967 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1968 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001970 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001971 }
Chris Lattner516b9622006-09-14 20:50:57 +00001972
Chris Lattner1ec72732006-09-14 21:11:37 +00001973 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1974 if (N0.getOpcode() == ISD::AND &&
1975 N1.getOpcode() == ISD::AND &&
1976 N0.getOperand(1).getOpcode() == ISD::Constant &&
1977 N1.getOperand(1).getOpcode() == ISD::Constant &&
1978 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00001979 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001980 // We can only do this xform if we know that bits from X that are set in C2
1981 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001982 const APInt &LHSMask =
1983 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1984 const APInt &RHSMask =
1985 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001986
Dan Gohmanea859be2007-06-22 14:59:07 +00001987 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1988 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001989 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001990 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1991 }
1992 }
1993
1994
Chris Lattner516b9622006-09-14 20:50:57 +00001995 // See if this is some rotate idiom.
1996 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001997 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001998
Dan Gohman475871a2008-07-27 21:46:04 +00001999 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002000}
2001
Chris Lattner516b9622006-09-14 20:50:57 +00002002
2003/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002004static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002005 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002006 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002007 Mask = Op.getOperand(1);
2008 Op = Op.getOperand(0);
2009 } else {
2010 return false;
2011 }
2012 }
2013
2014 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2015 Shift = Op;
2016 return true;
2017 }
2018 return false;
2019}
2020
2021
2022// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2023// idioms for rotate, and if the target supports rotation instructions, generate
2024// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00002025SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002026 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002027 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002028 if (!TLI.isTypeLegal(VT)) return 0;
2029
2030 // The target must have at least one rotate flavor.
2031 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
2032 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
2033 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002034
Chris Lattner516b9622006-09-14 20:50:57 +00002035 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002036 SDValue LHSShift; // The shift.
2037 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002038 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2039 return 0; // Not part of a rotate.
2040
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SDValue RHSShift; // The shift.
2042 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002043 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2044 return 0; // Not part of a rotate.
2045
2046 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2047 return 0; // Not shifting the same value.
2048
2049 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2050 return 0; // Shifts must disagree.
2051
2052 // Canonicalize shl to left side in a shl/srl pair.
2053 if (RHSShift.getOpcode() == ISD::SHL) {
2054 std::swap(LHS, RHS);
2055 std::swap(LHSShift, RHSShift);
2056 std::swap(LHSMask , RHSMask );
2057 }
2058
Duncan Sands83ec4b62008-06-06 12:08:01 +00002059 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002060 SDValue LHSShiftArg = LHSShift.getOperand(0);
2061 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2062 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002063
2064 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2065 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002066 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2067 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002068 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2069 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002070 if ((LShVal + RShVal) != OpSizeInBits)
2071 return 0;
2072
Dan Gohman475871a2008-07-27 21:46:04 +00002073 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002074 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002075 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002076 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002077 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002078
2079 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002080 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002081 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002082
Gabor Greifba36cb52008-08-28 21:40:38 +00002083 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002084 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2085 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002086 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002087 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002088 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2089 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002090 }
2091
2092 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2093 }
2094
Gabor Greifba36cb52008-08-28 21:40:38 +00002095 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002096 }
2097
2098 // If there is a mask here, and we have a variable shift, we can't be sure
2099 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002100 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002101 return 0;
2102
2103 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2104 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002105 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2106 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002107 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002108 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002109 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002110 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002111 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002112 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002113 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002114 }
Chris Lattner516b9622006-09-14 20:50:57 +00002115 }
2116 }
2117
2118 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2119 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002120 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2121 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002122 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002123 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002124 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002125 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002126 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002127 else
2128 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002129 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002130 }
2131 }
2132
Dan Gohman74feef22008-10-17 01:23:35 +00002133 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002134 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2135 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002136 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2137 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002138 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2139 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002140 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2141 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002142 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2143 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002144 if (RExtOp0.getOpcode() == ISD::SUB &&
2145 RExtOp0.getOperand(1) == LExtOp0) {
2146 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002147 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002148 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002149 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002150 if (ConstantSDNode *SUBC =
2151 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002152 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002153 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2154 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002155 }
2156 }
2157 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2158 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002159 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002160 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002161 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002162 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002163 if (ConstantSDNode *SUBC =
2164 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002165 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002166 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2167 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002168 }
2169 }
Chris Lattner516b9622006-09-14 20:50:57 +00002170 }
2171 }
2172
2173 return 0;
2174}
2175
2176
Dan Gohman475871a2008-07-27 21:46:04 +00002177SDValue DAGCombiner::visitXOR(SDNode *N) {
2178 SDValue N0 = N->getOperand(0);
2179 SDValue N1 = N->getOperand(1);
2180 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002181 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2182 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002183 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184
Dan Gohman7f321562007-06-25 16:23:39 +00002185 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002186 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002187 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002188 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002189 }
Dan Gohman7f321562007-06-25 16:23:39 +00002190
Evan Cheng26471c42008-03-25 20:08:07 +00002191 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2192 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2193 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002194 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002195 if (N0.getOpcode() == ISD::UNDEF)
2196 return N0;
2197 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002198 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002199 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002200 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002201 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002202 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002203 if (N0C && !N1C)
2204 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002205 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002206 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002207 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002208 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002209 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002210 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002211 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002212
Nate Begeman1d4d4142005-09-01 00:19:25 +00002213 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002214 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002215 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002216 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2217 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002218
Duncan Sands25cf2272008-11-24 14:53:14 +00002219 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002220 switch (N0.getOpcode()) {
2221 default:
2222 assert(0 && "Unhandled SetCC Equivalent!");
2223 abort();
2224 case ISD::SETCC:
2225 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2226 case ISD::SELECT_CC:
2227 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2228 N0.getOperand(3), NotCC);
2229 }
2230 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002231 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002232
Chris Lattner61c5ff42007-09-10 21:39:07 +00002233 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002234 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002235 N0.getNode()->hasOneUse() &&
2236 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002237 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002238 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002239 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002240 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002241 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2242 }
2243
Nate Begeman99801192005-09-07 23:25:52 +00002244 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002245 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002246 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002247 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002248 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2249 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002250 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2251 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002252 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002253 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002254 }
2255 }
Nate Begeman99801192005-09-07 23:25:52 +00002256 // fold !(x or y) -> (!x and !y) iff x or y are constants
2257 if (N1C && N1C->isAllOnesValue() &&
2258 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002259 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002260 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2261 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002262 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2263 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002264 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002265 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002266 }
2267 }
Nate Begeman223df222005-09-08 20:18:10 +00002268 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2269 if (N1C && N0.getOpcode() == ISD::XOR) {
2270 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2271 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2272 if (N00C)
2273 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002274 DAG.getConstant(N1C->getAPIntValue()^
2275 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002276 if (N01C)
2277 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002278 DAG.getConstant(N1C->getAPIntValue()^
2279 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002280 }
2281 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002282 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002283 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002284 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002285 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002286 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002287 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2288 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002289 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002290 }
2291 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002292
2293 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2294 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002295 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002296 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002297 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002298
Chris Lattner3e104b12006-04-08 04:15:24 +00002299 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002300 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002301 SimplifyDemandedBits(SDValue(N, 0)))
2302 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002303
Dan Gohman475871a2008-07-27 21:46:04 +00002304 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002305}
2306
Chris Lattnere70da202007-12-06 07:33:36 +00002307/// visitShiftByConstant - Handle transforms common to the three shifts, when
2308/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002309SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002310 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002311 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002312
2313 // We want to pull some binops through shifts, so that we have (and (shift))
2314 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2315 // thing happens with address calculations, so it's important to canonicalize
2316 // it.
2317 bool HighBitSet = false; // Can we transform this if the high bit is set?
2318
2319 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002320 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002321 case ISD::OR:
2322 case ISD::XOR:
2323 HighBitSet = false; // We can only transform sra if the high bit is clear.
2324 break;
2325 case ISD::AND:
2326 HighBitSet = true; // We can only transform sra if the high bit is set.
2327 break;
2328 case ISD::ADD:
2329 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002330 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002331 HighBitSet = false; // We can only transform sra if the high bit is clear.
2332 break;
2333 }
2334
2335 // We require the RHS of the binop to be a constant as well.
2336 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002337 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002338
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002339
2340 // FIXME: disable this for unless the input to the binop is a shift by a
2341 // constant. If it is not a shift, it pessimizes some common cases like:
2342 //
2343 //void foo(int *X, int i) { X[i & 1235] = 1; }
2344 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002345 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002346 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2347 BinOpLHSVal->getOpcode() != ISD::SRA &&
2348 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2349 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002350 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002351
Duncan Sands83ec4b62008-06-06 12:08:01 +00002352 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002353
2354 // If this is a signed shift right, and the high bit is modified
2355 // by the logical operation, do not perform the transformation.
2356 // The highBitSet boolean indicates the value of the high bit of
2357 // the constant which would cause it to be modified for this
2358 // operation.
2359 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002360 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2361 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002362 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002363 }
2364
2365 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002366 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002367 LHS->getOperand(1), N->getOperand(1));
2368
2369 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002370 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002371 N->getOperand(1));
2372
2373 // Create the new binop.
2374 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2375}
2376
2377
Dan Gohman475871a2008-07-27 21:46:04 +00002378SDValue DAGCombiner::visitSHL(SDNode *N) {
2379 SDValue N0 = N->getOperand(0);
2380 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002381 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2382 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002383 MVT VT = N0.getValueType();
2384 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002385
2386 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002387 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002388 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002389 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002390 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002391 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002392 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002393 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002394 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002395 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002396 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002397 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002398 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002399 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002400 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002401 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002402 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2403 // iff (trunc c) == c
2404 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002405 N1.getOperand(0).getOpcode() == ISD::AND &&
2406 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002407 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002408 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002409 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002410 SDValue N100 = N1.getOperand(0).getOperand(0);
2411 return DAG.getNode(ISD::SHL, VT, N0,
2412 DAG.getNode(ISD::AND, TruncVT,
2413 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2414 DAG.getConstant(N101C->getZExtValue(),
2415 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002416 }
2417 }
2418
Dan Gohman475871a2008-07-27 21:46:04 +00002419 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2420 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002422 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002424 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2425 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002426 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002427 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002428 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002430 }
2431 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2432 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002433 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002434 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002435 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2436 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002437 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002438 DAG.getConstant(~0ULL << c1, VT));
2439 if (c2 > c1)
2440 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002441 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002443 return DAG.getNode(ISD::SRL, VT, Mask,
2444 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 }
2446 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002447 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002448 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002449 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002450
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002451 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002452}
2453
Dan Gohman475871a2008-07-27 21:46:04 +00002454SDValue DAGCombiner::visitSRA(SDNode *N) {
2455 SDValue N0 = N->getOperand(0);
2456 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002457 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2458 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002459 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002460
2461 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002462 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002463 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002465 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002467 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002468 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002469 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002470 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002471 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002472 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002473 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002474 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002475 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002476 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2477 // sext_inreg.
2478 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002479 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002480 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002481 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002482 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2483 DAG.getValueType(EVT));
2484 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002485
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002486 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2487 if (N1C && N0.getOpcode() == ISD::SRA) {
2488 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002489 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002490 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002491 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2492 DAG.getConstant(Sum, N1C->getValueType(0)));
2493 }
2494 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002495
2496 // fold sra (shl X, m), result_size - n
2497 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002498 // result_size - n != m.
2499 // If truncate is free for the target sext(shl) is likely to result in better
2500 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002501 if (N0.getOpcode() == ISD::SHL) {
2502 // Get the two constanst of the shifts, CN0 = m, CN = n.
2503 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2504 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002505 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002506 unsigned VTValSize = VT.getSizeInBits();
2507 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002508 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002509 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002510 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002511
Christopher Lambb9b04282008-03-20 04:31:39 +00002512 // If the shift is not a no-op (in which case this should be just a sign
2513 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002514 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002515 // perform the transform.
Duncan Sands25cf2272008-11-24 14:53:14 +00002516 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002517 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2518 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002519 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002520
Dan Gohman475871a2008-07-27 21:46:04 +00002521 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2522 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2523 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002524 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002525 }
2526 }
2527 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002528
Evan Chengeb9f8922008-08-30 02:03:58 +00002529 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2530 // iff (trunc c) == c
2531 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002532 N1.getOperand(0).getOpcode() == ISD::AND &&
2533 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002534 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002535 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002536 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002537 SDValue N100 = N1.getOperand(0).getOperand(0);
2538 return DAG.getNode(ISD::SRA, VT, N0,
2539 DAG.getNode(ISD::AND, TruncVT,
2540 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2541 DAG.getConstant(N101C->getZExtValue(),
2542 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002543 }
2544 }
2545
Chris Lattnera8504462006-05-08 20:51:54 +00002546 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002547 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2548 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002549
2550
Nate Begeman1d4d4142005-09-01 00:19:25 +00002551 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002552 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002553 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002554
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002555 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002556}
2557
Dan Gohman475871a2008-07-27 21:46:04 +00002558SDValue DAGCombiner::visitSRL(SDNode *N) {
2559 SDValue N0 = N->getOperand(0);
2560 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002561 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2562 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002563 MVT VT = N0.getValueType();
2564 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002565
2566 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002567 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002568 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002570 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002571 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002572 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002573 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002574 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002575 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002576 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002577 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002578 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002579 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002580 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002581 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002582
Nate Begeman1d4d4142005-09-01 00:19:25 +00002583 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002584 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002585 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002586 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2587 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002588 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002589 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002590 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002591 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002592 }
Chris Lattner350bec02006-04-02 06:11:11 +00002593
Chris Lattner06afe072006-05-05 22:53:17 +00002594 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2595 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2596 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002597 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002598 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002599 return DAG.getNode(ISD::UNDEF, VT);
2600
Dan Gohman475871a2008-07-27 21:46:04 +00002601 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002602 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002603 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2604 }
2605
Chris Lattner3657ffe2006-10-12 20:23:19 +00002606 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2607 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002608 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002609 if (N0.getOpcode() == ISD::SRA)
2610 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2611 }
2612
Chris Lattner350bec02006-04-02 06:11:11 +00002613 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2614 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002616 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002617 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002618 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002619
2620 // If any of the input bits are KnownOne, then the input couldn't be all
2621 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002622 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002623
2624 // If all of the bits input the to ctlz node are known to be zero, then
2625 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002626 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002627 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2628
2629 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2630 if ((UnknownBits & (UnknownBits-1)) == 0) {
2631 // Okay, we know that only that the single bit specified by UnknownBits
2632 // could be set on input to the CTLZ node. If this bit is set, the SRL
2633 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2634 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002635 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002636 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002637 if (ShAmt) {
2638 Op = DAG.getNode(ISD::SRL, VT, Op,
2639 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002640 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002641 }
2642 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2643 }
2644 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002645
2646 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2647 // iff (trunc c) == c
2648 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002649 N1.getOperand(0).getOpcode() == ISD::AND &&
2650 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002651 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002652 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002653 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002654 SDValue N100 = N1.getOperand(0).getOperand(0);
2655 return DAG.getNode(ISD::SRL, VT, N0,
2656 DAG.getNode(ISD::AND, TruncVT,
2657 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2658 DAG.getConstant(N101C->getZExtValue(),
2659 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002660 }
2661 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002662
2663 // fold operands of srl based on knowledge that the low bits are not
2664 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002665 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2666 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002667
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002668 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002669}
2670
Dan Gohman475871a2008-07-27 21:46:04 +00002671SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2672 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002673 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002674
2675 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002676 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002677 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002678 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002679}
2680
Dan Gohman475871a2008-07-27 21:46:04 +00002681SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2682 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002683 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002684
2685 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002686 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002687 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002688 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002689}
2690
Dan Gohman475871a2008-07-27 21:46:04 +00002691SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2692 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002693 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002694
2695 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002696 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002697 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002698 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002699}
2700
Dan Gohman475871a2008-07-27 21:46:04 +00002701SDValue DAGCombiner::visitSELECT(SDNode *N) {
2702 SDValue N0 = N->getOperand(0);
2703 SDValue N1 = N->getOperand(1);
2704 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002705 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2706 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2707 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002708 MVT VT = N->getValueType(0);
2709 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002710
Nate Begeman452d7be2005-09-16 00:54:12 +00002711 // fold select C, X, X -> X
2712 if (N1 == N2)
2713 return N1;
2714 // fold select true, X, Y -> X
2715 if (N0C && !N0C->isNullValue())
2716 return N1;
2717 // fold select false, X, Y -> Y
2718 if (N0C && N0C->isNullValue())
2719 return N2;
2720 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002721 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002722 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002723 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002724 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002725 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002726 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002727 if (VT == VT0)
2728 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002729 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002730 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002731 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2732 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2733 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002734 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002735 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002736 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002737 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002738 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2739 }
2740 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002741 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002743 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002744 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2745 }
2746 // fold select C, X, 0 -> C & X
2747 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002748 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002749 return DAG.getNode(ISD::AND, VT, N0, N1);
2750 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002751 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002752 return DAG.getNode(ISD::OR, VT, N0, N2);
2753 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002754 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002755 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002756
Chris Lattner40c62d52005-10-18 06:04:22 +00002757 // If we can fold this based on the true/false value, do so.
2758 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002759 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002760
Nate Begeman44728a72005-09-19 22:34:01 +00002761 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002762 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002763 // FIXME:
2764 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2765 // having to say they don't support SELECT_CC on every type the DAG knows
2766 // about, since there is no way to mark an opcode illegal at all value types
2767 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2768 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2769 N1, N2, N0.getOperand(2));
2770 else
2771 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002772 }
Dan Gohman475871a2008-07-27 21:46:04 +00002773 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002774}
2775
Dan Gohman475871a2008-07-27 21:46:04 +00002776SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2777 SDValue N0 = N->getOperand(0);
2778 SDValue N1 = N->getOperand(1);
2779 SDValue N2 = N->getOperand(2);
2780 SDValue N3 = N->getOperand(3);
2781 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002782 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2783
Nate Begeman44728a72005-09-19 22:34:01 +00002784 // fold select_cc lhs, rhs, x, x, cc -> x
2785 if (N2 == N3)
2786 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002787
Chris Lattner5f42a242006-09-20 06:19:26 +00002788 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002789 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002790 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002791
Gabor Greifba36cb52008-08-28 21:40:38 +00002792 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002793 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002794 return N2; // cond always true -> true val
2795 else
2796 return N3; // cond always false -> false val
2797 }
2798
2799 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002800 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002801 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2802 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2803 SCC.getOperand(2));
2804
Chris Lattner40c62d52005-10-18 06:04:22 +00002805 // If we can fold this based on the true/false value, do so.
2806 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002807 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002808
Nate Begeman44728a72005-09-19 22:34:01 +00002809 // fold select_cc into other things, such as min/max/abs
2810 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002811}
2812
Dan Gohman475871a2008-07-27 21:46:04 +00002813SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002814 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2815 cast<CondCodeSDNode>(N->getOperand(2))->get());
2816}
2817
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002818// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2819// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2820// transformation. Returns true if extension are possible and the above
2821// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002822static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002823 unsigned ExtOpc,
2824 SmallVector<SDNode*, 4> &ExtendNodes,
2825 TargetLowering &TLI) {
2826 bool HasCopyToRegUses = false;
2827 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002828 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2829 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002830 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002831 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002832 if (User == N)
2833 continue;
2834 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2835 if (User->getOpcode() == ISD::SETCC) {
2836 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2837 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2838 // Sign bits will be lost after a zext.
2839 return false;
2840 bool Add = false;
2841 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002842 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002843 if (UseOp == N0)
2844 continue;
2845 if (!isa<ConstantSDNode>(UseOp))
2846 return false;
2847 Add = true;
2848 }
2849 if (Add)
2850 ExtendNodes.push_back(User);
2851 } else {
2852 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002853 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002854 if (UseOp == N0) {
2855 // If truncate from extended type to original load type is free
2856 // on this target, then it's ok to extend a CopyToReg.
2857 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2858 HasCopyToRegUses = true;
2859 else
2860 return false;
2861 }
2862 }
2863 }
2864 }
2865
2866 if (HasCopyToRegUses) {
2867 bool BothLiveOut = false;
2868 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2869 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002870 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002871 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002872 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002873 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002874 BothLiveOut = true;
2875 break;
2876 }
2877 }
2878 }
2879 if (BothLiveOut)
2880 // Both unextended and extended values are live out. There had better be
2881 // good a reason for the transformation.
2882 return ExtendNodes.size();
2883 }
2884 return true;
2885}
2886
Dan Gohman475871a2008-07-27 21:46:04 +00002887SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2888 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002889 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002890
Nate Begeman1d4d4142005-09-01 00:19:25 +00002891 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002892 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002893 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002894
Nate Begeman1d4d4142005-09-01 00:19:25 +00002895 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002896 // fold (sext (aext x)) -> (sext x)
2897 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002898 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002899
Chris Lattner22558872007-02-26 03:13:59 +00002900 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002901 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2902 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002903 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2904 if (NarrowLoad.getNode()) {
2905 if (NarrowLoad.getNode() != N0.getNode())
2906 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002907 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2908 }
Evan Chengc88138f2007-03-22 01:54:19 +00002909
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002910 // See if the value being truncated is already sign extended. If so, just
2911 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002912 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002913 unsigned OpBits = Op.getValueType().getSizeInBits();
2914 unsigned MidBits = N0.getValueType().getSizeInBits();
2915 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002916 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002917
2918 if (OpBits == DestBits) {
2919 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2920 // bits, it is already ready.
2921 if (NumSignBits > DestBits-MidBits)
2922 return Op;
2923 } else if (OpBits < DestBits) {
2924 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2925 // bits, just sext from i32.
2926 if (NumSignBits > OpBits-MidBits)
2927 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2928 } else {
2929 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2930 // bits, just truncate to i32.
2931 if (NumSignBits > OpBits-MidBits)
2932 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002933 }
Chris Lattner22558872007-02-26 03:13:59 +00002934
2935 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00002936 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2937 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002938 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002939 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002940 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002941 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2942 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2943 DAG.getValueType(N0.getValueType()));
2944 }
Chris Lattner6007b842006-09-21 06:00:20 +00002945 }
Chris Lattner310b5782006-05-06 23:06:26 +00002946
Evan Cheng110dec22005-12-14 02:19:23 +00002947 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002948 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002949 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002950 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002951 bool DoXform = true;
2952 SmallVector<SDNode*, 4> SetCCs;
2953 if (!N0.hasOneUse())
2954 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2955 if (DoXform) {
2956 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002957 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002958 LN0->getBasePtr(), LN0->getSrcValue(),
2959 LN0->getSrcValueOffset(),
2960 N0.getValueType(),
2961 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002962 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002963 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002964 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002965 // Extend SetCC uses if necessary.
2966 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2967 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002968 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002969 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002970 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002971 if (SOp == Trunc)
2972 Ops.push_back(ExtLoad);
2973 else
2974 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2975 }
2976 Ops.push_back(SetCC->getOperand(2));
2977 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2978 &Ops[0], Ops.size()));
2979 }
Dan Gohman475871a2008-07-27 21:46:04 +00002980 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002981 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002982 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002983
2984 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2985 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002986 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
2987 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002988 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002989 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00002990 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002991 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002992 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002993 LN0->getBasePtr(), LN0->getSrcValue(),
2994 LN0->getSrcValueOffset(), EVT,
2995 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002996 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00002997 CombineTo(N0.getNode(),
2998 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002999 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003000 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003001 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003002 }
3003
Chris Lattner20a35c32007-04-11 05:32:27 +00003004 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3005 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003006 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003007 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3008 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3009 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003010 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003011 }
3012
Dan Gohman8f0ad582008-04-28 16:58:24 +00003013 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003014 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003015 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00003016 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3017
Dan Gohman475871a2008-07-27 21:46:04 +00003018 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003019}
3020
Dan Gohman475871a2008-07-27 21:46:04 +00003021SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3022 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003023 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003024
Nate Begeman1d4d4142005-09-01 00:19:25 +00003025 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003026 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003027 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003028 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003029 // fold (zext (aext x)) -> (zext x)
3030 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003031 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003032
Evan Chengc88138f2007-03-22 01:54:19 +00003033 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3034 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003035 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003036 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3037 if (NarrowLoad.getNode()) {
3038 if (NarrowLoad.getNode() != N0.getNode())
3039 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003040 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3041 }
Evan Chengc88138f2007-03-22 01:54:19 +00003042 }
3043
Chris Lattner6007b842006-09-21 06:00:20 +00003044 // fold (zext (truncate x)) -> (and x, mask)
3045 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003046 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003047 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003048 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003049 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003050 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003051 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3052 }
3053 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3054 }
3055
Chris Lattner111c2282006-09-21 06:14:31 +00003056 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3057 if (N0.getOpcode() == ISD::AND &&
3058 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3059 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003060 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003061 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003062 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003063 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003064 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3065 }
Dan Gohman220a8232008-03-03 23:51:38 +00003066 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003068 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3069 }
3070
Evan Cheng110dec22005-12-14 02:19:23 +00003071 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003072 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003073 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003074 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003075 bool DoXform = true;
3076 SmallVector<SDNode*, 4> SetCCs;
3077 if (!N0.hasOneUse())
3078 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3079 if (DoXform) {
3080 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003081 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003082 LN0->getBasePtr(), LN0->getSrcValue(),
3083 LN0->getSrcValueOffset(),
3084 N0.getValueType(),
3085 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003086 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003087 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003088 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003089 // Extend SetCC uses if necessary.
3090 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3091 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003092 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003093 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003094 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003095 if (SOp == Trunc)
3096 Ops.push_back(ExtLoad);
3097 else
Evan Chengde1631b2007-10-30 20:11:21 +00003098 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003099 }
3100 Ops.push_back(SetCC->getOperand(2));
3101 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3102 &Ops[0], Ops.size()));
3103 }
Dan Gohman475871a2008-07-27 21:46:04 +00003104 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003105 }
Evan Cheng110dec22005-12-14 02:19:23 +00003106 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003107
3108 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3109 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003110 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3111 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003112 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003113 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003114 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003115 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003116 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003117 LN0->getBasePtr(), LN0->getSrcValue(),
3118 LN0->getSrcValueOffset(), EVT,
3119 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003120 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003121 CombineTo(N0.getNode(),
3122 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003123 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003124 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003125 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003126 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003127
3128 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3129 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003130 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003131 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3132 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003133 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003134 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003135 }
3136
Dan Gohman475871a2008-07-27 21:46:04 +00003137 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003138}
3139
Dan Gohman475871a2008-07-27 21:46:04 +00003140SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3141 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003142 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003143
3144 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003145 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003146 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3147 // fold (aext (aext x)) -> (aext x)
3148 // fold (aext (zext x)) -> (zext x)
3149 // fold (aext (sext x)) -> (sext x)
3150 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3151 N0.getOpcode() == ISD::ZERO_EXTEND ||
3152 N0.getOpcode() == ISD::SIGN_EXTEND)
3153 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3154
Evan Chengc88138f2007-03-22 01:54:19 +00003155 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3156 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3157 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003158 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3159 if (NarrowLoad.getNode()) {
3160 if (NarrowLoad.getNode() != N0.getNode())
3161 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003162 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3163 }
Evan Chengc88138f2007-03-22 01:54:19 +00003164 }
3165
Chris Lattner84750582006-09-20 06:29:17 +00003166 // fold (aext (truncate x))
3167 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003168 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003169 if (TruncOp.getValueType() == VT)
3170 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003171 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003172 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3173 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3174 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003175
3176 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3177 if (N0.getOpcode() == ISD::AND &&
3178 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3179 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003180 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003181 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003182 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003183 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003184 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3185 }
Dan Gohman220a8232008-03-03 23:51:38 +00003186 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003187 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003188 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3189 }
3190
Chris Lattner5ffc0662006-05-05 05:58:59 +00003191 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003192 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003193 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003194 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003195 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003196 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003197 LN0->getBasePtr(), LN0->getSrcValue(),
3198 LN0->getSrcValueOffset(),
3199 N0.getValueType(),
3200 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003201 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003202 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003203 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3204 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003205 // If any node needs the original loaded value, recompute it.
3206 if (!LN0->use_empty())
3207 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3208 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003209 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003210 }
3211
3212 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3213 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3214 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003215 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003216 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003217 N0.hasOneUse()) {
3218 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003219 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003220 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003221 LN0->getChain(), LN0->getBasePtr(),
3222 LN0->getSrcValue(),
3223 LN0->getSrcValueOffset(), EVT,
3224 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003225 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003226 CombineTo(N0.getNode(),
3227 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003228 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003229 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003230 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003231
3232 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3233 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003234 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003235 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3236 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003237 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003238 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003239 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003240 }
3241
Dan Gohman475871a2008-07-27 21:46:04 +00003242 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003243}
3244
Chris Lattner2b4c2792007-10-13 06:35:54 +00003245/// GetDemandedBits - See if the specified operand can be simplified with the
3246/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003247/// simpler operand, otherwise return a null SDValue.
3248SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003249 switch (V.getOpcode()) {
3250 default: break;
3251 case ISD::OR:
3252 case ISD::XOR:
3253 // If the LHS or RHS don't contribute bits to the or, drop them.
3254 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3255 return V.getOperand(1);
3256 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3257 return V.getOperand(0);
3258 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003259 case ISD::SRL:
3260 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003261 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003262 break;
3263 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3264 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003265 unsigned Amt = RHSC->getZExtValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003266 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003267 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003268 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003269 return DAG.getNode(ISD::SRL, V.getValueType(),
3270 SimplifyLHS, V.getOperand(1));
3271 }
3272 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003273 }
Dan Gohman475871a2008-07-27 21:46:04 +00003274 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003275}
3276
Evan Chengc88138f2007-03-22 01:54:19 +00003277/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3278/// bits and then truncated to a narrower type and where N is a multiple
3279/// of number of bits of the narrower type, transform it to a narrower load
3280/// from address + N / num of bits of new type. If the result is to be
3281/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003282SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003283 unsigned Opc = N->getOpcode();
3284 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003285 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003286 MVT VT = N->getValueType(0);
3287 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003288
Dan Gohman7f8613e2008-08-14 20:04:46 +00003289 // This transformation isn't valid for vector loads.
3290 if (VT.isVector())
3291 return SDValue();
3292
Evan Chenge177e302007-03-23 22:13:36 +00003293 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3294 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003295 if (Opc == ISD::SIGN_EXTEND_INREG) {
3296 ExtType = ISD::SEXTLOAD;
3297 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003298 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003299 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003300 }
3301
Duncan Sands83ec4b62008-06-06 12:08:01 +00003302 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003303 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003304 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003305 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3306 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003307 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003308 // Is the shift amount a multiple of size of VT?
3309 if ((ShAmt & (EVTBits-1)) == 0) {
3310 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003311 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003312 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003313 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003314 }
3315 }
3316 }
3317
Duncan Sandsad205a72008-06-16 08:14:38 +00003318 // Do not generate loads of non-round integer types since these can
3319 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003320 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3321 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003322 // Do not change the width of a volatile load.
3323 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003324 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003325 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003326 // For big endian targets, we need to adjust the offset to the pointer to
3327 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003328 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003329 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003330 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003331 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3332 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003333 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003334 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003335 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003336 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003337 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003338 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003339 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003340 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003341 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003342 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003343 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3344 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003345 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003346 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003347 WorkListRemover DeadNodes(*this);
3348 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3349 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003350 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003351 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003352 CombineTo(N0.getNode(), Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003353 if (ShAmt) {
3354 if (Opc == ISD::SIGN_EXTEND_INREG)
3355 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3356 else
3357 return DAG.getNode(Opc, VT, Load);
3358 }
Dan Gohman475871a2008-07-27 21:46:04 +00003359 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003360 }
3361
Dan Gohman475871a2008-07-27 21:46:04 +00003362 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003363}
3364
Chris Lattner5ffc0662006-05-05 05:58:59 +00003365
Dan Gohman475871a2008-07-27 21:46:04 +00003366SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3367 SDValue N0 = N->getOperand(0);
3368 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003369 MVT VT = N->getValueType(0);
3370 MVT EVT = cast<VTSDNode>(N1)->getVT();
3371 unsigned VTBits = VT.getSizeInBits();
3372 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003373
Nate Begeman1d4d4142005-09-01 00:19:25 +00003374 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003375 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003376 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003377
Chris Lattner541a24f2006-05-06 22:43:44 +00003378 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003379 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003380 return N0;
3381
Nate Begeman646d7e22005-09-02 21:18:40 +00003382 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3383 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003384 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003385 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003386 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003387
Dan Gohman75dcf082008-07-31 00:50:31 +00003388 // fold (sext_in_reg (sext x)) -> (sext x)
3389 // fold (sext_in_reg (aext x)) -> (sext x)
3390 // if x is small enough.
3391 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3392 SDValue N00 = N0.getOperand(0);
3393 if (N00.getValueType().getSizeInBits() < EVTBits)
3394 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3395 }
3396
Chris Lattner95a5e052007-04-17 19:03:21 +00003397 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003398 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003399 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003400
Chris Lattner95a5e052007-04-17 19:03:21 +00003401 // fold operands of sext_in_reg based on knowledge that the top bits are not
3402 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003403 if (SimplifyDemandedBits(SDValue(N, 0)))
3404 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003405
Evan Chengc88138f2007-03-22 01:54:19 +00003406 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3407 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003408 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003409 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003410 return NarrowLoad;
3411
Chris Lattner4b37e872006-05-08 21:18:59 +00003412 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3413 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3414 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3415 if (N0.getOpcode() == ISD::SRL) {
3416 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003417 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003418 // We can turn this into an SRA iff the input to the SRL is already sign
3419 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003420 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003421 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003422 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3423 }
3424 }
Evan Chengc88138f2007-03-22 01:54:19 +00003425
Nate Begemanded49632005-10-13 03:11:28 +00003426 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003427 if (ISD::isEXTLoad(N0.getNode()) &&
3428 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003429 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003430 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003431 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003432 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003433 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003434 LN0->getBasePtr(), LN0->getSrcValue(),
3435 LN0->getSrcValueOffset(), EVT,
3436 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003437 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003438 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003439 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003440 }
3441 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003442 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003443 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003444 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003445 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003446 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003447 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003448 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003449 LN0->getBasePtr(), LN0->getSrcValue(),
3450 LN0->getSrcValueOffset(), EVT,
3451 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003452 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003453 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003454 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003455 }
Dan Gohman475871a2008-07-27 21:46:04 +00003456 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003457}
3458
Dan Gohman475871a2008-07-27 21:46:04 +00003459SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3460 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003461 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003462
3463 // noop truncate
3464 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003465 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003466 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003467 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003468 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003469 // fold (truncate (truncate x)) -> (truncate x)
3470 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003471 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003472 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003473 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3474 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003475 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003476 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003477 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003478 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003479 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003480 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003481 else
3482 // if the source and dest are the same type, we can drop both the extend
3483 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003484 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003485 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003486
Chris Lattner2b4c2792007-10-13 06:35:54 +00003487 // See if we can simplify the input to this truncate through knowledge that
3488 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3489 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003490 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003491 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003492 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003493 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003494 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3495
Nate Begeman3df4d522005-10-12 20:40:40 +00003496 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003497 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003498 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003499}
3500
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003501static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003502 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003503 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003504 return Elt.getNode();
3505 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003506}
3507
3508/// CombineConsecutiveLoads - build_pair (load, load) -> load
3509/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003510SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003511 assert(N->getOpcode() == ISD::BUILD_PAIR);
3512
3513 SDNode *LD1 = getBuildPairElt(N, 0);
3514 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003515 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003516 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003517 SDNode *LD2 = getBuildPairElt(N, 1);
3518 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3519 if (ISD::isNON_EXTLoad(LD2) &&
3520 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003521 // If both are volatile this would reduce the number of volatile loads.
3522 // If one is volatile it might be ok, but play conservative and bail out.
3523 !cast<LoadSDNode>(LD1)->isVolatile() &&
3524 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003525 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003526 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3527 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003528 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003529 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003530 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003531 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003532 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3533 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003534 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003535 }
Dan Gohman475871a2008-07-27 21:46:04 +00003536 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003537}
3538
Dan Gohman475871a2008-07-27 21:46:04 +00003539SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3540 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003541 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003542
Dan Gohman7f321562007-06-25 16:23:39 +00003543 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3544 // Only do this before legalize, since afterward the target may be depending
3545 // on the bitconvert.
3546 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003547 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003548 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003550 bool isSimple = true;
3551 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3552 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3553 N0.getOperand(i).getOpcode() != ISD::Constant &&
3554 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3555 isSimple = false;
3556 break;
3557 }
3558
Duncan Sands83ec4b62008-06-06 12:08:01 +00003559 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3560 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003561 "Element type of vector ValueType must not be vector!");
3562 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003563 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003564 }
3565 }
3566
Dan Gohman3dd168d2008-09-05 01:58:21 +00003567 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003568 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003569 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003570 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003571 }
3572
Chris Lattnerc8547d82005-12-23 05:37:50 +00003573 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3574 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003575
Chris Lattner57104102005-12-23 05:44:41 +00003576 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003577 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003578 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003579 // Do not change the width of a volatile load.
3580 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003581 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003582 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003583 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003584 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003585 unsigned OrigAlign = LN0->getAlignment();
3586 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003588 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3589 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003590 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003591 CombineTo(N0.getNode(),
3592 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003593 Load.getValue(1));
3594 return Load;
3595 }
Chris Lattner57104102005-12-23 05:44:41 +00003596 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003597
Chris Lattner3bd39d42008-01-27 17:42:27 +00003598 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3599 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3600 // This often reduces constant pool loads.
3601 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003602 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003603 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003604 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003605
Duncan Sands83ec4b62008-06-06 12:08:01 +00003606 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003607 if (N0.getOpcode() == ISD::FNEG)
3608 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3609 assert(N0.getOpcode() == ISD::FABS);
3610 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3611 }
3612
3613 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3614 // Note that we don't handle copysign(x,cst) because this can always be folded
3615 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003616 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003617 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003618 VT.isInteger() && !VT.isVector()) {
3619 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sands25cf2272008-11-24 14:53:14 +00003620 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3621 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3622 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3623 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003624
Duncan Sands25cf2272008-11-24 14:53:14 +00003625 // If X has a different width than the result/lhs, sext it or truncate it.
3626 unsigned VTWidth = VT.getSizeInBits();
3627 if (OrigXWidth < VTWidth) {
3628 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3629 AddToWorkList(X.getNode());
3630 } else if (OrigXWidth > VTWidth) {
3631 // To get the sign bit in the right place, we have to shift it right
3632 // before truncating.
3633 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3634 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3635 AddToWorkList(X.getNode());
3636 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3637 AddToWorkList(X.getNode());
3638 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003639
Duncan Sands25cf2272008-11-24 14:53:14 +00003640 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3641 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3642 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003643
Duncan Sands25cf2272008-11-24 14:53:14 +00003644 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3645 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3646 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003647
Duncan Sands25cf2272008-11-24 14:53:14 +00003648 return DAG.getNode(ISD::OR, VT, X, Cst);
3649 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003650 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003651
3652 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3653 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003654 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3655 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003656 return CombineLD;
3657 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003658
Dan Gohman475871a2008-07-27 21:46:04 +00003659 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003660}
3661
Dan Gohman475871a2008-07-27 21:46:04 +00003662SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003663 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003664 return CombineConsecutiveLoads(N, VT);
3665}
3666
Dan Gohman7f321562007-06-25 16:23:39 +00003667/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003668/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3669/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003670SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003671ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3672 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003673
3674 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003675 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003676
Duncan Sands83ec4b62008-06-06 12:08:01 +00003677 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3678 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003679
3680 // If this is a conversion of N elements of one type to N elements of another
3681 // type, convert each element. This handles FP<->INT cases.
3682 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003683 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003684 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003685 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003686 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003687 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003688 MVT VT = MVT::getVectorVT(DstEltVT,
3689 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003690 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003691 }
3692
3693 // Otherwise, we're growing or shrinking the elements. To avoid having to
3694 // handle annoying details of growing/shrinking FP values, we convert them to
3695 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003696 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003697 // Convert the input float vector to a int vector where the elements are the
3698 // same sizes.
3699 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003700 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003701 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003702 SrcEltVT = IntVT;
3703 }
3704
3705 // Now we know the input is an integer vector. If the output is a FP type,
3706 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003707 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003708 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003709 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003710 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003711
3712 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003713 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003714 }
3715
3716 // Okay, we know the src/dst types are both integers of differing types.
3717 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003719 if (SrcBitSize < DstBitSize) {
3720 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3721
Dan Gohman475871a2008-07-27 21:46:04 +00003722 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003723 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003724 i += NumInputsPerOutput) {
3725 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003726 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003727 bool EltIsUndef = true;
3728 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3729 // Shift the previously computed bits over.
3730 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003731 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003732 if (Op.getOpcode() == ISD::UNDEF) continue;
3733 EltIsUndef = false;
3734
Dan Gohman220a8232008-03-03 23:51:38 +00003735 NewBits |=
3736 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003737 }
3738
3739 if (EltIsUndef)
3740 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3741 else
3742 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3743 }
3744
Duncan Sands83ec4b62008-06-06 12:08:01 +00003745 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003746 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003747 }
3748
3749 // Finally, this must be the case where we are shrinking elements: each input
3750 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003751 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003752 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003753 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003754 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003755 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003756 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3757 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3758 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3759 continue;
3760 }
Dan Gohman220a8232008-03-03 23:51:38 +00003761 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003762 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003763 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003764 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003765 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003766 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3767 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003768 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003769 }
3770
3771 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003772 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003773 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3774 }
Dan Gohman7f321562007-06-25 16:23:39 +00003775 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003776}
3777
3778
3779
Dan Gohman475871a2008-07-27 21:46:04 +00003780SDValue DAGCombiner::visitFADD(SDNode *N) {
3781 SDValue N0 = N->getOperand(0);
3782 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003783 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3784 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003785 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003786
Dan Gohman7f321562007-06-25 16:23:39 +00003787 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003788 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003790 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003791 }
Dan Gohman7f321562007-06-25 16:23:39 +00003792
Nate Begemana0e221d2005-10-18 00:28:13 +00003793 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003794 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003795 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003796 // canonicalize constant to RHS
3797 if (N0CFP && !N1CFP)
3798 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003799 // fold (A + (-B)) -> A-B
Duncan Sands25cf2272008-11-24 14:53:14 +00003800 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003801 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003802 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner01b3d732005-09-28 22:28:18 +00003803 // fold ((-A) + B) -> B-A
Duncan Sands25cf2272008-11-24 14:53:14 +00003804 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003805 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00003806 GetNegatedExpression(N0, DAG, LegalOperations));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003807
3808 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3809 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003810 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003811 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3812 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3813
Dan Gohman475871a2008-07-27 21:46:04 +00003814 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003815}
3816
Dan Gohman475871a2008-07-27 21:46:04 +00003817SDValue DAGCombiner::visitFSUB(SDNode *N) {
3818 SDValue N0 = N->getOperand(0);
3819 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003820 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3821 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003822 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003823
Dan Gohman7f321562007-06-25 16:23:39 +00003824 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003825 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003826 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003827 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003828 }
Dan Gohman7f321562007-06-25 16:23:39 +00003829
Nate Begemana0e221d2005-10-18 00:28:13 +00003830 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003831 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003832 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003833 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003834 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00003835 if (isNegatibleForFree(N1, LegalOperations))
3836 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00003837 return DAG.getNode(ISD::FNEG, VT, N1);
3838 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003839 // fold (A-(-B)) -> A+B
Duncan Sands25cf2272008-11-24 14:53:14 +00003840 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattner0254e702008-02-26 07:04:54 +00003841 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003842 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003843
Dan Gohman475871a2008-07-27 21:46:04 +00003844 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003845}
3846
Dan Gohman475871a2008-07-27 21:46:04 +00003847SDValue DAGCombiner::visitFMUL(SDNode *N) {
3848 SDValue N0 = N->getOperand(0);
3849 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003850 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3851 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003853
Dan Gohman7f321562007-06-25 16:23:39 +00003854 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003855 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003856 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003857 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003858 }
Dan Gohman7f321562007-06-25 16:23:39 +00003859
Nate Begeman11af4ea2005-10-17 20:40:11 +00003860 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003861 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003862 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003863 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003864 if (N0CFP && !N1CFP)
3865 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003866 // fold (fmul X, 2.0) -> (fadd X, X)
3867 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3868 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003869 // fold (fmul X, -1.0) -> (fneg X)
3870 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3871 return DAG.getNode(ISD::FNEG, VT, N0);
3872
3873 // -X * -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003874 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3875 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003876 // Both can be negated for free, check to see if at least one is cheaper
3877 // negated.
3878 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003879 return DAG.getNode(ISD::FMUL, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003880 GetNegatedExpression(N0, DAG, LegalOperations),
3881 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003882 }
3883 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003884
3885 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3886 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003887 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003888 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3889 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3890
Dan Gohman475871a2008-07-27 21:46:04 +00003891 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003892}
3893
Dan Gohman475871a2008-07-27 21:46:04 +00003894SDValue DAGCombiner::visitFDIV(SDNode *N) {
3895 SDValue N0 = N->getOperand(0);
3896 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003897 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3898 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003899 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003900
Dan Gohman7f321562007-06-25 16:23:39 +00003901 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003902 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003903 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003904 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003905 }
Dan Gohman7f321562007-06-25 16:23:39 +00003906
Nate Begemana148d982006-01-18 22:35:16 +00003907 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003908 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003909 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003910
3911
3912 // -X / -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003913 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3914 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003915 // Both can be negated for free, check to see if at least one is cheaper
3916 // negated.
3917 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003918 return DAG.getNode(ISD::FDIV, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003919 GetNegatedExpression(N0, DAG, LegalOperations),
3920 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003921 }
3922 }
3923
Dan Gohman475871a2008-07-27 21:46:04 +00003924 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003925}
3926
Dan Gohman475871a2008-07-27 21:46:04 +00003927SDValue DAGCombiner::visitFREM(SDNode *N) {
3928 SDValue N0 = N->getOperand(0);
3929 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003930 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3931 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003932 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003933
Nate Begemana148d982006-01-18 22:35:16 +00003934 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003935 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003936 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003937
Dan Gohman475871a2008-07-27 21:46:04 +00003938 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003939}
3940
Dan Gohman475871a2008-07-27 21:46:04 +00003941SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3942 SDValue N0 = N->getOperand(0);
3943 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003944 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3945 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003946 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003947
Dale Johannesendb44bf82007-10-16 23:38:29 +00003948 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003949 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3950
3951 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003952 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003953 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3954 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003955 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003956 return DAG.getNode(ISD::FABS, VT, N0);
3957 else
3958 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3959 }
3960
3961 // copysign(fabs(x), y) -> copysign(x, y)
3962 // copysign(fneg(x), y) -> copysign(x, y)
3963 // copysign(copysign(x,z), y) -> copysign(x, y)
3964 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3965 N0.getOpcode() == ISD::FCOPYSIGN)
3966 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3967
3968 // copysign(x, abs(y)) -> abs(x)
3969 if (N1.getOpcode() == ISD::FABS)
3970 return DAG.getNode(ISD::FABS, VT, N0);
3971
3972 // copysign(x, copysign(y,z)) -> copysign(x, z)
3973 if (N1.getOpcode() == ISD::FCOPYSIGN)
3974 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3975
3976 // copysign(x, fp_extend(y)) -> copysign(x, y)
3977 // copysign(x, fp_round(y)) -> copysign(x, y)
3978 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3979 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3980
Dan Gohman475871a2008-07-27 21:46:04 +00003981 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003982}
3983
3984
Chris Lattner01b3d732005-09-28 22:28:18 +00003985
Dan Gohman475871a2008-07-27 21:46:04 +00003986SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3987 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003988 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003989 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003990 MVT OpVT = N0.getValueType();
3991
Nate Begeman1d4d4142005-09-01 00:19:25 +00003992 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003993 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003994 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003995
3996 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3997 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003998 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003999 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
4000 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4001 if (DAG.SignBitIsZero(N0))
4002 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4003 }
4004
4005
Dan Gohman475871a2008-07-27 21:46:04 +00004006 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004007}
4008
Dan Gohman475871a2008-07-27 21:46:04 +00004009SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4010 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004011 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004012 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004013 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004014
Nate Begeman1d4d4142005-09-01 00:19:25 +00004015 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004016 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004017 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004018
4019 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4020 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00004021 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00004022 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
4023 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4024 if (DAG.SignBitIsZero(N0))
4025 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4026 }
4027
Dan Gohman475871a2008-07-27 21:46:04 +00004028 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004029}
4030
Dan Gohman475871a2008-07-27 21:46:04 +00004031SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4032 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004033 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004034 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004035
4036 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004037 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004038 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004039 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004040}
4041
Dan Gohman475871a2008-07-27 21:46:04 +00004042SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4043 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004044 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004045 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004046
4047 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004048 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004049 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004050 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004051}
4052
Dan Gohman475871a2008-07-27 21:46:04 +00004053SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4054 SDValue N0 = N->getOperand(0);
4055 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004056 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004057 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004058
4059 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004060 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004061 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004062
4063 // fold (fp_round (fp_extend x)) -> x
4064 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4065 return N0.getOperand(0);
4066
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004067 // fold (fp_round (fp_round x)) -> (fp_round x)
4068 if (N0.getOpcode() == ISD::FP_ROUND) {
4069 // This is a value preserving truncation if both round's are.
4070 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004071 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004072 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4073 DAG.getIntPtrConstant(IsTrunc));
4074 }
4075
Chris Lattner79dbea52006-03-13 06:26:26 +00004076 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004077 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004078 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004079 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004080 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4081 }
4082
Dan Gohman475871a2008-07-27 21:46:04 +00004083 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004084}
4085
Dan Gohman475871a2008-07-27 21:46:04 +00004086SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4087 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088 MVT VT = N->getValueType(0);
4089 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004090 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004091
Nate Begeman1d4d4142005-09-01 00:19:25 +00004092 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004093 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004094 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004095 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004096 }
Dan Gohman475871a2008-07-27 21:46:04 +00004097 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004098}
4099
Dan Gohman475871a2008-07-27 21:46:04 +00004100SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4101 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004102 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004103 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004104
Chris Lattner5938bef2007-12-29 06:55:23 +00004105 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004106 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004107 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4108 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004109
Nate Begeman1d4d4142005-09-01 00:19:25 +00004110 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004111 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004112 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004113
4114 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4115 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004116 if (N0.getOpcode() == ISD::FP_ROUND
4117 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004118 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004119 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004120 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004121 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4122 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4123 }
4124
4125 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004126 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004127 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004128 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004129 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004130 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004131 LN0->getBasePtr(), LN0->getSrcValue(),
4132 LN0->getSrcValueOffset(),
4133 N0.getValueType(),
4134 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004135 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004136 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4137 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004138 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004139 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004140 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004141
Dan Gohman475871a2008-07-27 21:46:04 +00004142 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004143}
4144
Dan Gohman475871a2008-07-27 21:46:04 +00004145SDValue DAGCombiner::visitFNEG(SDNode *N) {
4146 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004147
Duncan Sands25cf2272008-11-24 14:53:14 +00004148 if (isNegatibleForFree(N0, LegalOperations))
4149 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004150
Chris Lattner3bd39d42008-01-27 17:42:27 +00004151 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4152 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004153 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004154 N0.getOperand(0).getValueType().isInteger() &&
4155 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004157 MVT IntVT = Int.getValueType();
4158 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004159 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004160 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004161 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004162 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4163 }
4164 }
4165
Dan Gohman475871a2008-07-27 21:46:04 +00004166 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004167}
4168
Dan Gohman475871a2008-07-27 21:46:04 +00004169SDValue DAGCombiner::visitFABS(SDNode *N) {
4170 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004171 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004172 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004173
Nate Begeman1d4d4142005-09-01 00:19:25 +00004174 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004175 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004176 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004177 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004178 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004179 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004180 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004181 // fold (fabs (fcopysign x, y)) -> (fabs x)
4182 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4183 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4184
Chris Lattner3bd39d42008-01-27 17:42:27 +00004185 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4186 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004187 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004188 N0.getOperand(0).getValueType().isInteger() &&
4189 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004190 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004191 MVT IntVT = Int.getValueType();
4192 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004193 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004194 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004195 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004196 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4197 }
4198 }
4199
Dan Gohman475871a2008-07-27 21:46:04 +00004200 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004201}
4202
Dan Gohman475871a2008-07-27 21:46:04 +00004203SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4204 SDValue Chain = N->getOperand(0);
4205 SDValue N1 = N->getOperand(1);
4206 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004207 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4208
4209 // never taken branch, fold to chain
4210 if (N1C && N1C->isNullValue())
4211 return Chain;
4212 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004213 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004214 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004215 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4216 // on the target.
4217 if (N1.getOpcode() == ISD::SETCC &&
4218 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4219 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4220 N1.getOperand(0), N1.getOperand(1), N2);
4221 }
Dan Gohman475871a2008-07-27 21:46:04 +00004222 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004223}
4224
Chris Lattner3ea0b472005-10-05 06:47:48 +00004225// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4226//
Dan Gohman475871a2008-07-27 21:46:04 +00004227SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004228 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004229 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004230
Duncan Sands8eab8a22008-06-09 11:32:28 +00004231 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands25cf2272008-11-24 14:53:14 +00004232 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS),
4233 CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004234 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004235
Gabor Greifba36cb52008-08-28 21:40:38 +00004236 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004237
4238 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004239 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004240 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4241 N->getOperand(4));
4242 // fold br_cc false, dest -> unconditional fall through
4243 if (SCCC && SCCC->isNullValue())
4244 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004245
Nate Begemane17daeb2005-10-05 21:43:42 +00004246 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004247 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004248 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4249 Simp.getOperand(2), Simp.getOperand(0),
4250 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004251 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004252}
4253
Chris Lattner448f2192006-11-11 00:39:41 +00004254
Duncan Sandsec87aa82008-06-15 20:12:31 +00004255/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4256/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004257/// and it has other uses besides the load / store. After the
4258/// transformation, the new indexed load / store has effectively folded
4259/// the add / subtract in and all of its other uses are redirected to the
4260/// new load / store.
4261bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004262 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004263 return false;
4264
4265 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004266 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004267 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004268 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004269 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004270 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004271 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004272 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004273 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4274 return false;
4275 Ptr = LD->getBasePtr();
4276 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004277 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004278 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004279 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004280 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4281 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4282 return false;
4283 Ptr = ST->getBasePtr();
4284 isLoad = false;
4285 } else
4286 return false;
4287
Chris Lattner9f1794e2006-11-11 00:56:29 +00004288 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4289 // out. There is no reason to make this a preinc/predec.
4290 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004291 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004292 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004293
Chris Lattner9f1794e2006-11-11 00:56:29 +00004294 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004295 SDValue BasePtr;
4296 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004297 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4298 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4299 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004300 // Don't create a indexed load / store with zero offset.
4301 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004302 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004303 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004304
Chris Lattner41e53fd2006-11-11 01:00:15 +00004305 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004306 // 1) The new base ptr is a frame index.
4307 // 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 +00004308 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004309 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004310 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004311 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004312
Chris Lattner41e53fd2006-11-11 01:00:15 +00004313 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4314 // (plus the implicit offset) to a register to preinc anyway.
4315 if (isa<FrameIndexSDNode>(BasePtr))
4316 return false;
4317
4318 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004319 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004320 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004321 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004322 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004323 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004324
Evan Chengc843abe2007-05-24 02:35:39 +00004325 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004327 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4328 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004329 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004330 if (Use == N)
4331 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004332 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004333 return false;
4334
4335 if (!((Use->getOpcode() == ISD::LOAD &&
4336 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004337 (Use->getOpcode() == ISD::STORE &&
4338 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004339 RealUse = true;
4340 }
4341 if (!RealUse)
4342 return false;
4343
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004345 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004346 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004347 else
Dan Gohman475871a2008-07-27 21:46:04 +00004348 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004349 ++PreIndexedNodes;
4350 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004351 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004352 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004353 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004354 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004355 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004356 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004357 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004358 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004359 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004360 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004361 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004362 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004363 }
4364
Chris Lattner9f1794e2006-11-11 00:56:29 +00004365 // Finally, since the node is now dead, remove it from the graph.
4366 DAG.DeleteNode(N);
4367
4368 // Replace the uses of Ptr with uses of the updated base value.
4369 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004370 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004371 removeFromWorkList(Ptr.getNode());
4372 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004373
4374 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004375}
4376
Duncan Sandsec87aa82008-06-15 20:12:31 +00004377/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004378/// add / sub of the base pointer node into a post-indexed load / store.
4379/// The transformation folded the add / subtract into the new indexed
4380/// load / store effectively and all of its uses are redirected to the
4381/// new load / store.
4382bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004383 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004384 return false;
4385
4386 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004387 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004388 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004389 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004390 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004391 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004392 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004393 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4394 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4395 return false;
4396 Ptr = LD->getBasePtr();
4397 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004398 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004399 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004400 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004401 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4402 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4403 return false;
4404 Ptr = ST->getBasePtr();
4405 isLoad = false;
4406 } else
4407 return false;
4408
Gabor Greifba36cb52008-08-28 21:40:38 +00004409 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004410 return false;
4411
Gabor Greifba36cb52008-08-28 21:40:38 +00004412 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4413 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004414 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004415 if (Op == N ||
4416 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4417 continue;
4418
Dan Gohman475871a2008-07-27 21:46:04 +00004419 SDValue BasePtr;
4420 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004421 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4422 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4423 if (Ptr == Offset)
4424 std::swap(BasePtr, Offset);
4425 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004426 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004427 // Don't create a indexed load / store with zero offset.
4428 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004429 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004430 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004431
Chris Lattner9f1794e2006-11-11 00:56:29 +00004432 // Try turning it into a post-indexed load / store except when
4433 // 1) All uses are load / store ops that use it as base ptr.
4434 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4435 // nor a successor of N. Otherwise, if Op is folded that would
4436 // create a cycle.
4437
4438 // Check for #1.
4439 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004440 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4441 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004442 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004443 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004444 continue;
4445
Chris Lattner9f1794e2006-11-11 00:56:29 +00004446 // If all the uses are load / store addresses, then don't do the
4447 // transformation.
4448 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4449 bool RealUse = false;
4450 for (SDNode::use_iterator III = Use->use_begin(),
4451 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004452 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004453 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004454 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004455 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004456 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004457 RealUse = true;
4458 }
Chris Lattner448f2192006-11-11 00:39:41 +00004459
Chris Lattner9f1794e2006-11-11 00:56:29 +00004460 if (!RealUse) {
4461 TryNext = true;
4462 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004463 }
4464 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004465 }
4466 if (TryNext)
4467 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004468
Chris Lattner9f1794e2006-11-11 00:56:29 +00004469 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004470 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004471 SDValue Result = isLoad
4472 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4473 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004474 ++PostIndexedNodes;
4475 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004476 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004477 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004478 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004479 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004480 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004481 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004482 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004483 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004484 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004485 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004486 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004487 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004488 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004489
Chris Lattner9f1794e2006-11-11 00:56:29 +00004490 // Finally, since the node is now dead, remove it from the graph.
4491 DAG.DeleteNode(N);
4492
4493 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004494 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004495 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004496 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004497 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004498 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004499 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004500 }
4501 }
4502 }
4503 return false;
4504}
4505
Chris Lattner00161a62008-01-25 07:20:16 +00004506/// InferAlignment - If we can infer some alignment information from this
4507/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004508static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004509 // If this is a direct reference to a stack slot, use information about the
4510 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004511 int FrameIdx = 1 << 31;
4512 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004513 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004514 FrameIdx = FI->getIndex();
4515 } else if (Ptr.getOpcode() == ISD::ADD &&
4516 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4517 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4518 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4519 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004520 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004521
4522 if (FrameIdx != (1 << 31)) {
4523 // FIXME: Handle FI+CST.
4524 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4525 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004526 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004527
4528 // The alignment of the frame index can be determined from its offset from
4529 // the incoming frame position. If the frame object is at offset 32 and
4530 // the stack is guaranteed to be 16-byte aligned, then we know that the
4531 // object is 16-byte aligned.
4532 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4533 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4534
4535 // Finally, the frame object itself may have a known alignment. Factor
4536 // the alignment + offset into a new alignment. For example, if we know
4537 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4538 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4539 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4540 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4541 FrameOffset);
4542 return std::max(Align, FIInfoAlign);
4543 }
4544 }
Chris Lattner00161a62008-01-25 07:20:16 +00004545
4546 return 0;
4547}
Chris Lattner448f2192006-11-11 00:39:41 +00004548
Dan Gohman475871a2008-07-27 21:46:04 +00004549SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004550 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue Chain = LD->getChain();
4552 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004553
4554 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004555 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004556 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4557 if (Align > LD->getAlignment())
4558 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4559 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004560 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004561 LD->isVolatile(), Align);
4562 }
4563 }
4564
Evan Cheng45a7ca92007-05-01 00:38:21 +00004565
4566 // If load is not volatile and there are no uses of the loaded value (and
4567 // the updated indexed value in case of indexed loads), change uses of the
4568 // chain value into uses of the chain input (i.e. delete the dead load).
4569 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004570 if (N->getValueType(1) == MVT::Other) {
4571 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004572 if (N->hasNUsesOfValue(0, 0)) {
4573 // It's not safe to use the two value CombineTo variant here. e.g.
4574 // v1, chain2 = load chain1, loc
4575 // v2, chain3 = load chain2, loc
4576 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004577 // Now we replace use of chain2 with chain1. This makes the second load
4578 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004579 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004580 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004581 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004582 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004583 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004584 if (N->use_empty()) {
4585 removeFromWorkList(N);
4586 DAG.DeleteNode(N);
4587 }
Dan Gohman475871a2008-07-27 21:46:04 +00004588 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004589 }
Evan Cheng498f5592007-05-01 08:53:39 +00004590 } else {
4591 // Indexed loads.
4592 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4593 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004594 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004595 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004596 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004597 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004598 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004599 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4600 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004601 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004602 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004603 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004604 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004605 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004606 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004607 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004608 }
4609 }
Chris Lattner01a22022005-10-10 22:04:48 +00004610
4611 // If this load is directly stored, replace the load value with the stored
4612 // value.
4613 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004614 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004615 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4616 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004617 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004618 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4619 if (PrevST->getBasePtr() == Ptr &&
4620 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004621 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004622 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004623 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004624
Jim Laskey7ca56af2006-10-11 13:47:09 +00004625 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004626 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004627 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004628
Jim Laskey6ff23e52006-10-04 16:53:27 +00004629 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004630 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004631 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004632
Jim Laskey279f0532006-09-25 16:29:54 +00004633 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004634 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4635 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004636 LD->getSrcValue(), LD->getSrcValueOffset(),
4637 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004638 } else {
4639 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4640 LD->getValueType(0),
4641 BetterChain, Ptr, LD->getSrcValue(),
4642 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004643 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004644 LD->isVolatile(),
4645 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004646 }
Jim Laskey279f0532006-09-25 16:29:54 +00004647
Jim Laskey6ff23e52006-10-04 16:53:27 +00004648 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004649 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004650 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004651
Jim Laskey274062c2006-10-13 23:32:28 +00004652 // Replace uses with load result and token factor. Don't add users
4653 // to work list.
4654 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004655 }
4656 }
4657
Evan Cheng7fc033a2006-11-03 03:06:21 +00004658 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004659 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004660 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004661
Dan Gohman475871a2008-07-27 21:46:04 +00004662 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004663}
4664
Chris Lattner07649d92008-01-08 23:08:06 +00004665
Dan Gohman475871a2008-07-27 21:46:04 +00004666SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004667 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004668 SDValue Chain = ST->getChain();
4669 SDValue Value = ST->getValue();
4670 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004671
Chris Lattner00161a62008-01-25 07:20:16 +00004672 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004673 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004674 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4675 if (Align > ST->getAlignment())
4676 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004677 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004678 ST->isVolatile(), Align);
4679 }
4680 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004681
Evan Cheng59d5b682007-05-07 21:27:48 +00004682 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004683 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004684 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004685 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004686 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004687 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004688 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004689 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004690 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004691 ((!LegalOperations && !ST->isVolatile()) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004692 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004693 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004694 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004695 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004696
Nate Begeman2cbba892006-12-11 02:23:46 +00004697 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004698 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004699 // NOTE: If the original store is volatile, this transform must not increase
4700 // the number of stores. For example, on x86-32 an f64 can be stored in one
4701 // processor operation but an i64 (which is not legal) requires two. So the
4702 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004703 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004704 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004705 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004706 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004707 case MVT::f80: // We don't do this for these yet.
4708 case MVT::f128:
4709 case MVT::ppcf128:
4710 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004711 case MVT::f32:
Duncan Sands25cf2272008-11-24 14:53:14 +00004712 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
4713 !ST->isVolatile()) || TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004714 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004715 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004716 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004717 ST->getSrcValueOffset(), ST->isVolatile(),
4718 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004719 }
4720 break;
4721 case MVT::f64:
Duncan Sands25cf2272008-11-24 14:53:14 +00004722 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
4723 !ST->isVolatile()) || TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004724 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004725 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004726 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004727 ST->getSrcValueOffset(), ST->isVolatile(),
4728 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004729 } else if (!ST->isVolatile() &&
4730 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004731 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004732 // argument passing. Since this is so common, custom legalize the
4733 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004734 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004735 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4736 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004737 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004738
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004739 int SVOffset = ST->getSrcValueOffset();
4740 unsigned Alignment = ST->getAlignment();
4741 bool isVolatile = ST->isVolatile();
4742
Dan Gohman475871a2008-07-27 21:46:04 +00004743 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004744 ST->getSrcValueOffset(),
4745 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004746 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4747 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004748 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004749 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004750 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004751 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004752 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4753 }
4754 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004755 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004756 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004757 }
4758
Jim Laskey279f0532006-09-25 16:29:54 +00004759 if (CombinerAA) {
4760 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004761 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004762
Jim Laskey6ff23e52006-10-04 16:53:27 +00004763 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004764 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004765 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004766 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004767 if (ST->isTruncatingStore()) {
4768 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004769 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004770 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004771 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004772 } else {
4773 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004774 ST->getSrcValue(), ST->getSrcValueOffset(),
4775 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004776 }
4777
Jim Laskey279f0532006-09-25 16:29:54 +00004778 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004779 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004780 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4781
4782 // Don't add users to work list.
4783 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004784 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004785 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004786
Evan Cheng33dbedc2006-11-05 09:31:14 +00004787 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004788 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004789 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004790
Chris Lattner3c872852007-12-29 06:26:16 +00004791 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004792 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004793 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004794 // See if we can simplify the input to this truncstore with knowledge that
4795 // only the low bits are being used. For example:
4796 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004797 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004798 GetDemandedBits(Value,
4799 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004800 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004801 AddToWorkList(Value.getNode());
4802 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004803 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004804 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004805 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004806
4807 // Otherwise, see if we can simplify the operation with
4808 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004809 if (SimplifyDemandedBits(Value,
4810 APInt::getLowBitsSet(
4811 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004812 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004813 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004814 }
4815
Chris Lattner3c872852007-12-29 06:26:16 +00004816 // If this is a load followed by a store to the same location, then the store
4817 // is dead/noop.
4818 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004819 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004820 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004821 // There can't be any side effects between the load and store, such as
4822 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004823 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004824 // The store is dead, remove it.
4825 return Chain;
4826 }
4827 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004828
Chris Lattnerddf89562008-01-17 19:59:44 +00004829 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4830 // truncating store. We can do this even if this is already a truncstore.
4831 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004832 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004833 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004834 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004835 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004836 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004837 ST->isVolatile(), ST->getAlignment());
4838 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004839
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004841}
4842
Dan Gohman475871a2008-07-27 21:46:04 +00004843SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4844 SDValue InVec = N->getOperand(0);
4845 SDValue InVal = N->getOperand(1);
4846 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004847
4848 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4849 // vector with the inserted element.
4850 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004851 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004852 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4853 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004854 if (Elt < Ops.size())
4855 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004856 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4857 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004858 }
4859
Dan Gohman475871a2008-07-27 21:46:04 +00004860 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004861}
4862
Dan Gohman475871a2008-07-27 21:46:04 +00004863SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004864 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4865 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4866 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4867
4868 // Perform only after legalization to ensure build_vector / vector_shuffle
4869 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00004870 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004871
Dan Gohman475871a2008-07-27 21:46:04 +00004872 SDValue InVec = N->getOperand(0);
4873 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004874
Evan Cheng513da432007-10-06 08:19:55 +00004875 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004876 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004877 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00004878 bool BCNumEltsChanged = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004879 MVT VT = InVec.getValueType();
4880 MVT EVT = VT.getVectorElementType();
4881 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004882 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004883 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wanga60b5232008-12-11 00:26:16 +00004884 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004885 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00004886 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4887 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004888 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004889 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004890 NewLoad = true;
4891 }
Evan Cheng513da432007-10-06 08:19:55 +00004892
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004893 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004894 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004895 LN0 = cast<LoadSDNode>(InVec);
4896 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4897 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004898 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004899 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4900 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4901 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4902 // =>
4903 // (load $addr+1*size)
Mon P Wanga60b5232008-12-11 00:26:16 +00004904
4905 // If the bit convert changed the number of elements, it is unsafe
4906 // to examine the mask.
4907 if (BCNumEltsChanged)
4908 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004909 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004910 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004911 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4912 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4913 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4914 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004915 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004916 LN0 = cast<LoadSDNode>(InVec);
4917 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004918 }
4919 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004920 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004921 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004922
4923 unsigned Align = LN0->getAlignment();
4924 if (NewLoad) {
4925 // Check the resultant load doesn't need a higher alignment than the
4926 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004927 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004928 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004929 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004930 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004931 Align = NewAlign;
4932 }
4933
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004935 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004936 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4937 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004938 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004939 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004940 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4941 DAG.getConstant(PtrOff, PtrType));
4942 }
4943 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4944 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4945 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004946 }
Dan Gohman475871a2008-07-27 21:46:04 +00004947 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004948}
4949
4950
Dan Gohman475871a2008-07-27 21:46:04 +00004951SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004952 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004953 MVT VT = N->getValueType(0);
4954 unsigned NumElts = VT.getVectorNumElements();
4955 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004956
Dan Gohman7f321562007-06-25 16:23:39 +00004957 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4958 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4959 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004960 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004961 for (unsigned i = 0; i != NumInScalars; ++i) {
4962 // Ignore undef inputs.
4963 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4964
Dan Gohman7f321562007-06-25 16:23:39 +00004965 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004966 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004967 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004968 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004969 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004970 break;
4971 }
4972
Dan Gohman7f321562007-06-25 16:23:39 +00004973 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004974 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004975 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004976 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004977 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004978 break;
4979 }
4980
4981 // Otherwise, remember this. We allow up to two distinct input vectors.
4982 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4983 continue;
4984
Gabor Greifba36cb52008-08-28 21:40:38 +00004985 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004986 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00004987 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004988 VecIn2 = ExtractedFromVec;
4989 } else {
4990 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004991 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004992 break;
4993 }
4994 }
4995
4996 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00004997 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004998 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004999 for (unsigned i = 0; i != NumInScalars; ++i) {
5000 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00005001 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005002 continue;
5003 }
5004
Dan Gohman475871a2008-07-27 21:46:04 +00005005 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005006
5007 // If extracting from the first vector, just use the index directly.
5008 if (Extract.getOperand(0) == VecIn1) {
5009 BuildVecIndices.push_back(Extract.getOperand(1));
5010 continue;
5011 }
5012
5013 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005014 unsigned Idx =
5015 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005016 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005017 }
5018
5019 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005020 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sands25cf2272008-11-24 14:53:14 +00005021 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5022 return SDValue();
5023
Dan Gohman7f321562007-06-25 16:23:39 +00005024 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00005025 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005026 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00005027 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005028 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00005029 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005030 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005031 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00005032 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00005033 EltType));
5034 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005035 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005036 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00005037 }
Dan Gohman7f321562007-06-25 16:23:39 +00005038 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005039 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005040 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005041 }
5042
Dan Gohman475871a2008-07-27 21:46:04 +00005043 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005044}
5045
Dan Gohman475871a2008-07-27 21:46:04 +00005046SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005047 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5048 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5049 // inputs come from at most two distinct vectors, turn this into a shuffle
5050 // node.
5051
5052 // If we only have one input vector, we don't need to do any concatenation.
5053 if (N->getNumOperands() == 1) {
5054 return N->getOperand(0);
5055 }
5056
Dan Gohman475871a2008-07-27 21:46:04 +00005057 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005058}
5059
Dan Gohman475871a2008-07-27 21:46:04 +00005060SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5061 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005062 unsigned NumElts = ShufMask.getNumOperands();
5063
Mon P Wangaeb06d22008-11-10 04:46:22 +00005064 SDValue N0 = N->getOperand(0);
5065 SDValue N1 = N->getOperand(1);
5066
5067 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5068 "Vector shuffle must be normalized in DAG");
5069
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005070 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5071 bool isIdentity = true;
5072 for (unsigned i = 0; i != NumElts; ++i) {
5073 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005074 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005075 isIdentity = false;
5076 break;
5077 }
5078 }
5079 if (isIdentity) return N->getOperand(0);
5080
5081 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5082 isIdentity = true;
5083 for (unsigned i = 0; i != NumElts; ++i) {
5084 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005085 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5086 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005087 isIdentity = false;
5088 break;
5089 }
5090 }
5091 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005092
5093 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5094 // needed at all.
5095 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005096 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005097 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005098 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005099 for (unsigned i = 0; i != NumElts; ++i)
5100 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005101 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005102 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005103 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005104 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005105 BaseIdx = Idx;
5106 } else {
5107 if (BaseIdx != Idx)
5108 isSplat = false;
5109 if (VecNum != V) {
5110 isUnary = false;
5111 break;
5112 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005113 }
5114 }
5115
Evan Chenge7bec0d2006-07-20 22:44:41 +00005116 // Normalize unary shuffle so the RHS is undef.
5117 if (isUnary && VecNum == 1)
5118 std::swap(N0, N1);
5119
Evan Cheng917ec982006-07-21 08:25:53 +00005120 // If it is a splat, check if the argument vector is a build_vector with
5121 // all scalar elements the same.
5122 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005123 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005124
Dan Gohman7f321562007-06-25 16:23:39 +00005125 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005126 // not the number of vector elements, look through it. Be careful not to
5127 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005128 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005129 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005130 if (ConvInput.getValueType().isVector() &&
5131 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005132 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005133 }
5134
Dan Gohman7f321562007-06-25 16:23:39 +00005135 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5136 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005137 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005138 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005139 bool AllSame = true;
5140 for (unsigned i = 0; i != NumElems; ++i) {
5141 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5142 Base = V->getOperand(i);
5143 break;
5144 }
5145 }
5146 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005147 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005148 return N0;
5149 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005150 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005151 AllSame = false;
5152 break;
5153 }
5154 }
5155 // Splat of <x, x, x, x>, return <x, x, x, x>
5156 if (AllSame)
5157 return N0;
5158 }
5159 }
5160 }
5161
Evan Chenge7bec0d2006-07-20 22:44:41 +00005162 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5163 // into an undef.
5164 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005165 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5166 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005167 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005168 for (unsigned i = 0; i != NumElts; ++i) {
5169 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005170 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5171 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005172 MappedOps.push_back(ShufMask.getOperand(i));
5173 } else {
5174 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005175 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5176 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005177 MappedOps.push_back(DAG.getConstant(NewIdx,
5178 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005179 }
5180 }
Dan Gohman7f321562007-06-25 16:23:39 +00005181 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005182 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005183 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005184 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5185 N0,
5186 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5187 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005188 }
Dan Gohman7f321562007-06-25 16:23:39 +00005189
Dan Gohman475871a2008-07-27 21:46:04 +00005190 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005191}
5192
Evan Cheng44f1f092006-04-20 08:56:16 +00005193/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005194/// an AND to a vector_shuffle with the destination vector and a zero vector.
5195/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005196/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005197SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5198 SDValue LHS = N->getOperand(0);
5199 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005200 if (N->getOpcode() == ISD::AND) {
5201 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005202 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005203 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005204 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005205 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005206 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005207 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005208 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005209 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005210 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005211 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005212 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005213 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005214 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005215 else
Dan Gohman475871a2008-07-27 21:46:04 +00005216 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005217 }
5218
5219 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005220 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005221 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005222
Dan Gohman7f321562007-06-25 16:23:39 +00005223 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005224 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005225 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005226 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005227 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005228 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005229 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005230 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005231 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005232 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005233 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005234 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005235 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005236 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005237 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005238 if (VT != N->getValueType(0))
5239 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005240 return Result;
5241 }
5242 }
Dan Gohman475871a2008-07-27 21:46:04 +00005243 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005244}
5245
Dan Gohman7f321562007-06-25 16:23:39 +00005246/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005247SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005248 // After legalize, the target may be depending on adds and other
5249 // binary ops to provide legal ways to construct constants or other
5250 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005251 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005252
Duncan Sands83ec4b62008-06-06 12:08:01 +00005253 MVT VT = N->getValueType(0);
5254 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005255
Duncan Sands83ec4b62008-06-06 12:08:01 +00005256 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005257 SDValue LHS = N->getOperand(0);
5258 SDValue RHS = N->getOperand(1);
5259 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005260 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005261
Dan Gohman7f321562007-06-25 16:23:39 +00005262 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005263 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005264 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5265 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005266 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005267 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005268 SDValue LHSOp = LHS.getOperand(i);
5269 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005270 // If these two elements can't be folded, bail out.
5271 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5272 LHSOp.getOpcode() != ISD::Constant &&
5273 LHSOp.getOpcode() != ISD::ConstantFP) ||
5274 (RHSOp.getOpcode() != ISD::UNDEF &&
5275 RHSOp.getOpcode() != ISD::Constant &&
5276 RHSOp.getOpcode() != ISD::ConstantFP))
5277 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005278 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005279 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5280 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005281 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005282 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005283 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005284 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005285 break;
5286 }
Dan Gohman7f321562007-06-25 16:23:39 +00005287 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005288 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005289 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5290 Ops.back().getOpcode() == ISD::Constant ||
5291 Ops.back().getOpcode() == ISD::ConstantFP) &&
5292 "Scalar binop didn't fold!");
5293 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005294
Dan Gohman7f321562007-06-25 16:23:39 +00005295 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005296 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005297 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005298 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005299 }
5300
Dan Gohman475871a2008-07-27 21:46:04 +00005301 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005302}
5303
Dan Gohman475871a2008-07-27 21:46:04 +00005304SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005305 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5306
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005308 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5309 // If we got a simplified select_cc node back from SimplifySelectCC, then
5310 // break it down into a new SETCC node, and a new SELECT node, and then return
5311 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005312 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005313 // Check to see if we got a select_cc back (to turn into setcc/select).
5314 // Otherwise, just return whatever node we got back, like fabs.
5315 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005316 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005317 SCC.getOperand(0), SCC.getOperand(1),
5318 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005319 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005320 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5321 SCC.getOperand(3), SETCC);
5322 }
5323 return SCC;
5324 }
Dan Gohman475871a2008-07-27 21:46:04 +00005325 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005326}
5327
Chris Lattner40c62d52005-10-18 06:04:22 +00005328/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5329/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005330/// select. Callers of this should assume that TheSelect is deleted if this
5331/// returns true. As such, they should return the appropriate thing (e.g. the
5332/// node) back to the top-level of the DAG combiner loop to avoid it being
5333/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005334///
Dan Gohman475871a2008-07-27 21:46:04 +00005335bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5336 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005337
5338 // If this is a select from two identical things, try to pull the operation
5339 // through the select.
5340 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005341 // If this is a load and the token chain is identical, replace the select
5342 // of two loads with a load through a select of the address to load from.
5343 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5344 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005345 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005346 // Do not let this transformation reduce the number of volatile loads.
5347 !cast<LoadSDNode>(LHS)->isVolatile() &&
5348 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005349 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005350 LHS.getOperand(0) == RHS.getOperand(0)) {
5351 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5352 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5353
5354 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005355 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005356 // FIXME: this conflates two src values, discarding one. This is not
5357 // the right thing to do, but nothing uses srcvalues now. When they do,
5358 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005359 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005360 if (TheSelect->getOpcode() == ISD::SELECT) {
5361 // Check that the condition doesn't reach either load. If so, folding
5362 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005363 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5364 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005365 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5366 TheSelect->getOperand(0), LLD->getBasePtr(),
5367 RLD->getBasePtr());
5368 }
5369 } else {
5370 // Check that the condition doesn't reach either load. If so, folding
5371 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005372 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5373 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5374 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5375 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005376 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005377 TheSelect->getOperand(0),
5378 TheSelect->getOperand(1),
5379 LLD->getBasePtr(), RLD->getBasePtr(),
5380 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005381 }
Evan Cheng466685d2006-10-09 20:57:25 +00005382 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005383
Gabor Greifba36cb52008-08-28 21:40:38 +00005384 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005385 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005386 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5387 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5388 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005389 LLD->getSrcValueOffset(),
5390 LLD->isVolatile(),
5391 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005392 else {
5393 Load = DAG.getExtLoad(LLD->getExtensionType(),
5394 TheSelect->getValueType(0),
5395 LLD->getChain(), Addr, LLD->getSrcValue(),
5396 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005397 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005398 LLD->isVolatile(),
5399 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005400 }
5401 // Users of the select now use the result of the load.
5402 CombineTo(TheSelect, Load);
5403
5404 // Users of the old loads now use the new load's chain. We know the
5405 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005406 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5407 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005408 return true;
5409 }
Evan Chengc5484282006-10-04 00:56:09 +00005410 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005411 }
5412 }
5413
5414 return false;
5415}
5416
Dan Gohman475871a2008-07-27 21:46:04 +00005417SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5418 SDValue N2, SDValue N3,
5419 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005420
Duncan Sands83ec4b62008-06-06 12:08:01 +00005421 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005422 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5423 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5424 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005425
5426 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005427 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005428 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5429 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005430
5431 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005432 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005433 return N2;
5434 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005435 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005436 return N3;
5437
5438 // Check to see if we can simplify the select into an fabs node
5439 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5440 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005441 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005442 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5443 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5444 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5445 N2 == N3.getOperand(0))
5446 return DAG.getNode(ISD::FABS, VT, N0);
5447
5448 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5449 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5450 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5451 N2.getOperand(0) == N3)
5452 return DAG.getNode(ISD::FABS, VT, N3);
5453 }
5454 }
5455
5456 // Check to see if we can perform the "gzip trick", transforming
5457 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005458 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005459 N0.getValueType().isInteger() &&
5460 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005461 (N1C->isNullValue() || // (a < 0) ? b : 0
5462 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005463 MVT XType = N0.getValueType();
5464 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005465 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005466 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005467 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005468 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5469 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005470 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005471 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5472 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005473 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005474 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005475 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005476 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005477 }
5478 return DAG.getNode(ISD::AND, AType, Shift, N2);
5479 }
Dan Gohman475871a2008-07-27 21:46:04 +00005480 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005481 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005482 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005483 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005484 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005485 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005486 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005487 }
5488 return DAG.getNode(ISD::AND, AType, Shift, N2);
5489 }
5490 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005491
5492 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005493 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005494 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005495
5496 // If the caller doesn't want us to simplify this into a zext of a compare,
5497 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005498 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005499 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005500
Nate Begeman07ed4172005-10-10 21:26:48 +00005501 // Get a SetCC of the condition
5502 // FIXME: Should probably make sure that setcc is legal if we ever have a
5503 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005505 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005506 if (LegalTypes) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005507 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005508 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005509 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5510 else
5511 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005512 } else {
5513 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005514 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005515 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005516 AddToWorkList(SCC.getNode());
5517 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005518
Dan Gohman002e5d02008-03-13 22:13:53 +00005519 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005520 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005521 // shl setcc result by log2 n2c
5522 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005523 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005524 TLI.getShiftAmountTy()));
5525 }
5526
Nate Begemanf845b452005-10-08 00:29:44 +00005527 // Check to see if this is the equivalent of setcc
5528 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5529 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005530 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005531 MVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005532 if (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005533 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005535 if (Res.getValueType() != VT)
5536 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5537 return Res;
5538 }
5539
5540 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5541 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005542 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005543 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005544 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005545 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005546 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005547 TLI.getShiftAmountTy()));
5548 }
5549 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5550 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005551 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005552 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005553 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005554 DAG.getConstant(~0ULL, XType));
5555 return DAG.getNode(ISD::SRL, XType,
5556 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005557 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005558 TLI.getShiftAmountTy()));
5559 }
5560 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5561 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005562 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005563 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005564 TLI.getShiftAmountTy()));
5565 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5566 }
5567 }
5568
5569 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5570 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5571 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005572 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005573 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5574 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005575 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005576 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005577 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005578 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005579 AddToWorkList(Shift.getNode());
5580 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005581 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5582 }
5583 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5584 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5585 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5586 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5587 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005588 MVT XType = N0.getValueType();
5589 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005590 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005591 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005592 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005593 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005594 AddToWorkList(Shift.getNode());
5595 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005596 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5597 }
5598 }
5599 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005600
Dan Gohman475871a2008-07-27 21:46:04 +00005601 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005602}
5603
Evan Chengfa1eb272007-02-08 22:13:59 +00005604/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005605SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5606 SDValue N1, ISD::CondCode Cond,
5607 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005608 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +00005609 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Evan Chengfa1eb272007-02-08 22:13:59 +00005610 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005611}
5612
Nate Begeman69575232005-10-20 02:15:44 +00005613/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5614/// return a DAG expression to select that will generate the same value by
5615/// multiplying by a magic number. See:
5616/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005617SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005618 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005619 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005620
Andrew Lenharth232c9102006-06-12 16:07:18 +00005621 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005622 ii != ee; ++ii)
5623 AddToWorkList(*ii);
5624 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005625}
5626
5627/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5628/// return a DAG expression to select that will generate the same value by
5629/// multiplying by a magic number. See:
5630/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005631SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005632 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005633 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005634
Andrew Lenharth232c9102006-06-12 16:07:18 +00005635 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005636 ii != ee; ++ii)
5637 AddToWorkList(*ii);
5638 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005639}
5640
Jim Laskey71382342006-10-07 23:37:56 +00005641/// FindBaseOffset - Return true if base is known not to alias with anything
5642/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005643static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005644 // Assume it is a primitive operation.
5645 Base = Ptr; Offset = 0;
5646
5647 // If it's an adding a simple constant then integrate the offset.
5648 if (Base.getOpcode() == ISD::ADD) {
5649 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5650 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005651 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005652 }
5653 }
5654
5655 // If it's any of the following then it can't alias with anything but itself.
5656 return isa<FrameIndexSDNode>(Base) ||
5657 isa<ConstantPoolSDNode>(Base) ||
5658 isa<GlobalAddressSDNode>(Base);
5659}
5660
5661/// isAlias - Return true if there is any possibility that the two addresses
5662/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005663bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005664 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005665 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005666 const Value *SrcValue2, int SrcValueOffset2)
5667{
Jim Laskey71382342006-10-07 23:37:56 +00005668 // If they are the same then they must be aliases.
5669 if (Ptr1 == Ptr2) return true;
5670
5671 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005672 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005673 int64_t Offset1, Offset2;
5674 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5675 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5676
5677 // If they have a same base address then...
5678 if (Base1 == Base2) {
5679 // Check to see if the addresses overlap.
5680 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5681 }
5682
Jim Laskey096c22e2006-10-18 12:29:57 +00005683 // If we know both bases then they can't alias.
5684 if (KnownBase1 && KnownBase2) return false;
5685
Jim Laskey07a27092006-10-18 19:08:31 +00005686 if (CombinerGlobalAA) {
5687 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005688 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5689 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5690 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005691 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005692 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005693 if (AAResult == AliasAnalysis::NoAlias)
5694 return false;
5695 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005696
5697 // Otherwise we have to assume they alias.
5698 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005699}
5700
5701/// FindAliasInfo - Extracts the relevant alias information from the memory
5702/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005703bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005704 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005705 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005706 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5707 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005708 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005709 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005710 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005711 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005712 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005713 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005714 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005715 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005716 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005717 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005718 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005719 }
5720
5721 return false;
5722}
5723
Jim Laskey6ff23e52006-10-04 16:53:27 +00005724/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5725/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005726void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5727 SmallVector<SDValue, 8> &Aliases) {
5728 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005729 std::set<SDNode *> Visited; // Visited node set.
5730
Jim Laskey279f0532006-09-25 16:29:54 +00005731 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005733 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005734 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005735 int SrcValueOffset;
5736 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005737
Jim Laskey6ff23e52006-10-04 16:53:27 +00005738 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005739 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005740
Jim Laskeybc588b82006-10-05 15:07:25 +00005741 // Look at each chain and determine if it is an alias. If so, add it to the
5742 // aliases list. If not, then continue up the chain looking for the next
5743 // candidate.
5744 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005745 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005746 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005747
Jim Laskeybc588b82006-10-05 15:07:25 +00005748 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005749 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5750 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005751
5752 switch (Chain.getOpcode()) {
5753 case ISD::EntryToken:
5754 // Entry token is ideal chain operand, but handled in FindBetterChain.
5755 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005756
Jim Laskeybc588b82006-10-05 15:07:25 +00005757 case ISD::LOAD:
5758 case ISD::STORE: {
5759 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005760 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005761 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005762 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005763 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005764 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005765 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005766
5767 // If chain is alias then stop here.
5768 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005769 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5770 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005771 Aliases.push_back(Chain);
5772 } else {
5773 // Look further up the chain.
5774 Chains.push_back(Chain.getOperand(0));
5775 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005776 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005777 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005778 break;
5779 }
5780
5781 case ISD::TokenFactor:
5782 // We have to check each of the operands of the token factor, so we queue
5783 // then up. Adding the operands to the queue (stack) in reverse order
5784 // maintains the original order and increases the likelihood that getNode
5785 // will find a matching token factor (CSE.)
5786 for (unsigned n = Chain.getNumOperands(); n;)
5787 Chains.push_back(Chain.getOperand(--n));
5788 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005789 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005790 break;
5791
5792 default:
5793 // For all other instructions we will just have to take what we can get.
5794 Aliases.push_back(Chain);
5795 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005796 }
5797 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005798}
5799
5800/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5801/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005802SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5803 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005804
Jim Laskey6ff23e52006-10-04 16:53:27 +00005805 // Accumulate all the aliases to this node.
5806 GatherAllAliases(N, OldChain, Aliases);
5807
5808 if (Aliases.size() == 0) {
5809 // If no operands then chain to entry token.
5810 return DAG.getEntryNode();
5811 } else if (Aliases.size() == 1) {
5812 // If a single operand then chain to it. We don't need to revisit it.
5813 return Aliases[0];
5814 }
5815
5816 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005817 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005818 &Aliases[0], Aliases.size());
5819
5820 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005821 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005822
5823 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005824}
5825
Nate Begeman1d4d4142005-09-01 00:19:25 +00005826// SelectionDAG::Combine - This is the entry point for the file.
5827//
Duncan Sands25cf2272008-11-24 14:53:14 +00005828void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005829 /// run - This is the main entry point to this class.
5830 ///
Duncan Sands25cf2272008-11-24 14:53:14 +00005831 DAGCombiner(*this, AA, Fast).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005832}