blob: 0d90fc0748798f85b90ddbace6f599c43865cfa5 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000052 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000053 CombineLevel Level;
54 bool LegalOperations;
55 bool LegalTypes;
Dan Gohmana2676512008-08-20 16:30:28 +000056 bool Fast;
Nate Begeman1d4d4142005-09-01 00:19:25 +000057
58 // Worklist of all of the nodes that need to be simplified.
Evan Cheng17a568b2008-08-29 22:21:44 +000059 std::vector<SDNode*> WorkList;
Nate Begeman1d4d4142005-09-01 00:19:25 +000060
Jim Laskeyc7c3f112006-10-16 20:52:31 +000061 // AA - Used for DAG load/store alias analysis.
62 AliasAnalysis &AA;
63
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 /// AddUsersToWorkList - When an instruction is simplified, add all users of
65 /// the instruction to the work lists because they might get more simplified
66 /// now.
67 ///
68 void AddUsersToWorkList(SDNode *N) {
69 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000070 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000071 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000072 }
73
Dan Gohman389079b2007-10-08 17:57:15 +000074 /// visit - call the node-specific routine that knows how to fold each
75 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000076 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000077
Chris Lattner24664722006-03-01 04:53:38 +000078 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000079 /// AddToWorkList - Add to the work list making sure it's instance is at the
80 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000081 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000082 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000083 WorkList.push_back(N);
84 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000085
Chris Lattnerf8dc0612008-02-03 06:49:24 +000086 /// removeFromWorkList - remove all instances of N from the worklist.
87 ///
88 void removeFromWorkList(SDNode *N) {
89 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
90 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000091 }
Nate Begeman368e18d2006-02-16 21:11:51 +000092
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000094 bool AddTo = true);
95
Dan Gohman475871a2008-07-27 21:46:04 +000096 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000097 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000098 }
99
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +0000101 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000103 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000104 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000105
Chris Lattner24664722006-03-01 04:53:38 +0000106 private:
107
Chris Lattner012f2412006-02-17 21:58:01 +0000108 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000109 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000110 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000111 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000112 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
113 return SimplifyDemandedBits(Op, Demanded);
114 }
115
Dan Gohman475871a2008-07-27 21:46:04 +0000116 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000117
Chris Lattner448f2192006-11-11 00:39:41 +0000118 bool CombineToPreIndexedLoadStore(SDNode *N);
119 bool CombineToPostIndexedLoadStore(SDNode *N);
120
121
Dan Gohman389079b2007-10-08 17:57:15 +0000122 /// combine - call the node-specific routine that knows how to fold each
123 /// particular type of node. If that doesn't do anything, try the
124 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000126
127 // Visitation implementation - Implement dag node combining for different
128 // node types. The semantics are as follows:
129 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000130 // SDValue.getNode() == 0 - No change was made
131 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
132 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000133 //
Dan Gohman475871a2008-07-27 21:46:04 +0000134 SDValue visitTokenFactor(SDNode *N);
135 SDValue visitMERGE_VALUES(SDNode *N);
136 SDValue visitADD(SDNode *N);
137 SDValue visitSUB(SDNode *N);
138 SDValue visitADDC(SDNode *N);
139 SDValue visitADDE(SDNode *N);
140 SDValue visitMUL(SDNode *N);
141 SDValue visitSDIV(SDNode *N);
142 SDValue visitUDIV(SDNode *N);
143 SDValue visitSREM(SDNode *N);
144 SDValue visitUREM(SDNode *N);
145 SDValue visitMULHU(SDNode *N);
146 SDValue visitMULHS(SDNode *N);
147 SDValue visitSMUL_LOHI(SDNode *N);
148 SDValue visitUMUL_LOHI(SDNode *N);
149 SDValue visitSDIVREM(SDNode *N);
150 SDValue visitUDIVREM(SDNode *N);
151 SDValue visitAND(SDNode *N);
152 SDValue visitOR(SDNode *N);
153 SDValue visitXOR(SDNode *N);
154 SDValue SimplifyVBinOp(SDNode *N);
155 SDValue visitSHL(SDNode *N);
156 SDValue visitSRA(SDNode *N);
157 SDValue visitSRL(SDNode *N);
158 SDValue visitCTLZ(SDNode *N);
159 SDValue visitCTTZ(SDNode *N);
160 SDValue visitCTPOP(SDNode *N);
161 SDValue visitSELECT(SDNode *N);
162 SDValue visitSELECT_CC(SDNode *N);
163 SDValue visitSETCC(SDNode *N);
164 SDValue visitSIGN_EXTEND(SDNode *N);
165 SDValue visitZERO_EXTEND(SDNode *N);
166 SDValue visitANY_EXTEND(SDNode *N);
167 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
168 SDValue visitTRUNCATE(SDNode *N);
169 SDValue visitBIT_CONVERT(SDNode *N);
170 SDValue visitBUILD_PAIR(SDNode *N);
171 SDValue visitFADD(SDNode *N);
172 SDValue visitFSUB(SDNode *N);
173 SDValue visitFMUL(SDNode *N);
174 SDValue visitFDIV(SDNode *N);
175 SDValue visitFREM(SDNode *N);
176 SDValue visitFCOPYSIGN(SDNode *N);
177 SDValue visitSINT_TO_FP(SDNode *N);
178 SDValue visitUINT_TO_FP(SDNode *N);
179 SDValue visitFP_TO_SINT(SDNode *N);
180 SDValue visitFP_TO_UINT(SDNode *N);
181 SDValue visitFP_ROUND(SDNode *N);
182 SDValue visitFP_ROUND_INREG(SDNode *N);
183 SDValue visitFP_EXTEND(SDNode *N);
184 SDValue visitFNEG(SDNode *N);
185 SDValue visitFABS(SDNode *N);
186 SDValue visitBRCOND(SDNode *N);
187 SDValue visitBR_CC(SDNode *N);
188 SDValue visitLOAD(SDNode *N);
189 SDValue visitSTORE(SDNode *N);
190 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
191 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
192 SDValue visitBUILD_VECTOR(SDNode *N);
193 SDValue visitCONCAT_VECTORS(SDNode *N);
194 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000195
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDValue XformToShuffleWithZero(SDNode *N);
197 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000198
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000200
Dan Gohman475871a2008-07-27 21:46:04 +0000201 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
202 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
203 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
204 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
205 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000206 bool NotExtCompare = false);
Duncan Sands25cf2272008-11-24 14:53:14 +0000207 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
208 bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000210 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000211 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
212 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
213 SDValue BuildSDIV(SDNode *N);
214 SDValue BuildUDIV(SDNode *N);
215 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
216 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000217
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000219
Jim Laskey6ff23e52006-10-04 16:53:27 +0000220 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
221 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000222 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
223 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000224
Jim Laskey096c22e2006-10-18 12:29:57 +0000225 /// isAlias - Return true if there is any possibility that the two addresses
226 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000227 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000228 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000229 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000230 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000231
Jim Laskey7ca56af2006-10-11 13:47:09 +0000232 /// FindAliasInfo - Extracts the relevant alias information from the memory
233 /// node. Returns true if the operand was a load.
234 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000236 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000237
Jim Laskey279f0532006-09-25 16:29:54 +0000238 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000239 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000240 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000241
Nate Begeman1d4d4142005-09-01 00:19:25 +0000242public:
Dan Gohmana2676512008-08-20 16:30:28 +0000243 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, bool fast)
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000244 : DAG(D),
245 TLI(D.getTargetLoweringInfo()),
Duncan Sands25cf2272008-11-24 14:53:14 +0000246 Level(Unrestricted),
247 LegalOperations(false),
248 LegalTypes(false),
Dan Gohmana2676512008-08-20 16:30:28 +0000249 Fast(fast),
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000250 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000251
252 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000253 void Run(CombineLevel AtLevel);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000254 };
255}
256
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000257
258namespace {
259/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
260/// nodes from the worklist.
261class VISIBILITY_HIDDEN WorkListRemover :
262 public SelectionDAG::DAGUpdateListener {
263 DAGCombiner &DC;
264public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000265 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000266
Duncan Sandsedfcf592008-06-11 11:42:12 +0000267 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000268 DC.removeFromWorkList(N);
269 }
270
271 virtual void NodeUpdated(SDNode *N) {
272 // Ignore updates.
273 }
274};
275}
276
Chris Lattner24664722006-03-01 04:53:38 +0000277//===----------------------------------------------------------------------===//
278// TargetLowering::DAGCombinerInfo implementation
279//===----------------------------------------------------------------------===//
280
281void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
282 ((DAGCombiner*)DC)->AddToWorkList(N);
283}
284
Dan Gohman475871a2008-07-27 21:46:04 +0000285SDValue TargetLowering::DAGCombinerInfo::
286CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000287 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000288}
289
Dan Gohman475871a2008-07-27 21:46:04 +0000290SDValue TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000292 return ((DAGCombiner*)DC)->CombineTo(N, Res);
293}
294
295
Dan Gohman475871a2008-07-27 21:46:04 +0000296SDValue TargetLowering::DAGCombinerInfo::
297CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000298 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
299}
300
301
Chris Lattner24664722006-03-01 04:53:38 +0000302//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000303// Helper Functions
304//===----------------------------------------------------------------------===//
305
306/// isNegatibleForFree - Return 1 if we can compute the negated form of the
307/// specified expression for the same cost as the expression itself, or 2 if we
308/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000309static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattner0254e702008-02-26 07:04:54 +0000310 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000311 // No compile time optimizations on this type.
312 if (Op.getValueType() == MVT::ppcf128)
313 return 0;
314
Chris Lattner29446522007-05-14 22:04:50 +0000315 // fneg is removable even if it has multiple uses.
316 if (Op.getOpcode() == ISD::FNEG) return 2;
317
318 // Don't allow anything with multiple uses.
319 if (!Op.hasOneUse()) return 0;
320
Chris Lattner3adf9512007-05-25 02:19:06 +0000321 // Don't recurse exponentially.
322 if (Depth > 6) return 0;
323
Chris Lattner29446522007-05-14 22:04:50 +0000324 switch (Op.getOpcode()) {
325 default: return false;
326 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000327 // Don't invert constant FP values after legalize. The negated constant
328 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000329 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000330 case ISD::FADD:
331 // FIXME: determine better conditions for this xform.
332 if (!UnsafeFPMath) return 0;
333
334 // -(A+B) -> -A - B
Duncan Sands25cf2272008-11-24 14:53:14 +0000335 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000336 return V;
337 // -(A+B) -> -B - A
Duncan Sands25cf2272008-11-24 14:53:14 +0000338 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000339 case ISD::FSUB:
340 // We can't turn -(A-B) into B-A when we honor signed zeros.
341 if (!UnsafeFPMath) return 0;
342
343 // -(A-B) -> B-A
344 return 1;
345
346 case ISD::FMUL:
347 case ISD::FDIV:
348 if (HonorSignDependentRoundingFPMath()) return 0;
349
350 // -(X*Y) -> (-X * Y) or (X*-Y)
Duncan Sands25cf2272008-11-24 14:53:14 +0000351 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000352 return V;
353
Duncan Sands25cf2272008-11-24 14:53:14 +0000354 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000355
356 case ISD::FP_EXTEND:
357 case ISD::FP_ROUND:
358 case ISD::FSIN:
Duncan Sands25cf2272008-11-24 14:53:14 +0000359 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000360 }
361}
362
363/// GetNegatedExpression - If isNegatibleForFree returns true, this function
364/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000365static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000366 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000367 // fneg is removable even if it has multiple uses.
368 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
369
370 // Don't allow anything with multiple uses.
371 assert(Op.hasOneUse() && "Unknown reuse!");
372
Chris Lattner3adf9512007-05-25 02:19:06 +0000373 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000374 switch (Op.getOpcode()) {
375 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000376 case ISD::ConstantFP: {
377 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
378 V.changeSign();
379 return DAG.getConstantFP(V, Op.getValueType());
380 }
Chris Lattner29446522007-05-14 22:04:50 +0000381 case ISD::FADD:
382 // FIXME: determine better conditions for this xform.
383 assert(UnsafeFPMath);
384
385 // -(A+B) -> -A - B
Duncan Sands25cf2272008-11-24 14:53:14 +0000386 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000387 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000388 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000389 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000390 Op.getOperand(1));
391 // -(A+B) -> -B - A
392 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000393 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000394 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000395 Op.getOperand(0));
396 case ISD::FSUB:
397 // We can't turn -(A-B) into B-A when we honor signed zeros.
398 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000399
400 // -(0-B) -> B
401 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000402 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000403 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000404
405 // -(A-B) -> B-A
406 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
407 Op.getOperand(0));
408
409 case ISD::FMUL:
410 case ISD::FDIV:
411 assert(!HonorSignDependentRoundingFPMath());
412
413 // -(X*Y) -> -X * Y
Duncan Sands25cf2272008-11-24 14:53:14 +0000414 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000416 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000417 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000418 Op.getOperand(1));
419
420 // -(X*Y) -> X * -Y
421 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
422 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000423 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000424 LegalOperations, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000425
426 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000427 case ISD::FSIN:
428 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000429 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000430 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000431 case ISD::FP_ROUND:
432 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000433 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000434 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000435 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000436 }
437}
Chris Lattner24664722006-03-01 04:53:38 +0000438
439
Nate Begeman4ebd8052005-09-01 23:24:04 +0000440// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
441// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000442// Also, set the incoming LHS, RHS, and CC references to the appropriate
443// nodes based on the type of node we are checking. This simplifies life a
444// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000445static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
446 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000447 if (N.getOpcode() == ISD::SETCC) {
448 LHS = N.getOperand(0);
449 RHS = N.getOperand(1);
450 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000451 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000452 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000453 if (N.getOpcode() == ISD::SELECT_CC &&
454 N.getOperand(2).getOpcode() == ISD::Constant &&
455 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000456 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
458 LHS = N.getOperand(0);
459 RHS = N.getOperand(1);
460 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000463 return false;
464}
465
Nate Begeman99801192005-09-07 23:25:52 +0000466// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
467// one use. If this is true, it allows the users to invert the operation for
468// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000469static bool isOneUseSetCC(SDValue N) {
470 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000471 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000472 return true;
473 return false;
474}
475
Dan Gohman475871a2008-07-27 21:46:04 +0000476SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000477 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000478 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
479 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
480 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
481 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000482 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000483 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
485 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000486 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000487 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000488 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
489 }
490 }
491 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
492 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
493 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
494 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000495 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000496 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000497 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
498 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000499 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000500 AddToWorkList(OpNode.getNode());
Nate Begemancd4d58c2006-02-03 06:46:56 +0000501 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
502 }
503 }
Dan Gohman475871a2008-07-27 21:46:04 +0000504 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505}
506
Dan Gohman475871a2008-07-27 21:46:04 +0000507SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
508 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000509 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
510 ++NodesCombined;
511 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +0000512 DOUT << "\nWith: "; DEBUG(To[0].getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000513 DOUT << " and " << NumTo-1 << " other values\n";
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
Dan Gohmandbe664a2009-01-19 21:44:21 +0000525 // Finally, if the node is now dead, remove it from the graph. The node
526 // may not be dead if the replacement process recursively simplified to
527 // something else needing this node.
528 if (N->use_empty()) {
529 // Nodes can be reintroduced into the worklist. Make sure we do not
530 // process a node that has been replaced.
531 removeFromWorkList(N);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000532
Dan Gohmandbe664a2009-01-19 21:44:21 +0000533 // Finally, since the node is now dead, remove it from the graph.
534 DAG.DeleteNode(N);
535 }
Dan Gohman475871a2008-07-27 21:46:04 +0000536 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000537}
538
539/// SimplifyDemandedBits - Check the specified integer node value to see if
540/// it can be simplified or if things it uses can be simplified by bit
541/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000542bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Duncan Sands25cf2272008-11-24 14:53:14 +0000543 TargetLowering::TargetLoweringOpt TLO(DAG);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000544 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000545 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
546 return false;
547
548 // Revisit the node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000549 AddToWorkList(Op.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000550
551 // Replace the old value with the new one.
552 ++NodesCombined;
Gabor Greifba36cb52008-08-28 21:40:38 +0000553 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.getNode()->dump(&DAG));
554 DOUT << "\nWith: "; DEBUG(TLO.New.getNode()->dump(&DAG));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000555 DOUT << '\n';
556
557 // Replace all uses. If any nodes become isomorphic to other nodes and
558 // are deleted, make sure to remove them from our worklist.
559 WorkListRemover DeadNodes(*this);
560 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
561
562 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000563 AddToWorkList(TLO.New.getNode());
564 AddUsersToWorkList(TLO.New.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000565
566 // Finally, if the node is now dead, remove it from the graph. The node
567 // may not be dead if the replacement process recursively simplified to
568 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000569 if (TLO.Old.getNode()->use_empty()) {
570 removeFromWorkList(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000571
572 // If the operands of this node are only used by the node, they will now
573 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000574 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
575 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
576 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000577
Gabor Greifba36cb52008-08-28 21:40:38 +0000578 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000579 }
580 return true;
581}
582
Chris Lattner29446522007-05-14 22:04:50 +0000583//===----------------------------------------------------------------------===//
584// Main DAG Combiner implementation
585//===----------------------------------------------------------------------===//
586
Duncan Sands25cf2272008-11-24 14:53:14 +0000587void DAGCombiner::Run(CombineLevel AtLevel) {
588 // set the instance variables, so that the various visit routines may use it.
589 Level = AtLevel;
590 LegalOperations = Level >= NoIllegalOperations;
591 LegalTypes = Level >= NoIllegalTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000592
Evan Cheng17a568b2008-08-29 22:21:44 +0000593 // Add all the dag nodes to the worklist.
594 WorkList.reserve(DAG.allnodes_size());
595 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
596 E = DAG.allnodes_end(); I != E; ++I)
597 WorkList.push_back(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000598
Evan Cheng17a568b2008-08-29 22:21:44 +0000599 // Create a dummy node (which is not added to allnodes), that adds a reference
600 // to the root node, preventing it from being deleted, and tracking any
601 // changes of the root.
602 HandleSDNode Dummy(DAG.getRoot());
603
Jim Laskey26f7fa72006-10-17 19:33:52 +0000604 // The root of the dag may dangle to deleted nodes until the dag combiner is
605 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000606 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000607
Evan Cheng17a568b2008-08-29 22:21:44 +0000608 // while the worklist isn't empty, inspect the node on the end of it and
609 // try and combine it.
610 while (!WorkList.empty()) {
611 SDNode *N = WorkList.back();
612 WorkList.pop_back();
613
614 // If N has no uses, it is dead. Make sure to revisit all N's operands once
615 // N is deleted from the DAG, since they too may now be dead or may have a
616 // reduced number of uses, allowing other xforms.
617 if (N->use_empty() && N != &Dummy) {
618 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
619 AddToWorkList(N->getOperand(i).getNode());
620
621 DAG.DeleteNode(N);
622 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000623 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000624
625 SDValue RV = combine(N);
626
627 if (RV.getNode() == 0)
628 continue;
629
630 ++NodesCombined;
631
632 // If we get back the same node we passed in, rather than a new node or
633 // zero, we know that the node must have defined multiple values and
634 // CombineTo was used. Since CombineTo takes care of the worklist
635 // mechanics for us, we have no work to do in this case.
636 if (RV.getNode() == N)
637 continue;
638
639 assert(N->getOpcode() != ISD::DELETED_NODE &&
640 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
641 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000642
Evan Cheng17a568b2008-08-29 22:21:44 +0000643 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
644 DOUT << "\nWith: "; DEBUG(RV.getNode()->dump(&DAG));
645 DOUT << '\n';
646 WorkListRemover DeadNodes(*this);
647 if (N->getNumValues() == RV.getNode()->getNumValues())
648 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
649 else {
650 assert(N->getValueType(0) == RV.getValueType() &&
651 N->getNumValues() == 1 && "Type mismatch");
652 SDValue OpV = RV;
653 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
654 }
655
656 // Push the new node and any users onto the worklist
657 AddToWorkList(RV.getNode());
658 AddUsersToWorkList(RV.getNode());
659
660 // Add any uses of the old node to the worklist in case this node is the
661 // last one that uses them. They may become dead after this node is
662 // deleted.
663 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
664 AddToWorkList(N->getOperand(i).getNode());
665
Dan Gohmandbe664a2009-01-19 21:44:21 +0000666 // Finally, if the node is now dead, remove it from the graph. The node
667 // may not be dead if the replacement process recursively simplified to
668 // something else needing this node.
669 if (N->use_empty()) {
670 // Nodes can be reintroduced into the worklist. Make sure we do not
671 // process a node that has been replaced.
672 removeFromWorkList(N);
Evan Cheng17a568b2008-08-29 22:21:44 +0000673
Dan Gohmandbe664a2009-01-19 21:44:21 +0000674 // Finally, since the node is now dead, remove it from the graph.
675 DAG.DeleteNode(N);
676 }
Evan Cheng17a568b2008-08-29 22:21:44 +0000677 }
678
Chris Lattner95038592005-10-05 06:35:28 +0000679 // If the root changed (e.g. it was a dead load, update the root).
680 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000681}
682
Dan Gohman475871a2008-07-27 21:46:04 +0000683SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000684 switch(N->getOpcode()) {
685 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000686 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000687 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000688 case ISD::ADD: return visitADD(N);
689 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000690 case ISD::ADDC: return visitADDC(N);
691 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000692 case ISD::MUL: return visitMUL(N);
693 case ISD::SDIV: return visitSDIV(N);
694 case ISD::UDIV: return visitUDIV(N);
695 case ISD::SREM: return visitSREM(N);
696 case ISD::UREM: return visitUREM(N);
697 case ISD::MULHU: return visitMULHU(N);
698 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000699 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
700 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
701 case ISD::SDIVREM: return visitSDIVREM(N);
702 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000703 case ISD::AND: return visitAND(N);
704 case ISD::OR: return visitOR(N);
705 case ISD::XOR: return visitXOR(N);
706 case ISD::SHL: return visitSHL(N);
707 case ISD::SRA: return visitSRA(N);
708 case ISD::SRL: return visitSRL(N);
709 case ISD::CTLZ: return visitCTLZ(N);
710 case ISD::CTTZ: return visitCTTZ(N);
711 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000712 case ISD::SELECT: return visitSELECT(N);
713 case ISD::SELECT_CC: return visitSELECT_CC(N);
714 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000715 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
716 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000717 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000718 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
719 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000720 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000721 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000722 case ISD::FADD: return visitFADD(N);
723 case ISD::FSUB: return visitFSUB(N);
724 case ISD::FMUL: return visitFMUL(N);
725 case ISD::FDIV: return visitFDIV(N);
726 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000727 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000728 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
729 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
730 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
731 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
732 case ISD::FP_ROUND: return visitFP_ROUND(N);
733 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
734 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
735 case ISD::FNEG: return visitFNEG(N);
736 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000737 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000738 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000739 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000740 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000741 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000742 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000743 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
744 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000745 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000746 }
Dan Gohman475871a2008-07-27 21:46:04 +0000747 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000748}
749
Dan Gohman475871a2008-07-27 21:46:04 +0000750SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000751
Dan Gohman475871a2008-07-27 21:46:04 +0000752 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000753
754 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +0000755 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +0000756 assert(N->getOpcode() != ISD::DELETED_NODE &&
757 "Node was deleted but visit returned NULL!");
758
759 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
760 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
761
762 // Expose the DAG combiner to the target combiner impls.
763 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +0000764 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +0000765
766 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
767 }
768 }
769
Evan Cheng08b11732008-03-22 01:55:50 +0000770 // If N is a commutative binary node, try commuting it to enable more
771 // sdisel CSE.
Gabor Greifba36cb52008-08-28 21:40:38 +0000772 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +0000773 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
774 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000775 SDValue N0 = N->getOperand(0);
776 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000777 // Constant operands are canonicalized to RHS.
778 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000779 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000780 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
781 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000782 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000783 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000784 }
785 }
786
Dan Gohman389079b2007-10-08 17:57:15 +0000787 return RV;
788}
789
Chris Lattner6270f682006-10-08 22:57:01 +0000790/// getInputChainForNode - Given a node, return its input chain if it has one,
791/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000792static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000793 if (unsigned NumOps = N->getNumOperands()) {
794 if (N->getOperand(0).getValueType() == MVT::Other)
795 return N->getOperand(0);
796 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
797 return N->getOperand(NumOps-1);
798 for (unsigned i = 1; i < NumOps-1; ++i)
799 if (N->getOperand(i).getValueType() == MVT::Other)
800 return N->getOperand(i);
801 }
Dan Gohman475871a2008-07-27 21:46:04 +0000802 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000803}
804
Dan Gohman475871a2008-07-27 21:46:04 +0000805SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000806 // If N has two operands, where one has an input chain equal to the other,
807 // the 'other' chain is redundant.
808 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000809 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +0000810 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000811 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +0000812 return N->getOperand(1);
813 }
814
Chris Lattnerc76d4412007-05-16 06:37:59 +0000815 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000817 SmallPtrSet<SDNode*, 16> SeenOps;
818 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000819
820 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000821 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000822
Jim Laskey71382342006-10-07 23:37:56 +0000823 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000824 // encountered.
825 for (unsigned i = 0; i < TFs.size(); ++i) {
826 SDNode *TF = TFs[i];
827
Jim Laskey6ff23e52006-10-04 16:53:27 +0000828 // Check each of the operands.
829 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000830 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000831
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 switch (Op.getOpcode()) {
833 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000834 // Entry tokens don't need to be added to the list. They are
835 // rededundant.
836 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000837 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000838
Jim Laskey6ff23e52006-10-04 16:53:27 +0000839 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000840 if ((CombinerAA || Op.hasOneUse()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000841 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000842 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +0000843 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000844 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +0000845 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +0000846 Changed = true;
847 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000848 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849 // Fall thru
850
851 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000852 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +0000853 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +0000854 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000855 else
856 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000857 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000858 }
859 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000860 }
861
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000863
864 // If we've change things around then replace token factor.
865 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000866 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000867 // The entry token is the only possible outcome.
868 Result = DAG.getEntryNode();
869 } else {
870 // New and improved token factor.
871 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000872 }
Jim Laskey274062c2006-10-13 23:32:28 +0000873
874 // Don't add users to work list.
875 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000876 }
Jim Laskey279f0532006-09-25 16:29:54 +0000877
Jim Laskey6ff23e52006-10-04 16:53:27 +0000878 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000879}
880
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000881/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000882SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000883 WorkListRemover DeadNodes(*this);
884 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000885 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000886 &DeadNodes);
887 removeFromWorkList(N);
888 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000889 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000890}
891
892
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000893static
Dan Gohman475871a2008-07-27 21:46:04 +0000894SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000895 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000896 SDValue N00 = N0.getOperand(0);
897 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000898 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Gabor Greifba36cb52008-08-28 21:40:38 +0000899 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000900 isa<ConstantSDNode>(N00.getOperand(1))) {
901 N0 = DAG.getNode(ISD::ADD, VT,
902 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
903 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
904 return DAG.getNode(ISD::ADD, VT, N0, N1);
905 }
Dan Gohman475871a2008-07-27 21:46:04 +0000906 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000907}
908
Evan Chengb13cdbd2007-06-21 07:39:16 +0000909static
Dan Gohman475871a2008-07-27 21:46:04 +0000910SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Bill Wendlingae89bb12008-11-11 08:25:46 +0000911 SelectionDAG &DAG, const TargetLowering &TLI,
Duncan Sands25cf2272008-11-24 14:53:14 +0000912 bool LegalOperations) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000913 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000914 unsigned Opc = N->getOpcode();
915 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000916 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
917 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000918 ISD::CondCode CC = ISD::SETCC_INVALID;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000919
920 if (isSlctCC) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000921 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
Bill Wendlingae89bb12008-11-11 08:25:46 +0000922 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000923 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000924 if (CCOp.getOpcode() == ISD::SETCC)
925 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
926 }
927
928 bool DoXform = false;
929 bool InvCC = false;
930 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
931 "Bad input!");
Bill Wendlingae89bb12008-11-11 08:25:46 +0000932
Evan Chengb13cdbd2007-06-21 07:39:16 +0000933 if (LHS.getOpcode() == ISD::Constant &&
Bill Wendlingae89bb12008-11-11 08:25:46 +0000934 cast<ConstantSDNode>(LHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000935 DoXform = true;
Bill Wendlingae89bb12008-11-11 08:25:46 +0000936 } else if (CC != ISD::SETCC_INVALID &&
937 RHS.getOpcode() == ISD::Constant &&
938 cast<ConstantSDNode>(RHS)->isNullValue()) {
Evan Chengb13cdbd2007-06-21 07:39:16 +0000939 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000940 SDValue Op0 = Slct.getOperand(0);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000941 MVT OpVT = isSlctCC ? Op0.getValueType() :
942 Op0.getOperand(0).getValueType();
943 bool isInt = OpVT.isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000944 CC = ISD::getSetCCInverse(CC, isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +0000945
Duncan Sands25cf2272008-11-24 14:53:14 +0000946 if (LegalOperations && !TLI.isCondCodeLegal(CC, OpVT))
Bill Wendlingae89bb12008-11-11 08:25:46 +0000947 return SDValue(); // Inverse operator isn't legal.
948
Evan Chengb13cdbd2007-06-21 07:39:16 +0000949 DoXform = true;
950 InvCC = true;
951 }
952
953 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000954 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000955 if (isSlctCC)
956 return DAG.getSelectCC(OtherOp, Result,
957 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000958 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000959 if (InvCC)
960 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
961 CCOp.getOperand(1), CC);
962 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
963 }
Dan Gohman475871a2008-07-27 21:46:04 +0000964 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000965}
966
Dan Gohman475871a2008-07-27 21:46:04 +0000967SDValue DAGCombiner::visitADD(SDNode *N) {
968 SDValue N0 = N->getOperand(0);
969 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000970 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
971 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000972 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000973
974 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000975 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +0000977 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +0000978 }
Bill Wendling2476e5d2008-12-10 22:36:00 +0000979
Dan Gohman613e0d82007-07-03 14:03:57 +0000980 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000981 if (N0.getOpcode() == ISD::UNDEF)
982 return N0;
983 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000984 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000985 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000986 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +0000987 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +0000988 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000989 if (N0C && !N1C)
990 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000991 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000992 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000993 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +0000994 // fold (add Sym, c) -> Sym+c
995 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +0000996 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +0000997 GA->getOpcode() == ISD::GlobalAddress)
998 return DAG.getGlobalAddress(GA->getGlobal(), VT,
999 GA->getOffset() +
1000 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001001 // fold ((c1-A)+c2) -> (c1+c2)-A
1002 if (N1C && N0.getOpcode() == ISD::SUB)
1003 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
1004 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001005 DAG.getConstant(N1C->getAPIntValue()+
1006 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001007 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001008 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +00001009 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001010 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001011 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001012 // fold ((0-A) + B) -> B-A
1013 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1014 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001015 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001016 // fold (A + (0-B)) -> A-B
1017 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1018 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +00001019 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001020 // fold (A+(B-A)) -> B
1021 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001023 // fold ((B-A)+A) -> B
1024 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1025 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001026 // fold (A+(B-(A+C))) to (B-C)
1027 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1028 N0 == N1.getOperand(1).getOperand(0)) {
1029 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1030 N1.getOperand(1).getOperand(1));
1031 }
1032 // fold (A+(B-(C+A))) to (B-C)
1033 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
1034 N0 == N1.getOperand(1).getOperand(1)) {
1035 return DAG.getNode(ISD::SUB, VT, N1.getOperand(0),
1036 N1.getOperand(1).getOperand(0));
1037 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001038 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001039 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1040 N1.getOperand(0).getOpcode() == ISD::SUB &&
1041 N0 == N1.getOperand(0).getOperand(1)) {
1042 return DAG.getNode(N1.getOpcode(), VT, N1.getOperand(0).getOperand(0),
1043 N1.getOperand(1));
1044 }
1045
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001046 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1047 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1048 SDValue N00 = N0.getOperand(0);
1049 SDValue N01 = N0.getOperand(1);
1050 SDValue N10 = N1.getOperand(0);
1051 SDValue N11 = N1.getOperand(1);
1052 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10)) {
1053 return DAG.getNode(ISD::SUB, VT,
1054 DAG.getNode(ISD::ADD, VT, N00, N10),
1055 DAG.getNode(ISD::ADD, VT, N01, N11));
1056 }
1057 }
Chris Lattner947c2892006-03-13 06:51:27 +00001058
Dan Gohman475871a2008-07-27 21:46:04 +00001059 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1060 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001061
1062 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001063 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001064 APInt LHSZero, LHSOne;
1065 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001066 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001067 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001068 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001069 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001070
1071 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1072 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1073 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1074 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1075 return DAG.getNode(ISD::OR, VT, N0, N1);
1076 }
1077 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001078
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001079 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001080 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001081 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001083 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001084 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001085 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001087 }
1088
Evan Chengb13cdbd2007-06-21 07:39:16 +00001089 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001090 if (N0.getOpcode() == ISD::SELECT && N0.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001091 SDValue Result = combineSelectAndUse(N, N0, N1, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001092 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001093 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001094 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001095 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001096 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001097 }
1098
Dan Gohman475871a2008-07-27 21:46:04 +00001099 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100}
1101
Dan Gohman475871a2008-07-27 21:46:04 +00001102SDValue DAGCombiner::visitADDC(SDNode *N) {
1103 SDValue N0 = N->getOperand(0);
1104 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001105 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1106 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001107 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001108
1109 // If the flag result is dead, turn this into an ADD.
1110 if (N->hasNUsesOfValue(0, 1))
1111 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001112 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001113
1114 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001115 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001116 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001117
Chris Lattnerb6541762007-03-04 20:40:38 +00001118 // fold (addc x, 0) -> x + no carry out
1119 if (N1C && N1C->isNullValue())
1120 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1121
1122 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001123 APInt LHSZero, LHSOne;
1124 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001125 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001126 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001127 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001128 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001129
1130 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1131 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1132 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1133 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1134 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1135 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1136 }
Chris Lattner91153682007-03-04 20:03:15 +00001137
Dan Gohman475871a2008-07-27 21:46:04 +00001138 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001139}
1140
Dan Gohman475871a2008-07-27 21:46:04 +00001141SDValue DAGCombiner::visitADDE(SDNode *N) {
1142 SDValue N0 = N->getOperand(0);
1143 SDValue N1 = N->getOperand(1);
1144 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001145 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1146 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001147 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001148
1149 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001150 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001151 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001152
Chris Lattnerb6541762007-03-04 20:40:38 +00001153 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001154 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001155 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001156
Dan Gohman475871a2008-07-27 21:46:04 +00001157 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001158}
1159
1160
1161
Dan Gohman475871a2008-07-27 21:46:04 +00001162SDValue DAGCombiner::visitSUB(SDNode *N) {
1163 SDValue N0 = N->getOperand(0);
1164 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001165 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1166 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001167 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001168
Dan Gohman7f321562007-06-25 16:23:39 +00001169 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001170 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001171 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001172 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001173 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001174
Chris Lattner854077d2005-10-17 01:07:11 +00001175 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001176 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001177 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001180 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001181 // fold (sub x, c) -> (add x, -c)
1182 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001183 return DAG.getNode(ISD::ADD, VT, N0,
1184 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001185 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001186 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001187 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001188 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001189 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001190 return N0.getOperand(0);
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001191 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001192 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001193 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1194 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001195 N0.getOperand(1).getOperand(0) == N1)
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001196 return DAG.getNode(N0.getOperand(1).getOpcode(), VT, N0.getOperand(0),
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001197 N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001198 // fold ((A+(C+B))-B) -> A+C
1199 if (N0.getOpcode() == ISD::ADD &&
1200 N0.getOperand(1).getOpcode() == ISD::ADD &&
1201 N0.getOperand(1).getOperand(1) == N1)
1202 return DAG.getNode(ISD::ADD, VT, N0.getOperand(0),
1203 N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001204 // fold ((A-(B-C))-C) -> A-B
1205 if (N0.getOpcode() == ISD::SUB &&
1206 N0.getOperand(1).getOpcode() == ISD::SUB &&
1207 N0.getOperand(1).getOperand(1) == N1)
1208 return DAG.getNode(ISD::SUB, VT, N0.getOperand(0),
1209 N0.getOperand(1).getOperand(0));
Evan Chengb13cdbd2007-06-21 07:39:16 +00001210 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Gabor Greifba36cb52008-08-28 21:40:38 +00001211 if (N1.getOpcode() == ISD::SELECT && N1.getNode()->hasOneUse()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00001212 SDValue Result = combineSelectAndUse(N, N1, N0, DAG, TLI, LegalOperations);
Gabor Greifba36cb52008-08-28 21:40:38 +00001213 if (Result.getNode()) return Result;
Evan Chengb13cdbd2007-06-21 07:39:16 +00001214 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001215 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001216 if (N0.getOpcode() == ISD::UNDEF)
1217 return N0;
1218 if (N1.getOpcode() == ISD::UNDEF)
1219 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001220
Dan Gohman6520e202008-10-18 02:06:02 +00001221 // If the relocation model supports it, consider symbol offsets.
1222 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001223 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001224 // fold (sub Sym, c) -> Sym-c
1225 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1226 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1227 GA->getOffset() -
1228 (uint64_t)N1C->getSExtValue());
1229 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1230 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1231 if (GA->getGlobal() == GB->getGlobal())
1232 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1233 VT);
1234 }
1235
Dan Gohman475871a2008-07-27 21:46:04 +00001236 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001237}
1238
Dan Gohman475871a2008-07-27 21:46:04 +00001239SDValue DAGCombiner::visitMUL(SDNode *N) {
1240 SDValue N0 = N->getOperand(0);
1241 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001242 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1243 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001245
Dan Gohman7f321562007-06-25 16:23:39 +00001246 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001247 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001248 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001249 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001250 }
Dan Gohman7f321562007-06-25 16:23:39 +00001251
Dan Gohman613e0d82007-07-03 14:03:57 +00001252 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001253 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001254 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001255 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001256 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001257 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001258 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001259 if (N0C && !N1C)
1260 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001261 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001262 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001263 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001264 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001265 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001266 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001267 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001268 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001269 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001270 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001271 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001272 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Dan Gohman7810bfe2008-09-26 21:54:37 +00001273 if (N1C && isPowerOf2_64(-N1C->getSExtValue())) {
Chris Lattner3e6099b2005-10-30 06:41:49 +00001274 // FIXME: If the input is something that is easily negated (e.g. a
1275 // single-use add), we should put the negate there.
1276 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1277 DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman7810bfe2008-09-26 21:54:37 +00001278 DAG.getConstant(Log2_64(-N1C->getSExtValue()),
Chris Lattner3e6099b2005-10-30 06:41:49 +00001279 TLI.getShiftAmountTy())));
1280 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001281
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001282 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1283 if (N1C && N0.getOpcode() == ISD::SHL &&
1284 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001285 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 AddToWorkList(C3.getNode());
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001287 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1288 }
1289
1290 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1291 // use.
1292 {
Dan Gohman475871a2008-07-27 21:46:04 +00001293 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001294 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1295 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001296 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001297 Sh = N0; Y = N1;
1298 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001299 isa<ConstantSDNode>(N1.getOperand(1)) &&
1300 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001301 Sh = N1; Y = N0;
1302 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 if (Sh.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001304 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001305 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1306 }
1307 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001308 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001309 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Chris Lattnera1deca32006-03-04 23:33:26 +00001310 isa<ConstantSDNode>(N0.getOperand(1))) {
1311 return DAG.getNode(ISD::ADD, VT,
1312 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1313 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1314 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001315
Nate Begemancd4d58c2006-02-03 06:46:56 +00001316 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001317 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001318 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001319 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001320
Dan Gohman475871a2008-07-27 21:46:04 +00001321 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001322}
1323
Dan Gohman475871a2008-07-27 21:46:04 +00001324SDValue DAGCombiner::visitSDIV(SDNode *N) {
1325 SDValue N0 = N->getOperand(0);
1326 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001327 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1328 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001329 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001330
Dan Gohman7f321562007-06-25 16:23:39 +00001331 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001332 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001333 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001334 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001335 }
Dan Gohman7f321562007-06-25 16:23:39 +00001336
Nate Begeman1d4d4142005-09-01 00:19:25 +00001337 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001338 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001339 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001340 // fold (sdiv X, 1) -> X
Dan Gohman7810bfe2008-09-26 21:54:37 +00001341 if (N1C && N1C->getSExtValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001342 return N0;
1343 // fold (sdiv X, -1) -> 0-X
1344 if (N1C && N1C->isAllOnesValue())
1345 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001346 // If we know the sign bits of both operands are zero, strength reduce to a
1347 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001348 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001349 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001350 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1351 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001352 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001353 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Dan Gohman7810bfe2008-09-26 21:54:37 +00001354 (isPowerOf2_64(N1C->getSExtValue()) ||
1355 isPowerOf2_64(-N1C->getSExtValue()))) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001356 // If dividing by powers of two is cheap, then don't perform the following
1357 // fold.
1358 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001359 return SDValue();
Dan Gohman7810bfe2008-09-26 21:54:37 +00001360 int64_t pow2 = N1C->getSExtValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001361 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001362 unsigned lg2 = Log2_64(abs2);
1363 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001364 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001365 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001366 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00001367 AddToWorkList(SGN.getNode());
Chris Lattner8f4880b2006-02-16 08:02:36 +00001368 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001369 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001370 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001371 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001373 AddToWorkList(SRL.getNode());
1374 AddToWorkList(ADD.getNode()); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001375 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001376 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001377 // If we're dividing by a positive value, we're done. Otherwise, we must
1378 // negate the result.
1379 if (pow2 > 0)
1380 return SRA;
Gabor Greifba36cb52008-08-28 21:40:38 +00001381 AddToWorkList(SRA.getNode());
Nate Begeman405e3ec2005-10-21 00:02:42 +00001382 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1383 }
Nate Begeman69575232005-10-20 02:15:44 +00001384 // if integer divide is expensive and we satisfy the requirements, emit an
1385 // alternate sequence.
Dan Gohman7810bfe2008-09-26 21:54:37 +00001386 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001387 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001389 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001390 }
Dan Gohman7f321562007-06-25 16:23:39 +00001391
Dan Gohman613e0d82007-07-03 14:03:57 +00001392 // undef / X -> 0
1393 if (N0.getOpcode() == ISD::UNDEF)
1394 return DAG.getConstant(0, VT);
1395 // X / undef -> undef
1396 if (N1.getOpcode() == ISD::UNDEF)
1397 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001398
Dan Gohman475871a2008-07-27 21:46:04 +00001399 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001400}
1401
Dan Gohman475871a2008-07-27 21:46:04 +00001402SDValue DAGCombiner::visitUDIV(SDNode *N) {
1403 SDValue N0 = N->getOperand(0);
1404 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001405 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1406 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Duncan Sands83ec4b62008-06-06 12:08:01 +00001407 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408
Dan Gohman7f321562007-06-25 16:23:39 +00001409 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001410 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001412 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001413 }
Dan Gohman7f321562007-06-25 16:23:39 +00001414
Nate Begeman1d4d4142005-09-01 00:19:25 +00001415 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001416 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001417 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001418 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001419 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001420 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001421 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001422 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001423 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1424 if (N1.getOpcode() == ISD::SHL) {
1425 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001426 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001427 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001428 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001429 DAG.getConstant(SHC->getAPIntValue()
1430 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001431 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001432 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001433 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001434 }
1435 }
1436 }
Nate Begeman69575232005-10-20 02:15:44 +00001437 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001438 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001439 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001440 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001441 }
Dan Gohman7f321562007-06-25 16:23:39 +00001442
Dan Gohman613e0d82007-07-03 14:03:57 +00001443 // undef / X -> 0
1444 if (N0.getOpcode() == ISD::UNDEF)
1445 return DAG.getConstant(0, VT);
1446 // X / undef -> undef
1447 if (N1.getOpcode() == ISD::UNDEF)
1448 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001449
Dan Gohman475871a2008-07-27 21:46:04 +00001450 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451}
1452
Dan Gohman475871a2008-07-27 21:46:04 +00001453SDValue DAGCombiner::visitSREM(SDNode *N) {
1454 SDValue N0 = N->getOperand(0);
1455 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001456 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1457 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459
1460 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001462 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001463 // If we know the sign bits of both operands are zero, strength reduce to a
1464 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001465 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001466 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001467 return DAG.getNode(ISD::UREM, VT, N0, N1);
1468 }
Chris Lattner26d29902006-10-12 20:58:32 +00001469
Dan Gohman77003042007-11-26 23:46:11 +00001470 // If X/C can be simplified by the division-by-constant logic, lower
1471 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001472 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001473 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001474 AddToWorkList(Div.getNode());
1475 SDValue OptimizedDiv = combine(Div.getNode());
1476 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001477 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1478 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001479 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001480 return Sub;
1481 }
Chris Lattner26d29902006-10-12 20:58:32 +00001482 }
1483
Dan Gohman613e0d82007-07-03 14:03:57 +00001484 // undef % X -> 0
1485 if (N0.getOpcode() == ISD::UNDEF)
1486 return DAG.getConstant(0, VT);
1487 // X % undef -> undef
1488 if (N1.getOpcode() == ISD::UNDEF)
1489 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001490
Dan Gohman475871a2008-07-27 21:46:04 +00001491 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001492}
1493
Dan Gohman475871a2008-07-27 21:46:04 +00001494SDValue DAGCombiner::visitUREM(SDNode *N) {
1495 SDValue N0 = N->getOperand(0);
1496 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001497 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001499 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001500
1501 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001502 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001503 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001504 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001505 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1506 return DAG.getNode(ISD::AND, VT, N0,
1507 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001508 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1509 if (N1.getOpcode() == ISD::SHL) {
1510 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001511 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001512 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001513 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001514 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001515 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001516 AddToWorkList(Add.getNode());
Nate Begemanc031e332006-02-05 07:36:48 +00001517 return DAG.getNode(ISD::AND, VT, N0, Add);
1518 }
1519 }
1520 }
Chris Lattner26d29902006-10-12 20:58:32 +00001521
Dan Gohman77003042007-11-26 23:46:11 +00001522 // If X/C can be simplified by the division-by-constant logic, lower
1523 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001524 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001525 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00001526 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 SDValue OptimizedDiv = combine(Div.getNode());
1528 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1530 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001531 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001532 return Sub;
1533 }
Chris Lattner26d29902006-10-12 20:58:32 +00001534 }
1535
Dan Gohman613e0d82007-07-03 14:03:57 +00001536 // undef % X -> 0
1537 if (N0.getOpcode() == ISD::UNDEF)
1538 return DAG.getConstant(0, VT);
1539 // X % undef -> undef
1540 if (N1.getOpcode() == ISD::UNDEF)
1541 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001542
Dan Gohman475871a2008-07-27 21:46:04 +00001543 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001544}
1545
Dan Gohman475871a2008-07-27 21:46:04 +00001546SDValue DAGCombiner::visitMULHS(SDNode *N) {
1547 SDValue N0 = N->getOperand(0);
1548 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001549 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001550 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001551
1552 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001553 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001554 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001555 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001556 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001557 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001558 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001559 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001560 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001561 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001562 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001563
Dan Gohman475871a2008-07-27 21:46:04 +00001564 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001565}
1566
Dan Gohman475871a2008-07-27 21:46:04 +00001567SDValue DAGCombiner::visitMULHU(SDNode *N) {
1568 SDValue N0 = N->getOperand(0);
1569 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001570 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001571 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001572
1573 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001574 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001575 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001576 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001577 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001578 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001579 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001580 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001581 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001582
Dan Gohman475871a2008-07-27 21:46:04 +00001583 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001584}
1585
Dan Gohman389079b2007-10-08 17:57:15 +00001586/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1587/// compute two values. LoOp and HiOp give the opcodes for the two computations
1588/// that are being performed. Return true if a simplification was made.
1589///
Dan Gohman475871a2008-07-27 21:46:04 +00001590SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1591 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001592 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001593 bool HiExists = N->hasAnyUseOfValue(1);
1594 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001595 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001596 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001597 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001598 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001599 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001600 }
1601
1602 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001603 bool LoExists = N->hasAnyUseOfValue(0);
1604 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001605 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00001606 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001607 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001608 N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001609 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001610 }
1611
Evan Cheng44711942007-11-08 09:25:29 +00001612 // If both halves are used, return as it is.
1613 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001614 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001615
1616 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001617 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001619 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001620 AddToWorkList(Lo.getNode());
1621 SDValue LoOpt = combine(Lo.getNode());
1622 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001623 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001624 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001625 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001626 }
1627
Evan Cheng44711942007-11-08 09:25:29 +00001628 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001629 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00001630 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00001631 AddToWorkList(Hi.getNode());
1632 SDValue HiOpt = combine(Hi.getNode());
1633 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001634 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001635 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001636 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001637 }
Dan Gohman475871a2008-07-27 21:46:04 +00001638 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001639}
1640
Dan Gohman475871a2008-07-27 21:46:04 +00001641SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1642 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00001643 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001644
Dan Gohman475871a2008-07-27 21:46:04 +00001645 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001646}
1647
Dan Gohman475871a2008-07-27 21:46:04 +00001648SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1649 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00001650 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001651
Dan Gohman475871a2008-07-27 21:46:04 +00001652 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001653}
1654
Dan Gohman475871a2008-07-27 21:46:04 +00001655SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1656 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001657 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001658
Dan Gohman475871a2008-07-27 21:46:04 +00001659 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001660}
1661
Dan Gohman475871a2008-07-27 21:46:04 +00001662SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1663 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00001664 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001665
Dan Gohman475871a2008-07-27 21:46:04 +00001666 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001667}
1668
Chris Lattner35e5c142006-05-05 05:51:50 +00001669/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1670/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001671SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1672 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001673 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001674 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1675
Chris Lattner540121f2006-05-05 06:31:05 +00001676 // For each of OP in AND/OR/XOR:
1677 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1678 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1679 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001680 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001681 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001682 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001683 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001685 N0.getOperand(0).getValueType(),
1686 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001687 AddToWorkList(ORNode.getNode());
Chris Lattner540121f2006-05-05 06:31:05 +00001688 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001689 }
1690
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001691 // For each of OP in SHL/SRL/SRA/AND...
1692 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1693 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1694 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001695 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001696 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001697 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001698 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001699 N0.getOperand(0).getValueType(),
1700 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001701 AddToWorkList(ORNode.getNode());
Chris Lattner35e5c142006-05-05 05:51:50 +00001702 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1703 }
1704
Dan Gohman475871a2008-07-27 21:46:04 +00001705 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001706}
1707
Dan Gohman475871a2008-07-27 21:46:04 +00001708SDValue DAGCombiner::visitAND(SDNode *N) {
1709 SDValue N0 = N->getOperand(0);
1710 SDValue N1 = N->getOperand(1);
1711 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001712 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1713 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001714 MVT VT = N1.getValueType();
1715 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001716
Dan Gohman7f321562007-06-25 16:23:39 +00001717 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001718 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001719 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001720 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001721 }
Dan Gohman7f321562007-06-25 16:23:39 +00001722
Dan Gohman613e0d82007-07-03 14:03:57 +00001723 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001724 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001725 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001726 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001727 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001728 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001729 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001730 if (N0C && !N1C)
1731 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001732 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001733 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001734 return N0;
1735 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001736 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001737 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001738 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001739 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001740 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001741 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001742 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001743 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001744 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001745 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001746 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001747 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001748 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1749 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001750 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001751 APInt Mask = ~N1C->getAPIntValue();
1752 Mask.trunc(N0Op0.getValueSizeInBits());
1753 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001754 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001755 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001756
1757 // Replace uses of the AND with uses of the Zero extend node.
1758 CombineTo(N, Zext);
1759
Chris Lattner3603cd62006-02-02 07:17:31 +00001760 // We actually want to replace all uses of the any_extend with the
1761 // zero_extend, to avoid duplicating things. This will later cause this
1762 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00001763 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001764 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001765 }
1766 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001767 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1768 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1769 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1770 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1771
1772 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001773 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001774 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001775 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001776 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001777 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001778 return DAG.getSetCC(VT, ORNode, LR, Op1);
1779 }
1780 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1781 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001782 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001783 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001784 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1785 }
1786 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1787 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001789 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001790 return DAG.getSetCC(VT, ORNode, LR, Op1);
1791 }
1792 }
1793 // canonicalize equivalent to ll == rl
1794 if (LL == RR && LR == RL) {
1795 Op1 = ISD::getSetCCSwappedOperands(Op1);
1796 std::swap(RL, RR);
1797 }
1798 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001799 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001800 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001801 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001802 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001803 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1804 }
1805 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001806
1807 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1808 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001809 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001810 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001811 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001812
Nate Begemande996292006-02-03 22:24:05 +00001813 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1814 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001815 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001816 SimplifyDemandedBits(SDValue(N, 0)))
1817 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001818 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00001819 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00001820 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001821 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001822 // If we zero all the possible extended bits, then we can turn this into
1823 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001824 unsigned BitWidth = N1.getValueSizeInBits();
1825 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001827 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001828 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001829 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001830 LN0->getBasePtr(), LN0->getSrcValue(),
1831 LN0->getSrcValueOffset(), EVT,
1832 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001833 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001834 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001835 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001836 }
1837 }
1838 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00001839 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00001840 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001841 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001842 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001843 // If we zero all the possible extended bits, then we can turn this into
1844 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001845 unsigned BitWidth = N1.getValueSizeInBits();
1846 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001847 BitWidth - EVT.getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001848 ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00001849 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00001851 LN0->getBasePtr(), LN0->getSrcValue(),
1852 LN0->getSrcValueOffset(), EVT,
1853 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001854 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001855 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001856 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001857 }
1858 }
Chris Lattner15045b62006-02-28 06:35:35 +00001859
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001860 // fold (and (load x), 255) -> (zextload x, i8)
1861 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001862 if (N1C && N0.getOpcode() == ISD::LOAD) {
1863 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1864 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001865 LN0->isUnindexed() && N0.hasOneUse() &&
1866 // Do not change the width of a volatile load.
1867 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001868 MVT EVT = MVT::Other;
1869 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1870 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1871 EVT = MVT::getIntegerVT(ActiveBits);
1872
1873 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001874 // Do not generate loads of non-round integer types since these can
1875 // be expensive (and would be wrong if the type is not byte sized).
1876 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001877 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001878 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001879 // For big endian targets, we need to add an offset to the pointer to
1880 // load the correct bytes. For little endian systems, we merely need to
1881 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001882 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1883 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001884 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001885 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001886 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001887 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001888 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1889 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001890 Alignment = MinAlign(Alignment, PtrOff);
1891 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001892 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001894 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001895 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001896 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001897 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001899 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001900 }
Chris Lattner15045b62006-02-28 06:35:35 +00001901 }
1902 }
1903
Dan Gohman475871a2008-07-27 21:46:04 +00001904 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001905}
1906
Dan Gohman475871a2008-07-27 21:46:04 +00001907SDValue DAGCombiner::visitOR(SDNode *N) {
1908 SDValue N0 = N->getOperand(0);
1909 SDValue N1 = N->getOperand(1);
1910 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001911 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1912 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001913 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001914
Dan Gohman7f321562007-06-25 16:23:39 +00001915 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001916 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001917 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001918 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001919 }
Dan Gohman7f321562007-06-25 16:23:39 +00001920
Dan Gohman613e0d82007-07-03 14:03:57 +00001921 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001922 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001923 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001924 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001925 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001926 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001927 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001928 if (N0C && !N1C)
1929 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001932 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001934 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001935 return N1;
1936 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001937 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001938 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001939 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001940 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001942 return ROR;
1943 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greifba36cb52008-08-28 21:40:38 +00001944 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001945 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001946 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1947 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1948 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001949 DAG.getConstant(N1C->getAPIntValue() |
1950 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001951 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001952 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1953 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1954 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1955 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1956
1957 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001958 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001959 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1960 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001961 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001962 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001964 AddToWorkList(ORNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001965 return DAG.getSetCC(VT, ORNode, LR, Op1);
1966 }
1967 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1968 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1969 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1970 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001971 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001972 AddToWorkList(ANDNode.getNode());
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001973 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1974 }
1975 }
1976 // canonicalize equivalent to ll == rl
1977 if (LL == RR && LR == RL) {
1978 Op1 = ISD::getSetCCSwappedOperands(Op1);
1979 std::swap(RL, RR);
1980 }
1981 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001982 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001983 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00001984 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00001985 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001986 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1987 }
1988 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001989
1990 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1991 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001993 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001994 }
Chris Lattner516b9622006-09-14 20:50:57 +00001995
Chris Lattner1ec72732006-09-14 21:11:37 +00001996 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1997 if (N0.getOpcode() == ISD::AND &&
1998 N1.getOpcode() == ISD::AND &&
1999 N0.getOperand(1).getOpcode() == ISD::Constant &&
2000 N1.getOperand(1).getOpcode() == ISD::Constant &&
2001 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00002002 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00002003 // We can only do this xform if we know that bits from X that are set in C2
2004 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002005 const APInt &LHSMask =
2006 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2007 const APInt &RHSMask =
2008 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00002009
Dan Gohmanea859be2007-06-22 14:59:07 +00002010 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2011 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002012 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00002013 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
2014 }
2015 }
2016
2017
Chris Lattner516b9622006-09-14 20:50:57 +00002018 // See if this is some rotate idiom.
2019 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00002020 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00002021
Dan Gohman475871a2008-07-27 21:46:04 +00002022 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002023}
2024
Chris Lattner516b9622006-09-14 20:50:57 +00002025
2026/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002027static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00002028 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00002029 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00002030 Mask = Op.getOperand(1);
2031 Op = Op.getOperand(0);
2032 } else {
2033 return false;
2034 }
2035 }
2036
2037 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2038 Shift = Op;
2039 return true;
2040 }
2041 return false;
2042}
2043
2044
2045// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2046// idioms for rotate, and if the target supports rotation instructions, generate
2047// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00002048SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002049 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002050 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00002051 if (!TLI.isTypeLegal(VT)) return 0;
2052
2053 // The target must have at least one rotate flavor.
2054 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
2055 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
2056 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002057
Chris Lattner516b9622006-09-14 20:50:57 +00002058 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SDValue LHSShift; // The shift.
2060 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002061 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2062 return 0; // Not part of a rotate.
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue RHSShift; // The shift.
2065 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00002066 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2067 return 0; // Not part of a rotate.
2068
2069 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2070 return 0; // Not shifting the same value.
2071
2072 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2073 return 0; // Shifts must disagree.
2074
2075 // Canonicalize shl to left side in a shl/srl pair.
2076 if (RHSShift.getOpcode() == ISD::SHL) {
2077 std::swap(LHS, RHS);
2078 std::swap(LHSShift, RHSShift);
2079 std::swap(LHSMask , RHSMask );
2080 }
2081
Duncan Sands83ec4b62008-06-06 12:08:01 +00002082 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue LHSShiftArg = LHSShift.getOperand(0);
2084 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2085 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00002086
2087 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2088 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002089 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2090 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002091 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2092 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002093 if ((LShVal + RShVal) != OpSizeInBits)
2094 return 0;
2095
Dan Gohman475871a2008-07-27 21:46:04 +00002096 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00002097 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002098 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002099 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002100 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002101
2102 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00002103 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002104 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002105
Gabor Greifba36cb52008-08-28 21:40:38 +00002106 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002107 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2108 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002109 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002110 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00002111 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2112 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002113 }
2114
2115 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2116 }
2117
Gabor Greifba36cb52008-08-28 21:40:38 +00002118 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002119 }
2120
2121 // If there is a mask here, and we have a variable shift, we can't be sure
2122 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00002123 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00002124 return 0;
2125
2126 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2127 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002128 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2129 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002130 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002131 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002132 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002133 if (HasROTL)
Gabor Greifba36cb52008-08-28 21:40:38 +00002134 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00002135 else
Gabor Greifba36cb52008-08-28 21:40:38 +00002136 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002137 }
Chris Lattner516b9622006-09-14 20:50:57 +00002138 }
2139 }
2140
2141 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2142 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002143 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2144 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002145 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002146 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002147 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00002148 if (HasROTR)
Gabor Greifba36cb52008-08-28 21:40:38 +00002149 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00002150 else
2151 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002152 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002153 }
2154 }
2155
Dan Gohman74feef22008-10-17 01:23:35 +00002156 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00002157 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2158 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002159 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2160 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00002161 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2162 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00002163 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2164 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002165 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2166 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002167 if (RExtOp0.getOpcode() == ISD::SUB &&
2168 RExtOp0.getOperand(1) == LExtOp0) {
2169 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002170 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00002171 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002172 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002173 if (ConstantSDNode *SUBC =
2174 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002175 if (SUBC->getAPIntValue() == OpSizeInBits) {
Gabor Greif12632d22008-08-30 19:29:20 +00002176 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, VT, LHSShiftArg,
2177 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002178 }
2179 }
2180 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2181 RExtOp0 == LExtOp0.getOperand(1)) {
Bill Wendling353dea22008-08-31 01:04:56 +00002182 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002183 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00002184 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00002185 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00002186 if (ConstantSDNode *SUBC =
2187 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002188 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling353dea22008-08-31 01:04:56 +00002189 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, VT, LHSShiftArg,
2190 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00002191 }
2192 }
Chris Lattner516b9622006-09-14 20:50:57 +00002193 }
2194 }
2195
2196 return 0;
2197}
2198
2199
Dan Gohman475871a2008-07-27 21:46:04 +00002200SDValue DAGCombiner::visitXOR(SDNode *N) {
2201 SDValue N0 = N->getOperand(0);
2202 SDValue N1 = N->getOperand(1);
2203 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002204 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2205 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002206 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002207
Dan Gohman7f321562007-06-25 16:23:39 +00002208 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002210 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002211 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002212 }
Dan Gohman7f321562007-06-25 16:23:39 +00002213
Evan Cheng26471c42008-03-25 20:08:07 +00002214 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2215 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2216 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002217 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002218 if (N0.getOpcode() == ISD::UNDEF)
2219 return N0;
2220 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002221 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002222 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002223 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002224 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002225 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002226 if (N0C && !N1C)
2227 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002228 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002229 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002230 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002231 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002232 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002233 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002234 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00002235
Nate Begeman1d4d4142005-09-01 00:19:25 +00002236 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002237 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002238 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002239 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2240 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00002241
Duncan Sands25cf2272008-11-24 14:53:14 +00002242 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00002243 switch (N0.getOpcode()) {
2244 default:
2245 assert(0 && "Unhandled SetCC Equivalent!");
2246 abort();
2247 case ISD::SETCC:
2248 return DAG.getSetCC(VT, LHS, RHS, NotCC);
2249 case ISD::SELECT_CC:
2250 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),
2251 N0.getOperand(3), NotCC);
2252 }
2253 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00002254 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00002255
Chris Lattner61c5ff42007-09-10 21:39:07 +00002256 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002257 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00002258 N0.getNode()->hasOneUse() &&
2259 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002260 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002261 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002262 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002263 AddToWorkList(V.getNode());
Chris Lattner61c5ff42007-09-10 21:39:07 +00002264 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2265 }
2266
Nate Begeman99801192005-09-07 23:25:52 +00002267 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002268 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002269 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002270 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002271 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2272 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002273 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2274 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002275 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002276 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002277 }
2278 }
Nate Begeman99801192005-09-07 23:25:52 +00002279 // fold !(x or y) -> (!x and !y) iff x or y are constants
2280 if (N1C && N1C->isAllOnesValue() &&
2281 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002282 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002283 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2284 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002285 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2286 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002287 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Nate Begeman99801192005-09-07 23:25:52 +00002288 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002289 }
2290 }
Nate Begeman223df222005-09-08 20:18:10 +00002291 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2292 if (N1C && N0.getOpcode() == ISD::XOR) {
2293 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2294 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2295 if (N00C)
2296 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002297 DAG.getConstant(N1C->getAPIntValue()^
2298 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002299 if (N01C)
2300 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002301 DAG.getConstant(N1C->getAPIntValue()^
2302 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002303 }
2304 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002305 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002306 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002307 return DAG.getConstant(0, VT);
Duncan Sands25cf2272008-11-24 14:53:14 +00002308 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Chris Lattner4fbdd592006-03-28 19:11:05 +00002309 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2311 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002312 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002313 }
2314 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002315
2316 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2317 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002318 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002319 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002320 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002321
Chris Lattner3e104b12006-04-08 04:15:24 +00002322 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002323 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002324 SimplifyDemandedBits(SDValue(N, 0)))
2325 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002326
Dan Gohman475871a2008-07-27 21:46:04 +00002327 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002328}
2329
Chris Lattnere70da202007-12-06 07:33:36 +00002330/// visitShiftByConstant - Handle transforms common to the three shifts, when
2331/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002332SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002333 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00002334 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002335
2336 // We want to pull some binops through shifts, so that we have (and (shift))
2337 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2338 // thing happens with address calculations, so it's important to canonicalize
2339 // it.
2340 bool HighBitSet = false; // Can we transform this if the high bit is set?
2341
2342 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002343 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002344 case ISD::OR:
2345 case ISD::XOR:
2346 HighBitSet = false; // We can only transform sra if the high bit is clear.
2347 break;
2348 case ISD::AND:
2349 HighBitSet = true; // We can only transform sra if the high bit is set.
2350 break;
2351 case ISD::ADD:
2352 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002353 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002354 HighBitSet = false; // We can only transform sra if the high bit is clear.
2355 break;
2356 }
2357
2358 // We require the RHS of the binop to be a constant as well.
2359 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002360 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002361
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002362
2363 // FIXME: disable this for unless the input to the binop is a shift by a
2364 // constant. If it is not a shift, it pessimizes some common cases like:
2365 //
2366 //void foo(int *X, int i) { X[i & 1235] = 1; }
2367 //int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00002368 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002369 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2370 BinOpLHSVal->getOpcode() != ISD::SRA &&
2371 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2372 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002373 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002374
Duncan Sands83ec4b62008-06-06 12:08:01 +00002375 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002376
2377 // If this is a signed shift right, and the high bit is modified
2378 // by the logical operation, do not perform the transformation.
2379 // The highBitSet boolean indicates the value of the high bit of
2380 // the constant which would cause it to be modified for this
2381 // operation.
2382 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002383 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2384 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002385 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002386 }
2387
2388 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002389 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002390 LHS->getOperand(1), N->getOperand(1));
2391
2392 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002393 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002394 N->getOperand(1));
2395
2396 // Create the new binop.
2397 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2398}
2399
2400
Dan Gohman475871a2008-07-27 21:46:04 +00002401SDValue DAGCombiner::visitSHL(SDNode *N) {
2402 SDValue N0 = N->getOperand(0);
2403 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002404 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002406 MVT VT = N0.getValueType();
2407 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002408
2409 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002410 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002411 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002412 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002413 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002414 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002415 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002416 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002417 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002418 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002419 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002420 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002421 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002422 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002423 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002424 return DAG.getConstant(0, VT);
Evan Chengeb9f8922008-08-30 02:03:58 +00002425 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), c))
2426 // iff (trunc c) == c
2427 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002428 N1.getOperand(0).getOpcode() == ISD::AND &&
2429 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002430 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002431 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002432 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002433 SDValue N100 = N1.getOperand(0).getOperand(0);
2434 return DAG.getNode(ISD::SHL, VT, N0,
2435 DAG.getNode(ISD::AND, TruncVT,
2436 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2437 DAG.getConstant(N101C->getZExtValue(),
2438 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002439 }
2440 }
2441
Dan Gohman475871a2008-07-27 21:46:04 +00002442 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2443 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002444 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002445 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002446 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002447 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2448 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002449 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002450 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002451 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002452 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002453 }
2454 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2455 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002456 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002457 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002458 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2459 uint64_t c2 = N1C->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002460 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461 DAG.getConstant(~0ULL << c1, VT));
2462 if (c2 > c1)
2463 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002464 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002465 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466 return DAG.getNode(ISD::SRL, VT, Mask,
2467 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 }
2469 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002470 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002471 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002472 DAG.getConstant(~0ULL << N1C->getZExtValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002473
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002474 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475}
2476
Dan Gohman475871a2008-07-27 21:46:04 +00002477SDValue DAGCombiner::visitSRA(SDNode *N) {
2478 SDValue N0 = N->getOperand(0);
2479 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002480 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2481 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002482 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002483
2484 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002485 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002486 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002487 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002488 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002489 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002490 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002491 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002492 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002493 // fold (sra x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002494 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002495 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002496 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002497 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002498 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002499 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2500 // sext_inreg.
2501 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002502 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002503 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sands25cf2272008-11-24 14:53:14 +00002504 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002505 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2506 DAG.getValueType(EVT));
2507 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002508
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002509 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2510 if (N1C && N0.getOpcode() == ISD::SRA) {
2511 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002512 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002513 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002514 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2515 DAG.getConstant(Sum, N1C->getValueType(0)));
2516 }
2517 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002518
2519 // fold sra (shl X, m), result_size - n
2520 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002521 // result_size - n != m.
2522 // If truncate is free for the target sext(shl) is likely to result in better
2523 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002524 if (N0.getOpcode() == ISD::SHL) {
2525 // Get the two constanst of the shifts, CN0 = m, CN = n.
2526 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2527 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002528 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 unsigned VTValSize = VT.getSizeInBits();
2530 MVT TruncVT =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002531 MVT::getIntegerVT(VTValSize - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002532 // Determine the residual right-shift amount.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002533 unsigned ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002534
Christopher Lambb9b04282008-03-20 04:31:39 +00002535 // If the shift is not a no-op (in which case this should be just a sign
2536 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greif12632d22008-08-30 19:29:20 +00002537 // on that type, and the the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00002538 // perform the transform.
Duncan Sands25cf2272008-11-24 14:53:14 +00002539 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002540 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2541 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002542 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002543
Dan Gohman475871a2008-07-27 21:46:04 +00002544 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2545 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2546 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002547 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002548 }
2549 }
2550 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002551
Evan Chengeb9f8922008-08-30 02:03:58 +00002552 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), c))
2553 // iff (trunc c) == c
2554 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002555 N1.getOperand(0).getOpcode() == ISD::AND &&
2556 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002557 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002558 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002559 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002560 SDValue N100 = N1.getOperand(0).getOperand(0);
2561 return DAG.getNode(ISD::SRA, VT, N0,
2562 DAG.getNode(ISD::AND, TruncVT,
2563 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2564 DAG.getConstant(N101C->getZExtValue(),
2565 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002566 }
2567 }
2568
Chris Lattnera8504462006-05-08 20:51:54 +00002569 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002570 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2571 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002572
2573
Nate Begeman1d4d4142005-09-01 00:19:25 +00002574 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002575 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002576 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002577
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002578 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002579}
2580
Dan Gohman475871a2008-07-27 21:46:04 +00002581SDValue DAGCombiner::visitSRL(SDNode *N) {
2582 SDValue N0 = N->getOperand(0);
2583 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002584 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2585 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002586 MVT VT = N0.getValueType();
2587 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002588
2589 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002590 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002591 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002592 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002593 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002594 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002595 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002596 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002597 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002598 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002599 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002600 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002601 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002602 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002603 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002604 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002605
Nate Begeman1d4d4142005-09-01 00:19:25 +00002606 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002607 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002608 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002609 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2610 uint64_t c2 = N1C->getZExtValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002611 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002612 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002613 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002614 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002615 }
Chris Lattner350bec02006-04-02 06:11:11 +00002616
Chris Lattner06afe072006-05-05 22:53:17 +00002617 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2618 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2619 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002620 MVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002621 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002622 return DAG.getNode(ISD::UNDEF, VT);
2623
Dan Gohman475871a2008-07-27 21:46:04 +00002624 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002625 AddToWorkList(SmallShift.getNode());
Chris Lattner06afe072006-05-05 22:53:17 +00002626 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2627 }
2628
Chris Lattner3657ffe2006-10-12 20:23:19 +00002629 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2630 // bit, which is unmodified by sra.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002631 if (N1C && N1C->getZExtValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002632 if (N0.getOpcode() == ISD::SRA)
2633 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2634 }
2635
Chris Lattner350bec02006-04-02 06:11:11 +00002636 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2637 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002638 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002639 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002640 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002641 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002642
2643 // If any of the input bits are KnownOne, then the input couldn't be all
2644 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002645 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002646
2647 // If all of the bits input the to ctlz node are known to be zero, then
2648 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002649 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002650 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2651
2652 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2653 if ((UnknownBits & (UnknownBits-1)) == 0) {
2654 // Okay, we know that only that the single bit specified by UnknownBits
2655 // could be set on input to the CTLZ node. If this bit is set, the SRL
2656 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2657 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002658 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002659 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002660 if (ShAmt) {
2661 Op = DAG.getNode(ISD::SRL, VT, Op,
2662 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00002663 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00002664 }
2665 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2666 }
2667 }
Evan Chengeb9f8922008-08-30 02:03:58 +00002668
2669 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), c))
2670 // iff (trunc c) == c
2671 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00002672 N1.getOperand(0).getOpcode() == ISD::AND &&
2673 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002674 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00002675 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Evan Chengeb9f8922008-08-30 02:03:58 +00002676 MVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00002677 SDValue N100 = N1.getOperand(0).getOperand(0);
2678 return DAG.getNode(ISD::SRL, VT, N0,
2679 DAG.getNode(ISD::AND, TruncVT,
2680 DAG.getNode(ISD::TRUNCATE, TruncVT, N100),
2681 DAG.getConstant(N101C->getZExtValue(),
2682 TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00002683 }
2684 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002685
2686 // fold operands of srl based on knowledge that the low bits are not
2687 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002688 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2689 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002690
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002691 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002692}
2693
Dan Gohman475871a2008-07-27 21:46:04 +00002694SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2695 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002696 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002697
2698 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002699 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002700 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002701 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002702}
2703
Dan Gohman475871a2008-07-27 21:46:04 +00002704SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2705 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002706 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002707
2708 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002709 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002710 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002711 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002712}
2713
Dan Gohman475871a2008-07-27 21:46:04 +00002714SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2715 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002716 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002717
2718 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002719 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002720 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002721 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002722}
2723
Dan Gohman475871a2008-07-27 21:46:04 +00002724SDValue DAGCombiner::visitSELECT(SDNode *N) {
2725 SDValue N0 = N->getOperand(0);
2726 SDValue N1 = N->getOperand(1);
2727 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002728 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2729 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2730 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002731 MVT VT = N->getValueType(0);
2732 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002733
Nate Begeman452d7be2005-09-16 00:54:12 +00002734 // fold select C, X, X -> X
2735 if (N1 == N2)
2736 return N1;
2737 // fold select true, X, Y -> X
2738 if (N0C && !N0C->isNullValue())
2739 return N1;
2740 // fold select false, X, Y -> Y
2741 if (N0C && N0C->isNullValue())
2742 return N2;
2743 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002744 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002745 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002746 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002747 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002748 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002749 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002750 if (VT == VT0)
2751 return XORNode;
Gabor Greifba36cb52008-08-28 21:40:38 +00002752 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00002753 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002754 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2755 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2756 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002757 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002758 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002759 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002760 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002761 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2762 }
2763 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002764 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002765 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002766 AddToWorkList(XORNode.getNode());
Nate Begeman452d7be2005-09-16 00:54:12 +00002767 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2768 }
2769 // fold select C, X, 0 -> C & X
2770 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002771 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002772 return DAG.getNode(ISD::AND, VT, N0, N1);
2773 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002774 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002775 return DAG.getNode(ISD::OR, VT, N0, N2);
2776 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002777 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002778 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002779
Chris Lattner40c62d52005-10-18 06:04:22 +00002780 // If we can fold this based on the true/false value, do so.
2781 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002782 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002783
Nate Begeman44728a72005-09-19 22:34:01 +00002784 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002785 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002786 // FIXME:
2787 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2788 // having to say they don't support SELECT_CC on every type the DAG knows
2789 // about, since there is no way to mark an opcode illegal at all value types
2790 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2791 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2792 N1, N2, N0.getOperand(2));
2793 else
2794 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002795 }
Dan Gohman475871a2008-07-27 21:46:04 +00002796 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002797}
2798
Dan Gohman475871a2008-07-27 21:46:04 +00002799SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2800 SDValue N0 = N->getOperand(0);
2801 SDValue N1 = N->getOperand(1);
2802 SDValue N2 = N->getOperand(2);
2803 SDValue N3 = N->getOperand(3);
2804 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002805 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2806
Nate Begeman44728a72005-09-19 22:34:01 +00002807 // fold select_cc lhs, rhs, x, x, cc -> x
2808 if (N2 == N3)
2809 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002810
Chris Lattner5f42a242006-09-20 06:19:26 +00002811 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00002812 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
2813 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00002814 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00002815
Gabor Greifba36cb52008-08-28 21:40:38 +00002816 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002817 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002818 return N2; // cond always true -> true val
2819 else
2820 return N3; // cond always false -> false val
2821 }
2822
2823 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00002824 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Chris Lattner5f42a242006-09-20 06:19:26 +00002825 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2826 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2827 SCC.getOperand(2));
2828
Chris Lattner40c62d52005-10-18 06:04:22 +00002829 // If we can fold this based on the true/false value, do so.
2830 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002831 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002832
Nate Begeman44728a72005-09-19 22:34:01 +00002833 // fold select_cc into other things, such as min/max/abs
2834 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002835}
2836
Dan Gohman475871a2008-07-27 21:46:04 +00002837SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002838 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2839 cast<CondCodeSDNode>(N->getOperand(2))->get());
2840}
2841
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002842// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2843// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2844// transformation. Returns true if extension are possible and the above
2845// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002846static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002847 unsigned ExtOpc,
2848 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00002849 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002850 bool HasCopyToRegUses = false;
2851 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00002852 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2853 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002854 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002855 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002856 if (User == N)
2857 continue;
2858 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2859 if (User->getOpcode() == ISD::SETCC) {
2860 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2861 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2862 // Sign bits will be lost after a zext.
2863 return false;
2864 bool Add = false;
2865 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002866 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002867 if (UseOp == N0)
2868 continue;
2869 if (!isa<ConstantSDNode>(UseOp))
2870 return false;
2871 Add = true;
2872 }
2873 if (Add)
2874 ExtendNodes.push_back(User);
2875 } else {
2876 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002877 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002878 if (UseOp == N0) {
2879 // If truncate from extended type to original load type is free
2880 // on this target, then it's ok to extend a CopyToReg.
2881 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2882 HasCopyToRegUses = true;
2883 else
2884 return false;
2885 }
2886 }
2887 }
2888 }
2889
2890 if (HasCopyToRegUses) {
2891 bool BothLiveOut = false;
2892 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2893 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002894 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002895 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002896 SDValue UseOp = User->getOperand(i);
Gabor Greifba36cb52008-08-28 21:40:38 +00002897 if (UseOp.getNode() == N && UseOp.getResNo() == 0) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002898 BothLiveOut = true;
2899 break;
2900 }
2901 }
2902 }
2903 if (BothLiveOut)
2904 // Both unextended and extended values are live out. There had better be
2905 // good a reason for the transformation.
2906 return ExtendNodes.size();
2907 }
2908 return true;
2909}
2910
Dan Gohman475871a2008-07-27 21:46:04 +00002911SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2912 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002913 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002914
Nate Begeman1d4d4142005-09-01 00:19:25 +00002915 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002916 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002917 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002918
Nate Begeman1d4d4142005-09-01 00:19:25 +00002919 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002920 // fold (sext (aext x)) -> (sext x)
2921 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002922 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002923
Chris Lattner22558872007-02-26 03:13:59 +00002924 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002925 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2926 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002927 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2928 if (NarrowLoad.getNode()) {
2929 if (NarrowLoad.getNode() != N0.getNode())
2930 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00002931 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2932 }
Evan Chengc88138f2007-03-22 01:54:19 +00002933
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002934 // See if the value being truncated is already sign extended. If so, just
2935 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002936 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002937 unsigned OpBits = Op.getValueType().getSizeInBits();
2938 unsigned MidBits = N0.getValueType().getSizeInBits();
2939 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002940 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002941
2942 if (OpBits == DestBits) {
2943 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2944 // bits, it is already ready.
2945 if (NumSignBits > DestBits-MidBits)
2946 return Op;
2947 } else if (OpBits < DestBits) {
2948 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2949 // bits, just sext from i32.
2950 if (NumSignBits > OpBits-MidBits)
2951 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2952 } else {
2953 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2954 // bits, just truncate to i32.
2955 if (NumSignBits > OpBits-MidBits)
2956 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002957 }
Chris Lattner22558872007-02-26 03:13:59 +00002958
2959 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00002960 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2961 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002962 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002963 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002964 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002965 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2966 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2967 DAG.getValueType(N0.getValueType()));
2968 }
Chris Lattner6007b842006-09-21 06:00:20 +00002969 }
Chris Lattner310b5782006-05-06 23:06:26 +00002970
Evan Cheng110dec22005-12-14 02:19:23 +00002971 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00002972 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002973 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00002974 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002975 bool DoXform = true;
2976 SmallVector<SDNode*, 4> SetCCs;
2977 if (!N0.hasOneUse())
2978 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2979 if (DoXform) {
2980 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002981 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00002982 LN0->getBasePtr(), LN0->getSrcValue(),
2983 LN0->getSrcValueOffset(),
2984 N0.getValueType(),
2985 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002986 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002987 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00002988 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002989 // Extend SetCC uses if necessary.
2990 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2991 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002992 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002993 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002994 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002995 if (SOp == Trunc)
2996 Ops.push_back(ExtLoad);
2997 else
2998 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2999 }
3000 Ops.push_back(SetCC->getOperand(2));
3001 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3002 &Ops[0], Ops.size()));
3003 }
Dan Gohman475871a2008-07-27 21:46:04 +00003004 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003005 }
Nate Begeman3df4d522005-10-12 20:40:40 +00003006 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003007
3008 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3009 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003010 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3011 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003012 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003013 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003014 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003015 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003016 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003017 LN0->getBasePtr(), LN0->getSrcValue(),
3018 LN0->getSrcValueOffset(), EVT,
3019 LN0->isVolatile(), LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003020 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003021 CombineTo(N0.getNode(),
3022 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003023 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003024 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00003025 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003026 }
3027
Chris Lattner20a35c32007-04-11 05:32:27 +00003028 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
3029 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003030 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003031 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3032 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
3033 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003034 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003035 }
3036
Dan Gohman8f0ad582008-04-28 16:58:24 +00003037 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00003038 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00003039 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00003040 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
3041
Dan Gohman475871a2008-07-27 21:46:04 +00003042 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003043}
3044
Dan Gohman475871a2008-07-27 21:46:04 +00003045SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3046 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003048
Nate Begeman1d4d4142005-09-01 00:19:25 +00003049 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00003050 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003051 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003052 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00003053 // fold (zext (aext x)) -> (zext x)
3054 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003055 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00003056
Evan Chengc88138f2007-03-22 01:54:19 +00003057 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3058 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00003059 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003060 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3061 if (NarrowLoad.getNode()) {
3062 if (NarrowLoad.getNode() != N0.getNode())
3063 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003064 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
3065 }
Evan Chengc88138f2007-03-22 01:54:19 +00003066 }
3067
Chris Lattner6007b842006-09-21 06:00:20 +00003068 // fold (zext (truncate x)) -> (and x, mask)
3069 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003070 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00003071 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003072 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003073 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003074 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00003075 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
3076 }
3077 return DAG.getZeroExtendInReg(Op, N0.getValueType());
3078 }
3079
Chris Lattner111c2282006-09-21 06:14:31 +00003080 // fold (zext (and (trunc x), cst)) -> (and x, cst).
3081 if (N0.getOpcode() == ISD::AND &&
3082 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3083 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003085 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003086 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003087 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00003088 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3089 }
Dan Gohman220a8232008-03-03 23:51:38 +00003090 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003091 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00003092 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3093 }
3094
Evan Cheng110dec22005-12-14 02:19:23 +00003095 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003096 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003097 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003098 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003099 bool DoXform = true;
3100 SmallVector<SDNode*, 4> SetCCs;
3101 if (!N0.hasOneUse())
3102 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3103 if (DoXform) {
3104 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003105 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003106 LN0->getBasePtr(), LN0->getSrcValue(),
3107 LN0->getSrcValueOffset(),
3108 N0.getValueType(),
3109 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003110 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00003111 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003112 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003113 // Extend SetCC uses if necessary.
3114 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3115 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00003116 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003117 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003118 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003119 if (SOp == Trunc)
3120 Ops.push_back(ExtLoad);
3121 else
Evan Chengde1631b2007-10-30 20:11:21 +00003122 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003123 }
3124 Ops.push_back(SetCC->getOperand(2));
3125 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
3126 &Ops[0], Ops.size()));
3127 }
Dan Gohman475871a2008-07-27 21:46:04 +00003128 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00003129 }
Evan Cheng110dec22005-12-14 02:19:23 +00003130 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003131
3132 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3133 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003134 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3135 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00003136 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003137 MVT EVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003138 if ((!LegalOperations && !LN0->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003139 TLI.isLoadExtLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003140 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003141 LN0->getBasePtr(), LN0->getSrcValue(),
3142 LN0->getSrcValueOffset(), EVT,
3143 LN0->isVolatile(), LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003144 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00003145 CombineTo(N0.getNode(),
3146 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003147 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003148 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003149 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003150 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003151
3152 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3153 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003154 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00003155 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3156 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003157 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003158 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003159 }
3160
Dan Gohman475871a2008-07-27 21:46:04 +00003161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003162}
3163
Dan Gohman475871a2008-07-27 21:46:04 +00003164SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3165 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003166 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003167
3168 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003169 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003170 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3171 // fold (aext (aext x)) -> (aext x)
3172 // fold (aext (zext x)) -> (zext x)
3173 // fold (aext (sext x)) -> (sext x)
3174 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3175 N0.getOpcode() == ISD::ZERO_EXTEND ||
3176 N0.getOpcode() == ISD::SIGN_EXTEND)
3177 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3178
Evan Chengc88138f2007-03-22 01:54:19 +00003179 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3180 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3181 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003182 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3183 if (NarrowLoad.getNode()) {
3184 if (NarrowLoad.getNode() != N0.getNode())
3185 CombineTo(N0.getNode(), NarrowLoad);
Evan Cheng0b063de2007-03-23 02:16:52 +00003186 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3187 }
Evan Chengc88138f2007-03-22 01:54:19 +00003188 }
3189
Chris Lattner84750582006-09-20 06:29:17 +00003190 // fold (aext (truncate x))
3191 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003192 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003193 if (TruncOp.getValueType() == VT)
3194 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003195 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003196 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3197 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3198 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003199
3200 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3201 if (N0.getOpcode() == ISD::AND &&
3202 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3203 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003205 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003206 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003207 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003208 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3209 }
Dan Gohman220a8232008-03-03 23:51:38 +00003210 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003211 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003212 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3213 }
3214
Chris Lattner5ffc0662006-05-05 05:58:59 +00003215 // fold (aext (load x)) -> (aext (truncate (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00003216 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003217 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003218 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003219 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003220 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003221 LN0->getBasePtr(), LN0->getSrcValue(),
3222 LN0->getSrcValueOffset(),
3223 N0.getValueType(),
3224 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003225 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003226 // Redirect any chain users to the new load.
Evan Cheng45299662008-08-29 23:20:46 +00003227 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1),
3228 SDValue(ExtLoad.getNode(), 1));
Dan Gohman75dcf082008-07-31 00:50:31 +00003229 // If any node needs the original loaded value, recompute it.
3230 if (!LN0->use_empty())
3231 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3232 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003233 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003234 }
3235
3236 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3237 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3238 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003239 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003240 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003241 N0.hasOneUse()) {
3242 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003243 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003244 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003245 LN0->getChain(), LN0->getBasePtr(),
3246 LN0->getSrcValue(),
3247 LN0->getSrcValueOffset(), EVT,
3248 LN0->isVolatile(), LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003249 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00003250 CombineTo(N0.getNode(),
3251 DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00003252 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003253 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003254 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003255
3256 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3257 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003258 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003259 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3260 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003261 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00003262 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003263 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003264 }
3265
Dan Gohman475871a2008-07-27 21:46:04 +00003266 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003267}
3268
Chris Lattner2b4c2792007-10-13 06:35:54 +00003269/// GetDemandedBits - See if the specified operand can be simplified with the
3270/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003271/// simpler operand, otherwise return a null SDValue.
3272SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003273 switch (V.getOpcode()) {
3274 default: break;
3275 case ISD::OR:
3276 case ISD::XOR:
3277 // If the LHS or RHS don't contribute bits to the or, drop them.
3278 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3279 return V.getOperand(1);
3280 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3281 return V.getOperand(0);
3282 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003283 case ISD::SRL:
3284 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003285 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00003286 break;
3287 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3288 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003289 unsigned Amt = RHSC->getZExtValue();
Dan Gohmancc91d632009-01-03 19:22:06 +00003290 // Watch out for shift count overflow though.
3291 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003292 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003293 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00003294 if (SimplifyLHS.getNode()) {
Chris Lattnere33544c2007-10-13 06:58:48 +00003295 return DAG.getNode(ISD::SRL, V.getValueType(),
3296 SimplifyLHS, V.getOperand(1));
3297 }
3298 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003299 }
Dan Gohman475871a2008-07-27 21:46:04 +00003300 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003301}
3302
Evan Chengc88138f2007-03-22 01:54:19 +00003303/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3304/// bits and then truncated to a narrower type and where N is a multiple
3305/// of number of bits of the narrower type, transform it to a narrower load
3306/// from address + N / num of bits of new type. If the result is to be
3307/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003308SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003309 unsigned Opc = N->getOpcode();
3310 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003311 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 MVT VT = N->getValueType(0);
3313 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003314
Dan Gohman7f8613e2008-08-14 20:04:46 +00003315 // This transformation isn't valid for vector loads.
3316 if (VT.isVector())
3317 return SDValue();
3318
Evan Chenge177e302007-03-23 22:13:36 +00003319 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3320 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003321 if (Opc == ISD::SIGN_EXTEND_INREG) {
3322 ExtType = ISD::SEXTLOAD;
3323 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00003324 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003325 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003326 }
3327
Duncan Sands83ec4b62008-06-06 12:08:01 +00003328 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003329 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003330 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003331 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3332 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003333 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003334 // Is the shift amount a multiple of size of VT?
3335 if ((ShAmt & (EVTBits-1)) == 0) {
3336 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003337 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003338 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003339 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003340 }
3341 }
3342 }
3343
Duncan Sandsad205a72008-06-16 08:14:38 +00003344 // Do not generate loads of non-round integer types since these can
3345 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003346 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3347 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003348 // Do not change the width of a volatile load.
3349 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003350 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003352 // For big endian targets, we need to adjust the offset to the pointer to
3353 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003354 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003355 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003356 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003357 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3358 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003359 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003360 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003361 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003362 DAG.getConstant(PtrOff, PtrType));
Gabor Greifba36cb52008-08-28 21:40:38 +00003363 AddToWorkList(NewPtr.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00003364 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003365 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003366 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003367 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003368 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003369 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3370 EVT, LN0->isVolatile(), NewAlign);
Dan Gohmanb6a61642009-01-16 21:47:21 +00003371 AddToWorkList(Load.getNode());
Evan Chengb37b80c2007-03-23 20:55:21 +00003372 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003373 WorkListRemover DeadNodes(*this);
3374 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3375 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00003376 CombineTo(N->getOperand(0).getNode(), Load);
Evan Chengb37b80c2007-03-23 20:55:21 +00003377 } else
Gabor Greifba36cb52008-08-28 21:40:38 +00003378 CombineTo(N0.getNode(), Load, Load.getValue(1));
Chris Lattnerdf874ee2009-01-16 21:15:56 +00003379
Evan Cheng15213b72007-03-26 07:12:51 +00003380 if (ShAmt) {
3381 if (Opc == ISD::SIGN_EXTEND_INREG)
3382 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3383 else
3384 return DAG.getNode(Opc, VT, Load);
3385 }
Dan Gohman475871a2008-07-27 21:46:04 +00003386 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003387 }
3388
Dan Gohman475871a2008-07-27 21:46:04 +00003389 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003390}
3391
Chris Lattner5ffc0662006-05-05 05:58:59 +00003392
Dan Gohman475871a2008-07-27 21:46:04 +00003393SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3394 SDValue N0 = N->getOperand(0);
3395 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003396 MVT VT = N->getValueType(0);
3397 MVT EVT = cast<VTSDNode>(N1)->getVT();
3398 unsigned VTBits = VT.getSizeInBits();
3399 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003400
Nate Begeman1d4d4142005-09-01 00:19:25 +00003401 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003402 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003403 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003404
Chris Lattner541a24f2006-05-06 22:43:44 +00003405 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003406 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003407 return N0;
3408
Nate Begeman646d7e22005-09-02 21:18:40 +00003409 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3410 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003411 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003412 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003413 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003414
Dan Gohman75dcf082008-07-31 00:50:31 +00003415 // fold (sext_in_reg (sext x)) -> (sext x)
3416 // fold (sext_in_reg (aext x)) -> (sext x)
3417 // if x is small enough.
3418 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3419 SDValue N00 = N0.getOperand(0);
3420 if (N00.getValueType().getSizeInBits() < EVTBits)
3421 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3422 }
3423
Chris Lattner95a5e052007-04-17 19:03:21 +00003424 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003425 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003426 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003427
Chris Lattner95a5e052007-04-17 19:03:21 +00003428 // fold operands of sext_in_reg based on knowledge that the top bits are not
3429 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003430 if (SimplifyDemandedBits(SDValue(N, 0)))
3431 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003432
Evan Chengc88138f2007-03-22 01:54:19 +00003433 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3434 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003435 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003436 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00003437 return NarrowLoad;
3438
Chris Lattner4b37e872006-05-08 21:18:59 +00003439 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3440 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3441 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3442 if (N0.getOpcode() == ISD::SRL) {
3443 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003444 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003445 // We can turn this into an SRA iff the input to the SRL is already sign
3446 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003447 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003448 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003449 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3450 }
3451 }
Evan Chengc88138f2007-03-22 01:54:19 +00003452
Nate Begemanded49632005-10-13 03:11:28 +00003453 // fold (sext_inreg (extload x)) -> (sextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00003454 if (ISD::isEXTLoad(N0.getNode()) &&
3455 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003456 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003457 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003458 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003459 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003460 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003461 LN0->getBasePtr(), LN0->getSrcValue(),
3462 LN0->getSrcValueOffset(), EVT,
3463 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003464 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003465 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003466 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003467 }
3468 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00003469 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003470 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003471 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003472 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00003473 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003474 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003475 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003476 LN0->getBasePtr(), LN0->getSrcValue(),
3477 LN0->getSrcValueOffset(), EVT,
3478 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003479 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00003480 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003481 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003482 }
Dan Gohman475871a2008-07-27 21:46:04 +00003483 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003484}
3485
Dan Gohman475871a2008-07-27 21:46:04 +00003486SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3487 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003488 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003489
3490 // noop truncate
3491 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003492 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003493 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003494 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003495 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003496 // fold (truncate (truncate x)) -> (truncate x)
3497 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003498 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003499 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003500 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3501 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003502 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003503 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003504 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003505 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003506 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003507 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003508 else
3509 // if the source and dest are the same type, we can drop both the extend
3510 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003511 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003512 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003513
Chris Lattner2b4c2792007-10-13 06:35:54 +00003514 // See if we can simplify the input to this truncate through knowledge that
3515 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3516 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003517 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003518 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003519 VT.getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003520 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00003521 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3522
Nate Begeman3df4d522005-10-12 20:40:40 +00003523 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003524 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003525 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003526}
3527
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003528static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003529 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003530 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00003531 return Elt.getNode();
3532 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003533}
3534
3535/// CombineConsecutiveLoads - build_pair (load, load) -> load
3536/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003537SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003538 assert(N->getOpcode() == ISD::BUILD_PAIR);
3539
3540 SDNode *LD1 = getBuildPairElt(N, 0);
3541 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003542 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003543 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003544 SDNode *LD2 = getBuildPairElt(N, 1);
3545 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3546 if (ISD::isNON_EXTLoad(LD2) &&
3547 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003548 // If both are volatile this would reduce the number of volatile loads.
3549 // If one is volatile it might be ok, but play conservative and bail out.
3550 !cast<LoadSDNode>(LD1)->isVolatile() &&
3551 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003553 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3554 unsigned Align = LD->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00003555 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003556 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003557 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003558 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003559 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3560 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003561 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003562 }
Dan Gohman475871a2008-07-27 21:46:04 +00003563 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003564}
3565
Dan Gohman475871a2008-07-27 21:46:04 +00003566SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3567 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003568 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003569
Dan Gohman7f321562007-06-25 16:23:39 +00003570 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3571 // Only do this before legalize, since afterward the target may be depending
3572 // on the bitconvert.
3573 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00003574 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003575 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003577 bool isSimple = true;
3578 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3579 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3580 N0.getOperand(i).getOpcode() != ISD::Constant &&
3581 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3582 isSimple = false;
3583 break;
3584 }
3585
Duncan Sands83ec4b62008-06-06 12:08:01 +00003586 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3587 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003588 "Element type of vector ValueType must not be vector!");
3589 if (isSimple) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003590 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00003591 }
3592 }
3593
Dan Gohman3dd168d2008-09-05 01:58:21 +00003594 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00003595 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003596 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003597 if (Res.getNode() != N) return Res;
Chris Lattner94683772005-12-23 05:30:37 +00003598 }
3599
Chris Lattnerc8547d82005-12-23 05:37:50 +00003600 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3601 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003602
Chris Lattner57104102005-12-23 05:44:41 +00003603 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003604 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00003605 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003606 // Do not change the width of a volatile load.
3607 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003608 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003609 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00003610 unsigned Align = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003611 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003612 unsigned OrigAlign = LN0->getAlignment();
3613 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003614 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sands25cf2272008-11-24 14:53:14 +00003615 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3616 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003617 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00003618 CombineTo(N0.getNode(),
3619 DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00003620 Load.getValue(1));
3621 return Load;
3622 }
Chris Lattner57104102005-12-23 05:44:41 +00003623 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003624
Chris Lattner3bd39d42008-01-27 17:42:27 +00003625 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3626 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3627 // This often reduces constant pool loads.
3628 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003629 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003630 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00003631 AddToWorkList(NewConv.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003632
Duncan Sands83ec4b62008-06-06 12:08:01 +00003633 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003634 if (N0.getOpcode() == ISD::FNEG)
3635 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3636 assert(N0.getOpcode() == ISD::FABS);
3637 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3638 }
3639
3640 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3641 // Note that we don't handle copysign(x,cst) because this can always be folded
3642 // to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00003643 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003644 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003645 VT.isInteger() && !VT.isVector()) {
3646 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Duncan Sands25cf2272008-11-24 14:53:14 +00003647 MVT IntXVT = MVT::getIntegerVT(OrigXWidth);
3648 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
3649 SDValue X = DAG.getNode(ISD::BIT_CONVERT, IntXVT, N0.getOperand(1));
3650 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003651
Duncan Sands25cf2272008-11-24 14:53:14 +00003652 // If X has a different width than the result/lhs, sext it or truncate it.
3653 unsigned VTWidth = VT.getSizeInBits();
3654 if (OrigXWidth < VTWidth) {
3655 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3656 AddToWorkList(X.getNode());
3657 } else if (OrigXWidth > VTWidth) {
3658 // To get the sign bit in the right place, we have to shift it right
3659 // before truncating.
3660 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3661 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3662 AddToWorkList(X.getNode());
3663 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3664 AddToWorkList(X.getNode());
3665 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003666
Duncan Sands25cf2272008-11-24 14:53:14 +00003667 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
3668 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3669 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003670
Duncan Sands25cf2272008-11-24 14:53:14 +00003671 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3672 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3673 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003674
Duncan Sands25cf2272008-11-24 14:53:14 +00003675 return DAG.getNode(ISD::OR, VT, X, Cst);
3676 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003677 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003678
3679 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3680 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003681 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3682 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003683 return CombineLD;
3684 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003685
Dan Gohman475871a2008-07-27 21:46:04 +00003686 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003687}
3688
Dan Gohman475871a2008-07-27 21:46:04 +00003689SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003690 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003691 return CombineConsecutiveLoads(N, VT);
3692}
3693
Dan Gohman7f321562007-06-25 16:23:39 +00003694/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003695/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3696/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003697SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003698ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3699 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003700
3701 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003702 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003703
Duncan Sands83ec4b62008-06-06 12:08:01 +00003704 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3705 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003706
3707 // If this is a conversion of N elements of one type to N elements of another
3708 // type, convert each element. This handles FP<->INT cases.
3709 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003710 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003711 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003712 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Gabor Greifba36cb52008-08-28 21:40:38 +00003713 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00003714 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 MVT VT = MVT::getVectorVT(DstEltVT,
3716 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003717 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003718 }
3719
3720 // Otherwise, we're growing or shrinking the elements. To avoid having to
3721 // handle annoying details of growing/shrinking FP values, we convert them to
3722 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003723 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003724 // Convert the input float vector to a int vector where the elements are the
3725 // same sizes.
3726 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003727 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003728 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003729 SrcEltVT = IntVT;
3730 }
3731
3732 // Now we know the input is an integer vector. If the output is a FP type,
3733 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003734 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003735 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003736 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Gabor Greifba36cb52008-08-28 21:40:38 +00003737 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00003738
3739 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003740 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003741 }
3742
3743 // Okay, we know the src/dst types are both integers of differing types.
3744 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003745 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003746 if (SrcBitSize < DstBitSize) {
3747 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3748
Dan Gohman475871a2008-07-27 21:46:04 +00003749 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003750 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003751 i += NumInputsPerOutput) {
3752 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003753 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003754 bool EltIsUndef = true;
3755 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3756 // Shift the previously computed bits over.
3757 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003758 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003759 if (Op.getOpcode() == ISD::UNDEF) continue;
3760 EltIsUndef = false;
3761
Dan Gohman220a8232008-03-03 23:51:38 +00003762 NewBits |=
3763 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003764 }
3765
3766 if (EltIsUndef)
3767 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3768 else
3769 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3770 }
3771
Duncan Sands83ec4b62008-06-06 12:08:01 +00003772 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003773 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003774 }
3775
3776 // Finally, this must be the case where we are shrinking elements: each input
3777 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003778 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003779 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003780 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003781 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003782 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003783 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3784 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3785 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3786 continue;
3787 }
Dan Gohman220a8232008-03-03 23:51:38 +00003788 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003789 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003790 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003791 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003792 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003793 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3794 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003795 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003796 }
3797
3798 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003799 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003800 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3801 }
Dan Gohman7f321562007-06-25 16:23:39 +00003802 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003803}
3804
3805
3806
Dan Gohman475871a2008-07-27 21:46:04 +00003807SDValue DAGCombiner::visitFADD(SDNode *N) {
3808 SDValue N0 = N->getOperand(0);
3809 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003810 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3811 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003812 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003813
Dan Gohman7f321562007-06-25 16:23:39 +00003814 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003815 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003816 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003817 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003818 }
Dan Gohman7f321562007-06-25 16:23:39 +00003819
Nate Begemana0e221d2005-10-18 00:28:13 +00003820 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003821 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003822 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003823 // canonicalize constant to RHS
3824 if (N0CFP && !N1CFP)
3825 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003826 // fold (A + (-B)) -> A-B
Duncan Sands25cf2272008-11-24 14:53:14 +00003827 if (isNegatibleForFree(N1, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003828 return DAG.getNode(ISD::FSUB, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003829 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner01b3d732005-09-28 22:28:18 +00003830 // fold ((-A) + B) -> B-A
Duncan Sands25cf2272008-11-24 14:53:14 +00003831 if (isNegatibleForFree(N0, LegalOperations) == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003832 return DAG.getNode(ISD::FSUB, VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00003833 GetNegatedExpression(N0, DAG, LegalOperations));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003834
3835 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3836 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003837 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003838 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3839 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3840
Dan Gohman475871a2008-07-27 21:46:04 +00003841 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003842}
3843
Dan Gohman475871a2008-07-27 21:46:04 +00003844SDValue DAGCombiner::visitFSUB(SDNode *N) {
3845 SDValue N0 = N->getOperand(0);
3846 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003847 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3848 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003849 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003850
Dan Gohman7f321562007-06-25 16:23:39 +00003851 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003852 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003853 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003854 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003855 }
Dan Gohman7f321562007-06-25 16:23:39 +00003856
Nate Begemana0e221d2005-10-18 00:28:13 +00003857 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003858 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003859 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003860 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003861 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sands25cf2272008-11-24 14:53:14 +00003862 if (isNegatibleForFree(N1, LegalOperations))
3863 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00003864 return DAG.getNode(ISD::FNEG, VT, N1);
3865 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003866 // fold (A-(-B)) -> A+B
Duncan Sands25cf2272008-11-24 14:53:14 +00003867 if (isNegatibleForFree(N1, LegalOperations))
Chris Lattner0254e702008-02-26 07:04:54 +00003868 return DAG.getNode(ISD::FADD, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00003869 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003870
Dan Gohman475871a2008-07-27 21:46:04 +00003871 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003872}
3873
Dan Gohman475871a2008-07-27 21:46:04 +00003874SDValue DAGCombiner::visitFMUL(SDNode *N) {
3875 SDValue N0 = N->getOperand(0);
3876 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003877 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3878 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003879 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003880
Dan Gohman7f321562007-06-25 16:23:39 +00003881 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003883 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003884 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003885 }
Dan Gohman7f321562007-06-25 16:23:39 +00003886
Nate Begeman11af4ea2005-10-17 20:40:11 +00003887 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003888 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003889 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003890 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003891 if (N0CFP && !N1CFP)
3892 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003893 // fold (fmul X, 2.0) -> (fadd X, X)
3894 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3895 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003896 // fold (fmul X, -1.0) -> (fneg X)
3897 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3898 return DAG.getNode(ISD::FNEG, VT, N0);
3899
3900 // -X * -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003901 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3902 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003903 // Both can be negated for free, check to see if at least one is cheaper
3904 // negated.
3905 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003906 return DAG.getNode(ISD::FMUL, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003907 GetNegatedExpression(N0, DAG, LegalOperations),
3908 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003909 }
3910 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003911
3912 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3913 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00003914 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003915 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3916 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3917
Dan Gohman475871a2008-07-27 21:46:04 +00003918 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003919}
3920
Dan Gohman475871a2008-07-27 21:46:04 +00003921SDValue DAGCombiner::visitFDIV(SDNode *N) {
3922 SDValue N0 = N->getOperand(0);
3923 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003924 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3925 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003926 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003927
Dan Gohman7f321562007-06-25 16:23:39 +00003928 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003929 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003930 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003931 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003932 }
Dan Gohman7f321562007-06-25 16:23:39 +00003933
Nate Begemana148d982006-01-18 22:35:16 +00003934 // fold (fdiv c1, c2) -> 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::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003937
3938
3939 // -X / -Y -> X*Y
Duncan Sands25cf2272008-11-24 14:53:14 +00003940 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
3941 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Chris Lattner29446522007-05-14 22:04:50 +00003942 // Both can be negated for free, check to see if at least one is cheaper
3943 // negated.
3944 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003945 return DAG.getNode(ISD::FDIV, VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00003946 GetNegatedExpression(N0, DAG, LegalOperations),
3947 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00003948 }
3949 }
3950
Dan Gohman475871a2008-07-27 21:46:04 +00003951 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003952}
3953
Dan Gohman475871a2008-07-27 21:46:04 +00003954SDValue DAGCombiner::visitFREM(SDNode *N) {
3955 SDValue N0 = N->getOperand(0);
3956 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003957 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3958 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003959 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003960
Nate Begemana148d982006-01-18 22:35:16 +00003961 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003962 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003963 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003964
Dan Gohman475871a2008-07-27 21:46:04 +00003965 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003966}
3967
Dan Gohman475871a2008-07-27 21:46:04 +00003968SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3969 SDValue N0 = N->getOperand(0);
3970 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003971 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3972 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003973 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003974
Dale Johannesendb44bf82007-10-16 23:38:29 +00003975 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003976 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3977
3978 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003979 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003980 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3981 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003982 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003983 return DAG.getNode(ISD::FABS, VT, N0);
3984 else
3985 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3986 }
3987
3988 // copysign(fabs(x), y) -> copysign(x, y)
3989 // copysign(fneg(x), y) -> copysign(x, y)
3990 // copysign(copysign(x,z), y) -> copysign(x, y)
3991 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3992 N0.getOpcode() == ISD::FCOPYSIGN)
3993 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3994
3995 // copysign(x, abs(y)) -> abs(x)
3996 if (N1.getOpcode() == ISD::FABS)
3997 return DAG.getNode(ISD::FABS, VT, N0);
3998
3999 // copysign(x, copysign(y,z)) -> copysign(x, z)
4000 if (N1.getOpcode() == ISD::FCOPYSIGN)
4001 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
4002
4003 // copysign(x, fp_extend(y)) -> copysign(x, y)
4004 // copysign(x, fp_round(y)) -> copysign(x, y)
4005 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
4006 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
4007
Dan Gohman475871a2008-07-27 21:46:04 +00004008 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00004009}
4010
4011
Chris Lattner01b3d732005-09-28 22:28:18 +00004012
Dan Gohman475871a2008-07-27 21:46:04 +00004013SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4014 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004015 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004016 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004017 MVT OpVT = N0.getValueType();
4018
Nate Begeman1d4d4142005-09-01 00:19:25 +00004019 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004020 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004021 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004022
4023 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4024 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00004025 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00004026 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
4027 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
4028 if (DAG.SignBitIsZero(N0))
4029 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
4030 }
4031
4032
Dan Gohman475871a2008-07-27 21:46:04 +00004033 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004034}
4035
Dan Gohman475871a2008-07-27 21:46:04 +00004036SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4037 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00004038 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004039 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00004040 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00004041
Nate Begeman1d4d4142005-09-01 00:19:25 +00004042 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00004043 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004044 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00004045
4046 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4047 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00004048 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00004049 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
4050 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
4051 if (DAG.SignBitIsZero(N0))
4052 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
4053 }
4054
Dan Gohman475871a2008-07-27 21:46:04 +00004055 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004056}
4057
Dan Gohman475871a2008-07-27 21:46:04 +00004058SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4059 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004060 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004061 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004062
4063 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00004064 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00004065 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004066 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004067}
4068
Dan Gohman475871a2008-07-27 21:46:04 +00004069SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4070 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004071 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004072 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004073
4074 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00004075 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004076 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004077 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004078}
4079
Dan Gohman475871a2008-07-27 21:46:04 +00004080SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4081 SDValue N0 = N->getOperand(0);
4082 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00004083 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004084 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004085
4086 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004087 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00004088 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00004089
4090 // fold (fp_round (fp_extend x)) -> x
4091 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4092 return N0.getOperand(0);
4093
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004094 // fold (fp_round (fp_round x)) -> (fp_round x)
4095 if (N0.getOpcode() == ISD::FP_ROUND) {
4096 // This is a value preserving truncation if both round's are.
4097 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004098 N0.getNode()->getConstantOperandVal(1) == 1;
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00004099 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
4100 DAG.getIntPtrConstant(IsTrunc));
4101 }
4102
Chris Lattner79dbea52006-03-13 06:26:26 +00004103 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00004104 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004105 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00004106 AddToWorkList(Tmp.getNode());
Chris Lattner79dbea52006-03-13 06:26:26 +00004107 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
4108 }
4109
Dan Gohman475871a2008-07-27 21:46:04 +00004110 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004111}
4112
Dan Gohman475871a2008-07-27 21:46:04 +00004113SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4114 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004115 MVT VT = N->getValueType(0);
4116 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00004117 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004118
Nate Begeman1d4d4142005-09-01 00:19:25 +00004119 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sands25cf2272008-11-24 14:53:14 +00004120 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00004121 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00004122 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004123 }
Dan Gohman475871a2008-07-27 21:46:04 +00004124 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004125}
4126
Dan Gohman475871a2008-07-27 21:46:04 +00004127SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4128 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004129 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004130 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004131
Chris Lattner5938bef2007-12-29 06:55:23 +00004132 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00004133 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00004134 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
4135 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004136
Nate Begeman1d4d4142005-09-01 00:19:25 +00004137 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00004138 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004139 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004140
4141 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4142 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00004143 if (N0.getOpcode() == ISD::FP_ROUND
4144 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00004145 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00004146 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00004147 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00004148 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
4149 return DAG.getNode(ISD::FP_EXTEND, VT, In);
4150 }
4151
4152 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004153 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004154 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004155 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00004156 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004157 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004158 LN0->getBasePtr(), LN0->getSrcValue(),
4159 LN0->getSrcValueOffset(),
4160 N0.getValueType(),
4161 LN0->isVolatile(), LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00004162 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004163 CombineTo(N0.getNode(), DAG.getNode(ISD::FP_ROUND, N0.getValueType(),
4164 ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00004165 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004166 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00004167 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004168
Dan Gohman475871a2008-07-27 21:46:04 +00004169 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004170}
4171
Dan Gohman475871a2008-07-27 21:46:04 +00004172SDValue DAGCombiner::visitFNEG(SDNode *N) {
4173 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004174
Duncan Sands25cf2272008-11-24 14:53:14 +00004175 if (isNegatibleForFree(N0, LegalOperations))
4176 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00004177
Chris Lattner3bd39d42008-01-27 17:42:27 +00004178 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4179 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004180 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 N0.getOperand(0).getValueType().isInteger() &&
4182 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004183 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004184 MVT IntVT = Int.getValueType();
4185 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004186 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004187 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004188 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004189 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4190 }
4191 }
4192
Dan Gohman475871a2008-07-27 21:46:04 +00004193 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004194}
4195
Dan Gohman475871a2008-07-27 21:46:04 +00004196SDValue DAGCombiner::visitFABS(SDNode *N) {
4197 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004198 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004199 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004200
Nate Begeman1d4d4142005-09-01 00:19:25 +00004201 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004202 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004203 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004204 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004205 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004206 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004207 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004208 // fold (fabs (fcopysign x, y)) -> (fabs x)
4209 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4210 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4211
Chris Lattner3bd39d42008-01-27 17:42:27 +00004212 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4213 // constant pool values.
Gabor Greifba36cb52008-08-28 21:40:38 +00004214 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004215 N0.getOperand(0).getValueType().isInteger() &&
4216 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004217 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004218 MVT IntVT = Int.getValueType();
4219 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004220 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004221 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00004222 AddToWorkList(Int.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00004223 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4224 }
4225 }
4226
Dan Gohman475871a2008-07-27 21:46:04 +00004227 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004228}
4229
Dan Gohman475871a2008-07-27 21:46:04 +00004230SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4231 SDValue Chain = N->getOperand(0);
4232 SDValue N1 = N->getOperand(1);
4233 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004234 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4235
4236 // never taken branch, fold to chain
4237 if (N1C && N1C->isNullValue())
4238 return Chain;
4239 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004240 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004241 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004242 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4243 // on the target.
4244 if (N1.getOpcode() == ISD::SETCC &&
4245 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4246 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4247 N1.getOperand(0), N1.getOperand(1), N2);
4248 }
Dan Gohman475871a2008-07-27 21:46:04 +00004249 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004250}
4251
Chris Lattner3ea0b472005-10-05 06:47:48 +00004252// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4253//
Dan Gohman475871a2008-07-27 21:46:04 +00004254SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004255 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004256 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004257
Duncan Sands8eab8a22008-06-09 11:32:28 +00004258 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00004259 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Duncan Sands25cf2272008-11-24 14:53:14 +00004260 CondLHS, CondRHS, CC->get(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004261 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00004262
Gabor Greifba36cb52008-08-28 21:40:38 +00004263 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Nate Begemane17daeb2005-10-05 21:43:42 +00004264
4265 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004266 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004267 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4268 N->getOperand(4));
4269 // fold br_cc false, dest -> unconditional fall through
4270 if (SCCC && SCCC->isNullValue())
4271 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004272
Nate Begemane17daeb2005-10-05 21:43:42 +00004273 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00004274 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Nate Begemane17daeb2005-10-05 21:43:42 +00004275 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4276 Simp.getOperand(2), Simp.getOperand(0),
4277 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004278 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004279}
4280
Chris Lattner448f2192006-11-11 00:39:41 +00004281
Duncan Sandsec87aa82008-06-15 20:12:31 +00004282/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4283/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004284/// and it has other uses besides the load / store. After the
4285/// transformation, the new indexed load / store has effectively folded
4286/// the add / subtract in and all of its other uses are redirected to the
4287/// new load / store.
4288bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004289 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004290 return false;
4291
4292 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004293 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004294 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004295 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004296 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004297 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004298 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004299 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004300 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4301 return false;
4302 Ptr = LD->getBasePtr();
4303 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004304 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004305 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004306 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004307 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4308 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4309 return false;
4310 Ptr = ST->getBasePtr();
4311 isLoad = false;
4312 } else
4313 return false;
4314
Chris Lattner9f1794e2006-11-11 00:56:29 +00004315 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4316 // out. There is no reason to make this a preinc/predec.
4317 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00004318 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004319 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004320
Chris Lattner9f1794e2006-11-11 00:56:29 +00004321 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004322 SDValue BasePtr;
4323 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004324 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4325 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4326 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004327 // Don't create a indexed load / store with zero offset.
4328 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004329 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004330 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004331
Chris Lattner41e53fd2006-11-11 01:00:15 +00004332 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004333 // 1) The new base ptr is a frame index.
4334 // 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 +00004335 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004336 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004337 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004338 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004339
Chris Lattner41e53fd2006-11-11 01:00:15 +00004340 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4341 // (plus the implicit offset) to a register to preinc anyway.
4342 if (isa<FrameIndexSDNode>(BasePtr))
4343 return false;
4344
4345 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004346 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004347 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004348 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004349 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004350 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004351
Evan Chengc843abe2007-05-24 02:35:39 +00004352 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004353 bool RealUse = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004354 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4355 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004356 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004357 if (Use == N)
4358 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004359 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004360 return false;
4361
4362 if (!((Use->getOpcode() == ISD::LOAD &&
4363 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004364 (Use->getOpcode() == ISD::STORE &&
4365 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004366 RealUse = true;
4367 }
4368 if (!RealUse)
4369 return false;
4370
Dan Gohman475871a2008-07-27 21:46:04 +00004371 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004372 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004373 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004374 else
Dan Gohman475871a2008-07-27 21:46:04 +00004375 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004376 ++PreIndexedNodes;
4377 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004378 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004379 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004380 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004381 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004382 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004383 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004384 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004385 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004386 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004387 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004388 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004389 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004390 }
4391
Chris Lattner9f1794e2006-11-11 00:56:29 +00004392 // Finally, since the node is now dead, remove it from the graph.
4393 DAG.DeleteNode(N);
4394
4395 // Replace the uses of Ptr with uses of the updated base value.
4396 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004397 &DeadNodes);
Gabor Greifba36cb52008-08-28 21:40:38 +00004398 removeFromWorkList(Ptr.getNode());
4399 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00004400
4401 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004402}
4403
Duncan Sandsec87aa82008-06-15 20:12:31 +00004404/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004405/// add / sub of the base pointer node into a post-indexed load / store.
4406/// The transformation folded the add / subtract into the new indexed
4407/// load / store effectively and all of its uses are redirected to the
4408/// new load / store.
4409bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sands25cf2272008-11-24 14:53:14 +00004410 if (!LegalOperations)
Chris Lattner448f2192006-11-11 00:39:41 +00004411 return false;
4412
4413 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004414 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004415 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004416 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004417 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004418 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004419 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004420 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4421 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4422 return false;
4423 Ptr = LD->getBasePtr();
4424 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004425 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004426 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004427 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004428 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4429 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4430 return false;
4431 Ptr = ST->getBasePtr();
4432 isLoad = false;
4433 } else
4434 return false;
4435
Gabor Greifba36cb52008-08-28 21:40:38 +00004436 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004437 return false;
4438
Gabor Greifba36cb52008-08-28 21:40:38 +00004439 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4440 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004441 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004442 if (Op == N ||
4443 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4444 continue;
4445
Dan Gohman475871a2008-07-27 21:46:04 +00004446 SDValue BasePtr;
4447 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004448 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4449 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4450 if (Ptr == Offset)
4451 std::swap(BasePtr, Offset);
4452 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004453 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004454 // Don't create a indexed load / store with zero offset.
4455 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004456 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004457 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004458
Chris Lattner9f1794e2006-11-11 00:56:29 +00004459 // Try turning it into a post-indexed load / store except when
4460 // 1) All uses are load / store ops that use it as base ptr.
4461 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4462 // nor a successor of N. Otherwise, if Op is folded that would
4463 // create a cycle.
4464
4465 // Check for #1.
4466 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00004467 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4468 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004469 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00004470 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00004471 continue;
4472
Chris Lattner9f1794e2006-11-11 00:56:29 +00004473 // If all the uses are load / store addresses, then don't do the
4474 // transformation.
4475 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4476 bool RealUse = false;
4477 for (SDNode::use_iterator III = Use->use_begin(),
4478 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004479 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004480 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004481 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004482 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004483 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004484 RealUse = true;
4485 }
Chris Lattner448f2192006-11-11 00:39:41 +00004486
Chris Lattner9f1794e2006-11-11 00:56:29 +00004487 if (!RealUse) {
4488 TryNext = true;
4489 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004490 }
4491 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004492 }
4493 if (TryNext)
4494 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004495
Chris Lattner9f1794e2006-11-11 00:56:29 +00004496 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004497 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004498 SDValue Result = isLoad
4499 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4500 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004501 ++PostIndexedNodes;
4502 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004503 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004504 DOUT << "\nWith: "; DEBUG(Result.getNode()->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004505 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004506 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004507 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004508 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004509 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004510 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004511 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004512 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004513 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004514 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004515 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004516
Chris Lattner9f1794e2006-11-11 00:56:29 +00004517 // Finally, since the node is now dead, remove it from the graph.
4518 DAG.DeleteNode(N);
4519
4520 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004521 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004522 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004523 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004524 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004525 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004526 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004527 }
4528 }
4529 }
4530 return false;
4531}
4532
Chris Lattner00161a62008-01-25 07:20:16 +00004533/// InferAlignment - If we can infer some alignment information from this
4534/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004535static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004536 // If this is a direct reference to a stack slot, use information about the
4537 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004538 int FrameIdx = 1 << 31;
4539 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004540 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004541 FrameIdx = FI->getIndex();
4542 } else if (Ptr.getOpcode() == ISD::ADD &&
4543 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4544 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4545 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4546 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004547 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004548
4549 if (FrameIdx != (1 << 31)) {
4550 // FIXME: Handle FI+CST.
4551 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4552 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004553 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004554
4555 // The alignment of the frame index can be determined from its offset from
4556 // the incoming frame position. If the frame object is at offset 32 and
4557 // the stack is guaranteed to be 16-byte aligned, then we know that the
4558 // object is 16-byte aligned.
4559 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4560 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4561
4562 // Finally, the frame object itself may have a known alignment. Factor
4563 // the alignment + offset into a new alignment. For example, if we know
4564 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4565 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4566 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4567 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4568 FrameOffset);
4569 return std::max(Align, FIInfoAlign);
4570 }
4571 }
Chris Lattner00161a62008-01-25 07:20:16 +00004572
4573 return 0;
4574}
Chris Lattner448f2192006-11-11 00:39:41 +00004575
Dan Gohman475871a2008-07-27 21:46:04 +00004576SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004577 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004578 SDValue Chain = LD->getChain();
4579 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004580
4581 // Try to infer better alignment information than the load already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004582 if (!Fast && LD->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004583 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4584 if (Align > LD->getAlignment())
4585 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4586 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004587 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004588 LD->isVolatile(), Align);
4589 }
4590 }
4591
Evan Cheng45a7ca92007-05-01 00:38:21 +00004592
4593 // If load is not volatile and there are no uses of the loaded value (and
4594 // the updated indexed value in case of indexed loads), change uses of the
4595 // chain value into uses of the chain input (i.e. delete the dead load).
4596 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004597 if (N->getValueType(1) == MVT::Other) {
4598 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004599 if (N->hasNUsesOfValue(0, 0)) {
4600 // It's not safe to use the two value CombineTo variant here. e.g.
4601 // v1, chain2 = load chain1, loc
4602 // v2, chain3 = load chain2, loc
4603 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004604 // Now we replace use of chain2 with chain1. This makes the second load
4605 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004606 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004607 DOUT << "\nWith chain: "; DEBUG(Chain.getNode()->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004608 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004609 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004610 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004611 if (N->use_empty()) {
4612 removeFromWorkList(N);
4613 DAG.DeleteNode(N);
4614 }
Dan Gohman475871a2008-07-27 21:46:04 +00004615 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004616 }
Evan Cheng498f5592007-05-01 08:53:39 +00004617 } else {
4618 // Indexed loads.
4619 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4620 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004622 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Gabor Greifba36cb52008-08-28 21:40:38 +00004623 DOUT << "\nWith: "; DEBUG(Undef.getNode()->dump(&DAG));
Evan Cheng02c42852008-01-16 23:11:54 +00004624 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004625 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004626 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4627 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004628 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004629 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004630 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004631 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004632 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004633 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004634 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004635 }
4636 }
Chris Lattner01a22022005-10-10 22:04:48 +00004637
4638 // If this load is directly stored, replace the load value with the stored
4639 // value.
4640 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004641 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004642 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4643 !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004644 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004645 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4646 if (PrevST->getBasePtr() == Ptr &&
4647 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004648 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004649 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004650 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004651
Jim Laskey7ca56af2006-10-11 13:47:09 +00004652 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004653 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004654 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004655
Jim Laskey6ff23e52006-10-04 16:53:27 +00004656 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004657 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004659
Jim Laskey279f0532006-09-25 16:29:54 +00004660 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004661 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4662 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004663 LD->getSrcValue(), LD->getSrcValueOffset(),
4664 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004665 } else {
4666 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4667 LD->getValueType(0),
4668 BetterChain, Ptr, LD->getSrcValue(),
4669 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004670 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004671 LD->isVolatile(),
4672 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004673 }
Jim Laskey279f0532006-09-25 16:29:54 +00004674
Jim Laskey6ff23e52006-10-04 16:53:27 +00004675 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004676 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004677 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004678
Jim Laskey274062c2006-10-13 23:32:28 +00004679 // Replace uses with load result and token factor. Don't add users
4680 // to work list.
4681 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004682 }
4683 }
4684
Evan Cheng7fc033a2006-11-03 03:06:21 +00004685 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004686 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004687 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004688
Dan Gohman475871a2008-07-27 21:46:04 +00004689 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004690}
4691
Chris Lattner07649d92008-01-08 23:08:06 +00004692
Dan Gohman475871a2008-07-27 21:46:04 +00004693SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004694 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004695 SDValue Chain = ST->getChain();
4696 SDValue Value = ST->getValue();
4697 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004698
Chris Lattner00161a62008-01-25 07:20:16 +00004699 // Try to infer better alignment information than the store already has.
Dan Gohmana2676512008-08-20 16:30:28 +00004700 if (!Fast && ST->isUnindexed()) {
Chris Lattner00161a62008-01-25 07:20:16 +00004701 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4702 if (Align > ST->getAlignment())
4703 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004704 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004705 ST->isVolatile(), Align);
4706 }
4707 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004708
Evan Cheng59d5b682007-05-07 21:27:48 +00004709 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004710 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004711 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004712 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004713 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004714 MVT SVT = Value.getOperand(0).getValueType();
Dan Gohman6448d912008-09-04 15:39:15 +00004715 unsigned OrigAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004716 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004717 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004718 ((!LegalOperations && !ST->isVolatile()) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004719 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004720 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004721 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004722 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004723
Nate Begeman2cbba892006-12-11 02:23:46 +00004724 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004725 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004726 // NOTE: If the original store is volatile, this transform must not increase
4727 // the number of stores. For example, on x86-32 an f64 can be stored in one
4728 // processor operation but an i64 (which is not legal) requires two. So the
4729 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004730 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004731 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004732 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004733 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004734 case MVT::f80: // We don't do this for these yet.
4735 case MVT::f128:
4736 case MVT::ppcf128:
4737 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004738 case MVT::f32:
Duncan Sands25cf2272008-11-24 14:53:14 +00004739 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
4740 !ST->isVolatile()) || TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004741 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00004742 bitcastToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004743 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004744 ST->getSrcValueOffset(), ST->isVolatile(),
4745 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004746 }
4747 break;
4748 case MVT::f64:
Duncan Sands25cf2272008-11-24 14:53:14 +00004749 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
4750 !ST->isVolatile()) || TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00004751 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004752 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004753 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004754 ST->getSrcValueOffset(), ST->isVolatile(),
4755 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004756 } else if (!ST->isVolatile() &&
4757 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004758 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004759 // argument passing. Since this is so common, custom legalize the
4760 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00004761 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004762 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4763 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004764 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004765
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004766 int SVOffset = ST->getSrcValueOffset();
4767 unsigned Alignment = ST->getAlignment();
4768 bool isVolatile = ST->isVolatile();
4769
Dan Gohman475871a2008-07-27 21:46:04 +00004770 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004771 ST->getSrcValueOffset(),
4772 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004773 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4774 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004775 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004776 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004777 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004778 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004779 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4780 }
4781 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004782 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004783 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004784 }
4785
Jim Laskey279f0532006-09-25 16:29:54 +00004786 if (CombinerAA) {
4787 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004788 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004789
Jim Laskey6ff23e52006-10-04 16:53:27 +00004790 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004791 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004792 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004793 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004794 if (ST->isTruncatingStore()) {
4795 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004796 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004797 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004798 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004799 } else {
4800 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004801 ST->getSrcValue(), ST->getSrcValueOffset(),
4802 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004803 }
4804
Jim Laskey279f0532006-09-25 16:29:54 +00004805 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004807 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4808
4809 // Don't add users to work list.
4810 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004811 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004812 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004813
Evan Cheng33dbedc2006-11-05 09:31:14 +00004814 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004815 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004816 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004817
Chris Lattner3c872852007-12-29 06:26:16 +00004818 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004819 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004820 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004821 // See if we can simplify the input to this truncstore with knowledge that
4822 // only the low bits are being used. For example:
4823 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004824 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004825 GetDemandedBits(Value,
4826 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004827 ST->getMemoryVT().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00004828 AddToWorkList(Value.getNode());
4829 if (Shorter.getNode())
Chris Lattner2b4c2792007-10-13 06:35:54 +00004830 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004831 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004832 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004833
4834 // Otherwise, see if we can simplify the operation with
4835 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004836 if (SimplifyDemandedBits(Value,
4837 APInt::getLowBitsSet(
4838 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004839 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004840 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004841 }
4842
Chris Lattner3c872852007-12-29 06:26:16 +00004843 // If this is a load followed by a store to the same location, then the store
4844 // is dead/noop.
4845 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004846 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004847 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004848 // There can't be any side effects between the load and store, such as
4849 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004850 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004851 // The store is dead, remove it.
4852 return Chain;
4853 }
4854 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004855
Chris Lattnerddf89562008-01-17 19:59:44 +00004856 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4857 // truncating store. We can do this even if this is already a truncstore.
4858 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00004859 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004860 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004861 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004862 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004863 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004864 ST->isVolatile(), ST->getAlignment());
4865 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004866
Dan Gohman475871a2008-07-27 21:46:04 +00004867 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004868}
4869
Dan Gohman475871a2008-07-27 21:46:04 +00004870SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4871 SDValue InVec = N->getOperand(0);
4872 SDValue InVal = N->getOperand(1);
4873 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004874
4875 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4876 // vector with the inserted element.
4877 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004878 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greif12632d22008-08-30 19:29:20 +00004879 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
4880 InVec.getNode()->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004881 if (Elt < Ops.size())
4882 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004883 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4884 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004885 }
4886
Dan Gohman475871a2008-07-27 21:46:04 +00004887 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004888}
4889
Dan Gohman475871a2008-07-27 21:46:04 +00004890SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004891 // (vextract (scalar_to_vector val, 0) -> val
4892 SDValue InVec = N->getOperand(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004893
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004894 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR)
4895 return InVec.getOperand(0);
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004896
4897 // Perform only after legalization to ensure build_vector / vector_shuffle
4898 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00004899 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004900
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00004901 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4902 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4903 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wange3bc6ae2009-01-18 06:43:40 +00004904 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004905
Evan Cheng513da432007-10-06 08:19:55 +00004906 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004907 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00004908 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00004909 bool BCNumEltsChanged = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004910 MVT VT = InVec.getValueType();
4911 MVT EVT = VT.getVectorElementType();
4912 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004913 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004914 MVT BCVT = InVec.getOperand(0).getValueType();
Mon P Wanga60b5232008-12-11 00:26:16 +00004915 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004916 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00004917 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
4918 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004919 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004920 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004921 NewLoad = true;
4922 }
Evan Cheng513da432007-10-06 08:19:55 +00004923
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004924 LoadSDNode *LN0 = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004925 if (ISD::isNormalLoad(InVec.getNode()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004926 LN0 = cast<LoadSDNode>(InVec);
4927 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4928 InVec.getOperand(0).getValueType() == EVT &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004929 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004930 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4931 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4932 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4933 // =>
4934 // (load $addr+1*size)
Mon P Wanga60b5232008-12-11 00:26:16 +00004935
4936 // If the bit convert changed the number of elements, it is unsafe
4937 // to examine the mask.
4938 if (BCNumEltsChanged)
4939 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004940 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004941 getOperand(Elt))->getZExtValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004942 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4943 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4944 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4945 InVec = InVec.getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00004946 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004947 LN0 = cast<LoadSDNode>(InVec);
4948 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004949 }
4950 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004951 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004952 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004953
4954 unsigned Align = LN0->getAlignment();
4955 if (NewLoad) {
4956 // Check the resultant load doesn't need a higher alignment than the
4957 // original load.
Dan Gohman6448d912008-09-04 15:39:15 +00004958 unsigned NewAlign = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004959 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004960 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004961 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004962 Align = NewAlign;
4963 }
4964
Dan Gohman475871a2008-07-27 21:46:04 +00004965 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004966 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004967 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4968 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004969 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004970 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004971 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4972 DAG.getConstant(PtrOff, PtrType));
4973 }
4974 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4975 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4976 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004977 }
Dan Gohman475871a2008-07-27 21:46:04 +00004978 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004979}
4980
4981
Dan Gohman475871a2008-07-27 21:46:04 +00004982SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004983 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004984 MVT VT = N->getValueType(0);
4985 unsigned NumElts = VT.getVectorNumElements();
4986 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004987
Dan Gohman7f321562007-06-25 16:23:39 +00004988 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4989 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4990 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004991 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004992 for (unsigned i = 0; i != NumInScalars; ++i) {
4993 // Ignore undef inputs.
4994 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4995
Dan Gohman7f321562007-06-25 16:23:39 +00004996 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004997 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004998 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004999 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005000 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005001 break;
5002 }
5003
Dan Gohman7f321562007-06-25 16:23:39 +00005004 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00005005 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005006 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005007 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005008 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005009 break;
5010 }
5011
5012 // Otherwise, remember this. We allow up to two distinct input vectors.
5013 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5014 continue;
5015
Gabor Greifba36cb52008-08-28 21:40:38 +00005016 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005017 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00005018 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00005019 VecIn2 = ExtractedFromVec;
5020 } else {
5021 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005022 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005023 break;
5024 }
5025 }
5026
5027 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00005028 if (VecIn1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00005030 for (unsigned i = 0; i != NumInScalars; ++i) {
5031 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00005032 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005033 continue;
5034 }
5035
Dan Gohman475871a2008-07-27 21:46:04 +00005036 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005037
5038 // If extracting from the first vector, just use the index directly.
5039 if (Extract.getOperand(0) == VecIn1) {
5040 BuildVecIndices.push_back(Extract.getOperand(1));
5041 continue;
5042 }
5043
5044 // Otherwise, use InIdx + VecSize
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005045 unsigned Idx =
5046 cast<ConstantSDNode>(Extract.getOperand(1))->getZExtValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00005047 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00005048 }
5049
5050 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005051 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Duncan Sands25cf2272008-11-24 14:53:14 +00005052 if (!TLI.isTypeLegal(BuildVecVT) && LegalTypes)
5053 return SDValue();
5054
Dan Gohman7f321562007-06-25 16:23:39 +00005055 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00005056 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005057 Ops[0] = VecIn1;
Gabor Greifba36cb52008-08-28 21:40:38 +00005058 if (VecIn2.getNode()) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005059 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00005060 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005061 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005062 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00005063 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00005064 EltType));
5065 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005066 &UnOps[0], UnOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005067 AddToWorkList(Ops[1].getNode());
Chris Lattnercef896e2006-03-28 22:19:47 +00005068 }
Dan Gohman7f321562007-06-25 16:23:39 +00005069 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005070 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005071 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00005072 }
5073
Dan Gohman475871a2008-07-27 21:46:04 +00005074 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00005075}
5076
Dan Gohman475871a2008-07-27 21:46:04 +00005077SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005078 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5079 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5080 // inputs come from at most two distinct vectors, turn this into a shuffle
5081 // node.
5082
5083 // If we only have one input vector, we don't need to do any concatenation.
5084 if (N->getNumOperands() == 1) {
5085 return N->getOperand(0);
5086 }
5087
Dan Gohman475871a2008-07-27 21:46:04 +00005088 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005089}
5090
Dan Gohman475871a2008-07-27 21:46:04 +00005091SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
5092 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005093 unsigned NumElts = ShufMask.getNumOperands();
5094
Mon P Wangaeb06d22008-11-10 04:46:22 +00005095 SDValue N0 = N->getOperand(0);
5096 SDValue N1 = N->getOperand(1);
5097
5098 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5099 "Vector shuffle must be normalized in DAG");
5100
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005101 // If the shuffle mask is an identity operation on the LHS, return the LHS.
5102 bool isIdentity = true;
5103 for (unsigned i = 0; i != NumElts; ++i) {
5104 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005105 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() != i) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005106 isIdentity = false;
5107 break;
5108 }
5109 }
5110 if (isIdentity) return N->getOperand(0);
5111
5112 // If the shuffle mask is an identity operation on the RHS, return the RHS.
5113 isIdentity = true;
5114 for (unsigned i = 0; i != NumElts; ++i) {
5115 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005116 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() !=
5117 i+NumElts) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005118 isIdentity = false;
5119 break;
5120 }
5121 }
5122 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00005123
5124 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
5125 // needed at all.
5126 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00005127 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005128 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00005129 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00005130 for (unsigned i = 0; i != NumElts; ++i)
5131 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005132 unsigned Idx=cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue();
Evan Chenge7bec0d2006-07-20 22:44:41 +00005133 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00005134 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00005135 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00005136 BaseIdx = Idx;
5137 } else {
5138 if (BaseIdx != Idx)
5139 isSplat = false;
5140 if (VecNum != V) {
5141 isUnary = false;
5142 break;
5143 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00005144 }
5145 }
5146
Evan Chenge7bec0d2006-07-20 22:44:41 +00005147 // Normalize unary shuffle so the RHS is undef.
5148 if (isUnary && VecNum == 1)
5149 std::swap(N0, N1);
5150
Evan Cheng917ec982006-07-21 08:25:53 +00005151 // If it is a splat, check if the argument vector is a build_vector with
5152 // all scalar elements the same.
5153 if (isSplat) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005154 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00005155
Dan Gohman7f321562007-06-25 16:23:39 +00005156 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00005157 // not the number of vector elements, look through it. Be careful not to
5158 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00005159 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005160 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00005161 if (ConvInput.getValueType().isVector() &&
5162 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00005163 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00005164 }
5165
Dan Gohman7f321562007-06-25 16:23:39 +00005166 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5167 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00005168 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00005169 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00005170 bool AllSame = true;
5171 for (unsigned i = 0; i != NumElems; ++i) {
5172 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5173 Base = V->getOperand(i);
5174 break;
5175 }
5176 }
5177 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greifba36cb52008-08-28 21:40:38 +00005178 if (!Base.getNode())
Evan Cheng917ec982006-07-21 08:25:53 +00005179 return N0;
5180 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00005181 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00005182 AllSame = false;
5183 break;
5184 }
5185 }
5186 // Splat of <x, x, x, x>, return <x, x, x, x>
5187 if (AllSame)
5188 return N0;
5189 }
5190 }
5191 }
5192
Evan Chenge7bec0d2006-07-20 22:44:41 +00005193 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5194 // into an undef.
5195 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005196 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5197 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005199 for (unsigned i = 0; i != NumElts; ++i) {
5200 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005201 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() <
5202 NumElts) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005203 MappedOps.push_back(ShufMask.getOperand(i));
5204 } else {
5205 unsigned NewIdx =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005206 cast<ConstantSDNode>(ShufMask.getOperand(i))->getZExtValue() -
5207 NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005208 MappedOps.push_back(DAG.getConstant(NewIdx,
5209 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005210 }
5211 }
Dan Gohman7f321562007-06-25 16:23:39 +00005212 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005213 &MappedOps[0], MappedOps.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00005214 AddToWorkList(ShufMask.getNode());
Dan Gohman7f321562007-06-25 16:23:39 +00005215 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5216 N0,
5217 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5218 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005219 }
Dan Gohman7f321562007-06-25 16:23:39 +00005220
Dan Gohman475871a2008-07-27 21:46:04 +00005221 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005222}
5223
Evan Cheng44f1f092006-04-20 08:56:16 +00005224/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005225/// an AND to a vector_shuffle with the destination vector and a zero vector.
5226/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005227/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005228SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5229 SDValue LHS = N->getOperand(0);
5230 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005231 if (N->getOpcode() == ISD::AND) {
5232 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005233 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005234 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005235 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005236 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005237 unsigned NumElts = NumOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005238 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005239 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005240 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005241 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005242 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005243 IdxOps.push_back(DAG.getIntPtrConstant(i));
Evan Cheng44f1f092006-04-20 08:56:16 +00005244 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands94989ac2008-10-19 14:58:05 +00005245 IdxOps.push_back(DAG.getIntPtrConstant(NumElts));
Evan Cheng44f1f092006-04-20 08:56:16 +00005246 else
Dan Gohman475871a2008-07-27 21:46:04 +00005247 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005248 }
5249
5250 // Let's see if the target supports this vector_shuffle.
Duncan Sands94989ac2008-10-19 14:58:05 +00005251 if (!TLI.isVectorClearMaskLegal(IdxOps, TLI.getPointerTy(), DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005252 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005253
Dan Gohman7f321562007-06-25 16:23:39 +00005254 // Return the new VECTOR_SHUFFLE node.
Duncan Sands94989ac2008-10-19 14:58:05 +00005255 MVT EVT = RHS.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005256 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng3eb57d52008-11-05 06:04:18 +00005257 MVT MaskVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005258 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005259 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005260 Ops.push_back(LHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00005261 AddToWorkList(LHS.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005262 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005263 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005264 &ZeroOps[0], ZeroOps.size()));
Evan Cheng3eb57d52008-11-05 06:04:18 +00005265 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005266 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005267 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005268 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005269 if (VT != N->getValueType(0))
5270 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005271 return Result;
5272 }
5273 }
Dan Gohman475871a2008-07-27 21:46:04 +00005274 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005275}
5276
Dan Gohman7f321562007-06-25 16:23:39 +00005277/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005278SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005279 // After legalize, the target may be depending on adds and other
5280 // binary ops to provide legal ways to construct constants or other
5281 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00005282 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005283
Duncan Sands83ec4b62008-06-06 12:08:01 +00005284 MVT VT = N->getValueType(0);
5285 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005286
Duncan Sands83ec4b62008-06-06 12:08:01 +00005287 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005288 SDValue LHS = N->getOperand(0);
5289 SDValue RHS = N->getOperand(1);
5290 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005291 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00005292
Dan Gohman7f321562007-06-25 16:23:39 +00005293 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005294 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005295 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5296 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005297 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005298 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005299 SDValue LHSOp = LHS.getOperand(i);
5300 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005301 // If these two elements can't be folded, bail out.
5302 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5303 LHSOp.getOpcode() != ISD::Constant &&
5304 LHSOp.getOpcode() != ISD::ConstantFP) ||
5305 (RHSOp.getOpcode() != ISD::UNDEF &&
5306 RHSOp.getOpcode() != ISD::Constant &&
5307 RHSOp.getOpcode() != ISD::ConstantFP))
5308 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005309 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005310 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5311 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005312 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005313 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00005314 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005315 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005316 break;
5317 }
Dan Gohman7f321562007-06-25 16:23:39 +00005318 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Gabor Greifba36cb52008-08-28 21:40:38 +00005319 AddToWorkList(Ops.back().getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00005320 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5321 Ops.back().getOpcode() == ISD::Constant ||
5322 Ops.back().getOpcode() == ISD::ConstantFP) &&
5323 "Scalar binop didn't fold!");
5324 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005325
Dan Gohman7f321562007-06-25 16:23:39 +00005326 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005327 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005328 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005329 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005330 }
5331
Dan Gohman475871a2008-07-27 21:46:04 +00005332 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005333}
5334
Dan Gohman475871a2008-07-27 21:46:04 +00005335SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005336 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5337
Dan Gohman475871a2008-07-27 21:46:04 +00005338 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005339 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5340 // If we got a simplified select_cc node back from SimplifySelectCC, then
5341 // break it down into a new SETCC node, and a new SELECT node, and then return
5342 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00005343 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005344 // Check to see if we got a select_cc back (to turn into setcc/select).
5345 // Otherwise, just return whatever node we got back, like fabs.
5346 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005347 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005348 SCC.getOperand(0), SCC.getOperand(1),
5349 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00005350 AddToWorkList(SETCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005351 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5352 SCC.getOperand(3), SETCC);
5353 }
5354 return SCC;
5355 }
Dan Gohman475871a2008-07-27 21:46:04 +00005356 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005357}
5358
Chris Lattner40c62d52005-10-18 06:04:22 +00005359/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5360/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005361/// select. Callers of this should assume that TheSelect is deleted if this
5362/// returns true. As such, they should return the appropriate thing (e.g. the
5363/// node) back to the top-level of the DAG combiner loop to avoid it being
5364/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005365///
Dan Gohman475871a2008-07-27 21:46:04 +00005366bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5367 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005368
5369 // If this is a select from two identical things, try to pull the operation
5370 // through the select.
5371 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005372 // If this is a load and the token chain is identical, replace the select
5373 // of two loads with a load through a select of the address to load from.
5374 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5375 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005376 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005377 // Do not let this transformation reduce the number of volatile loads.
5378 !cast<LoadSDNode>(LHS)->isVolatile() &&
5379 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005380 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005381 LHS.getOperand(0) == RHS.getOperand(0)) {
5382 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5383 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5384
5385 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005386 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005387 // FIXME: this conflates two src values, discarding one. This is not
5388 // the right thing to do, but nothing uses srcvalues now. When they do,
5389 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005390 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005391 if (TheSelect->getOpcode() == ISD::SELECT) {
5392 // Check that the condition doesn't reach either load. If so, folding
5393 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005394 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5395 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005396 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5397 TheSelect->getOperand(0), LLD->getBasePtr(),
5398 RLD->getBasePtr());
5399 }
5400 } else {
5401 // Check that the condition doesn't reach either load. If so, folding
5402 // this will induce a cycle into the DAG.
Gabor Greifba36cb52008-08-28 21:40:38 +00005403 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5404 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5405 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5406 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005407 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005408 TheSelect->getOperand(0),
5409 TheSelect->getOperand(1),
5410 LLD->getBasePtr(), RLD->getBasePtr(),
5411 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005412 }
Evan Cheng466685d2006-10-09 20:57:25 +00005413 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005414
Gabor Greifba36cb52008-08-28 21:40:38 +00005415 if (Addr.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005417 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5418 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5419 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005420 LLD->getSrcValueOffset(),
5421 LLD->isVolatile(),
5422 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005423 else {
5424 Load = DAG.getExtLoad(LLD->getExtensionType(),
5425 TheSelect->getValueType(0),
5426 LLD->getChain(), Addr, LLD->getSrcValue(),
5427 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005428 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005429 LLD->isVolatile(),
5430 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005431 }
5432 // Users of the select now use the result of the load.
5433 CombineTo(TheSelect, Load);
5434
5435 // Users of the old loads now use the new load's chain. We know the
5436 // old-load value is dead now.
Gabor Greifba36cb52008-08-28 21:40:38 +00005437 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5438 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005439 return true;
5440 }
Evan Chengc5484282006-10-04 00:56:09 +00005441 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005442 }
5443 }
5444
5445 return false;
5446}
5447
Dan Gohman475871a2008-07-27 21:46:04 +00005448SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5449 SDValue N2, SDValue N3,
5450 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005451
Duncan Sands83ec4b62008-06-06 12:08:01 +00005452 MVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00005453 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5454 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5455 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005456
5457 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00005458 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
5459 N0, N1, CC, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00005460 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5461 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005462
5463 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005464 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005465 return N2;
5466 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005467 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005468 return N3;
5469
5470 // Check to see if we can simplify the select into an fabs node
5471 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5472 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005473 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005474 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5475 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5476 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5477 N2 == N3.getOperand(0))
5478 return DAG.getNode(ISD::FABS, VT, N0);
5479
5480 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5481 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5482 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5483 N2.getOperand(0) == N3)
5484 return DAG.getNode(ISD::FABS, VT, N3);
5485 }
5486 }
5487
5488 // Check to see if we can perform the "gzip trick", transforming
5489 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005490 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005491 N0.getValueType().isInteger() &&
5492 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005493 (N1C->isNullValue() || // (a < 0) ? b : 0
5494 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005495 MVT XType = N0.getValueType();
5496 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005497 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005498 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005499 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005500 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5501 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005502 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005503 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5504 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00005505 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005506 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005507 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005508 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005509 }
5510 return DAG.getNode(ISD::AND, AType, Shift, N2);
5511 }
Dan Gohman475871a2008-07-27 21:46:04 +00005512 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005513 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005514 TLI.getShiftAmountTy()));
Gabor Greifba36cb52008-08-28 21:40:38 +00005515 AddToWorkList(Shift.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00005516 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005517 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005518 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005519 }
5520 return DAG.getNode(ISD::AND, AType, Shift, N2);
5521 }
5522 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005523
5524 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005525 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands03228082008-11-23 15:47:28 +00005526 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005527
5528 // If the caller doesn't want us to simplify this into a zext of a compare,
5529 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005530 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005531 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005532
Nate Begeman07ed4172005-10-10 21:26:48 +00005533 // Get a SetCC of the condition
5534 // FIXME: Should probably make sure that setcc is legal if we ever have a
5535 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005536 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005537 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00005538 if (LegalTypes) {
Duncan Sands5480c042009-01-01 15:52:00 +00005539 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0.getValueType()),
5540 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005541 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005542 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5543 else
5544 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005545 } else {
5546 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005547 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005548 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005549 AddToWorkList(SCC.getNode());
5550 AddToWorkList(Temp.getNode());
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005551
Dan Gohman002e5d02008-03-13 22:13:53 +00005552 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005553 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005554 // shl setcc result by log2 n2c
5555 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005556 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005557 TLI.getShiftAmountTy()));
5558 }
5559
Nate Begemanf845b452005-10-08 00:29:44 +00005560 // Check to see if this is the equivalent of setcc
5561 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5562 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005563 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005564 MVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00005565 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00005566 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
5567 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005568 if (Res.getValueType() != VT)
5569 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5570 return Res;
5571 }
5572
5573 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5574 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005575 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00005576 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005578 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005579 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005580 TLI.getShiftAmountTy()));
5581 }
5582 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5583 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005584 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005585 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005587 DAG.getConstant(~0ULL, XType));
5588 return DAG.getNode(ISD::SRL, XType,
5589 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005590 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005591 TLI.getShiftAmountTy()));
5592 }
5593 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5594 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005595 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005596 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005597 TLI.getShiftAmountTy()));
5598 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5599 }
5600 }
5601
5602 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5603 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5604 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005605 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005606 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5607 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005608 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005609 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005610 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005611 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005612 AddToWorkList(Shift.getNode());
5613 AddToWorkList(Add.getNode());
Chris Lattner1982ef22007-04-11 05:11:38 +00005614 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5615 }
5616 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5617 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5618 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5619 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5620 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005621 MVT XType = N0.getValueType();
5622 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005623 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005624 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005625 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005626 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00005627 AddToWorkList(Shift.getNode());
5628 AddToWorkList(Add.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00005629 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5630 }
5631 }
5632 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005633
Dan Gohman475871a2008-07-27 21:46:04 +00005634 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005635}
5636
Evan Chengfa1eb272007-02-08 22:13:59 +00005637/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005638SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5639 SDValue N1, ISD::CondCode Cond,
5640 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005641 TargetLowering::DAGCombinerInfo
Duncan Sands25cf2272008-11-24 14:53:14 +00005642 DagCombineInfo(DAG, Level == Unrestricted, false, this);
Evan Chengfa1eb272007-02-08 22:13:59 +00005643 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005644}
5645
Nate Begeman69575232005-10-20 02:15:44 +00005646/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5647/// return a DAG expression to select that will generate the same value by
5648/// multiplying by a magic number. See:
5649/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005650SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005651 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005652 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005653
Andrew Lenharth232c9102006-06-12 16:07:18 +00005654 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005655 ii != ee; ++ii)
5656 AddToWorkList(*ii);
5657 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005658}
5659
5660/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5661/// return a DAG expression to select that will generate the same value by
5662/// multiplying by a magic number. See:
5663/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005664SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005665 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005666 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005667
Andrew Lenharth232c9102006-06-12 16:07:18 +00005668 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005669 ii != ee; ++ii)
5670 AddToWorkList(*ii);
5671 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005672}
5673
Jim Laskey71382342006-10-07 23:37:56 +00005674/// FindBaseOffset - Return true if base is known not to alias with anything
5675/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005676static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005677 // Assume it is a primitive operation.
5678 Base = Ptr; Offset = 0;
5679
5680 // If it's an adding a simple constant then integrate the offset.
5681 if (Base.getOpcode() == ISD::ADD) {
5682 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5683 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005684 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00005685 }
5686 }
5687
5688 // If it's any of the following then it can't alias with anything but itself.
5689 return isa<FrameIndexSDNode>(Base) ||
5690 isa<ConstantPoolSDNode>(Base) ||
5691 isa<GlobalAddressSDNode>(Base);
5692}
5693
5694/// isAlias - Return true if there is any possibility that the two addresses
5695/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005696bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005697 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005698 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005699 const Value *SrcValue2, int SrcValueOffset2)
5700{
Jim Laskey71382342006-10-07 23:37:56 +00005701 // If they are the same then they must be aliases.
5702 if (Ptr1 == Ptr2) return true;
5703
5704 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005705 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005706 int64_t Offset1, Offset2;
5707 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5708 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5709
5710 // If they have a same base address then...
5711 if (Base1 == Base2) {
5712 // Check to see if the addresses overlap.
5713 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5714 }
5715
Jim Laskey096c22e2006-10-18 12:29:57 +00005716 // If we know both bases then they can't alias.
5717 if (KnownBase1 && KnownBase2) return false;
5718
Jim Laskey07a27092006-10-18 19:08:31 +00005719 if (CombinerGlobalAA) {
5720 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005721 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5722 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5723 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005724 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005725 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005726 if (AAResult == AliasAnalysis::NoAlias)
5727 return false;
5728 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005729
5730 // Otherwise we have to assume they alias.
5731 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005732}
5733
5734/// FindAliasInfo - Extracts the relevant alias information from the memory
5735/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005736bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005737 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005738 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005739 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5740 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005741 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005742 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005743 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005744 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005745 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005746 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005747 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005748 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005749 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005750 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005751 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005752 }
5753
5754 return false;
5755}
5756
Jim Laskey6ff23e52006-10-04 16:53:27 +00005757/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5758/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005759void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5760 SmallVector<SDValue, 8> &Aliases) {
5761 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005762 std::set<SDNode *> Visited; // Visited node set.
5763
Jim Laskey279f0532006-09-25 16:29:54 +00005764 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005765 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005766 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005767 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005768 int SrcValueOffset;
5769 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005770
Jim Laskey6ff23e52006-10-04 16:53:27 +00005771 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005772 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005773
Jim Laskeybc588b82006-10-05 15:07:25 +00005774 // Look at each chain and determine if it is an alias. If so, add it to the
5775 // aliases list. If not, then continue up the chain looking for the next
5776 // candidate.
5777 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005778 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005779 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005780
Jim Laskeybc588b82006-10-05 15:07:25 +00005781 // Don't bother if we've been before.
Gabor Greifba36cb52008-08-28 21:40:38 +00005782 if (Visited.find(Chain.getNode()) != Visited.end()) continue;
5783 Visited.insert(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005784
5785 switch (Chain.getOpcode()) {
5786 case ISD::EntryToken:
5787 // Entry token is ideal chain operand, but handled in FindBetterChain.
5788 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005789
Jim Laskeybc588b82006-10-05 15:07:25 +00005790 case ISD::LOAD:
5791 case ISD::STORE: {
5792 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005793 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005794 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005795 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005796 int OpSrcValueOffset;
Gabor Greifba36cb52008-08-28 21:40:38 +00005797 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Jim Laskey096c22e2006-10-18 12:29:57 +00005798 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005799
5800 // If chain is alias then stop here.
5801 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005802 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5803 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005804 Aliases.push_back(Chain);
5805 } else {
5806 // Look further up the chain.
5807 Chains.push_back(Chain.getOperand(0));
5808 // Clean up old chain.
Gabor Greifba36cb52008-08-28 21:40:38 +00005809 AddToWorkList(Chain.getNode());
Jim Laskey279f0532006-09-25 16:29:54 +00005810 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005811 break;
5812 }
5813
5814 case ISD::TokenFactor:
5815 // We have to check each of the operands of the token factor, so we queue
5816 // then up. Adding the operands to the queue (stack) in reverse order
5817 // maintains the original order and increases the likelihood that getNode
5818 // will find a matching token factor (CSE.)
5819 for (unsigned n = Chain.getNumOperands(); n;)
5820 Chains.push_back(Chain.getOperand(--n));
5821 // Eliminate the token factor if we can.
Gabor Greifba36cb52008-08-28 21:40:38 +00005822 AddToWorkList(Chain.getNode());
Jim Laskeybc588b82006-10-05 15:07:25 +00005823 break;
5824
5825 default:
5826 // For all other instructions we will just have to take what we can get.
5827 Aliases.push_back(Chain);
5828 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005829 }
5830 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005831}
5832
5833/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5834/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005835SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5836 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005837
Jim Laskey6ff23e52006-10-04 16:53:27 +00005838 // Accumulate all the aliases to this node.
5839 GatherAllAliases(N, OldChain, Aliases);
5840
5841 if (Aliases.size() == 0) {
5842 // If no operands then chain to entry token.
5843 return DAG.getEntryNode();
5844 } else if (Aliases.size() == 1) {
5845 // If a single operand then chain to it. We don't need to revisit it.
5846 return Aliases[0];
5847 }
5848
5849 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005850 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005851 &Aliases[0], Aliases.size());
5852
5853 // Make sure the old chain gets cleaned up.
Gabor Greifba36cb52008-08-28 21:40:38 +00005854 if (NewChain != OldChain) AddToWorkList(OldChain.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00005855
5856 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005857}
5858
Nate Begeman1d4d4142005-09-01 00:19:25 +00005859// SelectionDAG::Combine - This is the entry point for the file.
5860//
Duncan Sands25cf2272008-11-24 14:53:14 +00005861void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA, bool Fast) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005862 /// run - This is the main entry point to this class.
5863 ///
Duncan Sands25cf2272008-11-24 14:53:14 +00005864 DAGCombiner(*this, AA, Fast).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005865}